Sound Stream.cpp 98 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642
  1. /******************************************************************************/
  2. #include "stdafx.h"
  3. #define CC4_SND CC4('S', 'N', 'D', '\0')
  4. #include "../../../ThirdPartyLibs/begin.h"
  5. #if SUPPORT_FLAC
  6. #define FLAC__NO_DLL
  7. #include "../../../ThirdPartyLibs/Flac/lib/include/FLAC/all.h"
  8. #endif
  9. #if SUPPORT_VORBIS || SUPPORT_OPUS
  10. #include "../../../ThirdPartyLibs/VP/libvpx/third_party/libwebm/mkvparser/mkvparser.h"
  11. #define NANOS 1000000000
  12. #endif
  13. #if SUPPORT_VORBIS
  14. #include "../../../ThirdPartyLibs/Vorbis/include/vorbis/vorbisfile.h"
  15. #endif
  16. #if SUPPORT_OPUS || SUPPORT_OPUS_ENC
  17. namespace
  18. {
  19. #include "../../../ThirdPartyLibs/Opus/lib/include/opus.h"
  20. #include "../../../ThirdPartyLibs/Opus/file/src/internal.h"
  21. #include "../../../ThirdPartyLibs/Opus/file/include/opusfile.h"
  22. #include "../../../ThirdPartyLibs/Ogg/include/ogg/ogg.h"
  23. typedef OpusEncoder *OE;
  24. typedef OpusDecoder *OD;
  25. }
  26. #define OPUS_MAX_FRAME_SAMPLES 2880
  27. #define OPUS_FRAME_HISTORY 32 // tests have shown we need to decode at least 32 packets earlier to get same results
  28. #define OPUS_FRAME_HISTORY_FAST 4 // 4 frames was enough to produce satisfactory results
  29. static Int OpusMaxFrameSize(Int channels)
  30. {
  31. return (OPUS_MAX_FRAME_SAMPLES*SIZE(I16))*channels;
  32. }
  33. #endif
  34. #if SUPPORT_MP3
  35. #define MINIMP3_ONLY_MP3
  36. #if !((WINDOWS && ARM) || WEB)
  37. #define MINIMP3_ONLY_SIMD
  38. #endif
  39. //#define MINIMP3_NONSTANDARD_BUT_LOGICAL
  40. //#define MINIMP3_FLOAT_OUTPUT
  41. #define MINIMP3_IMPLEMENTATION
  42. #define MINIMP3_NO_STDIO
  43. #define MAX_FRAME_SYNC_MATCHES 8
  44. #include "../../../ThirdPartyLibs/MiniMP3/minimp3.h"
  45. #define MP3_FRAME_SAMPLES 1152 // MPEG-1 Layer 3
  46. #define MP3_FRAME_SAMPLES2 576 // MPEG-2 Layer 3
  47. #define MP3_SAMPLE_DELAY 529 // http://mp3decoders.mp3-tech.org/decoders_lame.html or 528 http://lame.sourceforge.net/tech-FAQ.txt
  48. #endif
  49. #if SUPPORT_AAC
  50. #define INT64 AAC_INT64 // on Linux there are name conflicts
  51. #include "../../../ThirdPartyLibs/FDK-AAC/lib/libAACdec/include/aacdecoder_lib.h"
  52. #undef INT64
  53. #endif
  54. #include "../../../ThirdPartyLibs/end.h"
  55. #pragma warning(disable:4267) // 64bit - conversion from 'size_t' to 'Int', possible loss of data
  56. namespace EE{
  57. /******************************************************************************/
  58. struct FileData
  59. {
  60. Mems<Byte> data; // file data
  61. Bool load(C Str &name) // load, false on fail
  62. {
  63. File f; if(f.readTry(name))
  64. {
  65. data.setNum(f.size());
  66. if(f.getFast(data.data(), data.elms()))return true;
  67. }
  68. data.del(); return false;
  69. }
  70. };
  71. /******************************************************************************/
  72. DECLARE_CACHE(FileData, FileDatas, FileDataPtr);
  73. DEFINE_CACHE(FileData, FileDatas, FileDataPtr, "Sound");
  74. /******************************************************************************/
  75. CChar8* CodecName(SOUND_CODEC codec)
  76. {
  77. switch(codec)
  78. {
  79. default : return null;
  80. case SOUND_WAV : return "WAV";
  81. case SOUND_FLAC : return "Flac";
  82. case SOUND_MP3 : return "MP3";
  83. case SOUND_AAC : return "AAC";
  84. case SOUND_DYNAMIC: return "Dynamic";
  85. case SOUND_SND_VORBIS :
  86. case SOUND_OGG_VORBIS :
  87. case SOUND_WEBM_VORBIS: return "Vorbis";
  88. case SOUND_SND_OPUS :
  89. case SOUND_OGG_OPUS :
  90. case SOUND_WEBM_OPUS: return "Opus";
  91. }
  92. }
  93. /******************************************************************************/
  94. static inline Short SampleFltToShort(Flt x) {return Mid(Trunc(x*32768.0f), -32768, 32767);} // this code matches "Vorbis File" library
  95. /******************************************************************************/
  96. // WAV
  97. /******************************************************************************/
  98. Bool SaveWavHeader(File &f, Int bits, Int channels, Int frequency, UInt size)
  99. {
  100. Int bytes=bits/8, block=channels*bytes;
  101. f.putUInt (CC4('R','I','F','F'));
  102. f.putUInt (size+36);
  103. f.putUInt (CC4('W','A','V','E'));
  104. f.putUInt (CC4('f','m','t',' '));
  105. f.putInt (0x10);
  106. f.putUShort(0x01);
  107. f.putUShort(channels);
  108. f.putInt (frequency);
  109. f.putInt (block*frequency);
  110. f.putUShort(block);
  111. f.putUShort(bits);
  112. f.putUInt (CC4('d','a','t','a'));
  113. f.putUInt (size);
  114. return f.ok();
  115. }
  116. static SOUND_CODEC LoadWavHeader(File &f, SoundStream::Params &params)
  117. {
  118. SOUND_CODEC codec=SOUND_NONE;
  119. if(f.getUInt()==CC4('R','I','F','F'))
  120. {
  121. f.getInt();
  122. if(f.getUInt()==CC4('W','A','V','E'))
  123. for(; !f.end(); )
  124. {
  125. UInt data=f.getUInt();
  126. UInt size=f.getUInt();
  127. if(data==CC4('f','m','t',' '))
  128. {
  129. switch(f.getUShort()) // type
  130. {
  131. case 1: codec=SOUND_WAV; break; // PCM
  132. case 85: codec=SOUND_MP3; break;
  133. default: goto error; // unsupported
  134. }
  135. params.channels =f.getUShort(); if(params.channels!=1 && params.channels!=2)goto error;
  136. params.frequency=f.getInt (); f.skip(6);
  137. U16 bits =f.getUShort();
  138. switch(codec)
  139. {
  140. case SOUND_WAV: if(bits!=8 && bits!=16 && bits!=24 && bits!=32)goto error; break;
  141. case SOUND_MP3: break; // allow all bits because actual data will be loaded in 'LoadMP3Header'
  142. }
  143. params.bytes=bits/8;
  144. size-=16; // 16 bytes were read
  145. }else
  146. if(data==CC4('d','a','t','a'))
  147. {
  148. params.size =size;
  149. params.block =params.channels*params.bytes;
  150. params.bit_rate=params.channels*params.bits()*params.frequency;
  151. if(codec)return codec; goto error;
  152. }
  153. f.skip(size);
  154. }
  155. }
  156. error:
  157. params.zero(); return SOUND_NONE;
  158. }
  159. /******************************************************************************/
  160. // SND
  161. /******************************************************************************/
  162. Bool SaveSndHeader(File &f, SND_CODEC codec, Int channels, Int frequency, Long samples)
  163. {
  164. // !! don't modify 'samples' if negative, because it may mean unknown number of samples, 'SndRawEncoder' can use that !!
  165. f.putMulti(UInt(CC4_SND), Byte(0), codec, Byte(channels), Int(frequency)); // CC4, version, codec, channels, frequency
  166. f.cmpULongV(samples); // !! samples must be saved as last, because 'SndRawEncoder' will offset by 'MaxCmpULongVSize' !!
  167. return f.ok();
  168. }
  169. Bool LoadSndHeader(File &f, SoundStream::Params &params, SND_CODEC &codec)
  170. {
  171. if(f.getUInt()==CC4_SND)switch(f.decUIntV())
  172. {
  173. case 0:
  174. {
  175. f>>codec;
  176. params.channels =f.getByte();
  177. params.frequency=f.getInt ();
  178. Long samples=f.decULongV();
  179. params.bytes =2; // all codecs currently support only 16-bit
  180. params.block =params.bytes*params.channels;
  181. params.size =params.block*samples;
  182. params.bit_rate =(samples ? f.size()*8*params.frequency/samples : 0);
  183. }return f.ok();
  184. }
  185. codec=SND_RAW_16;
  186. params.zero();
  187. return false;
  188. }
  189. /******************************************************************************/
  190. // WEBM
  191. /******************************************************************************/
  192. #if SUPPORT_VORBIS || SUPPORT_OPUS
  193. struct MkvReader : mkvparser::IMkvReader
  194. {
  195. File *file;
  196. MkvReader()=delete;
  197. MkvReader(File &file) : file(&file) {}
  198. virtual int Read(long long pos, long len, unsigned char *buf)override // 0=OK, -1=error
  199. {
  200. if(len==0)return 0;
  201. if(!file->pos(pos))return -1;
  202. return file->getFast(buf, len) ? 0 : -1;
  203. }
  204. virtual int Length(long long* total, long long *available)override // 0=OK, -1=error
  205. {
  206. if(total )*total =file->size();
  207. if(available)*available=file->size();
  208. return 0;
  209. }
  210. };
  211. struct WebmFile : MkvReader
  212. {
  213. #define USE_TIME_RAW 0
  214. mkvparser::Segment *segment;
  215. C mkvparser::Cluster *cluster;
  216. C mkvparser::BlockEntry *block_entry;
  217. C mkvparser::Block *block;
  218. Byte extra_frames;
  219. UShort delay;
  220. Int buffer_pos, frame_index, track_number, buf_block, frequency;
  221. Memc<Byte> buffer;
  222. #if USE_TIME_RAW
  223. instead of time<->raw it would be better if this would operate directly on frame_size, frame_pos, decoded_frame_size..
  224. struct TimeRaw
  225. {
  226. Long time, raw;
  227. void set(Long time, Long raw) {T.time=time; T.raw=raw;}
  228. static Int Compare(C TimeRaw &tr, C Long &raw) {return ::Compare(tr.raw, raw);}
  229. };
  230. Mems<TimeRaw> time_raw;
  231. virtual Bool decodeFrameSize(Ptr temp, Int temp_size, Int &size)=NULL;
  232. Bool setTimeRaw()
  233. {
  234. Memc<TimeRaw> tr;
  235. Long size=0;
  236. C mkvparser::BlockEntry *block_entry;
  237. if(C mkvparser::Tracks *tracks=segment->GetTracks())
  238. if(C mkvparser::Track *track =tracks->GetTrackByNumber(track_number))
  239. if(!track->GetFirst(block_entry))
  240. if(C mkvparser::Cluster *cluster=block_entry->GetCluster())
  241. if(C mkvparser::Block *block=block_entry->GetBlock())
  242. {
  243. C mkvparser::Cluster *last=null;
  244. again:
  245. if(last!=cluster) // need to record only at the start of the cluster (and not on every block inside it, because Vorbis will fail to loop, it must be decoded from the start of the cluster)
  246. {
  247. last=cluster;
  248. tr.New().set(block->GetTime(cluster), size);
  249. }
  250. FREP(block->GetFrameCount())
  251. {
  252. C mkvparser::Block::Frame &frame=block->GetFrame(i);
  253. Byte temp[65536];
  254. Int frame_size;
  255. if( frame.len>SIZE(temp)
  256. || !file->pos(frame.pos)
  257. || !file->getFast(temp, frame.len)
  258. || !decodeFrameSize(temp, frame.len, frame_size))goto error;
  259. size+=frame_size;
  260. }
  261. if(advance(cluster, block_entry, block))goto again;
  262. }
  263. //Flt time=size/Dbl(frequency*buf_block); // this may be bigger than SoundStream.length() because here the last frame size is reported in full
  264. time_raw=tr;
  265. return true;
  266. error:
  267. clearPtr();
  268. return false;
  269. }
  270. #endif
  271. void clearBuffer() {buffer_pos=0; buffer.clear();}
  272. void clearPtr()
  273. {
  274. cluster =null;
  275. block_entry=null;
  276. block =null;
  277. }
  278. inline Bool advance(C mkvparser::Cluster* &cluster, C mkvparser::BlockEntry* &block_entry, C mkvparser::Block* &block)
  279. {
  280. advance:
  281. cluster->GetNext(block_entry, block_entry);
  282. if(!block_entry || block_entry->EOS())
  283. {
  284. cluster=segment->GetNext(cluster);
  285. if(!cluster || cluster->EOS())return false;
  286. cluster->GetFirst(block_entry);
  287. if(!block_entry)return false;
  288. }
  289. block=block_entry->GetBlock();
  290. if(!block)return false;
  291. if( block->GetTrackNumber()!=track_number)goto advance;
  292. return true;
  293. }
  294. Bool advance()
  295. {
  296. if(advance(cluster, block_entry, block))return true;
  297. clearPtr(); return false;
  298. }
  299. virtual Bool pos(Long pos)
  300. {
  301. if(pos<0)return false;
  302. pos+=delay;
  303. clearBuffer();
  304. if(segment)
  305. {
  306. Long time;
  307. #if USE_TIME_RAW
  308. C TimeRaw *tr;
  309. if(time_raw.elms()) // use time raw map
  310. {
  311. Int index; if(!time_raw.binarySearch(pos, index, TimeRaw::Compare))index--; // if didn't found an exact match, then we need to use the previous one
  312. tr=&time_raw[index];
  313. time=tr->time;
  314. }else
  315. #endif
  316. time=pos*NANOS/(buf_block*frequency);
  317. if(C mkvparser::Tracks *tracks=segment->GetTracks())
  318. if(C mkvparser::Track *track =tracks->GetTrackByNumber(track_number))
  319. #if 1 // this is more precise
  320. if(cluster=segment->FindCluster(time))
  321. if(block_entry=cluster->GetEntry(track, time))
  322. if(block=block_entry->GetBlock())
  323. if(block->GetTrackNumber()==track_number || advance())
  324. #else
  325. if(!track->Seek(time, block_entry))
  326. if(cluster=block_entry->GetCluster())
  327. if(block=block_entry->GetBlock())
  328. #endif
  329. {
  330. Long block_time=block->GetTime(cluster);
  331. if(block_time>time)goto error;
  332. frame_index=0;
  333. REP(extra_frames)
  334. {
  335. block_time--; // we need previous frame, so just look for a frame using time smaller by 1 from the next frame
  336. if(cluster=segment->FindCluster(block_time))
  337. if(block_entry=cluster->GetEntry(track, block_time))
  338. if(block=block_entry->GetBlock())
  339. if(block->GetTrackNumber()==track_number || advance())
  340. goto found_prev_block;
  341. goto error;
  342. found_prev_block:
  343. block_time=block->GetTime(cluster);
  344. }
  345. #if USE_TIME_RAW
  346. if(time_raw.elms())buffer_pos=pos-tr->raw;else this is wrong because we may have used previous block
  347. #endif
  348. buffer_pos=pos-(block_time*frequency/NANOS)*buf_block; // mul by block last to make sure we've got alignment
  349. return true;
  350. }
  351. }
  352. error:
  353. clearPtr();
  354. return false;
  355. }
  356. virtual Bool decode(Ptr &data, Int &size, Ptr temp, Int temp_size, Int &read)=NULL;
  357. Int set(Ptr data, Int size)
  358. {
  359. if(size>0)
  360. {
  361. Int read=0;
  362. again:
  363. if(!buffer.elms() && block)
  364. {
  365. if(frame_index>=block->GetFrameCount())
  366. {
  367. if(!advance())goto error;
  368. frame_index=0;
  369. }
  370. #if 0 // log
  371. if(frame_index==0)
  372. {
  373. Long time_ns=block->GetTime(cluster);
  374. Flt time=time_ns/Dbl(NANOS);
  375. Flt time2=ppp/Dbl(frequency*buf_block);
  376. LogN(S+time+' '+time2+' '+(time2-time));
  377. }
  378. #endif
  379. C mkvparser::Block::Frame &frame=block->GetFrame(frame_index++);
  380. Byte temp[65536];
  381. if( frame.len>SIZE(temp)
  382. || !file->pos(frame.pos)
  383. || !file->getFast(temp, frame.len)
  384. || !decode(data, size, temp, frame.len, read))goto error;
  385. if(!read && !buffer.elms())goto again; // if 'decode' succeeded, but nothing was 'read' or placed to the 'buffer' then try again (this can happen for Vorbis)
  386. }
  387. Int buffer_left=Min(buffer.elms()-buffer_pos, size);
  388. if( buffer_left>0) // if we have any left in the buffer
  389. {
  390. CopyFast(data, buffer.data()+buffer_pos, buffer_left);
  391. buffer_pos+=buffer_left; if(buffer_pos>=buffer.elms())clearBuffer(); // if we've read the entire buffer then clear it
  392. data =(Byte*)data+buffer_left;
  393. read +=buffer_left;
  394. size -=buffer_left;
  395. }else
  396. if(buffer.elms() && buffer_pos>=buffer.elms()) // this can happen if we had some data in the buffer, but we had to skip everything
  397. {
  398. buffer_pos-=buffer.elms(); // we've skipped this
  399. buffer.clear();
  400. goto again; // try reading again instead of returning 0 which implies an error
  401. }
  402. return read;
  403. }
  404. error:
  405. return 0;
  406. }
  407. virtual ~WebmFile() {Delete(segment);}
  408. WebmFile()=delete;
  409. WebmFile(C SoundStream::Params &params, File &file, Int track_number, mkvparser::Segment* &segment, Int extra_frames) : MkvReader(file)
  410. {
  411. T.extra_frames=extra_frames;
  412. T.delay=0;
  413. T.buffer_pos=0;
  414. T.frame_index=0;
  415. T.buf_block=params.block;
  416. T.frequency=params.frequency;
  417. T.track_number=track_number;
  418. T.cluster=null;
  419. T.block_entry=null;
  420. T.block=null;
  421. if(T.segment=segment)
  422. {
  423. ConstCast(T.segment->m_pReader)=this;
  424. if(T.cluster=T.segment->GetFirst())
  425. {
  426. T.cluster->GetFirst(T.block_entry);
  427. if(T.block_entry)if(T.block=T.block_entry->GetBlock())if(T.block->GetTrackNumber()!=track_number)advance();
  428. }
  429. segment=null; // set this to null because we've taken ownership
  430. }
  431. }
  432. };
  433. #endif
  434. /******************************************************************************/
  435. // VORBIS
  436. /******************************************************************************/
  437. #if SUPPORT_VORBIS
  438. static Int BitRate(C vorbis_info &vi)
  439. {
  440. return (vi.bitrate_nominal>0) ? vi.bitrate_nominal : (vi.bitrate_lower>0 && vi.bitrate_upper>0) ? AvgI(UInt(vi.bitrate_lower), UInt(vi.bitrate_upper)) : 0;
  441. }
  442. static ov_callbacks VorbisCallbacks;
  443. static size_t VorbisRead (void *data, size_t size, size_t nmemb, void *file) {return ((File*)file)->getReturnSize(data, nmemb*size);}
  444. static long VorbisTell (void *file ) {return ((File*)file)->pos ( );}
  445. static Int VorbisClose(void *file ) { /*((File*)file)->del ( );*/ return 0;} // don't do anything in case it wants to close on load fail, but we still need the file to try other codecs, also we delete the file manually, so this is not needed
  446. static Int VorbisSeek (void *file, ogg_int64_t offset, Int whence ) // should return -1 if unseekable (fail) and 0 otherwise
  447. {
  448. File &f=*(File*)file;
  449. switch(whence)
  450. {
  451. case SEEK_CUR: offset+=f.pos (); break;
  452. case SEEK_END: offset+=f.size(); break;
  453. }
  454. return f.pos(offset)-1;
  455. }
  456. static OggVorbis_File* LoadOggVorbisHeader(File &f, SoundStream::Params &params)
  457. {
  458. OggVorbis_File *vorbis=Alloc<OggVorbis_File>();
  459. if(OK(ov_open_callbacks(&f, vorbis, null, 0, VorbisCallbacks)))
  460. {
  461. vorbis_info *vi=ov_info(vorbis, -1);
  462. if(vi)
  463. {
  464. params.frequency=vi->rate;
  465. params.channels =vi->channels;
  466. params.bit_rate =BitRate(*vi);
  467. params.bytes =2;
  468. params.block =params.channels*params.bytes;
  469. params.size =params.block *ov_pcm_total(vorbis, -1);
  470. return vorbis;
  471. }
  472. ov_clear(vorbis);
  473. }
  474. params.zero(); Free(vorbis); return null;
  475. }
  476. STRUCT(WebmVorbisFile , WebmFile)
  477. //{
  478. vorbis_info vi;
  479. vorbis_dsp_state vd;
  480. vorbis_block vb;
  481. static ULong xiph_lace_value(C Byte **np)
  482. {
  483. C Byte *p=*np;
  484. ULong lace=*p++, value=lace; while(lace==255){lace=*p++; value+=lace;}
  485. *np=p; return value;
  486. }
  487. static C Byte* GetPrivateDataPart(C Byte *private_data, size_t private_size, int part, size_t &part_size)
  488. {
  489. part_size=0;
  490. C Byte *result=null, *p=private_data;
  491. Int count =*p++ + 1;
  492. if(count==3 && *p>=part)
  493. {
  494. ULong sizes[3], total=0;
  495. Int i=0;
  496. while(--count)
  497. {
  498. sizes[i]=xiph_lace_value(&p);
  499. total +=sizes[i];
  500. i++;
  501. }
  502. sizes[i]=private_size-total-(p-private_data);
  503. for(i=0; i<part; ++i)p+=sizes[i];
  504. result =p;
  505. part_size=sizes[part];
  506. }
  507. return result;
  508. }
  509. ~WebmVorbisFile()
  510. {
  511. vorbis_block_clear(&vb);
  512. vorbis_dsp_clear (&vd);
  513. vorbis_info_clear (&vi);
  514. }
  515. WebmVorbisFile(SoundStream::Params &params, File &file, Int track_number, mkvparser::Segment* &segment, C Byte *priv_data, Int priv_data_size) : WebmFile(params, file, track_number, segment, 1)
  516. {
  517. Zero(vi);
  518. Zero(vd);
  519. Zero(vb);
  520. if(priv_data && priv_data_size && priv_data[0]==2)
  521. {
  522. vorbis_comment vc; Zero(vc);
  523. vorbis_info_init (&vi);
  524. vorbis_comment_init(&vc);
  525. Int ogg_packet_num=0, error=0;
  526. for(Int h=0; h<3 && !error; h++)
  527. {
  528. size_t length=0;
  529. if(C Byte *data =GetPrivateDataPart(priv_data, priv_data_size, h, length))
  530. {
  531. ogg_packet packet;
  532. packet.packet =ConstCast(data);
  533. packet.bytes =length;
  534. packet.b_o_s =(h==0);
  535. packet.e_o_s =false;
  536. packet.granulepos=0;
  537. packet.packetno =ogg_packet_num++;
  538. error=vorbis_synthesis_headerin(&vi, &vc, &packet);
  539. }
  540. }
  541. if(!error
  542. && !vorbis_synthesis_init(&vd, &vi)
  543. && !vorbis_block_init (&vd, &vb))
  544. {
  545. Int bit_rate=BitRate(vi); if(bit_rate>0)params.bit_rate=bit_rate;
  546. }
  547. vorbis_comment_clear(&vc);
  548. }
  549. }
  550. virtual Bool pos(Long pos)override
  551. {
  552. #if USE_TIME_RAW
  553. //if(!time_raw.elms())setTimeRaw();
  554. #endif
  555. if(super::pos(pos))
  556. {
  557. vorbis_synthesis_restart(&vd);
  558. return true;
  559. }
  560. return false;
  561. }
  562. #if USE_TIME_RAW
  563. virtual Bool decodeFrameSize(Ptr temp, Int temp_size, Int &size)override
  564. {
  565. ogg_packet packet;
  566. packet.packet =(Byte*)temp;
  567. packet.bytes =temp_size;
  568. packet.b_o_s =false;
  569. packet.e_o_s =false;
  570. packet.granulepos=-1;
  571. packet.packetno =-1;
  572. size=vorbis_packet_blocksize(&vi, &packet); if(size<0)return false;
  573. size*=buf_block/vi.channels; // this is OK because 'buf_block' will always be a multiple of channels
  574. return true;
  575. }
  576. #endif
  577. virtual Bool decode(Ptr &data, Int &size, Ptr temp, Int temp_size, Int &read)override
  578. {
  579. ogg_packet packet;
  580. packet.packet =(Byte*)temp;
  581. packet.bytes =temp_size;
  582. packet.b_o_s =false;
  583. packet.e_o_s =false;
  584. packet.granulepos=-1;
  585. packet.packetno =-1;
  586. if(!vorbis_synthesis (&vb, &packet)
  587. && !vorbis_synthesis_blockin(&vd, &vb))
  588. {
  589. for(;;)
  590. {
  591. Flt **pcm=null;
  592. Int decoded_samples=vorbis_synthesis_pcmout(&vd, &pcm); if(decoded_samples<=0)break;
  593. Int pcm_samples=decoded_samples;
  594. if(buffer_pos) // we need to skip some samples
  595. {
  596. Int skip_samples=Min(buffer_pos/buf_block, pcm_samples);
  597. REP(vi.channels)pcm[i]+=skip_samples;
  598. buffer_pos -=skip_samples*buf_block;
  599. pcm_samples-=skip_samples;
  600. }
  601. if(size>0) // write directly to 'data'
  602. {
  603. Int data_samples=size/buf_block,
  604. write_samples=Min(data_samples, pcm_samples);
  605. FREPD(s, write_samples)
  606. FREPD(c, vi.channels)*((I16*&)data)++=SampleFltToShort(pcm[c][s]);
  607. Int r=write_samples*buf_block;
  608. size-=r;
  609. read+=r;
  610. pcm_samples-=write_samples;
  611. REP(vi.channels)pcm[i]+=write_samples;
  612. }
  613. if(pcm_samples) // write leftovers to buffer
  614. {
  615. Int start=buffer.addNum(pcm_samples*buf_block);
  616. I16 *buf =(I16*)&buffer[start];
  617. FREPD(s, pcm_samples)
  618. FREPD(c, vi.channels)*buf++=SampleFltToShort(pcm[c][s]);
  619. }
  620. if(vorbis_synthesis_read(&vd, decoded_samples))return false;
  621. }
  622. return true;
  623. }
  624. return false;
  625. }
  626. };
  627. // decoder could be written based on 'WebmVorbisFile' and 'SndVorbisEncoder'
  628. struct SndVorbisFile
  629. {
  630. File *file;
  631. Bool pos(Long pos)
  632. {
  633. return false;
  634. }
  635. Int set(Ptr data, Int size)
  636. {
  637. return 0;
  638. }
  639. Bool init(SoundStream &stream)
  640. {
  641. T.file=&stream._f;
  642. return false;
  643. }
  644. };
  645. #endif
  646. /******************************************************************************/
  647. // OPUS
  648. /******************************************************************************/
  649. #if SUPPORT_OPUS
  650. static OpusFileCallbacks OpusCallbacks;
  651. static int OpusRead (void *file, unsigned char *data, int size) {return ((File*)file)->getReturnSize(data, size);}
  652. static opus_int64 OpusTell (void *file ) {return ((File*)file)->pos ( );}
  653. static int OpusClose(void *file ) { /*((File*)file)->del ( );*/ return 0;} // don't do anything in case it wants to close on load fail, but we still need the file to try other codecs, also we delete the file manually, so this is not needed
  654. static int OpusSeek (void *file, opus_int64 offset, int whence)
  655. {
  656. File &f=*(File*)file;
  657. switch(whence)
  658. {
  659. case SEEK_CUR: offset+=f.pos (); break;
  660. case SEEK_END: offset+=f.size(); break;
  661. }
  662. return f.pos(offset)-1;
  663. }
  664. static OggOpusFile* LoadOggOpusHeader(File &f, SoundStream::Params &params)
  665. {
  666. if(OggOpusFile *opus=op_open_callbacks(&f, &OpusCallbacks, null, 0, null))
  667. {
  668. ogg_int64_t samples=op_pcm_total(opus, -1);
  669. params.frequency=OPUS_FREQUENCY;
  670. params.bytes =OPUS_BYTES;
  671. params.channels =Min(op_channel_count(opus, 0), 2); // support only mono/stereo, it's okay to limit here, because 'op_read_stereo' will handle down-mixing
  672. params.block =params.channels*params.bytes;
  673. params.size =params.block *samples;
  674. params.bit_rate =(samples ? f.size()*8*params.frequency/samples : 0);
  675. return opus;
  676. //op_free(opus);
  677. }
  678. params.zero(); return null;
  679. }
  680. STRUCT(WebmOpusFile , WebmFile)
  681. //{
  682. OpusDecoder od;
  683. #if USE_TIME_RAW
  684. virtual Bool decodeFrameSize(Ptr temp, Int temp_size, Int &size)override
  685. {
  686. size=opus_packet_get_nb_samples((Byte*)temp, temp_size, od.frequency()); if(size<0)return false;
  687. size*=buf_block;
  688. return true;
  689. }
  690. #endif
  691. virtual Bool pos(Long pos)override
  692. {
  693. if(super::pos(pos))
  694. {
  695. od.reset();
  696. return true;
  697. }
  698. return false;
  699. }
  700. virtual Bool decode(Ptr &data, Int &size, Ptr temp, Int temp_size, Int &read)override
  701. {
  702. if(buffer_pos==0 && size>=OpusMaxFrameSize(od.channels())) // decode directly into 'data', we can do this only if we're not skipping (buffer_pos==0) and if we know that 'data' can fit an entire frame
  703. {
  704. Int decoded=od.decode(temp, temp_size, data, size);
  705. if( decoded<=0)return false;
  706. data =(Byte*)data+decoded;
  707. read+=decoded;
  708. size-=decoded;
  709. }else
  710. if(!od.decode(temp, temp_size, buffer))return false;
  711. return true;
  712. }
  713. WebmOpusFile(SoundStream::Params &params, File &file, Int track_number, mkvparser::Segment* &segment, C Byte *priv_data, Int priv_data_size) : WebmFile(params, file, track_number, segment, OPUS_FRAME_HISTORY)
  714. {
  715. if(priv_data_size>=12 && EqualMem(priv_data, "OpusHead", 8))
  716. {
  717. buffer_pos=delay=(priv_data[10]|(priv_data[11]<<8))*params.block;
  718. MAX(params.size-=delay, 0);
  719. }
  720. od.create(params.channels);
  721. }
  722. };
  723. struct SndOpusFile
  724. {
  725. OpusDecoder od;
  726. File *file;
  727. Mems<U16> packet_sizes;
  728. Memc<Byte> buffer;
  729. Byte extra_frames;
  730. U16 delay;
  731. Int packet_index, buffer_pos;
  732. UInt frame_size, audio_offset;
  733. Bool pos(Long raw)
  734. {
  735. if(raw<0)return false;
  736. raw+=delay;
  737. od.reset(); // need to reset state
  738. packet_index=Unsigned(raw)/frame_size;
  739. MAX(packet_index-=extra_frames, 0);
  740. buffer_pos=raw-packet_index*frame_size; buffer.clear();
  741. Long pos=audio_offset; for(Int i=0, end=Min(packet_sizes.elms(), packet_index); i<end; i++)pos+=packet_sizes[i];
  742. return file->pos(pos);
  743. }
  744. Int set(Ptr data, Int size) // !! Warning: 'size' should be clamped to skip the zero padding at the end !! this is done in 'SoundStream.set' because 'SndOpusFile' doesn't store current position and total size (to reduce memory usage)
  745. {
  746. Int read=0;
  747. for(; size>0; )
  748. {
  749. Int left=buffer.elms()-buffer_pos;
  750. if( left>0) // if something left in buffer
  751. {
  752. Int copy=Min(size, left);
  753. CopyFast(data, &buffer[buffer_pos], copy);
  754. size -=copy;
  755. buffer_pos+=copy;
  756. read +=copy;
  757. data =(Byte*)data+copy;
  758. if(buffer_pos>=buffer.elms()){buffer_pos=0; buffer.clear();}
  759. }else // need to decode
  760. {
  761. if(!InRange(packet_index, packet_sizes))break;
  762. Byte packet_data[65536];
  763. Int packet_size=packet_sizes[packet_index++];
  764. if( packet_size>SIZE(packet_data) || !file->getFast(packet_data, packet_size))break;
  765. if(buffer_pos==0 && size>=frame_size) // decode directly into 'data', we can do this only if we're not skipping (buffer_pos==0) and if we know that 'data' can fit an entire frame
  766. {
  767. Int decoded=od.decode(packet_data, packet_size, data, size);
  768. if( decoded<=0)break;
  769. read+=decoded;
  770. size-=decoded;
  771. data =(Byte*)data+decoded;
  772. }else
  773. {
  774. if(!od.decode(packet_data, packet_size, buffer))break;
  775. if(buffer_pos>=buffer.elms()) // if we have to skip everything
  776. {
  777. buffer_pos-=buffer.elms(); // we've skipped this
  778. buffer.clear();
  779. }
  780. }
  781. }
  782. }
  783. return read;
  784. }
  785. Bool init(SoundStream &stream)
  786. {
  787. if(od.create(stream.channels(), stream.frequency()))
  788. {
  789. delay=stream._f.decUIntV();
  790. const UInt frame_samples=OPUS_FRAME_SIZE_BEST_QUALITY*48; // 'SndOpusEncoder' always uses OPUS_FRAME_SIZE_BEST_QUALITY
  791. packet_sizes.setNum(DivCeil(ULong(stream.samples()+delay), (ULong)frame_samples));
  792. if(stream._f.getN(packet_sizes.data(), packet_sizes.elms()))
  793. {
  794. T.extra_frames= OPUS_FRAME_HISTORY;
  795. T.delay *= stream.block(); // convert from sample to raw
  796. T.frame_size = stream.block()*frame_samples;
  797. T.packet_index= 0;
  798. T.buffer_pos = delay;
  799. T.file =&stream._f;
  800. T.audio_offset= stream._f.pos();
  801. return true;
  802. }
  803. }
  804. return false;
  805. }
  806. };
  807. #endif
  808. /******************************************************************************/
  809. // WEBM VORBIS+OPUS
  810. /******************************************************************************/
  811. #if SUPPORT_VORBIS || SUPPORT_OPUS
  812. static WebmFile* LoadWebmHeader(File &f, SoundStream::Params &params, SOUND_CODEC &codec)
  813. {
  814. codec=SOUND_NONE;
  815. MkvReader reader(f);
  816. long long pos=0;
  817. mkvparser::EBMLHeader ebmlHeader; if(ebmlHeader.Parse(&reader, pos)>=0)
  818. {
  819. mkvparser::Segment *segment=null;
  820. mkvparser::Segment::CreateInstance(&reader, pos, segment);
  821. if(segment)
  822. {
  823. if(segment->Load()>=0)
  824. if(C mkvparser::SegmentInfo *segment_info=segment->GetInfo())
  825. if(C mkvparser::Tracks *tracks=segment->GetTracks())
  826. {
  827. Int tracks_num=tracks->GetTracksCount();
  828. FREP(tracks_num) // process in order
  829. if(C mkvparser::Track *track=tracks->GetTrackByIndex(i))
  830. if(track->GetType()==mkvparser::Track::kAudio)
  831. {
  832. #if SUPPORT_VORBIS
  833. if(Equal(track->GetCodecId(), "A_VORBIS"))codec=SOUND_WEBM_VORBIS;
  834. #endif
  835. #if SUPPORT_OPUS
  836. if(Equal(track->GetCodecId(), "A_OPUS" ))codec=SOUND_WEBM_OPUS;
  837. #endif
  838. if(codec) // stop on first found
  839. {
  840. C mkvparser::AudioTrack &audio_track=SCAST(C mkvparser::AudioTrack, *track);
  841. Int frequency=RoundPos(audio_track.GetSamplingRate());
  842. if(audio_track.GetChannels()>=1 && audio_track.GetChannels()<=2 // support only mono/stereo
  843. && frequency>=1) // to avoid div by zero
  844. {
  845. Long duration_ns=segment_info->GetDuration();
  846. params.channels =audio_track.GetChannels();
  847. params.frequency=frequency;
  848. params.bytes =2;
  849. params.block =params.channels*params.bytes;
  850. params.size =(duration_ns*params.frequency/NANOS)*params.block; // mul by 'params.block' last to make sure it's a multiple
  851. params.bit_rate =(params.size ? f.size()*8*params.block*params.frequency/params.size : 0);
  852. size_t priv_data_size; C Byte *priv_data=audio_track.GetCodecPrivate(priv_data_size);
  853. #if SUPPORT_VORBIS
  854. if(codec==SOUND_WEBM_VORBIS)return new WebmVorbisFile(params, f, audio_track.GetNumber(), segment, priv_data, priv_data_size);
  855. #endif
  856. #if SUPPORT_OPUS
  857. if(codec==SOUND_WEBM_OPUS )return new WebmOpusFile(params, f, audio_track.GetNumber(), segment, priv_data, priv_data_size);
  858. #endif
  859. }
  860. }
  861. }
  862. }
  863. Delete(segment);
  864. }
  865. }
  866. params.zero(); return null;
  867. }
  868. #endif
  869. /******************************************************************************/
  870. // MP3
  871. /******************************************************************************/
  872. #if SUPPORT_MP3
  873. struct MP3
  874. {
  875. mp3dec_t dec;
  876. File *file;
  877. Int buf_pos , // amount of bytes to skip
  878. buf_left, // amount of bytes still in the buffer
  879. block ,
  880. frame_index,
  881. frame_samples;
  882. Byte buf[MP3_FRAME_SAMPLES*SIZE(I16)*2]; // max samples * sample size * 2 channels
  883. Mems<UInt> frame_pos;
  884. Bool pos(Long sample)
  885. {
  886. if(sample>=0)
  887. {
  888. sample+=MP3_SAMPLE_DELAY;
  889. Int frame_index=sample/frame_samples; if(InRange(frame_index, frame_pos))
  890. {
  891. MAX(frame_index-=2, 0); // "I suggest start from-middle decoding 2 frames earlier and discard it's data." according to https://github.com/lieff/minimp3/issues/38
  892. T.frame_index=frame_index;
  893. buf_pos =(sample-frame_index*frame_samples)*block;
  894. buf_left=0;
  895. mp3dec_init(&dec); // reset decoder state
  896. return file->pos(frame_pos[frame_index]);
  897. }
  898. }
  899. return false;
  900. }
  901. Int decode(const uint8_t *mp3, int mp3_bytes, Byte *pcm) // return number of bytes decoded (not samples)
  902. {
  903. mp3dec_frame_info_t frame_info;
  904. Int samples=mp3dec_decode_frame(&dec, mp3, mp3_bytes, (mp3d_sample_t*)pcm, &frame_info);
  905. if( samples==frame_samples)return frame_samples*block; // all OK
  906. switch(samples)
  907. {
  908. default: return 0; // failed
  909. case -1: // reservoir failed
  910. {
  911. if(1 // actually always continue because some MP3's can have reservoir broken at the start
  912. || buf_pos>=frame_samples*block) // here allow to continue ONLY if we intend to skip this entire frame (this is needed because of how the MP3 library works, when seeking we need to go 2 frames before, reservoir is lost which causes failures)
  913. {
  914. Zero(pcm+buf_pos, frame_samples*block-buf_pos); // zero only data that we're going to use (and not skip)
  915. return frame_samples*block;
  916. }
  917. }return 0;
  918. }
  919. }
  920. Int set(Ptr data, Int size)
  921. {
  922. Int read=0;
  923. for(; size>0; )
  924. {
  925. if(buf_left>0) // if something left in buffer
  926. {
  927. Int copy=Min(size, buf_left);
  928. CopyFast(data, buf+buf_pos, copy);
  929. data =(Byte*)data+copy;
  930. size -=copy;
  931. read +=copy;
  932. buf_left-=copy; if(buf_left>0)buf_pos+=copy;else buf_pos=0;
  933. }else // need to decode
  934. {
  935. Int frame_size;
  936. Byte frame_data[MAX_L3_FRAME_PAYLOAD_BYTES];
  937. if(InRange(frame_index+1, frame_pos)) // if there is a next frame
  938. {
  939. frame_size=frame_pos[++frame_index]-file->pos(); // calculate distance between next frame position and current position
  940. }else
  941. if(InRange(frame_index, frame_pos)) // last frame
  942. {
  943. frame_index++; // increase so we won't process again
  944. frame_size=file->left();
  945. }else break;
  946. MIN(frame_size, SIZEI(frame_data));
  947. if(!file->getFast(frame_data, frame_size))break;
  948. if(buf_pos==0 && size>=SIZE(buf)) // decode directly into 'data', we can do this only if we're not skipping (buf_pos==0) and if we know that 'data' can fit an entire frame
  949. {
  950. Int decoded=decode(frame_data, frame_size, (Byte*)data); if(decoded<=0)break;
  951. read+=decoded;
  952. size-=decoded;
  953. data =(Byte*)data+decoded;
  954. }else
  955. {
  956. Int decoded=decode(frame_data, frame_size, buf); if(decoded<=0)break;
  957. buf_left =decoded-buf_pos;
  958. if(buf_left<=0) // if we had to skip everything
  959. {
  960. buf_left=0; // nothing left
  961. buf_pos-=decoded; // we've skipped this
  962. }
  963. }
  964. }
  965. }
  966. return read;
  967. }
  968. MP3(File &f, SoundStream::Params &params, Int frame_samples, Memt<UInt> &frame_pos)
  969. {
  970. T.file=&f;
  971. T.frame_pos=frame_pos;
  972. T.block=params.block;
  973. T.buf_pos=MP3_SAMPLE_DELAY*block;
  974. T.buf_left=T.frame_index=0;
  975. T.frame_samples=frame_samples;
  976. mp3dec_init(&dec);
  977. }
  978. };
  979. static Int mp3dec_skip_id3v2(const uint8_t *buf, UInt buf_size)
  980. {
  981. if(buf_size>10 && !strncmp((char *)buf, "ID3", 3))
  982. return (((buf[6] & 0x7f) << 21) | ((buf[7] & 0x7f) << 14) | ((buf[8] & 0x7f) << 7) | (buf[9] & 0x7f)) + 10;
  983. return 0;
  984. }
  985. static MP3* LoadMP3Header(File &f, SoundStream::Params &params)
  986. {
  987. params.zero();
  988. Byte temp[65536];
  989. Int buf_size=Min(f.left(), SIZEI(temp));
  990. if(!f.getFast(temp, buf_size))return null;
  991. Int id3v2size=mp3dec_skip_id3v2(temp, buf_size); if(id3v2size>buf_size)return null;
  992. Byte *buf =temp+id3v2size;
  993. buf_size-= id3v2size;
  994. mp3dec_t dec; mp3dec_init(&dec);
  995. Int frame_samples;
  996. Memt<UInt> frame_pos;
  997. ULong bit_rate=0;
  998. for(;;)
  999. {
  1000. if(buf_size<MAX_FRAME_SYNC_MATCHES*MAX_L3_FRAME_PAYLOAD_BYTES && !f.end()) // if not enough data left for N frames, but still have some data in the file
  1001. {
  1002. CopyFast(temp, buf, buf_size); // copy leftover data to the start
  1003. buf=temp; // we now have data at the start
  1004. Int read=Min(f.left(), SIZEI(temp)-buf_size); // calculate how much we can read
  1005. if(!f.getFast(buf+buf_size, read))return null; // read extra data
  1006. buf_size+=read;
  1007. }
  1008. mp3dec_frame_info_t frame_info;
  1009. Int samples=mp3dec_decode_frame(&dec, buf, buf_size, null, &frame_info); Int offset_size=frame_info.frame_offset+frame_info.frame_size; if(!offset_size)break;
  1010. if( samples>0)
  1011. {
  1012. if(!frame_pos.elms())
  1013. {
  1014. if(frame_info.channels<1 || frame_info.channels>2)return null; // support only mono and stereo
  1015. if(samples!=MP3_FRAME_SAMPLES && samples!=MP3_FRAME_SAMPLES2)return null; // support only these samples
  1016. params.channels =frame_info.channels;
  1017. params.frequency=frame_info.hz;
  1018. frame_samples =samples;
  1019. }else
  1020. {
  1021. if(params.channels !=frame_info.channels
  1022. || params.frequency!=frame_info.hz
  1023. || frame_samples !=samples)return null;
  1024. }
  1025. frame_pos.add(f.pos()-buf_size+frame_info.frame_offset);
  1026. bit_rate+=frame_info.bitrate_kbps;
  1027. }
  1028. buf +=offset_size;
  1029. buf_size-=offset_size;
  1030. }
  1031. if(frame_pos.elms())
  1032. {
  1033. Long total_samples=Long(frame_pos.elms())*frame_samples-MP3_SAMPLE_DELAY;
  1034. params.bytes =2;
  1035. params.block =params.channels*params.bytes;
  1036. params.size =params.block*total_samples;
  1037. params.bit_rate=(1 ? bit_rate*1000/frame_pos.elms() : total_samples ? f.size()*8*params.frequency/total_samples : 0);
  1038. f.pos(frame_pos[0]); // go to the first frame
  1039. return new MP3(f, params, frame_samples, frame_pos);
  1040. }
  1041. return null;
  1042. }
  1043. #endif
  1044. /******************************************************************************/
  1045. // FLAC
  1046. /******************************************************************************/
  1047. #if SUPPORT_FLAC
  1048. struct FLAC
  1049. {
  1050. FLAC__StreamDecoder *decoder;
  1051. File *file;
  1052. Memc<Byte> buffer;
  1053. Int buffer_pos, read, to_read;
  1054. Long seek;
  1055. Ptr data;
  1056. static FLAC__StreamDecoderReadStatus Read(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], size_t *bytes, void *client_data)
  1057. {
  1058. File &f=*((FLAC*)client_data)->file;
  1059. if(*bytes<=0)return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
  1060. *bytes=f.getReturnSize(buffer, *bytes);
  1061. return (*bytes>0) ? FLAC__STREAM_DECODER_READ_STATUS_CONTINUE : FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM;
  1062. }
  1063. static FLAC__StreamDecoderSeekStatus Seek(const FLAC__StreamDecoder *decoder, FLAC__uint64 absolute_byte_offset, void *client_data)
  1064. {
  1065. File &f=*((FLAC*)client_data)->file;
  1066. return f.pos(absolute_byte_offset) ? FLAC__STREAM_DECODER_SEEK_STATUS_OK : FLAC__STREAM_DECODER_SEEK_STATUS_ERROR;
  1067. }
  1068. static FLAC__StreamDecoderTellStatus Tell(const FLAC__StreamDecoder *decoder, FLAC__uint64 *absolute_byte_offset, void *client_data)
  1069. {
  1070. File &f=*((FLAC*)client_data)->file;
  1071. *absolute_byte_offset=f.pos();
  1072. return FLAC__STREAM_DECODER_TELL_STATUS_OK;
  1073. }
  1074. static FLAC__StreamDecoderLengthStatus Length(const FLAC__StreamDecoder *decoder, FLAC__uint64 *stream_length, void *client_data)
  1075. {
  1076. File &f=*((FLAC*)client_data)->file;
  1077. *stream_length=f.size();
  1078. return FLAC__STREAM_DECODER_LENGTH_STATUS_OK;
  1079. }
  1080. static FLAC__bool Eof(const FLAC__StreamDecoder *decoder, void *client_data)
  1081. {
  1082. File &f=*((FLAC*)client_data)->file;
  1083. return f.end();
  1084. }
  1085. static void Meta(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data)
  1086. {
  1087. if(metadata && metadata->type==FLAC__METADATA_TYPE_STREAMINFO)
  1088. {
  1089. FLAC &flac=*(FLAC*)client_data; if(flac.to_read<0) // only if we're expecting metadata
  1090. {
  1091. C FLAC__StreamMetadata_StreamInfo &stream=metadata->data.stream_info;
  1092. File &f=*flac.file;
  1093. SoundStream::Params &params=*(SoundStream::Params*)flac.data;
  1094. params.frequency=stream.sample_rate;
  1095. params.channels =stream.channels;
  1096. params.bytes =stream.bits_per_sample/8;
  1097. params.block =params.channels*params.bytes;
  1098. params.size =params.block *stream.total_samples;
  1099. params.bit_rate =(stream.total_samples ? f.size()*8*params.frequency/stream.total_samples : 0);
  1100. }
  1101. }
  1102. }
  1103. static FLAC__StreamDecoderWriteStatus Write(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 *const buffer[], void *client_data)
  1104. {
  1105. FLAC &flac=*(FLAC*)client_data;
  1106. if(flac.to_read>0) // process this only if we want to read something, this is to avoid cases when this callback is called during some other flac call like seeking (yes this can happen)
  1107. {
  1108. Int frame_samples=frame->header.blocksize,
  1109. channels =frame->header.channels,
  1110. bits =frame->header.bits_per_sample,
  1111. bytes =bits/8,
  1112. block =bytes*channels,
  1113. to_read_samples=flac.to_read/block,
  1114. read_samples =0,
  1115. n=Min(to_read_samples, frame_samples);
  1116. switch(bits)
  1117. {
  1118. default: flac.to_read=0; break;
  1119. case 8:
  1120. {
  1121. U8 *data=(U8*)flac.data;
  1122. for(; read_samples<n; read_samples++)FREPD(c, channels)*data++=buffer[c][read_samples]+128; // FLAC data will be signed, however we need unsigned
  1123. Int read=read_samples*block; // this needs to be set before processing left-over data to the 'buffer'
  1124. flac. read+=read;
  1125. flac.to_read-=read;
  1126. flac. data =data;
  1127. if(Int left_samples=frame_samples-read_samples)
  1128. {
  1129. data=(U8*)flac.buffer.setNum(left_samples*block).data();
  1130. for(; read_samples<frame_samples; read_samples++)FREPD(c, channels)*data++=buffer[c][read_samples]+128; // FLAC data will be signed, however we need unsigned
  1131. }
  1132. }break;
  1133. case 16:
  1134. {
  1135. I16 *data=(I16*)flac.data;
  1136. for(; read_samples<n; read_samples++)FREPD(c, channels)*data++=buffer[c][read_samples];
  1137. Int read=read_samples*block; // this needs to be set before processing left-over data to the 'buffer'
  1138. flac. read+=read;
  1139. flac.to_read-=read;
  1140. flac. data =data;
  1141. if(Int left_samples=frame_samples-read_samples)
  1142. {
  1143. data=(I16*)flac.buffer.setNum(left_samples*block).data();
  1144. for(; read_samples<frame_samples; read_samples++)FREPD(c, channels)*data++=buffer[c][read_samples];
  1145. }
  1146. }break;
  1147. case 24:
  1148. {
  1149. Int24 *data=(Int24*)flac.data;
  1150. for(; read_samples<n; read_samples++)FREPD(c, channels)*data++=buffer[c][read_samples];
  1151. Int read=read_samples*block; // this needs to be set before processing left-over data to the 'buffer'
  1152. flac. read+=read;
  1153. flac.to_read-=read;
  1154. flac. data =data;
  1155. if(Int left_samples=frame_samples-read_samples)
  1156. {
  1157. data=(Int24*)flac.buffer.setNum(left_samples*block).data();
  1158. for(; read_samples<frame_samples; read_samples++)FREPD(c, channels)*data++=buffer[c][read_samples];
  1159. }
  1160. }break;
  1161. }
  1162. }
  1163. return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
  1164. }
  1165. static void Error(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data) {}
  1166. void del() {if(decoder){FLAC__stream_decoder_delete(decoder); decoder=null;}}
  1167. ~FLAC() {del();}
  1168. FLAC(File &f, SoundStream::Params &params)
  1169. {
  1170. if(decoder=FLAC__stream_decoder_new())
  1171. {
  1172. buffer_pos=read=0;
  1173. seek=-1;
  1174. to_read=-1; // set that we're expecting meta
  1175. data=&params; // store pointer to the 'params' which will be used in 'Meta' callback (one time use)
  1176. file=&f;
  1177. FLAC__stream_decoder_set_md5_checking (decoder, false);
  1178. if(FLAC__stream_decoder_init_stream (decoder, Read, Seek, Tell, Length, Eof, Write, Meta, Error, this)==FLAC__STREAM_DECODER_INIT_STATUS_OK)
  1179. if(FLAC__stream_decoder_process_until_end_of_metadata(decoder))
  1180. {
  1181. data=null;
  1182. to_read=0;
  1183. #if 0 // accessing values here results in 0, use 'Meta' callback instead
  1184. FLAC__uint64 samples=FLAC__stream_decoder_get_total_samples (decoder);
  1185. params.frequency =FLAC__stream_decoder_get_sample_rate (decoder);
  1186. params.channels =FLAC__stream_decoder_get_channels (decoder);
  1187. params.bytes =FLAC__stream_decoder_get_bits_per_sample(decoder)/8;
  1188. params.block =params.channels*params.bytes;
  1189. params.size =params.block *samples;
  1190. params.bit_rate =(samples ? f.size()*8*params.frequency/samples : 0);
  1191. #endif
  1192. return; // ok
  1193. }
  1194. del();
  1195. }
  1196. }
  1197. void clearBuffer() {buffer_pos=0; buffer.clear();}
  1198. Bool pos(Long sample)
  1199. {
  1200. clearBuffer();
  1201. if(sample>=0)
  1202. {
  1203. seek=sample; // don't seek here, because it may trigger calling 'Write' function with the sound data for desired sample
  1204. return true;
  1205. }
  1206. return false;
  1207. }
  1208. Int set(Ptr data, Int size)
  1209. {
  1210. Int read=0, buffer_left=Min(buffer.elms()-buffer_pos, size);
  1211. if( buffer_left>0) // if we have any left in the buffer
  1212. {
  1213. CopyFast(data, buffer.data()+buffer_pos, buffer_left);
  1214. buffer_pos+=buffer_left; if(buffer_pos>=buffer.elms())clearBuffer(); // if we've read the entire buffer then clear it
  1215. data =(Byte*)data+buffer_left;
  1216. read +=buffer_left;
  1217. size -=buffer_left;
  1218. }
  1219. if(size>0) // if we still need more
  1220. {
  1221. T. data=data;
  1222. T. read=0;
  1223. T.to_read=size;
  1224. if(seek>=0) // if seek was requested
  1225. {
  1226. FLAC__bool ok=FLAC__stream_decoder_seek_absolute(decoder, seek); // this may trigger 'Write' function
  1227. seek=-1; // disable seeking
  1228. if(!ok)goto finish;
  1229. if(read+=T.read)goto finish;
  1230. }
  1231. if(FLAC__stream_decoder_process_single(decoder))read+=T.read;
  1232. finish:
  1233. T.to_read=0; // make sure to clear this because 'Write' callback can be called at any operation (for example when seeking)
  1234. }
  1235. return read;
  1236. }
  1237. };
  1238. extern "C"
  1239. {
  1240. FILE* fopen_utf8(const char *filename, const char *mode) {return null;} // create a dummy function which is needed for FLAC
  1241. }
  1242. static FLAC* LoadFLACHeader(File &f, SoundStream::Params &params)
  1243. {
  1244. params.zero();
  1245. FLAC *flac=new FLAC(f, params);
  1246. if(flac->decoder)return flac;
  1247. delete flac; return null;
  1248. }
  1249. #endif
  1250. /******************************************************************************/
  1251. // AAC
  1252. /******************************************************************************/
  1253. #if SUPPORT_AAC
  1254. static Bool Decode(HANDLE_AACDECODER decoder, UCHAR *packet_data, UInt packet_size, Ptr data, Int data_size, UInt flag)
  1255. {
  1256. UINT valid=packet_size;
  1257. return aacDecoder_Fill (decoder, &packet_data, &packet_size, &valid )==AAC_DEC_OK
  1258. && aacDecoder_DecodeFrame(decoder, (Short*)data, data_size/SIZE(Short), flag)==AAC_DEC_OK;
  1259. }
  1260. struct AAC
  1261. {
  1262. struct Part
  1263. {
  1264. Long pos;
  1265. Mems<U16> packet_sizes;
  1266. };
  1267. HANDLE_AACDECODER decoder;
  1268. Int part_index, packet_index, buffer_pos;
  1269. UInt frame_size, flag, delay;
  1270. File *file;
  1271. Memc<Byte> buffer;
  1272. Mems<Part> parts;
  1273. AAC() {decoder=null; part_index=packet_index=buffer_pos=0; frame_size=flag=delay=0; file=null;}
  1274. ~AAC() {if(decoder){aacDecoder_Close(decoder); decoder=null;}}
  1275. inline Bool decode(UCHAR *packet_data, UInt packet_size, Ptr data, Int data_size)
  1276. {
  1277. Bool ok=Decode(decoder, packet_data, packet_size, data, data_size, flag); flag=0;
  1278. return ok;
  1279. }
  1280. void advance( ) {if(InRange(part_index, parts))advance(parts[part_index]);}
  1281. void advance(C Part &part)
  1282. {
  1283. if(++packet_index>=part.packet_sizes.elms())
  1284. {
  1285. packet_index=0;
  1286. if(InRange(++part_index, parts))
  1287. {
  1288. C Part &part=parts[part_index];
  1289. file->pos(part.pos);
  1290. }
  1291. }
  1292. }
  1293. Bool pos(Long raw)
  1294. {
  1295. if(raw<0)return false;
  1296. raw+=delay;
  1297. Long frame_index=Unsigned(raw)/frame_size;
  1298. MAX( frame_index-=3, 0); // go back at least 3 frames
  1299. buffer_pos=raw-frame_index*frame_size; buffer.clear();
  1300. FREPA(parts)
  1301. {
  1302. C Part &part=parts[i];
  1303. if(InRange(frame_index, part.packet_sizes))
  1304. {
  1305. flag=AACDEC_CLRHIST|AACDEC_INTR; // this will reset the state
  1306. part_index=i;
  1307. packet_index=frame_index;
  1308. Long pos=part.pos; REP(packet_index)pos+=part.packet_sizes[i]; return file->pos(pos);
  1309. }
  1310. frame_index-=part.packet_sizes.elms();
  1311. }
  1312. return false;
  1313. }
  1314. Int set(Ptr data, Int size)
  1315. {
  1316. Int read=0;
  1317. for(; size>0; )
  1318. {
  1319. Int left=buffer.elms()-buffer_pos;
  1320. if( left>0) // if something left in buffer
  1321. {
  1322. Int copy=Min(size, left);
  1323. CopyFast(data, &buffer[buffer_pos], copy);
  1324. size -=copy;
  1325. buffer_pos+=copy;
  1326. read +=copy;
  1327. data =(Byte*)data+copy;
  1328. if(buffer_pos>=buffer.elms()){buffer_pos=0; buffer.clear();}
  1329. }else // need to decode
  1330. {
  1331. if(!InRange(part_index, parts))break; C Part &part=parts[part_index];
  1332. if(!InRange(packet_index, part.packet_sizes))break;
  1333. Byte packet_data[65536];
  1334. Int packet_size=part.packet_sizes[packet_index];
  1335. if( packet_size>SIZE(packet_data) || !file->getFast(packet_data, packet_size))break;
  1336. advance(part);
  1337. if(buffer_pos==0 && size>=frame_size) // decode directly into 'data', we can do this only if we're not skipping (buffer_pos==0) and if we know that 'data' can fit an entire frame
  1338. {
  1339. if(!decode(packet_data, packet_size, data, size))break;
  1340. read+=frame_size;
  1341. size-=frame_size;
  1342. data =(Byte*)data+frame_size;
  1343. }else
  1344. {
  1345. buffer.setNum(frame_size);
  1346. if(!decode(packet_data, packet_size, buffer.data(), buffer.elms())){buffer.clear(); break;}
  1347. if(buffer_pos>=buffer.elms()) // if we have to skip everything
  1348. {
  1349. buffer_pos-=buffer.elms(); // we've skipped this
  1350. buffer.clear();
  1351. }
  1352. }
  1353. }
  1354. }
  1355. return read;
  1356. }
  1357. };
  1358. #include "../../H/Misc/MP4.h"
  1359. static AAC* LoadAACHeader(File &f, SoundStream::Params &params, Int audio_track=0, Bool limit_to_stereo=true)
  1360. {
  1361. params.zero();
  1362. MP4 mp4; if(mp4.read(f))FREPA(mp4.tracks)
  1363. {
  1364. MP4::Track &track=mp4.tracks[i]; if(track.decoder_config.elms() && !audio_track--)
  1365. {
  1366. if(HANDLE_AACDECODER decoder=aacDecoder_Open(TT_MP4_RAW, 1))
  1367. {
  1368. UCHAR *ASC_data=track.decoder_config.data();
  1369. UINT ASC_size=track.decoder_config.elms();
  1370. if(limit_to_stereo)aacDecoder_SetParam(decoder, AAC_PCM_MAX_OUTPUT_CHANNELS, 2);
  1371. if(aacDecoder_ConfigRaw(decoder, &ASC_data, &ASC_size)==AAC_DEC_OK)
  1372. if(CStreamInfo *info=aacDecoder_GetStreamInfo(decoder))
  1373. if(track.frames())
  1374. {
  1375. // we have to decode one frame to get actual parameters of the audio stream, because 'info' at this point is just a guidance
  1376. UInt frame_size =track.frameSize (0);
  1377. Long frame_offset=track.frameOffset(0);
  1378. Memt<Byte> frame_data; frame_data.setNum(frame_size);
  1379. if(f.pos(frame_offset) && frame_data.loadRawData(f))
  1380. {
  1381. Int channels=Max(2, track.channels, info->channelConfig, info->aacNumChannels); if(limit_to_stereo)MIN(channels, 2); // use Max(2 because in one case (track.channels=1, info->channelConfig=1, info->aacNumChannels=0, but info->numChannels=2)
  1382. Int frame_samples=Max(2048, info->aacSamplesPerFrame); // Max(2048, because in one case info->aacSamplesPerFrame=1024, but info->frameSize=2048
  1383. Memt<Byte> pcm_data; pcm_data.setNum(frame_samples*channels*SIZE(Short));
  1384. if(Decode(decoder, frame_data.data(), frame_data.elms(), pcm_data.data(), pcm_data.elms(), 0))
  1385. {
  1386. params.bytes =2;
  1387. params.channels =info->numChannels;
  1388. params.block =params.channels*params.bytes;
  1389. params.frequency=info->sampleRate;
  1390. // time in seconds = track.duration / track.time_scale
  1391. params.size=((track.duration>=0 && track.time_scale) ? track.duration*params.frequency/track.time_scale // use given duration if known
  1392. : Long(track.frames())*info->frameSize // calculate time of all frames
  1393. );
  1394. MAX(params.size-=info->outputDelay, 0); params.size*=params.block;
  1395. params.bit_rate=track.avg_bit_rate;
  1396. if(!params.bit_rate && params.size)params.bit_rate=track.dataSize()*8*params.block*params.frequency/params.size;
  1397. AAC *aac=new AAC;
  1398. aac->decoder=decoder;
  1399. aac->frame_size=info->frameSize*params.block; // 'info->frameSize' is number of samples in 1 channel
  1400. aac->file=&f;
  1401. aac->buffer_pos=aac->delay=info->outputDelay*params.block;
  1402. aac->buffer.setNum(aac->frame_size).copyFrom(pcm_data.data()); // copy what we've decoded
  1403. if(aac->buffer_pos>=aac->buffer.elms()) // if we have to skip everything
  1404. {
  1405. aac->buffer_pos-=aac->buffer.elms(); // we've skipped this
  1406. aac->buffer.clear();
  1407. }
  1408. // setup aac->parts
  1409. Long data_pos=-1;
  1410. #if 0 // slow method
  1411. FREP(track.frames())
  1412. {
  1413. UInt frame_size =track.frameSize (i);
  1414. Long frame_offset=track.frameOffset(i);
  1415. if(data_pos!=frame_offset || !aac->parts.elms())aac->parts.New().pos=data_pos=frame_offset;
  1416. aac->parts.last().packet_sizes.add(frame_size);
  1417. data_pos+=frame_size;
  1418. }
  1419. #else
  1420. Int parts=0; Mems<U16> *part_packet_sizes=null;
  1421. if(track.parts.elms())
  1422. {
  1423. aac->parts.setNum(track.parts.elms()); // reserve memory because most likely this size we're going to get
  1424. FREPA(track.parts)
  1425. {
  1426. C MP4::Track::Part &part=track.parts[i]; if(part.frames)
  1427. {
  1428. if(data_pos!=part.pos || !parts){AAC::Part &aac_part=aac->parts(parts++); aac_part.pos=data_pos=part.pos; part_packet_sizes=&aac_part.packet_sizes;}
  1429. Int first=part_packet_sizes->addNum(part.frames);
  1430. if(part.frame_sizes.elms())
  1431. {
  1432. REPA(part.frame_sizes)data_pos+=((*part_packet_sizes)[first+i]=part.frame_sizes[i]);
  1433. }else
  1434. {
  1435. for(Int i=first; i<part_packet_sizes->elms(); i++)(*part_packet_sizes)[i]=part.frame_size;
  1436. data_pos+=part.frames*part.frame_size;
  1437. }
  1438. }
  1439. }
  1440. }else
  1441. if(track.stsc.elms())
  1442. {
  1443. aac->parts.setNum(track.chunk_offset.elms()); // reserve memory because most likely this size we're going to get
  1444. Int cur_frame=0, total_frames=track.frames(), chunk_frames=track.stsc[0].frames_per_chunk, chunk_i=1; // start with 1 because MP4 has chunk indexes starting from 1
  1445. if(track.frame_sizes.elms())MIN(total_frames, track.frame_sizes.elms());
  1446. for(Int i=1; i<track.stsc.elms(); i++)
  1447. {
  1448. C MP4::Track::STSC &next=track.stsc[i];
  1449. // process chunks in this run
  1450. if(chunk_frames>0)for(; chunk_i<next.first_chunk; chunk_i++)
  1451. {
  1452. if(Int add_frames=Min(total_frames-cur_frame, chunk_frames))
  1453. {
  1454. Long chunk_offset=track.chunkOffset(chunk_i-1); // -1 because we need 0-based indexes for 'chunkOffset'
  1455. if(data_pos!=chunk_offset || !parts){AAC::Part &aac_part=aac->parts(parts++); aac_part.pos=data_pos=chunk_offset; part_packet_sizes=&aac_part.packet_sizes;}
  1456. Int first=part_packet_sizes->addNum(add_frames);
  1457. if(track.frame_sizes.elms())
  1458. {
  1459. FREP(add_frames)data_pos+=((*part_packet_sizes)[first+i]=track.frame_sizes[cur_frame++]);
  1460. }else
  1461. {
  1462. REP(add_frames)(*part_packet_sizes)[first+i]=track.frame_size;
  1463. cur_frame+=add_frames;
  1464. data_pos +=add_frames*track.frame_size;
  1465. }
  1466. }
  1467. }else chunk_i=next.first_chunk;
  1468. chunk_frames=next.frames_per_chunk;
  1469. }
  1470. // process all chunks that are left
  1471. if(chunk_frames>0)for(chunk_i--; chunk_i<track.chunk_offset.elms(); chunk_i++) // first convert to 0-based indexes
  1472. if(Int add_frames=Min(total_frames-cur_frame, chunk_frames))
  1473. {
  1474. Long chunk_offset=track.chunk_offset[chunk_i];
  1475. if(data_pos!=chunk_offset || !parts){AAC::Part &aac_part=aac->parts(parts++); aac_part.pos=data_pos=chunk_offset; part_packet_sizes=&aac_part.packet_sizes;}
  1476. Int first=part_packet_sizes->addNum(add_frames);
  1477. if(track.frame_sizes.elms())
  1478. {
  1479. FREP(add_frames)data_pos+=((*part_packet_sizes)[first+i]=track.frame_sizes[cur_frame++]);
  1480. }else
  1481. {
  1482. REP(add_frames)(*part_packet_sizes)[first+i]=track.frame_size;
  1483. cur_frame+=add_frames;
  1484. data_pos +=add_frames*track.frame_size;
  1485. }
  1486. }
  1487. }
  1488. aac->parts.setNum(parts); // remove allocated but unprocessed parts (this can happen)
  1489. #endif
  1490. aac->advance(); // !! call after 'aac->parts' are setup !! we've already read one packet, so we need to advance
  1491. return aac;
  1492. }
  1493. }
  1494. }
  1495. aacDecoder_Close(decoder); // error
  1496. }
  1497. break;
  1498. }
  1499. }
  1500. return null;
  1501. }
  1502. #endif
  1503. /******************************************************************************/
  1504. // SOUND CACHE
  1505. /******************************************************************************/
  1506. Bool CacheSound(C Str &name)
  1507. {
  1508. return FileDatas.get(name)!=null; // load and keep forever
  1509. }
  1510. Bool CacheSound(C UID &id)
  1511. {
  1512. return FileDatas.get(id)!=null; // load and keep forever
  1513. }
  1514. /******************************************************************************/
  1515. // SOUND STREAM
  1516. /******************************************************************************/
  1517. Flt SoundStream::Params::length()C {Int div=block*frequency; return div ? Dbl(size)/div : 0;} // use Dbl because 'size' can be huge (more than INT_MAX)
  1518. /******************************************************************************/
  1519. void SoundStream::del()
  1520. {
  1521. switch(_codec)
  1522. {
  1523. #if SUPPORT_FLAC
  1524. case SOUND_FLAC: if(FLAC* &flac=(FLAC*&)_extra)Delete(flac); break;
  1525. #endif
  1526. #if SUPPORT_VORBIS
  1527. case SOUND_OGG_VORBIS : if(OggVorbis_File* &vorbis=(OggVorbis_File*&)_extra){ov_clear(vorbis); Free(vorbis);} break;
  1528. case SOUND_WEBM_VORBIS: if(WebmVorbisFile* &vorbis=(WebmVorbisFile*&)_extra)Delete(vorbis); break;
  1529. case SOUND_SND_VORBIS : if( SndVorbisFile* &vorbis=( SndVorbisFile*&)_extra)Delete(vorbis); break;
  1530. #endif
  1531. #if SUPPORT_OPUS
  1532. case SOUND_OGG_OPUS : if( OggOpusFile* &opus=( OggOpusFile*&)_extra){op_free(opus); opus=null;} break;
  1533. case SOUND_WEBM_OPUS: if(WebmOpusFile* &opus=(WebmOpusFile*&)_extra)Delete(opus); break;
  1534. case SOUND_SND_OPUS : if( SndOpusFile* &opus=( SndOpusFile*&)_extra)Delete(opus); break;
  1535. #endif
  1536. #if SUPPORT_MP3
  1537. case SOUND_MP3: if(MP3* &mp3=(MP3*&)_extra)Delete(mp3); break;
  1538. #endif
  1539. #if SUPPORT_AAC
  1540. case SOUND_AAC: if(AAC* &aac=(AAC*&)_extra)Delete(aac); break;
  1541. #endif
  1542. }
  1543. if(_callback){_callback->del(); _callback=null;}
  1544. _codec=SOUND_NONE;
  1545. _pos =0;
  1546. _raw_ofs=0; // !! clear '_raw_ofs' after releasing '_extra' in case they form a union !!
  1547. _f .del();
  1548. _par.zero();
  1549. }
  1550. SoundStream::SoundStream()
  1551. {
  1552. _codec =SOUND_NONE;
  1553. _pos =0;
  1554. _raw_ofs =0;
  1555. _extra =null;
  1556. _callback=null;
  1557. _par.zero();
  1558. }
  1559. /******************************************************************************/
  1560. Bool SoundStream::open(C Str &name)
  1561. {
  1562. if(name.is())
  1563. {
  1564. if(FileData *fd=FileDatas.find(name)){_f.readMem(fd->data.data(), fd->data.elms()); return true;} // try from cache if was loaded
  1565. return _f.readTry(name); // try from file on disk
  1566. }
  1567. return false;
  1568. }
  1569. Bool SoundStream::create(C Str &name) // !! warning: set T._codec as last thing because it indicates that stream is loaded !!
  1570. {
  1571. del();
  1572. if(open(name))
  1573. {
  1574. UInt cc4=_f.getUInt(); // first check cc4 to avoid libs reading other types of formats (in one case mp3 lib read a flac file and reported it was mp3)
  1575. switch(cc4)
  1576. {
  1577. case CC4_SND: // Esenthel Sound
  1578. {
  1579. _f.pos(0); SND_CODEC codec; if(LoadSndHeader(_f, _par, codec))switch(codec)
  1580. {
  1581. case SND_RAW_16: _raw_ofs=_f.pos(); _codec=SOUND_WAV; return true; // set '_codec' as the last thing
  1582. #if SUPPORT_VORBIS
  1583. case SND_VORBIS:
  1584. {
  1585. SndVorbisFile temp; if(temp.init(T))
  1586. {
  1587. SndVorbisFile *extra=new SndVorbisFile; Swap(*extra, temp);
  1588. _extra=extra; _codec=SOUND_SND_VORBIS; return true; // set '_codec' as the last thing
  1589. }
  1590. }break;
  1591. #endif
  1592. #if SUPPORT_OPUS
  1593. case SND_OPUS:
  1594. {
  1595. SndOpusFile temp; if(temp.init(T))
  1596. {
  1597. SndOpusFile *extra=new SndOpusFile; Swap(*extra, temp);
  1598. _extra=extra; _codec=SOUND_SND_OPUS; return true; // set '_codec' as the last thing
  1599. }
  1600. }break;
  1601. #endif
  1602. }
  1603. }break;
  1604. case CC4('R','I','F','F'): // WAV
  1605. {
  1606. _f.pos(0); switch(LoadWavHeader(_f, _par)) // reset file position after previous attempt
  1607. {
  1608. case SOUND_MP3: goto load_mp3;
  1609. case SOUND_WAV: _raw_ofs=_f.pos(); _codec=SOUND_WAV; return true; // set '_codec' as the last thing
  1610. }
  1611. }break;
  1612. case CC4('O', 'g', 'g', 'S'): // Ogg Vorbis and Opus
  1613. {
  1614. #if SUPPORT_VORBIS
  1615. _f.pos(0); if(_extra=LoadOggVorbisHeader(_f, _par)){_codec=SOUND_OGG_VORBIS; return true;} // reset file position after previous attempt and set '_codec' as the last thing
  1616. #endif
  1617. #if SUPPORT_OPUS
  1618. _f.pos(0); if(_extra=LoadOggOpusHeader(_f, _par)){_codec=SOUND_OGG_OPUS; return true;} // reset file position after previous attempt and set '_codec' as the last thing
  1619. #endif
  1620. }break;
  1621. case 0xA3DF451A: // WebM Vorbis and Opus
  1622. {
  1623. #if SUPPORT_VORBIS || SUPPORT_OPUS
  1624. _f.pos(0); SOUND_CODEC codec; if(_extra=LoadWebmHeader(_f, _par, codec)){_codec=codec; return true;} // reset file position after previous attempt and set '_codec' as the last thing
  1625. #endif
  1626. }break;
  1627. case CC4('f', 'L', 'a', 'C'): // Flac
  1628. {
  1629. #if SUPPORT_FLAC
  1630. _f.pos(0); if(_extra=LoadFLACHeader(_f, _par)){_codec=SOUND_FLAC; return true;} // reset file position after previous attempt and set '_codec' as the last thing
  1631. #endif
  1632. }break;
  1633. default:
  1634. {
  1635. #if SUPPORT_AAC // MP4
  1636. if(!(cc4&0xFFFFFF)) // first UInt is the chunk size in BigEndian format, and for 'ftyp' it usually should be very small, so check 3 highest bytes that they are zero (have to check lowest because it's BigEndian)
  1637. {
  1638. _f.pos(4); if(_f.getUInt()==CC4('f','t','y','p'))
  1639. {
  1640. _f.pos(0); if(_extra=LoadAACHeader(_f, _par)){_codec=SOUND_AAC; return true;} // reset file position after previous attempt and set '_codec' as the last thing
  1641. }
  1642. }
  1643. #endif
  1644. if((cc4&0xFFFFFF)==CC4('I', 'D', '3', 0) // inside ID3 tag
  1645. || (cc4&0xE0FF)==0xE0FF // 0b11100000 == 0xE0 mask (3bit on-frame sync, 2bit off-MPEG type, 2bit off-layer type, 1bit off-CRC) http://www.mp3-tech.org/programmer/frame_header.html
  1646. ) // 0b11100000 == 0xE0 (111 , ignored , ignored , ignored ) https://en.wikipedia.org/wiki/MP3#File_structure
  1647. {
  1648. #if SUPPORT_MP3
  1649. _f.pos(0); // reset file position after previous attempt
  1650. #endif
  1651. load_mp3:;
  1652. #if SUPPORT_MP3
  1653. if(_extra=LoadMP3Header(_f, _par)){_codec=SOUND_MP3; return true;} // set '_codec' as the last thing
  1654. #endif
  1655. }
  1656. }break;
  1657. }
  1658. del();
  1659. }
  1660. return false;
  1661. }
  1662. Bool SoundStream::create(C UID &id) {return create(_EncodeFileName(id));}
  1663. Bool SoundStream::create(SoundCallback &callback)
  1664. {
  1665. del();
  1666. Byte bits=0, channels=0; Int frequency=0, bit_rate=-1; Long size=-1;
  1667. if(callback.create(bits, channels, frequency, size, bit_rate))
  1668. if(bits ==8 || bits ==16 || bits==24)
  1669. if(channels >=1 && channels <=2)
  1670. if(frequency>=1 && frequency<=192000)
  1671. {
  1672. _par.bytes = bits/8;
  1673. _par.channels = channels;
  1674. _par.frequency= frequency;
  1675. _par.size = size;
  1676. _par.bit_rate = bit_rate;
  1677. _par.block = channels*_par.bytes;
  1678. _callback =&callback;
  1679. _codec = SOUND_DYNAMIC;
  1680. return true;
  1681. }
  1682. callback.del(); return false;
  1683. }
  1684. /******************************************************************************/
  1685. Bool SoundStream::pos(Long pos)
  1686. {
  1687. if(T._pos==pos)return true;
  1688. switch(_codec)
  1689. {
  1690. case SOUND_WAV: if(pos>=0 && _f.pos(pos+_raw_ofs))goto ok; break;
  1691. #if SUPPORT_FLAC
  1692. case SOUND_FLAC: if(((FLAC*)_extra)->pos(pos/_par.block))goto ok; break;
  1693. #endif
  1694. #if SUPPORT_VORBIS
  1695. case SOUND_OGG_VORBIS : if(!ov_pcm_seek((OggVorbis_File*)_extra, pos/_par.block))goto ok; break;
  1696. case SOUND_WEBM_VORBIS: if(((WebmVorbisFile*)_extra)->pos(pos))goto ok; break;
  1697. case SOUND_SND_VORBIS : if((( SndVorbisFile*)_extra)->pos(pos))goto ok; break;
  1698. #endif
  1699. #if SUPPORT_OPUS
  1700. case SOUND_OGG_OPUS : if(!op_pcm_seek((OggOpusFile*)_extra, pos/_par.block))goto ok; break;
  1701. case SOUND_WEBM_OPUS: if(((WebmOpusFile*)_extra)->pos(pos))goto ok; break;
  1702. case SOUND_SND_OPUS : if((( SndOpusFile*)_extra)->pos(pos))goto ok; break;
  1703. #endif
  1704. #if SUPPORT_MP3
  1705. case SOUND_MP3: if(((MP3*)_extra)->pos(pos/_par.block))goto ok; break;
  1706. #endif
  1707. #if SUPPORT_AAC
  1708. case SOUND_AAC: if(((AAC*)_extra)->pos(pos))goto ok; break;
  1709. #endif
  1710. case SOUND_DYNAMIC: if(_callback->raw(pos))goto ok; break;
  1711. }
  1712. return false;
  1713. ok:
  1714. T._pos=pos; return true;
  1715. }
  1716. Bool SoundStream::sample(Long sample) {return pos(sample*block());}
  1717. /******************************************************************************/
  1718. Int SoundStream::set(Ptr data, Int size)
  1719. {
  1720. switch(_codec)
  1721. {
  1722. case SOUND_WAV: size=_f.getReturnSize(data, size); break;
  1723. #if SUPPORT_FLAC
  1724. case SOUND_FLAC: size=((FLAC*)_extra)->set(data, size); break;
  1725. #endif
  1726. #if SUPPORT_VORBIS
  1727. case SOUND_OGG_VORBIS : size=ov_read((OggVorbis_File*)_extra, (char*)data, size, 0, 2, 1, null); break;
  1728. case SOUND_WEBM_VORBIS: size=((WebmVorbisFile*)_extra)->set(data, Min(size, left())); break; // 'WebmVorbisFile' may generate some extra data at the end when seeking, so we need to limit what we can return to skip this padding
  1729. case SOUND_SND_VORBIS : size=(( SndVorbisFile*)_extra)->set(data, size ); break;
  1730. #endif
  1731. #if SUPPORT_OPUS
  1732. case SOUND_OGG_OPUS:
  1733. {
  1734. OggOpusFile *opus=(OggOpusFile*)_extra;
  1735. switch(_par.channels)
  1736. {
  1737. default: size=0; break;
  1738. case 2: size=op_read_stereo(opus, (opus_int16*)data, size/SIZE(opus_int16))*_par.block; break;
  1739. case 1:
  1740. {
  1741. int link, samples=op_read(opus, (opus_int16*)data, size/SIZE(opus_int16), &link);
  1742. size=((op_channel_count(opus, link)==1) ? samples*_par.block : 0);
  1743. }break;
  1744. }
  1745. }break;
  1746. case SOUND_WEBM_OPUS: size=((WebmOpusFile*)_extra)->set(data, size ); break;
  1747. case SOUND_SND_OPUS : size=(( SndOpusFile*)_extra)->set(data, Min(size, left())); break; // 'SndOpusEncoder' may have introduced some zero padding which would result in extra data at the end, so we need to limit what we can return to skip this padding
  1748. #endif
  1749. #if SUPPORT_MP3
  1750. case SOUND_MP3: size=((MP3*)_extra)->set(data, size); break;
  1751. #endif
  1752. #if SUPPORT_AAC
  1753. case SOUND_AAC: size=((AAC*)_extra)->set(data, size); break;
  1754. #endif
  1755. case SOUND_DYNAMIC: size=_callback->set(data, size); break;
  1756. default : size=0; break;
  1757. }
  1758. if(size>0)T._pos+=size; return size;
  1759. }
  1760. /******************************************************************************/
  1761. void SoundStream::fastSeek()
  1762. {
  1763. switch(_codec)
  1764. {
  1765. #if SUPPORT_OPUS
  1766. case SOUND_WEBM_OPUS: ((WebmOpusFile*)_extra)->extra_frames=OPUS_FRAME_HISTORY_FAST; break;
  1767. case SOUND_SND_OPUS : (( SndOpusFile*)_extra)->extra_frames=OPUS_FRAME_HISTORY_FAST; break;
  1768. #endif
  1769. }
  1770. }
  1771. void SoundStream::memAddressChanged()
  1772. {
  1773. switch(_codec)
  1774. {
  1775. #if SUPPORT_FLAC
  1776. case SOUND_FLAC: ((FLAC*)_extra)->file=&_f; break;
  1777. #endif
  1778. #if SUPPORT_VORBIS
  1779. case SOUND_OGG_VORBIS : ((OggVorbis_File*)_extra)->datasource=&_f; break;
  1780. case SOUND_WEBM_VORBIS: ((WebmVorbisFile*)_extra)->file =&_f; break;
  1781. case SOUND_SND_VORBIS : (( SndVorbisFile*)_extra)->file =&_f; break;
  1782. #endif
  1783. #if SUPPORT_OPUS
  1784. case SOUND_OGG_OPUS : (( OggOpusFile*)_extra)->stream=&_f; break;
  1785. case SOUND_WEBM_OPUS: ((WebmOpusFile*)_extra)->file =&_f; break;
  1786. case SOUND_SND_OPUS : (( SndOpusFile*)_extra)->file =&_f; break;
  1787. #endif
  1788. #if SUPPORT_MP3
  1789. case SOUND_MP3: ((MP3*)_extra)->file=&_f; break;
  1790. #endif
  1791. #if SUPPORT_AAC
  1792. case SOUND_AAC: ((AAC*)_extra)->file=&_f; break;
  1793. #endif
  1794. }
  1795. }
  1796. /******************************************************************************/
  1797. void SoundHeader::zero () {bytes=bits=channels=0; frequency=bit_rate=0; size=0; length=0; codec=SOUND_NONE;}
  1798. CChar8* SoundHeader::codecName()C {return CodecName(codec);}
  1799. Bool SoundHeader::load (C Str &name)
  1800. {
  1801. SoundStream stream; if(stream.create(name))
  1802. {
  1803. bytes =stream.bytes();
  1804. bits =stream.bits();
  1805. channels =stream.channels();
  1806. frequency=stream.frequency();
  1807. bit_rate =stream.bitRate();
  1808. size =stream.size();
  1809. length =stream.length();
  1810. codec =stream.codec();
  1811. return true;
  1812. }
  1813. zero(); return false;
  1814. }
  1815. /******************************************************************************/
  1816. static const Int OpusFreqs[]={8000, 12000, 16000, 24000, 48000}; // these are the only frequencies supported by Opus, values taken from comments in 'opus_encoder_create'
  1817. static Int OpusFrequency(Int freq) // !! do not change in the future, because old 'SndOpusFile' will fail !!
  1818. {
  1819. for(Int i=Elms(OpusFreqs); --i>0; )
  1820. {
  1821. Int avg=AvgI(OpusFreqs[i], OpusFreqs[i-1]);
  1822. if(freq>=avg)return OpusFreqs[i]; // yes use >= instead of >, because this allows precise mapping if 'OpusFreqs' had 48000-2, 48000-1, 48000, 48000+1, 48000+2
  1823. }
  1824. return OpusFreqs[0];
  1825. }
  1826. /******************************************************************************/
  1827. OpusEncoder::OpusEncoder() {_encoder=null; _channels=_frame_samples=0;}
  1828. OpusEncoder& OpusEncoder::del()
  1829. {
  1830. #if SUPPORT_OPUS_ENC
  1831. if(_encoder){opus_encoder_destroy((OE)_encoder); _encoder=null; _channels=_frame_samples=0; _data.del();}
  1832. #endif
  1833. return T;
  1834. }
  1835. Bool OpusEncoder::create(Int channels, Bool voice, Int frequency)
  1836. {
  1837. #if SUPPORT_OPUS_ENC
  1838. del();
  1839. if(channels>=1 && channels<=2) // among others, this is needed for '_last_sample' which has room for up to 2 channels
  1840. if(_encoder=opus_encoder_create(OpusFrequency(frequency), channels, voice ? OPUS_APPLICATION_VOIP : OPUS_APPLICATION_AUDIO, null))
  1841. {
  1842. T._channels=channels;
  1843. T._last_sample_size=0;
  1844. frameLength(OPUS_FRAME_SIZE_BEST_QUALITY); // 2.5 .. 60 range is valid
  1845. complexity(1); // turn on highest complexity by default
  1846. return true;
  1847. }
  1848. #endif
  1849. return false;
  1850. }
  1851. void OpusEncoder::reset()
  1852. {
  1853. #if SUPPORT_OPUS_ENC
  1854. if(_encoder)
  1855. {
  1856. opus_encoder_ctl((OE)_encoder, OPUS_RESET_STATE);
  1857. T._last_sample_size=0;
  1858. T._data.clear();
  1859. }
  1860. #endif
  1861. }
  1862. #if SUPPORT_OPUS_ENC
  1863. static const Int OpusValidFramesize[]={120, 240, 480, 960, 1920, 2880}; // ranges were taken from comments in 'opus_encode' function, 120 (2.5 ms), 960 (20 ms), 2880 (60 ms)
  1864. #endif
  1865. Int OpusEncoder::frameLength()C {return _frame_samples/48;} // 48 because of OPUS_FREQUENCY/1000 = 48,000/1000 = 48
  1866. OpusEncoder& OpusEncoder::frameLength(Int length)
  1867. {
  1868. #if SUPPORT_OPUS_ENC
  1869. if(_channels)
  1870. {
  1871. Int frame_size=length*48, nearest=0, dist;
  1872. REPA(OpusValidFramesize)
  1873. {
  1874. Int value=OpusValidFramesize[i], d=Abs(frame_size-value);
  1875. if(!nearest || d<dist){dist=d; nearest=value;}
  1876. }
  1877. _frame_samples=nearest; // set once at the end to avoid multi-threading issues
  1878. }
  1879. #endif
  1880. return T;
  1881. }
  1882. Int OpusEncoder::frequency()C
  1883. {
  1884. Int bit_rate=0;
  1885. #if SUPPORT_OPUS_ENC
  1886. if(_encoder)opus_encoder_ctl((OE)_encoder, OPUS_GET_SAMPLE_RATE(&bit_rate));
  1887. #endif
  1888. return bit_rate;
  1889. }
  1890. Int OpusEncoder::bitRate()C
  1891. {
  1892. Int bit_rate=0;
  1893. #if SUPPORT_OPUS_ENC
  1894. if(_encoder)opus_encoder_ctl((OE)_encoder, OPUS_GET_BITRATE(&bit_rate));
  1895. #endif
  1896. return bit_rate;
  1897. }
  1898. OpusEncoder& OpusEncoder::bitRate(Int bit_rate)
  1899. {
  1900. #if SUPPORT_OPUS_ENC
  1901. if(_encoder)opus_encoder_ctl((OE)_encoder, OPUS_SET_BITRATE(Mid(bit_rate, 500, 512000))); // limits are described in 'OPUS_SET_BITRATE' header comments
  1902. #endif
  1903. return T;
  1904. }
  1905. Bool OpusEncoder::vbr()C
  1906. {
  1907. Int vbr=0;
  1908. #if SUPPORT_OPUS_ENC
  1909. if(_encoder)opus_encoder_ctl((OE)_encoder, OPUS_GET_VBR(&vbr));
  1910. #endif
  1911. return vbr==1;
  1912. }
  1913. OpusEncoder& OpusEncoder::vbr(Bool on)
  1914. {
  1915. #if SUPPORT_OPUS_ENC
  1916. if(_encoder)opus_encoder_ctl((OE)_encoder, OPUS_SET_VBR(on ? 1 : 0));
  1917. #endif
  1918. return T;
  1919. }
  1920. Flt OpusEncoder::complexity()C
  1921. {
  1922. Int complexity=0;
  1923. #if SUPPORT_OPUS_ENC
  1924. if(_encoder)opus_encoder_ctl((OE)_encoder, OPUS_GET_COMPLEXITY(&complexity));
  1925. #endif
  1926. return complexity/10.0f;
  1927. }
  1928. OpusEncoder& OpusEncoder::complexity(Flt complexity)
  1929. {
  1930. #if SUPPORT_OPUS_ENC
  1931. if(_encoder)opus_encoder_ctl((OE)_encoder, OPUS_SET_COMPLEXITY(Mid(RoundPos(complexity*10), 0, 10))); // range of 0..10 described in 'OPUS_SET_COMPLEXITY' header comments
  1932. #endif
  1933. return T;
  1934. }
  1935. Int OpusEncoder::delay()C
  1936. {
  1937. Int delay=0;
  1938. #if SUPPORT_OPUS_ENC
  1939. if(_encoder)opus_encoder_ctl((OE)_encoder, OPUS_GET_LOOKAHEAD(&delay));
  1940. #endif
  1941. return delay;
  1942. }
  1943. #if SUPPORT_OPUS_ENC
  1944. static Bool OpusEncode(OE encoder, opus_int16 *data, Int frame_size, MemPtr<Byte> compressed_data, MemPtr<Int> packet_sizes)
  1945. {
  1946. const Int max_packet_size=4096, // max 60 ms * 512000 bits per second = 60*512000/8/1000 = 3840 (round up to 4096 if there are any headers needed)
  1947. size=compressed_data.elms(); // remember current size
  1948. if(compressed_data.continuous()) // compress directly to 'compressed_data'
  1949. {
  1950. compressed_data.addNum(max_packet_size); // make room for another packet
  1951. Int packet_size=opus_encode(encoder, data, frame_size, &compressed_data[size], max_packet_size);
  1952. if( packet_size>0)
  1953. {
  1954. packet_sizes.add(packet_size);
  1955. compressed_data.setNum(size+packet_size);
  1956. return true;
  1957. }
  1958. // failed
  1959. compressed_data.setNum(size); // restore original size
  1960. }else // compress to 'temp' memory
  1961. {
  1962. Byte temp[max_packet_size];
  1963. Int packet_size=opus_encode(encoder, data, frame_size, temp, SIZE(temp));
  1964. if( packet_size>0)
  1965. {
  1966. packet_sizes.add(packet_size);
  1967. compressed_data.addNum(packet_size);
  1968. FREP(packet_size)compressed_data[size+i]=temp[i];
  1969. return true;
  1970. }
  1971. }
  1972. return false;
  1973. }
  1974. #endif
  1975. Bool OpusEncoder::encode(CPtr data, Int size, MemPtr<Byte> compressed_data, MemPtr<Int> packet_sizes)
  1976. {
  1977. Bool ok=false;
  1978. compressed_data.clear();
  1979. packet_sizes .clear();
  1980. #if SUPPORT_OPUS_ENC
  1981. if(_encoder)
  1982. {
  1983. ok=true;
  1984. if(data && size>0)
  1985. {
  1986. // store 'data' in '_last_sample'
  1987. {
  1988. Int block=T.block(), copy=Min(size, block), new_size=block-copy;
  1989. //if(_last_sample_size+copy>block) // if what we will have will exceed the storage
  1990. //if(_last_sample_size>block-copy)
  1991. if(_last_sample_size>new_size ) // have to move data in '_last_sample' to the left
  1992. {
  1993. MoveFast((Byte*)_last_sample, (Byte*)_last_sample+(_last_sample_size-new_size), new_size); // copy last 'new_size' bytes from '_last_sample'
  1994. _last_sample_size=new_size;
  1995. }
  1996. CopyFast((Byte*)_last_sample+_last_sample_size, (Byte*)data+(size-copy), copy); // copy last 'copy' bytes from 'data'
  1997. _last_sample_size+=copy;
  1998. }
  1999. const Int frame_samples=frameSamples(), // copy this first into a temp variable in case it gets changed on a secondary thread
  2000. frame_raw =frame_samples*SIZE(I16)*_channels;
  2001. for(; size>0 && ok; )
  2002. {
  2003. if(_data.elms() || size<frame_raw) // need to buffer
  2004. {
  2005. Int copy=Min(size, frame_raw-_data.elms()); // this can be negative if encoder was first called with a bigger frame which would allocate '_data' to some size, and then frame size got decreased
  2006. if( copy>0)
  2007. {
  2008. CopyFast(&_data[_data.addNum(copy)], data, copy);
  2009. data =(Byte*)data+copy;
  2010. size-=copy;
  2011. }
  2012. if(_data.elms()>=frame_raw) // if we've gathered enough to fill a frame
  2013. {
  2014. ok&=OpusEncode((OE)_encoder, (opus_int16*)_data.data(), frame_samples, compressed_data, packet_sizes);
  2015. if(_data.elms()>frame_raw) // this can happen if frame size got changed, and now we have leftovers
  2016. {
  2017. MoveFast(_data.data(), _data.data()+frame_raw, _data.elms()-frame_raw); // move leftovers to the start
  2018. _data.addNum(-frame_raw); // remove processed elements
  2019. }else _data.clear();
  2020. }
  2021. }else // encode directly from provided data
  2022. {
  2023. ok&=OpusEncode((OE)_encoder, (opus_int16*)data, frame_samples, compressed_data, packet_sizes);
  2024. data =(Byte*)data+frame_raw;
  2025. size-=frame_raw;
  2026. }
  2027. }
  2028. }
  2029. }
  2030. #endif
  2031. return ok;
  2032. }
  2033. Bool OpusEncoder::flush(Int &flushed_samples, MemPtr<Byte> compressed_data, MemPtr<Int> packet_sizes) // !! has to be called only once, because it always adds zero padding for delay !!
  2034. {
  2035. Bool ok=false;
  2036. compressed_data.clear();
  2037. packet_sizes .clear();
  2038. flushed_samples=0;
  2039. #if SUPPORT_OPUS_ENC
  2040. if(_encoder)
  2041. {
  2042. Int block=T.block();
  2043. Zero((Byte*)_last_sample+_last_sample_size, block-_last_sample_size); // zero unwritten data
  2044. // have to add delay data
  2045. #if 0 // don't add zeros because it causes sudden jumps and pop/clicks in the last audio data
  2046. _data.addNumZero(delay()*block); // as zero bytes
  2047. #else // duplicate last encoded sample multiple times
  2048. Int delay=T.delay();
  2049. Byte *dest=&_data[_data.addNum(delay*block)];
  2050. FREP(delay){CopyFast(dest, _last_sample, block); dest+=block;}
  2051. #endif
  2052. flushed_samples=_data.elms()/block; // must include delay, but not zero padding added for last frame below
  2053. ok=true;
  2054. for(Int pos=0, left=_data.elms(); left>0; )
  2055. {
  2056. Int frame_samples=frameSamples();
  2057. #if 0 // do not attempt to reduce the frame size if we want to write less data, because it can drastically reduce the quality and cause pop/clicks, especially for looped audio
  2058. Int left_samples=left/block;
  2059. if( left_samples<frame_samples) // if we want to write less samples than encoder frame samples, this check is important, without it we could request a bigger frame size than encoder's, which could result in fewer frames than expected
  2060. {
  2061. Int smallest_frame_i=0; for(; left_samples>OpusValidFramesize[smallest_frame_i] && InRange(smallest_frame_i+1, OpusValidFramesize); smallest_frame_i++); // find smallest frame capable of storing all samples
  2062. frame_samples=OpusValidFramesize[smallest_frame_i];
  2063. }
  2064. #endif
  2065. Int frame_raw=frame_samples*block;
  2066. if(left<frame_raw)_data.addNumZero(frame_raw-left); // if what we have is not enough to fit in frame then padd with zeros, here it's actually better to use zeroes than '_last_sample' because it will give more accurate results
  2067. ok&=OpusEncode((OE)_encoder, (opus_int16*)&_data[pos], frame_samples, compressed_data, packet_sizes);
  2068. left-=frame_raw;
  2069. pos +=frame_raw;
  2070. }
  2071. _data.clear(); _last_sample_size=0;
  2072. }
  2073. #endif
  2074. return ok;
  2075. }
  2076. /******************************************************************************/
  2077. OpusDecoder::OpusDecoder() {_decoder=null; _channels=0;}
  2078. OpusDecoder& OpusDecoder::del()
  2079. {
  2080. #if SUPPORT_OPUS
  2081. if(_decoder){opus_decoder_destroy((OD)_decoder); _decoder=null; _channels=0;}
  2082. #endif
  2083. return T;
  2084. }
  2085. Bool OpusDecoder::create(Int channels, Int frequency)
  2086. {
  2087. #if SUPPORT_OPUS
  2088. del();
  2089. if(channels>=1 && channels<=2)
  2090. if(_decoder=opus_decoder_create(OpusFrequency(frequency), channels, null))
  2091. {
  2092. T._channels=channels;
  2093. return true;
  2094. }
  2095. #endif
  2096. return false;
  2097. }
  2098. void OpusDecoder::reset()
  2099. {
  2100. #if SUPPORT_OPUS
  2101. if(_decoder)opus_decoder_ctl((OD)_decoder, OPUS_RESET_STATE);
  2102. #endif
  2103. }
  2104. Int OpusDecoder::frequency()C
  2105. {
  2106. Int bit_rate=0;
  2107. #if SUPPORT_OPUS
  2108. if(_decoder)opus_decoder_ctl((OD)_decoder, OPUS_GET_SAMPLE_RATE(&bit_rate));
  2109. #endif
  2110. return bit_rate;
  2111. }
  2112. Bool OpusDecoder::decode(CPtr packet_data, Int packet_size, MemPtr<Byte> decompressed_data)
  2113. {
  2114. decompressed_data.clear();
  2115. if(packet_size<=0)return true;
  2116. #if SUPPORT_OPUS
  2117. if(_decoder && packet_data)
  2118. {
  2119. const Int block=SIZE(I16)*_channels,
  2120. max_frame_size=OPUS_MAX_FRAME_SAMPLES*block;
  2121. if(decompressed_data.continuous())
  2122. {
  2123. decompressed_data.setNum(max_frame_size);
  2124. Int decoded_samples=opus_decode((OD)_decoder, (Byte*)packet_data, packet_size, (opus_int16*)decompressed_data.data(), OPUS_MAX_FRAME_SAMPLES, 0); // keep 'decode_fec' as 0 because when set to 1 decoding may fail if it was not started from first packet
  2125. if( decoded_samples>0)
  2126. {
  2127. decompressed_data.setNum(decoded_samples*block);
  2128. return true;
  2129. }
  2130. decompressed_data.clear();
  2131. }else
  2132. if(_channels<=2)
  2133. {
  2134. Byte temp[OPUS_MAX_FRAME_SAMPLES*SIZE(I16)*2]; // *2 = 2 channels (stereo)
  2135. Int decoded_samples=opus_decode((OD)_decoder, (Byte*)packet_data, packet_size, (opus_int16*)temp, OPUS_MAX_FRAME_SAMPLES, 0); // keep 'decode_fec' as 0 because when set to 1 decoding may fail if it was not started from first packet
  2136. if( decoded_samples>0)
  2137. {
  2138. decompressed_data.setNum(decoded_samples*block).copyFrom(temp);
  2139. return true;
  2140. }
  2141. }
  2142. }
  2143. #endif
  2144. return false;
  2145. }
  2146. Int OpusDecoder::decode(CPtr packet_data, Int packet_size, Ptr decompressed_data, Int decompressed_size)
  2147. {
  2148. if(packet_size<=0)return 0; // nothing was decoded
  2149. #if SUPPORT_OPUS
  2150. if(_decoder && packet_data)
  2151. {
  2152. const Int block=SIZE(I16)*_channels,
  2153. max_frame_samples=decompressed_size/block;
  2154. Int decoded_samples=opus_decode((OD)_decoder, (Byte*)packet_data, packet_size, (opus_int16*)decompressed_data, max_frame_samples, 0); // keep 'decode_fec' as 0 because when set to 1 decoding may fail if it was not started from first packet
  2155. if( decoded_samples>0)return decoded_samples*block;
  2156. }
  2157. #endif
  2158. return -1; // error
  2159. }
  2160. /******************************************************************************/
  2161. // OGG OPUS ENCODER
  2162. /******************************************************************************/
  2163. struct _OggOpusEncoder
  2164. {
  2165. ogg_stream_state os;
  2166. ogg_page og;
  2167. ogg_packet op;
  2168. OpusEncoder oe;
  2169. File *f;
  2170. void zero() {Zero(os); Zero(og); Zero(op); f=null;}
  2171. _OggOpusEncoder() {zero();}
  2172. ~_OggOpusEncoder() {del();}
  2173. void del()
  2174. {
  2175. ogg_stream_clear(&os);
  2176. oe.del();
  2177. zero();
  2178. }
  2179. Bool init(File &f, Int channels, Int bit_rate, Bool vbr)
  2180. {
  2181. del();
  2182. if(oe.create(channels, false))
  2183. {
  2184. oe.bitRate(bit_rate).vbr(vbr);
  2185. ogg_stream_init(&os, Random());
  2186. #pragma pack(push, 1)
  2187. struct OggHeader
  2188. {
  2189. Char8 magicSignature[8];
  2190. Byte version, channelCount;
  2191. U16 preSkip;
  2192. UInt sampleRate;
  2193. I16 outputGain;
  2194. Byte mappingFamily;
  2195. }header=
  2196. {
  2197. {'O', 'p', 'u', 's', 'H', 'e', 'a', 'd'},
  2198. 1, // ver
  2199. (Byte)channels,
  2200. (U16 )oe.delay(), // pre skip
  2201. OPUS_FREQUENCY, // frequency
  2202. 0, // output gain
  2203. 0, // channel mapping family
  2204. };
  2205. op.packet=(Byte*)&header;
  2206. op.bytes =SIZE(header);
  2207. op.b_o_s =true;
  2208. ogg_stream_packetin(&os, &op);
  2209. struct OggCommentHeader
  2210. {
  2211. Byte magicSignature[8];
  2212. U32 vendorStringLength;
  2213. //Char8 vendorString[];
  2214. U32 userCommentListLength;
  2215. //comments..
  2216. }comment=
  2217. {
  2218. {'O', 'p', 'u', 's', 'T', 'a', 'g', 's'},
  2219. 0,
  2220. 0,
  2221. };
  2222. op.packet=(Byte*)&comment;
  2223. op.bytes =SIZE(comment);
  2224. op.b_o_s =false;
  2225. op.packetno++;
  2226. ogg_stream_packetin(&os, &op);
  2227. #pragma pack(pop)
  2228. for(;;)
  2229. {
  2230. if(!ogg_stream_flush(&os, &og))break;
  2231. f.put(og.header, og.header_len);
  2232. f.put(og. body, og. body_len);
  2233. }
  2234. if(f.ok()){T.f=&f; return true;}
  2235. }
  2236. //error:
  2237. del(); return false;
  2238. }
  2239. Bool encodeEx(CPtr data, Int size)
  2240. {
  2241. if(f)
  2242. {
  2243. Memt<Byte> compressed_data;
  2244. Memt<Int > packet_sizes;
  2245. Int flushed_samples;
  2246. if(data ? !oe.encode(data, size, compressed_data, packet_sizes)
  2247. : !oe.flush (flushed_samples, compressed_data, packet_sizes))return false;
  2248. Byte *save_data=compressed_data.data();
  2249. if(!data && !packet_sizes.elms())packet_sizes.add(0); // add a dummy packet if flushing but there's no audio
  2250. Int final_packet=(data ? -1 : packet_sizes.elms()-1); // index of the final packet in stream (only set when flushing)
  2251. FREPA(packet_sizes)
  2252. {
  2253. Bool last=(i==final_packet);
  2254. op.packet=save_data;
  2255. op.bytes =packet_sizes[i]; save_data+=op.bytes;
  2256. op.packetno++;
  2257. if(last)
  2258. {
  2259. op.e_o_s=true;
  2260. op.granulepos+=flushed_samples%oe.frameSamples(); // last packet had only few samples
  2261. }else op.granulepos+= oe.frameSamples();
  2262. ogg_stream_packetin(&os, &op);
  2263. for(;;)
  2264. {
  2265. if(last ? !ogg_stream_flush (&os, &og)
  2266. : !ogg_stream_pageout(&os, &og))break;
  2267. f->put(og.header, og.header_len);
  2268. f->put(og. body, og. body_len);
  2269. }
  2270. }
  2271. return true;
  2272. }
  2273. return false;
  2274. }
  2275. Bool encode(CPtr data, Int size)
  2276. {
  2277. for(Byte *d=(Byte*)data; size>0; ) // encode in chunks to avoid big memory allocation inside 'OpusEncoder' for encoded data
  2278. {
  2279. Int write=Min(size, 65536);
  2280. if(!encodeEx(d, write))return false;
  2281. d +=write;
  2282. size-=write;
  2283. }
  2284. return true;
  2285. }
  2286. Bool finish()
  2287. {
  2288. if(f)
  2289. {
  2290. Bool ok=encodeEx(null, 0); // encode end of stream, use 'encodeEx' because 'encode' will do nothing for 0 size
  2291. f=null;
  2292. return ok;
  2293. }
  2294. return true;
  2295. }
  2296. };
  2297. /******************************************************************************/
  2298. OggOpusEncoder& OggOpusEncoder::del()
  2299. {
  2300. #if SUPPORT_OPUS_ENC
  2301. if(_encoder)
  2302. {
  2303. ((_OggOpusEncoder*)_encoder)->finish();
  2304. Delete((_OggOpusEncoder*&)_encoder);
  2305. }
  2306. #endif
  2307. return T;
  2308. }
  2309. Bool OggOpusEncoder::create(File &f, Int channels, Int bit_rate, Bool vbr)
  2310. {
  2311. #if SUPPORT_OPUS_ENC
  2312. if(_encoder)
  2313. {
  2314. ((_OggOpusEncoder*)_encoder)->finish();
  2315. }else
  2316. {
  2317. New((_OggOpusEncoder*&)_encoder);
  2318. }
  2319. if(((_OggOpusEncoder*)_encoder)->init(f, channels, bit_rate, vbr))return true;
  2320. Delete((_OggOpusEncoder*&)_encoder);
  2321. #endif
  2322. return false;
  2323. }
  2324. Bool OggOpusEncoder::encode(CPtr data, Int size)
  2325. {
  2326. #if SUPPORT_OPUS_ENC
  2327. if(!size)return true;
  2328. if(!data || size<=0 || !_encoder)return false;
  2329. return ((_OggOpusEncoder*)_encoder)->encode(data, size);
  2330. #else
  2331. return false;
  2332. #endif
  2333. }
  2334. Bool OggOpusEncoder::finish()
  2335. {
  2336. #if SUPPORT_OPUS_ENC
  2337. return _encoder ? ((_OggOpusEncoder*)_encoder)->finish() : true;
  2338. #else
  2339. return false;
  2340. #endif
  2341. }
  2342. /******************************************************************************/
  2343. // SND OPUS ENCODER
  2344. /******************************************************************************/
  2345. SndOpusEncoder::SndOpusEncoder() {zero();}
  2346. void SndOpusEncoder::zero() {_f=null; _packet_sizes_pos=0; _packet_sizes_expected=0; _frequency=0;}
  2347. SndOpusEncoder& SndOpusEncoder::del () {finish(); return T;}
  2348. Bool SndOpusEncoder::create(File &f, Long samples, Int frequency, Int channels, Int bit_rate, Bool vbr)
  2349. {
  2350. del();
  2351. if(samples>=0 && frequency>=0 && _encoder.create(channels, false, frequency) && SaveSndHeader(f, SND_OPUS, channels, frequency, samples))
  2352. {
  2353. _f=&f;
  2354. _frequency=frequency; bitRate(bit_rate); // set bit rate after frequency
  2355. _encoder.vbr(vbr);
  2356. Int delay=_encoder.delay(); f.cmpUIntV(delay);
  2357. ULong frames=DivCeil(ULong(samples+delay), (ULong)frameSamples()); // calculate number of frames
  2358. _packet_sizes_pos =f.pos();
  2359. _packet_sizes_expected=frames;
  2360. f.put(null, SIZE(U16)*frames); // make room for frame sizes (can use U16 sizes, because max frame size is 4096)
  2361. return f.ok();
  2362. }
  2363. return false;
  2364. }
  2365. void SndOpusEncoder::write(Memt<Byte> &compressed_data, Memt<Int> &packet_sizes)
  2366. {
  2367. Byte *data=compressed_data.data();
  2368. FREPA(packet_sizes)
  2369. {
  2370. Int size=packet_sizes[i];
  2371. _packet_sizes.add(size);
  2372. _f->put(data, size);
  2373. data+=size;
  2374. }
  2375. }
  2376. Bool SndOpusEncoder::encode(CPtr data, Int size)
  2377. {
  2378. if(_f)
  2379. {
  2380. Memt<Byte> compressed_data;
  2381. Memt<Int > packet_sizes;
  2382. for(Byte *d=(Byte*)data; size>0; ) // encode in chunks to avoid big memory allocation inside 'OpusEncoder' for encoded data
  2383. {
  2384. Int s=Min(size, 65536);
  2385. if(!_encoder.encode(d, s, compressed_data, packet_sizes)){del(); return false;}
  2386. write(compressed_data, packet_sizes);
  2387. d +=s;
  2388. size-=s;
  2389. }
  2390. return _f->ok();
  2391. }
  2392. return false;
  2393. }
  2394. Bool SndOpusEncoder::finish()
  2395. {
  2396. Bool ok=false;
  2397. if(_f)
  2398. {
  2399. Int flushed_samples;
  2400. Memt<Byte> compressed_data;
  2401. Memt<Int > packet_sizes;
  2402. ok=_encoder.flush(flushed_samples, compressed_data, packet_sizes);
  2403. write(compressed_data, packet_sizes);
  2404. Long pos=_f->pos();
  2405. if(_f->pos(_packet_sizes_pos))
  2406. {
  2407. ok&=(_packet_sizes_expected==_packet_sizes.elms());
  2408. _f->putN(_packet_sizes.data(), Min(_packet_sizes_expected, _packet_sizes.elms())); // limit to what was written and what was expected (leave remaining as zeros if any)
  2409. }else ok=false;
  2410. ok&=(_f->pos(pos) && _f->ok());
  2411. }
  2412. _encoder.del();
  2413. _packet_sizes.clear();
  2414. zero();
  2415. return ok;
  2416. }
  2417. SndOpusEncoder& SndOpusEncoder::bitRate(Int bit_rate)
  2418. {
  2419. if(frequency()>0 && bit_rate>=0)
  2420. {
  2421. bit_rate=DivRound(ULong(bit_rate)*_encoder.frequency(), (ULong)frequency()); // currently sounds are encoded without resampling, always at '_encoder.frequency()', so for lower frequencies we actually need to increase the bit-rate
  2422. _encoder.bitRate(bit_rate);
  2423. }
  2424. return T;
  2425. }
  2426. Int SndOpusEncoder::bitRate()C
  2427. {
  2428. if(Int enc_freq=_encoder.frequency())return DivRound(ULong(_encoder.bitRate())*frequency(), (ULong)enc_freq);
  2429. return 0;
  2430. }
  2431. /******************************************************************************/
  2432. // SND RAW ENCODER
  2433. /******************************************************************************/
  2434. SndRawEncoder::SndRawEncoder() {zero();}
  2435. void SndRawEncoder::zero() {_f=null; _block=0; _samples_pos=-1;}
  2436. SndRawEncoder& SndRawEncoder::del () {finish(); return T;}
  2437. Bool SndRawEncoder::create(File &f, Int frequency, Int channels, Long samples)
  2438. {
  2439. del();
  2440. if(SaveSndHeader(f, SND_RAW_16, channels, frequency, (samples<0) ? -1 : samples)) // always use -1 for unknown to reserve max possible size for sample count
  2441. {
  2442. if(samples<0)_samples_pos=f.pos(); // if unknown then remember position to write later
  2443. _block=channels*2; // 16-bit samples
  2444. _f=&f;
  2445. return f.ok();
  2446. }
  2447. return false;
  2448. }
  2449. Bool SndRawEncoder::encode(CPtr data, Int size)
  2450. {
  2451. return _f ? _f->put(data, size) : false;
  2452. }
  2453. Bool SndRawEncoder::finish()
  2454. {
  2455. Bool ok=false;
  2456. if(_f)
  2457. {
  2458. ok=true;
  2459. if(_samples_pos>=0) // need to write sample count
  2460. {
  2461. ok=false;
  2462. Long pos=_f->pos(); if(_f->pos(_samples_pos-MaxCmpULongVSize))
  2463. {
  2464. ULong samples=(_block ? (pos-_samples_pos)/_block : 0);
  2465. _f->cmpULongVMax(samples);
  2466. ok=(_f->pos(pos) && _f->ok());
  2467. }
  2468. }
  2469. }
  2470. zero(); return ok;
  2471. }
  2472. /******************************************************************************/
  2473. // WAV ENCODER
  2474. /******************************************************************************/
  2475. WavEncoder::WavEncoder() {zero();}
  2476. void WavEncoder::zero() {_f=null; _size_pos=-1;}
  2477. WavEncoder& WavEncoder::del () {finish(); return T;}
  2478. Bool WavEncoder::create(File &f, Int bits, Int frequency, Int channels, Long samples)
  2479. {
  2480. del();
  2481. if(SaveWavHeader(f, bits, channels, frequency, samples*channels*bits/8))
  2482. {
  2483. if(samples<0)_size_pos=f.pos(); // if unknown then remember position to write later
  2484. _f=&f;
  2485. return f.ok();
  2486. }
  2487. return false;
  2488. }
  2489. Bool WavEncoder::encode(CPtr data, Int size)
  2490. {
  2491. return _f ? _f->put(data, size) : false;
  2492. }
  2493. Bool WavEncoder::finish()
  2494. {
  2495. Bool ok=false;
  2496. if(_f)
  2497. {
  2498. ok=true;
  2499. if(_size_pos>=0) // need to write sample count
  2500. {
  2501. ok=false;
  2502. Long pos=_f->pos(); if(_f->pos(_size_pos-44+4))
  2503. {
  2504. UInt size=pos-_size_pos;
  2505. _f->putUInt(size+36);
  2506. if(_f->pos(_size_pos-4))_f->putUInt(size);
  2507. ok=(_f->pos(pos) && _f->ok());
  2508. }
  2509. }
  2510. }
  2511. zero(); return ok;
  2512. }
  2513. /******************************************************************************/
  2514. // MAIN
  2515. /******************************************************************************/
  2516. void InitStream()
  2517. {
  2518. #if SUPPORT_VORBIS
  2519. VorbisCallbacks. read_func=VorbisRead;
  2520. VorbisCallbacks. seek_func=VorbisSeek;
  2521. VorbisCallbacks. tell_func=VorbisTell;
  2522. VorbisCallbacks.close_func=VorbisClose;
  2523. #endif
  2524. #if SUPPORT_OPUS
  2525. OpusCallbacks.read =OpusRead;
  2526. OpusCallbacks.seek =OpusSeek;
  2527. OpusCallbacks.tell =OpusTell;
  2528. OpusCallbacks.close=OpusClose;
  2529. #endif
  2530. }
  2531. void ShutStream()
  2532. {
  2533. }
  2534. /******************************************************************************/
  2535. }
  2536. /******************************************************************************/