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