@@ -265,17 +265,11 @@ WebAudio.prototype.clear = function() {
265265 this . _sourceNode = null ;
266266 this . _gainNode = null ;
267267 this . _pannerNode = null ;
268- this . _partialArray = null ;
269- this . _wholeArray = null ;
270- this . _chunkGainRate = 5 ;
271- this . _chunkSize = 75 * 1024 ;
272- this . _loadedSize = 0 ;
273268 this . _totalTime = 0 ;
274269 this . _sampleRate = 0 ;
275270 this . _loopStart = 0 ;
276271 this . _loopLength = 0 ;
277272 this . _startTime = 0 ;
278- this . _offset = 0 ;
279273 this . _volume = 1 ;
280274 this . _pitch = 1 ;
281275 this . _pan = 0 ;
@@ -363,7 +357,7 @@ Object.defineProperty(WebAudio.prototype, 'pan', {
363357 * @return {Boolean } True if the audio data is ready to play
364358 */
365359WebAudio . prototype . isReady = function ( ) {
366- return ! ! this . _buffer && this . _buffer . duration >= this . _offset ;
360+ return ! ! this . _buffer ;
367361} ;
368362
369363/**
@@ -394,8 +388,8 @@ WebAudio.prototype.isPlaying = function() {
394388 * @param {Number } offset The start position to play in seconds
395389 */
396390WebAudio . prototype . play = function ( loop , offset ) {
397- this . _offset = offset = offset || 0 ;
398391 if ( this . isReady ( ) ) {
392+ offset = offset || 0 ;
399393 this . _startPlaying ( loop , offset ) ;
400394 } else if ( WebAudio . _context ) {
401395 this . _autoPlay = true ;
@@ -510,9 +504,6 @@ WebAudio.prototype._load = function(url) {
510504 var xhr = new XMLHttpRequest ( ) ;
511505 if ( Decrypter . hasEncryptedAudio ) url = Decrypter . extToEncryptExt ( url ) ;
512506 xhr . open ( 'GET' , url ) ;
513- if ( typeof require === 'undefined' ) {
514- xhr . setRequestHeader ( 'Range' , 'bytes=' + this . _loadedSize + '-' + ( this . _loadedSize + this . _chunkSize - 1 ) ) ;
515- }
516507 xhr . responseType = 'arraybuffer' ;
517508 xhr . onload = function ( ) {
518509 if ( xhr . status < 400 ) {
@@ -530,89 +521,21 @@ WebAudio.prototype._load = function(url) {
530521 * @private
531522 */
532523WebAudio . prototype . _onXhrLoad = function ( xhr ) {
533- if ( xhr . status === 206 ) {
534- this . _onPartialLoad ( xhr ) ;
535- } else {
536- this . _onWholeLoad ( xhr . response ) ;
537- }
538- } ;
539-
540- /**
541- * @method _onPartialLoad
542- * @param {XMLHttpRequest } xhr
543- * @private
544- */
545- WebAudio . prototype . _onPartialLoad = function ( xhr ) {
546524 var array = xhr . response ;
547- if ( ! this . _partialArray ) {
548- this . _partialArray = new Uint8Array ( + xhr . getResponseHeader ( 'Content-Range' ) . split ( '/' ) . pop ( ) ) ;
549- this . _chunkSize *= this . _chunkGainRate - 1 ;
550- } else {
551- this . _chunkSize = this . _partialArray . byteLength ;
552- }
553- this . _partialArray . set ( new Uint8Array ( array ) , this . _loadedSize ) ;
554- this . _loadedSize += array . byteLength ;
555- if ( this . _loadedSize < this . _partialArray . byteLength ) {
556- array = this . _partialArray . buffer . slice ( 0 , this . _loadedSize ) ;
557- this . _load ( this . _url ) ;
558- } else {
559- array = this . _partialArray . buffer ;
560- this . _partialArray = null ;
561- }
562- if ( Decrypter . hasEncryptedAudio ) {
563- array = Decrypter . decryptArrayBuffer ( array ) ;
564- }
525+ if ( Decrypter . hasEncryptedAudio ) array = Decrypter . decryptArrayBuffer ( array ) ;
565526 this . _readLoopComments ( new Uint8Array ( array ) ) ;
566- WebAudio . _context . decodeAudioData ( array , this . _onDecode . bind ( this ) ) ;
567- } ;
568-
569- /**
570- * @method _onWholeLoad
571- * @param {ArrayBuffer } array
572- * @private
573- */
574- WebAudio . prototype . _onWholeLoad = function ( array ) {
575- if ( array ) {
576- if ( Decrypter . hasEncryptedAudio ) {
577- array = Decrypter . decryptArrayBuffer ( array ) ;
578- }
579- this . _readLoopComments ( new Uint8Array ( array ) ) ;
580- if ( this . _chunkSize < array . byteLength ) {
581- this . _wholeArray = array ;
582- array = this . _wholeArray . slice ( 0 , this . _chunkSize ) ;
583- this . _chunkSize *= this . _chunkGainRate ;
584- }
585- } else if ( this . _chunkSize < this . _wholeArray . byteLength ) {
586- array = this . _wholeArray . slice ( 0 , this . _chunkSize ) ;
587- this . _chunkSize = this . _wholeArray . byteLength ;
588- } else {
589- array = this . _wholeArray ;
590- this . _wholeArray = null ;
591- }
592- WebAudio . _context . decodeAudioData ( array , this . _onDecode . bind ( this ) , function ( ) {
593- this . _onWholeLoad ( ) ;
594- } . bind ( this ) ) ;
595- } ;
596-
597- /**
598- * @method _onDecode
599- * @param {AudioBuffer } buffer
600- * @private
601- */
602- WebAudio . prototype . _onDecode = function ( buffer ) {
603- if ( ! this . _buffer || this . _buffer . length < buffer . length ) {
527+ WebAudio . _context . decodeAudioData ( array , function ( buffer ) {
604528 this . _buffer = buffer ;
605529 this . _totalTime = buffer . duration ;
606- if ( this . isPlaying ( ) ) {
607- this . _startPlaying ( this . _sourceNode . loop , this . seek ( ) ) ;
608- }
609- if ( this . isReady ( ) ) {
610- this . _onLoad ( ) ;
611- }
612- if ( this . _wholeArray ) {
613- this . _onWholeLoad ( ) ;
530+ if ( this . _loopLength > 0 && this . _sampleRate > 0 ) {
531+ this . _loopStart /= this . _sampleRate ;
532+ this . _loopLength /= this . _sampleRate ;
533+ } else {
534+ this . _loopStart = 0 ;
535+ this . _loopLength = this . _totalTime ;
614536 }
615- }
537+ this . _onLoad ( ) ;
538+ } . bind ( this ) ) ;
616539} ;
617540
618541/**
@@ -645,10 +568,8 @@ WebAudio.prototype._createNodes = function() {
645568 var context = WebAudio . _context ;
646569 this . _sourceNode = context . createBufferSource ( ) ;
647570 this . _sourceNode . buffer = this . _buffer ;
648- if ( this . _buffer . duration > this . _loopStart ) {
649- this . _sourceNode . loopStart = this . _loopStart ;
650- this . _sourceNode . loopEnd = this . _loopStart + this . _loopLength ;
651- }
571+ this . _sourceNode . loopStart = this . _loopStart ;
572+ this . _sourceNode . loopEnd = this . _loopStart + this . _loopLength ;
652573 this . _sourceNode . playbackRate . setValueAtTime ( this . _pitch , context . currentTime ) ;
653574 this . _gainNode = context . createGain ( ) ;
654575 this . _gainNode . gain . setValueAtTime ( this . _volume , context . currentTime ) ;
@@ -734,14 +655,8 @@ WebAudio.prototype._onLoad = function() {
734655 * @private
735656 */
736657WebAudio . prototype . _readLoopComments = function ( array ) {
737- if ( this . _sampleRate === 0 ) {
738- this . _readOgg ( array ) ;
739- this . _readMp4 ( array ) ;
740- if ( this . _loopLength > 0 && this . _sampleRate > 0 ) {
741- this . _loopStart /= this . _sampleRate ;
742- this . _loopLength /= this . _sampleRate ;
743- }
744- }
658+ this . _readOgg ( array ) ;
659+ this . _readMp4 ( array ) ;
745660} ;
746661
747662/**
0 commit comments