dr_wav.h 236 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434
  1. /*
  2. WAV audio loader and writer. Choice of public domain or MIT-0. See license statements at the end of this file.
  3. dr_wav - v0.12.16 - 2020-12-02
  4. David Reid - mackron@gmail.com
  5. GitHub: https://github.com/mackron/dr_libs
  6. */
  7. /*
  8. RELEASE NOTES - VERSION 0.12
  9. ============================
  10. Version 0.12 includes breaking changes to custom chunk handling.
  11. Changes to Chunk Callback
  12. -------------------------
  13. dr_wav supports the ability to fire a callback when a chunk is encounted (except for WAVE and FMT chunks). The callback has been updated to include both the
  14. container (RIFF or Wave64) and the FMT chunk which contains information about the format of the data in the wave file.
  15. Previously, there was no direct way to determine the container, and therefore no way to discriminate against the different IDs in the chunk header (RIFF and
  16. Wave64 containers encode chunk ID's differently). The `container` parameter can be used to know which ID to use.
  17. Sometimes it can be useful to know the data format at the time the chunk callback is fired. A pointer to a `drwav_fmt` object is now passed into the chunk
  18. callback which will give you information about the data format. To determine the sample format, use `drwav_fmt_get_format()`. This will return one of the
  19. `DR_WAVE_FORMAT_*` tokens.
  20. */
  21. /*
  22. Introduction
  23. ============
  24. This is a single file library. To use it, do something like the following in one .c file.
  25. ```c
  26. #define DR_WAV_IMPLEMENTATION
  27. #include "dr_wav.h"
  28. ```
  29. You can then #include this file in other parts of the program as you would with any other header file. Do something like the following to read audio data:
  30. ```c
  31. drwav wav;
  32. if (!drwav_init_file(&wav, "my_song.wav", NULL)) {
  33. // Error opening WAV file.
  34. }
  35. drwav_int32* pDecodedInterleavedPCMFrames = malloc(wav.totalPCMFrameCount * wav.channels * sizeof(drwav_int32));
  36. size_t numberOfSamplesActuallyDecoded = drwav_read_pcm_frames_s32(&wav, wav.totalPCMFrameCount, pDecodedInterleavedPCMFrames);
  37. ...
  38. drwav_uninit(&wav);
  39. ```
  40. If you just want to quickly open and read the audio data in a single operation you can do something like this:
  41. ```c
  42. unsigned int channels;
  43. unsigned int sampleRate;
  44. drwav_uint64 totalPCMFrameCount;
  45. float* pSampleData = drwav_open_file_and_read_pcm_frames_f32("my_song.wav", &channels, &sampleRate, &totalPCMFrameCount, NULL);
  46. if (pSampleData == NULL) {
  47. // Error opening and reading WAV file.
  48. }
  49. ...
  50. drwav_free(pSampleData);
  51. ```
  52. The examples above use versions of the API that convert the audio data to a consistent format (32-bit signed PCM, in this case), but you can still output the
  53. audio data in its internal format (see notes below for supported formats):
  54. ```c
  55. size_t framesRead = drwav_read_pcm_frames(&wav, wav.totalPCMFrameCount, pDecodedInterleavedPCMFrames);
  56. ```
  57. You can also read the raw bytes of audio data, which could be useful if dr_wav does not have native support for a particular data format:
  58. ```c
  59. size_t bytesRead = drwav_read_raw(&wav, bytesToRead, pRawDataBuffer);
  60. ```
  61. dr_wav can also be used to output WAV files. This does not currently support compressed formats. To use this, look at `drwav_init_write()`,
  62. `drwav_init_file_write()`, etc. Use `drwav_write_pcm_frames()` to write samples, or `drwav_write_raw()` to write raw data in the "data" chunk.
  63. ```c
  64. drwav_data_format format;
  65. format.container = drwav_container_riff; // <-- drwav_container_riff = normal WAV files, drwav_container_w64 = Sony Wave64.
  66. format.format = DR_WAVE_FORMAT_PCM; // <-- Any of the DR_WAVE_FORMAT_* codes.
  67. format.channels = 2;
  68. format.sampleRate = 44100;
  69. format.bitsPerSample = 16;
  70. drwav_init_file_write(&wav, "data/recording.wav", &format, NULL);
  71. ...
  72. drwav_uint64 framesWritten = drwav_write_pcm_frames(pWav, frameCount, pSamples);
  73. ```
  74. dr_wav has seamless support the Sony Wave64 format. The decoder will automatically detect it and it should Just Work without any manual intervention.
  75. Build Options
  76. =============
  77. #define these options before including this file.
  78. #define DR_WAV_NO_CONVERSION_API
  79. Disables conversion APIs such as `drwav_read_pcm_frames_f32()` and `drwav_s16_to_f32()`.
  80. #define DR_WAV_NO_STDIO
  81. Disables APIs that initialize a decoder from a file such as `drwav_init_file()`, `drwav_init_file_write()`, etc.
  82. Notes
  83. =====
  84. - Samples are always interleaved.
  85. - The default read function does not do any data conversion. Use `drwav_read_pcm_frames_f32()`, `drwav_read_pcm_frames_s32()` and `drwav_read_pcm_frames_s16()`
  86. to read and convert audio data to 32-bit floating point, signed 32-bit integer and signed 16-bit integer samples respectively. Tested and supported internal
  87. formats include the following:
  88. - Unsigned 8-bit PCM
  89. - Signed 12-bit PCM
  90. - Signed 16-bit PCM
  91. - Signed 24-bit PCM
  92. - Signed 32-bit PCM
  93. - IEEE 32-bit floating point
  94. - IEEE 64-bit floating point
  95. - A-law and u-law
  96. - Microsoft ADPCM
  97. - IMA ADPCM (DVI, format code 0x11)
  98. - dr_wav will try to read the WAV file as best it can, even if it's not strictly conformant to the WAV format.
  99. */
  100. #ifndef dr_wav_h
  101. #define dr_wav_h
  102. #ifdef __cplusplus
  103. extern "C" {
  104. #endif
  105. #define DRWAV_STRINGIFY(x) #x
  106. #define DRWAV_XSTRINGIFY(x) DRWAV_STRINGIFY(x)
  107. #define DRWAV_VERSION_MAJOR 0
  108. #define DRWAV_VERSION_MINOR 12
  109. #define DRWAV_VERSION_REVISION 16
  110. #define DRWAV_VERSION_STRING DRWAV_XSTRINGIFY(DRWAV_VERSION_MAJOR) "." DRWAV_XSTRINGIFY(DRWAV_VERSION_MINOR) "." DRWAV_XSTRINGIFY(DRWAV_VERSION_REVISION)
  111. #include <stddef.h> /* For size_t. */
  112. /* Sized types. */
  113. typedef signed char drwav_int8;
  114. typedef unsigned char drwav_uint8;
  115. typedef signed short drwav_int16;
  116. typedef unsigned short drwav_uint16;
  117. typedef signed int drwav_int32;
  118. typedef unsigned int drwav_uint32;
  119. #if defined(_MSC_VER)
  120. typedef signed __int64 drwav_int64;
  121. typedef unsigned __int64 drwav_uint64;
  122. #else
  123. #if defined(__clang__) || (defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)))
  124. #pragma GCC diagnostic push
  125. #pragma GCC diagnostic ignored "-Wlong-long"
  126. #if defined(__clang__)
  127. #pragma GCC diagnostic ignored "-Wc++11-long-long"
  128. #endif
  129. #endif
  130. typedef signed long long drwav_int64;
  131. typedef unsigned long long drwav_uint64;
  132. #if defined(__clang__) || (defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)))
  133. #pragma GCC diagnostic pop
  134. #endif
  135. #endif
  136. #if defined(__LP64__) || defined(_WIN64) || (defined(__x86_64__) && !defined(__ILP32__)) || defined(_M_X64) || defined(__ia64) || defined (_M_IA64) || defined(__aarch64__) || defined(__powerpc64__)
  137. typedef drwav_uint64 drwav_uintptr;
  138. #else
  139. typedef drwav_uint32 drwav_uintptr;
  140. #endif
  141. typedef drwav_uint8 drwav_bool8;
  142. typedef drwav_uint32 drwav_bool32;
  143. #define DRWAV_TRUE 1
  144. #define DRWAV_FALSE 0
  145. #if !defined(DRWAV_API)
  146. #if defined(DRWAV_DLL)
  147. #if defined(_WIN32)
  148. #define DRWAV_DLL_IMPORT __declspec(dllimport)
  149. #define DRWAV_DLL_EXPORT __declspec(dllexport)
  150. #define DRWAV_DLL_PRIVATE static
  151. #else
  152. #if defined(__GNUC__) && __GNUC__ >= 4
  153. #define DRWAV_DLL_IMPORT __attribute__((visibility("default")))
  154. #define DRWAV_DLL_EXPORT __attribute__((visibility("default")))
  155. #define DRWAV_DLL_PRIVATE __attribute__((visibility("hidden")))
  156. #else
  157. #define DRWAV_DLL_IMPORT
  158. #define DRWAV_DLL_EXPORT
  159. #define DRWAV_DLL_PRIVATE static
  160. #endif
  161. #endif
  162. #if defined(DR_WAV_IMPLEMENTATION) || defined(DRWAV_IMPLEMENTATION)
  163. #define DRWAV_API DRWAV_DLL_EXPORT
  164. #else
  165. #define DRWAV_API DRWAV_DLL_IMPORT
  166. #endif
  167. #define DRWAV_PRIVATE DRWAV_DLL_PRIVATE
  168. #else
  169. #define DRWAV_API extern
  170. #define DRWAV_PRIVATE static
  171. #endif
  172. #endif
  173. typedef drwav_int32 drwav_result;
  174. #define DRWAV_SUCCESS 0
  175. #define DRWAV_ERROR -1 /* A generic error. */
  176. #define DRWAV_INVALID_ARGS -2
  177. #define DRWAV_INVALID_OPERATION -3
  178. #define DRWAV_OUT_OF_MEMORY -4
  179. #define DRWAV_OUT_OF_RANGE -5
  180. #define DRWAV_ACCESS_DENIED -6
  181. #define DRWAV_DOES_NOT_EXIST -7
  182. #define DRWAV_ALREADY_EXISTS -8
  183. #define DRWAV_TOO_MANY_OPEN_FILES -9
  184. #define DRWAV_INVALID_FILE -10
  185. #define DRWAV_TOO_BIG -11
  186. #define DRWAV_PATH_TOO_LONG -12
  187. #define DRWAV_NAME_TOO_LONG -13
  188. #define DRWAV_NOT_DIRECTORY -14
  189. #define DRWAV_IS_DIRECTORY -15
  190. #define DRWAV_DIRECTORY_NOT_EMPTY -16
  191. #define DRWAV_END_OF_FILE -17
  192. #define DRWAV_NO_SPACE -18
  193. #define DRWAV_BUSY -19
  194. #define DRWAV_IO_ERROR -20
  195. #define DRWAV_INTERRUPT -21
  196. #define DRWAV_UNAVAILABLE -22
  197. #define DRWAV_ALREADY_IN_USE -23
  198. #define DRWAV_BAD_ADDRESS -24
  199. #define DRWAV_BAD_SEEK -25
  200. #define DRWAV_BAD_PIPE -26
  201. #define DRWAV_DEADLOCK -27
  202. #define DRWAV_TOO_MANY_LINKS -28
  203. #define DRWAV_NOT_IMPLEMENTED -29
  204. #define DRWAV_NO_MESSAGE -30
  205. #define DRWAV_BAD_MESSAGE -31
  206. #define DRWAV_NO_DATA_AVAILABLE -32
  207. #define DRWAV_INVALID_DATA -33
  208. #define DRWAV_TIMEOUT -34
  209. #define DRWAV_NO_NETWORK -35
  210. #define DRWAV_NOT_UNIQUE -36
  211. #define DRWAV_NOT_SOCKET -37
  212. #define DRWAV_NO_ADDRESS -38
  213. #define DRWAV_BAD_PROTOCOL -39
  214. #define DRWAV_PROTOCOL_UNAVAILABLE -40
  215. #define DRWAV_PROTOCOL_NOT_SUPPORTED -41
  216. #define DRWAV_PROTOCOL_FAMILY_NOT_SUPPORTED -42
  217. #define DRWAV_ADDRESS_FAMILY_NOT_SUPPORTED -43
  218. #define DRWAV_SOCKET_NOT_SUPPORTED -44
  219. #define DRWAV_CONNECTION_RESET -45
  220. #define DRWAV_ALREADY_CONNECTED -46
  221. #define DRWAV_NOT_CONNECTED -47
  222. #define DRWAV_CONNECTION_REFUSED -48
  223. #define DRWAV_NO_HOST -49
  224. #define DRWAV_IN_PROGRESS -50
  225. #define DRWAV_CANCELLED -51
  226. #define DRWAV_MEMORY_ALREADY_MAPPED -52
  227. #define DRWAV_AT_END -53
  228. /* Common data formats. */
  229. #define DR_WAVE_FORMAT_PCM 0x1
  230. #define DR_WAVE_FORMAT_ADPCM 0x2
  231. #define DR_WAVE_FORMAT_IEEE_FLOAT 0x3
  232. #define DR_WAVE_FORMAT_ALAW 0x6
  233. #define DR_WAVE_FORMAT_MULAW 0x7
  234. #define DR_WAVE_FORMAT_DVI_ADPCM 0x11
  235. #define DR_WAVE_FORMAT_EXTENSIBLE 0xFFFE
  236. /* Constants. */
  237. #ifndef DRWAV_MAX_SMPL_LOOPS
  238. #define DRWAV_MAX_SMPL_LOOPS 1
  239. #endif
  240. /* Flags to pass into drwav_init_ex(), etc. */
  241. #define DRWAV_SEQUENTIAL 0x00000001
  242. DRWAV_API void drwav_version(drwav_uint32* pMajor, drwav_uint32* pMinor, drwav_uint32* pRevision);
  243. DRWAV_API const char* drwav_version_string(void);
  244. typedef enum
  245. {
  246. drwav_seek_origin_start,
  247. drwav_seek_origin_current
  248. } drwav_seek_origin;
  249. typedef enum
  250. {
  251. drwav_container_riff,
  252. drwav_container_w64,
  253. drwav_container_rf64
  254. } drwav_container;
  255. typedef struct
  256. {
  257. union
  258. {
  259. drwav_uint8 fourcc[4];
  260. drwav_uint8 guid[16];
  261. } id;
  262. /* The size in bytes of the chunk. */
  263. drwav_uint64 sizeInBytes;
  264. /*
  265. RIFF = 2 byte alignment.
  266. W64 = 8 byte alignment.
  267. */
  268. unsigned int paddingSize;
  269. } drwav_chunk_header;
  270. typedef struct
  271. {
  272. /*
  273. The format tag exactly as specified in the wave file's "fmt" chunk. This can be used by applications
  274. that require support for data formats not natively supported by dr_wav.
  275. */
  276. drwav_uint16 formatTag;
  277. /* The number of channels making up the audio data. When this is set to 1 it is mono, 2 is stereo, etc. */
  278. drwav_uint16 channels;
  279. /* The sample rate. Usually set to something like 44100. */
  280. drwav_uint32 sampleRate;
  281. /* Average bytes per second. You probably don't need this, but it's left here for informational purposes. */
  282. drwav_uint32 avgBytesPerSec;
  283. /* Block align. This is equal to the number of channels * bytes per sample. */
  284. drwav_uint16 blockAlign;
  285. /* Bits per sample. */
  286. drwav_uint16 bitsPerSample;
  287. /* The size of the extended data. Only used internally for validation, but left here for informational purposes. */
  288. drwav_uint16 extendedSize;
  289. /*
  290. The number of valid bits per sample. When <formatTag> is equal to WAVE_FORMAT_EXTENSIBLE, <bitsPerSample>
  291. is always rounded up to the nearest multiple of 8. This variable contains information about exactly how
  292. many bits are valid per sample. Mainly used for informational purposes.
  293. */
  294. drwav_uint16 validBitsPerSample;
  295. /* The channel mask. Not used at the moment. */
  296. drwav_uint32 channelMask;
  297. /* The sub-format, exactly as specified by the wave file. */
  298. drwav_uint8 subFormat[16];
  299. } drwav_fmt;
  300. DRWAV_API drwav_uint16 drwav_fmt_get_format(const drwav_fmt* pFMT);
  301. /*
  302. Callback for when data is read. Return value is the number of bytes actually read.
  303. pUserData [in] The user data that was passed to drwav_init() and family.
  304. pBufferOut [out] The output buffer.
  305. bytesToRead [in] The number of bytes to read.
  306. Returns the number of bytes actually read.
  307. A return value of less than bytesToRead indicates the end of the stream. Do _not_ return from this callback until
  308. either the entire bytesToRead is filled or you have reached the end of the stream.
  309. */
  310. typedef size_t (* drwav_read_proc)(void* pUserData, void* pBufferOut, size_t bytesToRead);
  311. /*
  312. Callback for when data is written. Returns value is the number of bytes actually written.
  313. pUserData [in] The user data that was passed to drwav_init_write() and family.
  314. pData [out] A pointer to the data to write.
  315. bytesToWrite [in] The number of bytes to write.
  316. Returns the number of bytes actually written.
  317. If the return value differs from bytesToWrite, it indicates an error.
  318. */
  319. typedef size_t (* drwav_write_proc)(void* pUserData, const void* pData, size_t bytesToWrite);
  320. /*
  321. Callback for when data needs to be seeked.
  322. pUserData [in] The user data that was passed to drwav_init() and family.
  323. offset [in] The number of bytes to move, relative to the origin. Will never be negative.
  324. origin [in] The origin of the seek - the current position or the start of the stream.
  325. Returns whether or not the seek was successful.
  326. Whether or not it is relative to the beginning or current position is determined by the "origin" parameter which will be either drwav_seek_origin_start or
  327. drwav_seek_origin_current.
  328. */
  329. typedef drwav_bool32 (* drwav_seek_proc)(void* pUserData, int offset, drwav_seek_origin origin);
  330. /*
  331. Callback for when drwav_init_ex() finds a chunk.
  332. pChunkUserData [in] The user data that was passed to the pChunkUserData parameter of drwav_init_ex() and family.
  333. onRead [in] A pointer to the function to call when reading.
  334. onSeek [in] A pointer to the function to call when seeking.
  335. pReadSeekUserData [in] The user data that was passed to the pReadSeekUserData parameter of drwav_init_ex() and family.
  336. pChunkHeader [in] A pointer to an object containing basic header information about the chunk. Use this to identify the chunk.
  337. container [in] Whether or not the WAV file is a RIFF or Wave64 container. If you're unsure of the difference, assume RIFF.
  338. pFMT [in] A pointer to the object containing the contents of the "fmt" chunk.
  339. Returns the number of bytes read + seeked.
  340. To read data from the chunk, call onRead(), passing in pReadSeekUserData as the first parameter. Do the same for seeking with onSeek(). The return value must
  341. be the total number of bytes you have read _plus_ seeked.
  342. Use the `container` argument to discriminate the fields in `pChunkHeader->id`. If the container is `drwav_container_riff` or `drwav_container_rf64` you should
  343. use `id.fourcc`, otherwise you should use `id.guid`.
  344. The `pFMT` parameter can be used to determine the data format of the wave file. Use `drwav_fmt_get_format()` to get the sample format, which will be one of the
  345. `DR_WAVE_FORMAT_*` identifiers.
  346. The read pointer will be sitting on the first byte after the chunk's header. You must not attempt to read beyond the boundary of the chunk.
  347. */
  348. typedef drwav_uint64 (* drwav_chunk_proc)(void* pChunkUserData, drwav_read_proc onRead, drwav_seek_proc onSeek, void* pReadSeekUserData, const drwav_chunk_header* pChunkHeader, drwav_container container, const drwav_fmt* pFMT);
  349. typedef struct
  350. {
  351. void* pUserData;
  352. void* (* onMalloc)(size_t sz, void* pUserData);
  353. void* (* onRealloc)(void* p, size_t sz, void* pUserData);
  354. void (* onFree)(void* p, void* pUserData);
  355. } drwav_allocation_callbacks;
  356. /* Structure for internal use. Only used for loaders opened with drwav_init_memory(). */
  357. typedef struct
  358. {
  359. const drwav_uint8* data;
  360. size_t dataSize;
  361. size_t currentReadPos;
  362. } drwav__memory_stream;
  363. /* Structure for internal use. Only used for writers opened with drwav_init_memory_write(). */
  364. typedef struct
  365. {
  366. void** ppData;
  367. size_t* pDataSize;
  368. size_t dataSize;
  369. size_t dataCapacity;
  370. size_t currentWritePos;
  371. } drwav__memory_stream_write;
  372. typedef struct
  373. {
  374. drwav_container container; /* RIFF, W64. */
  375. drwav_uint32 format; /* DR_WAVE_FORMAT_* */
  376. drwav_uint32 channels;
  377. drwav_uint32 sampleRate;
  378. drwav_uint32 bitsPerSample;
  379. } drwav_data_format;
  380. /* See the following for details on the 'smpl' chunk: https://sites.google.com/site/musicgapi/technical-documents/wav-file-format#smpl */
  381. typedef struct
  382. {
  383. drwav_uint32 cuePointId;
  384. drwav_uint32 type;
  385. drwav_uint32 start;
  386. drwav_uint32 end;
  387. drwav_uint32 fraction;
  388. drwav_uint32 playCount;
  389. } drwav_smpl_loop;
  390. typedef struct
  391. {
  392. drwav_uint32 manufacturer;
  393. drwav_uint32 product;
  394. drwav_uint32 samplePeriod;
  395. drwav_uint32 midiUnityNotes;
  396. drwav_uint32 midiPitchFraction;
  397. drwav_uint32 smpteFormat;
  398. drwav_uint32 smpteOffset;
  399. drwav_uint32 numSampleLoops;
  400. drwav_uint32 samplerData;
  401. drwav_smpl_loop loops[DRWAV_MAX_SMPL_LOOPS];
  402. } drwav_smpl;
  403. typedef struct
  404. {
  405. /* A pointer to the function to call when more data is needed. */
  406. drwav_read_proc onRead;
  407. /* A pointer to the function to call when data needs to be written. Only used when the drwav object is opened in write mode. */
  408. drwav_write_proc onWrite;
  409. /* A pointer to the function to call when the wav file needs to be seeked. */
  410. drwav_seek_proc onSeek;
  411. /* The user data to pass to callbacks. */
  412. void* pUserData;
  413. /* Allocation callbacks. */
  414. drwav_allocation_callbacks allocationCallbacks;
  415. /* Whether or not the WAV file is formatted as a standard RIFF file or W64. */
  416. drwav_container container;
  417. /* Structure containing format information exactly as specified by the wav file. */
  418. drwav_fmt fmt;
  419. /* The sample rate. Will be set to something like 44100. */
  420. drwav_uint32 sampleRate;
  421. /* The number of channels. This will be set to 1 for monaural streams, 2 for stereo, etc. */
  422. drwav_uint16 channels;
  423. /* The bits per sample. Will be set to something like 16, 24, etc. */
  424. drwav_uint16 bitsPerSample;
  425. /* Equal to fmt.formatTag, or the value specified by fmt.subFormat if fmt.formatTag is equal to 65534 (WAVE_FORMAT_EXTENSIBLE). */
  426. drwav_uint16 translatedFormatTag;
  427. /* The total number of PCM frames making up the audio data. */
  428. drwav_uint64 totalPCMFrameCount;
  429. /* The size in bytes of the data chunk. */
  430. drwav_uint64 dataChunkDataSize;
  431. /* The position in the stream of the first byte of the data chunk. This is used for seeking. */
  432. drwav_uint64 dataChunkDataPos;
  433. /* The number of bytes remaining in the data chunk. */
  434. drwav_uint64 bytesRemaining;
  435. /*
  436. Only used in sequential write mode. Keeps track of the desired size of the "data" chunk at the point of initialization time. Always
  437. set to 0 for non-sequential writes and when the drwav object is opened in read mode. Used for validation.
  438. */
  439. drwav_uint64 dataChunkDataSizeTargetWrite;
  440. /* Keeps track of whether or not the wav writer was initialized in sequential mode. */
  441. drwav_bool32 isSequentialWrite;
  442. /* smpl chunk. */
  443. drwav_smpl smpl;
  444. /* A hack to avoid a DRWAV_MALLOC() when opening a decoder with drwav_init_memory(). */
  445. drwav__memory_stream memoryStream;
  446. drwav__memory_stream_write memoryStreamWrite;
  447. /* Generic data for compressed formats. This data is shared across all block-compressed formats. */
  448. struct
  449. {
  450. drwav_uint64 iCurrentPCMFrame; /* The index of the next PCM frame that will be read by drwav_read_*(). This is used with "totalPCMFrameCount" to ensure we don't read excess samples at the end of the last block. */
  451. } compressed;
  452. /* Microsoft ADPCM specific data. */
  453. struct
  454. {
  455. drwav_uint32 bytesRemainingInBlock;
  456. drwav_uint16 predictor[2];
  457. drwav_int32 delta[2];
  458. drwav_int32 cachedFrames[4]; /* Samples are stored in this cache during decoding. */
  459. drwav_uint32 cachedFrameCount;
  460. drwav_int32 prevFrames[2][2]; /* The previous 2 samples for each channel (2 channels at most). */
  461. } msadpcm;
  462. /* IMA ADPCM specific data. */
  463. struct
  464. {
  465. drwav_uint32 bytesRemainingInBlock;
  466. drwav_int32 predictor[2];
  467. drwav_int32 stepIndex[2];
  468. drwav_int32 cachedFrames[16]; /* Samples are stored in this cache during decoding. */
  469. drwav_uint32 cachedFrameCount;
  470. } ima;
  471. } drwav;
  472. /*
  473. Initializes a pre-allocated drwav object for reading.
  474. pWav [out] A pointer to the drwav object being initialized.
  475. onRead [in] The function to call when data needs to be read from the client.
  476. onSeek [in] The function to call when the read position of the client data needs to move.
  477. onChunk [in, optional] The function to call when a chunk is enumerated at initialized time.
  478. pUserData, pReadSeekUserData [in, optional] A pointer to application defined data that will be passed to onRead and onSeek.
  479. pChunkUserData [in, optional] A pointer to application defined data that will be passed to onChunk.
  480. flags [in, optional] A set of flags for controlling how things are loaded.
  481. Returns true if successful; false otherwise.
  482. Close the loader with drwav_uninit().
  483. This is the lowest level function for initializing a WAV file. You can also use drwav_init_file() and drwav_init_memory()
  484. to open the stream from a file or from a block of memory respectively.
  485. Possible values for flags:
  486. DRWAV_SEQUENTIAL: Never perform a backwards seek while loading. This disables the chunk callback and will cause this function
  487. to return as soon as the data chunk is found. Any chunks after the data chunk will be ignored.
  488. drwav_init() is equivalent to "drwav_init_ex(pWav, onRead, onSeek, NULL, pUserData, NULL, 0);".
  489. The onChunk callback is not called for the WAVE or FMT chunks. The contents of the FMT chunk can be read from pWav->fmt
  490. after the function returns.
  491. See also: drwav_init_file(), drwav_init_memory(), drwav_uninit()
  492. */
  493. DRWAV_API drwav_bool32 drwav_init(drwav* pWav, drwav_read_proc onRead, drwav_seek_proc onSeek, void* pUserData, const drwav_allocation_callbacks* pAllocationCallbacks);
  494. DRWAV_API drwav_bool32 drwav_init_ex(drwav* pWav, drwav_read_proc onRead, drwav_seek_proc onSeek, drwav_chunk_proc onChunk, void* pReadSeekUserData, void* pChunkUserData, drwav_uint32 flags, const drwav_allocation_callbacks* pAllocationCallbacks);
  495. /*
  496. Initializes a pre-allocated drwav object for writing.
  497. onWrite [in] The function to call when data needs to be written.
  498. onSeek [in] The function to call when the write position needs to move.
  499. pUserData [in, optional] A pointer to application defined data that will be passed to onWrite and onSeek.
  500. Returns true if successful; false otherwise.
  501. Close the writer with drwav_uninit().
  502. This is the lowest level function for initializing a WAV file. You can also use drwav_init_file_write() and drwav_init_memory_write()
  503. to open the stream from a file or from a block of memory respectively.
  504. If the total sample count is known, you can use drwav_init_write_sequential(). This avoids the need for dr_wav to perform
  505. a post-processing step for storing the total sample count and the size of the data chunk which requires a backwards seek.
  506. See also: drwav_init_file_write(), drwav_init_memory_write(), drwav_uninit()
  507. */
  508. DRWAV_API drwav_bool32 drwav_init_write(drwav* pWav, const drwav_data_format* pFormat, drwav_write_proc onWrite, drwav_seek_proc onSeek, void* pUserData, const drwav_allocation_callbacks* pAllocationCallbacks);
  509. DRWAV_API drwav_bool32 drwav_init_write_sequential(drwav* pWav, const drwav_data_format* pFormat, drwav_uint64 totalSampleCount, drwav_write_proc onWrite, void* pUserData, const drwav_allocation_callbacks* pAllocationCallbacks);
  510. DRWAV_API drwav_bool32 drwav_init_write_sequential_pcm_frames(drwav* pWav, const drwav_data_format* pFormat, drwav_uint64 totalPCMFrameCount, drwav_write_proc onWrite, void* pUserData, const drwav_allocation_callbacks* pAllocationCallbacks);
  511. /*
  512. Utility function to determine the target size of the entire data to be written (including all headers and chunks).
  513. Returns the target size in bytes.
  514. Useful if the application needs to know the size to allocate.
  515. Only writing to the RIFF chunk and one data chunk is currently supported.
  516. See also: drwav_init_write(), drwav_init_file_write(), drwav_init_memory_write()
  517. */
  518. DRWAV_API drwav_uint64 drwav_target_write_size_bytes(const drwav_data_format* pFormat, drwav_uint64 totalSampleCount);
  519. /*
  520. Uninitializes the given drwav object.
  521. Use this only for objects initialized with drwav_init*() functions (drwav_init(), drwav_init_ex(), drwav_init_write(), drwav_init_write_sequential()).
  522. */
  523. DRWAV_API drwav_result drwav_uninit(drwav* pWav);
  524. /*
  525. Reads raw audio data.
  526. This is the lowest level function for reading audio data. It simply reads the given number of
  527. bytes of the raw internal sample data.
  528. Consider using drwav_read_pcm_frames_s16(), drwav_read_pcm_frames_s32() or drwav_read_pcm_frames_f32() for
  529. reading sample data in a consistent format.
  530. pBufferOut can be NULL in which case a seek will be performed.
  531. Returns the number of bytes actually read.
  532. */
  533. DRWAV_API size_t drwav_read_raw(drwav* pWav, size_t bytesToRead, void* pBufferOut);
  534. /*
  535. Reads up to the specified number of PCM frames from the WAV file.
  536. The output data will be in the file's internal format, converted to native-endian byte order. Use
  537. drwav_read_pcm_frames_s16/f32/s32() to read data in a specific format.
  538. If the return value is less than <framesToRead> it means the end of the file has been reached or
  539. you have requested more PCM frames than can possibly fit in the output buffer.
  540. This function will only work when sample data is of a fixed size and uncompressed. If you are
  541. using a compressed format consider using drwav_read_raw() or drwav_read_pcm_frames_s16/s32/f32().
  542. pBufferOut can be NULL in which case a seek will be performed.
  543. */
  544. DRWAV_API drwav_uint64 drwav_read_pcm_frames(drwav* pWav, drwav_uint64 framesToRead, void* pBufferOut);
  545. DRWAV_API drwav_uint64 drwav_read_pcm_frames_le(drwav* pWav, drwav_uint64 framesToRead, void* pBufferOut);
  546. DRWAV_API drwav_uint64 drwav_read_pcm_frames_be(drwav* pWav, drwav_uint64 framesToRead, void* pBufferOut);
  547. /*
  548. Seeks to the given PCM frame.
  549. Returns true if successful; false otherwise.
  550. */
  551. DRWAV_API drwav_bool32 drwav_seek_to_pcm_frame(drwav* pWav, drwav_uint64 targetFrameIndex);
  552. /*
  553. Writes raw audio data.
  554. Returns the number of bytes actually written. If this differs from bytesToWrite, it indicates an error.
  555. */
  556. DRWAV_API size_t drwav_write_raw(drwav* pWav, size_t bytesToWrite, const void* pData);
  557. /*
  558. Writes PCM frames.
  559. Returns the number of PCM frames written.
  560. Input samples need to be in native-endian byte order. On big-endian architectures the input data will be converted to
  561. little-endian. Use drwav_write_raw() to write raw audio data without performing any conversion.
  562. */
  563. DRWAV_API drwav_uint64 drwav_write_pcm_frames(drwav* pWav, drwav_uint64 framesToWrite, const void* pData);
  564. DRWAV_API drwav_uint64 drwav_write_pcm_frames_le(drwav* pWav, drwav_uint64 framesToWrite, const void* pData);
  565. DRWAV_API drwav_uint64 drwav_write_pcm_frames_be(drwav* pWav, drwav_uint64 framesToWrite, const void* pData);
  566. /* Conversion Utilities */
  567. #ifndef DR_WAV_NO_CONVERSION_API
  568. /*
  569. Reads a chunk of audio data and converts it to signed 16-bit PCM samples.
  570. pBufferOut can be NULL in which case a seek will be performed.
  571. Returns the number of PCM frames actually read.
  572. If the return value is less than <framesToRead> it means the end of the file has been reached.
  573. */
  574. DRWAV_API drwav_uint64 drwav_read_pcm_frames_s16(drwav* pWav, drwav_uint64 framesToRead, drwav_int16* pBufferOut);
  575. DRWAV_API drwav_uint64 drwav_read_pcm_frames_s16le(drwav* pWav, drwav_uint64 framesToRead, drwav_int16* pBufferOut);
  576. DRWAV_API drwav_uint64 drwav_read_pcm_frames_s16be(drwav* pWav, drwav_uint64 framesToRead, drwav_int16* pBufferOut);
  577. /* Low-level function for converting unsigned 8-bit PCM samples to signed 16-bit PCM samples. */
  578. DRWAV_API void drwav_u8_to_s16(drwav_int16* pOut, const drwav_uint8* pIn, size_t sampleCount);
  579. /* Low-level function for converting signed 24-bit PCM samples to signed 16-bit PCM samples. */
  580. DRWAV_API void drwav_s24_to_s16(drwav_int16* pOut, const drwav_uint8* pIn, size_t sampleCount);
  581. /* Low-level function for converting signed 32-bit PCM samples to signed 16-bit PCM samples. */
  582. DRWAV_API void drwav_s32_to_s16(drwav_int16* pOut, const drwav_int32* pIn, size_t sampleCount);
  583. /* Low-level function for converting IEEE 32-bit floating point samples to signed 16-bit PCM samples. */
  584. DRWAV_API void drwav_f32_to_s16(drwav_int16* pOut, const float* pIn, size_t sampleCount);
  585. /* Low-level function for converting IEEE 64-bit floating point samples to signed 16-bit PCM samples. */
  586. DRWAV_API void drwav_f64_to_s16(drwav_int16* pOut, const double* pIn, size_t sampleCount);
  587. /* Low-level function for converting A-law samples to signed 16-bit PCM samples. */
  588. DRWAV_API void drwav_alaw_to_s16(drwav_int16* pOut, const drwav_uint8* pIn, size_t sampleCount);
  589. /* Low-level function for converting u-law samples to signed 16-bit PCM samples. */
  590. DRWAV_API void drwav_mulaw_to_s16(drwav_int16* pOut, const drwav_uint8* pIn, size_t sampleCount);
  591. /*
  592. Reads a chunk of audio data and converts it to IEEE 32-bit floating point samples.
  593. pBufferOut can be NULL in which case a seek will be performed.
  594. Returns the number of PCM frames actually read.
  595. If the return value is less than <framesToRead> it means the end of the file has been reached.
  596. */
  597. DRWAV_API drwav_uint64 drwav_read_pcm_frames_f32(drwav* pWav, drwav_uint64 framesToRead, float* pBufferOut);
  598. DRWAV_API drwav_uint64 drwav_read_pcm_frames_f32le(drwav* pWav, drwav_uint64 framesToRead, float* pBufferOut);
  599. DRWAV_API drwav_uint64 drwav_read_pcm_frames_f32be(drwav* pWav, drwav_uint64 framesToRead, float* pBufferOut);
  600. /* Low-level function for converting unsigned 8-bit PCM samples to IEEE 32-bit floating point samples. */
  601. DRWAV_API void drwav_u8_to_f32(float* pOut, const drwav_uint8* pIn, size_t sampleCount);
  602. /* Low-level function for converting signed 16-bit PCM samples to IEEE 32-bit floating point samples. */
  603. DRWAV_API void drwav_s16_to_f32(float* pOut, const drwav_int16* pIn, size_t sampleCount);
  604. /* Low-level function for converting signed 24-bit PCM samples to IEEE 32-bit floating point samples. */
  605. DRWAV_API void drwav_s24_to_f32(float* pOut, const drwav_uint8* pIn, size_t sampleCount);
  606. /* Low-level function for converting signed 32-bit PCM samples to IEEE 32-bit floating point samples. */
  607. DRWAV_API void drwav_s32_to_f32(float* pOut, const drwav_int32* pIn, size_t sampleCount);
  608. /* Low-level function for converting IEEE 64-bit floating point samples to IEEE 32-bit floating point samples. */
  609. DRWAV_API void drwav_f64_to_f32(float* pOut, const double* pIn, size_t sampleCount);
  610. /* Low-level function for converting A-law samples to IEEE 32-bit floating point samples. */
  611. DRWAV_API void drwav_alaw_to_f32(float* pOut, const drwav_uint8* pIn, size_t sampleCount);
  612. /* Low-level function for converting u-law samples to IEEE 32-bit floating point samples. */
  613. DRWAV_API void drwav_mulaw_to_f32(float* pOut, const drwav_uint8* pIn, size_t sampleCount);
  614. /*
  615. Reads a chunk of audio data and converts it to signed 32-bit PCM samples.
  616. pBufferOut can be NULL in which case a seek will be performed.
  617. Returns the number of PCM frames actually read.
  618. If the return value is less than <framesToRead> it means the end of the file has been reached.
  619. */
  620. DRWAV_API drwav_uint64 drwav_read_pcm_frames_s32(drwav* pWav, drwav_uint64 framesToRead, drwav_int32* pBufferOut);
  621. DRWAV_API drwav_uint64 drwav_read_pcm_frames_s32le(drwav* pWav, drwav_uint64 framesToRead, drwav_int32* pBufferOut);
  622. DRWAV_API drwav_uint64 drwav_read_pcm_frames_s32be(drwav* pWav, drwav_uint64 framesToRead, drwav_int32* pBufferOut);
  623. /* Low-level function for converting unsigned 8-bit PCM samples to signed 32-bit PCM samples. */
  624. DRWAV_API void drwav_u8_to_s32(drwav_int32* pOut, const drwav_uint8* pIn, size_t sampleCount);
  625. /* Low-level function for converting signed 16-bit PCM samples to signed 32-bit PCM samples. */
  626. DRWAV_API void drwav_s16_to_s32(drwav_int32* pOut, const drwav_int16* pIn, size_t sampleCount);
  627. /* Low-level function for converting signed 24-bit PCM samples to signed 32-bit PCM samples. */
  628. DRWAV_API void drwav_s24_to_s32(drwav_int32* pOut, const drwav_uint8* pIn, size_t sampleCount);
  629. /* Low-level function for converting IEEE 32-bit floating point samples to signed 32-bit PCM samples. */
  630. DRWAV_API void drwav_f32_to_s32(drwav_int32* pOut, const float* pIn, size_t sampleCount);
  631. /* Low-level function for converting IEEE 64-bit floating point samples to signed 32-bit PCM samples. */
  632. DRWAV_API void drwav_f64_to_s32(drwav_int32* pOut, const double* pIn, size_t sampleCount);
  633. /* Low-level function for converting A-law samples to signed 32-bit PCM samples. */
  634. DRWAV_API void drwav_alaw_to_s32(drwav_int32* pOut, const drwav_uint8* pIn, size_t sampleCount);
  635. /* Low-level function for converting u-law samples to signed 32-bit PCM samples. */
  636. DRWAV_API void drwav_mulaw_to_s32(drwav_int32* pOut, const drwav_uint8* pIn, size_t sampleCount);
  637. #endif /* DR_WAV_NO_CONVERSION_API */
  638. /* High-Level Convenience Helpers */
  639. #ifndef DR_WAV_NO_STDIO
  640. /*
  641. Helper for initializing a wave file for reading using stdio.
  642. This holds the internal FILE object until drwav_uninit() is called. Keep this in mind if you're caching drwav
  643. objects because the operating system may restrict the number of file handles an application can have open at
  644. any given time.
  645. */
  646. DRWAV_API drwav_bool32 drwav_init_file(drwav* pWav, const char* filename, const drwav_allocation_callbacks* pAllocationCallbacks);
  647. DRWAV_API drwav_bool32 drwav_init_file_ex(drwav* pWav, const char* filename, drwav_chunk_proc onChunk, void* pChunkUserData, drwav_uint32 flags, const drwav_allocation_callbacks* pAllocationCallbacks);
  648. DRWAV_API drwav_bool32 drwav_init_file_w(drwav* pWav, const wchar_t* filename, const drwav_allocation_callbacks* pAllocationCallbacks);
  649. DRWAV_API drwav_bool32 drwav_init_file_ex_w(drwav* pWav, const wchar_t* filename, drwav_chunk_proc onChunk, void* pChunkUserData, drwav_uint32 flags, const drwav_allocation_callbacks* pAllocationCallbacks);
  650. /*
  651. Helper for initializing a wave file for writing using stdio.
  652. This holds the internal FILE object until drwav_uninit() is called. Keep this in mind if you're caching drwav
  653. objects because the operating system may restrict the number of file handles an application can have open at
  654. any given time.
  655. */
  656. DRWAV_API drwav_bool32 drwav_init_file_write(drwav* pWav, const char* filename, const drwav_data_format* pFormat, const drwav_allocation_callbacks* pAllocationCallbacks);
  657. DRWAV_API drwav_bool32 drwav_init_file_write_sequential(drwav* pWav, const char* filename, const drwav_data_format* pFormat, drwav_uint64 totalSampleCount, const drwav_allocation_callbacks* pAllocationCallbacks);
  658. DRWAV_API drwav_bool32 drwav_init_file_write_sequential_pcm_frames(drwav* pWav, const char* filename, const drwav_data_format* pFormat, drwav_uint64 totalPCMFrameCount, const drwav_allocation_callbacks* pAllocationCallbacks);
  659. DRWAV_API drwav_bool32 drwav_init_file_write_w(drwav* pWav, const wchar_t* filename, const drwav_data_format* pFormat, const drwav_allocation_callbacks* pAllocationCallbacks);
  660. DRWAV_API drwav_bool32 drwav_init_file_write_sequential_w(drwav* pWav, const wchar_t* filename, const drwav_data_format* pFormat, drwav_uint64 totalSampleCount, const drwav_allocation_callbacks* pAllocationCallbacks);
  661. DRWAV_API drwav_bool32 drwav_init_file_write_sequential_pcm_frames_w(drwav* pWav, const wchar_t* filename, const drwav_data_format* pFormat, drwav_uint64 totalPCMFrameCount, const drwav_allocation_callbacks* pAllocationCallbacks);
  662. #endif /* DR_WAV_NO_STDIO */
  663. /*
  664. Helper for initializing a loader from a pre-allocated memory buffer.
  665. This does not create a copy of the data. It is up to the application to ensure the buffer remains valid for
  666. the lifetime of the drwav object.
  667. The buffer should contain the contents of the entire wave file, not just the sample data.
  668. */
  669. DRWAV_API drwav_bool32 drwav_init_memory(drwav* pWav, const void* data, size_t dataSize, const drwav_allocation_callbacks* pAllocationCallbacks);
  670. DRWAV_API drwav_bool32 drwav_init_memory_ex(drwav* pWav, const void* data, size_t dataSize, drwav_chunk_proc onChunk, void* pChunkUserData, drwav_uint32 flags, const drwav_allocation_callbacks* pAllocationCallbacks);
  671. /*
  672. Helper for initializing a writer which outputs data to a memory buffer.
  673. dr_wav will manage the memory allocations, however it is up to the caller to free the data with drwav_free().
  674. The buffer will remain allocated even after drwav_uninit() is called. The buffer should not be considered valid
  675. until after drwav_uninit() has been called.
  676. */
  677. DRWAV_API drwav_bool32 drwav_init_memory_write(drwav* pWav, void** ppData, size_t* pDataSize, const drwav_data_format* pFormat, const drwav_allocation_callbacks* pAllocationCallbacks);
  678. DRWAV_API drwav_bool32 drwav_init_memory_write_sequential(drwav* pWav, void** ppData, size_t* pDataSize, const drwav_data_format* pFormat, drwav_uint64 totalSampleCount, const drwav_allocation_callbacks* pAllocationCallbacks);
  679. DRWAV_API drwav_bool32 drwav_init_memory_write_sequential_pcm_frames(drwav* pWav, void** ppData, size_t* pDataSize, const drwav_data_format* pFormat, drwav_uint64 totalPCMFrameCount, const drwav_allocation_callbacks* pAllocationCallbacks);
  680. #ifndef DR_WAV_NO_CONVERSION_API
  681. /*
  682. Opens and reads an entire wav file in a single operation.
  683. The return value is a heap-allocated buffer containing the audio data. Use drwav_free() to free the buffer.
  684. */
  685. DRWAV_API drwav_int16* drwav_open_and_read_pcm_frames_s16(drwav_read_proc onRead, drwav_seek_proc onSeek, void* pUserData, unsigned int* channelsOut, unsigned int* sampleRateOut, drwav_uint64* totalFrameCountOut, const drwav_allocation_callbacks* pAllocationCallbacks);
  686. DRWAV_API float* drwav_open_and_read_pcm_frames_f32(drwav_read_proc onRead, drwav_seek_proc onSeek, void* pUserData, unsigned int* channelsOut, unsigned int* sampleRateOut, drwav_uint64* totalFrameCountOut, const drwav_allocation_callbacks* pAllocationCallbacks);
  687. DRWAV_API drwav_int32* drwav_open_and_read_pcm_frames_s32(drwav_read_proc onRead, drwav_seek_proc onSeek, void* pUserData, unsigned int* channelsOut, unsigned int* sampleRateOut, drwav_uint64* totalFrameCountOut, const drwav_allocation_callbacks* pAllocationCallbacks);
  688. #ifndef DR_WAV_NO_STDIO
  689. /*
  690. Opens and decodes an entire wav file in a single operation.
  691. The return value is a heap-allocated buffer containing the audio data. Use drwav_free() to free the buffer.
  692. */
  693. DRWAV_API drwav_int16* drwav_open_file_and_read_pcm_frames_s16(const char* filename, unsigned int* channelsOut, unsigned int* sampleRateOut, drwav_uint64* totalFrameCountOut, const drwav_allocation_callbacks* pAllocationCallbacks);
  694. DRWAV_API float* drwav_open_file_and_read_pcm_frames_f32(const char* filename, unsigned int* channelsOut, unsigned int* sampleRateOut, drwav_uint64* totalFrameCountOut, const drwav_allocation_callbacks* pAllocationCallbacks);
  695. DRWAV_API drwav_int32* drwav_open_file_and_read_pcm_frames_s32(const char* filename, unsigned int* channelsOut, unsigned int* sampleRateOut, drwav_uint64* totalFrameCountOut, const drwav_allocation_callbacks* pAllocationCallbacks);
  696. DRWAV_API drwav_int16* drwav_open_file_and_read_pcm_frames_s16_w(const wchar_t* filename, unsigned int* channelsOut, unsigned int* sampleRateOut, drwav_uint64* totalFrameCountOut, const drwav_allocation_callbacks* pAllocationCallbacks);
  697. DRWAV_API float* drwav_open_file_and_read_pcm_frames_f32_w(const wchar_t* filename, unsigned int* channelsOut, unsigned int* sampleRateOut, drwav_uint64* totalFrameCountOut, const drwav_allocation_callbacks* pAllocationCallbacks);
  698. DRWAV_API drwav_int32* drwav_open_file_and_read_pcm_frames_s32_w(const wchar_t* filename, unsigned int* channelsOut, unsigned int* sampleRateOut, drwav_uint64* totalFrameCountOut, const drwav_allocation_callbacks* pAllocationCallbacks);
  699. #endif
  700. /*
  701. Opens and decodes an entire wav file from a block of memory in a single operation.
  702. The return value is a heap-allocated buffer containing the audio data. Use drwav_free() to free the buffer.
  703. */
  704. DRWAV_API drwav_int16* drwav_open_memory_and_read_pcm_frames_s16(const void* data, size_t dataSize, unsigned int* channelsOut, unsigned int* sampleRateOut, drwav_uint64* totalFrameCountOut, const drwav_allocation_callbacks* pAllocationCallbacks);
  705. DRWAV_API float* drwav_open_memory_and_read_pcm_frames_f32(const void* data, size_t dataSize, unsigned int* channelsOut, unsigned int* sampleRateOut, drwav_uint64* totalFrameCountOut, const drwav_allocation_callbacks* pAllocationCallbacks);
  706. DRWAV_API drwav_int32* drwav_open_memory_and_read_pcm_frames_s32(const void* data, size_t dataSize, unsigned int* channelsOut, unsigned int* sampleRateOut, drwav_uint64* totalFrameCountOut, const drwav_allocation_callbacks* pAllocationCallbacks);
  707. #endif
  708. /* Frees data that was allocated internally by dr_wav. */
  709. DRWAV_API void drwav_free(void* p, const drwav_allocation_callbacks* pAllocationCallbacks);
  710. /* Converts bytes from a wav stream to a sized type of native endian. */
  711. DRWAV_API drwav_uint16 drwav_bytes_to_u16(const drwav_uint8* data);
  712. DRWAV_API drwav_int16 drwav_bytes_to_s16(const drwav_uint8* data);
  713. DRWAV_API drwav_uint32 drwav_bytes_to_u32(const drwav_uint8* data);
  714. DRWAV_API drwav_int32 drwav_bytes_to_s32(const drwav_uint8* data);
  715. DRWAV_API drwav_uint64 drwav_bytes_to_u64(const drwav_uint8* data);
  716. DRWAV_API drwav_int64 drwav_bytes_to_s64(const drwav_uint8* data);
  717. /* Compares a GUID for the purpose of checking the type of a Wave64 chunk. */
  718. DRWAV_API drwav_bool32 drwav_guid_equal(const drwav_uint8 a[16], const drwav_uint8 b[16]);
  719. /* Compares a four-character-code for the purpose of checking the type of a RIFF chunk. */
  720. DRWAV_API drwav_bool32 drwav_fourcc_equal(const drwav_uint8* a, const char* b);
  721. #ifdef __cplusplus
  722. }
  723. #endif
  724. #endif /* dr_wav_h */
  725. /************************************************************************************************************************************************************
  726. ************************************************************************************************************************************************************
  727. IMPLEMENTATION
  728. ************************************************************************************************************************************************************
  729. ************************************************************************************************************************************************************/
  730. #if defined(DR_WAV_IMPLEMENTATION) || defined(DRWAV_IMPLEMENTATION)
  731. #ifndef dr_wav_c
  732. #define dr_wav_c
  733. #include <stdlib.h>
  734. #include <string.h> /* For memcpy(), memset() */
  735. #include <limits.h> /* For INT_MAX */
  736. #ifndef DR_WAV_NO_STDIO
  737. #include <stdio.h>
  738. #include <wchar.h>
  739. #endif
  740. /* Standard library stuff. */
  741. #ifndef DRWAV_ASSERT
  742. #include <assert.h>
  743. #define DRWAV_ASSERT(expression) assert(expression)
  744. #endif
  745. #ifndef DRWAV_MALLOC
  746. #define DRWAV_MALLOC(sz) malloc((sz))
  747. #endif
  748. #ifndef DRWAV_REALLOC
  749. #define DRWAV_REALLOC(p, sz) realloc((p), (sz))
  750. #endif
  751. #ifndef DRWAV_FREE
  752. #define DRWAV_FREE(p) free((p))
  753. #endif
  754. #ifndef DRWAV_COPY_MEMORY
  755. #define DRWAV_COPY_MEMORY(dst, src, sz) memcpy((dst), (src), (sz))
  756. #endif
  757. #ifndef DRWAV_ZERO_MEMORY
  758. #define DRWAV_ZERO_MEMORY(p, sz) memset((p), 0, (sz))
  759. #endif
  760. #ifndef DRWAV_ZERO_OBJECT
  761. #define DRWAV_ZERO_OBJECT(p) DRWAV_ZERO_MEMORY((p), sizeof(*p))
  762. #endif
  763. #define drwav_countof(x) (sizeof(x) / sizeof(x[0]))
  764. #define drwav_align(x, a) ((((x) + (a) - 1) / (a)) * (a))
  765. #define drwav_min(a, b) (((a) < (b)) ? (a) : (b))
  766. #define drwav_max(a, b) (((a) > (b)) ? (a) : (b))
  767. #define drwav_clamp(x, lo, hi) (drwav_max((lo), drwav_min((hi), (x))))
  768. #define DRWAV_MAX_SIMD_VECTOR_SIZE 64 /* 64 for AVX-512 in the future. */
  769. /* CPU architecture. */
  770. #if defined(__x86_64__) || defined(_M_X64)
  771. #define DRWAV_X64
  772. #elif defined(__i386) || defined(_M_IX86)
  773. #define DRWAV_X86
  774. #elif defined(__arm__) || defined(_M_ARM)
  775. #define DRWAV_ARM
  776. #endif
  777. #ifdef _MSC_VER
  778. #define DRWAV_INLINE __forceinline
  779. #elif defined(__GNUC__)
  780. /*
  781. I've had a bug report where GCC is emitting warnings about functions possibly not being inlineable. This warning happens when
  782. the __attribute__((always_inline)) attribute is defined without an "inline" statement. I think therefore there must be some
  783. case where "__inline__" is not always defined, thus the compiler emitting these warnings. When using -std=c89 or -ansi on the
  784. command line, we cannot use the "inline" keyword and instead need to use "__inline__". In an attempt to work around this issue
  785. I am using "__inline__" only when we're compiling in strict ANSI mode.
  786. */
  787. #if defined(__STRICT_ANSI__)
  788. #define DRWAV_INLINE __inline__ __attribute__((always_inline))
  789. #else
  790. #define DRWAV_INLINE inline __attribute__((always_inline))
  791. #endif
  792. #elif defined(__WATCOMC__)
  793. #define DRWAV_INLINE __inline
  794. #else
  795. #define DRWAV_INLINE
  796. #endif
  797. #if defined(SIZE_MAX)
  798. #define DRWAV_SIZE_MAX SIZE_MAX
  799. #else
  800. #if defined(_WIN64) || defined(_LP64) || defined(__LP64__)
  801. #define DRWAV_SIZE_MAX ((drwav_uint64)0xFFFFFFFFFFFFFFFF)
  802. #else
  803. #define DRWAV_SIZE_MAX 0xFFFFFFFF
  804. #endif
  805. #endif
  806. #if defined(_MSC_VER) && _MSC_VER >= 1400
  807. #define DRWAV_HAS_BYTESWAP16_INTRINSIC
  808. #define DRWAV_HAS_BYTESWAP32_INTRINSIC
  809. #define DRWAV_HAS_BYTESWAP64_INTRINSIC
  810. #elif defined(__clang__)
  811. #if defined(__has_builtin)
  812. #if __has_builtin(__builtin_bswap16)
  813. #define DRWAV_HAS_BYTESWAP16_INTRINSIC
  814. #endif
  815. #if __has_builtin(__builtin_bswap32)
  816. #define DRWAV_HAS_BYTESWAP32_INTRINSIC
  817. #endif
  818. #if __has_builtin(__builtin_bswap64)
  819. #define DRWAV_HAS_BYTESWAP64_INTRINSIC
  820. #endif
  821. #endif
  822. #elif defined(__GNUC__)
  823. #if ((__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3))
  824. #define DRWAV_HAS_BYTESWAP32_INTRINSIC
  825. #define DRWAV_HAS_BYTESWAP64_INTRINSIC
  826. #endif
  827. #if ((__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8))
  828. #define DRWAV_HAS_BYTESWAP16_INTRINSIC
  829. #endif
  830. #endif
  831. DRWAV_API void drwav_version(drwav_uint32* pMajor, drwav_uint32* pMinor, drwav_uint32* pRevision)
  832. {
  833. if (pMajor) {
  834. *pMajor = DRWAV_VERSION_MAJOR;
  835. }
  836. if (pMinor) {
  837. *pMinor = DRWAV_VERSION_MINOR;
  838. }
  839. if (pRevision) {
  840. *pRevision = DRWAV_VERSION_REVISION;
  841. }
  842. }
  843. DRWAV_API const char* drwav_version_string(void)
  844. {
  845. return DRWAV_VERSION_STRING;
  846. }
  847. /*
  848. These limits are used for basic validation when initializing the decoder. If you exceed these limits, first of all: what on Earth are
  849. you doing?! (Let me know, I'd be curious!) Second, you can adjust these by #define-ing them before the dr_wav implementation.
  850. */
  851. #ifndef DRWAV_MAX_SAMPLE_RATE
  852. #define DRWAV_MAX_SAMPLE_RATE 384000
  853. #endif
  854. #ifndef DRWAV_MAX_CHANNELS
  855. #define DRWAV_MAX_CHANNELS 256
  856. #endif
  857. #ifndef DRWAV_MAX_BITS_PER_SAMPLE
  858. #define DRWAV_MAX_BITS_PER_SAMPLE 64
  859. #endif
  860. static const drwav_uint8 drwavGUID_W64_RIFF[16] = {0x72,0x69,0x66,0x66, 0x2E,0x91, 0xCF,0x11, 0xA5,0xD6, 0x28,0xDB,0x04,0xC1,0x00,0x00}; /* 66666972-912E-11CF-A5D6-28DB04C10000 */
  861. static const drwav_uint8 drwavGUID_W64_WAVE[16] = {0x77,0x61,0x76,0x65, 0xF3,0xAC, 0xD3,0x11, 0x8C,0xD1, 0x00,0xC0,0x4F,0x8E,0xDB,0x8A}; /* 65766177-ACF3-11D3-8CD1-00C04F8EDB8A */
  862. /*static const drwav_uint8 drwavGUID_W64_JUNK[16] = {0x6A,0x75,0x6E,0x6B, 0xF3,0xAC, 0xD3,0x11, 0x8C,0xD1, 0x00,0xC0,0x4F,0x8E,0xDB,0x8A};*/ /* 6B6E756A-ACF3-11D3-8CD1-00C04F8EDB8A */
  863. static const drwav_uint8 drwavGUID_W64_FMT [16] = {0x66,0x6D,0x74,0x20, 0xF3,0xAC, 0xD3,0x11, 0x8C,0xD1, 0x00,0xC0,0x4F,0x8E,0xDB,0x8A}; /* 20746D66-ACF3-11D3-8CD1-00C04F8EDB8A */
  864. static const drwav_uint8 drwavGUID_W64_FACT[16] = {0x66,0x61,0x63,0x74, 0xF3,0xAC, 0xD3,0x11, 0x8C,0xD1, 0x00,0xC0,0x4F,0x8E,0xDB,0x8A}; /* 74636166-ACF3-11D3-8CD1-00C04F8EDB8A */
  865. static const drwav_uint8 drwavGUID_W64_DATA[16] = {0x64,0x61,0x74,0x61, 0xF3,0xAC, 0xD3,0x11, 0x8C,0xD1, 0x00,0xC0,0x4F,0x8E,0xDB,0x8A}; /* 61746164-ACF3-11D3-8CD1-00C04F8EDB8A */
  866. static const drwav_uint8 drwavGUID_W64_SMPL[16] = {0x73,0x6D,0x70,0x6C, 0xF3,0xAC, 0xD3,0x11, 0x8C,0xD1, 0x00,0xC0,0x4F,0x8E,0xDB,0x8A}; /* 6C706D73-ACF3-11D3-8CD1-00C04F8EDB8A */
  867. static DRWAV_INLINE drwav_bool32 drwav__guid_equal(const drwav_uint8 a[16], const drwav_uint8 b[16])
  868. {
  869. int i;
  870. for (i = 0; i < 16; i += 1) {
  871. if (a[i] != b[i]) {
  872. return DRWAV_FALSE;
  873. }
  874. }
  875. return DRWAV_TRUE;
  876. }
  877. static DRWAV_INLINE drwav_bool32 drwav__fourcc_equal(const drwav_uint8* a, const char* b)
  878. {
  879. return
  880. a[0] == b[0] &&
  881. a[1] == b[1] &&
  882. a[2] == b[2] &&
  883. a[3] == b[3];
  884. }
  885. static DRWAV_INLINE int drwav__is_little_endian(void)
  886. {
  887. #if defined(DRWAV_X86) || defined(DRWAV_X64)
  888. return DRWAV_TRUE;
  889. #elif defined(__BYTE_ORDER) && defined(__LITTLE_ENDIAN) && __BYTE_ORDER == __LITTLE_ENDIAN
  890. return DRWAV_TRUE;
  891. #else
  892. int n = 1;
  893. return (*(char*)&n) == 1;
  894. #endif
  895. }
  896. static DRWAV_INLINE drwav_uint16 drwav__bytes_to_u16(const drwav_uint8* data)
  897. {
  898. return (data[0] << 0) | (data[1] << 8);
  899. }
  900. static DRWAV_INLINE drwav_int16 drwav__bytes_to_s16(const drwav_uint8* data)
  901. {
  902. return (short)drwav__bytes_to_u16(data);
  903. }
  904. static DRWAV_INLINE drwav_uint32 drwav__bytes_to_u32(const drwav_uint8* data)
  905. {
  906. return (data[0] << 0) | (data[1] << 8) | (data[2] << 16) | (data[3] << 24);
  907. }
  908. static DRWAV_INLINE drwav_int32 drwav__bytes_to_s32(const drwav_uint8* data)
  909. {
  910. return (drwav_int32)drwav__bytes_to_u32(data);
  911. }
  912. static DRWAV_INLINE drwav_uint64 drwav__bytes_to_u64(const drwav_uint8* data)
  913. {
  914. return
  915. ((drwav_uint64)data[0] << 0) | ((drwav_uint64)data[1] << 8) | ((drwav_uint64)data[2] << 16) | ((drwav_uint64)data[3] << 24) |
  916. ((drwav_uint64)data[4] << 32) | ((drwav_uint64)data[5] << 40) | ((drwav_uint64)data[6] << 48) | ((drwav_uint64)data[7] << 56);
  917. }
  918. static DRWAV_INLINE drwav_int64 drwav__bytes_to_s64(const drwav_uint8* data)
  919. {
  920. return (drwav_int64)drwav__bytes_to_u64(data);
  921. }
  922. static DRWAV_INLINE void drwav__bytes_to_guid(const drwav_uint8* data, drwav_uint8* guid)
  923. {
  924. int i;
  925. for (i = 0; i < 16; ++i) {
  926. guid[i] = data[i];
  927. }
  928. }
  929. static DRWAV_INLINE drwav_uint16 drwav__bswap16(drwav_uint16 n)
  930. {
  931. #ifdef DRWAV_HAS_BYTESWAP16_INTRINSIC
  932. #if defined(_MSC_VER)
  933. return _byteswap_ushort(n);
  934. #elif defined(__GNUC__) || defined(__clang__)
  935. return __builtin_bswap16(n);
  936. #else
  937. #error "This compiler does not support the byte swap intrinsic."
  938. #endif
  939. #else
  940. return ((n & 0xFF00) >> 8) |
  941. ((n & 0x00FF) << 8);
  942. #endif
  943. }
  944. static DRWAV_INLINE drwav_uint32 drwav__bswap32(drwav_uint32 n)
  945. {
  946. #ifdef DRWAV_HAS_BYTESWAP32_INTRINSIC
  947. #if defined(_MSC_VER)
  948. return _byteswap_ulong(n);
  949. #elif defined(__GNUC__) || defined(__clang__)
  950. #if defined(DRWAV_ARM) && (defined(__ARM_ARCH) && __ARM_ARCH >= 6) && !defined(DRWAV_64BIT) /* <-- 64-bit inline assembly has not been tested, so disabling for now. */
  951. /* Inline assembly optimized implementation for ARM. In my testing, GCC does not generate optimized code with __builtin_bswap32(). */
  952. drwav_uint32 r;
  953. __asm__ __volatile__ (
  954. #if defined(DRWAV_64BIT)
  955. "rev %w[out], %w[in]" : [out]"=r"(r) : [in]"r"(n) /* <-- This is untested. If someone in the community could test this, that would be appreciated! */
  956. #else
  957. "rev %[out], %[in]" : [out]"=r"(r) : [in]"r"(n)
  958. #endif
  959. );
  960. return r;
  961. #else
  962. return __builtin_bswap32(n);
  963. #endif
  964. #else
  965. #error "This compiler does not support the byte swap intrinsic."
  966. #endif
  967. #else
  968. return ((n & 0xFF000000) >> 24) |
  969. ((n & 0x00FF0000) >> 8) |
  970. ((n & 0x0000FF00) << 8) |
  971. ((n & 0x000000FF) << 24);
  972. #endif
  973. }
  974. static DRWAV_INLINE drwav_uint64 drwav__bswap64(drwav_uint64 n)
  975. {
  976. #ifdef DRWAV_HAS_BYTESWAP64_INTRINSIC
  977. #if defined(_MSC_VER)
  978. return _byteswap_uint64(n);
  979. #elif defined(__GNUC__) || defined(__clang__)
  980. return __builtin_bswap64(n);
  981. #else
  982. #error "This compiler does not support the byte swap intrinsic."
  983. #endif
  984. #else
  985. /* Weird "<< 32" bitshift is required for C89 because it doesn't support 64-bit constants. Should be optimized out by a good compiler. */
  986. return ((n & ((drwav_uint64)0xFF000000 << 32)) >> 56) |
  987. ((n & ((drwav_uint64)0x00FF0000 << 32)) >> 40) |
  988. ((n & ((drwav_uint64)0x0000FF00 << 32)) >> 24) |
  989. ((n & ((drwav_uint64)0x000000FF << 32)) >> 8) |
  990. ((n & ((drwav_uint64)0xFF000000 )) << 8) |
  991. ((n & ((drwav_uint64)0x00FF0000 )) << 24) |
  992. ((n & ((drwav_uint64)0x0000FF00 )) << 40) |
  993. ((n & ((drwav_uint64)0x000000FF )) << 56);
  994. #endif
  995. }
  996. static DRWAV_INLINE drwav_int16 drwav__bswap_s16(drwav_int16 n)
  997. {
  998. return (drwav_int16)drwav__bswap16((drwav_uint16)n);
  999. }
  1000. static DRWAV_INLINE void drwav__bswap_samples_s16(drwav_int16* pSamples, drwav_uint64 sampleCount)
  1001. {
  1002. drwav_uint64 iSample;
  1003. for (iSample = 0; iSample < sampleCount; iSample += 1) {
  1004. pSamples[iSample] = drwav__bswap_s16(pSamples[iSample]);
  1005. }
  1006. }
  1007. static DRWAV_INLINE void drwav__bswap_s24(drwav_uint8* p)
  1008. {
  1009. drwav_uint8 t;
  1010. t = p[0];
  1011. p[0] = p[2];
  1012. p[2] = t;
  1013. }
  1014. static DRWAV_INLINE void drwav__bswap_samples_s24(drwav_uint8* pSamples, drwav_uint64 sampleCount)
  1015. {
  1016. drwav_uint64 iSample;
  1017. for (iSample = 0; iSample < sampleCount; iSample += 1) {
  1018. drwav_uint8* pSample = pSamples + (iSample*3);
  1019. drwav__bswap_s24(pSample);
  1020. }
  1021. }
  1022. static DRWAV_INLINE drwav_int32 drwav__bswap_s32(drwav_int32 n)
  1023. {
  1024. return (drwav_int32)drwav__bswap32((drwav_uint32)n);
  1025. }
  1026. static DRWAV_INLINE void drwav__bswap_samples_s32(drwav_int32* pSamples, drwav_uint64 sampleCount)
  1027. {
  1028. drwav_uint64 iSample;
  1029. for (iSample = 0; iSample < sampleCount; iSample += 1) {
  1030. pSamples[iSample] = drwav__bswap_s32(pSamples[iSample]);
  1031. }
  1032. }
  1033. static DRWAV_INLINE float drwav__bswap_f32(float n)
  1034. {
  1035. union {
  1036. drwav_uint32 i;
  1037. float f;
  1038. } x;
  1039. x.f = n;
  1040. x.i = drwav__bswap32(x.i);
  1041. return x.f;
  1042. }
  1043. static DRWAV_INLINE void drwav__bswap_samples_f32(float* pSamples, drwav_uint64 sampleCount)
  1044. {
  1045. drwav_uint64 iSample;
  1046. for (iSample = 0; iSample < sampleCount; iSample += 1) {
  1047. pSamples[iSample] = drwav__bswap_f32(pSamples[iSample]);
  1048. }
  1049. }
  1050. static DRWAV_INLINE double drwav__bswap_f64(double n)
  1051. {
  1052. union {
  1053. drwav_uint64 i;
  1054. double f;
  1055. } x;
  1056. x.f = n;
  1057. x.i = drwav__bswap64(x.i);
  1058. return x.f;
  1059. }
  1060. static DRWAV_INLINE void drwav__bswap_samples_f64(double* pSamples, drwav_uint64 sampleCount)
  1061. {
  1062. drwav_uint64 iSample;
  1063. for (iSample = 0; iSample < sampleCount; iSample += 1) {
  1064. pSamples[iSample] = drwav__bswap_f64(pSamples[iSample]);
  1065. }
  1066. }
  1067. static DRWAV_INLINE void drwav__bswap_samples_pcm(void* pSamples, drwav_uint64 sampleCount, drwav_uint32 bytesPerSample)
  1068. {
  1069. /* Assumes integer PCM. Floating point PCM is done in drwav__bswap_samples_ieee(). */
  1070. switch (bytesPerSample)
  1071. {
  1072. case 2: /* s16, s12 (loosely packed) */
  1073. {
  1074. drwav__bswap_samples_s16((drwav_int16*)pSamples, sampleCount);
  1075. } break;
  1076. case 3: /* s24 */
  1077. {
  1078. drwav__bswap_samples_s24((drwav_uint8*)pSamples, sampleCount);
  1079. } break;
  1080. case 4: /* s32 */
  1081. {
  1082. drwav__bswap_samples_s32((drwav_int32*)pSamples, sampleCount);
  1083. } break;
  1084. default:
  1085. {
  1086. /* Unsupported format. */
  1087. DRWAV_ASSERT(DRWAV_FALSE);
  1088. } break;
  1089. }
  1090. }
  1091. static DRWAV_INLINE void drwav__bswap_samples_ieee(void* pSamples, drwav_uint64 sampleCount, drwav_uint32 bytesPerSample)
  1092. {
  1093. switch (bytesPerSample)
  1094. {
  1095. #if 0 /* Contributions welcome for f16 support. */
  1096. case 2: /* f16 */
  1097. {
  1098. drwav__bswap_samples_f16((drwav_float16*)pSamples, sampleCount);
  1099. } break;
  1100. #endif
  1101. case 4: /* f32 */
  1102. {
  1103. drwav__bswap_samples_f32((float*)pSamples, sampleCount);
  1104. } break;
  1105. case 8: /* f64 */
  1106. {
  1107. drwav__bswap_samples_f64((double*)pSamples, sampleCount);
  1108. } break;
  1109. default:
  1110. {
  1111. /* Unsupported format. */
  1112. DRWAV_ASSERT(DRWAV_FALSE);
  1113. } break;
  1114. }
  1115. }
  1116. static DRWAV_INLINE void drwav__bswap_samples(void* pSamples, drwav_uint64 sampleCount, drwav_uint32 bytesPerSample, drwav_uint16 format)
  1117. {
  1118. switch (format)
  1119. {
  1120. case DR_WAVE_FORMAT_PCM:
  1121. {
  1122. drwav__bswap_samples_pcm(pSamples, sampleCount, bytesPerSample);
  1123. } break;
  1124. case DR_WAVE_FORMAT_IEEE_FLOAT:
  1125. {
  1126. drwav__bswap_samples_ieee(pSamples, sampleCount, bytesPerSample);
  1127. } break;
  1128. case DR_WAVE_FORMAT_ALAW:
  1129. case DR_WAVE_FORMAT_MULAW:
  1130. {
  1131. drwav__bswap_samples_s16((drwav_int16*)pSamples, sampleCount);
  1132. } break;
  1133. case DR_WAVE_FORMAT_ADPCM:
  1134. case DR_WAVE_FORMAT_DVI_ADPCM:
  1135. default:
  1136. {
  1137. /* Unsupported format. */
  1138. DRWAV_ASSERT(DRWAV_FALSE);
  1139. } break;
  1140. }
  1141. }
  1142. static void* drwav__malloc_default(size_t sz, void* pUserData)
  1143. {
  1144. (void)pUserData;
  1145. return DRWAV_MALLOC(sz);
  1146. }
  1147. static void* drwav__realloc_default(void* p, size_t sz, void* pUserData)
  1148. {
  1149. (void)pUserData;
  1150. return DRWAV_REALLOC(p, sz);
  1151. }
  1152. static void drwav__free_default(void* p, void* pUserData)
  1153. {
  1154. (void)pUserData;
  1155. DRWAV_FREE(p);
  1156. }
  1157. static void* drwav__malloc_from_callbacks(size_t sz, const drwav_allocation_callbacks* pAllocationCallbacks)
  1158. {
  1159. if (pAllocationCallbacks == NULL) {
  1160. return NULL;
  1161. }
  1162. if (pAllocationCallbacks->onMalloc != NULL) {
  1163. return pAllocationCallbacks->onMalloc(sz, pAllocationCallbacks->pUserData);
  1164. }
  1165. /* Try using realloc(). */
  1166. if (pAllocationCallbacks->onRealloc != NULL) {
  1167. return pAllocationCallbacks->onRealloc(NULL, sz, pAllocationCallbacks->pUserData);
  1168. }
  1169. return NULL;
  1170. }
  1171. static void* drwav__realloc_from_callbacks(void* p, size_t szNew, size_t szOld, const drwav_allocation_callbacks* pAllocationCallbacks)
  1172. {
  1173. if (pAllocationCallbacks == NULL) {
  1174. return NULL;
  1175. }
  1176. if (pAllocationCallbacks->onRealloc != NULL) {
  1177. return pAllocationCallbacks->onRealloc(p, szNew, pAllocationCallbacks->pUserData);
  1178. }
  1179. /* Try emulating realloc() in terms of malloc()/free(). */
  1180. if (pAllocationCallbacks->onMalloc != NULL && pAllocationCallbacks->onFree != NULL) {
  1181. void* p2;
  1182. p2 = pAllocationCallbacks->onMalloc(szNew, pAllocationCallbacks->pUserData);
  1183. if (p2 == NULL) {
  1184. return NULL;
  1185. }
  1186. if (p != NULL) {
  1187. DRWAV_COPY_MEMORY(p2, p, szOld);
  1188. pAllocationCallbacks->onFree(p, pAllocationCallbacks->pUserData);
  1189. }
  1190. return p2;
  1191. }
  1192. return NULL;
  1193. }
  1194. static void drwav__free_from_callbacks(void* p, const drwav_allocation_callbacks* pAllocationCallbacks)
  1195. {
  1196. if (p == NULL || pAllocationCallbacks == NULL) {
  1197. return;
  1198. }
  1199. if (pAllocationCallbacks->onFree != NULL) {
  1200. pAllocationCallbacks->onFree(p, pAllocationCallbacks->pUserData);
  1201. }
  1202. }
  1203. static drwav_allocation_callbacks drwav_copy_allocation_callbacks_or_defaults(const drwav_allocation_callbacks* pAllocationCallbacks)
  1204. {
  1205. if (pAllocationCallbacks != NULL) {
  1206. /* Copy. */
  1207. return *pAllocationCallbacks;
  1208. } else {
  1209. /* Defaults. */
  1210. drwav_allocation_callbacks allocationCallbacks;
  1211. allocationCallbacks.pUserData = NULL;
  1212. allocationCallbacks.onMalloc = drwav__malloc_default;
  1213. allocationCallbacks.onRealloc = drwav__realloc_default;
  1214. allocationCallbacks.onFree = drwav__free_default;
  1215. return allocationCallbacks;
  1216. }
  1217. }
  1218. static DRWAV_INLINE drwav_bool32 drwav__is_compressed_format_tag(drwav_uint16 formatTag)
  1219. {
  1220. return
  1221. formatTag == DR_WAVE_FORMAT_ADPCM ||
  1222. formatTag == DR_WAVE_FORMAT_DVI_ADPCM;
  1223. }
  1224. static unsigned int drwav__chunk_padding_size_riff(drwav_uint64 chunkSize)
  1225. {
  1226. return (unsigned int)(chunkSize % 2);
  1227. }
  1228. static unsigned int drwav__chunk_padding_size_w64(drwav_uint64 chunkSize)
  1229. {
  1230. return (unsigned int)(chunkSize % 8);
  1231. }
  1232. static drwav_uint64 drwav_read_pcm_frames_s16__msadpcm(drwav* pWav, drwav_uint64 samplesToRead, drwav_int16* pBufferOut);
  1233. static drwav_uint64 drwav_read_pcm_frames_s16__ima(drwav* pWav, drwav_uint64 samplesToRead, drwav_int16* pBufferOut);
  1234. static drwav_bool32 drwav_init_write__internal(drwav* pWav, const drwav_data_format* pFormat, drwav_uint64 totalSampleCount);
  1235. static drwav_result drwav__read_chunk_header(drwav_read_proc onRead, void* pUserData, drwav_container container, drwav_uint64* pRunningBytesReadOut, drwav_chunk_header* pHeaderOut)
  1236. {
  1237. if (container == drwav_container_riff || container == drwav_container_rf64) {
  1238. drwav_uint8 sizeInBytes[4];
  1239. if (onRead(pUserData, pHeaderOut->id.fourcc, 4) != 4) {
  1240. return DRWAV_AT_END;
  1241. }
  1242. if (onRead(pUserData, sizeInBytes, 4) != 4) {
  1243. return DRWAV_INVALID_FILE;
  1244. }
  1245. pHeaderOut->sizeInBytes = drwav__bytes_to_u32(sizeInBytes);
  1246. pHeaderOut->paddingSize = drwav__chunk_padding_size_riff(pHeaderOut->sizeInBytes);
  1247. *pRunningBytesReadOut += 8;
  1248. } else {
  1249. drwav_uint8 sizeInBytes[8];
  1250. if (onRead(pUserData, pHeaderOut->id.guid, 16) != 16) {
  1251. return DRWAV_AT_END;
  1252. }
  1253. if (onRead(pUserData, sizeInBytes, 8) != 8) {
  1254. return DRWAV_INVALID_FILE;
  1255. }
  1256. pHeaderOut->sizeInBytes = drwav__bytes_to_u64(sizeInBytes) - 24; /* <-- Subtract 24 because w64 includes the size of the header. */
  1257. pHeaderOut->paddingSize = drwav__chunk_padding_size_w64(pHeaderOut->sizeInBytes);
  1258. *pRunningBytesReadOut += 24;
  1259. }
  1260. return DRWAV_SUCCESS;
  1261. }
  1262. static drwav_bool32 drwav__seek_forward(drwav_seek_proc onSeek, drwav_uint64 offset, void* pUserData)
  1263. {
  1264. drwav_uint64 bytesRemainingToSeek = offset;
  1265. while (bytesRemainingToSeek > 0) {
  1266. if (bytesRemainingToSeek > 0x7FFFFFFF) {
  1267. if (!onSeek(pUserData, 0x7FFFFFFF, drwav_seek_origin_current)) {
  1268. return DRWAV_FALSE;
  1269. }
  1270. bytesRemainingToSeek -= 0x7FFFFFFF;
  1271. } else {
  1272. if (!onSeek(pUserData, (int)bytesRemainingToSeek, drwav_seek_origin_current)) {
  1273. return DRWAV_FALSE;
  1274. }
  1275. bytesRemainingToSeek = 0;
  1276. }
  1277. }
  1278. return DRWAV_TRUE;
  1279. }
  1280. static drwav_bool32 drwav__seek_from_start(drwav_seek_proc onSeek, drwav_uint64 offset, void* pUserData)
  1281. {
  1282. if (offset <= 0x7FFFFFFF) {
  1283. return onSeek(pUserData, (int)offset, drwav_seek_origin_start);
  1284. }
  1285. /* Larger than 32-bit seek. */
  1286. if (!onSeek(pUserData, 0x7FFFFFFF, drwav_seek_origin_start)) {
  1287. return DRWAV_FALSE;
  1288. }
  1289. offset -= 0x7FFFFFFF;
  1290. for (;;) {
  1291. if (offset <= 0x7FFFFFFF) {
  1292. return onSeek(pUserData, (int)offset, drwav_seek_origin_current);
  1293. }
  1294. if (!onSeek(pUserData, 0x7FFFFFFF, drwav_seek_origin_current)) {
  1295. return DRWAV_FALSE;
  1296. }
  1297. offset -= 0x7FFFFFFF;
  1298. }
  1299. /* Should never get here. */
  1300. /*return DRWAV_TRUE; */
  1301. }
  1302. static drwav_bool32 drwav__read_fmt(drwav_read_proc onRead, drwav_seek_proc onSeek, void* pUserData, drwav_container container, drwav_uint64* pRunningBytesReadOut, drwav_fmt* fmtOut)
  1303. {
  1304. drwav_chunk_header header;
  1305. drwav_uint8 fmt[16];
  1306. if (drwav__read_chunk_header(onRead, pUserData, container, pRunningBytesReadOut, &header) != DRWAV_SUCCESS) {
  1307. return DRWAV_FALSE;
  1308. }
  1309. /* Skip non-fmt chunks. */
  1310. while (((container == drwav_container_riff || container == drwav_container_rf64) && !drwav__fourcc_equal(header.id.fourcc, "fmt ")) || (container == drwav_container_w64 && !drwav__guid_equal(header.id.guid, drwavGUID_W64_FMT))) {
  1311. if (!drwav__seek_forward(onSeek, header.sizeInBytes + header.paddingSize, pUserData)) {
  1312. return DRWAV_FALSE;
  1313. }
  1314. *pRunningBytesReadOut += header.sizeInBytes + header.paddingSize;
  1315. /* Try the next header. */
  1316. if (drwav__read_chunk_header(onRead, pUserData, container, pRunningBytesReadOut, &header) != DRWAV_SUCCESS) {
  1317. return DRWAV_FALSE;
  1318. }
  1319. }
  1320. /* Validation. */
  1321. if (container == drwav_container_riff || container == drwav_container_rf64) {
  1322. if (!drwav__fourcc_equal(header.id.fourcc, "fmt ")) {
  1323. return DRWAV_FALSE;
  1324. }
  1325. } else {
  1326. if (!drwav__guid_equal(header.id.guid, drwavGUID_W64_FMT)) {
  1327. return DRWAV_FALSE;
  1328. }
  1329. }
  1330. if (onRead(pUserData, fmt, sizeof(fmt)) != sizeof(fmt)) {
  1331. return DRWAV_FALSE;
  1332. }
  1333. *pRunningBytesReadOut += sizeof(fmt);
  1334. fmtOut->formatTag = drwav__bytes_to_u16(fmt + 0);
  1335. fmtOut->channels = drwav__bytes_to_u16(fmt + 2);
  1336. fmtOut->sampleRate = drwav__bytes_to_u32(fmt + 4);
  1337. fmtOut->avgBytesPerSec = drwav__bytes_to_u32(fmt + 8);
  1338. fmtOut->blockAlign = drwav__bytes_to_u16(fmt + 12);
  1339. fmtOut->bitsPerSample = drwav__bytes_to_u16(fmt + 14);
  1340. fmtOut->extendedSize = 0;
  1341. fmtOut->validBitsPerSample = 0;
  1342. fmtOut->channelMask = 0;
  1343. memset(fmtOut->subFormat, 0, sizeof(fmtOut->subFormat));
  1344. if (header.sizeInBytes > 16) {
  1345. drwav_uint8 fmt_cbSize[2];
  1346. int bytesReadSoFar = 0;
  1347. if (onRead(pUserData, fmt_cbSize, sizeof(fmt_cbSize)) != sizeof(fmt_cbSize)) {
  1348. return DRWAV_FALSE; /* Expecting more data. */
  1349. }
  1350. *pRunningBytesReadOut += sizeof(fmt_cbSize);
  1351. bytesReadSoFar = 18;
  1352. fmtOut->extendedSize = drwav__bytes_to_u16(fmt_cbSize);
  1353. if (fmtOut->extendedSize > 0) {
  1354. /* Simple validation. */
  1355. if (fmtOut->formatTag == DR_WAVE_FORMAT_EXTENSIBLE) {
  1356. if (fmtOut->extendedSize != 22) {
  1357. return DRWAV_FALSE;
  1358. }
  1359. }
  1360. if (fmtOut->formatTag == DR_WAVE_FORMAT_EXTENSIBLE) {
  1361. drwav_uint8 fmtext[22];
  1362. if (onRead(pUserData, fmtext, fmtOut->extendedSize) != fmtOut->extendedSize) {
  1363. return DRWAV_FALSE; /* Expecting more data. */
  1364. }
  1365. fmtOut->validBitsPerSample = drwav__bytes_to_u16(fmtext + 0);
  1366. fmtOut->channelMask = drwav__bytes_to_u32(fmtext + 2);
  1367. drwav__bytes_to_guid(fmtext + 6, fmtOut->subFormat);
  1368. } else {
  1369. if (!onSeek(pUserData, fmtOut->extendedSize, drwav_seek_origin_current)) {
  1370. return DRWAV_FALSE;
  1371. }
  1372. }
  1373. *pRunningBytesReadOut += fmtOut->extendedSize;
  1374. bytesReadSoFar += fmtOut->extendedSize;
  1375. }
  1376. /* Seek past any leftover bytes. For w64 the leftover will be defined based on the chunk size. */
  1377. if (!onSeek(pUserData, (int)(header.sizeInBytes - bytesReadSoFar), drwav_seek_origin_current)) {
  1378. return DRWAV_FALSE;
  1379. }
  1380. *pRunningBytesReadOut += (header.sizeInBytes - bytesReadSoFar);
  1381. }
  1382. if (header.paddingSize > 0) {
  1383. if (!onSeek(pUserData, header.paddingSize, drwav_seek_origin_current)) {
  1384. return DRWAV_FALSE;
  1385. }
  1386. *pRunningBytesReadOut += header.paddingSize;
  1387. }
  1388. return DRWAV_TRUE;
  1389. }
  1390. static size_t drwav__on_read(drwav_read_proc onRead, void* pUserData, void* pBufferOut, size_t bytesToRead, drwav_uint64* pCursor)
  1391. {
  1392. size_t bytesRead;
  1393. DRWAV_ASSERT(onRead != NULL);
  1394. DRWAV_ASSERT(pCursor != NULL);
  1395. bytesRead = onRead(pUserData, pBufferOut, bytesToRead);
  1396. *pCursor += bytesRead;
  1397. return bytesRead;
  1398. }
  1399. #if 0
  1400. static drwav_bool32 drwav__on_seek(drwav_seek_proc onSeek, void* pUserData, int offset, drwav_seek_origin origin, drwav_uint64* pCursor)
  1401. {
  1402. DRWAV_ASSERT(onSeek != NULL);
  1403. DRWAV_ASSERT(pCursor != NULL);
  1404. if (!onSeek(pUserData, offset, origin)) {
  1405. return DRWAV_FALSE;
  1406. }
  1407. if (origin == drwav_seek_origin_start) {
  1408. *pCursor = offset;
  1409. } else {
  1410. *pCursor += offset;
  1411. }
  1412. return DRWAV_TRUE;
  1413. }
  1414. #endif
  1415. static drwav_uint32 drwav_get_bytes_per_pcm_frame(drwav* pWav)
  1416. {
  1417. /*
  1418. The bytes per frame is a bit ambiguous. It can be either be based on the bits per sample, or the block align. The way I'm doing it here
  1419. is that if the bits per sample is a multiple of 8, use floor(bitsPerSample*channels/8), otherwise fall back to the block align.
  1420. */
  1421. if ((pWav->bitsPerSample & 0x7) == 0) {
  1422. /* Bits per sample is a multiple of 8. */
  1423. return (pWav->bitsPerSample * pWav->fmt.channels) >> 3;
  1424. } else {
  1425. return pWav->fmt.blockAlign;
  1426. }
  1427. }
  1428. DRWAV_API drwav_uint16 drwav_fmt_get_format(const drwav_fmt* pFMT)
  1429. {
  1430. if (pFMT == NULL) {
  1431. return 0;
  1432. }
  1433. if (pFMT->formatTag != DR_WAVE_FORMAT_EXTENSIBLE) {
  1434. return pFMT->formatTag;
  1435. } else {
  1436. return drwav__bytes_to_u16(pFMT->subFormat); /* Only the first two bytes are required. */
  1437. }
  1438. }
  1439. static drwav_bool32 drwav_preinit(drwav* pWav, drwav_read_proc onRead, drwav_seek_proc onSeek, void* pReadSeekUserData, const drwav_allocation_callbacks* pAllocationCallbacks)
  1440. {
  1441. if (pWav == NULL || onRead == NULL || onSeek == NULL) {
  1442. return DRWAV_FALSE;
  1443. }
  1444. DRWAV_ZERO_MEMORY(pWav, sizeof(*pWav));
  1445. pWav->onRead = onRead;
  1446. pWav->onSeek = onSeek;
  1447. pWav->pUserData = pReadSeekUserData;
  1448. pWav->allocationCallbacks = drwav_copy_allocation_callbacks_or_defaults(pAllocationCallbacks);
  1449. if (pWav->allocationCallbacks.onFree == NULL || (pWav->allocationCallbacks.onMalloc == NULL && pWav->allocationCallbacks.onRealloc == NULL)) {
  1450. return DRWAV_FALSE; /* Invalid allocation callbacks. */
  1451. }
  1452. return DRWAV_TRUE;
  1453. }
  1454. static drwav_bool32 drwav_init__internal(drwav* pWav, drwav_chunk_proc onChunk, void* pChunkUserData, drwav_uint32 flags)
  1455. {
  1456. /* This function assumes drwav_preinit() has been called beforehand. */
  1457. drwav_uint64 cursor; /* <-- Keeps track of the byte position so we can seek to specific locations. */
  1458. drwav_bool32 sequential;
  1459. drwav_uint8 riff[4];
  1460. drwav_fmt fmt;
  1461. unsigned short translatedFormatTag;
  1462. drwav_bool32 foundDataChunk;
  1463. drwav_uint64 dataChunkSize = 0; /* <-- Important! Don't explicitly set this to 0 anywhere else. Calculation of the size of the data chunk is performed in different paths depending on the container. */
  1464. drwav_uint64 sampleCountFromFactChunk = 0; /* Same as dataChunkSize - make sure this is the only place this is initialized to 0. */
  1465. drwav_uint64 chunkSize;
  1466. cursor = 0;
  1467. sequential = (flags & DRWAV_SEQUENTIAL) != 0;
  1468. /* The first 4 bytes should be the RIFF identifier. */
  1469. if (drwav__on_read(pWav->onRead, pWav->pUserData, riff, sizeof(riff), &cursor) != sizeof(riff)) {
  1470. return DRWAV_FALSE;
  1471. }
  1472. /*
  1473. The first 4 bytes can be used to identify the container. For RIFF files it will start with "RIFF" and for
  1474. w64 it will start with "riff".
  1475. */
  1476. if (drwav__fourcc_equal(riff, "RIFF")) {
  1477. pWav->container = drwav_container_riff;
  1478. } else if (drwav__fourcc_equal(riff, "riff")) {
  1479. int i;
  1480. drwav_uint8 riff2[12];
  1481. pWav->container = drwav_container_w64;
  1482. /* Check the rest of the GUID for validity. */
  1483. if (drwav__on_read(pWav->onRead, pWav->pUserData, riff2, sizeof(riff2), &cursor) != sizeof(riff2)) {
  1484. return DRWAV_FALSE;
  1485. }
  1486. for (i = 0; i < 12; ++i) {
  1487. if (riff2[i] != drwavGUID_W64_RIFF[i+4]) {
  1488. return DRWAV_FALSE;
  1489. }
  1490. }
  1491. } else if (drwav__fourcc_equal(riff, "RF64")) {
  1492. pWav->container = drwav_container_rf64;
  1493. } else {
  1494. return DRWAV_FALSE; /* Unknown or unsupported container. */
  1495. }
  1496. if (pWav->container == drwav_container_riff || pWav->container == drwav_container_rf64) {
  1497. drwav_uint8 chunkSizeBytes[4];
  1498. drwav_uint8 wave[4];
  1499. /* RIFF/WAVE */
  1500. if (drwav__on_read(pWav->onRead, pWav->pUserData, chunkSizeBytes, sizeof(chunkSizeBytes), &cursor) != sizeof(chunkSizeBytes)) {
  1501. return DRWAV_FALSE;
  1502. }
  1503. if (pWav->container == drwav_container_riff) {
  1504. if (drwav__bytes_to_u32(chunkSizeBytes) < 36) {
  1505. return DRWAV_FALSE; /* Chunk size should always be at least 36 bytes. */
  1506. }
  1507. } else {
  1508. if (drwav__bytes_to_u32(chunkSizeBytes) != 0xFFFFFFFF) {
  1509. return DRWAV_FALSE; /* Chunk size should always be set to -1/0xFFFFFFFF for RF64. The actual size is retrieved later. */
  1510. }
  1511. }
  1512. if (drwav__on_read(pWav->onRead, pWav->pUserData, wave, sizeof(wave), &cursor) != sizeof(wave)) {
  1513. return DRWAV_FALSE;
  1514. }
  1515. if (!drwav__fourcc_equal(wave, "WAVE")) {
  1516. return DRWAV_FALSE; /* Expecting "WAVE". */
  1517. }
  1518. } else {
  1519. drwav_uint8 chunkSizeBytes[8];
  1520. drwav_uint8 wave[16];
  1521. /* W64 */
  1522. if (drwav__on_read(pWav->onRead, pWav->pUserData, chunkSizeBytes, sizeof(chunkSizeBytes), &cursor) != sizeof(chunkSizeBytes)) {
  1523. return DRWAV_FALSE;
  1524. }
  1525. if (drwav__bytes_to_u64(chunkSizeBytes) < 80) {
  1526. return DRWAV_FALSE;
  1527. }
  1528. if (drwav__on_read(pWav->onRead, pWav->pUserData, wave, sizeof(wave), &cursor) != sizeof(wave)) {
  1529. return DRWAV_FALSE;
  1530. }
  1531. if (!drwav__guid_equal(wave, drwavGUID_W64_WAVE)) {
  1532. return DRWAV_FALSE;
  1533. }
  1534. }
  1535. /* For RF64, the "ds64" chunk must come next, before the "fmt " chunk. */
  1536. if (pWav->container == drwav_container_rf64) {
  1537. drwav_uint8 sizeBytes[8];
  1538. drwav_uint64 bytesRemainingInChunk;
  1539. drwav_chunk_header header;
  1540. drwav_result result = drwav__read_chunk_header(pWav->onRead, pWav->pUserData, pWav->container, &cursor, &header);
  1541. if (result != DRWAV_SUCCESS) {
  1542. return DRWAV_FALSE;
  1543. }
  1544. if (!drwav__fourcc_equal(header.id.fourcc, "ds64")) {
  1545. return DRWAV_FALSE; /* Expecting "ds64". */
  1546. }
  1547. bytesRemainingInChunk = header.sizeInBytes + header.paddingSize;
  1548. /* We don't care about the size of the RIFF chunk - skip it. */
  1549. if (!drwav__seek_forward(pWav->onSeek, 8, pWav->pUserData)) {
  1550. return DRWAV_FALSE;
  1551. }
  1552. bytesRemainingInChunk -= 8;
  1553. cursor += 8;
  1554. /* Next 8 bytes is the size of the "data" chunk. */
  1555. if (drwav__on_read(pWav->onRead, pWav->pUserData, sizeBytes, sizeof(sizeBytes), &cursor) != sizeof(sizeBytes)) {
  1556. return DRWAV_FALSE;
  1557. }
  1558. bytesRemainingInChunk -= 8;
  1559. dataChunkSize = drwav__bytes_to_u64(sizeBytes);
  1560. /* Next 8 bytes is the same count which we would usually derived from the FACT chunk if it was available. */
  1561. if (drwav__on_read(pWav->onRead, pWav->pUserData, sizeBytes, sizeof(sizeBytes), &cursor) != sizeof(sizeBytes)) {
  1562. return DRWAV_FALSE;
  1563. }
  1564. bytesRemainingInChunk -= 8;
  1565. sampleCountFromFactChunk = drwav__bytes_to_u64(sizeBytes);
  1566. /* Skip over everything else. */
  1567. if (!drwav__seek_forward(pWav->onSeek, bytesRemainingInChunk, pWav->pUserData)) {
  1568. return DRWAV_FALSE;
  1569. }
  1570. cursor += bytesRemainingInChunk;
  1571. }
  1572. /* The next bytes should be the "fmt " chunk. */
  1573. if (!drwav__read_fmt(pWav->onRead, pWav->onSeek, pWav->pUserData, pWav->container, &cursor, &fmt)) {
  1574. return DRWAV_FALSE; /* Failed to read the "fmt " chunk. */
  1575. }
  1576. /* Basic validation. */
  1577. if ((fmt.sampleRate == 0 || fmt.sampleRate > DRWAV_MAX_SAMPLE_RATE) ||
  1578. (fmt.channels == 0 || fmt.channels > DRWAV_MAX_CHANNELS) ||
  1579. (fmt.bitsPerSample == 0 || fmt.bitsPerSample > DRWAV_MAX_BITS_PER_SAMPLE) ||
  1580. fmt.blockAlign == 0) {
  1581. return DRWAV_FALSE; /* Probably an invalid WAV file. */
  1582. }
  1583. /* Translate the internal format. */
  1584. translatedFormatTag = fmt.formatTag;
  1585. if (translatedFormatTag == DR_WAVE_FORMAT_EXTENSIBLE) {
  1586. translatedFormatTag = drwav__bytes_to_u16(fmt.subFormat + 0);
  1587. }
  1588. /*
  1589. We need to enumerate over each chunk for two reasons:
  1590. 1) The "data" chunk may not be the next one
  1591. 2) We may want to report each chunk back to the client
  1592. In order to correctly report each chunk back to the client we will need to keep looping until the end of the file.
  1593. */
  1594. foundDataChunk = DRWAV_FALSE;
  1595. /* The next chunk we care about is the "data" chunk. This is not necessarily the next chunk so we'll need to loop. */
  1596. for (;;)
  1597. {
  1598. drwav_chunk_header header;
  1599. drwav_result result = drwav__read_chunk_header(pWav->onRead, pWav->pUserData, pWav->container, &cursor, &header);
  1600. if (result != DRWAV_SUCCESS) {
  1601. if (!foundDataChunk) {
  1602. return DRWAV_FALSE;
  1603. } else {
  1604. break; /* Probably at the end of the file. Get out of the loop. */
  1605. }
  1606. }
  1607. /* Tell the client about this chunk. */
  1608. if (!sequential && onChunk != NULL) {
  1609. drwav_uint64 callbackBytesRead = onChunk(pChunkUserData, pWav->onRead, pWav->onSeek, pWav->pUserData, &header, pWav->container, &fmt);
  1610. /*
  1611. dr_wav may need to read the contents of the chunk, so we now need to seek back to the position before
  1612. we called the callback.
  1613. */
  1614. if (callbackBytesRead > 0) {
  1615. if (!drwav__seek_from_start(pWav->onSeek, cursor, pWav->pUserData)) {
  1616. return DRWAV_FALSE;
  1617. }
  1618. }
  1619. }
  1620. if (!foundDataChunk) {
  1621. pWav->dataChunkDataPos = cursor;
  1622. }
  1623. chunkSize = header.sizeInBytes;
  1624. if (pWav->container == drwav_container_riff || pWav->container == drwav_container_rf64) {
  1625. if (drwav__fourcc_equal(header.id.fourcc, "data")) {
  1626. foundDataChunk = DRWAV_TRUE;
  1627. if (pWav->container != drwav_container_rf64) { /* The data chunk size for RF64 will always be set to 0xFFFFFFFF here. It was set to it's true value earlier. */
  1628. dataChunkSize = chunkSize;
  1629. }
  1630. }
  1631. } else {
  1632. if (drwav__guid_equal(header.id.guid, drwavGUID_W64_DATA)) {
  1633. foundDataChunk = DRWAV_TRUE;
  1634. dataChunkSize = chunkSize;
  1635. }
  1636. }
  1637. /*
  1638. If at this point we have found the data chunk and we're running in sequential mode, we need to break out of this loop. The reason for
  1639. this is that we would otherwise require a backwards seek which sequential mode forbids.
  1640. */
  1641. if (foundDataChunk && sequential) {
  1642. break;
  1643. }
  1644. /* Optional. Get the total sample count from the FACT chunk. This is useful for compressed formats. */
  1645. if (pWav->container == drwav_container_riff) {
  1646. if (drwav__fourcc_equal(header.id.fourcc, "fact")) {
  1647. drwav_uint32 sampleCount;
  1648. if (drwav__on_read(pWav->onRead, pWav->pUserData, &sampleCount, 4, &cursor) != 4) {
  1649. return DRWAV_FALSE;
  1650. }
  1651. chunkSize -= 4;
  1652. if (!foundDataChunk) {
  1653. pWav->dataChunkDataPos = cursor;
  1654. }
  1655. /*
  1656. The sample count in the "fact" chunk is either unreliable, or I'm not understanding it properly. For now I am only enabling this
  1657. for Microsoft ADPCM formats.
  1658. */
  1659. if (pWav->translatedFormatTag == DR_WAVE_FORMAT_ADPCM) {
  1660. sampleCountFromFactChunk = sampleCount;
  1661. } else {
  1662. sampleCountFromFactChunk = 0;
  1663. }
  1664. }
  1665. } else if (pWav->container == drwav_container_w64) {
  1666. if (drwav__guid_equal(header.id.guid, drwavGUID_W64_FACT)) {
  1667. if (drwav__on_read(pWav->onRead, pWav->pUserData, &sampleCountFromFactChunk, 8, &cursor) != 8) {
  1668. return DRWAV_FALSE;
  1669. }
  1670. chunkSize -= 8;
  1671. if (!foundDataChunk) {
  1672. pWav->dataChunkDataPos = cursor;
  1673. }
  1674. }
  1675. } else if (pWav->container == drwav_container_rf64) {
  1676. /* We retrieved the sample count from the ds64 chunk earlier so no need to do that here. */
  1677. }
  1678. /* "smpl" chunk. */
  1679. if (pWav->container == drwav_container_riff || pWav->container == drwav_container_rf64) {
  1680. if (drwav__fourcc_equal(header.id.fourcc, "smpl")) {
  1681. drwav_uint8 smplHeaderData[36]; /* 36 = size of the smpl header section, not including the loop data. */
  1682. if (chunkSize >= sizeof(smplHeaderData)) {
  1683. drwav_uint64 bytesJustRead = drwav__on_read(pWav->onRead, pWav->pUserData, smplHeaderData, sizeof(smplHeaderData), &cursor);
  1684. chunkSize -= bytesJustRead;
  1685. if (bytesJustRead == sizeof(smplHeaderData)) {
  1686. drwav_uint32 iLoop;
  1687. pWav->smpl.manufacturer = drwav__bytes_to_u32(smplHeaderData+0);
  1688. pWav->smpl.product = drwav__bytes_to_u32(smplHeaderData+4);
  1689. pWav->smpl.samplePeriod = drwav__bytes_to_u32(smplHeaderData+8);
  1690. pWav->smpl.midiUnityNotes = drwav__bytes_to_u32(smplHeaderData+12);
  1691. pWav->smpl.midiPitchFraction = drwav__bytes_to_u32(smplHeaderData+16);
  1692. pWav->smpl.smpteFormat = drwav__bytes_to_u32(smplHeaderData+20);
  1693. pWav->smpl.smpteOffset = drwav__bytes_to_u32(smplHeaderData+24);
  1694. pWav->smpl.numSampleLoops = drwav__bytes_to_u32(smplHeaderData+28);
  1695. pWav->smpl.samplerData = drwav__bytes_to_u32(smplHeaderData+32);
  1696. for (iLoop = 0; iLoop < pWav->smpl.numSampleLoops && iLoop < drwav_countof(pWav->smpl.loops); ++iLoop) {
  1697. drwav_uint8 smplLoopData[24]; /* 24 = size of a loop section in the smpl chunk. */
  1698. bytesJustRead = drwav__on_read(pWav->onRead, pWav->pUserData, smplLoopData, sizeof(smplLoopData), &cursor);
  1699. chunkSize -= bytesJustRead;
  1700. if (bytesJustRead == sizeof(smplLoopData)) {
  1701. pWav->smpl.loops[iLoop].cuePointId = drwav__bytes_to_u32(smplLoopData+0);
  1702. pWav->smpl.loops[iLoop].type = drwav__bytes_to_u32(smplLoopData+4);
  1703. pWav->smpl.loops[iLoop].start = drwav__bytes_to_u32(smplLoopData+8);
  1704. pWav->smpl.loops[iLoop].end = drwav__bytes_to_u32(smplLoopData+12);
  1705. pWav->smpl.loops[iLoop].fraction = drwav__bytes_to_u32(smplLoopData+16);
  1706. pWav->smpl.loops[iLoop].playCount = drwav__bytes_to_u32(smplLoopData+20);
  1707. } else {
  1708. break; /* Break from the smpl loop for loop. */
  1709. }
  1710. }
  1711. }
  1712. } else {
  1713. /* Looks like invalid data. Ignore the chunk. */
  1714. }
  1715. }
  1716. } else {
  1717. if (drwav__guid_equal(header.id.guid, drwavGUID_W64_SMPL)) {
  1718. /*
  1719. This path will be hit when a W64 WAV file contains a smpl chunk. I don't have a sample file to test this path, so a contribution
  1720. is welcome to add support for this.
  1721. */
  1722. }
  1723. }
  1724. /* Make sure we seek past the padding. */
  1725. chunkSize += header.paddingSize;
  1726. if (!drwav__seek_forward(pWav->onSeek, chunkSize, pWav->pUserData)) {
  1727. break;
  1728. }
  1729. cursor += chunkSize;
  1730. if (!foundDataChunk) {
  1731. pWav->dataChunkDataPos = cursor;
  1732. }
  1733. }
  1734. /* If we haven't found a data chunk, return an error. */
  1735. if (!foundDataChunk) {
  1736. return DRWAV_FALSE;
  1737. }
  1738. /* We may have moved passed the data chunk. If so we need to move back. If running in sequential mode we can assume we are already sitting on the data chunk. */
  1739. if (!sequential) {
  1740. if (!drwav__seek_from_start(pWav->onSeek, pWav->dataChunkDataPos, pWav->pUserData)) {
  1741. return DRWAV_FALSE;
  1742. }
  1743. cursor = pWav->dataChunkDataPos;
  1744. }
  1745. /* At this point we should be sitting on the first byte of the raw audio data. */
  1746. pWav->fmt = fmt;
  1747. pWav->sampleRate = fmt.sampleRate;
  1748. pWav->channels = fmt.channels;
  1749. pWav->bitsPerSample = fmt.bitsPerSample;
  1750. pWav->bytesRemaining = dataChunkSize;
  1751. pWav->translatedFormatTag = translatedFormatTag;
  1752. pWav->dataChunkDataSize = dataChunkSize;
  1753. if (sampleCountFromFactChunk != 0) {
  1754. pWav->totalPCMFrameCount = sampleCountFromFactChunk;
  1755. } else {
  1756. pWav->totalPCMFrameCount = dataChunkSize / drwav_get_bytes_per_pcm_frame(pWav);
  1757. if (pWav->translatedFormatTag == DR_WAVE_FORMAT_ADPCM) {
  1758. drwav_uint64 totalBlockHeaderSizeInBytes;
  1759. drwav_uint64 blockCount = dataChunkSize / fmt.blockAlign;
  1760. /* Make sure any trailing partial block is accounted for. */
  1761. if ((blockCount * fmt.blockAlign) < dataChunkSize) {
  1762. blockCount += 1;
  1763. }
  1764. /* We decode two samples per byte. There will be blockCount headers in the data chunk. This is enough to know how to calculate the total PCM frame count. */
  1765. totalBlockHeaderSizeInBytes = blockCount * (6*fmt.channels);
  1766. pWav->totalPCMFrameCount = ((dataChunkSize - totalBlockHeaderSizeInBytes) * 2) / fmt.channels;
  1767. }
  1768. if (pWav->translatedFormatTag == DR_WAVE_FORMAT_DVI_ADPCM) {
  1769. drwav_uint64 totalBlockHeaderSizeInBytes;
  1770. drwav_uint64 blockCount = dataChunkSize / fmt.blockAlign;
  1771. /* Make sure any trailing partial block is accounted for. */
  1772. if ((blockCount * fmt.blockAlign) < dataChunkSize) {
  1773. blockCount += 1;
  1774. }
  1775. /* We decode two samples per byte. There will be blockCount headers in the data chunk. This is enough to know how to calculate the total PCM frame count. */
  1776. totalBlockHeaderSizeInBytes = blockCount * (4*fmt.channels);
  1777. pWav->totalPCMFrameCount = ((dataChunkSize - totalBlockHeaderSizeInBytes) * 2) / fmt.channels;
  1778. /* The header includes a decoded sample for each channel which acts as the initial predictor sample. */
  1779. pWav->totalPCMFrameCount += blockCount;
  1780. }
  1781. }
  1782. /* Some formats only support a certain number of channels. */
  1783. if (pWav->translatedFormatTag == DR_WAVE_FORMAT_ADPCM || pWav->translatedFormatTag == DR_WAVE_FORMAT_DVI_ADPCM) {
  1784. if (pWav->channels > 2) {
  1785. return DRWAV_FALSE;
  1786. }
  1787. }
  1788. #ifdef DR_WAV_LIBSNDFILE_COMPAT
  1789. /*
  1790. I use libsndfile as a benchmark for testing, however in the version I'm using (from the Windows installer on the libsndfile website),
  1791. it appears the total sample count libsndfile uses for MS-ADPCM is incorrect. It would seem they are computing the total sample count
  1792. from the number of blocks, however this results in the inclusion of extra silent samples at the end of the last block. The correct
  1793. way to know the total sample count is to inspect the "fact" chunk, which should always be present for compressed formats, and should
  1794. always include the sample count. This little block of code below is only used to emulate the libsndfile logic so I can properly run my
  1795. correctness tests against libsndfile, and is disabled by default.
  1796. */
  1797. if (pWav->translatedFormatTag == DR_WAVE_FORMAT_ADPCM) {
  1798. drwav_uint64 blockCount = dataChunkSize / fmt.blockAlign;
  1799. pWav->totalPCMFrameCount = (((blockCount * (fmt.blockAlign - (6*pWav->channels))) * 2)) / fmt.channels; /* x2 because two samples per byte. */
  1800. }
  1801. if (pWav->translatedFormatTag == DR_WAVE_FORMAT_DVI_ADPCM) {
  1802. drwav_uint64 blockCount = dataChunkSize / fmt.blockAlign;
  1803. pWav->totalPCMFrameCount = (((blockCount * (fmt.blockAlign - (4*pWav->channels))) * 2) + (blockCount * pWav->channels)) / fmt.channels;
  1804. }
  1805. #endif
  1806. return DRWAV_TRUE;
  1807. }
  1808. DRWAV_API drwav_bool32 drwav_init(drwav* pWav, drwav_read_proc onRead, drwav_seek_proc onSeek, void* pUserData, const drwav_allocation_callbacks* pAllocationCallbacks)
  1809. {
  1810. return drwav_init_ex(pWav, onRead, onSeek, NULL, pUserData, NULL, 0, pAllocationCallbacks);
  1811. }
  1812. DRWAV_API drwav_bool32 drwav_init_ex(drwav* pWav, drwav_read_proc onRead, drwav_seek_proc onSeek, drwav_chunk_proc onChunk, void* pReadSeekUserData, void* pChunkUserData, drwav_uint32 flags, const drwav_allocation_callbacks* pAllocationCallbacks)
  1813. {
  1814. if (!drwav_preinit(pWav, onRead, onSeek, pReadSeekUserData, pAllocationCallbacks)) {
  1815. return DRWAV_FALSE;
  1816. }
  1817. return drwav_init__internal(pWav, onChunk, pChunkUserData, flags);
  1818. }
  1819. static drwav_uint32 drwav__riff_chunk_size_riff(drwav_uint64 dataChunkSize)
  1820. {
  1821. drwav_uint64 chunkSize = 4 + 24 + dataChunkSize + drwav__chunk_padding_size_riff(dataChunkSize); /* 4 = "WAVE". 24 = "fmt " chunk. */
  1822. if (chunkSize > 0xFFFFFFFFUL) {
  1823. chunkSize = 0xFFFFFFFFUL;
  1824. }
  1825. return (drwav_uint32)chunkSize; /* Safe cast due to the clamp above. */
  1826. }
  1827. static drwav_uint32 drwav__data_chunk_size_riff(drwav_uint64 dataChunkSize)
  1828. {
  1829. if (dataChunkSize <= 0xFFFFFFFFUL) {
  1830. return (drwav_uint32)dataChunkSize;
  1831. } else {
  1832. return 0xFFFFFFFFUL;
  1833. }
  1834. }
  1835. static drwav_uint64 drwav__riff_chunk_size_w64(drwav_uint64 dataChunkSize)
  1836. {
  1837. drwav_uint64 dataSubchunkPaddingSize = drwav__chunk_padding_size_w64(dataChunkSize);
  1838. return 80 + 24 + dataChunkSize + dataSubchunkPaddingSize; /* +24 because W64 includes the size of the GUID and size fields. */
  1839. }
  1840. static drwav_uint64 drwav__data_chunk_size_w64(drwav_uint64 dataChunkSize)
  1841. {
  1842. return 24 + dataChunkSize; /* +24 because W64 includes the size of the GUID and size fields. */
  1843. }
  1844. static drwav_uint64 drwav__riff_chunk_size_rf64(drwav_uint64 dataChunkSize)
  1845. {
  1846. drwav_uint64 chunkSize = 4 + 36 + 24 + dataChunkSize + drwav__chunk_padding_size_riff(dataChunkSize); /* 4 = "WAVE". 36 = "ds64" chunk. 24 = "fmt " chunk. */
  1847. if (chunkSize > 0xFFFFFFFFUL) {
  1848. chunkSize = 0xFFFFFFFFUL;
  1849. }
  1850. return chunkSize;
  1851. }
  1852. static drwav_uint64 drwav__data_chunk_size_rf64(drwav_uint64 dataChunkSize)
  1853. {
  1854. return dataChunkSize;
  1855. }
  1856. static size_t drwav__write(drwav* pWav, const void* pData, size_t dataSize)
  1857. {
  1858. DRWAV_ASSERT(pWav != NULL);
  1859. DRWAV_ASSERT(pWav->onWrite != NULL);
  1860. /* Generic write. Assumes no byte reordering required. */
  1861. return pWav->onWrite(pWav->pUserData, pData, dataSize);
  1862. }
  1863. static size_t drwav__write_u16ne_to_le(drwav* pWav, drwav_uint16 value)
  1864. {
  1865. DRWAV_ASSERT(pWav != NULL);
  1866. DRWAV_ASSERT(pWav->onWrite != NULL);
  1867. if (!drwav__is_little_endian()) {
  1868. value = drwav__bswap16(value);
  1869. }
  1870. return drwav__write(pWav, &value, 2);
  1871. }
  1872. static size_t drwav__write_u32ne_to_le(drwav* pWav, drwav_uint32 value)
  1873. {
  1874. DRWAV_ASSERT(pWav != NULL);
  1875. DRWAV_ASSERT(pWav->onWrite != NULL);
  1876. if (!drwav__is_little_endian()) {
  1877. value = drwav__bswap32(value);
  1878. }
  1879. return drwav__write(pWav, &value, 4);
  1880. }
  1881. static size_t drwav__write_u64ne_to_le(drwav* pWav, drwav_uint64 value)
  1882. {
  1883. DRWAV_ASSERT(pWav != NULL);
  1884. DRWAV_ASSERT(pWav->onWrite != NULL);
  1885. if (!drwav__is_little_endian()) {
  1886. value = drwav__bswap64(value);
  1887. }
  1888. return drwav__write(pWav, &value, 8);
  1889. }
  1890. static drwav_bool32 drwav_preinit_write(drwav* pWav, const drwav_data_format* pFormat, drwav_bool32 isSequential, drwav_write_proc onWrite, drwav_seek_proc onSeek, void* pUserData, const drwav_allocation_callbacks* pAllocationCallbacks)
  1891. {
  1892. if (pWav == NULL || onWrite == NULL) {
  1893. return DRWAV_FALSE;
  1894. }
  1895. if (!isSequential && onSeek == NULL) {
  1896. return DRWAV_FALSE; /* <-- onSeek is required when in non-sequential mode. */
  1897. }
  1898. /* Not currently supporting compressed formats. Will need to add support for the "fact" chunk before we enable this. */
  1899. if (pFormat->format == DR_WAVE_FORMAT_EXTENSIBLE) {
  1900. return DRWAV_FALSE;
  1901. }
  1902. if (pFormat->format == DR_WAVE_FORMAT_ADPCM || pFormat->format == DR_WAVE_FORMAT_DVI_ADPCM) {
  1903. return DRWAV_FALSE;
  1904. }
  1905. DRWAV_ZERO_MEMORY(pWav, sizeof(*pWav));
  1906. pWav->onWrite = onWrite;
  1907. pWav->onSeek = onSeek;
  1908. pWav->pUserData = pUserData;
  1909. pWav->allocationCallbacks = drwav_copy_allocation_callbacks_or_defaults(pAllocationCallbacks);
  1910. if (pWav->allocationCallbacks.onFree == NULL || (pWav->allocationCallbacks.onMalloc == NULL && pWav->allocationCallbacks.onRealloc == NULL)) {
  1911. return DRWAV_FALSE; /* Invalid allocation callbacks. */
  1912. }
  1913. pWav->fmt.formatTag = (drwav_uint16)pFormat->format;
  1914. pWav->fmt.channels = (drwav_uint16)pFormat->channels;
  1915. pWav->fmt.sampleRate = pFormat->sampleRate;
  1916. pWav->fmt.avgBytesPerSec = (drwav_uint32)((pFormat->bitsPerSample * pFormat->sampleRate * pFormat->channels) / 8);
  1917. pWav->fmt.blockAlign = (drwav_uint16)((pFormat->channels * pFormat->bitsPerSample) / 8);
  1918. pWav->fmt.bitsPerSample = (drwav_uint16)pFormat->bitsPerSample;
  1919. pWav->fmt.extendedSize = 0;
  1920. pWav->isSequentialWrite = isSequential;
  1921. return DRWAV_TRUE;
  1922. }
  1923. static drwav_bool32 drwav_init_write__internal(drwav* pWav, const drwav_data_format* pFormat, drwav_uint64 totalSampleCount)
  1924. {
  1925. /* The function assumes drwav_preinit_write() was called beforehand. */
  1926. size_t runningPos = 0;
  1927. drwav_uint64 initialDataChunkSize = 0;
  1928. drwav_uint64 chunkSizeFMT;
  1929. /*
  1930. The initial values for the "RIFF" and "data" chunks depends on whether or not we are initializing in sequential mode or not. In
  1931. sequential mode we set this to its final values straight away since they can be calculated from the total sample count. In non-
  1932. sequential mode we initialize it all to zero and fill it out in drwav_uninit() using a backwards seek.
  1933. */
  1934. if (pWav->isSequentialWrite) {
  1935. initialDataChunkSize = (totalSampleCount * pWav->fmt.bitsPerSample) / 8;
  1936. /*
  1937. The RIFF container has a limit on the number of samples. drwav is not allowing this. There's no practical limits for Wave64
  1938. so for the sake of simplicity I'm not doing any validation for that.
  1939. */
  1940. if (pFormat->container == drwav_container_riff) {
  1941. if (initialDataChunkSize > (0xFFFFFFFFUL - 36)) {
  1942. return DRWAV_FALSE; /* Not enough room to store every sample. */
  1943. }
  1944. }
  1945. }
  1946. pWav->dataChunkDataSizeTargetWrite = initialDataChunkSize;
  1947. /* "RIFF" chunk. */
  1948. if (pFormat->container == drwav_container_riff) {
  1949. drwav_uint32 chunkSizeRIFF = 28 + (drwav_uint32)initialDataChunkSize; /* +28 = "WAVE" + [sizeof "fmt " chunk] */
  1950. runningPos += drwav__write(pWav, "RIFF", 4);
  1951. runningPos += drwav__write_u32ne_to_le(pWav, chunkSizeRIFF);
  1952. runningPos += drwav__write(pWav, "WAVE", 4);
  1953. } else if (pFormat->container == drwav_container_w64) {
  1954. drwav_uint64 chunkSizeRIFF = 80 + 24 + initialDataChunkSize; /* +24 because W64 includes the size of the GUID and size fields. */
  1955. runningPos += drwav__write(pWav, drwavGUID_W64_RIFF, 16);
  1956. runningPos += drwav__write_u64ne_to_le(pWav, chunkSizeRIFF);
  1957. runningPos += drwav__write(pWav, drwavGUID_W64_WAVE, 16);
  1958. } else if (pFormat->container == drwav_container_rf64) {
  1959. runningPos += drwav__write(pWav, "RF64", 4);
  1960. runningPos += drwav__write_u32ne_to_le(pWav, 0xFFFFFFFF); /* Always 0xFFFFFFFF for RF64. Set to a proper value in the "ds64" chunk. */
  1961. runningPos += drwav__write(pWav, "WAVE", 4);
  1962. }
  1963. /* "ds64" chunk (RF64 only). */
  1964. if (pFormat->container == drwav_container_rf64) {
  1965. drwav_uint32 initialds64ChunkSize = 28; /* 28 = [Size of RIFF (8 bytes)] + [Size of DATA (8 bytes)] + [Sample Count (8 bytes)] + [Table Length (4 bytes)]. Table length always set to 0. */
  1966. drwav_uint64 initialRiffChunkSize = 8 + initialds64ChunkSize + initialDataChunkSize; /* +8 for the ds64 header. */
  1967. runningPos += drwav__write(pWav, "ds64", 4);
  1968. runningPos += drwav__write_u32ne_to_le(pWav, initialds64ChunkSize); /* Size of ds64. */
  1969. runningPos += drwav__write_u64ne_to_le(pWav, initialRiffChunkSize); /* Size of RIFF. Set to true value at the end. */
  1970. runningPos += drwav__write_u64ne_to_le(pWav, initialDataChunkSize); /* Size of DATA. Set to true value at the end. */
  1971. runningPos += drwav__write_u64ne_to_le(pWav, totalSampleCount); /* Sample count. */
  1972. runningPos += drwav__write_u32ne_to_le(pWav, 0); /* Table length. Always set to zero in our case since we're not doing any other chunks than "DATA". */
  1973. }
  1974. /* "fmt " chunk. */
  1975. if (pFormat->container == drwav_container_riff || pFormat->container == drwav_container_rf64) {
  1976. chunkSizeFMT = 16;
  1977. runningPos += drwav__write(pWav, "fmt ", 4);
  1978. runningPos += drwav__write_u32ne_to_le(pWav, (drwav_uint32)chunkSizeFMT);
  1979. } else if (pFormat->container == drwav_container_w64) {
  1980. chunkSizeFMT = 40;
  1981. runningPos += drwav__write(pWav, drwavGUID_W64_FMT, 16);
  1982. runningPos += drwav__write_u64ne_to_le(pWav, chunkSizeFMT);
  1983. }
  1984. runningPos += drwav__write_u16ne_to_le(pWav, pWav->fmt.formatTag);
  1985. runningPos += drwav__write_u16ne_to_le(pWav, pWav->fmt.channels);
  1986. runningPos += drwav__write_u32ne_to_le(pWav, pWav->fmt.sampleRate);
  1987. runningPos += drwav__write_u32ne_to_le(pWav, pWav->fmt.avgBytesPerSec);
  1988. runningPos += drwav__write_u16ne_to_le(pWav, pWav->fmt.blockAlign);
  1989. runningPos += drwav__write_u16ne_to_le(pWav, pWav->fmt.bitsPerSample);
  1990. pWav->dataChunkDataPos = runningPos;
  1991. /* "data" chunk. */
  1992. if (pFormat->container == drwav_container_riff) {
  1993. drwav_uint32 chunkSizeDATA = (drwav_uint32)initialDataChunkSize;
  1994. runningPos += drwav__write(pWav, "data", 4);
  1995. runningPos += drwav__write_u32ne_to_le(pWav, chunkSizeDATA);
  1996. } else if (pFormat->container == drwav_container_w64) {
  1997. drwav_uint64 chunkSizeDATA = 24 + initialDataChunkSize; /* +24 because W64 includes the size of the GUID and size fields. */
  1998. runningPos += drwav__write(pWav, drwavGUID_W64_DATA, 16);
  1999. runningPos += drwav__write_u64ne_to_le(pWav, chunkSizeDATA);
  2000. } else if (pFormat->container == drwav_container_rf64) {
  2001. runningPos += drwav__write(pWav, "data", 4);
  2002. runningPos += drwav__write_u32ne_to_le(pWav, 0xFFFFFFFF); /* Always set to 0xFFFFFFFF for RF64. The true size of the data chunk is specified in the ds64 chunk. */
  2003. }
  2004. /*
  2005. The runningPos variable is incremented in the section above but is left unused which is causing some static analysis tools to detect it
  2006. as a dead store. I'm leaving this as-is for safety just in case I want to expand this function later to include other tags and want to
  2007. keep track of the running position for whatever reason. The line below should silence the static analysis tools.
  2008. */
  2009. (void)runningPos;
  2010. /* Set some properties for the client's convenience. */
  2011. pWav->container = pFormat->container;
  2012. pWav->channels = (drwav_uint16)pFormat->channels;
  2013. pWav->sampleRate = pFormat->sampleRate;
  2014. pWav->bitsPerSample = (drwav_uint16)pFormat->bitsPerSample;
  2015. pWav->translatedFormatTag = (drwav_uint16)pFormat->format;
  2016. return DRWAV_TRUE;
  2017. }
  2018. DRWAV_API drwav_bool32 drwav_init_write(drwav* pWav, const drwav_data_format* pFormat, drwav_write_proc onWrite, drwav_seek_proc onSeek, void* pUserData, const drwav_allocation_callbacks* pAllocationCallbacks)
  2019. {
  2020. if (!drwav_preinit_write(pWav, pFormat, DRWAV_FALSE, onWrite, onSeek, pUserData, pAllocationCallbacks)) {
  2021. return DRWAV_FALSE;
  2022. }
  2023. return drwav_init_write__internal(pWav, pFormat, 0); /* DRWAV_FALSE = Not Sequential */
  2024. }
  2025. DRWAV_API drwav_bool32 drwav_init_write_sequential(drwav* pWav, const drwav_data_format* pFormat, drwav_uint64 totalSampleCount, drwav_write_proc onWrite, void* pUserData, const drwav_allocation_callbacks* pAllocationCallbacks)
  2026. {
  2027. if (!drwav_preinit_write(pWav, pFormat, DRWAV_TRUE, onWrite, NULL, pUserData, pAllocationCallbacks)) {
  2028. return DRWAV_FALSE;
  2029. }
  2030. return drwav_init_write__internal(pWav, pFormat, totalSampleCount); /* DRWAV_TRUE = Sequential */
  2031. }
  2032. DRWAV_API drwav_bool32 drwav_init_write_sequential_pcm_frames(drwav* pWav, const drwav_data_format* pFormat, drwav_uint64 totalPCMFrameCount, drwav_write_proc onWrite, void* pUserData, const drwav_allocation_callbacks* pAllocationCallbacks)
  2033. {
  2034. if (pFormat == NULL) {
  2035. return DRWAV_FALSE;
  2036. }
  2037. return drwav_init_write_sequential(pWav, pFormat, totalPCMFrameCount*pFormat->channels, onWrite, pUserData, pAllocationCallbacks);
  2038. }
  2039. DRWAV_API drwav_uint64 drwav_target_write_size_bytes(const drwav_data_format* pFormat, drwav_uint64 totalSampleCount)
  2040. {
  2041. /* Casting totalSampleCount to drwav_int64 for VC6 compatibility. No issues in practice because nobody is going to exhaust the whole 63 bits. */
  2042. drwav_uint64 targetDataSizeBytes = (drwav_uint64)((drwav_int64)totalSampleCount * pFormat->channels * pFormat->bitsPerSample/8.0);
  2043. drwav_uint64 riffChunkSizeBytes;
  2044. drwav_uint64 fileSizeBytes = 0;
  2045. if (pFormat->container == drwav_container_riff) {
  2046. riffChunkSizeBytes = drwav__riff_chunk_size_riff(targetDataSizeBytes);
  2047. fileSizeBytes = (8 + riffChunkSizeBytes); /* +8 because WAV doesn't include the size of the ChunkID and ChunkSize fields. */
  2048. } else if (pFormat->container == drwav_container_w64) {
  2049. riffChunkSizeBytes = drwav__riff_chunk_size_w64(targetDataSizeBytes);
  2050. fileSizeBytes = riffChunkSizeBytes;
  2051. } else if (pFormat->container == drwav_container_rf64) {
  2052. riffChunkSizeBytes = drwav__riff_chunk_size_rf64(targetDataSizeBytes);
  2053. fileSizeBytes = (8 + riffChunkSizeBytes); /* +8 because WAV doesn't include the size of the ChunkID and ChunkSize fields. */
  2054. }
  2055. return fileSizeBytes;
  2056. }
  2057. #ifndef DR_WAV_NO_STDIO
  2058. /* drwav_result_from_errno() is only used for fopen() and wfopen() so putting it inside DR_WAV_NO_STDIO for now. If something else needs this later we can move it out. */
  2059. #include <errno.h>
  2060. static drwav_result drwav_result_from_errno(int e)
  2061. {
  2062. switch (e)
  2063. {
  2064. case 0: return DRWAV_SUCCESS;
  2065. #ifdef EPERM
  2066. case EPERM: return DRWAV_INVALID_OPERATION;
  2067. #endif
  2068. #ifdef ENOENT
  2069. case ENOENT: return DRWAV_DOES_NOT_EXIST;
  2070. #endif
  2071. #ifdef ESRCH
  2072. case ESRCH: return DRWAV_DOES_NOT_EXIST;
  2073. #endif
  2074. #ifdef EINTR
  2075. case EINTR: return DRWAV_INTERRUPT;
  2076. #endif
  2077. #ifdef EIO
  2078. case EIO: return DRWAV_IO_ERROR;
  2079. #endif
  2080. #ifdef ENXIO
  2081. case ENXIO: return DRWAV_DOES_NOT_EXIST;
  2082. #endif
  2083. #ifdef E2BIG
  2084. case E2BIG: return DRWAV_INVALID_ARGS;
  2085. #endif
  2086. #ifdef ENOEXEC
  2087. case ENOEXEC: return DRWAV_INVALID_FILE;
  2088. #endif
  2089. #ifdef EBADF
  2090. case EBADF: return DRWAV_INVALID_FILE;
  2091. #endif
  2092. #ifdef ECHILD
  2093. case ECHILD: return DRWAV_ERROR;
  2094. #endif
  2095. #ifdef EAGAIN
  2096. case EAGAIN: return DRWAV_UNAVAILABLE;
  2097. #endif
  2098. #ifdef ENOMEM
  2099. case ENOMEM: return DRWAV_OUT_OF_MEMORY;
  2100. #endif
  2101. #ifdef EACCES
  2102. case EACCES: return DRWAV_ACCESS_DENIED;
  2103. #endif
  2104. #ifdef EFAULT
  2105. case EFAULT: return DRWAV_BAD_ADDRESS;
  2106. #endif
  2107. #ifdef ENOTBLK
  2108. case ENOTBLK: return DRWAV_ERROR;
  2109. #endif
  2110. #ifdef EBUSY
  2111. case EBUSY: return DRWAV_BUSY;
  2112. #endif
  2113. #ifdef EEXIST
  2114. case EEXIST: return DRWAV_ALREADY_EXISTS;
  2115. #endif
  2116. #ifdef EXDEV
  2117. case EXDEV: return DRWAV_ERROR;
  2118. #endif
  2119. #ifdef ENODEV
  2120. case ENODEV: return DRWAV_DOES_NOT_EXIST;
  2121. #endif
  2122. #ifdef ENOTDIR
  2123. case ENOTDIR: return DRWAV_NOT_DIRECTORY;
  2124. #endif
  2125. #ifdef EISDIR
  2126. case EISDIR: return DRWAV_IS_DIRECTORY;
  2127. #endif
  2128. #ifdef EINVAL
  2129. case EINVAL: return DRWAV_INVALID_ARGS;
  2130. #endif
  2131. #ifdef ENFILE
  2132. case ENFILE: return DRWAV_TOO_MANY_OPEN_FILES;
  2133. #endif
  2134. #ifdef EMFILE
  2135. case EMFILE: return DRWAV_TOO_MANY_OPEN_FILES;
  2136. #endif
  2137. #ifdef ENOTTY
  2138. case ENOTTY: return DRWAV_INVALID_OPERATION;
  2139. #endif
  2140. #ifdef ETXTBSY
  2141. case ETXTBSY: return DRWAV_BUSY;
  2142. #endif
  2143. #ifdef EFBIG
  2144. case EFBIG: return DRWAV_TOO_BIG;
  2145. #endif
  2146. #ifdef ENOSPC
  2147. case ENOSPC: return DRWAV_NO_SPACE;
  2148. #endif
  2149. #ifdef ESPIPE
  2150. case ESPIPE: return DRWAV_BAD_SEEK;
  2151. #endif
  2152. #ifdef EROFS
  2153. case EROFS: return DRWAV_ACCESS_DENIED;
  2154. #endif
  2155. #ifdef EMLINK
  2156. case EMLINK: return DRWAV_TOO_MANY_LINKS;
  2157. #endif
  2158. #ifdef EPIPE
  2159. case EPIPE: return DRWAV_BAD_PIPE;
  2160. #endif
  2161. #ifdef EDOM
  2162. case EDOM: return DRWAV_OUT_OF_RANGE;
  2163. #endif
  2164. #ifdef ERANGE
  2165. case ERANGE: return DRWAV_OUT_OF_RANGE;
  2166. #endif
  2167. #ifdef EDEADLK
  2168. case EDEADLK: return DRWAV_DEADLOCK;
  2169. #endif
  2170. #ifdef ENAMETOOLONG
  2171. case ENAMETOOLONG: return DRWAV_PATH_TOO_LONG;
  2172. #endif
  2173. #ifdef ENOLCK
  2174. case ENOLCK: return DRWAV_ERROR;
  2175. #endif
  2176. #ifdef ENOSYS
  2177. case ENOSYS: return DRWAV_NOT_IMPLEMENTED;
  2178. #endif
  2179. #ifdef ENOTEMPTY
  2180. case ENOTEMPTY: return DRWAV_DIRECTORY_NOT_EMPTY;
  2181. #endif
  2182. #ifdef ELOOP
  2183. case ELOOP: return DRWAV_TOO_MANY_LINKS;
  2184. #endif
  2185. #ifdef ENOMSG
  2186. case ENOMSG: return DRWAV_NO_MESSAGE;
  2187. #endif
  2188. #ifdef EIDRM
  2189. case EIDRM: return DRWAV_ERROR;
  2190. #endif
  2191. #ifdef ECHRNG
  2192. case ECHRNG: return DRWAV_ERROR;
  2193. #endif
  2194. #ifdef EL2NSYNC
  2195. case EL2NSYNC: return DRWAV_ERROR;
  2196. #endif
  2197. #ifdef EL3HLT
  2198. case EL3HLT: return DRWAV_ERROR;
  2199. #endif
  2200. #ifdef EL3RST
  2201. case EL3RST: return DRWAV_ERROR;
  2202. #endif
  2203. #ifdef ELNRNG
  2204. case ELNRNG: return DRWAV_OUT_OF_RANGE;
  2205. #endif
  2206. #ifdef EUNATCH
  2207. case EUNATCH: return DRWAV_ERROR;
  2208. #endif
  2209. #ifdef ENOCSI
  2210. case ENOCSI: return DRWAV_ERROR;
  2211. #endif
  2212. #ifdef EL2HLT
  2213. case EL2HLT: return DRWAV_ERROR;
  2214. #endif
  2215. #ifdef EBADE
  2216. case EBADE: return DRWAV_ERROR;
  2217. #endif
  2218. #ifdef EBADR
  2219. case EBADR: return DRWAV_ERROR;
  2220. #endif
  2221. #ifdef EXFULL
  2222. case EXFULL: return DRWAV_ERROR;
  2223. #endif
  2224. #ifdef ENOANO
  2225. case ENOANO: return DRWAV_ERROR;
  2226. #endif
  2227. #ifdef EBADRQC
  2228. case EBADRQC: return DRWAV_ERROR;
  2229. #endif
  2230. #ifdef EBADSLT
  2231. case EBADSLT: return DRWAV_ERROR;
  2232. #endif
  2233. #ifdef EBFONT
  2234. case EBFONT: return DRWAV_INVALID_FILE;
  2235. #endif
  2236. #ifdef ENOSTR
  2237. case ENOSTR: return DRWAV_ERROR;
  2238. #endif
  2239. #ifdef ENODATA
  2240. case ENODATA: return DRWAV_NO_DATA_AVAILABLE;
  2241. #endif
  2242. #ifdef ETIME
  2243. case ETIME: return DRWAV_TIMEOUT;
  2244. #endif
  2245. #ifdef ENOSR
  2246. case ENOSR: return DRWAV_NO_DATA_AVAILABLE;
  2247. #endif
  2248. #ifdef ENONET
  2249. case ENONET: return DRWAV_NO_NETWORK;
  2250. #endif
  2251. #ifdef ENOPKG
  2252. case ENOPKG: return DRWAV_ERROR;
  2253. #endif
  2254. #ifdef EREMOTE
  2255. case EREMOTE: return DRWAV_ERROR;
  2256. #endif
  2257. #ifdef ENOLINK
  2258. case ENOLINK: return DRWAV_ERROR;
  2259. #endif
  2260. #ifdef EADV
  2261. case EADV: return DRWAV_ERROR;
  2262. #endif
  2263. #ifdef ESRMNT
  2264. case ESRMNT: return DRWAV_ERROR;
  2265. #endif
  2266. #ifdef ECOMM
  2267. case ECOMM: return DRWAV_ERROR;
  2268. #endif
  2269. #ifdef EPROTO
  2270. case EPROTO: return DRWAV_ERROR;
  2271. #endif
  2272. #ifdef EMULTIHOP
  2273. case EMULTIHOP: return DRWAV_ERROR;
  2274. #endif
  2275. #ifdef EDOTDOT
  2276. case EDOTDOT: return DRWAV_ERROR;
  2277. #endif
  2278. #ifdef EBADMSG
  2279. case EBADMSG: return DRWAV_BAD_MESSAGE;
  2280. #endif
  2281. #ifdef EOVERFLOW
  2282. case EOVERFLOW: return DRWAV_TOO_BIG;
  2283. #endif
  2284. #ifdef ENOTUNIQ
  2285. case ENOTUNIQ: return DRWAV_NOT_UNIQUE;
  2286. #endif
  2287. #ifdef EBADFD
  2288. case EBADFD: return DRWAV_ERROR;
  2289. #endif
  2290. #ifdef EREMCHG
  2291. case EREMCHG: return DRWAV_ERROR;
  2292. #endif
  2293. #ifdef ELIBACC
  2294. case ELIBACC: return DRWAV_ACCESS_DENIED;
  2295. #endif
  2296. #ifdef ELIBBAD
  2297. case ELIBBAD: return DRWAV_INVALID_FILE;
  2298. #endif
  2299. #ifdef ELIBSCN
  2300. case ELIBSCN: return DRWAV_INVALID_FILE;
  2301. #endif
  2302. #ifdef ELIBMAX
  2303. case ELIBMAX: return DRWAV_ERROR;
  2304. #endif
  2305. #ifdef ELIBEXEC
  2306. case ELIBEXEC: return DRWAV_ERROR;
  2307. #endif
  2308. #ifdef EILSEQ
  2309. case EILSEQ: return DRWAV_INVALID_DATA;
  2310. #endif
  2311. #ifdef ERESTART
  2312. case ERESTART: return DRWAV_ERROR;
  2313. #endif
  2314. #ifdef ESTRPIPE
  2315. case ESTRPIPE: return DRWAV_ERROR;
  2316. #endif
  2317. #ifdef EUSERS
  2318. case EUSERS: return DRWAV_ERROR;
  2319. #endif
  2320. #ifdef ENOTSOCK
  2321. case ENOTSOCK: return DRWAV_NOT_SOCKET;
  2322. #endif
  2323. #ifdef EDESTADDRREQ
  2324. case EDESTADDRREQ: return DRWAV_NO_ADDRESS;
  2325. #endif
  2326. #ifdef EMSGSIZE
  2327. case EMSGSIZE: return DRWAV_TOO_BIG;
  2328. #endif
  2329. #ifdef EPROTOTYPE
  2330. case EPROTOTYPE: return DRWAV_BAD_PROTOCOL;
  2331. #endif
  2332. #ifdef ENOPROTOOPT
  2333. case ENOPROTOOPT: return DRWAV_PROTOCOL_UNAVAILABLE;
  2334. #endif
  2335. #ifdef EPROTONOSUPPORT
  2336. case EPROTONOSUPPORT: return DRWAV_PROTOCOL_NOT_SUPPORTED;
  2337. #endif
  2338. #ifdef ESOCKTNOSUPPORT
  2339. case ESOCKTNOSUPPORT: return DRWAV_SOCKET_NOT_SUPPORTED;
  2340. #endif
  2341. #ifdef EOPNOTSUPP
  2342. case EOPNOTSUPP: return DRWAV_INVALID_OPERATION;
  2343. #endif
  2344. #ifdef EPFNOSUPPORT
  2345. case EPFNOSUPPORT: return DRWAV_PROTOCOL_FAMILY_NOT_SUPPORTED;
  2346. #endif
  2347. #ifdef EAFNOSUPPORT
  2348. case EAFNOSUPPORT: return DRWAV_ADDRESS_FAMILY_NOT_SUPPORTED;
  2349. #endif
  2350. #ifdef EADDRINUSE
  2351. case EADDRINUSE: return DRWAV_ALREADY_IN_USE;
  2352. #endif
  2353. #ifdef EADDRNOTAVAIL
  2354. case EADDRNOTAVAIL: return DRWAV_ERROR;
  2355. #endif
  2356. #ifdef ENETDOWN
  2357. case ENETDOWN: return DRWAV_NO_NETWORK;
  2358. #endif
  2359. #ifdef ENETUNREACH
  2360. case ENETUNREACH: return DRWAV_NO_NETWORK;
  2361. #endif
  2362. #ifdef ENETRESET
  2363. case ENETRESET: return DRWAV_NO_NETWORK;
  2364. #endif
  2365. #ifdef ECONNABORTED
  2366. case ECONNABORTED: return DRWAV_NO_NETWORK;
  2367. #endif
  2368. #ifdef ECONNRESET
  2369. case ECONNRESET: return DRWAV_CONNECTION_RESET;
  2370. #endif
  2371. #ifdef ENOBUFS
  2372. case ENOBUFS: return DRWAV_NO_SPACE;
  2373. #endif
  2374. #ifdef EISCONN
  2375. case EISCONN: return DRWAV_ALREADY_CONNECTED;
  2376. #endif
  2377. #ifdef ENOTCONN
  2378. case ENOTCONN: return DRWAV_NOT_CONNECTED;
  2379. #endif
  2380. #ifdef ESHUTDOWN
  2381. case ESHUTDOWN: return DRWAV_ERROR;
  2382. #endif
  2383. #ifdef ETOOMANYREFS
  2384. case ETOOMANYREFS: return DRWAV_ERROR;
  2385. #endif
  2386. #ifdef ETIMEDOUT
  2387. case ETIMEDOUT: return DRWAV_TIMEOUT;
  2388. #endif
  2389. #ifdef ECONNREFUSED
  2390. case ECONNREFUSED: return DRWAV_CONNECTION_REFUSED;
  2391. #endif
  2392. #ifdef EHOSTDOWN
  2393. case EHOSTDOWN: return DRWAV_NO_HOST;
  2394. #endif
  2395. #ifdef EHOSTUNREACH
  2396. case EHOSTUNREACH: return DRWAV_NO_HOST;
  2397. #endif
  2398. #ifdef EALREADY
  2399. case EALREADY: return DRWAV_IN_PROGRESS;
  2400. #endif
  2401. #ifdef EINPROGRESS
  2402. case EINPROGRESS: return DRWAV_IN_PROGRESS;
  2403. #endif
  2404. #ifdef ESTALE
  2405. case ESTALE: return DRWAV_INVALID_FILE;
  2406. #endif
  2407. #ifdef EUCLEAN
  2408. case EUCLEAN: return DRWAV_ERROR;
  2409. #endif
  2410. #ifdef ENOTNAM
  2411. case ENOTNAM: return DRWAV_ERROR;
  2412. #endif
  2413. #ifdef ENAVAIL
  2414. case ENAVAIL: return DRWAV_ERROR;
  2415. #endif
  2416. #ifdef EISNAM
  2417. case EISNAM: return DRWAV_ERROR;
  2418. #endif
  2419. #ifdef EREMOTEIO
  2420. case EREMOTEIO: return DRWAV_IO_ERROR;
  2421. #endif
  2422. #ifdef EDQUOT
  2423. case EDQUOT: return DRWAV_NO_SPACE;
  2424. #endif
  2425. #ifdef ENOMEDIUM
  2426. case ENOMEDIUM: return DRWAV_DOES_NOT_EXIST;
  2427. #endif
  2428. #ifdef EMEDIUMTYPE
  2429. case EMEDIUMTYPE: return DRWAV_ERROR;
  2430. #endif
  2431. #ifdef ECANCELED
  2432. case ECANCELED: return DRWAV_CANCELLED;
  2433. #endif
  2434. #ifdef ENOKEY
  2435. case ENOKEY: return DRWAV_ERROR;
  2436. #endif
  2437. #ifdef EKEYEXPIRED
  2438. case EKEYEXPIRED: return DRWAV_ERROR;
  2439. #endif
  2440. #ifdef EKEYREVOKED
  2441. case EKEYREVOKED: return DRWAV_ERROR;
  2442. #endif
  2443. #ifdef EKEYREJECTED
  2444. case EKEYREJECTED: return DRWAV_ERROR;
  2445. #endif
  2446. #ifdef EOWNERDEAD
  2447. case EOWNERDEAD: return DRWAV_ERROR;
  2448. #endif
  2449. #ifdef ENOTRECOVERABLE
  2450. case ENOTRECOVERABLE: return DRWAV_ERROR;
  2451. #endif
  2452. #ifdef ERFKILL
  2453. case ERFKILL: return DRWAV_ERROR;
  2454. #endif
  2455. #ifdef EHWPOISON
  2456. case EHWPOISON: return DRWAV_ERROR;
  2457. #endif
  2458. default: return DRWAV_ERROR;
  2459. }
  2460. }
  2461. static drwav_result drwav_fopen(FILE** ppFile, const char* pFilePath, const char* pOpenMode)
  2462. {
  2463. #if _MSC_VER && _MSC_VER >= 1400
  2464. errno_t err;
  2465. #endif
  2466. if (ppFile != NULL) {
  2467. *ppFile = NULL; /* Safety. */
  2468. }
  2469. if (pFilePath == NULL || pOpenMode == NULL || ppFile == NULL) {
  2470. return DRWAV_INVALID_ARGS;
  2471. }
  2472. #if _MSC_VER && _MSC_VER >= 1400
  2473. err = fopen_s(ppFile, pFilePath, pOpenMode);
  2474. if (err != 0) {
  2475. return drwav_result_from_errno(err);
  2476. }
  2477. #else
  2478. #if defined(_WIN32) || defined(__APPLE__)
  2479. *ppFile = fopen(pFilePath, pOpenMode);
  2480. #else
  2481. #if defined(_FILE_OFFSET_BITS) && _FILE_OFFSET_BITS == 64 && defined(_LARGEFILE64_SOURCE)
  2482. *ppFile = fopen64(pFilePath, pOpenMode);
  2483. #else
  2484. *ppFile = fopen(pFilePath, pOpenMode);
  2485. #endif
  2486. #endif
  2487. if (*ppFile == NULL) {
  2488. drwav_result result = drwav_result_from_errno(errno);
  2489. if (result == DRWAV_SUCCESS) {
  2490. result = DRWAV_ERROR; /* Just a safety check to make sure we never ever return success when pFile == NULL. */
  2491. }
  2492. return result;
  2493. }
  2494. #endif
  2495. return DRWAV_SUCCESS;
  2496. }
  2497. /*
  2498. _wfopen() isn't always available in all compilation environments.
  2499. * Windows only.
  2500. * MSVC seems to support it universally as far back as VC6 from what I can tell (haven't checked further back).
  2501. * MinGW-64 (both 32- and 64-bit) seems to support it.
  2502. * MinGW wraps it in !defined(__STRICT_ANSI__).
  2503. * OpenWatcom wraps it in !defined(_NO_EXT_KEYS).
  2504. This can be reviewed as compatibility issues arise. The preference is to use _wfopen_s() and _wfopen() as opposed to the wcsrtombs()
  2505. fallback, so if you notice your compiler not detecting this properly I'm happy to look at adding support.
  2506. */
  2507. #if defined(_WIN32)
  2508. #if defined(_MSC_VER) || defined(__MINGW64__) || (!defined(__STRICT_ANSI__) && !defined(_NO_EXT_KEYS))
  2509. #define DRWAV_HAS_WFOPEN
  2510. #endif
  2511. #endif
  2512. static drwav_result drwav_wfopen(FILE** ppFile, const wchar_t* pFilePath, const wchar_t* pOpenMode, const drwav_allocation_callbacks* pAllocationCallbacks)
  2513. {
  2514. if (ppFile != NULL) {
  2515. *ppFile = NULL; /* Safety. */
  2516. }
  2517. if (pFilePath == NULL || pOpenMode == NULL || ppFile == NULL) {
  2518. return DRWAV_INVALID_ARGS;
  2519. }
  2520. #if defined(DRWAV_HAS_WFOPEN)
  2521. {
  2522. /* Use _wfopen() on Windows. */
  2523. #if defined(_MSC_VER) && _MSC_VER >= 1400
  2524. errno_t err = _wfopen_s(ppFile, pFilePath, pOpenMode);
  2525. if (err != 0) {
  2526. return drwav_result_from_errno(err);
  2527. }
  2528. #else
  2529. *ppFile = _wfopen(pFilePath, pOpenMode);
  2530. if (*ppFile == NULL) {
  2531. return drwav_result_from_errno(errno);
  2532. }
  2533. #endif
  2534. (void)pAllocationCallbacks;
  2535. }
  2536. #else
  2537. /*
  2538. Use fopen() on anything other than Windows. Requires a conversion. This is annoying because fopen() is locale specific. The only real way I can
  2539. think of to do this is with wcsrtombs(). Note that wcstombs() is apparently not thread-safe because it uses a static global mbstate_t object for
  2540. maintaining state. I've checked this with -std=c89 and it works, but if somebody get's a compiler error I'll look into improving compatibility.
  2541. */
  2542. {
  2543. mbstate_t mbs;
  2544. size_t lenMB;
  2545. const wchar_t* pFilePathTemp = pFilePath;
  2546. char* pFilePathMB = NULL;
  2547. char pOpenModeMB[32] = {0};
  2548. /* Get the length first. */
  2549. DRWAV_ZERO_OBJECT(&mbs);
  2550. lenMB = wcsrtombs(NULL, &pFilePathTemp, 0, &mbs);
  2551. if (lenMB == (size_t)-1) {
  2552. return drwav_result_from_errno(errno);
  2553. }
  2554. pFilePathMB = (char*)drwav__malloc_from_callbacks(lenMB + 1, pAllocationCallbacks);
  2555. if (pFilePathMB == NULL) {
  2556. return DRWAV_OUT_OF_MEMORY;
  2557. }
  2558. pFilePathTemp = pFilePath;
  2559. DRWAV_ZERO_OBJECT(&mbs);
  2560. wcsrtombs(pFilePathMB, &pFilePathTemp, lenMB + 1, &mbs);
  2561. /* The open mode should always consist of ASCII characters so we should be able to do a trivial conversion. */
  2562. {
  2563. size_t i = 0;
  2564. for (;;) {
  2565. if (pOpenMode[i] == 0) {
  2566. pOpenModeMB[i] = '\0';
  2567. break;
  2568. }
  2569. pOpenModeMB[i] = (char)pOpenMode[i];
  2570. i += 1;
  2571. }
  2572. }
  2573. *ppFile = fopen(pFilePathMB, pOpenModeMB);
  2574. drwav__free_from_callbacks(pFilePathMB, pAllocationCallbacks);
  2575. }
  2576. if (*ppFile == NULL) {
  2577. return DRWAV_ERROR;
  2578. }
  2579. #endif
  2580. return DRWAV_SUCCESS;
  2581. }
  2582. static size_t drwav__on_read_stdio(void* pUserData, void* pBufferOut, size_t bytesToRead)
  2583. {
  2584. return fread(pBufferOut, 1, bytesToRead, (FILE*)pUserData);
  2585. }
  2586. static size_t drwav__on_write_stdio(void* pUserData, const void* pData, size_t bytesToWrite)
  2587. {
  2588. return fwrite(pData, 1, bytesToWrite, (FILE*)pUserData);
  2589. }
  2590. static drwav_bool32 drwav__on_seek_stdio(void* pUserData, int offset, drwav_seek_origin origin)
  2591. {
  2592. return fseek((FILE*)pUserData, offset, (origin == drwav_seek_origin_current) ? SEEK_CUR : SEEK_SET) == 0;
  2593. }
  2594. DRWAV_API drwav_bool32 drwav_init_file(drwav* pWav, const char* filename, const drwav_allocation_callbacks* pAllocationCallbacks)
  2595. {
  2596. return drwav_init_file_ex(pWav, filename, NULL, NULL, 0, pAllocationCallbacks);
  2597. }
  2598. static drwav_bool32 drwav_init_file__internal_FILE(drwav* pWav, FILE* pFile, drwav_chunk_proc onChunk, void* pChunkUserData, drwav_uint32 flags, const drwav_allocation_callbacks* pAllocationCallbacks)
  2599. {
  2600. drwav_bool32 result;
  2601. result = drwav_preinit(pWav, drwav__on_read_stdio, drwav__on_seek_stdio, (void*)pFile, pAllocationCallbacks);
  2602. if (result != DRWAV_TRUE) {
  2603. fclose(pFile);
  2604. return result;
  2605. }
  2606. result = drwav_init__internal(pWav, onChunk, pChunkUserData, flags);
  2607. if (result != DRWAV_TRUE) {
  2608. fclose(pFile);
  2609. return result;
  2610. }
  2611. return DRWAV_TRUE;
  2612. }
  2613. DRWAV_API drwav_bool32 drwav_init_file_ex(drwav* pWav, const char* filename, drwav_chunk_proc onChunk, void* pChunkUserData, drwav_uint32 flags, const drwav_allocation_callbacks* pAllocationCallbacks)
  2614. {
  2615. FILE* pFile;
  2616. if (drwav_fopen(&pFile, filename, "rb") != DRWAV_SUCCESS) {
  2617. return DRWAV_FALSE;
  2618. }
  2619. /* This takes ownership of the FILE* object. */
  2620. return drwav_init_file__internal_FILE(pWav, pFile, onChunk, pChunkUserData, flags, pAllocationCallbacks);
  2621. }
  2622. DRWAV_API drwav_bool32 drwav_init_file_w(drwav* pWav, const wchar_t* filename, const drwav_allocation_callbacks* pAllocationCallbacks)
  2623. {
  2624. return drwav_init_file_ex_w(pWav, filename, NULL, NULL, 0, pAllocationCallbacks);
  2625. }
  2626. DRWAV_API drwav_bool32 drwav_init_file_ex_w(drwav* pWav, const wchar_t* filename, drwav_chunk_proc onChunk, void* pChunkUserData, drwav_uint32 flags, const drwav_allocation_callbacks* pAllocationCallbacks)
  2627. {
  2628. FILE* pFile;
  2629. if (drwav_wfopen(&pFile, filename, L"rb", pAllocationCallbacks) != DRWAV_SUCCESS) {
  2630. return DRWAV_FALSE;
  2631. }
  2632. /* This takes ownership of the FILE* object. */
  2633. return drwav_init_file__internal_FILE(pWav, pFile, onChunk, pChunkUserData, flags, pAllocationCallbacks);
  2634. }
  2635. static drwav_bool32 drwav_init_file_write__internal_FILE(drwav* pWav, FILE* pFile, const drwav_data_format* pFormat, drwav_uint64 totalSampleCount, drwav_bool32 isSequential, const drwav_allocation_callbacks* pAllocationCallbacks)
  2636. {
  2637. drwav_bool32 result;
  2638. result = drwav_preinit_write(pWav, pFormat, isSequential, drwav__on_write_stdio, drwav__on_seek_stdio, (void*)pFile, pAllocationCallbacks);
  2639. if (result != DRWAV_TRUE) {
  2640. fclose(pFile);
  2641. return result;
  2642. }
  2643. result = drwav_init_write__internal(pWav, pFormat, totalSampleCount);
  2644. if (result != DRWAV_TRUE) {
  2645. fclose(pFile);
  2646. return result;
  2647. }
  2648. return DRWAV_TRUE;
  2649. }
  2650. static drwav_bool32 drwav_init_file_write__internal(drwav* pWav, const char* filename, const drwav_data_format* pFormat, drwav_uint64 totalSampleCount, drwav_bool32 isSequential, const drwav_allocation_callbacks* pAllocationCallbacks)
  2651. {
  2652. FILE* pFile;
  2653. if (drwav_fopen(&pFile, filename, "wb") != DRWAV_SUCCESS) {
  2654. return DRWAV_FALSE;
  2655. }
  2656. /* This takes ownership of the FILE* object. */
  2657. return drwav_init_file_write__internal_FILE(pWav, pFile, pFormat, totalSampleCount, isSequential, pAllocationCallbacks);
  2658. }
  2659. static drwav_bool32 drwav_init_file_write_w__internal(drwav* pWav, const wchar_t* filename, const drwav_data_format* pFormat, drwav_uint64 totalSampleCount, drwav_bool32 isSequential, const drwav_allocation_callbacks* pAllocationCallbacks)
  2660. {
  2661. FILE* pFile;
  2662. if (drwav_wfopen(&pFile, filename, L"wb", pAllocationCallbacks) != DRWAV_SUCCESS) {
  2663. return DRWAV_FALSE;
  2664. }
  2665. /* This takes ownership of the FILE* object. */
  2666. return drwav_init_file_write__internal_FILE(pWav, pFile, pFormat, totalSampleCount, isSequential, pAllocationCallbacks);
  2667. }
  2668. DRWAV_API drwav_bool32 drwav_init_file_write(drwav* pWav, const char* filename, const drwav_data_format* pFormat, const drwav_allocation_callbacks* pAllocationCallbacks)
  2669. {
  2670. return drwav_init_file_write__internal(pWav, filename, pFormat, 0, DRWAV_FALSE, pAllocationCallbacks);
  2671. }
  2672. DRWAV_API drwav_bool32 drwav_init_file_write_sequential(drwav* pWav, const char* filename, const drwav_data_format* pFormat, drwav_uint64 totalSampleCount, const drwav_allocation_callbacks* pAllocationCallbacks)
  2673. {
  2674. return drwav_init_file_write__internal(pWav, filename, pFormat, totalSampleCount, DRWAV_TRUE, pAllocationCallbacks);
  2675. }
  2676. DRWAV_API drwav_bool32 drwav_init_file_write_sequential_pcm_frames(drwav* pWav, const char* filename, const drwav_data_format* pFormat, drwav_uint64 totalPCMFrameCount, const drwav_allocation_callbacks* pAllocationCallbacks)
  2677. {
  2678. if (pFormat == NULL) {
  2679. return DRWAV_FALSE;
  2680. }
  2681. return drwav_init_file_write_sequential(pWav, filename, pFormat, totalPCMFrameCount*pFormat->channels, pAllocationCallbacks);
  2682. }
  2683. DRWAV_API drwav_bool32 drwav_init_file_write_w(drwav* pWav, const wchar_t* filename, const drwav_data_format* pFormat, const drwav_allocation_callbacks* pAllocationCallbacks)
  2684. {
  2685. return drwav_init_file_write_w__internal(pWav, filename, pFormat, 0, DRWAV_FALSE, pAllocationCallbacks);
  2686. }
  2687. DRWAV_API drwav_bool32 drwav_init_file_write_sequential_w(drwav* pWav, const wchar_t* filename, const drwav_data_format* pFormat, drwav_uint64 totalSampleCount, const drwav_allocation_callbacks* pAllocationCallbacks)
  2688. {
  2689. return drwav_init_file_write_w__internal(pWav, filename, pFormat, totalSampleCount, DRWAV_TRUE, pAllocationCallbacks);
  2690. }
  2691. DRWAV_API drwav_bool32 drwav_init_file_write_sequential_pcm_frames_w(drwav* pWav, const wchar_t* filename, const drwav_data_format* pFormat, drwav_uint64 totalPCMFrameCount, const drwav_allocation_callbacks* pAllocationCallbacks)
  2692. {
  2693. if (pFormat == NULL) {
  2694. return DRWAV_FALSE;
  2695. }
  2696. return drwav_init_file_write_sequential_w(pWav, filename, pFormat, totalPCMFrameCount*pFormat->channels, pAllocationCallbacks);
  2697. }
  2698. #endif /* DR_WAV_NO_STDIO */
  2699. static size_t drwav__on_read_memory(void* pUserData, void* pBufferOut, size_t bytesToRead)
  2700. {
  2701. drwav* pWav = (drwav*)pUserData;
  2702. size_t bytesRemaining;
  2703. DRWAV_ASSERT(pWav != NULL);
  2704. DRWAV_ASSERT(pWav->memoryStream.dataSize >= pWav->memoryStream.currentReadPos);
  2705. bytesRemaining = pWav->memoryStream.dataSize - pWav->memoryStream.currentReadPos;
  2706. if (bytesToRead > bytesRemaining) {
  2707. bytesToRead = bytesRemaining;
  2708. }
  2709. if (bytesToRead > 0) {
  2710. DRWAV_COPY_MEMORY(pBufferOut, pWav->memoryStream.data + pWav->memoryStream.currentReadPos, bytesToRead);
  2711. pWav->memoryStream.currentReadPos += bytesToRead;
  2712. }
  2713. return bytesToRead;
  2714. }
  2715. static drwav_bool32 drwav__on_seek_memory(void* pUserData, int offset, drwav_seek_origin origin)
  2716. {
  2717. drwav* pWav = (drwav*)pUserData;
  2718. DRWAV_ASSERT(pWav != NULL);
  2719. if (origin == drwav_seek_origin_current) {
  2720. if (offset > 0) {
  2721. if (pWav->memoryStream.currentReadPos + offset > pWav->memoryStream.dataSize) {
  2722. return DRWAV_FALSE; /* Trying to seek too far forward. */
  2723. }
  2724. } else {
  2725. if (pWav->memoryStream.currentReadPos < (size_t)-offset) {
  2726. return DRWAV_FALSE; /* Trying to seek too far backwards. */
  2727. }
  2728. }
  2729. /* This will never underflow thanks to the clamps above. */
  2730. pWav->memoryStream.currentReadPos += offset;
  2731. } else {
  2732. if ((drwav_uint32)offset <= pWav->memoryStream.dataSize) {
  2733. pWav->memoryStream.currentReadPos = offset;
  2734. } else {
  2735. return DRWAV_FALSE; /* Trying to seek too far forward. */
  2736. }
  2737. }
  2738. return DRWAV_TRUE;
  2739. }
  2740. static size_t drwav__on_write_memory(void* pUserData, const void* pDataIn, size_t bytesToWrite)
  2741. {
  2742. drwav* pWav = (drwav*)pUserData;
  2743. size_t bytesRemaining;
  2744. DRWAV_ASSERT(pWav != NULL);
  2745. DRWAV_ASSERT(pWav->memoryStreamWrite.dataCapacity >= pWav->memoryStreamWrite.currentWritePos);
  2746. bytesRemaining = pWav->memoryStreamWrite.dataCapacity - pWav->memoryStreamWrite.currentWritePos;
  2747. if (bytesRemaining < bytesToWrite) {
  2748. /* Need to reallocate. */
  2749. void* pNewData;
  2750. size_t newDataCapacity = (pWav->memoryStreamWrite.dataCapacity == 0) ? 256 : pWav->memoryStreamWrite.dataCapacity * 2;
  2751. /* If doubling wasn't enough, just make it the minimum required size to write the data. */
  2752. if ((newDataCapacity - pWav->memoryStreamWrite.currentWritePos) < bytesToWrite) {
  2753. newDataCapacity = pWav->memoryStreamWrite.currentWritePos + bytesToWrite;
  2754. }
  2755. pNewData = drwav__realloc_from_callbacks(*pWav->memoryStreamWrite.ppData, newDataCapacity, pWav->memoryStreamWrite.dataCapacity, &pWav->allocationCallbacks);
  2756. if (pNewData == NULL) {
  2757. return 0;
  2758. }
  2759. *pWav->memoryStreamWrite.ppData = pNewData;
  2760. pWav->memoryStreamWrite.dataCapacity = newDataCapacity;
  2761. }
  2762. DRWAV_COPY_MEMORY(((drwav_uint8*)(*pWav->memoryStreamWrite.ppData)) + pWav->memoryStreamWrite.currentWritePos, pDataIn, bytesToWrite);
  2763. pWav->memoryStreamWrite.currentWritePos += bytesToWrite;
  2764. if (pWav->memoryStreamWrite.dataSize < pWav->memoryStreamWrite.currentWritePos) {
  2765. pWav->memoryStreamWrite.dataSize = pWav->memoryStreamWrite.currentWritePos;
  2766. }
  2767. *pWav->memoryStreamWrite.pDataSize = pWav->memoryStreamWrite.dataSize;
  2768. return bytesToWrite;
  2769. }
  2770. static drwav_bool32 drwav__on_seek_memory_write(void* pUserData, int offset, drwav_seek_origin origin)
  2771. {
  2772. drwav* pWav = (drwav*)pUserData;
  2773. DRWAV_ASSERT(pWav != NULL);
  2774. if (origin == drwav_seek_origin_current) {
  2775. if (offset > 0) {
  2776. if (pWav->memoryStreamWrite.currentWritePos + offset > pWav->memoryStreamWrite.dataSize) {
  2777. offset = (int)(pWav->memoryStreamWrite.dataSize - pWav->memoryStreamWrite.currentWritePos); /* Trying to seek too far forward. */
  2778. }
  2779. } else {
  2780. if (pWav->memoryStreamWrite.currentWritePos < (size_t)-offset) {
  2781. offset = -(int)pWav->memoryStreamWrite.currentWritePos; /* Trying to seek too far backwards. */
  2782. }
  2783. }
  2784. /* This will never underflow thanks to the clamps above. */
  2785. pWav->memoryStreamWrite.currentWritePos += offset;
  2786. } else {
  2787. if ((drwav_uint32)offset <= pWav->memoryStreamWrite.dataSize) {
  2788. pWav->memoryStreamWrite.currentWritePos = offset;
  2789. } else {
  2790. pWav->memoryStreamWrite.currentWritePos = pWav->memoryStreamWrite.dataSize; /* Trying to seek too far forward. */
  2791. }
  2792. }
  2793. return DRWAV_TRUE;
  2794. }
  2795. DRWAV_API drwav_bool32 drwav_init_memory(drwav* pWav, const void* data, size_t dataSize, const drwav_allocation_callbacks* pAllocationCallbacks)
  2796. {
  2797. return drwav_init_memory_ex(pWav, data, dataSize, NULL, NULL, 0, pAllocationCallbacks);
  2798. }
  2799. DRWAV_API drwav_bool32 drwav_init_memory_ex(drwav* pWav, const void* data, size_t dataSize, drwav_chunk_proc onChunk, void* pChunkUserData, drwav_uint32 flags, const drwav_allocation_callbacks* pAllocationCallbacks)
  2800. {
  2801. if (data == NULL || dataSize == 0) {
  2802. return DRWAV_FALSE;
  2803. }
  2804. if (!drwav_preinit(pWav, drwav__on_read_memory, drwav__on_seek_memory, pWav, pAllocationCallbacks)) {
  2805. return DRWAV_FALSE;
  2806. }
  2807. pWav->memoryStream.data = (const drwav_uint8*)data;
  2808. pWav->memoryStream.dataSize = dataSize;
  2809. pWav->memoryStream.currentReadPos = 0;
  2810. return drwav_init__internal(pWav, onChunk, pChunkUserData, flags);
  2811. }
  2812. static drwav_bool32 drwav_init_memory_write__internal(drwav* pWav, void** ppData, size_t* pDataSize, const drwav_data_format* pFormat, drwav_uint64 totalSampleCount, drwav_bool32 isSequential, const drwav_allocation_callbacks* pAllocationCallbacks)
  2813. {
  2814. if (ppData == NULL || pDataSize == NULL) {
  2815. return DRWAV_FALSE;
  2816. }
  2817. *ppData = NULL; /* Important because we're using realloc()! */
  2818. *pDataSize = 0;
  2819. if (!drwav_preinit_write(pWav, pFormat, isSequential, drwav__on_write_memory, drwav__on_seek_memory_write, pWav, pAllocationCallbacks)) {
  2820. return DRWAV_FALSE;
  2821. }
  2822. pWav->memoryStreamWrite.ppData = ppData;
  2823. pWav->memoryStreamWrite.pDataSize = pDataSize;
  2824. pWav->memoryStreamWrite.dataSize = 0;
  2825. pWav->memoryStreamWrite.dataCapacity = 0;
  2826. pWav->memoryStreamWrite.currentWritePos = 0;
  2827. return drwav_init_write__internal(pWav, pFormat, totalSampleCount);
  2828. }
  2829. DRWAV_API drwav_bool32 drwav_init_memory_write(drwav* pWav, void** ppData, size_t* pDataSize, const drwav_data_format* pFormat, const drwav_allocation_callbacks* pAllocationCallbacks)
  2830. {
  2831. return drwav_init_memory_write__internal(pWav, ppData, pDataSize, pFormat, 0, DRWAV_FALSE, pAllocationCallbacks);
  2832. }
  2833. DRWAV_API drwav_bool32 drwav_init_memory_write_sequential(drwav* pWav, void** ppData, size_t* pDataSize, const drwav_data_format* pFormat, drwav_uint64 totalSampleCount, const drwav_allocation_callbacks* pAllocationCallbacks)
  2834. {
  2835. return drwav_init_memory_write__internal(pWav, ppData, pDataSize, pFormat, totalSampleCount, DRWAV_TRUE, pAllocationCallbacks);
  2836. }
  2837. DRWAV_API drwav_bool32 drwav_init_memory_write_sequential_pcm_frames(drwav* pWav, void** ppData, size_t* pDataSize, const drwav_data_format* pFormat, drwav_uint64 totalPCMFrameCount, const drwav_allocation_callbacks* pAllocationCallbacks)
  2838. {
  2839. if (pFormat == NULL) {
  2840. return DRWAV_FALSE;
  2841. }
  2842. return drwav_init_memory_write_sequential(pWav, ppData, pDataSize, pFormat, totalPCMFrameCount*pFormat->channels, pAllocationCallbacks);
  2843. }
  2844. DRWAV_API drwav_result drwav_uninit(drwav* pWav)
  2845. {
  2846. drwav_result result = DRWAV_SUCCESS;
  2847. if (pWav == NULL) {
  2848. return DRWAV_INVALID_ARGS;
  2849. }
  2850. /*
  2851. If the drwav object was opened in write mode we'll need to finalize a few things:
  2852. - Make sure the "data" chunk is aligned to 16-bits for RIFF containers, or 64 bits for W64 containers.
  2853. - Set the size of the "data" chunk.
  2854. */
  2855. if (pWav->onWrite != NULL) {
  2856. drwav_uint32 paddingSize = 0;
  2857. /* Padding. Do not adjust pWav->dataChunkDataSize - this should not include the padding. */
  2858. if (pWav->container == drwav_container_riff || pWav->container == drwav_container_rf64) {
  2859. paddingSize = drwav__chunk_padding_size_riff(pWav->dataChunkDataSize);
  2860. } else {
  2861. paddingSize = drwav__chunk_padding_size_w64(pWav->dataChunkDataSize);
  2862. }
  2863. if (paddingSize > 0) {
  2864. drwav_uint64 paddingData = 0;
  2865. drwav__write(pWav, &paddingData, paddingSize); /* Byte order does not matter for this. */
  2866. }
  2867. /*
  2868. Chunk sizes. When using sequential mode, these will have been filled in at initialization time. We only need
  2869. to do this when using non-sequential mode.
  2870. */
  2871. if (pWav->onSeek && !pWav->isSequentialWrite) {
  2872. if (pWav->container == drwav_container_riff) {
  2873. /* The "RIFF" chunk size. */
  2874. if (pWav->onSeek(pWav->pUserData, 4, drwav_seek_origin_start)) {
  2875. drwav_uint32 riffChunkSize = drwav__riff_chunk_size_riff(pWav->dataChunkDataSize);
  2876. drwav__write_u32ne_to_le(pWav, riffChunkSize);
  2877. }
  2878. /* the "data" chunk size. */
  2879. if (pWav->onSeek(pWav->pUserData, (int)pWav->dataChunkDataPos + 4, drwav_seek_origin_start)) {
  2880. drwav_uint32 dataChunkSize = drwav__data_chunk_size_riff(pWav->dataChunkDataSize);
  2881. drwav__write_u32ne_to_le(pWav, dataChunkSize);
  2882. }
  2883. } else if (pWav->container == drwav_container_w64) {
  2884. /* The "RIFF" chunk size. */
  2885. if (pWav->onSeek(pWav->pUserData, 16, drwav_seek_origin_start)) {
  2886. drwav_uint64 riffChunkSize = drwav__riff_chunk_size_w64(pWav->dataChunkDataSize);
  2887. drwav__write_u64ne_to_le(pWav, riffChunkSize);
  2888. }
  2889. /* The "data" chunk size. */
  2890. if (pWav->onSeek(pWav->pUserData, (int)pWav->dataChunkDataPos + 16, drwav_seek_origin_start)) {
  2891. drwav_uint64 dataChunkSize = drwav__data_chunk_size_w64(pWav->dataChunkDataSize);
  2892. drwav__write_u64ne_to_le(pWav, dataChunkSize);
  2893. }
  2894. } else if (pWav->container == drwav_container_rf64) {
  2895. /* We only need to update the ds64 chunk. The "RIFF" and "data" chunks always have their sizes set to 0xFFFFFFFF for RF64. */
  2896. int ds64BodyPos = 12 + 8;
  2897. /* The "RIFF" chunk size. */
  2898. if (pWav->onSeek(pWav->pUserData, ds64BodyPos + 0, drwav_seek_origin_start)) {
  2899. drwav_uint64 riffChunkSize = drwav__riff_chunk_size_rf64(pWav->dataChunkDataSize);
  2900. drwav__write_u64ne_to_le(pWav, riffChunkSize);
  2901. }
  2902. /* The "data" chunk size. */
  2903. if (pWav->onSeek(pWav->pUserData, ds64BodyPos + 8, drwav_seek_origin_start)) {
  2904. drwav_uint64 dataChunkSize = drwav__data_chunk_size_rf64(pWav->dataChunkDataSize);
  2905. drwav__write_u64ne_to_le(pWav, dataChunkSize);
  2906. }
  2907. }
  2908. }
  2909. /* Validation for sequential mode. */
  2910. if (pWav->isSequentialWrite) {
  2911. if (pWav->dataChunkDataSize != pWav->dataChunkDataSizeTargetWrite) {
  2912. result = DRWAV_INVALID_FILE;
  2913. }
  2914. }
  2915. }
  2916. #ifndef DR_WAV_NO_STDIO
  2917. /*
  2918. If we opened the file with drwav_open_file() we will want to close the file handle. We can know whether or not drwav_open_file()
  2919. was used by looking at the onRead and onSeek callbacks.
  2920. */
  2921. if (pWav->onRead == drwav__on_read_stdio || pWav->onWrite == drwav__on_write_stdio) {
  2922. fclose((FILE*)pWav->pUserData);
  2923. }
  2924. #endif
  2925. return result;
  2926. }
  2927. DRWAV_API size_t drwav_read_raw(drwav* pWav, size_t bytesToRead, void* pBufferOut)
  2928. {
  2929. size_t bytesRead;
  2930. if (pWav == NULL || bytesToRead == 0) {
  2931. return 0;
  2932. }
  2933. if (bytesToRead > pWav->bytesRemaining) {
  2934. bytesToRead = (size_t)pWav->bytesRemaining;
  2935. }
  2936. if (pBufferOut != NULL) {
  2937. bytesRead = pWav->onRead(pWav->pUserData, pBufferOut, bytesToRead);
  2938. } else {
  2939. /* We need to seek. If we fail, we need to read-and-discard to make sure we get a good byte count. */
  2940. bytesRead = 0;
  2941. while (bytesRead < bytesToRead) {
  2942. size_t bytesToSeek = (bytesToRead - bytesRead);
  2943. if (bytesToSeek > 0x7FFFFFFF) {
  2944. bytesToSeek = 0x7FFFFFFF;
  2945. }
  2946. if (pWav->onSeek(pWav->pUserData, (int)bytesToSeek, drwav_seek_origin_current) == DRWAV_FALSE) {
  2947. break;
  2948. }
  2949. bytesRead += bytesToSeek;
  2950. }
  2951. /* When we get here we may need to read-and-discard some data. */
  2952. while (bytesRead < bytesToRead) {
  2953. drwav_uint8 buffer[4096];
  2954. size_t bytesSeeked;
  2955. size_t bytesToSeek = (bytesToRead - bytesRead);
  2956. if (bytesToSeek > sizeof(buffer)) {
  2957. bytesToSeek = sizeof(buffer);
  2958. }
  2959. bytesSeeked = pWav->onRead(pWav->pUserData, buffer, bytesToSeek);
  2960. bytesRead += bytesSeeked;
  2961. if (bytesSeeked < bytesToSeek) {
  2962. break; /* Reached the end. */
  2963. }
  2964. }
  2965. }
  2966. pWav->bytesRemaining -= bytesRead;
  2967. return bytesRead;
  2968. }
  2969. DRWAV_API drwav_uint64 drwav_read_pcm_frames_le(drwav* pWav, drwav_uint64 framesToRead, void* pBufferOut)
  2970. {
  2971. drwav_uint32 bytesPerFrame;
  2972. drwav_uint64 bytesToRead; /* Intentionally uint64 instead of size_t so we can do a check that we're not reading too much on 32-bit builds. */
  2973. if (pWav == NULL || framesToRead == 0) {
  2974. return 0;
  2975. }
  2976. /* Cannot use this function for compressed formats. */
  2977. if (drwav__is_compressed_format_tag(pWav->translatedFormatTag)) {
  2978. return 0;
  2979. }
  2980. bytesPerFrame = drwav_get_bytes_per_pcm_frame(pWav);
  2981. if (bytesPerFrame == 0) {
  2982. return 0;
  2983. }
  2984. /* Don't try to read more samples than can potentially fit in the output buffer. */
  2985. bytesToRead = framesToRead * bytesPerFrame;
  2986. if (bytesToRead > DRWAV_SIZE_MAX) {
  2987. bytesToRead = (DRWAV_SIZE_MAX / bytesPerFrame) * bytesPerFrame; /* Round the number of bytes to read to a clean frame boundary. */
  2988. }
  2989. /*
  2990. Doing an explicit check here just to make it clear that we don't want to be attempt to read anything if there's no bytes to read. There
  2991. *could* be a time where it evaluates to 0 due to overflowing.
  2992. */
  2993. if (bytesToRead == 0) {
  2994. return 0;
  2995. }
  2996. return drwav_read_raw(pWav, (size_t)bytesToRead, pBufferOut) / bytesPerFrame;
  2997. }
  2998. DRWAV_API drwav_uint64 drwav_read_pcm_frames_be(drwav* pWav, drwav_uint64 framesToRead, void* pBufferOut)
  2999. {
  3000. drwav_uint64 framesRead = drwav_read_pcm_frames_le(pWav, framesToRead, pBufferOut);
  3001. if (pBufferOut != NULL) {
  3002. drwav__bswap_samples(pBufferOut, framesRead*pWav->channels, drwav_get_bytes_per_pcm_frame(pWav)/pWav->channels, pWav->translatedFormatTag);
  3003. }
  3004. return framesRead;
  3005. }
  3006. DRWAV_API drwav_uint64 drwav_read_pcm_frames(drwav* pWav, drwav_uint64 framesToRead, void* pBufferOut)
  3007. {
  3008. if (drwav__is_little_endian()) {
  3009. return drwav_read_pcm_frames_le(pWav, framesToRead, pBufferOut);
  3010. } else {
  3011. return drwav_read_pcm_frames_be(pWav, framesToRead, pBufferOut);
  3012. }
  3013. }
  3014. DRWAV_API drwav_bool32 drwav_seek_to_first_pcm_frame(drwav* pWav)
  3015. {
  3016. if (pWav->onWrite != NULL) {
  3017. return DRWAV_FALSE; /* No seeking in write mode. */
  3018. }
  3019. if (!pWav->onSeek(pWav->pUserData, (int)pWav->dataChunkDataPos, drwav_seek_origin_start)) {
  3020. return DRWAV_FALSE;
  3021. }
  3022. if (drwav__is_compressed_format_tag(pWav->translatedFormatTag)) {
  3023. pWav->compressed.iCurrentPCMFrame = 0;
  3024. /* Cached data needs to be cleared for compressed formats. */
  3025. if (pWav->translatedFormatTag == DR_WAVE_FORMAT_ADPCM) {
  3026. DRWAV_ZERO_OBJECT(&pWav->msadpcm);
  3027. } else if (pWav->translatedFormatTag == DR_WAVE_FORMAT_DVI_ADPCM) {
  3028. DRWAV_ZERO_OBJECT(&pWav->ima);
  3029. } else {
  3030. DRWAV_ASSERT(DRWAV_FALSE); /* If this assertion is triggered it means I've implemented a new compressed format but forgot to add a branch for it here. */
  3031. }
  3032. }
  3033. pWav->bytesRemaining = pWav->dataChunkDataSize;
  3034. return DRWAV_TRUE;
  3035. }
  3036. DRWAV_API drwav_bool32 drwav_seek_to_pcm_frame(drwav* pWav, drwav_uint64 targetFrameIndex)
  3037. {
  3038. /* Seeking should be compatible with wave files > 2GB. */
  3039. if (pWav == NULL || pWav->onSeek == NULL) {
  3040. return DRWAV_FALSE;
  3041. }
  3042. /* No seeking in write mode. */
  3043. if (pWav->onWrite != NULL) {
  3044. return DRWAV_FALSE;
  3045. }
  3046. /* If there are no samples, just return DRWAV_TRUE without doing anything. */
  3047. if (pWav->totalPCMFrameCount == 0) {
  3048. return DRWAV_TRUE;
  3049. }
  3050. /* Make sure the sample is clamped. */
  3051. if (targetFrameIndex >= pWav->totalPCMFrameCount) {
  3052. targetFrameIndex = pWav->totalPCMFrameCount - 1;
  3053. }
  3054. /*
  3055. For compressed formats we just use a slow generic seek. If we are seeking forward we just seek forward. If we are going backwards we need
  3056. to seek back to the start.
  3057. */
  3058. if (drwav__is_compressed_format_tag(pWav->translatedFormatTag)) {
  3059. /* TODO: This can be optimized. */
  3060. /*
  3061. If we're seeking forward it's simple - just keep reading samples until we hit the sample we're requesting. If we're seeking backwards,
  3062. we first need to seek back to the start and then just do the same thing as a forward seek.
  3063. */
  3064. if (targetFrameIndex < pWav->compressed.iCurrentPCMFrame) {
  3065. if (!drwav_seek_to_first_pcm_frame(pWav)) {
  3066. return DRWAV_FALSE;
  3067. }
  3068. }
  3069. if (targetFrameIndex > pWav->compressed.iCurrentPCMFrame) {
  3070. drwav_uint64 offsetInFrames = targetFrameIndex - pWav->compressed.iCurrentPCMFrame;
  3071. drwav_int16 devnull[2048];
  3072. while (offsetInFrames > 0) {
  3073. drwav_uint64 framesRead = 0;
  3074. drwav_uint64 framesToRead = offsetInFrames;
  3075. if (framesToRead > drwav_countof(devnull)/pWav->channels) {
  3076. framesToRead = drwav_countof(devnull)/pWav->channels;
  3077. }
  3078. if (pWav->translatedFormatTag == DR_WAVE_FORMAT_ADPCM) {
  3079. framesRead = drwav_read_pcm_frames_s16__msadpcm(pWav, framesToRead, devnull);
  3080. } else if (pWav->translatedFormatTag == DR_WAVE_FORMAT_DVI_ADPCM) {
  3081. framesRead = drwav_read_pcm_frames_s16__ima(pWav, framesToRead, devnull);
  3082. } else {
  3083. DRWAV_ASSERT(DRWAV_FALSE); /* If this assertion is triggered it means I've implemented a new compressed format but forgot to add a branch for it here. */
  3084. }
  3085. if (framesRead != framesToRead) {
  3086. return DRWAV_FALSE;
  3087. }
  3088. offsetInFrames -= framesRead;
  3089. }
  3090. }
  3091. } else {
  3092. drwav_uint64 totalSizeInBytes;
  3093. drwav_uint64 currentBytePos;
  3094. drwav_uint64 targetBytePos;
  3095. drwav_uint64 offset;
  3096. totalSizeInBytes = pWav->totalPCMFrameCount * drwav_get_bytes_per_pcm_frame(pWav);
  3097. DRWAV_ASSERT(totalSizeInBytes >= pWav->bytesRemaining);
  3098. currentBytePos = totalSizeInBytes - pWav->bytesRemaining;
  3099. targetBytePos = targetFrameIndex * drwav_get_bytes_per_pcm_frame(pWav);
  3100. if (currentBytePos < targetBytePos) {
  3101. /* Offset forwards. */
  3102. offset = (targetBytePos - currentBytePos);
  3103. } else {
  3104. /* Offset backwards. */
  3105. if (!drwav_seek_to_first_pcm_frame(pWav)) {
  3106. return DRWAV_FALSE;
  3107. }
  3108. offset = targetBytePos;
  3109. }
  3110. while (offset > 0) {
  3111. int offset32 = ((offset > INT_MAX) ? INT_MAX : (int)offset);
  3112. if (!pWav->onSeek(pWav->pUserData, offset32, drwav_seek_origin_current)) {
  3113. return DRWAV_FALSE;
  3114. }
  3115. pWav->bytesRemaining -= offset32;
  3116. offset -= offset32;
  3117. }
  3118. }
  3119. return DRWAV_TRUE;
  3120. }
  3121. DRWAV_API size_t drwav_write_raw(drwav* pWav, size_t bytesToWrite, const void* pData)
  3122. {
  3123. size_t bytesWritten;
  3124. if (pWav == NULL || bytesToWrite == 0 || pData == NULL) {
  3125. return 0;
  3126. }
  3127. bytesWritten = pWav->onWrite(pWav->pUserData, pData, bytesToWrite);
  3128. pWav->dataChunkDataSize += bytesWritten;
  3129. return bytesWritten;
  3130. }
  3131. DRWAV_API drwav_uint64 drwav_write_pcm_frames_le(drwav* pWav, drwav_uint64 framesToWrite, const void* pData)
  3132. {
  3133. drwav_uint64 bytesToWrite;
  3134. drwav_uint64 bytesWritten;
  3135. const drwav_uint8* pRunningData;
  3136. if (pWav == NULL || framesToWrite == 0 || pData == NULL) {
  3137. return 0;
  3138. }
  3139. bytesToWrite = ((framesToWrite * pWav->channels * pWav->bitsPerSample) / 8);
  3140. if (bytesToWrite > DRWAV_SIZE_MAX) {
  3141. return 0;
  3142. }
  3143. bytesWritten = 0;
  3144. pRunningData = (const drwav_uint8*)pData;
  3145. while (bytesToWrite > 0) {
  3146. size_t bytesJustWritten;
  3147. drwav_uint64 bytesToWriteThisIteration;
  3148. bytesToWriteThisIteration = bytesToWrite;
  3149. DRWAV_ASSERT(bytesToWriteThisIteration <= DRWAV_SIZE_MAX); /* <-- This is checked above. */
  3150. bytesJustWritten = drwav_write_raw(pWav, (size_t)bytesToWriteThisIteration, pRunningData);
  3151. if (bytesJustWritten == 0) {
  3152. break;
  3153. }
  3154. bytesToWrite -= bytesJustWritten;
  3155. bytesWritten += bytesJustWritten;
  3156. pRunningData += bytesJustWritten;
  3157. }
  3158. return (bytesWritten * 8) / pWav->bitsPerSample / pWav->channels;
  3159. }
  3160. DRWAV_API drwav_uint64 drwav_write_pcm_frames_be(drwav* pWav, drwav_uint64 framesToWrite, const void* pData)
  3161. {
  3162. drwav_uint64 bytesToWrite;
  3163. drwav_uint64 bytesWritten;
  3164. drwav_uint32 bytesPerSample;
  3165. const drwav_uint8* pRunningData;
  3166. if (pWav == NULL || framesToWrite == 0 || pData == NULL) {
  3167. return 0;
  3168. }
  3169. bytesToWrite = ((framesToWrite * pWav->channels * pWav->bitsPerSample) / 8);
  3170. if (bytesToWrite > DRWAV_SIZE_MAX) {
  3171. return 0;
  3172. }
  3173. bytesWritten = 0;
  3174. pRunningData = (const drwav_uint8*)pData;
  3175. bytesPerSample = drwav_get_bytes_per_pcm_frame(pWav) / pWav->channels;
  3176. while (bytesToWrite > 0) {
  3177. drwav_uint8 temp[4096];
  3178. drwav_uint32 sampleCount;
  3179. size_t bytesJustWritten;
  3180. drwav_uint64 bytesToWriteThisIteration;
  3181. bytesToWriteThisIteration = bytesToWrite;
  3182. DRWAV_ASSERT(bytesToWriteThisIteration <= DRWAV_SIZE_MAX); /* <-- This is checked above. */
  3183. /*
  3184. WAV files are always little-endian. We need to byte swap on big-endian architectures. Since our input buffer is read-only we need
  3185. to use an intermediary buffer for the conversion.
  3186. */
  3187. sampleCount = sizeof(temp)/bytesPerSample;
  3188. if (bytesToWriteThisIteration > ((drwav_uint64)sampleCount)*bytesPerSample) {
  3189. bytesToWriteThisIteration = ((drwav_uint64)sampleCount)*bytesPerSample;
  3190. }
  3191. DRWAV_COPY_MEMORY(temp, pRunningData, (size_t)bytesToWriteThisIteration);
  3192. drwav__bswap_samples(temp, sampleCount, bytesPerSample, pWav->translatedFormatTag);
  3193. bytesJustWritten = drwav_write_raw(pWav, (size_t)bytesToWriteThisIteration, temp);
  3194. if (bytesJustWritten == 0) {
  3195. break;
  3196. }
  3197. bytesToWrite -= bytesJustWritten;
  3198. bytesWritten += bytesJustWritten;
  3199. pRunningData += bytesJustWritten;
  3200. }
  3201. return (bytesWritten * 8) / pWav->bitsPerSample / pWav->channels;
  3202. }
  3203. DRWAV_API drwav_uint64 drwav_write_pcm_frames(drwav* pWav, drwav_uint64 framesToWrite, const void* pData)
  3204. {
  3205. if (drwav__is_little_endian()) {
  3206. return drwav_write_pcm_frames_le(pWav, framesToWrite, pData);
  3207. } else {
  3208. return drwav_write_pcm_frames_be(pWav, framesToWrite, pData);
  3209. }
  3210. }
  3211. static drwav_uint64 drwav_read_pcm_frames_s16__msadpcm(drwav* pWav, drwav_uint64 framesToRead, drwav_int16* pBufferOut)
  3212. {
  3213. drwav_uint64 totalFramesRead = 0;
  3214. DRWAV_ASSERT(pWav != NULL);
  3215. DRWAV_ASSERT(framesToRead > 0);
  3216. /* TODO: Lots of room for optimization here. */
  3217. while (framesToRead > 0 && pWav->compressed.iCurrentPCMFrame < pWav->totalPCMFrameCount) {
  3218. /* If there are no cached frames we need to load a new block. */
  3219. if (pWav->msadpcm.cachedFrameCount == 0 && pWav->msadpcm.bytesRemainingInBlock == 0) {
  3220. if (pWav->channels == 1) {
  3221. /* Mono. */
  3222. drwav_uint8 header[7];
  3223. if (pWav->onRead(pWav->pUserData, header, sizeof(header)) != sizeof(header)) {
  3224. return totalFramesRead;
  3225. }
  3226. pWav->msadpcm.bytesRemainingInBlock = pWav->fmt.blockAlign - sizeof(header);
  3227. pWav->msadpcm.predictor[0] = header[0];
  3228. pWav->msadpcm.delta[0] = drwav__bytes_to_s16(header + 1);
  3229. pWav->msadpcm.prevFrames[0][1] = (drwav_int32)drwav__bytes_to_s16(header + 3);
  3230. pWav->msadpcm.prevFrames[0][0] = (drwav_int32)drwav__bytes_to_s16(header + 5);
  3231. pWav->msadpcm.cachedFrames[2] = pWav->msadpcm.prevFrames[0][0];
  3232. pWav->msadpcm.cachedFrames[3] = pWav->msadpcm.prevFrames[0][1];
  3233. pWav->msadpcm.cachedFrameCount = 2;
  3234. } else {
  3235. /* Stereo. */
  3236. drwav_uint8 header[14];
  3237. if (pWav->onRead(pWav->pUserData, header, sizeof(header)) != sizeof(header)) {
  3238. return totalFramesRead;
  3239. }
  3240. pWav->msadpcm.bytesRemainingInBlock = pWav->fmt.blockAlign - sizeof(header);
  3241. pWav->msadpcm.predictor[0] = header[0];
  3242. pWav->msadpcm.predictor[1] = header[1];
  3243. pWav->msadpcm.delta[0] = drwav__bytes_to_s16(header + 2);
  3244. pWav->msadpcm.delta[1] = drwav__bytes_to_s16(header + 4);
  3245. pWav->msadpcm.prevFrames[0][1] = (drwav_int32)drwav__bytes_to_s16(header + 6);
  3246. pWav->msadpcm.prevFrames[1][1] = (drwav_int32)drwav__bytes_to_s16(header + 8);
  3247. pWav->msadpcm.prevFrames[0][0] = (drwav_int32)drwav__bytes_to_s16(header + 10);
  3248. pWav->msadpcm.prevFrames[1][0] = (drwav_int32)drwav__bytes_to_s16(header + 12);
  3249. pWav->msadpcm.cachedFrames[0] = pWav->msadpcm.prevFrames[0][0];
  3250. pWav->msadpcm.cachedFrames[1] = pWav->msadpcm.prevFrames[1][0];
  3251. pWav->msadpcm.cachedFrames[2] = pWav->msadpcm.prevFrames[0][1];
  3252. pWav->msadpcm.cachedFrames[3] = pWav->msadpcm.prevFrames[1][1];
  3253. pWav->msadpcm.cachedFrameCount = 2;
  3254. }
  3255. }
  3256. /* Output anything that's cached. */
  3257. while (framesToRead > 0 && pWav->msadpcm.cachedFrameCount > 0 && pWav->compressed.iCurrentPCMFrame < pWav->totalPCMFrameCount) {
  3258. if (pBufferOut != NULL) {
  3259. drwav_uint32 iSample = 0;
  3260. for (iSample = 0; iSample < pWav->channels; iSample += 1) {
  3261. pBufferOut[iSample] = (drwav_int16)pWav->msadpcm.cachedFrames[(drwav_countof(pWav->msadpcm.cachedFrames) - (pWav->msadpcm.cachedFrameCount*pWav->channels)) + iSample];
  3262. }
  3263. pBufferOut += pWav->channels;
  3264. }
  3265. framesToRead -= 1;
  3266. totalFramesRead += 1;
  3267. pWav->compressed.iCurrentPCMFrame += 1;
  3268. pWav->msadpcm.cachedFrameCount -= 1;
  3269. }
  3270. if (framesToRead == 0) {
  3271. return totalFramesRead;
  3272. }
  3273. /*
  3274. If there's nothing left in the cache, just go ahead and load more. If there's nothing left to load in the current block we just continue to the next
  3275. loop iteration which will trigger the loading of a new block.
  3276. */
  3277. if (pWav->msadpcm.cachedFrameCount == 0) {
  3278. if (pWav->msadpcm.bytesRemainingInBlock == 0) {
  3279. continue;
  3280. } else {
  3281. static drwav_int32 adaptationTable[] = {
  3282. 230, 230, 230, 230, 307, 409, 512, 614,
  3283. 768, 614, 512, 409, 307, 230, 230, 230
  3284. };
  3285. static drwav_int32 coeff1Table[] = { 256, 512, 0, 192, 240, 460, 392 };
  3286. static drwav_int32 coeff2Table[] = { 0, -256, 0, 64, 0, -208, -232 };
  3287. drwav_uint8 nibbles;
  3288. drwav_int32 nibble0;
  3289. drwav_int32 nibble1;
  3290. if (pWav->onRead(pWav->pUserData, &nibbles, 1) != 1) {
  3291. return totalFramesRead;
  3292. }
  3293. pWav->msadpcm.bytesRemainingInBlock -= 1;
  3294. /* TODO: Optimize away these if statements. */
  3295. nibble0 = ((nibbles & 0xF0) >> 4); if ((nibbles & 0x80)) { nibble0 |= 0xFFFFFFF0UL; }
  3296. nibble1 = ((nibbles & 0x0F) >> 0); if ((nibbles & 0x08)) { nibble1 |= 0xFFFFFFF0UL; }
  3297. if (pWav->channels == 1) {
  3298. /* Mono. */
  3299. drwav_int32 newSample0;
  3300. drwav_int32 newSample1;
  3301. newSample0 = ((pWav->msadpcm.prevFrames[0][1] * coeff1Table[pWav->msadpcm.predictor[0]]) + (pWav->msadpcm.prevFrames[0][0] * coeff2Table[pWav->msadpcm.predictor[0]])) >> 8;
  3302. newSample0 += nibble0 * pWav->msadpcm.delta[0];
  3303. newSample0 = drwav_clamp(newSample0, -32768, 32767);
  3304. pWav->msadpcm.delta[0] = (adaptationTable[((nibbles & 0xF0) >> 4)] * pWav->msadpcm.delta[0]) >> 8;
  3305. if (pWav->msadpcm.delta[0] < 16) {
  3306. pWav->msadpcm.delta[0] = 16;
  3307. }
  3308. pWav->msadpcm.prevFrames[0][0] = pWav->msadpcm.prevFrames[0][1];
  3309. pWav->msadpcm.prevFrames[0][1] = newSample0;
  3310. newSample1 = ((pWav->msadpcm.prevFrames[0][1] * coeff1Table[pWav->msadpcm.predictor[0]]) + (pWav->msadpcm.prevFrames[0][0] * coeff2Table[pWav->msadpcm.predictor[0]])) >> 8;
  3311. newSample1 += nibble1 * pWav->msadpcm.delta[0];
  3312. newSample1 = drwav_clamp(newSample1, -32768, 32767);
  3313. pWav->msadpcm.delta[0] = (adaptationTable[((nibbles & 0x0F) >> 0)] * pWav->msadpcm.delta[0]) >> 8;
  3314. if (pWav->msadpcm.delta[0] < 16) {
  3315. pWav->msadpcm.delta[0] = 16;
  3316. }
  3317. pWav->msadpcm.prevFrames[0][0] = pWav->msadpcm.prevFrames[0][1];
  3318. pWav->msadpcm.prevFrames[0][1] = newSample1;
  3319. pWav->msadpcm.cachedFrames[2] = newSample0;
  3320. pWav->msadpcm.cachedFrames[3] = newSample1;
  3321. pWav->msadpcm.cachedFrameCount = 2;
  3322. } else {
  3323. /* Stereo. */
  3324. drwav_int32 newSample0;
  3325. drwav_int32 newSample1;
  3326. /* Left. */
  3327. newSample0 = ((pWav->msadpcm.prevFrames[0][1] * coeff1Table[pWav->msadpcm.predictor[0]]) + (pWav->msadpcm.prevFrames[0][0] * coeff2Table[pWav->msadpcm.predictor[0]])) >> 8;
  3328. newSample0 += nibble0 * pWav->msadpcm.delta[0];
  3329. newSample0 = drwav_clamp(newSample0, -32768, 32767);
  3330. pWav->msadpcm.delta[0] = (adaptationTable[((nibbles & 0xF0) >> 4)] * pWav->msadpcm.delta[0]) >> 8;
  3331. if (pWav->msadpcm.delta[0] < 16) {
  3332. pWav->msadpcm.delta[0] = 16;
  3333. }
  3334. pWav->msadpcm.prevFrames[0][0] = pWav->msadpcm.prevFrames[0][1];
  3335. pWav->msadpcm.prevFrames[0][1] = newSample0;
  3336. /* Right. */
  3337. newSample1 = ((pWav->msadpcm.prevFrames[1][1] * coeff1Table[pWav->msadpcm.predictor[1]]) + (pWav->msadpcm.prevFrames[1][0] * coeff2Table[pWav->msadpcm.predictor[1]])) >> 8;
  3338. newSample1 += nibble1 * pWav->msadpcm.delta[1];
  3339. newSample1 = drwav_clamp(newSample1, -32768, 32767);
  3340. pWav->msadpcm.delta[1] = (adaptationTable[((nibbles & 0x0F) >> 0)] * pWav->msadpcm.delta[1]) >> 8;
  3341. if (pWav->msadpcm.delta[1] < 16) {
  3342. pWav->msadpcm.delta[1] = 16;
  3343. }
  3344. pWav->msadpcm.prevFrames[1][0] = pWav->msadpcm.prevFrames[1][1];
  3345. pWav->msadpcm.prevFrames[1][1] = newSample1;
  3346. pWav->msadpcm.cachedFrames[2] = newSample0;
  3347. pWav->msadpcm.cachedFrames[3] = newSample1;
  3348. pWav->msadpcm.cachedFrameCount = 1;
  3349. }
  3350. }
  3351. }
  3352. }
  3353. return totalFramesRead;
  3354. }
  3355. static drwav_uint64 drwav_read_pcm_frames_s16__ima(drwav* pWav, drwav_uint64 framesToRead, drwav_int16* pBufferOut)
  3356. {
  3357. drwav_uint64 totalFramesRead = 0;
  3358. drwav_uint32 iChannel;
  3359. static drwav_int32 indexTable[16] = {
  3360. -1, -1, -1, -1, 2, 4, 6, 8,
  3361. -1, -1, -1, -1, 2, 4, 6, 8
  3362. };
  3363. static drwav_int32 stepTable[89] = {
  3364. 7, 8, 9, 10, 11, 12, 13, 14, 16, 17,
  3365. 19, 21, 23, 25, 28, 31, 34, 37, 41, 45,
  3366. 50, 55, 60, 66, 73, 80, 88, 97, 107, 118,
  3367. 130, 143, 157, 173, 190, 209, 230, 253, 279, 307,
  3368. 337, 371, 408, 449, 494, 544, 598, 658, 724, 796,
  3369. 876, 963, 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066,
  3370. 2272, 2499, 2749, 3024, 3327, 3660, 4026, 4428, 4871, 5358,
  3371. 5894, 6484, 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899,
  3372. 15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767
  3373. };
  3374. DRWAV_ASSERT(pWav != NULL);
  3375. DRWAV_ASSERT(framesToRead > 0);
  3376. /* TODO: Lots of room for optimization here. */
  3377. while (framesToRead > 0 && pWav->compressed.iCurrentPCMFrame < pWav->totalPCMFrameCount) {
  3378. /* If there are no cached samples we need to load a new block. */
  3379. if (pWav->ima.cachedFrameCount == 0 && pWav->ima.bytesRemainingInBlock == 0) {
  3380. if (pWav->channels == 1) {
  3381. /* Mono. */
  3382. drwav_uint8 header[4];
  3383. if (pWav->onRead(pWav->pUserData, header, sizeof(header)) != sizeof(header)) {
  3384. return totalFramesRead;
  3385. }
  3386. pWav->ima.bytesRemainingInBlock = pWav->fmt.blockAlign - sizeof(header);
  3387. if (header[2] >= drwav_countof(stepTable)) {
  3388. pWav->onSeek(pWav->pUserData, pWav->ima.bytesRemainingInBlock, drwav_seek_origin_current);
  3389. pWav->ima.bytesRemainingInBlock = 0;
  3390. return totalFramesRead; /* Invalid data. */
  3391. }
  3392. pWav->ima.predictor[0] = drwav__bytes_to_s16(header + 0);
  3393. pWav->ima.stepIndex[0] = header[2];
  3394. pWav->ima.cachedFrames[drwav_countof(pWav->ima.cachedFrames) - 1] = pWav->ima.predictor[0];
  3395. pWav->ima.cachedFrameCount = 1;
  3396. } else {
  3397. /* Stereo. */
  3398. drwav_uint8 header[8];
  3399. if (pWav->onRead(pWav->pUserData, header, sizeof(header)) != sizeof(header)) {
  3400. return totalFramesRead;
  3401. }
  3402. pWav->ima.bytesRemainingInBlock = pWav->fmt.blockAlign - sizeof(header);
  3403. if (header[2] >= drwav_countof(stepTable) || header[6] >= drwav_countof(stepTable)) {
  3404. pWav->onSeek(pWav->pUserData, pWav->ima.bytesRemainingInBlock, drwav_seek_origin_current);
  3405. pWav->ima.bytesRemainingInBlock = 0;
  3406. return totalFramesRead; /* Invalid data. */
  3407. }
  3408. pWav->ima.predictor[0] = drwav__bytes_to_s16(header + 0);
  3409. pWav->ima.stepIndex[0] = header[2];
  3410. pWav->ima.predictor[1] = drwav__bytes_to_s16(header + 4);
  3411. pWav->ima.stepIndex[1] = header[6];
  3412. pWav->ima.cachedFrames[drwav_countof(pWav->ima.cachedFrames) - 2] = pWav->ima.predictor[0];
  3413. pWav->ima.cachedFrames[drwav_countof(pWav->ima.cachedFrames) - 1] = pWav->ima.predictor[1];
  3414. pWav->ima.cachedFrameCount = 1;
  3415. }
  3416. }
  3417. /* Output anything that's cached. */
  3418. while (framesToRead > 0 && pWav->ima.cachedFrameCount > 0 && pWav->compressed.iCurrentPCMFrame < pWav->totalPCMFrameCount) {
  3419. if (pBufferOut != NULL) {
  3420. drwav_uint32 iSample;
  3421. for (iSample = 0; iSample < pWav->channels; iSample += 1) {
  3422. pBufferOut[iSample] = (drwav_int16)pWav->ima.cachedFrames[(drwav_countof(pWav->ima.cachedFrames) - (pWav->ima.cachedFrameCount*pWav->channels)) + iSample];
  3423. }
  3424. pBufferOut += pWav->channels;
  3425. }
  3426. framesToRead -= 1;
  3427. totalFramesRead += 1;
  3428. pWav->compressed.iCurrentPCMFrame += 1;
  3429. pWav->ima.cachedFrameCount -= 1;
  3430. }
  3431. if (framesToRead == 0) {
  3432. return totalFramesRead;
  3433. }
  3434. /*
  3435. If there's nothing left in the cache, just go ahead and load more. If there's nothing left to load in the current block we just continue to the next
  3436. loop iteration which will trigger the loading of a new block.
  3437. */
  3438. if (pWav->ima.cachedFrameCount == 0) {
  3439. if (pWav->ima.bytesRemainingInBlock == 0) {
  3440. continue;
  3441. } else {
  3442. /*
  3443. From what I can tell with stereo streams, it looks like every 4 bytes (8 samples) is for one channel. So it goes 4 bytes for the
  3444. left channel, 4 bytes for the right channel.
  3445. */
  3446. pWav->ima.cachedFrameCount = 8;
  3447. for (iChannel = 0; iChannel < pWav->channels; ++iChannel) {
  3448. drwav_uint32 iByte;
  3449. drwav_uint8 nibbles[4];
  3450. if (pWav->onRead(pWav->pUserData, &nibbles, 4) != 4) {
  3451. pWav->ima.cachedFrameCount = 0;
  3452. return totalFramesRead;
  3453. }
  3454. pWav->ima.bytesRemainingInBlock -= 4;
  3455. for (iByte = 0; iByte < 4; ++iByte) {
  3456. drwav_uint8 nibble0 = ((nibbles[iByte] & 0x0F) >> 0);
  3457. drwav_uint8 nibble1 = ((nibbles[iByte] & 0xF0) >> 4);
  3458. drwav_int32 step = stepTable[pWav->ima.stepIndex[iChannel]];
  3459. drwav_int32 predictor = pWav->ima.predictor[iChannel];
  3460. drwav_int32 diff = step >> 3;
  3461. if (nibble0 & 1) diff += step >> 2;
  3462. if (nibble0 & 2) diff += step >> 1;
  3463. if (nibble0 & 4) diff += step;
  3464. if (nibble0 & 8) diff = -diff;
  3465. predictor = drwav_clamp(predictor + diff, -32768, 32767);
  3466. pWav->ima.predictor[iChannel] = predictor;
  3467. pWav->ima.stepIndex[iChannel] = drwav_clamp(pWav->ima.stepIndex[iChannel] + indexTable[nibble0], 0, (drwav_int32)drwav_countof(stepTable)-1);
  3468. pWav->ima.cachedFrames[(drwav_countof(pWav->ima.cachedFrames) - (pWav->ima.cachedFrameCount*pWav->channels)) + (iByte*2+0)*pWav->channels + iChannel] = predictor;
  3469. step = stepTable[pWav->ima.stepIndex[iChannel]];
  3470. predictor = pWav->ima.predictor[iChannel];
  3471. diff = step >> 3;
  3472. if (nibble1 & 1) diff += step >> 2;
  3473. if (nibble1 & 2) diff += step >> 1;
  3474. if (nibble1 & 4) diff += step;
  3475. if (nibble1 & 8) diff = -diff;
  3476. predictor = drwav_clamp(predictor + diff, -32768, 32767);
  3477. pWav->ima.predictor[iChannel] = predictor;
  3478. pWav->ima.stepIndex[iChannel] = drwav_clamp(pWav->ima.stepIndex[iChannel] + indexTable[nibble1], 0, (drwav_int32)drwav_countof(stepTable)-1);
  3479. pWav->ima.cachedFrames[(drwav_countof(pWav->ima.cachedFrames) - (pWav->ima.cachedFrameCount*pWav->channels)) + (iByte*2+1)*pWav->channels + iChannel] = predictor;
  3480. }
  3481. }
  3482. }
  3483. }
  3484. }
  3485. return totalFramesRead;
  3486. }
  3487. #ifndef DR_WAV_NO_CONVERSION_API
  3488. static unsigned short g_drwavAlawTable[256] = {
  3489. 0xEA80, 0xEB80, 0xE880, 0xE980, 0xEE80, 0xEF80, 0xEC80, 0xED80, 0xE280, 0xE380, 0xE080, 0xE180, 0xE680, 0xE780, 0xE480, 0xE580,
  3490. 0xF540, 0xF5C0, 0xF440, 0xF4C0, 0xF740, 0xF7C0, 0xF640, 0xF6C0, 0xF140, 0xF1C0, 0xF040, 0xF0C0, 0xF340, 0xF3C0, 0xF240, 0xF2C0,
  3491. 0xAA00, 0xAE00, 0xA200, 0xA600, 0xBA00, 0xBE00, 0xB200, 0xB600, 0x8A00, 0x8E00, 0x8200, 0x8600, 0x9A00, 0x9E00, 0x9200, 0x9600,
  3492. 0xD500, 0xD700, 0xD100, 0xD300, 0xDD00, 0xDF00, 0xD900, 0xDB00, 0xC500, 0xC700, 0xC100, 0xC300, 0xCD00, 0xCF00, 0xC900, 0xCB00,
  3493. 0xFEA8, 0xFEB8, 0xFE88, 0xFE98, 0xFEE8, 0xFEF8, 0xFEC8, 0xFED8, 0xFE28, 0xFE38, 0xFE08, 0xFE18, 0xFE68, 0xFE78, 0xFE48, 0xFE58,
  3494. 0xFFA8, 0xFFB8, 0xFF88, 0xFF98, 0xFFE8, 0xFFF8, 0xFFC8, 0xFFD8, 0xFF28, 0xFF38, 0xFF08, 0xFF18, 0xFF68, 0xFF78, 0xFF48, 0xFF58,
  3495. 0xFAA0, 0xFAE0, 0xFA20, 0xFA60, 0xFBA0, 0xFBE0, 0xFB20, 0xFB60, 0xF8A0, 0xF8E0, 0xF820, 0xF860, 0xF9A0, 0xF9E0, 0xF920, 0xF960,
  3496. 0xFD50, 0xFD70, 0xFD10, 0xFD30, 0xFDD0, 0xFDF0, 0xFD90, 0xFDB0, 0xFC50, 0xFC70, 0xFC10, 0xFC30, 0xFCD0, 0xFCF0, 0xFC90, 0xFCB0,
  3497. 0x1580, 0x1480, 0x1780, 0x1680, 0x1180, 0x1080, 0x1380, 0x1280, 0x1D80, 0x1C80, 0x1F80, 0x1E80, 0x1980, 0x1880, 0x1B80, 0x1A80,
  3498. 0x0AC0, 0x0A40, 0x0BC0, 0x0B40, 0x08C0, 0x0840, 0x09C0, 0x0940, 0x0EC0, 0x0E40, 0x0FC0, 0x0F40, 0x0CC0, 0x0C40, 0x0DC0, 0x0D40,
  3499. 0x5600, 0x5200, 0x5E00, 0x5A00, 0x4600, 0x4200, 0x4E00, 0x4A00, 0x7600, 0x7200, 0x7E00, 0x7A00, 0x6600, 0x6200, 0x6E00, 0x6A00,
  3500. 0x2B00, 0x2900, 0x2F00, 0x2D00, 0x2300, 0x2100, 0x2700, 0x2500, 0x3B00, 0x3900, 0x3F00, 0x3D00, 0x3300, 0x3100, 0x3700, 0x3500,
  3501. 0x0158, 0x0148, 0x0178, 0x0168, 0x0118, 0x0108, 0x0138, 0x0128, 0x01D8, 0x01C8, 0x01F8, 0x01E8, 0x0198, 0x0188, 0x01B8, 0x01A8,
  3502. 0x0058, 0x0048, 0x0078, 0x0068, 0x0018, 0x0008, 0x0038, 0x0028, 0x00D8, 0x00C8, 0x00F8, 0x00E8, 0x0098, 0x0088, 0x00B8, 0x00A8,
  3503. 0x0560, 0x0520, 0x05E0, 0x05A0, 0x0460, 0x0420, 0x04E0, 0x04A0, 0x0760, 0x0720, 0x07E0, 0x07A0, 0x0660, 0x0620, 0x06E0, 0x06A0,
  3504. 0x02B0, 0x0290, 0x02F0, 0x02D0, 0x0230, 0x0210, 0x0270, 0x0250, 0x03B0, 0x0390, 0x03F0, 0x03D0, 0x0330, 0x0310, 0x0370, 0x0350
  3505. };
  3506. static unsigned short g_drwavMulawTable[256] = {
  3507. 0x8284, 0x8684, 0x8A84, 0x8E84, 0x9284, 0x9684, 0x9A84, 0x9E84, 0xA284, 0xA684, 0xAA84, 0xAE84, 0xB284, 0xB684, 0xBA84, 0xBE84,
  3508. 0xC184, 0xC384, 0xC584, 0xC784, 0xC984, 0xCB84, 0xCD84, 0xCF84, 0xD184, 0xD384, 0xD584, 0xD784, 0xD984, 0xDB84, 0xDD84, 0xDF84,
  3509. 0xE104, 0xE204, 0xE304, 0xE404, 0xE504, 0xE604, 0xE704, 0xE804, 0xE904, 0xEA04, 0xEB04, 0xEC04, 0xED04, 0xEE04, 0xEF04, 0xF004,
  3510. 0xF0C4, 0xF144, 0xF1C4, 0xF244, 0xF2C4, 0xF344, 0xF3C4, 0xF444, 0xF4C4, 0xF544, 0xF5C4, 0xF644, 0xF6C4, 0xF744, 0xF7C4, 0xF844,
  3511. 0xF8A4, 0xF8E4, 0xF924, 0xF964, 0xF9A4, 0xF9E4, 0xFA24, 0xFA64, 0xFAA4, 0xFAE4, 0xFB24, 0xFB64, 0xFBA4, 0xFBE4, 0xFC24, 0xFC64,
  3512. 0xFC94, 0xFCB4, 0xFCD4, 0xFCF4, 0xFD14, 0xFD34, 0xFD54, 0xFD74, 0xFD94, 0xFDB4, 0xFDD4, 0xFDF4, 0xFE14, 0xFE34, 0xFE54, 0xFE74,
  3513. 0xFE8C, 0xFE9C, 0xFEAC, 0xFEBC, 0xFECC, 0xFEDC, 0xFEEC, 0xFEFC, 0xFF0C, 0xFF1C, 0xFF2C, 0xFF3C, 0xFF4C, 0xFF5C, 0xFF6C, 0xFF7C,
  3514. 0xFF88, 0xFF90, 0xFF98, 0xFFA0, 0xFFA8, 0xFFB0, 0xFFB8, 0xFFC0, 0xFFC8, 0xFFD0, 0xFFD8, 0xFFE0, 0xFFE8, 0xFFF0, 0xFFF8, 0x0000,
  3515. 0x7D7C, 0x797C, 0x757C, 0x717C, 0x6D7C, 0x697C, 0x657C, 0x617C, 0x5D7C, 0x597C, 0x557C, 0x517C, 0x4D7C, 0x497C, 0x457C, 0x417C,
  3516. 0x3E7C, 0x3C7C, 0x3A7C, 0x387C, 0x367C, 0x347C, 0x327C, 0x307C, 0x2E7C, 0x2C7C, 0x2A7C, 0x287C, 0x267C, 0x247C, 0x227C, 0x207C,
  3517. 0x1EFC, 0x1DFC, 0x1CFC, 0x1BFC, 0x1AFC, 0x19FC, 0x18FC, 0x17FC, 0x16FC, 0x15FC, 0x14FC, 0x13FC, 0x12FC, 0x11FC, 0x10FC, 0x0FFC,
  3518. 0x0F3C, 0x0EBC, 0x0E3C, 0x0DBC, 0x0D3C, 0x0CBC, 0x0C3C, 0x0BBC, 0x0B3C, 0x0ABC, 0x0A3C, 0x09BC, 0x093C, 0x08BC, 0x083C, 0x07BC,
  3519. 0x075C, 0x071C, 0x06DC, 0x069C, 0x065C, 0x061C, 0x05DC, 0x059C, 0x055C, 0x051C, 0x04DC, 0x049C, 0x045C, 0x041C, 0x03DC, 0x039C,
  3520. 0x036C, 0x034C, 0x032C, 0x030C, 0x02EC, 0x02CC, 0x02AC, 0x028C, 0x026C, 0x024C, 0x022C, 0x020C, 0x01EC, 0x01CC, 0x01AC, 0x018C,
  3521. 0x0174, 0x0164, 0x0154, 0x0144, 0x0134, 0x0124, 0x0114, 0x0104, 0x00F4, 0x00E4, 0x00D4, 0x00C4, 0x00B4, 0x00A4, 0x0094, 0x0084,
  3522. 0x0078, 0x0070, 0x0068, 0x0060, 0x0058, 0x0050, 0x0048, 0x0040, 0x0038, 0x0030, 0x0028, 0x0020, 0x0018, 0x0010, 0x0008, 0x0000
  3523. };
  3524. static DRWAV_INLINE drwav_int16 drwav__alaw_to_s16(drwav_uint8 sampleIn)
  3525. {
  3526. return (short)g_drwavAlawTable[sampleIn];
  3527. }
  3528. static DRWAV_INLINE drwav_int16 drwav__mulaw_to_s16(drwav_uint8 sampleIn)
  3529. {
  3530. return (short)g_drwavMulawTable[sampleIn];
  3531. }
  3532. static void drwav__pcm_to_s16(drwav_int16* pOut, const drwav_uint8* pIn, size_t totalSampleCount, unsigned int bytesPerSample)
  3533. {
  3534. unsigned int i;
  3535. /* Special case for 8-bit sample data because it's treated as unsigned. */
  3536. if (bytesPerSample == 1) {
  3537. drwav_u8_to_s16(pOut, pIn, totalSampleCount);
  3538. return;
  3539. }
  3540. /* Slightly more optimal implementation for common formats. */
  3541. if (bytesPerSample == 2) {
  3542. for (i = 0; i < totalSampleCount; ++i) {
  3543. *pOut++ = ((const drwav_int16*)pIn)[i];
  3544. }
  3545. return;
  3546. }
  3547. if (bytesPerSample == 3) {
  3548. drwav_s24_to_s16(pOut, pIn, totalSampleCount);
  3549. return;
  3550. }
  3551. if (bytesPerSample == 4) {
  3552. drwav_s32_to_s16(pOut, (const drwav_int32*)pIn, totalSampleCount);
  3553. return;
  3554. }
  3555. /* Anything more than 64 bits per sample is not supported. */
  3556. if (bytesPerSample > 8) {
  3557. DRWAV_ZERO_MEMORY(pOut, totalSampleCount * sizeof(*pOut));
  3558. return;
  3559. }
  3560. /* Generic, slow converter. */
  3561. for (i = 0; i < totalSampleCount; ++i) {
  3562. drwav_uint64 sample = 0;
  3563. unsigned int shift = (8 - bytesPerSample) * 8;
  3564. unsigned int j;
  3565. for (j = 0; j < bytesPerSample; j += 1) {
  3566. DRWAV_ASSERT(j < 8);
  3567. sample |= (drwav_uint64)(pIn[j]) << shift;
  3568. shift += 8;
  3569. }
  3570. pIn += j;
  3571. *pOut++ = (drwav_int16)((drwav_int64)sample >> 48);
  3572. }
  3573. }
  3574. static void drwav__ieee_to_s16(drwav_int16* pOut, const drwav_uint8* pIn, size_t totalSampleCount, unsigned int bytesPerSample)
  3575. {
  3576. if (bytesPerSample == 4) {
  3577. drwav_f32_to_s16(pOut, (const float*)pIn, totalSampleCount);
  3578. return;
  3579. } else if (bytesPerSample == 8) {
  3580. drwav_f64_to_s16(pOut, (const double*)pIn, totalSampleCount);
  3581. return;
  3582. } else {
  3583. /* Only supporting 32- and 64-bit float. Output silence in all other cases. Contributions welcome for 16-bit float. */
  3584. DRWAV_ZERO_MEMORY(pOut, totalSampleCount * sizeof(*pOut));
  3585. return;
  3586. }
  3587. }
  3588. static drwav_uint64 drwav_read_pcm_frames_s16__pcm(drwav* pWav, drwav_uint64 framesToRead, drwav_int16* pBufferOut)
  3589. {
  3590. drwav_uint32 bytesPerFrame;
  3591. drwav_uint64 totalFramesRead;
  3592. drwav_uint8 sampleData[4096];
  3593. /* Fast path. */
  3594. if ((pWav->translatedFormatTag == DR_WAVE_FORMAT_PCM && pWav->bitsPerSample == 16) || pBufferOut == NULL) {
  3595. return drwav_read_pcm_frames(pWav, framesToRead, pBufferOut);
  3596. }
  3597. bytesPerFrame = drwav_get_bytes_per_pcm_frame(pWav);
  3598. if (bytesPerFrame == 0) {
  3599. return 0;
  3600. }
  3601. totalFramesRead = 0;
  3602. while (framesToRead > 0) {
  3603. drwav_uint64 framesRead = drwav_read_pcm_frames(pWav, drwav_min(framesToRead, sizeof(sampleData)/bytesPerFrame), sampleData);
  3604. if (framesRead == 0) {
  3605. break;
  3606. }
  3607. drwav__pcm_to_s16(pBufferOut, sampleData, (size_t)(framesRead*pWav->channels), bytesPerFrame/pWav->channels);
  3608. pBufferOut += framesRead*pWav->channels;
  3609. framesToRead -= framesRead;
  3610. totalFramesRead += framesRead;
  3611. }
  3612. return totalFramesRead;
  3613. }
  3614. static drwav_uint64 drwav_read_pcm_frames_s16__ieee(drwav* pWav, drwav_uint64 framesToRead, drwav_int16* pBufferOut)
  3615. {
  3616. drwav_uint64 totalFramesRead;
  3617. drwav_uint8 sampleData[4096];
  3618. drwav_uint32 bytesPerFrame;
  3619. if (pBufferOut == NULL) {
  3620. return drwav_read_pcm_frames(pWav, framesToRead, NULL);
  3621. }
  3622. bytesPerFrame = drwav_get_bytes_per_pcm_frame(pWav);
  3623. if (bytesPerFrame == 0) {
  3624. return 0;
  3625. }
  3626. totalFramesRead = 0;
  3627. while (framesToRead > 0) {
  3628. drwav_uint64 framesRead = drwav_read_pcm_frames(pWav, drwav_min(framesToRead, sizeof(sampleData)/bytesPerFrame), sampleData);
  3629. if (framesRead == 0) {
  3630. break;
  3631. }
  3632. drwav__ieee_to_s16(pBufferOut, sampleData, (size_t)(framesRead*pWav->channels), bytesPerFrame/pWav->channels);
  3633. pBufferOut += framesRead*pWav->channels;
  3634. framesToRead -= framesRead;
  3635. totalFramesRead += framesRead;
  3636. }
  3637. return totalFramesRead;
  3638. }
  3639. static drwav_uint64 drwav_read_pcm_frames_s16__alaw(drwav* pWav, drwav_uint64 framesToRead, drwav_int16* pBufferOut)
  3640. {
  3641. drwav_uint64 totalFramesRead;
  3642. drwav_uint8 sampleData[4096];
  3643. drwav_uint32 bytesPerFrame;
  3644. if (pBufferOut == NULL) {
  3645. return drwav_read_pcm_frames(pWav, framesToRead, NULL);
  3646. }
  3647. bytesPerFrame = drwav_get_bytes_per_pcm_frame(pWav);
  3648. if (bytesPerFrame == 0) {
  3649. return 0;
  3650. }
  3651. totalFramesRead = 0;
  3652. while (framesToRead > 0) {
  3653. drwav_uint64 framesRead = drwav_read_pcm_frames(pWav, drwav_min(framesToRead, sizeof(sampleData)/bytesPerFrame), sampleData);
  3654. if (framesRead == 0) {
  3655. break;
  3656. }
  3657. drwav_alaw_to_s16(pBufferOut, sampleData, (size_t)(framesRead*pWav->channels));
  3658. pBufferOut += framesRead*pWav->channels;
  3659. framesToRead -= framesRead;
  3660. totalFramesRead += framesRead;
  3661. }
  3662. return totalFramesRead;
  3663. }
  3664. static drwav_uint64 drwav_read_pcm_frames_s16__mulaw(drwav* pWav, drwav_uint64 framesToRead, drwav_int16* pBufferOut)
  3665. {
  3666. drwav_uint64 totalFramesRead;
  3667. drwav_uint8 sampleData[4096];
  3668. drwav_uint32 bytesPerFrame;
  3669. if (pBufferOut == NULL) {
  3670. return drwav_read_pcm_frames(pWav, framesToRead, NULL);
  3671. }
  3672. bytesPerFrame = drwav_get_bytes_per_pcm_frame(pWav);
  3673. if (bytesPerFrame == 0) {
  3674. return 0;
  3675. }
  3676. totalFramesRead = 0;
  3677. while (framesToRead > 0) {
  3678. drwav_uint64 framesRead = drwav_read_pcm_frames(pWav, drwav_min(framesToRead, sizeof(sampleData)/bytesPerFrame), sampleData);
  3679. if (framesRead == 0) {
  3680. break;
  3681. }
  3682. drwav_mulaw_to_s16(pBufferOut, sampleData, (size_t)(framesRead*pWav->channels));
  3683. pBufferOut += framesRead*pWav->channels;
  3684. framesToRead -= framesRead;
  3685. totalFramesRead += framesRead;
  3686. }
  3687. return totalFramesRead;
  3688. }
  3689. DRWAV_API drwav_uint64 drwav_read_pcm_frames_s16(drwav* pWav, drwav_uint64 framesToRead, drwav_int16* pBufferOut)
  3690. {
  3691. if (pWav == NULL || framesToRead == 0) {
  3692. return 0;
  3693. }
  3694. if (pBufferOut == NULL) {
  3695. return drwav_read_pcm_frames(pWav, framesToRead, NULL);
  3696. }
  3697. /* Don't try to read more samples than can potentially fit in the output buffer. */
  3698. if (framesToRead * pWav->channels * sizeof(drwav_int16) > DRWAV_SIZE_MAX) {
  3699. framesToRead = DRWAV_SIZE_MAX / sizeof(drwav_int16) / pWav->channels;
  3700. }
  3701. if (pWav->translatedFormatTag == DR_WAVE_FORMAT_PCM) {
  3702. return drwav_read_pcm_frames_s16__pcm(pWav, framesToRead, pBufferOut);
  3703. }
  3704. if (pWav->translatedFormatTag == DR_WAVE_FORMAT_IEEE_FLOAT) {
  3705. return drwav_read_pcm_frames_s16__ieee(pWav, framesToRead, pBufferOut);
  3706. }
  3707. if (pWav->translatedFormatTag == DR_WAVE_FORMAT_ALAW) {
  3708. return drwav_read_pcm_frames_s16__alaw(pWav, framesToRead, pBufferOut);
  3709. }
  3710. if (pWav->translatedFormatTag == DR_WAVE_FORMAT_MULAW) {
  3711. return drwav_read_pcm_frames_s16__mulaw(pWav, framesToRead, pBufferOut);
  3712. }
  3713. if (pWav->translatedFormatTag == DR_WAVE_FORMAT_ADPCM) {
  3714. return drwav_read_pcm_frames_s16__msadpcm(pWav, framesToRead, pBufferOut);
  3715. }
  3716. if (pWav->translatedFormatTag == DR_WAVE_FORMAT_DVI_ADPCM) {
  3717. return drwav_read_pcm_frames_s16__ima(pWav, framesToRead, pBufferOut);
  3718. }
  3719. return 0;
  3720. }
  3721. DRWAV_API drwav_uint64 drwav_read_pcm_frames_s16le(drwav* pWav, drwav_uint64 framesToRead, drwav_int16* pBufferOut)
  3722. {
  3723. drwav_uint64 framesRead = drwav_read_pcm_frames_s16(pWav, framesToRead, pBufferOut);
  3724. if (pBufferOut != NULL && drwav__is_little_endian() == DRWAV_FALSE) {
  3725. drwav__bswap_samples_s16(pBufferOut, framesRead*pWav->channels);
  3726. }
  3727. return framesRead;
  3728. }
  3729. DRWAV_API drwav_uint64 drwav_read_pcm_frames_s16be(drwav* pWav, drwav_uint64 framesToRead, drwav_int16* pBufferOut)
  3730. {
  3731. drwav_uint64 framesRead = drwav_read_pcm_frames_s16(pWav, framesToRead, pBufferOut);
  3732. if (pBufferOut != NULL && drwav__is_little_endian() == DRWAV_TRUE) {
  3733. drwav__bswap_samples_s16(pBufferOut, framesRead*pWav->channels);
  3734. }
  3735. return framesRead;
  3736. }
  3737. DRWAV_API void drwav_u8_to_s16(drwav_int16* pOut, const drwav_uint8* pIn, size_t sampleCount)
  3738. {
  3739. int r;
  3740. size_t i;
  3741. for (i = 0; i < sampleCount; ++i) {
  3742. int x = pIn[i];
  3743. r = x << 8;
  3744. r = r - 32768;
  3745. pOut[i] = (short)r;
  3746. }
  3747. }
  3748. DRWAV_API void drwav_s24_to_s16(drwav_int16* pOut, const drwav_uint8* pIn, size_t sampleCount)
  3749. {
  3750. int r;
  3751. size_t i;
  3752. for (i = 0; i < sampleCount; ++i) {
  3753. int x = ((int)(((unsigned int)(((const drwav_uint8*)pIn)[i*3+0]) << 8) | ((unsigned int)(((const drwav_uint8*)pIn)[i*3+1]) << 16) | ((unsigned int)(((const drwav_uint8*)pIn)[i*3+2])) << 24)) >> 8;
  3754. r = x >> 8;
  3755. pOut[i] = (short)r;
  3756. }
  3757. }
  3758. DRWAV_API void drwav_s32_to_s16(drwav_int16* pOut, const drwav_int32* pIn, size_t sampleCount)
  3759. {
  3760. int r;
  3761. size_t i;
  3762. for (i = 0; i < sampleCount; ++i) {
  3763. int x = pIn[i];
  3764. r = x >> 16;
  3765. pOut[i] = (short)r;
  3766. }
  3767. }
  3768. DRWAV_API void drwav_f32_to_s16(drwav_int16* pOut, const float* pIn, size_t sampleCount)
  3769. {
  3770. int r;
  3771. size_t i;
  3772. for (i = 0; i < sampleCount; ++i) {
  3773. float x = pIn[i];
  3774. float c;
  3775. c = ((x < -1) ? -1 : ((x > 1) ? 1 : x));
  3776. c = c + 1;
  3777. r = (int)(c * 32767.5f);
  3778. r = r - 32768;
  3779. pOut[i] = (short)r;
  3780. }
  3781. }
  3782. DRWAV_API void drwav_f64_to_s16(drwav_int16* pOut, const double* pIn, size_t sampleCount)
  3783. {
  3784. int r;
  3785. size_t i;
  3786. for (i = 0; i < sampleCount; ++i) {
  3787. double x = pIn[i];
  3788. double c;
  3789. c = ((x < -1) ? -1 : ((x > 1) ? 1 : x));
  3790. c = c + 1;
  3791. r = (int)(c * 32767.5);
  3792. r = r - 32768;
  3793. pOut[i] = (short)r;
  3794. }
  3795. }
  3796. DRWAV_API void drwav_alaw_to_s16(drwav_int16* pOut, const drwav_uint8* pIn, size_t sampleCount)
  3797. {
  3798. size_t i;
  3799. for (i = 0; i < sampleCount; ++i) {
  3800. pOut[i] = drwav__alaw_to_s16(pIn[i]);
  3801. }
  3802. }
  3803. DRWAV_API void drwav_mulaw_to_s16(drwav_int16* pOut, const drwav_uint8* pIn, size_t sampleCount)
  3804. {
  3805. size_t i;
  3806. for (i = 0; i < sampleCount; ++i) {
  3807. pOut[i] = drwav__mulaw_to_s16(pIn[i]);
  3808. }
  3809. }
  3810. static void drwav__pcm_to_f32(float* pOut, const drwav_uint8* pIn, size_t sampleCount, unsigned int bytesPerSample)
  3811. {
  3812. unsigned int i;
  3813. /* Special case for 8-bit sample data because it's treated as unsigned. */
  3814. if (bytesPerSample == 1) {
  3815. drwav_u8_to_f32(pOut, pIn, sampleCount);
  3816. return;
  3817. }
  3818. /* Slightly more optimal implementation for common formats. */
  3819. if (bytesPerSample == 2) {
  3820. drwav_s16_to_f32(pOut, (const drwav_int16*)pIn, sampleCount);
  3821. return;
  3822. }
  3823. if (bytesPerSample == 3) {
  3824. drwav_s24_to_f32(pOut, pIn, sampleCount);
  3825. return;
  3826. }
  3827. if (bytesPerSample == 4) {
  3828. drwav_s32_to_f32(pOut, (const drwav_int32*)pIn, sampleCount);
  3829. return;
  3830. }
  3831. /* Anything more than 64 bits per sample is not supported. */
  3832. if (bytesPerSample > 8) {
  3833. DRWAV_ZERO_MEMORY(pOut, sampleCount * sizeof(*pOut));
  3834. return;
  3835. }
  3836. /* Generic, slow converter. */
  3837. for (i = 0; i < sampleCount; ++i) {
  3838. drwav_uint64 sample = 0;
  3839. unsigned int shift = (8 - bytesPerSample) * 8;
  3840. unsigned int j;
  3841. for (j = 0; j < bytesPerSample; j += 1) {
  3842. DRWAV_ASSERT(j < 8);
  3843. sample |= (drwav_uint64)(pIn[j]) << shift;
  3844. shift += 8;
  3845. }
  3846. pIn += j;
  3847. *pOut++ = (float)((drwav_int64)sample / 9223372036854775807.0);
  3848. }
  3849. }
  3850. static void drwav__ieee_to_f32(float* pOut, const drwav_uint8* pIn, size_t sampleCount, unsigned int bytesPerSample)
  3851. {
  3852. if (bytesPerSample == 4) {
  3853. unsigned int i;
  3854. for (i = 0; i < sampleCount; ++i) {
  3855. *pOut++ = ((const float*)pIn)[i];
  3856. }
  3857. return;
  3858. } else if (bytesPerSample == 8) {
  3859. drwav_f64_to_f32(pOut, (const double*)pIn, sampleCount);
  3860. return;
  3861. } else {
  3862. /* Only supporting 32- and 64-bit float. Output silence in all other cases. Contributions welcome for 16-bit float. */
  3863. DRWAV_ZERO_MEMORY(pOut, sampleCount * sizeof(*pOut));
  3864. return;
  3865. }
  3866. }
  3867. static drwav_uint64 drwav_read_pcm_frames_f32__pcm(drwav* pWav, drwav_uint64 framesToRead, float* pBufferOut)
  3868. {
  3869. drwav_uint64 totalFramesRead;
  3870. drwav_uint8 sampleData[4096];
  3871. drwav_uint32 bytesPerFrame = drwav_get_bytes_per_pcm_frame(pWav);
  3872. if (bytesPerFrame == 0) {
  3873. return 0;
  3874. }
  3875. totalFramesRead = 0;
  3876. while (framesToRead > 0) {
  3877. drwav_uint64 framesRead = drwav_read_pcm_frames(pWav, drwav_min(framesToRead, sizeof(sampleData)/bytesPerFrame), sampleData);
  3878. if (framesRead == 0) {
  3879. break;
  3880. }
  3881. drwav__pcm_to_f32(pBufferOut, sampleData, (size_t)framesRead*pWav->channels, bytesPerFrame/pWav->channels);
  3882. pBufferOut += framesRead*pWav->channels;
  3883. framesToRead -= framesRead;
  3884. totalFramesRead += framesRead;
  3885. }
  3886. return totalFramesRead;
  3887. }
  3888. static drwav_uint64 drwav_read_pcm_frames_f32__msadpcm(drwav* pWav, drwav_uint64 framesToRead, float* pBufferOut)
  3889. {
  3890. /*
  3891. We're just going to borrow the implementation from the drwav_read_s16() since ADPCM is a little bit more complicated than other formats and I don't
  3892. want to duplicate that code.
  3893. */
  3894. drwav_uint64 totalFramesRead = 0;
  3895. drwav_int16 samples16[2048];
  3896. while (framesToRead > 0) {
  3897. drwav_uint64 framesRead = drwav_read_pcm_frames_s16(pWav, drwav_min(framesToRead, drwav_countof(samples16)/pWav->channels), samples16);
  3898. if (framesRead == 0) {
  3899. break;
  3900. }
  3901. drwav_s16_to_f32(pBufferOut, samples16, (size_t)(framesRead*pWav->channels)); /* <-- Safe cast because we're clamping to 2048. */
  3902. pBufferOut += framesRead*pWav->channels;
  3903. framesToRead -= framesRead;
  3904. totalFramesRead += framesRead;
  3905. }
  3906. return totalFramesRead;
  3907. }
  3908. static drwav_uint64 drwav_read_pcm_frames_f32__ima(drwav* pWav, drwav_uint64 framesToRead, float* pBufferOut)
  3909. {
  3910. /*
  3911. We're just going to borrow the implementation from the drwav_read_s16() since IMA-ADPCM is a little bit more complicated than other formats and I don't
  3912. want to duplicate that code.
  3913. */
  3914. drwav_uint64 totalFramesRead = 0;
  3915. drwav_int16 samples16[2048];
  3916. while (framesToRead > 0) {
  3917. drwav_uint64 framesRead = drwav_read_pcm_frames_s16(pWav, drwav_min(framesToRead, drwav_countof(samples16)/pWav->channels), samples16);
  3918. if (framesRead == 0) {
  3919. break;
  3920. }
  3921. drwav_s16_to_f32(pBufferOut, samples16, (size_t)(framesRead*pWav->channels)); /* <-- Safe cast because we're clamping to 2048. */
  3922. pBufferOut += framesRead*pWav->channels;
  3923. framesToRead -= framesRead;
  3924. totalFramesRead += framesRead;
  3925. }
  3926. return totalFramesRead;
  3927. }
  3928. static drwav_uint64 drwav_read_pcm_frames_f32__ieee(drwav* pWav, drwav_uint64 framesToRead, float* pBufferOut)
  3929. {
  3930. drwav_uint64 totalFramesRead;
  3931. drwav_uint8 sampleData[4096];
  3932. drwav_uint32 bytesPerFrame;
  3933. /* Fast path. */
  3934. if (pWav->translatedFormatTag == DR_WAVE_FORMAT_IEEE_FLOAT && pWav->bitsPerSample == 32) {
  3935. return drwav_read_pcm_frames(pWav, framesToRead, pBufferOut);
  3936. }
  3937. bytesPerFrame = drwav_get_bytes_per_pcm_frame(pWav);
  3938. if (bytesPerFrame == 0) {
  3939. return 0;
  3940. }
  3941. totalFramesRead = 0;
  3942. while (framesToRead > 0) {
  3943. drwav_uint64 framesRead = drwav_read_pcm_frames(pWav, drwav_min(framesToRead, sizeof(sampleData)/bytesPerFrame), sampleData);
  3944. if (framesRead == 0) {
  3945. break;
  3946. }
  3947. drwav__ieee_to_f32(pBufferOut, sampleData, (size_t)(framesRead*pWav->channels), bytesPerFrame/pWav->channels);
  3948. pBufferOut += framesRead*pWav->channels;
  3949. framesToRead -= framesRead;
  3950. totalFramesRead += framesRead;
  3951. }
  3952. return totalFramesRead;
  3953. }
  3954. static drwav_uint64 drwav_read_pcm_frames_f32__alaw(drwav* pWav, drwav_uint64 framesToRead, float* pBufferOut)
  3955. {
  3956. drwav_uint64 totalFramesRead;
  3957. drwav_uint8 sampleData[4096];
  3958. drwav_uint32 bytesPerFrame = drwav_get_bytes_per_pcm_frame(pWav);
  3959. if (bytesPerFrame == 0) {
  3960. return 0;
  3961. }
  3962. totalFramesRead = 0;
  3963. while (framesToRead > 0) {
  3964. drwav_uint64 framesRead = drwav_read_pcm_frames(pWav, drwav_min(framesToRead, sizeof(sampleData)/bytesPerFrame), sampleData);
  3965. if (framesRead == 0) {
  3966. break;
  3967. }
  3968. drwav_alaw_to_f32(pBufferOut, sampleData, (size_t)(framesRead*pWav->channels));
  3969. pBufferOut += framesRead*pWav->channels;
  3970. framesToRead -= framesRead;
  3971. totalFramesRead += framesRead;
  3972. }
  3973. return totalFramesRead;
  3974. }
  3975. static drwav_uint64 drwav_read_pcm_frames_f32__mulaw(drwav* pWav, drwav_uint64 framesToRead, float* pBufferOut)
  3976. {
  3977. drwav_uint64 totalFramesRead;
  3978. drwav_uint8 sampleData[4096];
  3979. drwav_uint32 bytesPerFrame = drwav_get_bytes_per_pcm_frame(pWav);
  3980. if (bytesPerFrame == 0) {
  3981. return 0;
  3982. }
  3983. totalFramesRead = 0;
  3984. while (framesToRead > 0) {
  3985. drwav_uint64 framesRead = drwav_read_pcm_frames(pWav, drwav_min(framesToRead, sizeof(sampleData)/bytesPerFrame), sampleData);
  3986. if (framesRead == 0) {
  3987. break;
  3988. }
  3989. drwav_mulaw_to_f32(pBufferOut, sampleData, (size_t)(framesRead*pWav->channels));
  3990. pBufferOut += framesRead*pWav->channels;
  3991. framesToRead -= framesRead;
  3992. totalFramesRead += framesRead;
  3993. }
  3994. return totalFramesRead;
  3995. }
  3996. DRWAV_API drwav_uint64 drwav_read_pcm_frames_f32(drwav* pWav, drwav_uint64 framesToRead, float* pBufferOut)
  3997. {
  3998. if (pWav == NULL || framesToRead == 0) {
  3999. return 0;
  4000. }
  4001. if (pBufferOut == NULL) {
  4002. return drwav_read_pcm_frames(pWav, framesToRead, NULL);
  4003. }
  4004. /* Don't try to read more samples than can potentially fit in the output buffer. */
  4005. if (framesToRead * pWav->channels * sizeof(float) > DRWAV_SIZE_MAX) {
  4006. framesToRead = DRWAV_SIZE_MAX / sizeof(float) / pWav->channels;
  4007. }
  4008. if (pWav->translatedFormatTag == DR_WAVE_FORMAT_PCM) {
  4009. return drwav_read_pcm_frames_f32__pcm(pWav, framesToRead, pBufferOut);
  4010. }
  4011. if (pWav->translatedFormatTag == DR_WAVE_FORMAT_ADPCM) {
  4012. return drwav_read_pcm_frames_f32__msadpcm(pWav, framesToRead, pBufferOut);
  4013. }
  4014. if (pWav->translatedFormatTag == DR_WAVE_FORMAT_IEEE_FLOAT) {
  4015. return drwav_read_pcm_frames_f32__ieee(pWav, framesToRead, pBufferOut);
  4016. }
  4017. if (pWav->translatedFormatTag == DR_WAVE_FORMAT_ALAW) {
  4018. return drwav_read_pcm_frames_f32__alaw(pWav, framesToRead, pBufferOut);
  4019. }
  4020. if (pWav->translatedFormatTag == DR_WAVE_FORMAT_MULAW) {
  4021. return drwav_read_pcm_frames_f32__mulaw(pWav, framesToRead, pBufferOut);
  4022. }
  4023. if (pWav->translatedFormatTag == DR_WAVE_FORMAT_DVI_ADPCM) {
  4024. return drwav_read_pcm_frames_f32__ima(pWav, framesToRead, pBufferOut);
  4025. }
  4026. return 0;
  4027. }
  4028. DRWAV_API drwav_uint64 drwav_read_pcm_frames_f32le(drwav* pWav, drwav_uint64 framesToRead, float* pBufferOut)
  4029. {
  4030. drwav_uint64 framesRead = drwav_read_pcm_frames_f32(pWav, framesToRead, pBufferOut);
  4031. if (pBufferOut != NULL && drwav__is_little_endian() == DRWAV_FALSE) {
  4032. drwav__bswap_samples_f32(pBufferOut, framesRead*pWav->channels);
  4033. }
  4034. return framesRead;
  4035. }
  4036. DRWAV_API drwav_uint64 drwav_read_pcm_frames_f32be(drwav* pWav, drwav_uint64 framesToRead, float* pBufferOut)
  4037. {
  4038. drwav_uint64 framesRead = drwav_read_pcm_frames_f32(pWav, framesToRead, pBufferOut);
  4039. if (pBufferOut != NULL && drwav__is_little_endian() == DRWAV_TRUE) {
  4040. drwav__bswap_samples_f32(pBufferOut, framesRead*pWav->channels);
  4041. }
  4042. return framesRead;
  4043. }
  4044. DRWAV_API void drwav_u8_to_f32(float* pOut, const drwav_uint8* pIn, size_t sampleCount)
  4045. {
  4046. size_t i;
  4047. if (pOut == NULL || pIn == NULL) {
  4048. return;
  4049. }
  4050. #ifdef DR_WAV_LIBSNDFILE_COMPAT
  4051. /*
  4052. It appears libsndfile uses slightly different logic for the u8 -> f32 conversion to dr_wav, which in my opinion is incorrect. It appears
  4053. libsndfile performs the conversion something like "f32 = (u8 / 256) * 2 - 1", however I think it should be "f32 = (u8 / 255) * 2 - 1" (note
  4054. the divisor of 256 vs 255). I use libsndfile as a benchmark for testing, so I'm therefore leaving this block here just for my automated
  4055. correctness testing. This is disabled by default.
  4056. */
  4057. for (i = 0; i < sampleCount; ++i) {
  4058. *pOut++ = (pIn[i] / 256.0f) * 2 - 1;
  4059. }
  4060. #else
  4061. for (i = 0; i < sampleCount; ++i) {
  4062. float x = pIn[i];
  4063. x = x * 0.00784313725490196078f; /* 0..255 to 0..2 */
  4064. x = x - 1; /* 0..2 to -1..1 */
  4065. *pOut++ = x;
  4066. }
  4067. #endif
  4068. }
  4069. DRWAV_API void drwav_s16_to_f32(float* pOut, const drwav_int16* pIn, size_t sampleCount)
  4070. {
  4071. size_t i;
  4072. if (pOut == NULL || pIn == NULL) {
  4073. return;
  4074. }
  4075. for (i = 0; i < sampleCount; ++i) {
  4076. *pOut++ = pIn[i] * 0.000030517578125f;
  4077. }
  4078. }
  4079. DRWAV_API void drwav_s24_to_f32(float* pOut, const drwav_uint8* pIn, size_t sampleCount)
  4080. {
  4081. size_t i;
  4082. if (pOut == NULL || pIn == NULL) {
  4083. return;
  4084. }
  4085. for (i = 0; i < sampleCount; ++i) {
  4086. double x;
  4087. drwav_uint32 a = ((drwav_uint32)(pIn[i*3+0]) << 8);
  4088. drwav_uint32 b = ((drwav_uint32)(pIn[i*3+1]) << 16);
  4089. drwav_uint32 c = ((drwav_uint32)(pIn[i*3+2]) << 24);
  4090. x = (double)((drwav_int32)(a | b | c) >> 8);
  4091. *pOut++ = (float)(x * 0.00000011920928955078125);
  4092. }
  4093. }
  4094. DRWAV_API void drwav_s32_to_f32(float* pOut, const drwav_int32* pIn, size_t sampleCount)
  4095. {
  4096. size_t i;
  4097. if (pOut == NULL || pIn == NULL) {
  4098. return;
  4099. }
  4100. for (i = 0; i < sampleCount; ++i) {
  4101. *pOut++ = (float)(pIn[i] / 2147483648.0);
  4102. }
  4103. }
  4104. DRWAV_API void drwav_f64_to_f32(float* pOut, const double* pIn, size_t sampleCount)
  4105. {
  4106. size_t i;
  4107. if (pOut == NULL || pIn == NULL) {
  4108. return;
  4109. }
  4110. for (i = 0; i < sampleCount; ++i) {
  4111. *pOut++ = (float)pIn[i];
  4112. }
  4113. }
  4114. DRWAV_API void drwav_alaw_to_f32(float* pOut, const drwav_uint8* pIn, size_t sampleCount)
  4115. {
  4116. size_t i;
  4117. if (pOut == NULL || pIn == NULL) {
  4118. return;
  4119. }
  4120. for (i = 0; i < sampleCount; ++i) {
  4121. *pOut++ = drwav__alaw_to_s16(pIn[i]) / 32768.0f;
  4122. }
  4123. }
  4124. DRWAV_API void drwav_mulaw_to_f32(float* pOut, const drwav_uint8* pIn, size_t sampleCount)
  4125. {
  4126. size_t i;
  4127. if (pOut == NULL || pIn == NULL) {
  4128. return;
  4129. }
  4130. for (i = 0; i < sampleCount; ++i) {
  4131. *pOut++ = drwav__mulaw_to_s16(pIn[i]) / 32768.0f;
  4132. }
  4133. }
  4134. static void drwav__pcm_to_s32(drwav_int32* pOut, const drwav_uint8* pIn, size_t totalSampleCount, unsigned int bytesPerSample)
  4135. {
  4136. unsigned int i;
  4137. /* Special case for 8-bit sample data because it's treated as unsigned. */
  4138. if (bytesPerSample == 1) {
  4139. drwav_u8_to_s32(pOut, pIn, totalSampleCount);
  4140. return;
  4141. }
  4142. /* Slightly more optimal implementation for common formats. */
  4143. if (bytesPerSample == 2) {
  4144. drwav_s16_to_s32(pOut, (const drwav_int16*)pIn, totalSampleCount);
  4145. return;
  4146. }
  4147. if (bytesPerSample == 3) {
  4148. drwav_s24_to_s32(pOut, pIn, totalSampleCount);
  4149. return;
  4150. }
  4151. if (bytesPerSample == 4) {
  4152. for (i = 0; i < totalSampleCount; ++i) {
  4153. *pOut++ = ((const drwav_int32*)pIn)[i];
  4154. }
  4155. return;
  4156. }
  4157. /* Anything more than 64 bits per sample is not supported. */
  4158. if (bytesPerSample > 8) {
  4159. DRWAV_ZERO_MEMORY(pOut, totalSampleCount * sizeof(*pOut));
  4160. return;
  4161. }
  4162. /* Generic, slow converter. */
  4163. for (i = 0; i < totalSampleCount; ++i) {
  4164. drwav_uint64 sample = 0;
  4165. unsigned int shift = (8 - bytesPerSample) * 8;
  4166. unsigned int j;
  4167. for (j = 0; j < bytesPerSample; j += 1) {
  4168. DRWAV_ASSERT(j < 8);
  4169. sample |= (drwav_uint64)(pIn[j]) << shift;
  4170. shift += 8;
  4171. }
  4172. pIn += j;
  4173. *pOut++ = (drwav_int32)((drwav_int64)sample >> 32);
  4174. }
  4175. }
  4176. static void drwav__ieee_to_s32(drwav_int32* pOut, const drwav_uint8* pIn, size_t totalSampleCount, unsigned int bytesPerSample)
  4177. {
  4178. if (bytesPerSample == 4) {
  4179. drwav_f32_to_s32(pOut, (const float*)pIn, totalSampleCount);
  4180. return;
  4181. } else if (bytesPerSample == 8) {
  4182. drwav_f64_to_s32(pOut, (const double*)pIn, totalSampleCount);
  4183. return;
  4184. } else {
  4185. /* Only supporting 32- and 64-bit float. Output silence in all other cases. Contributions welcome for 16-bit float. */
  4186. DRWAV_ZERO_MEMORY(pOut, totalSampleCount * sizeof(*pOut));
  4187. return;
  4188. }
  4189. }
  4190. static drwav_uint64 drwav_read_pcm_frames_s32__pcm(drwav* pWav, drwav_uint64 framesToRead, drwav_int32* pBufferOut)
  4191. {
  4192. drwav_uint64 totalFramesRead;
  4193. drwav_uint8 sampleData[4096];
  4194. drwav_uint32 bytesPerFrame;
  4195. /* Fast path. */
  4196. if (pWav->translatedFormatTag == DR_WAVE_FORMAT_PCM && pWav->bitsPerSample == 32) {
  4197. return drwav_read_pcm_frames(pWav, framesToRead, pBufferOut);
  4198. }
  4199. bytesPerFrame = drwav_get_bytes_per_pcm_frame(pWav);
  4200. if (bytesPerFrame == 0) {
  4201. return 0;
  4202. }
  4203. totalFramesRead = 0;
  4204. while (framesToRead > 0) {
  4205. drwav_uint64 framesRead = drwav_read_pcm_frames(pWav, drwav_min(framesToRead, sizeof(sampleData)/bytesPerFrame), sampleData);
  4206. if (framesRead == 0) {
  4207. break;
  4208. }
  4209. drwav__pcm_to_s32(pBufferOut, sampleData, (size_t)(framesRead*pWav->channels), bytesPerFrame/pWav->channels);
  4210. pBufferOut += framesRead*pWav->channels;
  4211. framesToRead -= framesRead;
  4212. totalFramesRead += framesRead;
  4213. }
  4214. return totalFramesRead;
  4215. }
  4216. static drwav_uint64 drwav_read_pcm_frames_s32__msadpcm(drwav* pWav, drwav_uint64 framesToRead, drwav_int32* pBufferOut)
  4217. {
  4218. /*
  4219. We're just going to borrow the implementation from the drwav_read_s16() since ADPCM is a little bit more complicated than other formats and I don't
  4220. want to duplicate that code.
  4221. */
  4222. drwav_uint64 totalFramesRead = 0;
  4223. drwav_int16 samples16[2048];
  4224. while (framesToRead > 0) {
  4225. drwav_uint64 framesRead = drwav_read_pcm_frames_s16(pWav, drwav_min(framesToRead, drwav_countof(samples16)/pWav->channels), samples16);
  4226. if (framesRead == 0) {
  4227. break;
  4228. }
  4229. drwav_s16_to_s32(pBufferOut, samples16, (size_t)(framesRead*pWav->channels)); /* <-- Safe cast because we're clamping to 2048. */
  4230. pBufferOut += framesRead*pWav->channels;
  4231. framesToRead -= framesRead;
  4232. totalFramesRead += framesRead;
  4233. }
  4234. return totalFramesRead;
  4235. }
  4236. static drwav_uint64 drwav_read_pcm_frames_s32__ima(drwav* pWav, drwav_uint64 framesToRead, drwav_int32* pBufferOut)
  4237. {
  4238. /*
  4239. We're just going to borrow the implementation from the drwav_read_s16() since IMA-ADPCM is a little bit more complicated than other formats and I don't
  4240. want to duplicate that code.
  4241. */
  4242. drwav_uint64 totalFramesRead = 0;
  4243. drwav_int16 samples16[2048];
  4244. while (framesToRead > 0) {
  4245. drwav_uint64 framesRead = drwav_read_pcm_frames_s16(pWav, drwav_min(framesToRead, drwav_countof(samples16)/pWav->channels), samples16);
  4246. if (framesRead == 0) {
  4247. break;
  4248. }
  4249. drwav_s16_to_s32(pBufferOut, samples16, (size_t)(framesRead*pWav->channels)); /* <-- Safe cast because we're clamping to 2048. */
  4250. pBufferOut += framesRead*pWav->channels;
  4251. framesToRead -= framesRead;
  4252. totalFramesRead += framesRead;
  4253. }
  4254. return totalFramesRead;
  4255. }
  4256. static drwav_uint64 drwav_read_pcm_frames_s32__ieee(drwav* pWav, drwav_uint64 framesToRead, drwav_int32* pBufferOut)
  4257. {
  4258. drwav_uint64 totalFramesRead;
  4259. drwav_uint8 sampleData[4096];
  4260. drwav_uint32 bytesPerFrame = drwav_get_bytes_per_pcm_frame(pWav);
  4261. if (bytesPerFrame == 0) {
  4262. return 0;
  4263. }
  4264. totalFramesRead = 0;
  4265. while (framesToRead > 0) {
  4266. drwav_uint64 framesRead = drwav_read_pcm_frames(pWav, drwav_min(framesToRead, sizeof(sampleData)/bytesPerFrame), sampleData);
  4267. if (framesRead == 0) {
  4268. break;
  4269. }
  4270. drwav__ieee_to_s32(pBufferOut, sampleData, (size_t)(framesRead*pWav->channels), bytesPerFrame/pWav->channels);
  4271. pBufferOut += framesRead*pWav->channels;
  4272. framesToRead -= framesRead;
  4273. totalFramesRead += framesRead;
  4274. }
  4275. return totalFramesRead;
  4276. }
  4277. static drwav_uint64 drwav_read_pcm_frames_s32__alaw(drwav* pWav, drwav_uint64 framesToRead, drwav_int32* pBufferOut)
  4278. {
  4279. drwav_uint64 totalFramesRead;
  4280. drwav_uint8 sampleData[4096];
  4281. drwav_uint32 bytesPerFrame = drwav_get_bytes_per_pcm_frame(pWav);
  4282. if (bytesPerFrame == 0) {
  4283. return 0;
  4284. }
  4285. totalFramesRead = 0;
  4286. while (framesToRead > 0) {
  4287. drwav_uint64 framesRead = drwav_read_pcm_frames(pWav, drwav_min(framesToRead, sizeof(sampleData)/bytesPerFrame), sampleData);
  4288. if (framesRead == 0) {
  4289. break;
  4290. }
  4291. drwav_alaw_to_s32(pBufferOut, sampleData, (size_t)(framesRead*pWav->channels));
  4292. pBufferOut += framesRead*pWav->channels;
  4293. framesToRead -= framesRead;
  4294. totalFramesRead += framesRead;
  4295. }
  4296. return totalFramesRead;
  4297. }
  4298. static drwav_uint64 drwav_read_pcm_frames_s32__mulaw(drwav* pWav, drwav_uint64 framesToRead, drwav_int32* pBufferOut)
  4299. {
  4300. drwav_uint64 totalFramesRead;
  4301. drwav_uint8 sampleData[4096];
  4302. drwav_uint32 bytesPerFrame = drwav_get_bytes_per_pcm_frame(pWav);
  4303. if (bytesPerFrame == 0) {
  4304. return 0;
  4305. }
  4306. totalFramesRead = 0;
  4307. while (framesToRead > 0) {
  4308. drwav_uint64 framesRead = drwav_read_pcm_frames(pWav, drwav_min(framesToRead, sizeof(sampleData)/bytesPerFrame), sampleData);
  4309. if (framesRead == 0) {
  4310. break;
  4311. }
  4312. drwav_mulaw_to_s32(pBufferOut, sampleData, (size_t)(framesRead*pWav->channels));
  4313. pBufferOut += framesRead*pWav->channels;
  4314. framesToRead -= framesRead;
  4315. totalFramesRead += framesRead;
  4316. }
  4317. return totalFramesRead;
  4318. }
  4319. DRWAV_API drwav_uint64 drwav_read_pcm_frames_s32(drwav* pWav, drwav_uint64 framesToRead, drwav_int32* pBufferOut)
  4320. {
  4321. if (pWav == NULL || framesToRead == 0) {
  4322. return 0;
  4323. }
  4324. if (pBufferOut == NULL) {
  4325. return drwav_read_pcm_frames(pWav, framesToRead, NULL);
  4326. }
  4327. /* Don't try to read more samples than can potentially fit in the output buffer. */
  4328. if (framesToRead * pWav->channels * sizeof(drwav_int32) > DRWAV_SIZE_MAX) {
  4329. framesToRead = DRWAV_SIZE_MAX / sizeof(drwav_int32) / pWav->channels;
  4330. }
  4331. if (pWav->translatedFormatTag == DR_WAVE_FORMAT_PCM) {
  4332. return drwav_read_pcm_frames_s32__pcm(pWav, framesToRead, pBufferOut);
  4333. }
  4334. if (pWav->translatedFormatTag == DR_WAVE_FORMAT_ADPCM) {
  4335. return drwav_read_pcm_frames_s32__msadpcm(pWav, framesToRead, pBufferOut);
  4336. }
  4337. if (pWav->translatedFormatTag == DR_WAVE_FORMAT_IEEE_FLOAT) {
  4338. return drwav_read_pcm_frames_s32__ieee(pWav, framesToRead, pBufferOut);
  4339. }
  4340. if (pWav->translatedFormatTag == DR_WAVE_FORMAT_ALAW) {
  4341. return drwav_read_pcm_frames_s32__alaw(pWav, framesToRead, pBufferOut);
  4342. }
  4343. if (pWav->translatedFormatTag == DR_WAVE_FORMAT_MULAW) {
  4344. return drwav_read_pcm_frames_s32__mulaw(pWav, framesToRead, pBufferOut);
  4345. }
  4346. if (pWav->translatedFormatTag == DR_WAVE_FORMAT_DVI_ADPCM) {
  4347. return drwav_read_pcm_frames_s32__ima(pWav, framesToRead, pBufferOut);
  4348. }
  4349. return 0;
  4350. }
  4351. DRWAV_API drwav_uint64 drwav_read_pcm_frames_s32le(drwav* pWav, drwav_uint64 framesToRead, drwav_int32* pBufferOut)
  4352. {
  4353. drwav_uint64 framesRead = drwav_read_pcm_frames_s32(pWav, framesToRead, pBufferOut);
  4354. if (pBufferOut != NULL && drwav__is_little_endian() == DRWAV_FALSE) {
  4355. drwav__bswap_samples_s32(pBufferOut, framesRead*pWav->channels);
  4356. }
  4357. return framesRead;
  4358. }
  4359. DRWAV_API drwav_uint64 drwav_read_pcm_frames_s32be(drwav* pWav, drwav_uint64 framesToRead, drwav_int32* pBufferOut)
  4360. {
  4361. drwav_uint64 framesRead = drwav_read_pcm_frames_s32(pWav, framesToRead, pBufferOut);
  4362. if (pBufferOut != NULL && drwav__is_little_endian() == DRWAV_TRUE) {
  4363. drwav__bswap_samples_s32(pBufferOut, framesRead*pWav->channels);
  4364. }
  4365. return framesRead;
  4366. }
  4367. DRWAV_API void drwav_u8_to_s32(drwav_int32* pOut, const drwav_uint8* pIn, size_t sampleCount)
  4368. {
  4369. size_t i;
  4370. if (pOut == NULL || pIn == NULL) {
  4371. return;
  4372. }
  4373. for (i = 0; i < sampleCount; ++i) {
  4374. *pOut++ = ((int)pIn[i] - 128) << 24;
  4375. }
  4376. }
  4377. DRWAV_API void drwav_s16_to_s32(drwav_int32* pOut, const drwav_int16* pIn, size_t sampleCount)
  4378. {
  4379. size_t i;
  4380. if (pOut == NULL || pIn == NULL) {
  4381. return;
  4382. }
  4383. for (i = 0; i < sampleCount; ++i) {
  4384. *pOut++ = pIn[i] << 16;
  4385. }
  4386. }
  4387. DRWAV_API void drwav_s24_to_s32(drwav_int32* pOut, const drwav_uint8* pIn, size_t sampleCount)
  4388. {
  4389. size_t i;
  4390. if (pOut == NULL || pIn == NULL) {
  4391. return;
  4392. }
  4393. for (i = 0; i < sampleCount; ++i) {
  4394. unsigned int s0 = pIn[i*3 + 0];
  4395. unsigned int s1 = pIn[i*3 + 1];
  4396. unsigned int s2 = pIn[i*3 + 2];
  4397. drwav_int32 sample32 = (drwav_int32)((s0 << 8) | (s1 << 16) | (s2 << 24));
  4398. *pOut++ = sample32;
  4399. }
  4400. }
  4401. DRWAV_API void drwav_f32_to_s32(drwav_int32* pOut, const float* pIn, size_t sampleCount)
  4402. {
  4403. size_t i;
  4404. if (pOut == NULL || pIn == NULL) {
  4405. return;
  4406. }
  4407. for (i = 0; i < sampleCount; ++i) {
  4408. *pOut++ = (drwav_int32)(2147483648.0 * pIn[i]);
  4409. }
  4410. }
  4411. DRWAV_API void drwav_f64_to_s32(drwav_int32* pOut, const double* pIn, size_t sampleCount)
  4412. {
  4413. size_t i;
  4414. if (pOut == NULL || pIn == NULL) {
  4415. return;
  4416. }
  4417. for (i = 0; i < sampleCount; ++i) {
  4418. *pOut++ = (drwav_int32)(2147483648.0 * pIn[i]);
  4419. }
  4420. }
  4421. DRWAV_API void drwav_alaw_to_s32(drwav_int32* pOut, const drwav_uint8* pIn, size_t sampleCount)
  4422. {
  4423. size_t i;
  4424. if (pOut == NULL || pIn == NULL) {
  4425. return;
  4426. }
  4427. for (i = 0; i < sampleCount; ++i) {
  4428. *pOut++ = ((drwav_int32)drwav__alaw_to_s16(pIn[i])) << 16;
  4429. }
  4430. }
  4431. DRWAV_API void drwav_mulaw_to_s32(drwav_int32* pOut, const drwav_uint8* pIn, size_t sampleCount)
  4432. {
  4433. size_t i;
  4434. if (pOut == NULL || pIn == NULL) {
  4435. return;
  4436. }
  4437. for (i= 0; i < sampleCount; ++i) {
  4438. *pOut++ = ((drwav_int32)drwav__mulaw_to_s16(pIn[i])) << 16;
  4439. }
  4440. }
  4441. static drwav_int16* drwav__read_pcm_frames_and_close_s16(drwav* pWav, unsigned int* channels, unsigned int* sampleRate, drwav_uint64* totalFrameCount)
  4442. {
  4443. drwav_uint64 sampleDataSize;
  4444. drwav_int16* pSampleData;
  4445. drwav_uint64 framesRead;
  4446. DRWAV_ASSERT(pWav != NULL);
  4447. sampleDataSize = pWav->totalPCMFrameCount * pWav->channels * sizeof(drwav_int16);
  4448. if (sampleDataSize > DRWAV_SIZE_MAX) {
  4449. drwav_uninit(pWav);
  4450. return NULL; /* File's too big. */
  4451. }
  4452. pSampleData = (drwav_int16*)drwav__malloc_from_callbacks((size_t)sampleDataSize, &pWav->allocationCallbacks); /* <-- Safe cast due to the check above. */
  4453. if (pSampleData == NULL) {
  4454. drwav_uninit(pWav);
  4455. return NULL; /* Failed to allocate memory. */
  4456. }
  4457. framesRead = drwav_read_pcm_frames_s16(pWav, (size_t)pWav->totalPCMFrameCount, pSampleData);
  4458. if (framesRead != pWav->totalPCMFrameCount) {
  4459. drwav__free_from_callbacks(pSampleData, &pWav->allocationCallbacks);
  4460. drwav_uninit(pWav);
  4461. return NULL; /* There was an error reading the samples. */
  4462. }
  4463. drwav_uninit(pWav);
  4464. if (sampleRate) {
  4465. *sampleRate = pWav->sampleRate;
  4466. }
  4467. if (channels) {
  4468. *channels = pWav->channels;
  4469. }
  4470. if (totalFrameCount) {
  4471. *totalFrameCount = pWav->totalPCMFrameCount;
  4472. }
  4473. return pSampleData;
  4474. }
  4475. static float* drwav__read_pcm_frames_and_close_f32(drwav* pWav, unsigned int* channels, unsigned int* sampleRate, drwav_uint64* totalFrameCount)
  4476. {
  4477. drwav_uint64 sampleDataSize;
  4478. float* pSampleData;
  4479. drwav_uint64 framesRead;
  4480. DRWAV_ASSERT(pWav != NULL);
  4481. sampleDataSize = pWav->totalPCMFrameCount * pWav->channels * sizeof(float);
  4482. if (sampleDataSize > DRWAV_SIZE_MAX) {
  4483. drwav_uninit(pWav);
  4484. return NULL; /* File's too big. */
  4485. }
  4486. pSampleData = (float*)drwav__malloc_from_callbacks((size_t)sampleDataSize, &pWav->allocationCallbacks); /* <-- Safe cast due to the check above. */
  4487. if (pSampleData == NULL) {
  4488. drwav_uninit(pWav);
  4489. return NULL; /* Failed to allocate memory. */
  4490. }
  4491. framesRead = drwav_read_pcm_frames_f32(pWav, (size_t)pWav->totalPCMFrameCount, pSampleData);
  4492. if (framesRead != pWav->totalPCMFrameCount) {
  4493. drwav__free_from_callbacks(pSampleData, &pWav->allocationCallbacks);
  4494. drwav_uninit(pWav);
  4495. return NULL; /* There was an error reading the samples. */
  4496. }
  4497. drwav_uninit(pWav);
  4498. if (sampleRate) {
  4499. *sampleRate = pWav->sampleRate;
  4500. }
  4501. if (channels) {
  4502. *channels = pWav->channels;
  4503. }
  4504. if (totalFrameCount) {
  4505. *totalFrameCount = pWav->totalPCMFrameCount;
  4506. }
  4507. return pSampleData;
  4508. }
  4509. static drwav_int32* drwav__read_pcm_frames_and_close_s32(drwav* pWav, unsigned int* channels, unsigned int* sampleRate, drwav_uint64* totalFrameCount)
  4510. {
  4511. drwav_uint64 sampleDataSize;
  4512. drwav_int32* pSampleData;
  4513. drwav_uint64 framesRead;
  4514. DRWAV_ASSERT(pWav != NULL);
  4515. sampleDataSize = pWav->totalPCMFrameCount * pWav->channels * sizeof(drwav_int32);
  4516. if (sampleDataSize > DRWAV_SIZE_MAX) {
  4517. drwav_uninit(pWav);
  4518. return NULL; /* File's too big. */
  4519. }
  4520. pSampleData = (drwav_int32*)drwav__malloc_from_callbacks((size_t)sampleDataSize, &pWav->allocationCallbacks); /* <-- Safe cast due to the check above. */
  4521. if (pSampleData == NULL) {
  4522. drwav_uninit(pWav);
  4523. return NULL; /* Failed to allocate memory. */
  4524. }
  4525. framesRead = drwav_read_pcm_frames_s32(pWav, (size_t)pWav->totalPCMFrameCount, pSampleData);
  4526. if (framesRead != pWav->totalPCMFrameCount) {
  4527. drwav__free_from_callbacks(pSampleData, &pWav->allocationCallbacks);
  4528. drwav_uninit(pWav);
  4529. return NULL; /* There was an error reading the samples. */
  4530. }
  4531. drwav_uninit(pWav);
  4532. if (sampleRate) {
  4533. *sampleRate = pWav->sampleRate;
  4534. }
  4535. if (channels) {
  4536. *channels = pWav->channels;
  4537. }
  4538. if (totalFrameCount) {
  4539. *totalFrameCount = pWav->totalPCMFrameCount;
  4540. }
  4541. return pSampleData;
  4542. }
  4543. DRWAV_API drwav_int16* drwav_open_and_read_pcm_frames_s16(drwav_read_proc onRead, drwav_seek_proc onSeek, void* pUserData, unsigned int* channelsOut, unsigned int* sampleRateOut, drwav_uint64* totalFrameCountOut, const drwav_allocation_callbacks* pAllocationCallbacks)
  4544. {
  4545. drwav wav;
  4546. if (channelsOut) {
  4547. *channelsOut = 0;
  4548. }
  4549. if (sampleRateOut) {
  4550. *sampleRateOut = 0;
  4551. }
  4552. if (totalFrameCountOut) {
  4553. *totalFrameCountOut = 0;
  4554. }
  4555. if (!drwav_init(&wav, onRead, onSeek, pUserData, pAllocationCallbacks)) {
  4556. return NULL;
  4557. }
  4558. return drwav__read_pcm_frames_and_close_s16(&wav, channelsOut, sampleRateOut, totalFrameCountOut);
  4559. }
  4560. DRWAV_API float* drwav_open_and_read_pcm_frames_f32(drwav_read_proc onRead, drwav_seek_proc onSeek, void* pUserData, unsigned int* channelsOut, unsigned int* sampleRateOut, drwav_uint64* totalFrameCountOut, const drwav_allocation_callbacks* pAllocationCallbacks)
  4561. {
  4562. drwav wav;
  4563. if (channelsOut) {
  4564. *channelsOut = 0;
  4565. }
  4566. if (sampleRateOut) {
  4567. *sampleRateOut = 0;
  4568. }
  4569. if (totalFrameCountOut) {
  4570. *totalFrameCountOut = 0;
  4571. }
  4572. if (!drwav_init(&wav, onRead, onSeek, pUserData, pAllocationCallbacks)) {
  4573. return NULL;
  4574. }
  4575. return drwav__read_pcm_frames_and_close_f32(&wav, channelsOut, sampleRateOut, totalFrameCountOut);
  4576. }
  4577. DRWAV_API drwav_int32* drwav_open_and_read_pcm_frames_s32(drwav_read_proc onRead, drwav_seek_proc onSeek, void* pUserData, unsigned int* channelsOut, unsigned int* sampleRateOut, drwav_uint64* totalFrameCountOut, const drwav_allocation_callbacks* pAllocationCallbacks)
  4578. {
  4579. drwav wav;
  4580. if (channelsOut) {
  4581. *channelsOut = 0;
  4582. }
  4583. if (sampleRateOut) {
  4584. *sampleRateOut = 0;
  4585. }
  4586. if (totalFrameCountOut) {
  4587. *totalFrameCountOut = 0;
  4588. }
  4589. if (!drwav_init(&wav, onRead, onSeek, pUserData, pAllocationCallbacks)) {
  4590. return NULL;
  4591. }
  4592. return drwav__read_pcm_frames_and_close_s32(&wav, channelsOut, sampleRateOut, totalFrameCountOut);
  4593. }
  4594. #ifndef DR_WAV_NO_STDIO
  4595. DRWAV_API drwav_int16* drwav_open_file_and_read_pcm_frames_s16(const char* filename, unsigned int* channelsOut, unsigned int* sampleRateOut, drwav_uint64* totalFrameCountOut, const drwav_allocation_callbacks* pAllocationCallbacks)
  4596. {
  4597. drwav wav;
  4598. if (channelsOut) {
  4599. *channelsOut = 0;
  4600. }
  4601. if (sampleRateOut) {
  4602. *sampleRateOut = 0;
  4603. }
  4604. if (totalFrameCountOut) {
  4605. *totalFrameCountOut = 0;
  4606. }
  4607. if (!drwav_init_file(&wav, filename, pAllocationCallbacks)) {
  4608. return NULL;
  4609. }
  4610. return drwav__read_pcm_frames_and_close_s16(&wav, channelsOut, sampleRateOut, totalFrameCountOut);
  4611. }
  4612. DRWAV_API float* drwav_open_file_and_read_pcm_frames_f32(const char* filename, unsigned int* channelsOut, unsigned int* sampleRateOut, drwav_uint64* totalFrameCountOut, const drwav_allocation_callbacks* pAllocationCallbacks)
  4613. {
  4614. drwav wav;
  4615. if (channelsOut) {
  4616. *channelsOut = 0;
  4617. }
  4618. if (sampleRateOut) {
  4619. *sampleRateOut = 0;
  4620. }
  4621. if (totalFrameCountOut) {
  4622. *totalFrameCountOut = 0;
  4623. }
  4624. if (!drwav_init_file(&wav, filename, pAllocationCallbacks)) {
  4625. return NULL;
  4626. }
  4627. return drwav__read_pcm_frames_and_close_f32(&wav, channelsOut, sampleRateOut, totalFrameCountOut);
  4628. }
  4629. DRWAV_API drwav_int32* drwav_open_file_and_read_pcm_frames_s32(const char* filename, unsigned int* channelsOut, unsigned int* sampleRateOut, drwav_uint64* totalFrameCountOut, const drwav_allocation_callbacks* pAllocationCallbacks)
  4630. {
  4631. drwav wav;
  4632. if (channelsOut) {
  4633. *channelsOut = 0;
  4634. }
  4635. if (sampleRateOut) {
  4636. *sampleRateOut = 0;
  4637. }
  4638. if (totalFrameCountOut) {
  4639. *totalFrameCountOut = 0;
  4640. }
  4641. if (!drwav_init_file(&wav, filename, pAllocationCallbacks)) {
  4642. return NULL;
  4643. }
  4644. return drwav__read_pcm_frames_and_close_s32(&wav, channelsOut, sampleRateOut, totalFrameCountOut);
  4645. }
  4646. DRWAV_API drwav_int16* drwav_open_file_and_read_pcm_frames_s16_w(const wchar_t* filename, unsigned int* channelsOut, unsigned int* sampleRateOut, drwav_uint64* totalFrameCountOut, const drwav_allocation_callbacks* pAllocationCallbacks)
  4647. {
  4648. drwav wav;
  4649. if (sampleRateOut) {
  4650. *sampleRateOut = 0;
  4651. }
  4652. if (channelsOut) {
  4653. *channelsOut = 0;
  4654. }
  4655. if (totalFrameCountOut) {
  4656. *totalFrameCountOut = 0;
  4657. }
  4658. if (!drwav_init_file_w(&wav, filename, pAllocationCallbacks)) {
  4659. return NULL;
  4660. }
  4661. return drwav__read_pcm_frames_and_close_s16(&wav, channelsOut, sampleRateOut, totalFrameCountOut);
  4662. }
  4663. DRWAV_API float* drwav_open_file_and_read_pcm_frames_f32_w(const wchar_t* filename, unsigned int* channelsOut, unsigned int* sampleRateOut, drwav_uint64* totalFrameCountOut, const drwav_allocation_callbacks* pAllocationCallbacks)
  4664. {
  4665. drwav wav;
  4666. if (sampleRateOut) {
  4667. *sampleRateOut = 0;
  4668. }
  4669. if (channelsOut) {
  4670. *channelsOut = 0;
  4671. }
  4672. if (totalFrameCountOut) {
  4673. *totalFrameCountOut = 0;
  4674. }
  4675. if (!drwav_init_file_w(&wav, filename, pAllocationCallbacks)) {
  4676. return NULL;
  4677. }
  4678. return drwav__read_pcm_frames_and_close_f32(&wav, channelsOut, sampleRateOut, totalFrameCountOut);
  4679. }
  4680. DRWAV_API drwav_int32* drwav_open_file_and_read_pcm_frames_s32_w(const wchar_t* filename, unsigned int* channelsOut, unsigned int* sampleRateOut, drwav_uint64* totalFrameCountOut, const drwav_allocation_callbacks* pAllocationCallbacks)
  4681. {
  4682. drwav wav;
  4683. if (sampleRateOut) {
  4684. *sampleRateOut = 0;
  4685. }
  4686. if (channelsOut) {
  4687. *channelsOut = 0;
  4688. }
  4689. if (totalFrameCountOut) {
  4690. *totalFrameCountOut = 0;
  4691. }
  4692. if (!drwav_init_file_w(&wav, filename, pAllocationCallbacks)) {
  4693. return NULL;
  4694. }
  4695. return drwav__read_pcm_frames_and_close_s32(&wav, channelsOut, sampleRateOut, totalFrameCountOut);
  4696. }
  4697. #endif
  4698. DRWAV_API drwav_int16* drwav_open_memory_and_read_pcm_frames_s16(const void* data, size_t dataSize, unsigned int* channelsOut, unsigned int* sampleRateOut, drwav_uint64* totalFrameCountOut, const drwav_allocation_callbacks* pAllocationCallbacks)
  4699. {
  4700. drwav wav;
  4701. if (channelsOut) {
  4702. *channelsOut = 0;
  4703. }
  4704. if (sampleRateOut) {
  4705. *sampleRateOut = 0;
  4706. }
  4707. if (totalFrameCountOut) {
  4708. *totalFrameCountOut = 0;
  4709. }
  4710. if (!drwav_init_memory(&wav, data, dataSize, pAllocationCallbacks)) {
  4711. return NULL;
  4712. }
  4713. return drwav__read_pcm_frames_and_close_s16(&wav, channelsOut, sampleRateOut, totalFrameCountOut);
  4714. }
  4715. DRWAV_API float* drwav_open_memory_and_read_pcm_frames_f32(const void* data, size_t dataSize, unsigned int* channelsOut, unsigned int* sampleRateOut, drwav_uint64* totalFrameCountOut, const drwav_allocation_callbacks* pAllocationCallbacks)
  4716. {
  4717. drwav wav;
  4718. if (channelsOut) {
  4719. *channelsOut = 0;
  4720. }
  4721. if (sampleRateOut) {
  4722. *sampleRateOut = 0;
  4723. }
  4724. if (totalFrameCountOut) {
  4725. *totalFrameCountOut = 0;
  4726. }
  4727. if (!drwav_init_memory(&wav, data, dataSize, pAllocationCallbacks)) {
  4728. return NULL;
  4729. }
  4730. return drwav__read_pcm_frames_and_close_f32(&wav, channelsOut, sampleRateOut, totalFrameCountOut);
  4731. }
  4732. DRWAV_API drwav_int32* drwav_open_memory_and_read_pcm_frames_s32(const void* data, size_t dataSize, unsigned int* channelsOut, unsigned int* sampleRateOut, drwav_uint64* totalFrameCountOut, const drwav_allocation_callbacks* pAllocationCallbacks)
  4733. {
  4734. drwav wav;
  4735. if (channelsOut) {
  4736. *channelsOut = 0;
  4737. }
  4738. if (sampleRateOut) {
  4739. *sampleRateOut = 0;
  4740. }
  4741. if (totalFrameCountOut) {
  4742. *totalFrameCountOut = 0;
  4743. }
  4744. if (!drwav_init_memory(&wav, data, dataSize, pAllocationCallbacks)) {
  4745. return NULL;
  4746. }
  4747. return drwav__read_pcm_frames_and_close_s32(&wav, channelsOut, sampleRateOut, totalFrameCountOut);
  4748. }
  4749. #endif /* DR_WAV_NO_CONVERSION_API */
  4750. DRWAV_API void drwav_free(void* p, const drwav_allocation_callbacks* pAllocationCallbacks)
  4751. {
  4752. if (pAllocationCallbacks != NULL) {
  4753. drwav__free_from_callbacks(p, pAllocationCallbacks);
  4754. } else {
  4755. drwav__free_default(p, NULL);
  4756. }
  4757. }
  4758. DRWAV_API drwav_uint16 drwav_bytes_to_u16(const drwav_uint8* data)
  4759. {
  4760. return drwav__bytes_to_u16(data);
  4761. }
  4762. DRWAV_API drwav_int16 drwav_bytes_to_s16(const drwav_uint8* data)
  4763. {
  4764. return drwav__bytes_to_s16(data);
  4765. }
  4766. DRWAV_API drwav_uint32 drwav_bytes_to_u32(const drwav_uint8* data)
  4767. {
  4768. return drwav__bytes_to_u32(data);
  4769. }
  4770. DRWAV_API drwav_int32 drwav_bytes_to_s32(const drwav_uint8* data)
  4771. {
  4772. return drwav__bytes_to_s32(data);
  4773. }
  4774. DRWAV_API drwav_uint64 drwav_bytes_to_u64(const drwav_uint8* data)
  4775. {
  4776. return drwav__bytes_to_u64(data);
  4777. }
  4778. DRWAV_API drwav_int64 drwav_bytes_to_s64(const drwav_uint8* data)
  4779. {
  4780. return drwav__bytes_to_s64(data);
  4781. }
  4782. DRWAV_API drwav_bool32 drwav_guid_equal(const drwav_uint8 a[16], const drwav_uint8 b[16])
  4783. {
  4784. return drwav__guid_equal(a, b);
  4785. }
  4786. DRWAV_API drwav_bool32 drwav_fourcc_equal(const drwav_uint8* a, const char* b)
  4787. {
  4788. return drwav__fourcc_equal(a, b);
  4789. }
  4790. #endif /* dr_wav_c */
  4791. #endif /* DR_WAV_IMPLEMENTATION */
  4792. /*
  4793. RELEASE NOTES - v0.11.0
  4794. =======================
  4795. Version 0.11.0 has breaking API changes.
  4796. Improved Client-Defined Memory Allocation
  4797. -----------------------------------------
  4798. The main change with this release is the addition of a more flexible way of implementing custom memory allocation routines. The
  4799. existing system of DRWAV_MALLOC, DRWAV_REALLOC and DRWAV_FREE are still in place and will be used by default when no custom
  4800. allocation callbacks are specified.
  4801. To use the new system, you pass in a pointer to a drwav_allocation_callbacks object to drwav_init() and family, like this:
  4802. void* my_malloc(size_t sz, void* pUserData)
  4803. {
  4804. return malloc(sz);
  4805. }
  4806. void* my_realloc(void* p, size_t sz, void* pUserData)
  4807. {
  4808. return realloc(p, sz);
  4809. }
  4810. void my_free(void* p, void* pUserData)
  4811. {
  4812. free(p);
  4813. }
  4814. ...
  4815. drwav_allocation_callbacks allocationCallbacks;
  4816. allocationCallbacks.pUserData = &myData;
  4817. allocationCallbacks.onMalloc = my_malloc;
  4818. allocationCallbacks.onRealloc = my_realloc;
  4819. allocationCallbacks.onFree = my_free;
  4820. drwav_init_file(&wav, "my_file.wav", &allocationCallbacks);
  4821. The advantage of this new system is that it allows you to specify user data which will be passed in to the allocation routines.
  4822. Passing in null for the allocation callbacks object will cause dr_wav to use defaults which is the same as DRWAV_MALLOC,
  4823. DRWAV_REALLOC and DRWAV_FREE and the equivalent of how it worked in previous versions.
  4824. Every API that opens a drwav object now takes this extra parameter. These include the following:
  4825. drwav_init()
  4826. drwav_init_ex()
  4827. drwav_init_file()
  4828. drwav_init_file_ex()
  4829. drwav_init_file_w()
  4830. drwav_init_file_w_ex()
  4831. drwav_init_memory()
  4832. drwav_init_memory_ex()
  4833. drwav_init_write()
  4834. drwav_init_write_sequential()
  4835. drwav_init_write_sequential_pcm_frames()
  4836. drwav_init_file_write()
  4837. drwav_init_file_write_sequential()
  4838. drwav_init_file_write_sequential_pcm_frames()
  4839. drwav_init_file_write_w()
  4840. drwav_init_file_write_sequential_w()
  4841. drwav_init_file_write_sequential_pcm_frames_w()
  4842. drwav_init_memory_write()
  4843. drwav_init_memory_write_sequential()
  4844. drwav_init_memory_write_sequential_pcm_frames()
  4845. drwav_open_and_read_pcm_frames_s16()
  4846. drwav_open_and_read_pcm_frames_f32()
  4847. drwav_open_and_read_pcm_frames_s32()
  4848. drwav_open_file_and_read_pcm_frames_s16()
  4849. drwav_open_file_and_read_pcm_frames_f32()
  4850. drwav_open_file_and_read_pcm_frames_s32()
  4851. drwav_open_file_and_read_pcm_frames_s16_w()
  4852. drwav_open_file_and_read_pcm_frames_f32_w()
  4853. drwav_open_file_and_read_pcm_frames_s32_w()
  4854. drwav_open_memory_and_read_pcm_frames_s16()
  4855. drwav_open_memory_and_read_pcm_frames_f32()
  4856. drwav_open_memory_and_read_pcm_frames_s32()
  4857. Endian Improvements
  4858. -------------------
  4859. Previously, the following APIs returned little-endian audio data. These now return native-endian data. This improves compatibility
  4860. on big-endian architectures.
  4861. drwav_read_pcm_frames()
  4862. drwav_read_pcm_frames_s16()
  4863. drwav_read_pcm_frames_s32()
  4864. drwav_read_pcm_frames_f32()
  4865. drwav_open_and_read_pcm_frames_s16()
  4866. drwav_open_and_read_pcm_frames_s32()
  4867. drwav_open_and_read_pcm_frames_f32()
  4868. drwav_open_file_and_read_pcm_frames_s16()
  4869. drwav_open_file_and_read_pcm_frames_s32()
  4870. drwav_open_file_and_read_pcm_frames_f32()
  4871. drwav_open_file_and_read_pcm_frames_s16_w()
  4872. drwav_open_file_and_read_pcm_frames_s32_w()
  4873. drwav_open_file_and_read_pcm_frames_f32_w()
  4874. drwav_open_memory_and_read_pcm_frames_s16()
  4875. drwav_open_memory_and_read_pcm_frames_s32()
  4876. drwav_open_memory_and_read_pcm_frames_f32()
  4877. APIs have been added to give you explicit control over whether or not audio data is read or written in big- or little-endian byte
  4878. order:
  4879. drwav_read_pcm_frames_le()
  4880. drwav_read_pcm_frames_be()
  4881. drwav_read_pcm_frames_s16le()
  4882. drwav_read_pcm_frames_s16be()
  4883. drwav_read_pcm_frames_f32le()
  4884. drwav_read_pcm_frames_f32be()
  4885. drwav_read_pcm_frames_s32le()
  4886. drwav_read_pcm_frames_s32be()
  4887. drwav_write_pcm_frames_le()
  4888. drwav_write_pcm_frames_be()
  4889. Removed APIs
  4890. ------------
  4891. The following APIs were deprecated in version 0.10.0 and have now been removed:
  4892. drwav_open()
  4893. drwav_open_ex()
  4894. drwav_open_write()
  4895. drwav_open_write_sequential()
  4896. drwav_open_file()
  4897. drwav_open_file_ex()
  4898. drwav_open_file_write()
  4899. drwav_open_file_write_sequential()
  4900. drwav_open_memory()
  4901. drwav_open_memory_ex()
  4902. drwav_open_memory_write()
  4903. drwav_open_memory_write_sequential()
  4904. drwav_close()
  4905. RELEASE NOTES - v0.10.0
  4906. =======================
  4907. Version 0.10.0 has breaking API changes. There are no significant bug fixes in this release, so if you are affected you do
  4908. not need to upgrade.
  4909. Removed APIs
  4910. ------------
  4911. The following APIs were deprecated in version 0.9.0 and have been completely removed in version 0.10.0:
  4912. drwav_read()
  4913. drwav_read_s16()
  4914. drwav_read_f32()
  4915. drwav_read_s32()
  4916. drwav_seek_to_sample()
  4917. drwav_write()
  4918. drwav_open_and_read_s16()
  4919. drwav_open_and_read_f32()
  4920. drwav_open_and_read_s32()
  4921. drwav_open_file_and_read_s16()
  4922. drwav_open_file_and_read_f32()
  4923. drwav_open_file_and_read_s32()
  4924. drwav_open_memory_and_read_s16()
  4925. drwav_open_memory_and_read_f32()
  4926. drwav_open_memory_and_read_s32()
  4927. drwav::totalSampleCount
  4928. See release notes for version 0.9.0 at the bottom of this file for replacement APIs.
  4929. Deprecated APIs
  4930. ---------------
  4931. The following APIs have been deprecated. There is a confusing and completely arbitrary difference between drwav_init*() and
  4932. drwav_open*(), where drwav_init*() initializes a pre-allocated drwav object, whereas drwav_open*() will first allocated a
  4933. drwav object on the heap and then initialize it. drwav_open*() has been deprecated which means you must now use a pre-
  4934. allocated drwav object with drwav_init*(). If you need the previous functionality, you can just do a malloc() followed by
  4935. a called to one of the drwav_init*() APIs.
  4936. drwav_open()
  4937. drwav_open_ex()
  4938. drwav_open_write()
  4939. drwav_open_write_sequential()
  4940. drwav_open_file()
  4941. drwav_open_file_ex()
  4942. drwav_open_file_write()
  4943. drwav_open_file_write_sequential()
  4944. drwav_open_memory()
  4945. drwav_open_memory_ex()
  4946. drwav_open_memory_write()
  4947. drwav_open_memory_write_sequential()
  4948. drwav_close()
  4949. These APIs will be removed completely in a future version. The rationale for this change is to remove confusion between the
  4950. two different ways to initialize a drwav object.
  4951. */
  4952. /*
  4953. REVISION HISTORY
  4954. ================
  4955. v0.12.16 - 2020-12-02
  4956. - Fix a bug when trying to read more bytes than can fit in a size_t.
  4957. v0.12.15 - 2020-11-21
  4958. - Fix compilation with OpenWatcom.
  4959. v0.12.14 - 2020-11-13
  4960. - Minor code clean up.
  4961. v0.12.13 - 2020-11-01
  4962. - Improve compiler support for older versions of GCC.
  4963. v0.12.12 - 2020-09-28
  4964. - Add support for RF64.
  4965. - Fix a bug in writing mode where the size of the RIFF chunk incorrectly includes the header section.
  4966. v0.12.11 - 2020-09-08
  4967. - Fix a compilation error on older compilers.
  4968. v0.12.10 - 2020-08-24
  4969. - Fix a bug when seeking with ADPCM formats.
  4970. v0.12.9 - 2020-08-02
  4971. - Simplify sized types.
  4972. v0.12.8 - 2020-07-25
  4973. - Fix a compilation warning.
  4974. v0.12.7 - 2020-07-15
  4975. - Fix some bugs on big-endian architectures.
  4976. - Fix an error in s24 to f32 conversion.
  4977. v0.12.6 - 2020-06-23
  4978. - Change drwav_read_*() to allow NULL to be passed in as the output buffer which is equivalent to a forward seek.
  4979. - Fix a buffer overflow when trying to decode invalid IMA-ADPCM files.
  4980. - Add include guard for the implementation section.
  4981. v0.12.5 - 2020-05-27
  4982. - Minor documentation fix.
  4983. v0.12.4 - 2020-05-16
  4984. - Replace assert() with DRWAV_ASSERT().
  4985. - Add compile-time and run-time version querying.
  4986. - DRWAV_VERSION_MINOR
  4987. - DRWAV_VERSION_MAJOR
  4988. - DRWAV_VERSION_REVISION
  4989. - DRWAV_VERSION_STRING
  4990. - drwav_version()
  4991. - drwav_version_string()
  4992. v0.12.3 - 2020-04-30
  4993. - Fix compilation errors with VC6.
  4994. v0.12.2 - 2020-04-21
  4995. - Fix a bug where drwav_init_file() does not close the file handle after attempting to load an erroneous file.
  4996. v0.12.1 - 2020-04-13
  4997. - Fix some pedantic warnings.
  4998. v0.12.0 - 2020-04-04
  4999. - API CHANGE: Add container and format parameters to the chunk callback.
  5000. - Minor documentation updates.
  5001. v0.11.5 - 2020-03-07
  5002. - Fix compilation error with Visual Studio .NET 2003.
  5003. v0.11.4 - 2020-01-29
  5004. - Fix some static analysis warnings.
  5005. - Fix a bug when reading f32 samples from an A-law encoded stream.
  5006. v0.11.3 - 2020-01-12
  5007. - Minor changes to some f32 format conversion routines.
  5008. - Minor bug fix for ADPCM conversion when end of file is reached.
  5009. v0.11.2 - 2019-12-02
  5010. - Fix a possible crash when using custom memory allocators without a custom realloc() implementation.
  5011. - Fix an integer overflow bug.
  5012. - Fix a null pointer dereference bug.
  5013. - Add limits to sample rate, channels and bits per sample to tighten up some validation.
  5014. v0.11.1 - 2019-10-07
  5015. - Internal code clean up.
  5016. v0.11.0 - 2019-10-06
  5017. - API CHANGE: Add support for user defined memory allocation routines. This system allows the program to specify their own memory allocation
  5018. routines with a user data pointer for client-specific contextual data. This adds an extra parameter to the end of the following APIs:
  5019. - drwav_init()
  5020. - drwav_init_ex()
  5021. - drwav_init_file()
  5022. - drwav_init_file_ex()
  5023. - drwav_init_file_w()
  5024. - drwav_init_file_w_ex()
  5025. - drwav_init_memory()
  5026. - drwav_init_memory_ex()
  5027. - drwav_init_write()
  5028. - drwav_init_write_sequential()
  5029. - drwav_init_write_sequential_pcm_frames()
  5030. - drwav_init_file_write()
  5031. - drwav_init_file_write_sequential()
  5032. - drwav_init_file_write_sequential_pcm_frames()
  5033. - drwav_init_file_write_w()
  5034. - drwav_init_file_write_sequential_w()
  5035. - drwav_init_file_write_sequential_pcm_frames_w()
  5036. - drwav_init_memory_write()
  5037. - drwav_init_memory_write_sequential()
  5038. - drwav_init_memory_write_sequential_pcm_frames()
  5039. - drwav_open_and_read_pcm_frames_s16()
  5040. - drwav_open_and_read_pcm_frames_f32()
  5041. - drwav_open_and_read_pcm_frames_s32()
  5042. - drwav_open_file_and_read_pcm_frames_s16()
  5043. - drwav_open_file_and_read_pcm_frames_f32()
  5044. - drwav_open_file_and_read_pcm_frames_s32()
  5045. - drwav_open_file_and_read_pcm_frames_s16_w()
  5046. - drwav_open_file_and_read_pcm_frames_f32_w()
  5047. - drwav_open_file_and_read_pcm_frames_s32_w()
  5048. - drwav_open_memory_and_read_pcm_frames_s16()
  5049. - drwav_open_memory_and_read_pcm_frames_f32()
  5050. - drwav_open_memory_and_read_pcm_frames_s32()
  5051. Set this extra parameter to NULL to use defaults which is the same as the previous behaviour. Setting this NULL will use
  5052. DRWAV_MALLOC, DRWAV_REALLOC and DRWAV_FREE.
  5053. - Add support for reading and writing PCM frames in an explicit endianness. New APIs:
  5054. - drwav_read_pcm_frames_le()
  5055. - drwav_read_pcm_frames_be()
  5056. - drwav_read_pcm_frames_s16le()
  5057. - drwav_read_pcm_frames_s16be()
  5058. - drwav_read_pcm_frames_f32le()
  5059. - drwav_read_pcm_frames_f32be()
  5060. - drwav_read_pcm_frames_s32le()
  5061. - drwav_read_pcm_frames_s32be()
  5062. - drwav_write_pcm_frames_le()
  5063. - drwav_write_pcm_frames_be()
  5064. - Remove deprecated APIs.
  5065. - API CHANGE: The following APIs now return native-endian data. Previously they returned little-endian data.
  5066. - drwav_read_pcm_frames()
  5067. - drwav_read_pcm_frames_s16()
  5068. - drwav_read_pcm_frames_s32()
  5069. - drwav_read_pcm_frames_f32()
  5070. - drwav_open_and_read_pcm_frames_s16()
  5071. - drwav_open_and_read_pcm_frames_s32()
  5072. - drwav_open_and_read_pcm_frames_f32()
  5073. - drwav_open_file_and_read_pcm_frames_s16()
  5074. - drwav_open_file_and_read_pcm_frames_s32()
  5075. - drwav_open_file_and_read_pcm_frames_f32()
  5076. - drwav_open_file_and_read_pcm_frames_s16_w()
  5077. - drwav_open_file_and_read_pcm_frames_s32_w()
  5078. - drwav_open_file_and_read_pcm_frames_f32_w()
  5079. - drwav_open_memory_and_read_pcm_frames_s16()
  5080. - drwav_open_memory_and_read_pcm_frames_s32()
  5081. - drwav_open_memory_and_read_pcm_frames_f32()
  5082. v0.10.1 - 2019-08-31
  5083. - Correctly handle partial trailing ADPCM blocks.
  5084. v0.10.0 - 2019-08-04
  5085. - Remove deprecated APIs.
  5086. - Add wchar_t variants for file loading APIs:
  5087. drwav_init_file_w()
  5088. drwav_init_file_ex_w()
  5089. drwav_init_file_write_w()
  5090. drwav_init_file_write_sequential_w()
  5091. - Add drwav_target_write_size_bytes() which calculates the total size in bytes of a WAV file given a format and sample count.
  5092. - Add APIs for specifying the PCM frame count instead of the sample count when opening in sequential write mode:
  5093. drwav_init_write_sequential_pcm_frames()
  5094. drwav_init_file_write_sequential_pcm_frames()
  5095. drwav_init_file_write_sequential_pcm_frames_w()
  5096. drwav_init_memory_write_sequential_pcm_frames()
  5097. - Deprecate drwav_open*() and drwav_close():
  5098. drwav_open()
  5099. drwav_open_ex()
  5100. drwav_open_write()
  5101. drwav_open_write_sequential()
  5102. drwav_open_file()
  5103. drwav_open_file_ex()
  5104. drwav_open_file_write()
  5105. drwav_open_file_write_sequential()
  5106. drwav_open_memory()
  5107. drwav_open_memory_ex()
  5108. drwav_open_memory_write()
  5109. drwav_open_memory_write_sequential()
  5110. drwav_close()
  5111. - Minor documentation updates.
  5112. v0.9.2 - 2019-05-21
  5113. - Fix warnings.
  5114. v0.9.1 - 2019-05-05
  5115. - Add support for C89.
  5116. - Change license to choice of public domain or MIT-0.
  5117. v0.9.0 - 2018-12-16
  5118. - API CHANGE: Add new reading APIs for reading by PCM frames instead of samples. Old APIs have been deprecated and
  5119. will be removed in v0.10.0. Deprecated APIs and their replacements:
  5120. drwav_read() -> drwav_read_pcm_frames()
  5121. drwav_read_s16() -> drwav_read_pcm_frames_s16()
  5122. drwav_read_f32() -> drwav_read_pcm_frames_f32()
  5123. drwav_read_s32() -> drwav_read_pcm_frames_s32()
  5124. drwav_seek_to_sample() -> drwav_seek_to_pcm_frame()
  5125. drwav_write() -> drwav_write_pcm_frames()
  5126. drwav_open_and_read_s16() -> drwav_open_and_read_pcm_frames_s16()
  5127. drwav_open_and_read_f32() -> drwav_open_and_read_pcm_frames_f32()
  5128. drwav_open_and_read_s32() -> drwav_open_and_read_pcm_frames_s32()
  5129. drwav_open_file_and_read_s16() -> drwav_open_file_and_read_pcm_frames_s16()
  5130. drwav_open_file_and_read_f32() -> drwav_open_file_and_read_pcm_frames_f32()
  5131. drwav_open_file_and_read_s32() -> drwav_open_file_and_read_pcm_frames_s32()
  5132. drwav_open_memory_and_read_s16() -> drwav_open_memory_and_read_pcm_frames_s16()
  5133. drwav_open_memory_and_read_f32() -> drwav_open_memory_and_read_pcm_frames_f32()
  5134. drwav_open_memory_and_read_s32() -> drwav_open_memory_and_read_pcm_frames_s32()
  5135. drwav::totalSampleCount -> drwav::totalPCMFrameCount
  5136. - API CHANGE: Rename drwav_open_and_read_file_*() to drwav_open_file_and_read_*().
  5137. - API CHANGE: Rename drwav_open_and_read_memory_*() to drwav_open_memory_and_read_*().
  5138. - Add built-in support for smpl chunks.
  5139. - Add support for firing a callback for each chunk in the file at initialization time.
  5140. - This is enabled through the drwav_init_ex(), etc. family of APIs.
  5141. - Handle invalid FMT chunks more robustly.
  5142. v0.8.5 - 2018-09-11
  5143. - Const correctness.
  5144. - Fix a potential stack overflow.
  5145. v0.8.4 - 2018-08-07
  5146. - Improve 64-bit detection.
  5147. v0.8.3 - 2018-08-05
  5148. - Fix C++ build on older versions of GCC.
  5149. v0.8.2 - 2018-08-02
  5150. - Fix some big-endian bugs.
  5151. v0.8.1 - 2018-06-29
  5152. - Add support for sequential writing APIs.
  5153. - Disable seeking in write mode.
  5154. - Fix bugs with Wave64.
  5155. - Fix typos.
  5156. v0.8 - 2018-04-27
  5157. - Bug fix.
  5158. - Start using major.minor.revision versioning.
  5159. v0.7f - 2018-02-05
  5160. - Restrict ADPCM formats to a maximum of 2 channels.
  5161. v0.7e - 2018-02-02
  5162. - Fix a crash.
  5163. v0.7d - 2018-02-01
  5164. - Fix a crash.
  5165. v0.7c - 2018-02-01
  5166. - Set drwav.bytesPerSample to 0 for all compressed formats.
  5167. - Fix a crash when reading 16-bit floating point WAV files. In this case dr_wav will output silence for
  5168. all format conversion reading APIs (*_s16, *_s32, *_f32 APIs).
  5169. - Fix some divide-by-zero errors.
  5170. v0.7b - 2018-01-22
  5171. - Fix errors with seeking of compressed formats.
  5172. - Fix compilation error when DR_WAV_NO_CONVERSION_API
  5173. v0.7a - 2017-11-17
  5174. - Fix some GCC warnings.
  5175. v0.7 - 2017-11-04
  5176. - Add writing APIs.
  5177. v0.6 - 2017-08-16
  5178. - API CHANGE: Rename dr_* types to drwav_*.
  5179. - Add support for custom implementations of malloc(), realloc(), etc.
  5180. - Add support for Microsoft ADPCM.
  5181. - Add support for IMA ADPCM (DVI, format code 0x11).
  5182. - Optimizations to drwav_read_s16().
  5183. - Bug fixes.
  5184. v0.5g - 2017-07-16
  5185. - Change underlying type for booleans to unsigned.
  5186. v0.5f - 2017-04-04
  5187. - Fix a minor bug with drwav_open_and_read_s16() and family.
  5188. v0.5e - 2016-12-29
  5189. - Added support for reading samples as signed 16-bit integers. Use the _s16() family of APIs for this.
  5190. - Minor fixes to documentation.
  5191. v0.5d - 2016-12-28
  5192. - Use drwav_int* and drwav_uint* sized types to improve compiler support.
  5193. v0.5c - 2016-11-11
  5194. - Properly handle JUNK chunks that come before the FMT chunk.
  5195. v0.5b - 2016-10-23
  5196. - A minor change to drwav_bool8 and drwav_bool32 types.
  5197. v0.5a - 2016-10-11
  5198. - Fixed a bug with drwav_open_and_read() and family due to incorrect argument ordering.
  5199. - Improve A-law and mu-law efficiency.
  5200. v0.5 - 2016-09-29
  5201. - API CHANGE. Swap the order of "channels" and "sampleRate" parameters in drwav_open_and_read*(). Rationale for this is to
  5202. keep it consistent with dr_audio and dr_flac.
  5203. v0.4b - 2016-09-18
  5204. - Fixed a typo in documentation.
  5205. v0.4a - 2016-09-18
  5206. - Fixed a typo.
  5207. - Change date format to ISO 8601 (YYYY-MM-DD)
  5208. v0.4 - 2016-07-13
  5209. - API CHANGE. Make onSeek consistent with dr_flac.
  5210. - API CHANGE. Rename drwav_seek() to drwav_seek_to_sample() for clarity and consistency with dr_flac.
  5211. - Added support for Sony Wave64.
  5212. v0.3a - 2016-05-28
  5213. - API CHANGE. Return drwav_bool32 instead of int in onSeek callback.
  5214. - Fixed a memory leak.
  5215. v0.3 - 2016-05-22
  5216. - Lots of API changes for consistency.
  5217. v0.2a - 2016-05-16
  5218. - Fixed Linux/GCC build.
  5219. v0.2 - 2016-05-11
  5220. - Added support for reading data as signed 32-bit PCM for consistency with dr_flac.
  5221. v0.1a - 2016-05-07
  5222. - Fixed a bug in drwav_open_file() where the file handle would not be closed if the loader failed to initialize.
  5223. v0.1 - 2016-05-04
  5224. - Initial versioned release.
  5225. */
  5226. /*
  5227. This software is available as a choice of the following licenses. Choose
  5228. whichever you prefer.
  5229. ===============================================================================
  5230. ALTERNATIVE 1 - Public Domain (www.unlicense.org)
  5231. ===============================================================================
  5232. This is free and unencumbered software released into the public domain.
  5233. Anyone is free to copy, modify, publish, use, compile, sell, or distribute this
  5234. software, either in source code form or as a compiled binary, for any purpose,
  5235. commercial or non-commercial, and by any means.
  5236. In jurisdictions that recognize copyright laws, the author or authors of this
  5237. software dedicate any and all copyright interest in the software to the public
  5238. domain. We make this dedication for the benefit of the public at large and to
  5239. the detriment of our heirs and successors. We intend this dedication to be an
  5240. overt act of relinquishment in perpetuity of all present and future rights to
  5241. this software under copyright law.
  5242. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  5243. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  5244. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  5245. AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  5246. ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  5247. WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  5248. For more information, please refer to <http://unlicense.org/>
  5249. ===============================================================================
  5250. ALTERNATIVE 2 - MIT No Attribution
  5251. ===============================================================================
  5252. Copyright 2020 David Reid
  5253. Permission is hereby granted, free of charge, to any person obtaining a copy of
  5254. this software and associated documentation files (the "Software"), to deal in
  5255. the Software without restriction, including without limitation the rights to
  5256. use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
  5257. of the Software, and to permit persons to whom the Software is furnished to do
  5258. so.
  5259. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  5260. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  5261. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  5262. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  5263. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  5264. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  5265. SOFTWARE.
  5266. */