Skip to content

Commit

Permalink
Merge pull request #197 from rpgtkoolmv/revert-143-loadfast_sound
Browse files Browse the repository at this point in the history
Revert "Load sounds faster and faster"
  • Loading branch information
krmbn0576 authored Dec 17, 2018
2 parents be65f62 + cc90ff4 commit 3b426b6
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 251 deletions.
117 changes: 16 additions & 101 deletions js/rpg_core/WebAudio.js
Original file line number Diff line number Diff line change
Expand Up @@ -265,17 +265,11 @@ WebAudio.prototype.clear = function() {
this._sourceNode = null;
this._gainNode = null;
this._pannerNode = null;
this._partialArray = null;
this._wholeArray = null;
this._chunkGainRate = 5;
this._chunkSize = 75 * 1024;
this._loadedSize = 0;
this._totalTime = 0;
this._sampleRate = 0;
this._loopStart = 0;
this._loopLength = 0;
this._startTime = 0;
this._offset = 0;
this._volume = 1;
this._pitch = 1;
this._pan = 0;
Expand Down Expand Up @@ -363,7 +357,7 @@ Object.defineProperty(WebAudio.prototype, 'pan', {
* @return {Boolean} True if the audio data is ready to play
*/
WebAudio.prototype.isReady = function() {
return !!this._buffer && this._buffer.duration >= this._offset;
return !!this._buffer;
};

/**
Expand Down Expand Up @@ -394,8 +388,8 @@ WebAudio.prototype.isPlaying = function() {
* @param {Number} offset The start position to play in seconds
*/
WebAudio.prototype.play = function(loop, offset) {
this._offset = offset = offset || 0;
if (this.isReady()) {
offset = offset || 0;
this._startPlaying(loop, offset);
} else if (WebAudio._context) {
this._autoPlay = true;
Expand Down Expand Up @@ -510,9 +504,6 @@ WebAudio.prototype._load = function(url) {
var xhr = new XMLHttpRequest();
if(Decrypter.hasEncryptedAudio) url = Decrypter.extToEncryptExt(url);
xhr.open('GET', url);
if (typeof require === 'undefined') {
xhr.setRequestHeader('Range', 'bytes=' + this._loadedSize + '-' + (this._loadedSize + this._chunkSize - 1));
}
xhr.responseType = 'arraybuffer';
xhr.onload = function() {
if (xhr.status < 400) {
Expand All @@ -530,89 +521,21 @@ WebAudio.prototype._load = function(url) {
* @private
*/
WebAudio.prototype._onXhrLoad = function(xhr) {
if (xhr.status === 206) {
this._onPartialLoad(xhr);
} else {
this._onWholeLoad(xhr.response);
}
};

/**
* @method _onPartialLoad
* @param {XMLHttpRequest} xhr
* @private
*/
WebAudio.prototype._onPartialLoad = function(xhr) {
var array = xhr.response;
if (!this._partialArray) {
this._partialArray = new Uint8Array(+xhr.getResponseHeader('Content-Range').split('/').pop());
this._chunkSize *= this._chunkGainRate - 1;
} else {
this._chunkSize = this._partialArray.byteLength;
}
this._partialArray.set(new Uint8Array(array), this._loadedSize);
this._loadedSize += array.byteLength;
if (this._loadedSize < this._partialArray.byteLength) {
array = this._partialArray.buffer.slice(0, this._loadedSize);
this._load(this._url);
} else {
array = this._partialArray.buffer;
this._partialArray = null;
}
if (Decrypter.hasEncryptedAudio) {
array = Decrypter.decryptArrayBuffer(array);
}
if(Decrypter.hasEncryptedAudio) array = Decrypter.decryptArrayBuffer(array);
this._readLoopComments(new Uint8Array(array));
WebAudio._context.decodeAudioData(array, this._onDecode.bind(this));
};

/**
* @method _onWholeLoad
* @param {ArrayBuffer} array
* @private
*/
WebAudio.prototype._onWholeLoad = function(array) {
if (array) {
if (Decrypter.hasEncryptedAudio) {
array = Decrypter.decryptArrayBuffer(array);
}
this._readLoopComments(new Uint8Array(array));
if (this._chunkSize < array.byteLength) {
this._wholeArray = array;
array = this._wholeArray.slice(0, this._chunkSize);
this._chunkSize *= this._chunkGainRate;
}
} else if (this._chunkSize < this._wholeArray.byteLength) {
array = this._wholeArray.slice(0, this._chunkSize);
this._chunkSize = this._wholeArray.byteLength;
} else {
array = this._wholeArray;
this._wholeArray = null;
}
WebAudio._context.decodeAudioData(array, this._onDecode.bind(this), function() {
this._onWholeLoad();
}.bind(this));
};

/**
* @method _onDecode
* @param {AudioBuffer} buffer
* @private
*/
WebAudio.prototype._onDecode = function(buffer) {
if (!this._buffer || this._buffer.length < buffer.length) {
WebAudio._context.decodeAudioData(array, function(buffer) {
this._buffer = buffer;
this._totalTime = buffer.duration;
if (this.isPlaying()) {
this._startPlaying(this._sourceNode.loop, this.seek());
}
if (this.isReady()) {
this._onLoad();
}
if (this._wholeArray) {
this._onWholeLoad();
if (this._loopLength > 0 && this._sampleRate > 0) {
this._loopStart /= this._sampleRate;
this._loopLength /= this._sampleRate;
} else {
this._loopStart = 0;
this._loopLength = this._totalTime;
}
}
this._onLoad();
}.bind(this));
};

/**
Expand Down Expand Up @@ -645,10 +568,8 @@ WebAudio.prototype._createNodes = function() {
var context = WebAudio._context;
this._sourceNode = context.createBufferSource();
this._sourceNode.buffer = this._buffer;
if (this._buffer.duration > this._loopStart) {
this._sourceNode.loopStart = this._loopStart;
this._sourceNode.loopEnd = this._loopStart + this._loopLength;
}
this._sourceNode.loopStart = this._loopStart;
this._sourceNode.loopEnd = this._loopStart + this._loopLength;
this._sourceNode.playbackRate.setValueAtTime(this._pitch, context.currentTime);
this._gainNode = context.createGain();
this._gainNode.gain.setValueAtTime(this._volume, context.currentTime);
Expand Down Expand Up @@ -734,14 +655,8 @@ WebAudio.prototype._onLoad = function() {
* @private
*/
WebAudio.prototype._readLoopComments = function(array) {
if (this._sampleRate === 0) {
this._readOgg(array);
this._readMp4(array);
if (this._loopLength > 0 && this._sampleRate > 0) {
this._loopStart /= this._sampleRate;
this._loopLength /= this._sampleRate;
}
}
this._readOgg(array);
this._readMp4(array);
};

/**
Expand Down
2 changes: 1 addition & 1 deletion js/rpg_managers/AudioManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ AudioManager.updateBufferParameters = function(buffer, configVolume, audio) {
};

AudioManager.audioFileExt = function() {
if (WebAudio.canPlayOgg()) {
if (WebAudio.canPlayOgg() && !Utils.isMobileDevice()) {
return '.ogg';
} else {
return '.m4a';
Expand Down
149 changes: 0 additions & 149 deletions plugins/Debug_ReportSound.js

This file was deleted.

0 comments on commit 3b426b6

Please sign in to comment.