aurora.js 116 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937
  1. !function(e){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{var f;"undefined"!=typeof window?f=window:"undefined"!=typeof global?f=global:"undefined"!=typeof self&&(f=self),f.AV=e()}}(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);throw new Error("Cannot find module '"+o+"'")}var f=n[o]={exports:{}};t[o][0].call(f.exports,function(e){var n=t[o][1][e];return s(n?n:e)},f,f.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(_dereq_,module,exports){
  2. var key, val, _ref;
  3. _ref = _dereq_('./src/aurora');
  4. for (key in _ref) {
  5. val = _ref[key];
  6. exports[key] = val;
  7. }
  8. _dereq_('./src/devices/webaudio');
  9. _dereq_('./src/devices/mozilla');
  10. },{"./src/aurora":3,"./src/devices/mozilla":22,"./src/devices/webaudio":24}],2:[function(_dereq_,module,exports){
  11. var Asset, BufferSource, Decoder, Demuxer, EventEmitter, FileSource, HTTPSource,
  12. __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; },
  13. __hasProp = {}.hasOwnProperty,
  14. __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
  15. EventEmitter = _dereq_('./core/events');
  16. HTTPSource = _dereq_('./sources/node/http');
  17. FileSource = _dereq_('./sources/node/file');
  18. BufferSource = _dereq_('./sources/buffer');
  19. Demuxer = _dereq_('./demuxer');
  20. Decoder = _dereq_('./decoder');
  21. Asset = (function(_super) {
  22. __extends(Asset, _super);
  23. function Asset(source) {
  24. this.source = source;
  25. this._decode = __bind(this._decode, this);
  26. this.findDecoder = __bind(this.findDecoder, this);
  27. this.probe = __bind(this.probe, this);
  28. this.buffered = 0;
  29. this.duration = null;
  30. this.format = null;
  31. this.metadata = null;
  32. this.active = false;
  33. this.demuxer = null;
  34. this.decoder = null;
  35. this.source.once('data', this.probe);
  36. this.source.on('error', (function(_this) {
  37. return function(err) {
  38. _this.emit('error', err);
  39. return _this.stop();
  40. };
  41. })(this));
  42. this.source.on('progress', (function(_this) {
  43. return function(buffered) {
  44. _this.buffered = buffered;
  45. return _this.emit('buffer', _this.buffered);
  46. };
  47. })(this));
  48. }
  49. Asset.fromURL = function(url) {
  50. return new Asset(new HTTPSource(url));
  51. };
  52. Asset.fromFile = function(file) {
  53. return new Asset(new FileSource(file));
  54. };
  55. Asset.fromBuffer = function(buffer) {
  56. return new Asset(new BufferSource(buffer));
  57. };
  58. Asset.prototype.start = function(decode) {
  59. if (this.active) {
  60. return;
  61. }
  62. if (decode != null) {
  63. this.shouldDecode = decode;
  64. }
  65. if (this.shouldDecode == null) {
  66. this.shouldDecode = true;
  67. }
  68. this.active = true;
  69. this.source.start();
  70. if (this.decoder && this.shouldDecode) {
  71. return this._decode();
  72. }
  73. };
  74. Asset.prototype.stop = function() {
  75. if (!this.active) {
  76. return;
  77. }
  78. this.active = false;
  79. return this.source.pause();
  80. };
  81. Asset.prototype.get = function(event, callback) {
  82. if (event !== 'format' && event !== 'duration' && event !== 'metadata') {
  83. return;
  84. }
  85. if (this[event] != null) {
  86. return callback(this[event]);
  87. } else {
  88. this.once(event, (function(_this) {
  89. return function(value) {
  90. _this.stop();
  91. return callback(value);
  92. };
  93. })(this));
  94. return this.start();
  95. }
  96. };
  97. Asset.prototype.decodePacket = function() {
  98. return this.decoder.decode();
  99. };
  100. Asset.prototype.decodeToBuffer = function(callback) {
  101. var chunks, dataHandler, length;
  102. length = 0;
  103. chunks = [];
  104. this.on('data', dataHandler = function(chunk) {
  105. length += chunk.length;
  106. return chunks.push(chunk);
  107. });
  108. this.once('end', function() {
  109. var buf, chunk, offset, _i, _len;
  110. buf = new Float32Array(length);
  111. offset = 0;
  112. for (_i = 0, _len = chunks.length; _i < _len; _i++) {
  113. chunk = chunks[_i];
  114. buf.set(chunk, offset);
  115. offset += chunk.length;
  116. }
  117. this.off('data', dataHandler);
  118. return callback(buf);
  119. });
  120. return this.start();
  121. };
  122. Asset.prototype.probe = function(chunk) {
  123. var demuxer;
  124. if (!this.active) {
  125. return;
  126. }
  127. demuxer = Demuxer.find(chunk);
  128. if (!demuxer) {
  129. return this.emit('error', 'A demuxer for this container was not found.');
  130. }
  131. this.demuxer = new demuxer(this.source, chunk);
  132. this.demuxer.on('format', this.findDecoder);
  133. this.demuxer.on('duration', (function(_this) {
  134. return function(duration) {
  135. _this.duration = duration;
  136. return _this.emit('duration', _this.duration);
  137. };
  138. })(this));
  139. this.demuxer.on('metadata', (function(_this) {
  140. return function(metadata) {
  141. _this.metadata = metadata;
  142. return _this.emit('metadata', _this.metadata);
  143. };
  144. })(this));
  145. return this.demuxer.on('error', (function(_this) {
  146. return function(err) {
  147. _this.emit('error', err);
  148. return _this.stop();
  149. };
  150. })(this));
  151. };
  152. Asset.prototype.findDecoder = function(format) {
  153. var decoder, div;
  154. this.format = format;
  155. if (!this.active) {
  156. return;
  157. }
  158. this.emit('format', this.format);
  159. decoder = Decoder.find(this.format.formatID);
  160. if (!decoder) {
  161. return this.emit('error', "A decoder for " + this.format.formatID + " was not found.");
  162. }
  163. this.decoder = new decoder(this.demuxer, this.format);
  164. if (this.format.floatingPoint) {
  165. this.decoder.on('data', (function(_this) {
  166. return function(buffer) {
  167. return _this.emit('data', buffer);
  168. };
  169. })(this));
  170. } else {
  171. div = Math.pow(2, this.format.bitsPerChannel - 1);
  172. this.decoder.on('data', (function(_this) {
  173. return function(buffer) {
  174. var buf, i, sample, _i, _len;
  175. buf = new Float32Array(buffer.length);
  176. for (i = _i = 0, _len = buffer.length; _i < _len; i = ++_i) {
  177. sample = buffer[i];
  178. buf[i] = sample / div;
  179. }
  180. return _this.emit('data', buf);
  181. };
  182. })(this));
  183. }
  184. this.decoder.on('error', (function(_this) {
  185. return function(err) {
  186. _this.emit('error', err);
  187. return _this.stop();
  188. };
  189. })(this));
  190. this.decoder.on('end', (function(_this) {
  191. return function() {
  192. return _this.emit('end');
  193. };
  194. })(this));
  195. this.emit('decodeStart');
  196. if (this.shouldDecode) {
  197. return this._decode();
  198. }
  199. };
  200. Asset.prototype._decode = function() {
  201. while (this.decoder.decode() && this.active) {
  202. continue;
  203. }
  204. if (this.active) {
  205. return this.decoder.once('data', this._decode);
  206. }
  207. };
  208. return Asset;
  209. })(EventEmitter);
  210. module.exports = Asset;
  211. },{"./core/events":9,"./decoder":12,"./demuxer":15,"./sources/buffer":32,"./sources/node/file":30,"./sources/node/http":31}],3:[function(_dereq_,module,exports){
  212. var key, val, _ref;
  213. _ref = _dereq_('./aurora_base');
  214. for (key in _ref) {
  215. val = _ref[key];
  216. exports[key] = val;
  217. }
  218. _dereq_('./demuxers/caf');
  219. _dereq_('./demuxers/m4a');
  220. _dereq_('./demuxers/aiff');
  221. _dereq_('./demuxers/wave');
  222. _dereq_('./demuxers/au');
  223. _dereq_('./decoders/lpcm');
  224. _dereq_('./decoders/xlaw');
  225. },{"./aurora_base":4,"./decoders/lpcm":13,"./decoders/xlaw":14,"./demuxers/aiff":16,"./demuxers/au":17,"./demuxers/caf":18,"./demuxers/m4a":19,"./demuxers/wave":20}],4:[function(_dereq_,module,exports){
  226. exports.Base = _dereq_('./core/base');
  227. exports.Buffer = _dereq_('./core/buffer');
  228. exports.BufferList = _dereq_('./core/bufferlist');
  229. exports.Stream = _dereq_('./core/stream');
  230. exports.Bitstream = _dereq_('./core/bitstream');
  231. exports.EventEmitter = _dereq_('./core/events');
  232. exports.UnderflowError = _dereq_('./core/underflow');
  233. exports.HTTPSource = _dereq_('./sources/node/http');
  234. exports.FileSource = _dereq_('./sources/node/file');
  235. exports.BufferSource = _dereq_('./sources/buffer');
  236. exports.Demuxer = _dereq_('./demuxer');
  237. exports.Decoder = _dereq_('./decoder');
  238. exports.AudioDevice = _dereq_('./device');
  239. exports.Asset = _dereq_('./asset');
  240. exports.Player = _dereq_('./player');
  241. exports.Filter = _dereq_('./filter');
  242. exports.VolumeFilter = _dereq_('./filters/volume');
  243. exports.BalanceFilter = _dereq_('./filters/balance');
  244. },{"./asset":2,"./core/base":5,"./core/bitstream":6,"./core/buffer":7,"./core/bufferlist":8,"./core/events":9,"./core/stream":10,"./core/underflow":11,"./decoder":12,"./demuxer":15,"./device":21,"./filter":25,"./filters/balance":26,"./filters/volume":27,"./player":28,"./sources/buffer":32,"./sources/node/file":30,"./sources/node/http":31}],5:[function(_dereq_,module,exports){
  245. var Base,
  246. __hasProp = {}.hasOwnProperty,
  247. __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
  248. __indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; };
  249. Base = (function() {
  250. var fnTest;
  251. function Base() {}
  252. fnTest = /\b_super\b/;
  253. Base.extend = function(prop) {
  254. var Class, fn, key, keys, _ref, _super;
  255. Class = (function(_super) {
  256. __extends(Class, _super);
  257. function Class() {
  258. return Class.__super__.constructor.apply(this, arguments);
  259. }
  260. return Class;
  261. })(this);
  262. if (typeof prop === 'function') {
  263. keys = Object.keys(Class.prototype);
  264. prop.call(Class, Class);
  265. prop = {};
  266. _ref = Class.prototype;
  267. for (key in _ref) {
  268. fn = _ref[key];
  269. if (__indexOf.call(keys, key) < 0) {
  270. prop[key] = fn;
  271. }
  272. }
  273. }
  274. _super = Class.__super__;
  275. for (key in prop) {
  276. fn = prop[key];
  277. if (typeof fn === 'function' && fnTest.test(fn)) {
  278. (function(key, fn) {
  279. return Class.prototype[key] = function() {
  280. var ret, tmp;
  281. tmp = this._super;
  282. this._super = _super[key];
  283. ret = fn.apply(this, arguments);
  284. this._super = tmp;
  285. return ret;
  286. };
  287. })(key, fn);
  288. } else {
  289. Class.prototype[key] = fn;
  290. }
  291. }
  292. return Class;
  293. };
  294. return Base;
  295. })();
  296. module.exports = Base;
  297. },{}],6:[function(_dereq_,module,exports){
  298. var Bitstream;
  299. Bitstream = (function() {
  300. function Bitstream(stream) {
  301. this.stream = stream;
  302. this.bitPosition = 0;
  303. }
  304. Bitstream.prototype.copy = function() {
  305. var result;
  306. result = new Bitstream(this.stream.copy());
  307. result.bitPosition = this.bitPosition;
  308. return result;
  309. };
  310. Bitstream.prototype.offset = function() {
  311. return 8 * this.stream.offset + this.bitPosition;
  312. };
  313. Bitstream.prototype.available = function(bits) {
  314. return this.stream.available((bits + 8 - this.bitPosition) / 8);
  315. };
  316. Bitstream.prototype.advance = function(bits) {
  317. var pos;
  318. pos = this.bitPosition + bits;
  319. this.stream.advance(pos >> 3);
  320. return this.bitPosition = pos & 7;
  321. };
  322. Bitstream.prototype.rewind = function(bits) {
  323. var pos;
  324. pos = this.bitPosition - bits;
  325. this.stream.rewind(Math.abs(pos >> 3));
  326. return this.bitPosition = pos & 7;
  327. };
  328. Bitstream.prototype.seek = function(offset) {
  329. var curOffset;
  330. curOffset = this.offset();
  331. if (offset > curOffset) {
  332. return this.advance(offset - curOffset);
  333. } else if (offset < curOffset) {
  334. return this.rewind(curOffset - offset);
  335. }
  336. };
  337. Bitstream.prototype.align = function() {
  338. if (this.bitPosition !== 0) {
  339. this.bitPosition = 0;
  340. return this.stream.advance(1);
  341. }
  342. };
  343. Bitstream.prototype.read = function(bits, signed) {
  344. var a, a0, a1, a2, a3, a4, mBits;
  345. if (bits === 0) {
  346. return 0;
  347. }
  348. mBits = bits + this.bitPosition;
  349. if (mBits <= 8) {
  350. a = ((this.stream.peekUInt8() << this.bitPosition) & 0xff) >>> (8 - bits);
  351. } else if (mBits <= 16) {
  352. a = ((this.stream.peekUInt16() << this.bitPosition) & 0xffff) >>> (16 - bits);
  353. } else if (mBits <= 24) {
  354. a = ((this.stream.peekUInt24() << this.bitPosition) & 0xffffff) >>> (24 - bits);
  355. } else if (mBits <= 32) {
  356. a = (this.stream.peekUInt32() << this.bitPosition) >>> (32 - bits);
  357. } else if (mBits <= 40) {
  358. a0 = this.stream.peekUInt8(0) * 0x0100000000;
  359. a1 = this.stream.peekUInt8(1) << 24 >>> 0;
  360. a2 = this.stream.peekUInt8(2) << 16;
  361. a3 = this.stream.peekUInt8(3) << 8;
  362. a4 = this.stream.peekUInt8(4);
  363. a = a0 + a1 + a2 + a3 + a4;
  364. a %= Math.pow(2, 40 - this.bitPosition);
  365. a = Math.floor(a / Math.pow(2, 40 - this.bitPosition - bits));
  366. } else {
  367. throw new Error("Too many bits!");
  368. }
  369. if (signed) {
  370. if (mBits < 32) {
  371. if (a >>> (bits - 1)) {
  372. a = ((1 << bits >>> 0) - a) * -1;
  373. }
  374. } else {
  375. if (a / Math.pow(2, bits - 1) | 0) {
  376. a = (Math.pow(2, bits) - a) * -1;
  377. }
  378. }
  379. }
  380. this.advance(bits);
  381. return a;
  382. };
  383. Bitstream.prototype.peek = function(bits, signed) {
  384. var a, a0, a1, a2, a3, a4, mBits;
  385. if (bits === 0) {
  386. return 0;
  387. }
  388. mBits = bits + this.bitPosition;
  389. if (mBits <= 8) {
  390. a = ((this.stream.peekUInt8() << this.bitPosition) & 0xff) >>> (8 - bits);
  391. } else if (mBits <= 16) {
  392. a = ((this.stream.peekUInt16() << this.bitPosition) & 0xffff) >>> (16 - bits);
  393. } else if (mBits <= 24) {
  394. a = ((this.stream.peekUInt24() << this.bitPosition) & 0xffffff) >>> (24 - bits);
  395. } else if (mBits <= 32) {
  396. a = (this.stream.peekUInt32() << this.bitPosition) >>> (32 - bits);
  397. } else if (mBits <= 40) {
  398. a0 = this.stream.peekUInt8(0) * 0x0100000000;
  399. a1 = this.stream.peekUInt8(1) << 24 >>> 0;
  400. a2 = this.stream.peekUInt8(2) << 16;
  401. a3 = this.stream.peekUInt8(3) << 8;
  402. a4 = this.stream.peekUInt8(4);
  403. a = a0 + a1 + a2 + a3 + a4;
  404. a %= Math.pow(2, 40 - this.bitPosition);
  405. a = Math.floor(a / Math.pow(2, 40 - this.bitPosition - bits));
  406. } else {
  407. throw new Error("Too many bits!");
  408. }
  409. if (signed) {
  410. if (mBits < 32) {
  411. if (a >>> (bits - 1)) {
  412. a = ((1 << bits >>> 0) - a) * -1;
  413. }
  414. } else {
  415. if (a / Math.pow(2, bits - 1) | 0) {
  416. a = (Math.pow(2, bits) - a) * -1;
  417. }
  418. }
  419. }
  420. return a;
  421. };
  422. Bitstream.prototype.readLSB = function(bits, signed) {
  423. var a, mBits;
  424. if (bits === 0) {
  425. return 0;
  426. }
  427. if (bits > 40) {
  428. throw new Error("Too many bits!");
  429. }
  430. mBits = bits + this.bitPosition;
  431. a = (this.stream.peekUInt8(0)) >>> this.bitPosition;
  432. if (mBits > 8) {
  433. a |= (this.stream.peekUInt8(1)) << (8 - this.bitPosition);
  434. }
  435. if (mBits > 16) {
  436. a |= (this.stream.peekUInt8(2)) << (16 - this.bitPosition);
  437. }
  438. if (mBits > 24) {
  439. a += (this.stream.peekUInt8(3)) << (24 - this.bitPosition) >>> 0;
  440. }
  441. if (mBits > 32) {
  442. a += (this.stream.peekUInt8(4)) * Math.pow(2, 32 - this.bitPosition);
  443. }
  444. if (mBits >= 32) {
  445. a %= Math.pow(2, bits);
  446. } else {
  447. a &= (1 << bits) - 1;
  448. }
  449. if (signed) {
  450. if (mBits < 32) {
  451. if (a >>> (bits - 1)) {
  452. a = ((1 << bits >>> 0) - a) * -1;
  453. }
  454. } else {
  455. if (a / Math.pow(2, bits - 1) | 0) {
  456. a = (Math.pow(2, bits) - a) * -1;
  457. }
  458. }
  459. }
  460. this.advance(bits);
  461. return a;
  462. };
  463. Bitstream.prototype.peekLSB = function(bits, signed) {
  464. var a, mBits;
  465. if (bits === 0) {
  466. return 0;
  467. }
  468. if (bits > 40) {
  469. throw new Error("Too many bits!");
  470. }
  471. mBits = bits + this.bitPosition;
  472. a = (this.stream.peekUInt8(0)) >>> this.bitPosition;
  473. if (mBits > 8) {
  474. a |= (this.stream.peekUInt8(1)) << (8 - this.bitPosition);
  475. }
  476. if (mBits > 16) {
  477. a |= (this.stream.peekUInt8(2)) << (16 - this.bitPosition);
  478. }
  479. if (mBits > 24) {
  480. a += (this.stream.peekUInt8(3)) << (24 - this.bitPosition) >>> 0;
  481. }
  482. if (mBits > 32) {
  483. a += (this.stream.peekUInt8(4)) * Math.pow(2, 32 - this.bitPosition);
  484. }
  485. if (mBits >= 32) {
  486. a %= Math.pow(2, bits);
  487. } else {
  488. a &= (1 << bits) - 1;
  489. }
  490. if (signed) {
  491. if (mBits < 32) {
  492. if (a >>> (bits - 1)) {
  493. a = ((1 << bits >>> 0) - a) * -1;
  494. }
  495. } else {
  496. if (a / Math.pow(2, bits - 1) | 0) {
  497. a = (Math.pow(2, bits) - a) * -1;
  498. }
  499. }
  500. }
  501. return a;
  502. };
  503. return Bitstream;
  504. })();
  505. module.exports = Bitstream;
  506. },{}],7:[function(_dereq_,module,exports){
  507. (function (global){
  508. var AVBuffer;
  509. AVBuffer = (function() {
  510. var BlobBuilder, URL;
  511. function AVBuffer(input) {
  512. var _ref;
  513. if (input instanceof Uint8Array) {
  514. this.data = input;
  515. } else if (input instanceof ArrayBuffer || Array.isArray(input) || typeof input === 'number' || ((_ref = global.Buffer) != null ? _ref.isBuffer(input) : void 0)) {
  516. this.data = new Uint8Array(input);
  517. } else if (input.buffer instanceof ArrayBuffer) {
  518. this.data = new Uint8Array(input.buffer, input.byteOffset, input.length * input.BYTES_PER_ELEMENT);
  519. } else if (input instanceof AVBuffer) {
  520. this.data = input.data;
  521. } else {
  522. throw new Error("Constructing buffer with unknown type.");
  523. }
  524. this.length = this.data.length;
  525. this.next = null;
  526. this.prev = null;
  527. }
  528. AVBuffer.allocate = function(size) {
  529. return new AVBuffer(size);
  530. };
  531. AVBuffer.prototype.copy = function() {
  532. return new AVBuffer(new Uint8Array(this.data));
  533. };
  534. AVBuffer.prototype.slice = function(position, length) {
  535. if (length == null) {
  536. length = this.length;
  537. }
  538. if (position === 0 && length >= this.length) {
  539. return new AVBuffer(this.data);
  540. } else {
  541. return new AVBuffer(this.data.subarray(position, position + length));
  542. }
  543. };
  544. BlobBuilder = global.BlobBuilder || global.MozBlobBuilder || global.WebKitBlobBuilder;
  545. URL = global.URL || global.webkitURL || global.mozURL;
  546. AVBuffer.makeBlob = function(data, type) {
  547. var bb;
  548. if (type == null) {
  549. type = 'application/octet-stream';
  550. }
  551. try {
  552. return new Blob([data], {
  553. type: type
  554. });
  555. } catch (_error) {}
  556. if (BlobBuilder != null) {
  557. bb = new BlobBuilder;
  558. bb.append(data);
  559. return bb.getBlob(type);
  560. }
  561. return null;
  562. };
  563. AVBuffer.makeBlobURL = function(data, type) {
  564. return URL != null ? URL.createObjectURL(this.makeBlob(data, type)) : void 0;
  565. };
  566. AVBuffer.revokeBlobURL = function(url) {
  567. return URL != null ? URL.revokeObjectURL(url) : void 0;
  568. };
  569. AVBuffer.prototype.toBlob = function() {
  570. return AVBuffer.makeBlob(this.data.buffer);
  571. };
  572. AVBuffer.prototype.toBlobURL = function() {
  573. return AVBuffer.makeBlobURL(this.data.buffer);
  574. };
  575. return AVBuffer;
  576. })();
  577. module.exports = AVBuffer;
  578. }).call(this,typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {})
  579. },{}],8:[function(_dereq_,module,exports){
  580. var BufferList;
  581. BufferList = (function() {
  582. function BufferList() {
  583. this.first = null;
  584. this.last = null;
  585. this.numBuffers = 0;
  586. this.availableBytes = 0;
  587. this.availableBuffers = 0;
  588. }
  589. BufferList.prototype.copy = function() {
  590. var result;
  591. result = new BufferList;
  592. result.first = this.first;
  593. result.last = this.last;
  594. result.numBuffers = this.numBuffers;
  595. result.availableBytes = this.availableBytes;
  596. result.availableBuffers = this.availableBuffers;
  597. return result;
  598. };
  599. BufferList.prototype.append = function(buffer) {
  600. var _ref;
  601. buffer.prev = this.last;
  602. if ((_ref = this.last) != null) {
  603. _ref.next = buffer;
  604. }
  605. this.last = buffer;
  606. if (this.first == null) {
  607. this.first = buffer;
  608. }
  609. this.availableBytes += buffer.length;
  610. this.availableBuffers++;
  611. return this.numBuffers++;
  612. };
  613. BufferList.prototype.advance = function() {
  614. if (this.first) {
  615. this.availableBytes -= this.first.length;
  616. this.availableBuffers--;
  617. this.first = this.first.next;
  618. return this.first != null;
  619. }
  620. return false;
  621. };
  622. BufferList.prototype.rewind = function() {
  623. var _ref;
  624. if (this.first && !this.first.prev) {
  625. return false;
  626. }
  627. this.first = ((_ref = this.first) != null ? _ref.prev : void 0) || this.last;
  628. if (this.first) {
  629. this.availableBytes += this.first.length;
  630. this.availableBuffers++;
  631. }
  632. return this.first != null;
  633. };
  634. BufferList.prototype.reset = function() {
  635. var _results;
  636. _results = [];
  637. while (this.rewind()) {
  638. continue;
  639. }
  640. return _results;
  641. };
  642. return BufferList;
  643. })();
  644. module.exports = BufferList;
  645. },{}],9:[function(_dereq_,module,exports){
  646. var Base, EventEmitter,
  647. __hasProp = {}.hasOwnProperty,
  648. __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
  649. __slice = [].slice;
  650. Base = _dereq_('./base');
  651. EventEmitter = (function(_super) {
  652. __extends(EventEmitter, _super);
  653. function EventEmitter() {
  654. return EventEmitter.__super__.constructor.apply(this, arguments);
  655. }
  656. EventEmitter.prototype.on = function(event, fn) {
  657. var _base;
  658. if (this.events == null) {
  659. this.events = {};
  660. }
  661. if ((_base = this.events)[event] == null) {
  662. _base[event] = [];
  663. }
  664. return this.events[event].push(fn);
  665. };
  666. EventEmitter.prototype.off = function(event, fn) {
  667. var index, _ref;
  668. if (!((_ref = this.events) != null ? _ref[event] : void 0)) {
  669. return;
  670. }
  671. index = this.events[event].indexOf(fn);
  672. if (~index) {
  673. return this.events[event].splice(index, 1);
  674. }
  675. };
  676. EventEmitter.prototype.once = function(event, fn) {
  677. var cb;
  678. return this.on(event, cb = function() {
  679. this.off(event, cb);
  680. return fn.apply(this, arguments);
  681. });
  682. };
  683. EventEmitter.prototype.emit = function() {
  684. var args, event, fn, _i, _len, _ref, _ref1;
  685. event = arguments[0], args = 2 <= arguments.length ? __slice.call(arguments, 1) : [];
  686. if (!((_ref = this.events) != null ? _ref[event] : void 0)) {
  687. return;
  688. }
  689. _ref1 = this.events[event].slice();
  690. for (_i = 0, _len = _ref1.length; _i < _len; _i++) {
  691. fn = _ref1[_i];
  692. fn.apply(this, args);
  693. }
  694. };
  695. return EventEmitter;
  696. })(Base);
  697. module.exports = EventEmitter;
  698. },{"./base":5}],10:[function(_dereq_,module,exports){
  699. var AVBuffer, BufferList, Stream, UnderflowError;
  700. BufferList = _dereq_('./bufferlist');
  701. AVBuffer = _dereq_('./buffer');
  702. UnderflowError = _dereq_('./underflow');
  703. Stream = (function() {
  704. var buf, decodeString, float32, float64, float64Fallback, float80, int16, int32, int8, nativeEndian, uint16, uint32, uint8;
  705. buf = new ArrayBuffer(16);
  706. uint8 = new Uint8Array(buf);
  707. int8 = new Int8Array(buf);
  708. uint16 = new Uint16Array(buf);
  709. int16 = new Int16Array(buf);
  710. uint32 = new Uint32Array(buf);
  711. int32 = new Int32Array(buf);
  712. float32 = new Float32Array(buf);
  713. if (typeof Float64Array !== "undefined" && Float64Array !== null) {
  714. float64 = new Float64Array(buf);
  715. }
  716. nativeEndian = new Uint16Array(new Uint8Array([0x12, 0x34]).buffer)[0] === 0x3412;
  717. function Stream(list) {
  718. this.list = list;
  719. this.localOffset = 0;
  720. this.offset = 0;
  721. }
  722. Stream.fromBuffer = function(buffer) {
  723. var list;
  724. list = new BufferList;
  725. list.append(buffer);
  726. return new Stream(list);
  727. };
  728. Stream.prototype.copy = function() {
  729. var result;
  730. result = new Stream(this.list.copy());
  731. result.localOffset = this.localOffset;
  732. result.offset = this.offset;
  733. return result;
  734. };
  735. Stream.prototype.available = function(bytes) {
  736. return bytes <= this.list.availableBytes - this.localOffset;
  737. };
  738. Stream.prototype.remainingBytes = function() {
  739. return this.list.availableBytes - this.localOffset;
  740. };
  741. Stream.prototype.advance = function(bytes) {
  742. if (!this.available(bytes)) {
  743. throw new UnderflowError();
  744. }
  745. this.localOffset += bytes;
  746. this.offset += bytes;
  747. while (this.list.first && this.localOffset >= this.list.first.length) {
  748. this.localOffset -= this.list.first.length;
  749. this.list.advance();
  750. }
  751. return this;
  752. };
  753. Stream.prototype.rewind = function(bytes) {
  754. if (bytes > this.offset) {
  755. throw new UnderflowError();
  756. }
  757. if (!this.list.first) {
  758. this.list.rewind();
  759. this.localOffset = this.list.first.length;
  760. }
  761. this.localOffset -= bytes;
  762. this.offset -= bytes;
  763. while (this.list.first.prev && this.localOffset < 0) {
  764. this.list.rewind();
  765. this.localOffset += this.list.first.length;
  766. }
  767. return this;
  768. };
  769. Stream.prototype.seek = function(position) {
  770. if (position > this.offset) {
  771. return this.advance(position - this.offset);
  772. } else if (position < this.offset) {
  773. return this.rewind(this.offset - position);
  774. }
  775. };
  776. Stream.prototype.readUInt8 = function() {
  777. var a;
  778. if (!this.available(1)) {
  779. throw new UnderflowError();
  780. }
  781. a = this.list.first.data[this.localOffset];
  782. this.localOffset += 1;
  783. this.offset += 1;
  784. if (this.localOffset === this.list.first.length) {
  785. this.localOffset = 0;
  786. this.list.advance();
  787. }
  788. return a;
  789. };
  790. Stream.prototype.peekUInt8 = function(offset) {
  791. var buffer;
  792. if (offset == null) {
  793. offset = 0;
  794. }
  795. if (!this.available(offset + 1)) {
  796. throw new UnderflowError();
  797. }
  798. offset = this.localOffset + offset;
  799. buffer = this.list.first;
  800. while (buffer) {
  801. if (buffer.length > offset) {
  802. return buffer.data[offset];
  803. }
  804. offset -= buffer.length;
  805. buffer = buffer.next;
  806. }
  807. return 0;
  808. };
  809. Stream.prototype.read = function(bytes, littleEndian) {
  810. var i, _i, _j, _ref;
  811. if (littleEndian == null) {
  812. littleEndian = false;
  813. }
  814. if (littleEndian === nativeEndian) {
  815. for (i = _i = 0; _i < bytes; i = _i += 1) {
  816. uint8[i] = this.readUInt8();
  817. }
  818. } else {
  819. for (i = _j = _ref = bytes - 1; _j >= 0; i = _j += -1) {
  820. uint8[i] = this.readUInt8();
  821. }
  822. }
  823. };
  824. Stream.prototype.peek = function(bytes, offset, littleEndian) {
  825. var i, _i, _j;
  826. if (littleEndian == null) {
  827. littleEndian = false;
  828. }
  829. if (littleEndian === nativeEndian) {
  830. for (i = _i = 0; _i < bytes; i = _i += 1) {
  831. uint8[i] = this.peekUInt8(offset + i);
  832. }
  833. } else {
  834. for (i = _j = 0; _j < bytes; i = _j += 1) {
  835. uint8[bytes - i - 1] = this.peekUInt8(offset + i);
  836. }
  837. }
  838. };
  839. Stream.prototype.readInt8 = function() {
  840. this.read(1);
  841. return int8[0];
  842. };
  843. Stream.prototype.peekInt8 = function(offset) {
  844. if (offset == null) {
  845. offset = 0;
  846. }
  847. this.peek(1, offset);
  848. return int8[0];
  849. };
  850. Stream.prototype.readUInt16 = function(littleEndian) {
  851. this.read(2, littleEndian);
  852. return uint16[0];
  853. };
  854. Stream.prototype.peekUInt16 = function(offset, littleEndian) {
  855. if (offset == null) {
  856. offset = 0;
  857. }
  858. this.peek(2, offset, littleEndian);
  859. return uint16[0];
  860. };
  861. Stream.prototype.readInt16 = function(littleEndian) {
  862. this.read(2, littleEndian);
  863. return int16[0];
  864. };
  865. Stream.prototype.peekInt16 = function(offset, littleEndian) {
  866. if (offset == null) {
  867. offset = 0;
  868. }
  869. this.peek(2, offset, littleEndian);
  870. return int16[0];
  871. };
  872. Stream.prototype.readUInt24 = function(littleEndian) {
  873. if (littleEndian) {
  874. return this.readUInt16(true) + (this.readUInt8() << 16);
  875. } else {
  876. return (this.readUInt16() << 8) + this.readUInt8();
  877. }
  878. };
  879. Stream.prototype.peekUInt24 = function(offset, littleEndian) {
  880. if (offset == null) {
  881. offset = 0;
  882. }
  883. if (littleEndian) {
  884. return this.peekUInt16(offset, true) + (this.peekUInt8(offset + 2) << 16);
  885. } else {
  886. return (this.peekUInt16(offset) << 8) + this.peekUInt8(offset + 2);
  887. }
  888. };
  889. Stream.prototype.readInt24 = function(littleEndian) {
  890. if (littleEndian) {
  891. return this.readUInt16(true) + (this.readInt8() << 16);
  892. } else {
  893. return (this.readInt16() << 8) + this.readUInt8();
  894. }
  895. };
  896. Stream.prototype.peekInt24 = function(offset, littleEndian) {
  897. if (offset == null) {
  898. offset = 0;
  899. }
  900. if (littleEndian) {
  901. return this.peekUInt16(offset, true) + (this.peekInt8(offset + 2) << 16);
  902. } else {
  903. return (this.peekInt16(offset) << 8) + this.peekUInt8(offset + 2);
  904. }
  905. };
  906. Stream.prototype.readUInt32 = function(littleEndian) {
  907. this.read(4, littleEndian);
  908. return uint32[0];
  909. };
  910. Stream.prototype.peekUInt32 = function(offset, littleEndian) {
  911. if (offset == null) {
  912. offset = 0;
  913. }
  914. this.peek(4, offset, littleEndian);
  915. return uint32[0];
  916. };
  917. Stream.prototype.readInt32 = function(littleEndian) {
  918. this.read(4, littleEndian);
  919. return int32[0];
  920. };
  921. Stream.prototype.peekInt32 = function(offset, littleEndian) {
  922. if (offset == null) {
  923. offset = 0;
  924. }
  925. this.peek(4, offset, littleEndian);
  926. return int32[0];
  927. };
  928. Stream.prototype.readFloat32 = function(littleEndian) {
  929. this.read(4, littleEndian);
  930. return float32[0];
  931. };
  932. Stream.prototype.peekFloat32 = function(offset, littleEndian) {
  933. if (offset == null) {
  934. offset = 0;
  935. }
  936. this.peek(4, offset, littleEndian);
  937. return float32[0];
  938. };
  939. Stream.prototype.readFloat64 = function(littleEndian) {
  940. this.read(8, littleEndian);
  941. if (float64) {
  942. return float64[0];
  943. } else {
  944. return float64Fallback();
  945. }
  946. };
  947. float64Fallback = function() {
  948. var exp, frac, high, low, out, sign;
  949. low = uint32[0], high = uint32[1];
  950. if (!high || high === 0x80000000) {
  951. return 0.0;
  952. }
  953. sign = 1 - (high >>> 31) * 2;
  954. exp = (high >>> 20) & 0x7ff;
  955. frac = high & 0xfffff;
  956. if (exp === 0x7ff) {
  957. if (frac) {
  958. return NaN;
  959. }
  960. return sign * Infinity;
  961. }
  962. exp -= 1023;
  963. out = (frac | 0x100000) * Math.pow(2, exp - 20);
  964. out += low * Math.pow(2, exp - 52);
  965. return sign * out;
  966. };
  967. Stream.prototype.peekFloat64 = function(offset, littleEndian) {
  968. if (offset == null) {
  969. offset = 0;
  970. }
  971. this.peek(8, offset, littleEndian);
  972. if (float64) {
  973. return float64[0];
  974. } else {
  975. return float64Fallback();
  976. }
  977. };
  978. Stream.prototype.readFloat80 = function(littleEndian) {
  979. this.read(10, littleEndian);
  980. return float80();
  981. };
  982. float80 = function() {
  983. var a0, a1, exp, high, low, out, sign;
  984. high = uint32[0], low = uint32[1];
  985. a0 = uint8[9];
  986. a1 = uint8[8];
  987. sign = 1 - (a0 >>> 7) * 2;
  988. exp = ((a0 & 0x7F) << 8) | a1;
  989. if (exp === 0 && low === 0 && high === 0) {
  990. return 0;
  991. }
  992. if (exp === 0x7fff) {
  993. if (low === 0 && high === 0) {
  994. return sign * Infinity;
  995. }
  996. return NaN;
  997. }
  998. exp -= 16383;
  999. out = low * Math.pow(2, exp - 31);
  1000. out += high * Math.pow(2, exp - 63);
  1001. return sign * out;
  1002. };
  1003. Stream.prototype.peekFloat80 = function(offset, littleEndian) {
  1004. if (offset == null) {
  1005. offset = 0;
  1006. }
  1007. this.peek(10, offset, littleEndian);
  1008. return float80();
  1009. };
  1010. Stream.prototype.readBuffer = function(length) {
  1011. var i, result, to, _i;
  1012. result = AVBuffer.allocate(length);
  1013. to = result.data;
  1014. for (i = _i = 0; _i < length; i = _i += 1) {
  1015. to[i] = this.readUInt8();
  1016. }
  1017. return result;
  1018. };
  1019. Stream.prototype.peekBuffer = function(offset, length) {
  1020. var i, result, to, _i;
  1021. if (offset == null) {
  1022. offset = 0;
  1023. }
  1024. result = AVBuffer.allocate(length);
  1025. to = result.data;
  1026. for (i = _i = 0; _i < length; i = _i += 1) {
  1027. to[i] = this.peekUInt8(offset + i);
  1028. }
  1029. return result;
  1030. };
  1031. Stream.prototype.readSingleBuffer = function(length) {
  1032. var result;
  1033. result = this.list.first.slice(this.localOffset, length);
  1034. this.advance(result.length);
  1035. return result;
  1036. };
  1037. Stream.prototype.peekSingleBuffer = function(offset, length) {
  1038. var result;
  1039. result = this.list.first.slice(this.localOffset + offset, length);
  1040. return result;
  1041. };
  1042. Stream.prototype.readString = function(length, encoding) {
  1043. if (encoding == null) {
  1044. encoding = 'ascii';
  1045. }
  1046. return decodeString.call(this, 0, length, encoding, true);
  1047. };
  1048. Stream.prototype.peekString = function(offset, length, encoding) {
  1049. if (offset == null) {
  1050. offset = 0;
  1051. }
  1052. if (encoding == null) {
  1053. encoding = 'ascii';
  1054. }
  1055. return decodeString.call(this, offset, length, encoding, false);
  1056. };
  1057. decodeString = function(offset, length, encoding, advance) {
  1058. var b1, b2, b3, b4, bom, c, end, littleEndian, nullEnd, pt, result, w1, w2;
  1059. encoding = encoding.toLowerCase();
  1060. nullEnd = length === null ? 0 : -1;
  1061. if (length == null) {
  1062. length = Infinity;
  1063. }
  1064. end = offset + length;
  1065. result = '';
  1066. switch (encoding) {
  1067. case 'ascii':
  1068. case 'latin1':
  1069. while (offset < end && (c = this.peekUInt8(offset++)) !== nullEnd) {
  1070. result += String.fromCharCode(c);
  1071. }
  1072. break;
  1073. case 'utf8':
  1074. case 'utf-8':
  1075. while (offset < end && (b1 = this.peekUInt8(offset++)) !== nullEnd) {
  1076. if ((b1 & 0x80) === 0) {
  1077. result += String.fromCharCode(b1);
  1078. } else if ((b1 & 0xe0) === 0xc0) {
  1079. b2 = this.peekUInt8(offset++) & 0x3f;
  1080. result += String.fromCharCode(((b1 & 0x1f) << 6) | b2);
  1081. } else if ((b1 & 0xf0) === 0xe0) {
  1082. b2 = this.peekUInt8(offset++) & 0x3f;
  1083. b3 = this.peekUInt8(offset++) & 0x3f;
  1084. result += String.fromCharCode(((b1 & 0x0f) << 12) | (b2 << 6) | b3);
  1085. } else if ((b1 & 0xf8) === 0xf0) {
  1086. b2 = this.peekUInt8(offset++) & 0x3f;
  1087. b3 = this.peekUInt8(offset++) & 0x3f;
  1088. b4 = this.peekUInt8(offset++) & 0x3f;
  1089. pt = (((b1 & 0x0f) << 18) | (b2 << 12) | (b3 << 6) | b4) - 0x10000;
  1090. result += String.fromCharCode(0xd800 + (pt >> 10), 0xdc00 + (pt & 0x3ff));
  1091. }
  1092. }
  1093. break;
  1094. case 'utf16-be':
  1095. case 'utf16be':
  1096. case 'utf16le':
  1097. case 'utf16-le':
  1098. case 'utf16bom':
  1099. case 'utf16-bom':
  1100. switch (encoding) {
  1101. case 'utf16be':
  1102. case 'utf16-be':
  1103. littleEndian = false;
  1104. break;
  1105. case 'utf16le':
  1106. case 'utf16-le':
  1107. littleEndian = true;
  1108. break;
  1109. case 'utf16bom':
  1110. case 'utf16-bom':
  1111. if (length < 2 || (bom = this.peekUInt16(offset)) === nullEnd) {
  1112. if (advance) {
  1113. this.advance(offset += 2);
  1114. }
  1115. return result;
  1116. }
  1117. littleEndian = bom === 0xfffe;
  1118. offset += 2;
  1119. }
  1120. while (offset < end && (w1 = this.peekUInt16(offset, littleEndian)) !== nullEnd) {
  1121. offset += 2;
  1122. if (w1 < 0xd800 || w1 > 0xdfff) {
  1123. result += String.fromCharCode(w1);
  1124. } else {
  1125. if (w1 > 0xdbff) {
  1126. throw new Error("Invalid utf16 sequence.");
  1127. }
  1128. w2 = this.peekUInt16(offset, littleEndian);
  1129. if (w2 < 0xdc00 || w2 > 0xdfff) {
  1130. throw new Error("Invalid utf16 sequence.");
  1131. }
  1132. result += String.fromCharCode(w1, w2);
  1133. offset += 2;
  1134. }
  1135. }
  1136. if (w1 === nullEnd) {
  1137. offset += 2;
  1138. }
  1139. break;
  1140. default:
  1141. throw new Error("Unknown encoding: " + encoding);
  1142. }
  1143. if (advance) {
  1144. this.advance(offset);
  1145. }
  1146. return result;
  1147. };
  1148. return Stream;
  1149. })();
  1150. module.exports = Stream;
  1151. },{"./buffer":7,"./bufferlist":8,"./underflow":11}],11:[function(_dereq_,module,exports){
  1152. var UnderflowError,
  1153. __hasProp = {}.hasOwnProperty,
  1154. __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
  1155. UnderflowError = (function(_super) {
  1156. __extends(UnderflowError, _super);
  1157. function UnderflowError() {
  1158. UnderflowError.__super__.constructor.apply(this, arguments);
  1159. this.name = 'UnderflowError';
  1160. this.stack = new Error().stack;
  1161. }
  1162. return UnderflowError;
  1163. })(Error);
  1164. module.exports = UnderflowError;
  1165. },{}],12:[function(_dereq_,module,exports){
  1166. var Bitstream, BufferList, Decoder, EventEmitter, Stream, UnderflowError,
  1167. __hasProp = {}.hasOwnProperty,
  1168. __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
  1169. EventEmitter = _dereq_('./core/events');
  1170. BufferList = _dereq_('./core/bufferlist');
  1171. Stream = _dereq_('./core/stream');
  1172. Bitstream = _dereq_('./core/bitstream');
  1173. UnderflowError = _dereq_('./core/underflow');
  1174. Decoder = (function(_super) {
  1175. var codecs;
  1176. __extends(Decoder, _super);
  1177. function Decoder(demuxer, format) {
  1178. var list;
  1179. this.demuxer = demuxer;
  1180. this.format = format;
  1181. list = new BufferList;
  1182. this.stream = new Stream(list);
  1183. this.bitstream = new Bitstream(this.stream);
  1184. this.receivedFinalBuffer = false;
  1185. this.waiting = false;
  1186. this.demuxer.on('cookie', (function(_this) {
  1187. return function(cookie) {
  1188. var error;
  1189. try {
  1190. return _this.setCookie(cookie);
  1191. } catch (_error) {
  1192. error = _error;
  1193. return _this.emit('error', error);
  1194. }
  1195. };
  1196. })(this));
  1197. this.demuxer.on('data', (function(_this) {
  1198. return function(chunk) {
  1199. list.append(chunk);
  1200. if (_this.waiting) {
  1201. return _this.decode();
  1202. }
  1203. };
  1204. })(this));
  1205. this.demuxer.on('end', (function(_this) {
  1206. return function() {
  1207. _this.receivedFinalBuffer = true;
  1208. if (_this.waiting) {
  1209. return _this.decode();
  1210. }
  1211. };
  1212. })(this));
  1213. this.init();
  1214. }
  1215. Decoder.prototype.init = function() {};
  1216. Decoder.prototype.setCookie = function(cookie) {};
  1217. Decoder.prototype.readChunk = function() {};
  1218. Decoder.prototype.decode = function() {
  1219. var error, offset, packet;
  1220. this.waiting = false;
  1221. offset = this.bitstream.offset();
  1222. try {
  1223. packet = this.readChunk();
  1224. } catch (_error) {
  1225. error = _error;
  1226. if (!(error instanceof UnderflowError)) {
  1227. this.emit('error', error);
  1228. return false;
  1229. }
  1230. }
  1231. if (packet) {
  1232. this.emit('data', packet);
  1233. return true;
  1234. } else if (!this.receivedFinalBuffer) {
  1235. this.bitstream.seek(offset);
  1236. this.waiting = true;
  1237. } else {
  1238. this.emit('end');
  1239. }
  1240. return false;
  1241. };
  1242. Decoder.prototype.seek = function(timestamp) {
  1243. var seekPoint;
  1244. seekPoint = this.demuxer.seek(timestamp);
  1245. this.stream.seek(seekPoint.offset);
  1246. return seekPoint.timestamp;
  1247. };
  1248. codecs = {};
  1249. Decoder.register = function(id, decoder) {
  1250. return codecs[id] = decoder;
  1251. };
  1252. Decoder.find = function(id) {
  1253. return codecs[id] || null;
  1254. };
  1255. return Decoder;
  1256. })(EventEmitter);
  1257. module.exports = Decoder;
  1258. },{"./core/bitstream":6,"./core/bufferlist":8,"./core/events":9,"./core/stream":10,"./core/underflow":11}],13:[function(_dereq_,module,exports){
  1259. var Decoder, LPCMDecoder,
  1260. __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; },
  1261. __hasProp = {}.hasOwnProperty,
  1262. __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
  1263. Decoder = _dereq_('../decoder');
  1264. LPCMDecoder = (function(_super) {
  1265. __extends(LPCMDecoder, _super);
  1266. function LPCMDecoder() {
  1267. this.readChunk = __bind(this.readChunk, this);
  1268. return LPCMDecoder.__super__.constructor.apply(this, arguments);
  1269. }
  1270. Decoder.register('lpcm', LPCMDecoder);
  1271. LPCMDecoder.prototype.readChunk = function() {
  1272. var chunkSize, i, littleEndian, output, samples, stream, _i, _j, _k, _l, _m, _n;
  1273. stream = this.stream;
  1274. littleEndian = this.format.littleEndian;
  1275. chunkSize = Math.min(4096, stream.remainingBytes());
  1276. samples = chunkSize / (this.format.bitsPerChannel / 8) | 0;
  1277. if (chunkSize < this.format.bitsPerChannel / 8) {
  1278. return null;
  1279. }
  1280. if (this.format.floatingPoint) {
  1281. switch (this.format.bitsPerChannel) {
  1282. case 32:
  1283. output = new Float32Array(samples);
  1284. for (i = _i = 0; _i < samples; i = _i += 1) {
  1285. output[i] = stream.readFloat32(littleEndian);
  1286. }
  1287. break;
  1288. case 64:
  1289. output = new Float64Array(samples);
  1290. for (i = _j = 0; _j < samples; i = _j += 1) {
  1291. output[i] = stream.readFloat64(littleEndian);
  1292. }
  1293. break;
  1294. default:
  1295. throw new Error('Unsupported bit depth.');
  1296. }
  1297. } else {
  1298. switch (this.format.bitsPerChannel) {
  1299. case 8:
  1300. output = new Int8Array(samples);
  1301. for (i = _k = 0; _k < samples; i = _k += 1) {
  1302. output[i] = stream.readInt8();
  1303. }
  1304. break;
  1305. case 16:
  1306. output = new Int16Array(samples);
  1307. for (i = _l = 0; _l < samples; i = _l += 1) {
  1308. output[i] = stream.readInt16(littleEndian);
  1309. }
  1310. break;
  1311. case 24:
  1312. output = new Int32Array(samples);
  1313. for (i = _m = 0; _m < samples; i = _m += 1) {
  1314. output[i] = stream.readInt24(littleEndian);
  1315. }
  1316. break;
  1317. case 32:
  1318. output = new Int32Array(samples);
  1319. for (i = _n = 0; _n < samples; i = _n += 1) {
  1320. output[i] = stream.readInt32(littleEndian);
  1321. }
  1322. break;
  1323. default:
  1324. throw new Error('Unsupported bit depth.');
  1325. }
  1326. }
  1327. return output;
  1328. };
  1329. return LPCMDecoder;
  1330. })(Decoder);
  1331. },{"../decoder":12}],14:[function(_dereq_,module,exports){
  1332. var Decoder, XLAWDecoder,
  1333. __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; },
  1334. __hasProp = {}.hasOwnProperty,
  1335. __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
  1336. Decoder = _dereq_('../decoder');
  1337. XLAWDecoder = (function(_super) {
  1338. var BIAS, QUANT_MASK, SEG_MASK, SEG_SHIFT, SIGN_BIT;
  1339. __extends(XLAWDecoder, _super);
  1340. function XLAWDecoder() {
  1341. this.readChunk = __bind(this.readChunk, this);
  1342. return XLAWDecoder.__super__.constructor.apply(this, arguments);
  1343. }
  1344. Decoder.register('ulaw', XLAWDecoder);
  1345. Decoder.register('alaw', XLAWDecoder);
  1346. SIGN_BIT = 0x80;
  1347. QUANT_MASK = 0xf;
  1348. SEG_SHIFT = 4;
  1349. SEG_MASK = 0x70;
  1350. BIAS = 0x84;
  1351. XLAWDecoder.prototype.init = function() {
  1352. var i, seg, t, table, val, _i, _j;
  1353. this.format.bitsPerChannel = 16;
  1354. this.table = table = new Int16Array(256);
  1355. if (this.format.formatID === 'ulaw') {
  1356. for (i = _i = 0; _i < 256; i = ++_i) {
  1357. val = ~i;
  1358. t = ((val & QUANT_MASK) << 3) + BIAS;
  1359. t <<= (val & SEG_MASK) >>> SEG_SHIFT;
  1360. table[i] = val & SIGN_BIT ? BIAS - t : t - BIAS;
  1361. }
  1362. } else {
  1363. for (i = _j = 0; _j < 256; i = ++_j) {
  1364. val = i ^ 0x55;
  1365. t = val & QUANT_MASK;
  1366. seg = (val & SEG_MASK) >>> SEG_SHIFT;
  1367. if (seg) {
  1368. t = (t + t + 1 + 32) << (seg + 2);
  1369. } else {
  1370. t = (t + t + 1) << 3;
  1371. }
  1372. table[i] = val & SIGN_BIT ? t : -t;
  1373. }
  1374. }
  1375. };
  1376. XLAWDecoder.prototype.readChunk = function() {
  1377. var i, output, samples, stream, table, _i;
  1378. stream = this.stream, table = this.table;
  1379. samples = Math.min(4096, this.stream.remainingBytes());
  1380. if (samples === 0) {
  1381. return;
  1382. }
  1383. output = new Int16Array(samples);
  1384. for (i = _i = 0; _i < samples; i = _i += 1) {
  1385. output[i] = table[stream.readUInt8()];
  1386. }
  1387. return output;
  1388. };
  1389. return XLAWDecoder;
  1390. })(Decoder);
  1391. },{"../decoder":12}],15:[function(_dereq_,module,exports){
  1392. var BufferList, Demuxer, EventEmitter, Stream,
  1393. __hasProp = {}.hasOwnProperty,
  1394. __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
  1395. EventEmitter = _dereq_('./core/events');
  1396. BufferList = _dereq_('./core/bufferlist');
  1397. Stream = _dereq_('./core/stream');
  1398. Demuxer = (function(_super) {
  1399. var formats;
  1400. __extends(Demuxer, _super);
  1401. Demuxer.probe = function(buffer) {
  1402. return false;
  1403. };
  1404. function Demuxer(source, chunk) {
  1405. var list, received;
  1406. list = new BufferList;
  1407. list.append(chunk);
  1408. this.stream = new Stream(list);
  1409. received = false;
  1410. source.on('data', (function(_this) {
  1411. return function(chunk) {
  1412. received = true;
  1413. list.append(chunk);
  1414. return _this.readChunk(chunk);
  1415. };
  1416. })(this));
  1417. source.on('error', (function(_this) {
  1418. return function(err) {
  1419. return _this.emit('error', err);
  1420. };
  1421. })(this));
  1422. source.on('end', (function(_this) {
  1423. return function() {
  1424. if (!received) {
  1425. _this.readChunk(chunk);
  1426. }
  1427. return _this.emit('end');
  1428. };
  1429. })(this));
  1430. this.seekPoints = [];
  1431. this.init();
  1432. }
  1433. Demuxer.prototype.init = function() {};
  1434. Demuxer.prototype.readChunk = function(chunk) {};
  1435. Demuxer.prototype.addSeekPoint = function(offset, timestamp) {
  1436. var index;
  1437. index = this.searchTimestamp(timestamp);
  1438. return this.seekPoints.splice(index, 0, {
  1439. offset: offset,
  1440. timestamp: timestamp
  1441. });
  1442. };
  1443. Demuxer.prototype.searchTimestamp = function(timestamp, backward) {
  1444. var high, low, mid, time;
  1445. low = 0;
  1446. high = this.seekPoints.length;
  1447. if (high > 0 && this.seekPoints[high - 1].timestamp < timestamp) {
  1448. return high;
  1449. }
  1450. while (low < high) {
  1451. mid = (low + high) >> 1;
  1452. time = this.seekPoints[mid].timestamp;
  1453. if (time < timestamp) {
  1454. low = mid + 1;
  1455. } else if (time >= timestamp) {
  1456. high = mid;
  1457. }
  1458. }
  1459. if (high > this.seekPoints.length) {
  1460. high = this.seekPoints.length;
  1461. }
  1462. return high;
  1463. };
  1464. Demuxer.prototype.seek = function(timestamp) {
  1465. var index, seekPoint;
  1466. if (this.format && this.format.framesPerPacket > 0 && this.format.bytesPerPacket > 0) {
  1467. seekPoint = {
  1468. timestamp: timestamp,
  1469. offset: this.format.bytesPerPacket * timestamp / this.format.framesPerPacket
  1470. };
  1471. return seekPoint;
  1472. } else {
  1473. index = this.searchTimestamp(timestamp);
  1474. return this.seekPoints[index];
  1475. }
  1476. };
  1477. formats = [];
  1478. Demuxer.register = function(demuxer) {
  1479. return formats.push(demuxer);
  1480. };
  1481. Demuxer.find = function(buffer) {
  1482. var e, format, offset, stream, _i, _len;
  1483. stream = Stream.fromBuffer(buffer);
  1484. for (_i = 0, _len = formats.length; _i < _len; _i++) {
  1485. format = formats[_i];
  1486. offset = stream.offset;
  1487. try {
  1488. if (format.probe(stream)) {
  1489. return format;
  1490. }
  1491. } catch (_error) {
  1492. e = _error;
  1493. }
  1494. stream.seek(offset);
  1495. }
  1496. return null;
  1497. };
  1498. return Demuxer;
  1499. })(EventEmitter);
  1500. module.exports = Demuxer;
  1501. },{"./core/bufferlist":8,"./core/events":9,"./core/stream":10}],16:[function(_dereq_,module,exports){
  1502. var AIFFDemuxer, Demuxer,
  1503. __hasProp = {}.hasOwnProperty,
  1504. __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
  1505. Demuxer = _dereq_('../demuxer');
  1506. AIFFDemuxer = (function(_super) {
  1507. __extends(AIFFDemuxer, _super);
  1508. function AIFFDemuxer() {
  1509. return AIFFDemuxer.__super__.constructor.apply(this, arguments);
  1510. }
  1511. Demuxer.register(AIFFDemuxer);
  1512. AIFFDemuxer.probe = function(buffer) {
  1513. var _ref;
  1514. return buffer.peekString(0, 4) === 'FORM' && ((_ref = buffer.peekString(8, 4)) === 'AIFF' || _ref === 'AIFC');
  1515. };
  1516. AIFFDemuxer.prototype.readChunk = function() {
  1517. var buffer, format, offset, _ref;
  1518. if (!this.readStart && this.stream.available(12)) {
  1519. if (this.stream.readString(4) !== 'FORM') {
  1520. return this.emit('error', 'Invalid AIFF.');
  1521. }
  1522. this.fileSize = this.stream.readUInt32();
  1523. this.fileType = this.stream.readString(4);
  1524. this.readStart = true;
  1525. if ((_ref = this.fileType) !== 'AIFF' && _ref !== 'AIFC') {
  1526. return this.emit('error', 'Invalid AIFF.');
  1527. }
  1528. }
  1529. while (this.stream.available(1)) {
  1530. if (!this.readHeaders && this.stream.available(8)) {
  1531. this.type = this.stream.readString(4);
  1532. this.len = this.stream.readUInt32();
  1533. }
  1534. switch (this.type) {
  1535. case 'COMM':
  1536. if (!this.stream.available(this.len)) {
  1537. return;
  1538. }
  1539. this.format = {
  1540. formatID: 'lpcm',
  1541. channelsPerFrame: this.stream.readUInt16(),
  1542. sampleCount: this.stream.readUInt32(),
  1543. bitsPerChannel: this.stream.readUInt16(),
  1544. sampleRate: this.stream.readFloat80(),
  1545. framesPerPacket: 1,
  1546. littleEndian: false,
  1547. floatingPoint: false
  1548. };
  1549. this.format.bytesPerPacket = (this.format.bitsPerChannel / 8) * this.format.channelsPerFrame;
  1550. if (this.fileType === 'AIFC') {
  1551. format = this.stream.readString(4);
  1552. this.format.littleEndian = format === 'sowt' && this.format.bitsPerChannel > 8;
  1553. this.format.floatingPoint = format === 'fl32' || format === 'fl64';
  1554. if (format === 'twos' || format === 'sowt' || format === 'fl32' || format === 'fl64' || format === 'NONE') {
  1555. format = 'lpcm';
  1556. }
  1557. this.format.formatID = format;
  1558. this.len -= 4;
  1559. }
  1560. this.stream.advance(this.len - 18);
  1561. this.emit('format', this.format);
  1562. this.emit('duration', this.format.sampleCount / this.format.sampleRate * 1000 | 0);
  1563. break;
  1564. case 'SSND':
  1565. if (!(this.readSSNDHeader && this.stream.available(4))) {
  1566. offset = this.stream.readUInt32();
  1567. this.stream.advance(4);
  1568. this.stream.advance(offset);
  1569. this.readSSNDHeader = true;
  1570. }
  1571. buffer = this.stream.readSingleBuffer(this.len);
  1572. this.len -= buffer.length;
  1573. this.readHeaders = this.len > 0;
  1574. this.emit('data', buffer);
  1575. break;
  1576. default:
  1577. if (!this.stream.available(this.len)) {
  1578. return;
  1579. }
  1580. this.stream.advance(this.len);
  1581. }
  1582. if (this.type !== 'SSND') {
  1583. this.readHeaders = false;
  1584. }
  1585. }
  1586. };
  1587. return AIFFDemuxer;
  1588. })(Demuxer);
  1589. },{"../demuxer":15}],17:[function(_dereq_,module,exports){
  1590. var AUDemuxer, Demuxer,
  1591. __hasProp = {}.hasOwnProperty,
  1592. __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
  1593. Demuxer = _dereq_('../demuxer');
  1594. AUDemuxer = (function(_super) {
  1595. var bps, formats;
  1596. __extends(AUDemuxer, _super);
  1597. function AUDemuxer() {
  1598. return AUDemuxer.__super__.constructor.apply(this, arguments);
  1599. }
  1600. Demuxer.register(AUDemuxer);
  1601. AUDemuxer.probe = function(buffer) {
  1602. return buffer.peekString(0, 4) === '.snd';
  1603. };
  1604. bps = [8, 8, 16, 24, 32, 32, 64];
  1605. bps[26] = 8;
  1606. formats = {
  1607. 1: 'ulaw',
  1608. 27: 'alaw'
  1609. };
  1610. AUDemuxer.prototype.readChunk = function() {
  1611. var bytes, dataSize, encoding, size;
  1612. if (!this.readHeader && this.stream.available(24)) {
  1613. if (this.stream.readString(4) !== '.snd') {
  1614. return this.emit('error', 'Invalid AU file.');
  1615. }
  1616. size = this.stream.readUInt32();
  1617. dataSize = this.stream.readUInt32();
  1618. encoding = this.stream.readUInt32();
  1619. this.format = {
  1620. formatID: formats[encoding] || 'lpcm',
  1621. littleEndian: false,
  1622. floatingPoint: encoding === 6 || encoding === 7,
  1623. bitsPerChannel: bps[encoding - 1],
  1624. sampleRate: this.stream.readUInt32(),
  1625. channelsPerFrame: this.stream.readUInt32(),
  1626. framesPerPacket: 1
  1627. };
  1628. if (this.format.bitsPerChannel == null) {
  1629. return this.emit('error', 'Unsupported encoding in AU file.');
  1630. }
  1631. this.format.bytesPerPacket = (this.format.bitsPerChannel / 8) * this.format.channelsPerFrame;
  1632. if (dataSize !== 0xffffffff) {
  1633. bytes = this.format.bitsPerChannel / 8;
  1634. this.emit('duration', dataSize / bytes / this.format.channelsPerFrame / this.format.sampleRate * 1000 | 0);
  1635. }
  1636. this.emit('format', this.format);
  1637. this.readHeader = true;
  1638. }
  1639. if (this.readHeader) {
  1640. while (this.stream.available(1)) {
  1641. this.emit('data', this.stream.readSingleBuffer(this.stream.remainingBytes()));
  1642. }
  1643. }
  1644. };
  1645. return AUDemuxer;
  1646. })(Demuxer);
  1647. },{"../demuxer":15}],18:[function(_dereq_,module,exports){
  1648. var CAFDemuxer, Demuxer, M4ADemuxer,
  1649. __hasProp = {}.hasOwnProperty,
  1650. __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
  1651. Demuxer = _dereq_('../demuxer');
  1652. M4ADemuxer = _dereq_('./m4a');
  1653. CAFDemuxer = (function(_super) {
  1654. __extends(CAFDemuxer, _super);
  1655. function CAFDemuxer() {
  1656. return CAFDemuxer.__super__.constructor.apply(this, arguments);
  1657. }
  1658. Demuxer.register(CAFDemuxer);
  1659. CAFDemuxer.probe = function(buffer) {
  1660. return buffer.peekString(0, 4) === 'caff';
  1661. };
  1662. CAFDemuxer.prototype.readChunk = function() {
  1663. var buffer, byteOffset, cookie, entries, flags, i, key, metadata, offset, sampleOffset, value, _i, _j, _ref;
  1664. if (!this.format && this.stream.available(64)) {
  1665. if (this.stream.readString(4) !== 'caff') {
  1666. return this.emit('error', "Invalid CAF, does not begin with 'caff'");
  1667. }
  1668. this.stream.advance(4);
  1669. if (this.stream.readString(4) !== 'desc') {
  1670. return this.emit('error', "Invalid CAF, 'caff' is not followed by 'desc'");
  1671. }
  1672. if (!(this.stream.readUInt32() === 0 && this.stream.readUInt32() === 32)) {
  1673. return this.emit('error', "Invalid 'desc' size, should be 32");
  1674. }
  1675. this.format = {};
  1676. this.format.sampleRate = this.stream.readFloat64();
  1677. this.format.formatID = this.stream.readString(4);
  1678. flags = this.stream.readUInt32();
  1679. if (this.format.formatID === 'lpcm') {
  1680. this.format.floatingPoint = Boolean(flags & 1);
  1681. this.format.littleEndian = Boolean(flags & 2);
  1682. }
  1683. this.format.bytesPerPacket = this.stream.readUInt32();
  1684. this.format.framesPerPacket = this.stream.readUInt32();
  1685. this.format.channelsPerFrame = this.stream.readUInt32();
  1686. this.format.bitsPerChannel = this.stream.readUInt32();
  1687. this.emit('format', this.format);
  1688. }
  1689. while (this.stream.available(1)) {
  1690. if (!this.headerCache) {
  1691. this.headerCache = {
  1692. type: this.stream.readString(4),
  1693. oversize: this.stream.readUInt32() !== 0,
  1694. size: this.stream.readUInt32()
  1695. };
  1696. if (this.headerCache.oversize) {
  1697. return this.emit('error', "Holy Shit, an oversized file, not supported in JS");
  1698. }
  1699. }
  1700. switch (this.headerCache.type) {
  1701. case 'kuki':
  1702. if (this.stream.available(this.headerCache.size)) {
  1703. if (this.format.formatID === 'aac ') {
  1704. offset = this.stream.offset + this.headerCache.size;
  1705. if (cookie = M4ADemuxer.readEsds(this.stream)) {
  1706. this.emit('cookie', cookie);
  1707. }
  1708. this.stream.seek(offset);
  1709. } else {
  1710. buffer = this.stream.readBuffer(this.headerCache.size);
  1711. this.emit('cookie', buffer);
  1712. }
  1713. this.headerCache = null;
  1714. }
  1715. break;
  1716. case 'pakt':
  1717. if (this.stream.available(this.headerCache.size)) {
  1718. if (this.stream.readUInt32() !== 0) {
  1719. return this.emit('error', 'Sizes greater than 32 bits are not supported.');
  1720. }
  1721. this.numPackets = this.stream.readUInt32();
  1722. if (this.stream.readUInt32() !== 0) {
  1723. return this.emit('error', 'Sizes greater than 32 bits are not supported.');
  1724. }
  1725. this.numFrames = this.stream.readUInt32();
  1726. this.primingFrames = this.stream.readUInt32();
  1727. this.remainderFrames = this.stream.readUInt32();
  1728. this.emit('duration', this.numFrames / this.format.sampleRate * 1000 | 0);
  1729. this.sentDuration = true;
  1730. byteOffset = 0;
  1731. sampleOffset = 0;
  1732. for (i = _i = 0, _ref = this.numPackets; _i < _ref; i = _i += 1) {
  1733. this.addSeekPoint(byteOffset, sampleOffset);
  1734. byteOffset += this.format.bytesPerPacket || M4ADemuxer.readDescrLen(this.stream);
  1735. sampleOffset += this.format.framesPerPacket || M4ADemuxer.readDescrLen(this.stream);
  1736. }
  1737. this.headerCache = null;
  1738. }
  1739. break;
  1740. case 'info':
  1741. entries = this.stream.readUInt32();
  1742. metadata = {};
  1743. for (i = _j = 0; 0 <= entries ? _j < entries : _j > entries; i = 0 <= entries ? ++_j : --_j) {
  1744. key = this.stream.readString(null);
  1745. value = this.stream.readString(null);
  1746. metadata[key] = value;
  1747. }
  1748. this.emit('metadata', metadata);
  1749. this.headerCache = null;
  1750. break;
  1751. case 'data':
  1752. if (!this.sentFirstDataChunk) {
  1753. this.stream.advance(4);
  1754. this.headerCache.size -= 4;
  1755. if (this.format.bytesPerPacket !== 0 && !this.sentDuration) {
  1756. this.numFrames = this.headerCache.size / this.format.bytesPerPacket;
  1757. this.emit('duration', this.numFrames / this.format.sampleRate * 1000 | 0);
  1758. }
  1759. this.sentFirstDataChunk = true;
  1760. }
  1761. buffer = this.stream.readSingleBuffer(this.headerCache.size);
  1762. this.headerCache.size -= buffer.length;
  1763. this.emit('data', buffer);
  1764. if (this.headerCache.size <= 0) {
  1765. this.headerCache = null;
  1766. }
  1767. break;
  1768. default:
  1769. if (this.stream.available(this.headerCache.size)) {
  1770. this.stream.advance(this.headerCache.size);
  1771. this.headerCache = null;
  1772. }
  1773. }
  1774. }
  1775. };
  1776. return CAFDemuxer;
  1777. })(Demuxer);
  1778. },{"../demuxer":15,"./m4a":19}],19:[function(_dereq_,module,exports){
  1779. var Demuxer, M4ADemuxer,
  1780. __hasProp = {}.hasOwnProperty,
  1781. __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
  1782. __indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; };
  1783. Demuxer = _dereq_('../demuxer');
  1784. M4ADemuxer = (function(_super) {
  1785. var BITS_PER_CHANNEL, TYPES, after, atom, atoms, bool, containers, diskTrack, genres, meta, string;
  1786. __extends(M4ADemuxer, _super);
  1787. function M4ADemuxer() {
  1788. return M4ADemuxer.__super__.constructor.apply(this, arguments);
  1789. }
  1790. Demuxer.register(M4ADemuxer);
  1791. TYPES = ['M4A ', 'M4P ', 'M4B ', 'M4V ', 'isom', 'mp42', 'qt '];
  1792. M4ADemuxer.probe = function(buffer) {
  1793. var _ref;
  1794. return buffer.peekString(4, 4) === 'ftyp' && (_ref = buffer.peekString(8, 4), __indexOf.call(TYPES, _ref) >= 0);
  1795. };
  1796. M4ADemuxer.prototype.init = function() {
  1797. this.atoms = [];
  1798. this.offsets = [];
  1799. this.track = null;
  1800. return this.tracks = [];
  1801. };
  1802. atoms = {};
  1803. containers = {};
  1804. atom = function(name, fn) {
  1805. var c, container, _i, _len, _ref;
  1806. c = [];
  1807. _ref = name.split('.').slice(0, -1);
  1808. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  1809. container = _ref[_i];
  1810. c.push(container);
  1811. containers[c.join('.')] = true;
  1812. }
  1813. if (atoms[name] == null) {
  1814. atoms[name] = {};
  1815. }
  1816. return atoms[name].fn = fn;
  1817. };
  1818. after = function(name, fn) {
  1819. if (atoms[name] == null) {
  1820. atoms[name] = {};
  1821. }
  1822. return atoms[name].after = fn;
  1823. };
  1824. M4ADemuxer.prototype.readChunk = function() {
  1825. var handler, path, type;
  1826. this["break"] = false;
  1827. while (this.stream.available(1) && !this["break"]) {
  1828. if (!this.readHeaders) {
  1829. if (!this.stream.available(8)) {
  1830. return;
  1831. }
  1832. this.len = this.stream.readUInt32() - 8;
  1833. this.type = this.stream.readString(4);
  1834. if (this.len === 0) {
  1835. continue;
  1836. }
  1837. this.atoms.push(this.type);
  1838. this.offsets.push(this.stream.offset + this.len);
  1839. this.readHeaders = true;
  1840. }
  1841. path = this.atoms.join('.');
  1842. handler = atoms[path];
  1843. if (handler != null ? handler.fn : void 0) {
  1844. if (!(this.stream.available(this.len) || path === 'mdat')) {
  1845. return;
  1846. }
  1847. handler.fn.call(this);
  1848. if (path in containers) {
  1849. this.readHeaders = false;
  1850. }
  1851. } else if (path in containers) {
  1852. this.readHeaders = false;
  1853. } else {
  1854. if (!this.stream.available(this.len)) {
  1855. return;
  1856. }
  1857. this.stream.advance(this.len);
  1858. }
  1859. while (this.stream.offset >= this.offsets[this.offsets.length - 1]) {
  1860. handler = atoms[this.atoms.join('.')];
  1861. if (handler != null ? handler.after : void 0) {
  1862. handler.after.call(this);
  1863. }
  1864. type = this.atoms.pop();
  1865. this.offsets.pop();
  1866. this.readHeaders = false;
  1867. }
  1868. }
  1869. };
  1870. atom('ftyp', function() {
  1871. var _ref;
  1872. if (_ref = this.stream.readString(4), __indexOf.call(TYPES, _ref) < 0) {
  1873. return this.emit('error', 'Not a valid M4A file.');
  1874. }
  1875. return this.stream.advance(this.len - 4);
  1876. });
  1877. atom('moov.trak', function() {
  1878. this.track = {};
  1879. return this.tracks.push(this.track);
  1880. });
  1881. atom('moov.trak.tkhd', function() {
  1882. this.stream.advance(4);
  1883. this.stream.advance(8);
  1884. this.track.id = this.stream.readUInt32();
  1885. return this.stream.advance(this.len - 16);
  1886. });
  1887. atom('moov.trak.mdia.hdlr', function() {
  1888. this.stream.advance(4);
  1889. this.stream.advance(4);
  1890. this.track.type = this.stream.readString(4);
  1891. this.stream.advance(12);
  1892. return this.stream.advance(this.len - 24);
  1893. });
  1894. atom('moov.trak.mdia.mdhd', function() {
  1895. this.stream.advance(4);
  1896. this.stream.advance(8);
  1897. this.track.timeScale = this.stream.readUInt32();
  1898. this.track.duration = this.stream.readUInt32();
  1899. return this.stream.advance(4);
  1900. });
  1901. BITS_PER_CHANNEL = {
  1902. ulaw: 8,
  1903. alaw: 8,
  1904. in24: 24,
  1905. in32: 32,
  1906. fl32: 32,
  1907. fl64: 64
  1908. };
  1909. atom('moov.trak.mdia.minf.stbl.stsd', function() {
  1910. var format, numEntries, version, _ref, _ref1;
  1911. this.stream.advance(4);
  1912. numEntries = this.stream.readUInt32();
  1913. if (this.track.type !== 'soun') {
  1914. return this.stream.advance(this.len - 8);
  1915. }
  1916. if (numEntries !== 1) {
  1917. return this.emit('error', "Only expecting one entry in sample description atom!");
  1918. }
  1919. this.stream.advance(4);
  1920. format = this.track.format = {};
  1921. format.formatID = this.stream.readString(4);
  1922. this.stream.advance(6);
  1923. this.stream.advance(2);
  1924. version = this.stream.readUInt16();
  1925. this.stream.advance(6);
  1926. format.channelsPerFrame = this.stream.readUInt16();
  1927. format.bitsPerChannel = this.stream.readUInt16();
  1928. this.stream.advance(4);
  1929. format.sampleRate = this.stream.readUInt16();
  1930. this.stream.advance(2);
  1931. if (version === 1) {
  1932. format.framesPerPacket = this.stream.readUInt32();
  1933. this.stream.advance(4);
  1934. format.bytesPerFrame = this.stream.readUInt32();
  1935. this.stream.advance(4);
  1936. } else if (version !== 0) {
  1937. this.emit('error', 'Unknown version in stsd atom');
  1938. }
  1939. if (BITS_PER_CHANNEL[format.formatID] != null) {
  1940. format.bitsPerChannel = BITS_PER_CHANNEL[format.formatID];
  1941. }
  1942. format.floatingPoint = (_ref = format.formatID) === 'fl32' || _ref === 'fl64';
  1943. format.littleEndian = format.formatID === 'sowt' && format.bitsPerChannel > 8;
  1944. if ((_ref1 = format.formatID) === 'twos' || _ref1 === 'sowt' || _ref1 === 'in24' || _ref1 === 'in32' || _ref1 === 'fl32' || _ref1 === 'fl64' || _ref1 === 'raw ' || _ref1 === 'NONE') {
  1945. return format.formatID = 'lpcm';
  1946. }
  1947. });
  1948. atom('moov.trak.mdia.minf.stbl.stsd.alac', function() {
  1949. this.stream.advance(4);
  1950. return this.track.cookie = this.stream.readBuffer(this.len - 4);
  1951. });
  1952. atom('moov.trak.mdia.minf.stbl.stsd.esds', function() {
  1953. var offset;
  1954. offset = this.stream.offset + this.len;
  1955. this.track.cookie = M4ADemuxer.readEsds(this.stream);
  1956. return this.stream.seek(offset);
  1957. });
  1958. atom('moov.trak.mdia.minf.stbl.stsd.wave.enda', function() {
  1959. return this.track.format.littleEndian = !!this.stream.readUInt16();
  1960. });
  1961. M4ADemuxer.readDescrLen = function(stream) {
  1962. var c, count, len;
  1963. len = 0;
  1964. count = 4;
  1965. while (count--) {
  1966. c = stream.readUInt8();
  1967. len = (len << 7) | (c & 0x7f);
  1968. if (!(c & 0x80)) {
  1969. break;
  1970. }
  1971. }
  1972. return len;
  1973. };
  1974. M4ADemuxer.readEsds = function(stream) {
  1975. var codec_id, flags, len, tag;
  1976. stream.advance(4);
  1977. tag = stream.readUInt8();
  1978. len = M4ADemuxer.readDescrLen(stream);
  1979. if (tag === 0x03) {
  1980. stream.advance(2);
  1981. flags = stream.readUInt8();
  1982. if (flags & 0x80) {
  1983. stream.advance(2);
  1984. }
  1985. if (flags & 0x40) {
  1986. stream.advance(stream.readUInt8());
  1987. }
  1988. if (flags & 0x20) {
  1989. stream.advance(2);
  1990. }
  1991. } else {
  1992. stream.advance(2);
  1993. }
  1994. tag = stream.readUInt8();
  1995. len = M4ADemuxer.readDescrLen(stream);
  1996. if (tag === 0x04) {
  1997. codec_id = stream.readUInt8();
  1998. stream.advance(1);
  1999. stream.advance(3);
  2000. stream.advance(4);
  2001. stream.advance(4);
  2002. tag = stream.readUInt8();
  2003. len = M4ADemuxer.readDescrLen(stream);
  2004. if (tag === 0x05) {
  2005. return stream.readBuffer(len);
  2006. }
  2007. }
  2008. return null;
  2009. };
  2010. atom('moov.trak.mdia.minf.stbl.stts', function() {
  2011. var entries, i, _i;
  2012. this.stream.advance(4);
  2013. entries = this.stream.readUInt32();
  2014. this.track.stts = [];
  2015. for (i = _i = 0; _i < entries; i = _i += 1) {
  2016. this.track.stts[i] = {
  2017. count: this.stream.readUInt32(),
  2018. duration: this.stream.readUInt32()
  2019. };
  2020. }
  2021. return this.setupSeekPoints();
  2022. });
  2023. atom('moov.trak.mdia.minf.stbl.stsc', function() {
  2024. var entries, i, _i;
  2025. this.stream.advance(4);
  2026. entries = this.stream.readUInt32();
  2027. this.track.stsc = [];
  2028. for (i = _i = 0; _i < entries; i = _i += 1) {
  2029. this.track.stsc[i] = {
  2030. first: this.stream.readUInt32(),
  2031. count: this.stream.readUInt32(),
  2032. id: this.stream.readUInt32()
  2033. };
  2034. }
  2035. return this.setupSeekPoints();
  2036. });
  2037. atom('moov.trak.mdia.minf.stbl.stsz', function() {
  2038. var entries, i, _i;
  2039. this.stream.advance(4);
  2040. this.track.sampleSize = this.stream.readUInt32();
  2041. entries = this.stream.readUInt32();
  2042. if (this.track.sampleSize === 0 && entries > 0) {
  2043. this.track.sampleSizes = [];
  2044. for (i = _i = 0; _i < entries; i = _i += 1) {
  2045. this.track.sampleSizes[i] = this.stream.readUInt32();
  2046. }
  2047. }
  2048. return this.setupSeekPoints();
  2049. });
  2050. atom('moov.trak.mdia.minf.stbl.stco', function() {
  2051. var entries, i, _i;
  2052. this.stream.advance(4);
  2053. entries = this.stream.readUInt32();
  2054. this.track.chunkOffsets = [];
  2055. for (i = _i = 0; _i < entries; i = _i += 1) {
  2056. this.track.chunkOffsets[i] = this.stream.readUInt32();
  2057. }
  2058. return this.setupSeekPoints();
  2059. });
  2060. atom('moov.trak.tref.chap', function() {
  2061. var entries, i, _i;
  2062. entries = this.len >> 2;
  2063. this.track.chapterTracks = [];
  2064. for (i = _i = 0; _i < entries; i = _i += 1) {
  2065. this.track.chapterTracks[i] = this.stream.readUInt32();
  2066. }
  2067. });
  2068. M4ADemuxer.prototype.setupSeekPoints = function() {
  2069. var i, j, offset, position, sampleIndex, size, stscIndex, sttsIndex, sttsSample, timestamp, _i, _j, _len, _ref, _ref1, _results;
  2070. if (!((this.track.chunkOffsets != null) && (this.track.stsc != null) && (this.track.sampleSize != null) && (this.track.stts != null))) {
  2071. return;
  2072. }
  2073. stscIndex = 0;
  2074. sttsIndex = 0;
  2075. sttsIndex = 0;
  2076. sttsSample = 0;
  2077. sampleIndex = 0;
  2078. offset = 0;
  2079. timestamp = 0;
  2080. this.track.seekPoints = [];
  2081. _ref = this.track.chunkOffsets;
  2082. _results = [];
  2083. for (i = _i = 0, _len = _ref.length; _i < _len; i = ++_i) {
  2084. position = _ref[i];
  2085. for (j = _j = 0, _ref1 = this.track.stsc[stscIndex].count; _j < _ref1; j = _j += 1) {
  2086. this.track.seekPoints.push({
  2087. offset: offset,
  2088. position: position,
  2089. timestamp: timestamp
  2090. });
  2091. size = this.track.sampleSize || this.track.sampleSizes[sampleIndex++];
  2092. offset += size;
  2093. position += size;
  2094. timestamp += this.track.stts[sttsIndex].duration;
  2095. if (sttsIndex + 1 < this.track.stts.length && ++sttsSample === this.track.stts[sttsIndex].count) {
  2096. sttsSample = 0;
  2097. sttsIndex++;
  2098. }
  2099. }
  2100. if (stscIndex + 1 < this.track.stsc.length && i + 1 === this.track.stsc[stscIndex + 1].first) {
  2101. _results.push(stscIndex++);
  2102. } else {
  2103. _results.push(void 0);
  2104. }
  2105. }
  2106. return _results;
  2107. };
  2108. after('moov', function() {
  2109. var track, _i, _len, _ref;
  2110. if (this.mdatOffset != null) {
  2111. this.stream.seek(this.mdatOffset - 8);
  2112. }
  2113. _ref = this.tracks;
  2114. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  2115. track = _ref[_i];
  2116. if (!(track.type === 'soun')) {
  2117. continue;
  2118. }
  2119. this.track = track;
  2120. break;
  2121. }
  2122. if (this.track.type !== 'soun') {
  2123. this.track = null;
  2124. return this.emit('error', 'No audio tracks in m4a file.');
  2125. }
  2126. this.emit('format', this.track.format);
  2127. this.emit('duration', this.track.duration / this.track.timeScale * 1000 | 0);
  2128. if (this.track.cookie) {
  2129. this.emit('cookie', this.track.cookie);
  2130. }
  2131. return this.seekPoints = this.track.seekPoints;
  2132. });
  2133. atom('mdat', function() {
  2134. var bytes, chunkSize, length, numSamples, offset, sample, size, _i;
  2135. if (!this.startedData) {
  2136. if (this.mdatOffset == null) {
  2137. this.mdatOffset = this.stream.offset;
  2138. }
  2139. if (this.tracks.length === 0) {
  2140. bytes = Math.min(this.stream.remainingBytes(), this.len);
  2141. this.stream.advance(bytes);
  2142. this.len -= bytes;
  2143. return;
  2144. }
  2145. this.chunkIndex = 0;
  2146. this.stscIndex = 0;
  2147. this.sampleIndex = 0;
  2148. this.tailOffset = 0;
  2149. this.tailSamples = 0;
  2150. this.startedData = true;
  2151. }
  2152. if (!this.readChapters) {
  2153. this.readChapters = this.parseChapters();
  2154. if (this["break"] = !this.readChapters) {
  2155. return;
  2156. }
  2157. this.stream.seek(this.mdatOffset);
  2158. }
  2159. offset = this.track.chunkOffsets[this.chunkIndex] + this.tailOffset;
  2160. length = 0;
  2161. if (!this.stream.available(offset - this.stream.offset)) {
  2162. this["break"] = true;
  2163. return;
  2164. }
  2165. this.stream.seek(offset);
  2166. while (this.chunkIndex < this.track.chunkOffsets.length) {
  2167. numSamples = this.track.stsc[this.stscIndex].count - this.tailSamples;
  2168. chunkSize = 0;
  2169. for (sample = _i = 0; _i < numSamples; sample = _i += 1) {
  2170. size = this.track.sampleSize || this.track.sampleSizes[this.sampleIndex];
  2171. if (!this.stream.available(length + size)) {
  2172. break;
  2173. }
  2174. length += size;
  2175. chunkSize += size;
  2176. this.sampleIndex++;
  2177. }
  2178. if (sample < numSamples) {
  2179. this.tailOffset += chunkSize;
  2180. this.tailSamples += sample;
  2181. break;
  2182. } else {
  2183. this.chunkIndex++;
  2184. this.tailOffset = 0;
  2185. this.tailSamples = 0;
  2186. if (this.stscIndex + 1 < this.track.stsc.length && this.chunkIndex + 1 === this.track.stsc[this.stscIndex + 1].first) {
  2187. this.stscIndex++;
  2188. }
  2189. if (offset + length !== this.track.chunkOffsets[this.chunkIndex]) {
  2190. break;
  2191. }
  2192. }
  2193. }
  2194. if (length > 0) {
  2195. this.emit('data', this.stream.readBuffer(length));
  2196. return this["break"] = this.chunkIndex === this.track.chunkOffsets.length;
  2197. } else {
  2198. return this["break"] = true;
  2199. }
  2200. });
  2201. M4ADemuxer.prototype.parseChapters = function() {
  2202. var bom, id, len, nextTimestamp, point, title, track, _i, _len, _ref, _ref1, _ref2, _ref3;
  2203. if (!(((_ref = this.track.chapterTracks) != null ? _ref.length : void 0) > 0)) {
  2204. return true;
  2205. }
  2206. id = this.track.chapterTracks[0];
  2207. _ref1 = this.tracks;
  2208. for (_i = 0, _len = _ref1.length; _i < _len; _i++) {
  2209. track = _ref1[_i];
  2210. if (track.id === id) {
  2211. break;
  2212. }
  2213. }
  2214. if (track.id !== id) {
  2215. this.emit('error', 'Chapter track does not exist.');
  2216. }
  2217. if (this.chapters == null) {
  2218. this.chapters = [];
  2219. }
  2220. while (this.chapters.length < track.seekPoints.length) {
  2221. point = track.seekPoints[this.chapters.length];
  2222. if (!this.stream.available(point.position - this.stream.offset + 32)) {
  2223. return false;
  2224. }
  2225. this.stream.seek(point.position);
  2226. len = this.stream.readUInt16();
  2227. title = null;
  2228. if (!this.stream.available(len)) {
  2229. return false;
  2230. }
  2231. if (len > 2) {
  2232. bom = this.stream.peekUInt16();
  2233. if (bom === 0xfeff || bom === 0xfffe) {
  2234. title = this.stream.readString(len, 'utf16-bom');
  2235. }
  2236. }
  2237. if (title == null) {
  2238. title = this.stream.readString(len, 'utf8');
  2239. }
  2240. nextTimestamp = (_ref2 = (_ref3 = track.seekPoints[this.chapters.length + 1]) != null ? _ref3.timestamp : void 0) != null ? _ref2 : track.duration;
  2241. this.chapters.push({
  2242. title: title,
  2243. timestamp: point.timestamp / track.timeScale * 1000 | 0,
  2244. duration: (nextTimestamp - point.timestamp) / track.timeScale * 1000 | 0
  2245. });
  2246. }
  2247. this.emit('chapters', this.chapters);
  2248. return true;
  2249. };
  2250. atom('moov.udta.meta', function() {
  2251. this.metadata = {};
  2252. return this.stream.advance(4);
  2253. });
  2254. after('moov.udta.meta', function() {
  2255. return this.emit('metadata', this.metadata);
  2256. });
  2257. meta = function(field, name, fn) {
  2258. return atom("moov.udta.meta.ilst." + field + ".data", function() {
  2259. this.stream.advance(8);
  2260. this.len -= 8;
  2261. return fn.call(this, name);
  2262. });
  2263. };
  2264. string = function(field) {
  2265. return this.metadata[field] = this.stream.readString(this.len, 'utf8');
  2266. };
  2267. meta('©alb', 'album', string);
  2268. meta('©arg', 'arranger', string);
  2269. meta('©art', 'artist', string);
  2270. meta('©ART', 'artist', string);
  2271. meta('aART', 'albumArtist', string);
  2272. meta('catg', 'category', string);
  2273. meta('©com', 'composer', string);
  2274. meta('©cpy', 'copyright', string);
  2275. meta('cprt', 'copyright', string);
  2276. meta('©cmt', 'comments', string);
  2277. meta('©day', 'releaseDate', string);
  2278. meta('desc', 'description', string);
  2279. meta('©gen', 'genre', string);
  2280. meta('©grp', 'grouping', string);
  2281. meta('©isr', 'ISRC', string);
  2282. meta('keyw', 'keywords', string);
  2283. meta('©lab', 'recordLabel', string);
  2284. meta('ldes', 'longDescription', string);
  2285. meta('©lyr', 'lyrics', string);
  2286. meta('©nam', 'title', string);
  2287. meta('©phg', 'recordingCopyright', string);
  2288. meta('©prd', 'producer', string);
  2289. meta('©prf', 'performers', string);
  2290. meta('purd', 'purchaseDate', string);
  2291. meta('purl', 'podcastURL', string);
  2292. meta('©swf', 'songwriter', string);
  2293. meta('©too', 'encoder', string);
  2294. meta('©wrt', 'composer', string);
  2295. meta('covr', 'coverArt', function(field) {
  2296. return this.metadata[field] = this.stream.readBuffer(this.len);
  2297. });
  2298. genres = ["Blues", "Classic Rock", "Country", "Dance", "Disco", "Funk", "Grunge", "Hip-Hop", "Jazz", "Metal", "New Age", "Oldies", "Other", "Pop", "R&B", "Rap", "Reggae", "Rock", "Techno", "Industrial", "Alternative", "Ska", "Death Metal", "Pranks", "Soundtrack", "Euro-Techno", "Ambient", "Trip-Hop", "Vocal", "Jazz+Funk", "Fusion", "Trance", "Classical", "Instrumental", "Acid", "House", "Game", "Sound Clip", "Gospel", "Noise", "AlternRock", "Bass", "Soul", "Punk", "Space", "Meditative", "Instrumental Pop", "Instrumental Rock", "Ethnic", "Gothic", "Darkwave", "Techno-Industrial", "Electronic", "Pop-Folk", "Eurodance", "Dream", "Southern Rock", "Comedy", "Cult", "Gangsta", "Top 40", "Christian Rap", "Pop/Funk", "Jungle", "Native American", "Cabaret", "New Wave", "Psychadelic", "Rave", "Showtunes", "Trailer", "Lo-Fi", "Tribal", "Acid Punk", "Acid Jazz", "Polka", "Retro", "Musical", "Rock & Roll", "Hard Rock", "Folk", "Folk/Rock", "National Folk", "Swing", "Fast Fusion", "Bebob", "Latin", "Revival", "Celtic", "Bluegrass", "Avantgarde", "Gothic Rock", "Progressive Rock", "Psychedelic Rock", "Symphonic Rock", "Slow Rock", "Big Band", "Chorus", "Easy Listening", "Acoustic", "Humour", "Speech", "Chanson", "Opera", "Chamber Music", "Sonata", "Symphony", "Booty Bass", "Primus", "Porn Groove", "Satire", "Slow Jam", "Club", "Tango", "Samba", "Folklore", "Ballad", "Power Ballad", "Rhythmic Soul", "Freestyle", "Duet", "Punk Rock", "Drum Solo", "A Capella", "Euro-House", "Dance Hall"];
  2299. meta('gnre', 'genre', function(field) {
  2300. return this.metadata[field] = genres[this.stream.readUInt16() - 1];
  2301. });
  2302. meta('tmpo', 'tempo', function(field) {
  2303. return this.metadata[field] = this.stream.readUInt16();
  2304. });
  2305. meta('rtng', 'rating', function(field) {
  2306. var rating;
  2307. rating = this.stream.readUInt8();
  2308. return this.metadata[field] = rating === 2 ? 'Clean' : rating !== 0 ? 'Explicit' : 'None';
  2309. });
  2310. diskTrack = function(field) {
  2311. this.stream.advance(2);
  2312. this.metadata[field] = this.stream.readUInt16() + ' of ' + this.stream.readUInt16();
  2313. return this.stream.advance(this.len - 6);
  2314. };
  2315. meta('disk', 'diskNumber', diskTrack);
  2316. meta('trkn', 'trackNumber', diskTrack);
  2317. bool = function(field) {
  2318. return this.metadata[field] = this.stream.readUInt8() === 1;
  2319. };
  2320. meta('cpil', 'compilation', bool);
  2321. meta('pcst', 'podcast', bool);
  2322. meta('pgap', 'gapless', bool);
  2323. return M4ADemuxer;
  2324. })(Demuxer);
  2325. module.exports = M4ADemuxer;
  2326. },{"../demuxer":15}],20:[function(_dereq_,module,exports){
  2327. var Demuxer, WAVEDemuxer,
  2328. __hasProp = {}.hasOwnProperty,
  2329. __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
  2330. Demuxer = _dereq_('../demuxer');
  2331. WAVEDemuxer = (function(_super) {
  2332. var formats;
  2333. __extends(WAVEDemuxer, _super);
  2334. function WAVEDemuxer() {
  2335. return WAVEDemuxer.__super__.constructor.apply(this, arguments);
  2336. }
  2337. Demuxer.register(WAVEDemuxer);
  2338. WAVEDemuxer.probe = function(buffer) {
  2339. return buffer.peekString(0, 4) === 'RIFF' && buffer.peekString(8, 4) === 'WAVE';
  2340. };
  2341. formats = {
  2342. 0x0001: 'lpcm',
  2343. 0x0003: 'lpcm',
  2344. 0x0006: 'alaw',
  2345. 0x0007: 'ulaw'
  2346. };
  2347. WAVEDemuxer.prototype.readChunk = function() {
  2348. var buffer, bytes, encoding;
  2349. if (!this.readStart && this.stream.available(12)) {
  2350. if (this.stream.readString(4) !== 'RIFF') {
  2351. return this.emit('error', 'Invalid WAV file.');
  2352. }
  2353. this.fileSize = this.stream.readUInt32(true);
  2354. this.readStart = true;
  2355. if (this.stream.readString(4) !== 'WAVE') {
  2356. return this.emit('error', 'Invalid WAV file.');
  2357. }
  2358. }
  2359. while (this.stream.available(1)) {
  2360. if (!this.readHeaders && this.stream.available(8)) {
  2361. this.type = this.stream.readString(4);
  2362. this.len = this.stream.readUInt32(true);
  2363. }
  2364. switch (this.type) {
  2365. case 'fmt ':
  2366. encoding = this.stream.readUInt16(true);
  2367. if (!(encoding in formats)) {
  2368. return this.emit('error', 'Unsupported format in WAV file.');
  2369. }
  2370. this.format = {
  2371. formatID: formats[encoding],
  2372. floatingPoint: encoding === 0x0003,
  2373. littleEndian: formats[encoding] === 'lpcm',
  2374. channelsPerFrame: this.stream.readUInt16(true),
  2375. sampleRate: this.stream.readUInt32(true),
  2376. framesPerPacket: 1
  2377. };
  2378. this.stream.advance(4);
  2379. this.stream.advance(2);
  2380. this.format.bitsPerChannel = this.stream.readUInt16(true);
  2381. this.format.bytesPerPacket = (this.format.bitsPerChannel / 8) * this.format.channelsPerFrame;
  2382. this.emit('format', this.format);
  2383. this.stream.advance(this.len - 16);
  2384. break;
  2385. case 'data':
  2386. if (!this.sentDuration) {
  2387. bytes = this.format.bitsPerChannel / 8;
  2388. this.emit('duration', this.len / bytes / this.format.channelsPerFrame / this.format.sampleRate * 1000 | 0);
  2389. this.sentDuration = true;
  2390. }
  2391. buffer = this.stream.readSingleBuffer(this.len);
  2392. this.len -= buffer.length;
  2393. this.readHeaders = this.len > 0;
  2394. this.emit('data', buffer);
  2395. break;
  2396. default:
  2397. if (!this.stream.available(this.len)) {
  2398. return;
  2399. }
  2400. this.stream.advance(this.len);
  2401. }
  2402. if (this.type !== 'data') {
  2403. this.readHeaders = false;
  2404. }
  2405. }
  2406. };
  2407. return WAVEDemuxer;
  2408. })(Demuxer);
  2409. },{"../demuxer":15}],21:[function(_dereq_,module,exports){
  2410. var AudioDevice, EventEmitter,
  2411. __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; },
  2412. __hasProp = {}.hasOwnProperty,
  2413. __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
  2414. EventEmitter = _dereq_('./core/events');
  2415. AudioDevice = (function(_super) {
  2416. var devices;
  2417. __extends(AudioDevice, _super);
  2418. function AudioDevice(sampleRate, channels) {
  2419. this.sampleRate = sampleRate;
  2420. this.channels = channels;
  2421. this.updateTime = __bind(this.updateTime, this);
  2422. this.playing = false;
  2423. this.currentTime = 0;
  2424. this._lastTime = 0;
  2425. }
  2426. AudioDevice.prototype.start = function() {
  2427. if (this.playing) {
  2428. return;
  2429. }
  2430. this.playing = true;
  2431. if (this.device == null) {
  2432. this.device = AudioDevice.create(this.sampleRate, this.channels);
  2433. }
  2434. if (!this.device) {
  2435. throw new Error("No supported audio device found.");
  2436. }
  2437. this._lastTime = this.device.getDeviceTime();
  2438. this._timer = setInterval(this.updateTime, 200);
  2439. return this.device.on('refill', this.refill = (function(_this) {
  2440. return function(buffer) {
  2441. return _this.emit('refill', buffer);
  2442. };
  2443. })(this));
  2444. };
  2445. AudioDevice.prototype.stop = function() {
  2446. if (!this.playing) {
  2447. return;
  2448. }
  2449. this.playing = false;
  2450. this.device.off('refill', this.refill);
  2451. return clearInterval(this._timer);
  2452. };
  2453. AudioDevice.prototype.destroy = function() {
  2454. this.stop();
  2455. return this.device.destroy();
  2456. };
  2457. AudioDevice.prototype.seek = function(currentTime) {
  2458. this.currentTime = currentTime;
  2459. if (this.playing) {
  2460. this._lastTime = this.device.getDeviceTime();
  2461. }
  2462. return this.emit('timeUpdate', this.currentTime);
  2463. };
  2464. AudioDevice.prototype.updateTime = function() {
  2465. var time;
  2466. time = this.device.getDeviceTime();
  2467. this.currentTime += (time - this._lastTime) / this.device.sampleRate * 1000 | 0;
  2468. this._lastTime = time;
  2469. return this.emit('timeUpdate', this.currentTime);
  2470. };
  2471. devices = [];
  2472. AudioDevice.register = function(device) {
  2473. return devices.push(device);
  2474. };
  2475. AudioDevice.create = function(sampleRate, channels) {
  2476. var device, _i, _len;
  2477. for (_i = 0, _len = devices.length; _i < _len; _i++) {
  2478. device = devices[_i];
  2479. if (device.supported) {
  2480. return new device(sampleRate, channels);
  2481. }
  2482. }
  2483. return null;
  2484. };
  2485. return AudioDevice;
  2486. })(EventEmitter);
  2487. module.exports = AudioDevice;
  2488. },{"./core/events":9}],22:[function(_dereq_,module,exports){
  2489. var AVBuffer, AudioDevice, EventEmitter, MozillaAudioDevice,
  2490. __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; },
  2491. __hasProp = {}.hasOwnProperty,
  2492. __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
  2493. EventEmitter = _dereq_('../core/events');
  2494. AudioDevice = _dereq_('../device');
  2495. AVBuffer = _dereq_('../core/buffer');
  2496. MozillaAudioDevice = (function(_super) {
  2497. var createTimer, destroyTimer;
  2498. __extends(MozillaAudioDevice, _super);
  2499. AudioDevice.register(MozillaAudioDevice);
  2500. MozillaAudioDevice.supported = (typeof Audio !== "undefined" && Audio !== null) && 'mozWriteAudio' in new Audio;
  2501. function MozillaAudioDevice(sampleRate, channels) {
  2502. this.sampleRate = sampleRate;
  2503. this.channels = channels;
  2504. this.refill = __bind(this.refill, this);
  2505. this.audio = new Audio;
  2506. this.audio.mozSetup(this.channels, this.sampleRate);
  2507. this.writePosition = 0;
  2508. this.prebufferSize = this.sampleRate / 2;
  2509. this.tail = null;
  2510. this.timer = createTimer(this.refill, 100);
  2511. }
  2512. MozillaAudioDevice.prototype.refill = function() {
  2513. var available, buffer, currentPosition, written;
  2514. if (this.tail) {
  2515. written = this.audio.mozWriteAudio(this.tail);
  2516. this.writePosition += written;
  2517. if (this.writePosition < this.tail.length) {
  2518. this.tail = this.tail.subarray(written);
  2519. } else {
  2520. this.tail = null;
  2521. }
  2522. }
  2523. currentPosition = this.audio.mozCurrentSampleOffset();
  2524. available = currentPosition + this.prebufferSize - this.writePosition;
  2525. if (available > 0) {
  2526. buffer = new Float32Array(available);
  2527. this.emit('refill', buffer);
  2528. written = this.audio.mozWriteAudio(buffer);
  2529. if (written < buffer.length) {
  2530. this.tail = buffer.subarray(written);
  2531. }
  2532. this.writePosition += written;
  2533. }
  2534. };
  2535. MozillaAudioDevice.prototype.destroy = function() {
  2536. return destroyTimer(this.timer);
  2537. };
  2538. MozillaAudioDevice.prototype.getDeviceTime = function() {
  2539. return this.audio.mozCurrentSampleOffset() / this.channels;
  2540. };
  2541. createTimer = function(fn, interval) {
  2542. var url, worker;
  2543. url = AVBuffer.makeBlobURL("setInterval(function() { postMessage('ping'); }, " + interval + ");");
  2544. if (url == null) {
  2545. return setInterval(fn, interval);
  2546. }
  2547. worker = new Worker(url);
  2548. worker.onmessage = fn;
  2549. worker.url = url;
  2550. return worker;
  2551. };
  2552. destroyTimer = function(timer) {
  2553. if (timer.terminate) {
  2554. timer.terminate();
  2555. return URL.revokeObjectURL(timer.url);
  2556. } else {
  2557. return clearInterval(timer);
  2558. }
  2559. };
  2560. return MozillaAudioDevice;
  2561. })(EventEmitter);
  2562. },{"../core/buffer":7,"../core/events":9,"../device":21}],23:[function(_dereq_,module,exports){
  2563. /*
  2564. * This resampler is from XAudioJS: https://github.com/grantgalitz/XAudioJS
  2565. * Planned to be replaced with src.js, eventually: https://github.com/jussi-kalliokoski/src.js
  2566. */
  2567. //JavaScript Audio Resampler (c) 2011 - Grant Galitz
  2568. function Resampler(fromSampleRate, toSampleRate, channels, outputBufferSize, noReturn) {
  2569. this.fromSampleRate = fromSampleRate;
  2570. this.toSampleRate = toSampleRate;
  2571. this.channels = channels | 0;
  2572. this.outputBufferSize = outputBufferSize;
  2573. this.noReturn = !!noReturn;
  2574. this.initialize();
  2575. }
  2576. Resampler.prototype.initialize = function () {
  2577. //Perform some checks:
  2578. if (this.fromSampleRate > 0 && this.toSampleRate > 0 && this.channels > 0) {
  2579. if (this.fromSampleRate == this.toSampleRate) {
  2580. //Setup a resampler bypass:
  2581. this.resampler = this.bypassResampler; //Resampler just returns what was passed through.
  2582. this.ratioWeight = 1;
  2583. }
  2584. else {
  2585. if (this.fromSampleRate < this.toSampleRate) {
  2586. /*
  2587. Use generic linear interpolation if upsampling,
  2588. as linear interpolation produces a gradient that we want
  2589. and works fine with two input sample points per output in this case.
  2590. */
  2591. this.compileLinearInterpolationFunction();
  2592. this.lastWeight = 1;
  2593. }
  2594. else {
  2595. /*
  2596. Custom resampler I wrote that doesn't skip samples
  2597. like standard linear interpolation in high downsampling.
  2598. This is more accurate than linear interpolation on downsampling.
  2599. */
  2600. this.compileMultiTapFunction();
  2601. this.tailExists = false;
  2602. this.lastWeight = 0;
  2603. }
  2604. this.ratioWeight = this.fromSampleRate / this.toSampleRate;
  2605. this.initializeBuffers();
  2606. }
  2607. }
  2608. else {
  2609. throw(new Error("Invalid settings specified for the resampler."));
  2610. }
  2611. };
  2612. Resampler.prototype.compileLinearInterpolationFunction = function () {
  2613. var toCompile = "var bufferLength = buffer.length;\
  2614. var outLength = this.outputBufferSize;\
  2615. if ((bufferLength % " + this.channels + ") == 0) {\
  2616. if (bufferLength > 0) {\
  2617. var ratioWeight = this.ratioWeight;\
  2618. var weight = this.lastWeight;\
  2619. var firstWeight = 0;\
  2620. var secondWeight = 0;\
  2621. var sourceOffset = 0;\
  2622. var outputOffset = 0;\
  2623. var outputBuffer = this.outputBuffer;\
  2624. for (; weight < 1; weight += ratioWeight) {\
  2625. secondWeight = weight % 1;\
  2626. firstWeight = 1 - secondWeight;";
  2627. for (var channel = 0; channel < this.channels; ++channel) {
  2628. toCompile += "outputBuffer[outputOffset++] = (this.lastOutput[" + channel + "] * firstWeight) + (buffer[" + channel + "] * secondWeight);";
  2629. }
  2630. toCompile += "}\
  2631. weight -= 1;\
  2632. for (bufferLength -= " + this.channels + ", sourceOffset = Math.floor(weight) * " + this.channels + "; outputOffset < outLength && sourceOffset < bufferLength;) {\
  2633. secondWeight = weight % 1;\
  2634. firstWeight = 1 - secondWeight;";
  2635. for (var channel = 0; channel < this.channels; ++channel) {
  2636. toCompile += "outputBuffer[outputOffset++] = (buffer[sourceOffset" + ((channel > 0) ? (" + " + channel) : "") + "] * firstWeight) + (buffer[sourceOffset + " + (this.channels + channel) + "] * secondWeight);";
  2637. }
  2638. toCompile += "weight += ratioWeight;\
  2639. sourceOffset = Math.floor(weight) * " + this.channels + ";\
  2640. }";
  2641. for (var channel = 0; channel < this.channels; ++channel) {
  2642. toCompile += "this.lastOutput[" + channel + "] = buffer[sourceOffset++];";
  2643. }
  2644. toCompile += "this.lastWeight = weight % 1;\
  2645. return this.bufferSlice(outputOffset);\
  2646. }\
  2647. else {\
  2648. return (this.noReturn) ? 0 : [];\
  2649. }\
  2650. }\
  2651. else {\
  2652. throw(new Error(\"Buffer was of incorrect sample length.\"));\
  2653. }";
  2654. this.resampler = Function("buffer", toCompile);
  2655. };
  2656. Resampler.prototype.compileMultiTapFunction = function () {
  2657. var toCompile = "var bufferLength = buffer.length;\
  2658. var outLength = this.outputBufferSize;\
  2659. if ((bufferLength % " + this.channels + ") == 0) {\
  2660. if (bufferLength > 0) {\
  2661. var ratioWeight = this.ratioWeight;\
  2662. var weight = 0;";
  2663. for (var channel = 0; channel < this.channels; ++channel) {
  2664. toCompile += "var output" + channel + " = 0;"
  2665. }
  2666. toCompile += "var actualPosition = 0;\
  2667. var amountToNext = 0;\
  2668. var alreadyProcessedTail = !this.tailExists;\
  2669. this.tailExists = false;\
  2670. var outputBuffer = this.outputBuffer;\
  2671. var outputOffset = 0;\
  2672. var currentPosition = 0;\
  2673. do {\
  2674. if (alreadyProcessedTail) {\
  2675. weight = ratioWeight;";
  2676. for (channel = 0; channel < this.channels; ++channel) {
  2677. toCompile += "output" + channel + " = 0;"
  2678. }
  2679. toCompile += "}\
  2680. else {\
  2681. weight = this.lastWeight;";
  2682. for (channel = 0; channel < this.channels; ++channel) {
  2683. toCompile += "output" + channel + " = this.lastOutput[" + channel + "];"
  2684. }
  2685. toCompile += "alreadyProcessedTail = true;\
  2686. }\
  2687. while (weight > 0 && actualPosition < bufferLength) {\
  2688. amountToNext = 1 + actualPosition - currentPosition;\
  2689. if (weight >= amountToNext) {";
  2690. for (channel = 0; channel < this.channels; ++channel) {
  2691. toCompile += "output" + channel + " += buffer[actualPosition++] * amountToNext;"
  2692. }
  2693. toCompile += "currentPosition = actualPosition;\
  2694. weight -= amountToNext;\
  2695. }\
  2696. else {";
  2697. for (channel = 0; channel < this.channels; ++channel) {
  2698. toCompile += "output" + channel + " += buffer[actualPosition" + ((channel > 0) ? (" + " + channel) : "") + "] * weight;"
  2699. }
  2700. toCompile += "currentPosition += weight;\
  2701. weight = 0;\
  2702. break;\
  2703. }\
  2704. }\
  2705. if (weight == 0) {";
  2706. for (channel = 0; channel < this.channels; ++channel) {
  2707. toCompile += "outputBuffer[outputOffset++] = output" + channel + " / ratioWeight;"
  2708. }
  2709. toCompile += "}\
  2710. else {\
  2711. this.lastWeight = weight;";
  2712. for (channel = 0; channel < this.channels; ++channel) {
  2713. toCompile += "this.lastOutput[" + channel + "] = output" + channel + ";"
  2714. }
  2715. toCompile += "this.tailExists = true;\
  2716. break;\
  2717. }\
  2718. } while (actualPosition < bufferLength && outputOffset < outLength);\
  2719. return this.bufferSlice(outputOffset);\
  2720. }\
  2721. else {\
  2722. return (this.noReturn) ? 0 : [];\
  2723. }\
  2724. }\
  2725. else {\
  2726. throw(new Error(\"Buffer was of incorrect sample length.\"));\
  2727. }";
  2728. this.resampler = Function("buffer", toCompile);
  2729. };
  2730. Resampler.prototype.bypassResampler = function (buffer) {
  2731. if (this.noReturn) {
  2732. //Set the buffer passed as our own, as we don't need to resample it:
  2733. this.outputBuffer = buffer;
  2734. return buffer.length;
  2735. }
  2736. else {
  2737. //Just return the buffer passsed:
  2738. return buffer;
  2739. }
  2740. };
  2741. Resampler.prototype.bufferSlice = function (sliceAmount) {
  2742. if (this.noReturn) {
  2743. //If we're going to access the properties directly from this object:
  2744. return sliceAmount;
  2745. }
  2746. else {
  2747. //Typed array and normal array buffer section referencing:
  2748. try {
  2749. return this.outputBuffer.subarray(0, sliceAmount);
  2750. }
  2751. catch (error) {
  2752. try {
  2753. //Regular array pass:
  2754. this.outputBuffer.length = sliceAmount;
  2755. return this.outputBuffer;
  2756. }
  2757. catch (error) {
  2758. //Nightly Firefox 4 used to have the subarray function named as slice:
  2759. return this.outputBuffer.slice(0, sliceAmount);
  2760. }
  2761. }
  2762. }
  2763. };
  2764. Resampler.prototype.initializeBuffers = function () {
  2765. //Initialize the internal buffer:
  2766. try {
  2767. this.outputBuffer = new Float32Array(this.outputBufferSize);
  2768. this.lastOutput = new Float32Array(this.channels);
  2769. }
  2770. catch (error) {
  2771. this.outputBuffer = [];
  2772. this.lastOutput = [];
  2773. }
  2774. };
  2775. module.exports = Resampler;
  2776. },{}],24:[function(_dereq_,module,exports){
  2777. (function (global){
  2778. var AudioDevice, EventEmitter, Resampler, WebAudioDevice,
  2779. __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; },
  2780. __hasProp = {}.hasOwnProperty,
  2781. __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
  2782. EventEmitter = _dereq_('../core/events');
  2783. AudioDevice = _dereq_('../device');
  2784. Resampler = _dereq_('./resampler');
  2785. WebAudioDevice = (function(_super) {
  2786. var AudioContext, createProcessor, sharedContext;
  2787. __extends(WebAudioDevice, _super);
  2788. AudioDevice.register(WebAudioDevice);
  2789. AudioContext = global.AudioContext || global.webkitAudioContext;
  2790. WebAudioDevice.supported = AudioContext && (typeof AudioContext.prototype[createProcessor = 'createScriptProcessor'] === 'function' || typeof AudioContext.prototype[createProcessor = 'createJavaScriptNode'] === 'function');
  2791. sharedContext = null;
  2792. function WebAudioDevice(sampleRate, channels) {
  2793. this.sampleRate = sampleRate;
  2794. this.channels = channels;
  2795. this.refill = __bind(this.refill, this);
  2796. this.context = sharedContext != null ? sharedContext : sharedContext = new AudioContext;
  2797. this.deviceSampleRate = this.context.sampleRate;
  2798. this.bufferSize = Math.ceil(4096 / (this.deviceSampleRate / this.sampleRate) * this.channels);
  2799. this.bufferSize += this.bufferSize % this.channels;
  2800. if (this.deviceSampleRate !== this.sampleRate) {
  2801. this.resampler = new Resampler(this.sampleRate, this.deviceSampleRate, this.channels, 4096 * this.channels);
  2802. }
  2803. this.node = this.context[createProcessor](4096, this.channels, this.channels);
  2804. this.node.onaudioprocess = this.refill;
  2805. this.node.connect(this.context.destination);
  2806. }
  2807. WebAudioDevice.prototype.refill = function(event) {
  2808. var channelCount, channels, data, i, n, outputBuffer, _i, _j, _k, _ref;
  2809. outputBuffer = event.outputBuffer;
  2810. channelCount = outputBuffer.numberOfChannels;
  2811. channels = new Array(channelCount);
  2812. for (i = _i = 0; _i < channelCount; i = _i += 1) {
  2813. channels[i] = outputBuffer.getChannelData(i);
  2814. }
  2815. data = new Float32Array(this.bufferSize);
  2816. this.emit('refill', data);
  2817. if (this.resampler) {
  2818. data = this.resampler.resampler(data);
  2819. }
  2820. for (i = _j = 0, _ref = outputBuffer.length; _j < _ref; i = _j += 1) {
  2821. for (n = _k = 0; _k < channelCount; n = _k += 1) {
  2822. channels[n][i] = data[i * channelCount + n];
  2823. }
  2824. }
  2825. };
  2826. WebAudioDevice.prototype.destroy = function() {
  2827. return this.node.disconnect(0);
  2828. };
  2829. WebAudioDevice.prototype.getDeviceTime = function() {
  2830. return this.context.currentTime * this.sampleRate;
  2831. };
  2832. return WebAudioDevice;
  2833. })(EventEmitter);
  2834. }).call(this,typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {})
  2835. },{"../core/events":9,"../device":21,"./resampler":23}],25:[function(_dereq_,module,exports){
  2836. var Filter;
  2837. Filter = (function() {
  2838. function Filter(context, key) {
  2839. if (context && key) {
  2840. Object.defineProperty(this, 'value', {
  2841. get: function() {
  2842. return context[key];
  2843. }
  2844. });
  2845. }
  2846. }
  2847. Filter.prototype.process = function(buffer) {};
  2848. return Filter;
  2849. })();
  2850. module.exports = Filter;
  2851. },{}],26:[function(_dereq_,module,exports){
  2852. var BalanceFilter, Filter,
  2853. __hasProp = {}.hasOwnProperty,
  2854. __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
  2855. Filter = _dereq_('../filter');
  2856. BalanceFilter = (function(_super) {
  2857. __extends(BalanceFilter, _super);
  2858. function BalanceFilter() {
  2859. return BalanceFilter.__super__.constructor.apply(this, arguments);
  2860. }
  2861. BalanceFilter.prototype.process = function(buffer) {
  2862. var i, pan, _i, _ref;
  2863. if (this.value === 0) {
  2864. return;
  2865. }
  2866. pan = Math.max(-50, Math.min(50, this.value));
  2867. for (i = _i = 0, _ref = buffer.length; _i < _ref; i = _i += 2) {
  2868. buffer[i] *= Math.min(1, (50 - pan) / 50);
  2869. buffer[i + 1] *= Math.min(1, (50 + pan) / 50);
  2870. }
  2871. };
  2872. return BalanceFilter;
  2873. })(Filter);
  2874. module.exports = BalanceFilter;
  2875. },{"../filter":25}],27:[function(_dereq_,module,exports){
  2876. var Filter, VolumeFilter,
  2877. __hasProp = {}.hasOwnProperty,
  2878. __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
  2879. Filter = _dereq_('../filter');
  2880. VolumeFilter = (function(_super) {
  2881. __extends(VolumeFilter, _super);
  2882. function VolumeFilter() {
  2883. return VolumeFilter.__super__.constructor.apply(this, arguments);
  2884. }
  2885. VolumeFilter.prototype.process = function(buffer) {
  2886. var i, vol, _i, _ref;
  2887. if (this.value >= 100) {
  2888. return;
  2889. }
  2890. vol = Math.max(0, Math.min(100, this.value)) / 100;
  2891. for (i = _i = 0, _ref = buffer.length; _i < _ref; i = _i += 1) {
  2892. buffer[i] *= vol;
  2893. }
  2894. };
  2895. return VolumeFilter;
  2896. })(Filter);
  2897. module.exports = VolumeFilter;
  2898. },{"../filter":25}],28:[function(_dereq_,module,exports){
  2899. var Asset, AudioDevice, BalanceFilter, EventEmitter, Player, Queue, VolumeFilter,
  2900. __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; },
  2901. __hasProp = {}.hasOwnProperty,
  2902. __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
  2903. EventEmitter = _dereq_('./core/events');
  2904. Asset = _dereq_('./asset');
  2905. VolumeFilter = _dereq_('./filters/volume');
  2906. BalanceFilter = _dereq_('./filters/balance');
  2907. Queue = _dereq_('./queue');
  2908. AudioDevice = _dereq_('./device');
  2909. Player = (function(_super) {
  2910. __extends(Player, _super);
  2911. function Player(asset) {
  2912. this.asset = asset;
  2913. this.startPlaying = __bind(this.startPlaying, this);
  2914. this.playing = false;
  2915. this.buffered = 0;
  2916. this.currentTime = 0;
  2917. this.duration = 0;
  2918. this.volume = 100;
  2919. this.pan = 0;
  2920. this.metadata = {};
  2921. this.filters = [new VolumeFilter(this, 'volume'), new BalanceFilter(this, 'pan')];
  2922. this.asset.on('buffer', (function(_this) {
  2923. return function(buffered) {
  2924. _this.buffered = buffered;
  2925. return _this.emit('buffer', _this.buffered);
  2926. };
  2927. })(this));
  2928. this.asset.on('decodeStart', (function(_this) {
  2929. return function() {
  2930. _this.queue = new Queue(_this.asset);
  2931. return _this.queue.once('ready', _this.startPlaying);
  2932. };
  2933. })(this));
  2934. this.asset.on('format', (function(_this) {
  2935. return function(format) {
  2936. _this.format = format;
  2937. return _this.emit('format', _this.format);
  2938. };
  2939. })(this));
  2940. this.asset.on('metadata', (function(_this) {
  2941. return function(metadata) {
  2942. _this.metadata = metadata;
  2943. return _this.emit('metadata', _this.metadata);
  2944. };
  2945. })(this));
  2946. this.asset.on('duration', (function(_this) {
  2947. return function(duration) {
  2948. _this.duration = duration;
  2949. return _this.emit('duration', _this.duration);
  2950. };
  2951. })(this));
  2952. this.asset.on('error', (function(_this) {
  2953. return function(error) {
  2954. return _this.emit('error', error);
  2955. };
  2956. })(this));
  2957. }
  2958. Player.fromURL = function(url) {
  2959. return new Player(Asset.fromURL(url));
  2960. };
  2961. Player.fromFile = function(file) {
  2962. return new Player(Asset.fromFile(file));
  2963. };
  2964. Player.fromBuffer = function(buffer) {
  2965. return new Player(Asset.fromBuffer(buffer));
  2966. };
  2967. Player.prototype.preload = function() {
  2968. if (!this.asset) {
  2969. return;
  2970. }
  2971. this.startedPreloading = true;
  2972. return this.asset.start(false);
  2973. };
  2974. Player.prototype.play = function() {
  2975. var _ref;
  2976. if (this.playing) {
  2977. return;
  2978. }
  2979. if (!this.startedPreloading) {
  2980. this.preload();
  2981. }
  2982. this.playing = true;
  2983. return (_ref = this.device) != null ? _ref.start() : void 0;
  2984. };
  2985. Player.prototype.pause = function() {
  2986. var _ref;
  2987. if (!this.playing) {
  2988. return;
  2989. }
  2990. this.playing = false;
  2991. return (_ref = this.device) != null ? _ref.stop() : void 0;
  2992. };
  2993. Player.prototype.togglePlayback = function() {
  2994. if (this.playing) {
  2995. return this.pause();
  2996. } else {
  2997. return this.play();
  2998. }
  2999. };
  3000. Player.prototype.stop = function() {
  3001. var _ref;
  3002. this.pause();
  3003. this.asset.stop();
  3004. return (_ref = this.device) != null ? _ref.destroy() : void 0;
  3005. };
  3006. Player.prototype.seek = function(timestamp) {
  3007. var _ref;
  3008. if ((_ref = this.device) != null) {
  3009. _ref.stop();
  3010. }
  3011. this.queue.once('ready', (function(_this) {
  3012. return function() {
  3013. var _ref1, _ref2;
  3014. if ((_ref1 = _this.device) != null) {
  3015. _ref1.seek(_this.currentTime);
  3016. }
  3017. if (_this.playing) {
  3018. return (_ref2 = _this.device) != null ? _ref2.start() : void 0;
  3019. }
  3020. };
  3021. })(this));
  3022. timestamp = (timestamp / 1000) * this.format.sampleRate;
  3023. timestamp = this.asset.decoder.seek(timestamp);
  3024. this.currentTime = timestamp / this.format.sampleRate * 1000 | 0;
  3025. this.queue.reset();
  3026. return this.currentTime;
  3027. };
  3028. Player.prototype.startPlaying = function() {
  3029. var frame, frameOffset;
  3030. frame = this.queue.read();
  3031. frameOffset = 0;
  3032. this.device = new AudioDevice(this.format.sampleRate, this.format.channelsPerFrame);
  3033. this.device.on('timeUpdate', (function(_this) {
  3034. return function(currentTime) {
  3035. _this.currentTime = currentTime;
  3036. return _this.emit('progress', _this.currentTime);
  3037. };
  3038. })(this));
  3039. this.refill = (function(_this) {
  3040. return function(buffer) {
  3041. var bufferOffset, filter, i, max, _i, _j, _len, _ref;
  3042. if (!_this.playing) {
  3043. return;
  3044. }
  3045. if (!frame) {
  3046. frame = _this.queue.read();
  3047. frameOffset = 0;
  3048. }
  3049. bufferOffset = 0;
  3050. while (frame && bufferOffset < buffer.length) {
  3051. max = Math.min(frame.length - frameOffset, buffer.length - bufferOffset);
  3052. for (i = _i = 0; _i < max; i = _i += 1) {
  3053. buffer[bufferOffset++] = frame[frameOffset++];
  3054. }
  3055. if (frameOffset === frame.length) {
  3056. frame = _this.queue.read();
  3057. frameOffset = 0;
  3058. }
  3059. }
  3060. _ref = _this.filters;
  3061. for (_j = 0, _len = _ref.length; _j < _len; _j++) {
  3062. filter = _ref[_j];
  3063. filter.process(buffer);
  3064. }
  3065. if (!frame) {
  3066. if (_this.queue.ended) {
  3067. _this.currentTime = _this.duration;
  3068. _this.emit('progress', _this.currentTime);
  3069. _this.emit('end');
  3070. _this.stop();
  3071. } else {
  3072. _this.device.stop();
  3073. }
  3074. }
  3075. };
  3076. })(this);
  3077. this.device.on('refill', this.refill);
  3078. if (this.playing) {
  3079. this.device.start();
  3080. }
  3081. return this.emit('ready');
  3082. };
  3083. return Player;
  3084. })(EventEmitter);
  3085. module.exports = Player;
  3086. },{"./asset":2,"./core/events":9,"./device":21,"./filters/balance":26,"./filters/volume":27,"./queue":29}],29:[function(_dereq_,module,exports){
  3087. var EventEmitter, Queue,
  3088. __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; },
  3089. __hasProp = {}.hasOwnProperty,
  3090. __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
  3091. EventEmitter = _dereq_('./core/events');
  3092. Queue = (function(_super) {
  3093. __extends(Queue, _super);
  3094. function Queue(asset) {
  3095. this.asset = asset;
  3096. this.write = __bind(this.write, this);
  3097. this.readyMark = 64;
  3098. this.finished = false;
  3099. this.buffering = true;
  3100. this.ended = false;
  3101. this.buffers = [];
  3102. this.asset.on('data', this.write);
  3103. this.asset.on('end', (function(_this) {
  3104. return function() {
  3105. return _this.ended = true;
  3106. };
  3107. })(this));
  3108. this.asset.decodePacket();
  3109. }
  3110. Queue.prototype.write = function(buffer) {
  3111. if (buffer) {
  3112. this.buffers.push(buffer);
  3113. }
  3114. if (this.buffering) {
  3115. if (this.buffers.length >= this.readyMark || this.ended) {
  3116. this.buffering = false;
  3117. return this.emit('ready');
  3118. } else {
  3119. return this.asset.decodePacket();
  3120. }
  3121. }
  3122. };
  3123. Queue.prototype.read = function() {
  3124. if (this.buffers.length === 0) {
  3125. return null;
  3126. }
  3127. this.asset.decodePacket();
  3128. return this.buffers.shift();
  3129. };
  3130. Queue.prototype.reset = function() {
  3131. this.buffers.length = 0;
  3132. this.buffering = true;
  3133. return this.asset.decodePacket();
  3134. };
  3135. return Queue;
  3136. })(EventEmitter);
  3137. module.exports = Queue;
  3138. },{"./core/events":9}],30:[function(_dereq_,module,exports){
  3139. var AVBuffer, EventEmitter, FileSource,
  3140. __hasProp = {}.hasOwnProperty,
  3141. __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
  3142. EventEmitter = _dereq_('../../core/events');
  3143. AVBuffer = _dereq_('../../core/buffer');
  3144. FileSource = (function(_super) {
  3145. __extends(FileSource, _super);
  3146. function FileSource(file) {
  3147. this.file = file;
  3148. if (typeof FileReader === "undefined" || FileReader === null) {
  3149. return this.emit('error', 'This browser does not have FileReader support.');
  3150. }
  3151. this.offset = 0;
  3152. this.length = this.file.size;
  3153. this.chunkSize = 1 << 20;
  3154. this.file[this.slice = 'slice'] || this.file[this.slice = 'webkitSlice'] || this.file[this.slice = 'mozSlice'];
  3155. }
  3156. FileSource.prototype.start = function() {
  3157. if (this.reader) {
  3158. if (!this.active) {
  3159. return this.loop();
  3160. }
  3161. }
  3162. this.reader = new FileReader;
  3163. this.active = true;
  3164. this.reader.onload = (function(_this) {
  3165. return function(e) {
  3166. var buf;
  3167. buf = new AVBuffer(new Uint8Array(e.target.result));
  3168. _this.offset += buf.length;
  3169. _this.emit('data', buf);
  3170. _this.active = false;
  3171. if (_this.offset < _this.length) {
  3172. return _this.loop();
  3173. }
  3174. };
  3175. })(this);
  3176. this.reader.onloadend = (function(_this) {
  3177. return function() {
  3178. if (_this.offset === _this.length) {
  3179. _this.emit('end');
  3180. return _this.reader = null;
  3181. }
  3182. };
  3183. })(this);
  3184. this.reader.onerror = (function(_this) {
  3185. return function(e) {
  3186. return _this.emit('error', e);
  3187. };
  3188. })(this);
  3189. this.reader.onprogress = (function(_this) {
  3190. return function(e) {
  3191. return _this.emit('progress', (_this.offset + e.loaded) / _this.length * 100);
  3192. };
  3193. })(this);
  3194. return this.loop();
  3195. };
  3196. FileSource.prototype.loop = function() {
  3197. var blob, endPos;
  3198. this.active = true;
  3199. endPos = Math.min(this.offset + this.chunkSize, this.length);
  3200. blob = this.file[this.slice](this.offset, endPos);
  3201. return this.reader.readAsArrayBuffer(blob);
  3202. };
  3203. FileSource.prototype.pause = function() {
  3204. var _ref;
  3205. this.active = false;
  3206. try {
  3207. return (_ref = this.reader) != null ? _ref.abort() : void 0;
  3208. } catch (_error) {}
  3209. };
  3210. FileSource.prototype.reset = function() {
  3211. this.pause();
  3212. return this.offset = 0;
  3213. };
  3214. return FileSource;
  3215. })(EventEmitter);
  3216. module.exports = FileSource;
  3217. },{"../../core/buffer":7,"../../core/events":9}],31:[function(_dereq_,module,exports){
  3218. var AVBuffer, EventEmitter, HTTPSource,
  3219. __hasProp = {}.hasOwnProperty,
  3220. __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
  3221. EventEmitter = _dereq_('../../core/events');
  3222. AVBuffer = _dereq_('../../core/buffer');
  3223. HTTPSource = (function(_super) {
  3224. __extends(HTTPSource, _super);
  3225. function HTTPSource(url) {
  3226. this.url = url;
  3227. this.chunkSize = 1 << 20;
  3228. this.inflight = false;
  3229. this.reset();
  3230. }
  3231. HTTPSource.prototype.start = function() {
  3232. if (this.length) {
  3233. if (!this.inflight) {
  3234. return this.loop();
  3235. }
  3236. }
  3237. this.inflight = true;
  3238. this.xhr = new XMLHttpRequest();
  3239. this.xhr.onload = (function(_this) {
  3240. return function(event) {
  3241. _this.length = parseInt(_this.xhr.getResponseHeader("Content-Length"));
  3242. _this.inflight = false;
  3243. return _this.loop();
  3244. };
  3245. })(this);
  3246. this.xhr.onerror = (function(_this) {
  3247. return function(err) {
  3248. _this.pause();
  3249. return _this.emit('error', err);
  3250. };
  3251. })(this);
  3252. this.xhr.onabort = (function(_this) {
  3253. return function(event) {
  3254. return _this.inflight = false;
  3255. };
  3256. })(this);
  3257. this.xhr.open("HEAD", this.url, true);
  3258. return this.xhr.send(null);
  3259. };
  3260. HTTPSource.prototype.loop = function() {
  3261. var endPos;
  3262. if (this.inflight || !this.length) {
  3263. return this.emit('error', 'Something is wrong in HTTPSource.loop');
  3264. }
  3265. this.inflight = true;
  3266. this.xhr = new XMLHttpRequest();
  3267. this.xhr.onload = (function(_this) {
  3268. return function(event) {
  3269. var buf, buffer, i, txt, _i, _ref;
  3270. if (_this.xhr.response) {
  3271. buf = new Uint8Array(_this.xhr.response);
  3272. } else {
  3273. txt = _this.xhr.responseText;
  3274. buf = new Uint8Array(txt.length);
  3275. for (i = _i = 0, _ref = txt.length; 0 <= _ref ? _i < _ref : _i > _ref; i = 0 <= _ref ? ++_i : --_i) {
  3276. buf[i] = txt.charCodeAt(i) & 0xff;
  3277. }
  3278. }
  3279. buffer = new AVBuffer(buf);
  3280. _this.offset += buffer.length;
  3281. _this.emit('data', buffer);
  3282. if (_this.offset >= _this.length) {
  3283. _this.emit('end');
  3284. }
  3285. _this.inflight = false;
  3286. if (!(_this.offset >= _this.length)) {
  3287. return _this.loop();
  3288. }
  3289. };
  3290. })(this);
  3291. this.xhr.onprogress = (function(_this) {
  3292. return function(event) {
  3293. return _this.emit('progress', (_this.offset + event.loaded) / _this.length * 100);
  3294. };
  3295. })(this);
  3296. this.xhr.onerror = (function(_this) {
  3297. return function(err) {
  3298. _this.emit('error', err);
  3299. return _this.pause();
  3300. };
  3301. })(this);
  3302. this.xhr.onabort = (function(_this) {
  3303. return function(event) {
  3304. return _this.inflight = false;
  3305. };
  3306. })(this);
  3307. this.xhr.open("GET", this.url, true);
  3308. this.xhr.responseType = "arraybuffer";
  3309. endPos = Math.min(this.offset + this.chunkSize, this.length);
  3310. this.xhr.setRequestHeader("Range", "bytes=" + this.offset + "-" + endPos);
  3311. this.xhr.overrideMimeType('text/plain; charset=x-user-defined');
  3312. return this.xhr.send(null);
  3313. };
  3314. HTTPSource.prototype.pause = function() {
  3315. var _ref;
  3316. this.inflight = false;
  3317. return (_ref = this.xhr) != null ? _ref.abort() : void 0;
  3318. };
  3319. HTTPSource.prototype.reset = function() {
  3320. this.pause();
  3321. return this.offset = 0;
  3322. };
  3323. return HTTPSource;
  3324. })(EventEmitter);
  3325. module.exports = HTTPSource;
  3326. },{"../../core/buffer":7,"../../core/events":9}],32:[function(_dereq_,module,exports){
  3327. (function (global){
  3328. var AVBuffer, BufferList, BufferSource, EventEmitter,
  3329. __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; },
  3330. __hasProp = {}.hasOwnProperty,
  3331. __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
  3332. EventEmitter = _dereq_('../core/events');
  3333. BufferList = _dereq_('../core/bufferlist');
  3334. AVBuffer = _dereq_('../core/buffer');
  3335. BufferSource = (function(_super) {
  3336. var clearImmediate, setImmediate;
  3337. __extends(BufferSource, _super);
  3338. function BufferSource(input) {
  3339. this.loop = __bind(this.loop, this);
  3340. if (input instanceof BufferList) {
  3341. this.list = input;
  3342. } else {
  3343. this.list = new BufferList;
  3344. this.list.append(new AVBuffer(input));
  3345. }
  3346. this.paused = true;
  3347. }
  3348. setImmediate = global.setImmediate || function(fn) {
  3349. return global.setTimeout(fn, 0);
  3350. };
  3351. clearImmediate = global.clearImmediate || function(timer) {
  3352. return global.clearTimeout(timer);
  3353. };
  3354. BufferSource.prototype.start = function() {
  3355. this.paused = false;
  3356. return this._timer = setImmediate(this.loop);
  3357. };
  3358. BufferSource.prototype.loop = function() {
  3359. this.emit('progress', (this.list.numBuffers - this.list.availableBuffers + 1) / this.list.numBuffers * 100 | 0);
  3360. this.emit('data', this.list.first);
  3361. if (this.list.advance()) {
  3362. return setImmediate(this.loop);
  3363. } else {
  3364. return this.emit('end');
  3365. }
  3366. };
  3367. BufferSource.prototype.pause = function() {
  3368. clearImmediate(this._timer);
  3369. return this.paused = true;
  3370. };
  3371. BufferSource.prototype.reset = function() {
  3372. this.pause();
  3373. return this.list.rewind();
  3374. };
  3375. return BufferSource;
  3376. })(EventEmitter);
  3377. module.exports = BufferSource;
  3378. }).call(this,typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {})
  3379. },{"../core/buffer":7,"../core/bufferlist":8,"../core/events":9}]},{},[1])
  3380. (1)
  3381. });
  3382. //# sourceMappingURL=aurora.js.map