diff --git a/user_scripts/IodineGBAAudioGlueCode.js b/user_scripts/IodineGBAAudioGlueCode.js index 1b3a23d8..2e2e61c7 100644 --- a/user_scripts/IodineGBAAudioGlueCode.js +++ b/user_scripts/IodineGBAAudioGlueCode.js @@ -1,25 +1,22 @@ "use strict"; /* - Copyright (C) 2012-2015 Grant Galitz - + Copyright (C) 2012-2014 Grant Galitz + Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: - + The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. - + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -function GlueCodeMixer(playButton) { +function GlueCodeMixer() { var parentObj = this; - this.audio = new XAudioServer(2, this.sampleRate, 0, this.bufferAmount, null, function () { - parentObj.checkHeartbeats(); - }, function () { - parentObj.checkPostHeartbeats(); - }, 1, function () { - //Disable audio in the callback here: - parentObj.disableAudio(); - }, playButton); + this.audio = new XAudioServer(2, this.sampleRate, 0, this.bufferAmount, null, 1, function () { + //Disable audio in the callback here: + parentObj.disableAudio(); + }); this.outputUnits = []; this.outputUnitsValid = []; + setInterval(function(){parentObj.checkAudio();}, 16); this.initializeBuffer(); } GlueCodeMixer.prototype.sampleRate = 44100; @@ -53,18 +50,6 @@ GlueCodeMixer.prototype.unregister = function (stackPosition) { } } } -GlueCodeMixer.prototype.checkHeartbeats = function () { - var inputCount = this.outputUnitsValid.length; - for (var inputIndex = 0, output = 0; inputIndex < inputCount; ++inputIndex) { - this.outputUnitsValid[inputIndex].heartBeatCallback(); - } -} -GlueCodeMixer.prototype.checkPostHeartbeats = function () { - var inputCount = this.outputUnitsValid.length; - for (var inputIndex = 0, output = 0; inputIndex < inputCount; ++inputIndex) { - this.outputUnitsValid[inputIndex].postHeartBeatCallback(); - } -} GlueCodeMixer.prototype.checkAudio = function () { if (this.audio) { var inputCount = this.outputUnitsValid.length; @@ -77,15 +62,13 @@ GlueCodeMixer.prototype.checkAudio = function () { } this.buffer.push(output); } - var bufferLength = this.buffer.count(); - this.audio.writeAudioNoCallback(this.buffer.buffer, bufferLength); - this.buffer.reset(); + this.audio.writeAudioNoCallback(this.buffer.getSlice()); } } GlueCodeMixer.prototype.findLowestBufferCount = function () { var count = 0; for (var inputIndex = 0, inputCount = this.outputUnitsValid.length; inputIndex < inputCount; ++inputIndex) { - var tempCount = this.outputUnitsValid[inputIndex].buffer.resampledSamplesLeft(); + var tempCount = this.outputUnitsValid[inputIndex].buffer.remainingBuffer(); if (tempCount > 0) { if (count > 0) { count = Math.min(count, tempCount); @@ -95,38 +78,32 @@ GlueCodeMixer.prototype.findLowestBufferCount = function () { } } } - return Math.min(count, this.channelCount * this.bufferAmount); + return count; } GlueCodeMixer.prototype.disableAudio = function () { this.audio = null; } function GlueCodeMixerInput(mixer) { this.mixer = mixer; - this.volume = 1; } -GlueCodeMixerInput.prototype.initialize = function (channelCount, sampleRate, bufferAmount, heartBeatCallback, postHeartBeatCallback, errorCallback) { +GlueCodeMixerInput.prototype.initialize = function (channelCount, sampleRate, bufferAmount, startingVolume, errorCallback) { this.channelCount = channelCount; this.sampleRate = sampleRate; this.bufferAmount = bufferAmount; - this.heartBeatCallback = heartBeatCallback; - this.postHeartBeatCallback = postHeartBeatCallback; + this.volume = startingVolume; this.errorCallback = errorCallback; - var oldBuffer = this.buffer; this.buffer = new AudioBufferWrapper(this.channelCount, this.mixer.channelCount, this.bufferAmount, this.sampleRate, this.mixer.sampleRate); - if (oldBuffer) { - //If re-using same mixer input node, copy old buffer contents into the new buffer: - this.buffer.copyOld(oldBuffer); - } + } -GlueCodeMixerInput.prototype.register = function () { +GlueCodeMixerInput.prototype.register = function (volume) { this.mixer.appendInput(this); } -GlueCodeMixerInput.prototype.setVolume = function (volume) { - this.volume = Math.min(Math.max(volume, 0), 1); +GlueCodeMixerInput.prototype.changeVolume = function (volume) { + this.volume = volume; } GlueCodeMixerInput.prototype.prepareShift = function () { this.buffer.resampleRefill(); @@ -134,14 +111,8 @@ GlueCodeMixerInput.prototype.prepareShift = function () { GlueCodeMixerInput.prototype.shift = function () { return this.buffer.shift() * this.volume; } -GlueCodeMixerInput.prototype.push = function (buffer, start, end) { - this.buffer.push(buffer, start, end); - this.mixer.checkAudio(); -} -GlueCodeMixerInput.prototype.pushDeferred = function (buffer, start, end) { - this.buffer.push(buffer, start, end); -} -GlueCodeMixerInput.prototype.flush = function () { +GlueCodeMixerInput.prototype.push = function (buffer) { + this.buffer.push(buffer); this.mixer.checkAudio(); } GlueCodeMixerInput.prototype.remainingBuffer = function () { @@ -153,9 +124,6 @@ GlueCodeMixerInput.prototype.registerStackPosition = function (stackPosition) { GlueCodeMixerInput.prototype.unregister = function () { this.mixer.unregister(this.stackPosition); } -GlueCodeMixerInput.prototype.setBufferSpace = function (bufferAmount) { - this.buffer.setBufferSpace(bufferAmount); -} function AudioBufferWrapper(channelCount, mixerChannelCount, bufferAmount, @@ -171,54 +139,36 @@ function AudioBufferWrapper(channelCount, AudioBufferWrapper.prototype.initialize = function () { this.inBufferSize = this.bufferAmount * this.mixerChannelCount; this.inBuffer = getFloat32Array(this.inBufferSize); - this.resampler = new Resampler(this.sampleRate, this.mixerSampleRate, this.mixerChannelCount, this.inBuffer); - this.outBufferSize = this.resampler.outputBuffer.length; + this.outBufferSize = (Math.ceil(this.inBufferSize * this.mixerSampleRate / this.sampleRate / this.mixerChannelCount) * this.mixerChannelCount) + this.mixerChannelCount; this.outBuffer = getFloat32Array(this.outBufferSize); + this.resampler = new Resampler(this.sampleRate, this.mixerSampleRate, this.mixerChannelCount, this.outBufferSize, true); this.inputOffset = 0; this.resampleBufferStart = 0; this.resampleBufferEnd = 0; } -AudioBufferWrapper.prototype.copyOld = function (oldBuffer) { - this.resampleRefill(); - while (oldBuffer.resampleBufferStart != oldBuffer.resampleBufferEnd) { - this.outBuffer[this.resampleBufferEnd++] = oldBuffer.outBuffer[oldBuffer.resampleBufferStart++]; - if (this.resampleBufferEnd == this.outBufferSize) { - this.resampleBufferEnd = 0; - } - if (this.resampleBufferStart == this.resampleBufferEnd) { - this.resampleBufferStart += this.mixerChannelCount; - if (this.resampleBufferStart == this.outBufferSize) { - this.resampleBufferStart = 0; - } - } - if (oldBuffer.resampleBufferStart == oldBuffer.outBufferSize) { - oldBuffer.resampleBufferStart = 0; - } - } -} -AudioBufferWrapper.prototype.push = function (buffer, start, end) { - var length = Math.min(buffer.length, end); +AudioBufferWrapper.prototype.push = function (buffer) { + var length = buffer.length; if (this.channelCount < this.mixerChannelCount) { - for (; start < length && this.inputOffset < this.inBufferSize;) { + for (var bufferCounter = 0; bufferCounter < length && this.inputOffset < this.inBufferSize;) { for (var index = this.channelCount; index < this.mixerChannelCount; ++index) { - this.inBuffer[this.inputOffset++] = buffer[start]; + this.inBuffer[this.inputOffset++] = buffer[bufferCounter]; } - for (index = 0; index < this.channelCount && start < length; ++index) { - this.inBuffer[this.inputOffset++] = buffer[start++]; + for (index = 0; index < this.channelCount && bufferCounter < length; ++index) { + this.inBuffer[this.inputOffset++] = buffer[bufferCounter++]; } } } else if (this.channelCount == this.mixerChannelCount) { - for (; start < length && this.inputOffset < this.inBufferSize;) { - this.inBuffer[this.inputOffset++] = buffer[start++]; + for (var bufferCounter = 0; bufferCounter < length && this.inputOffset < this.inBufferSize;) { + this.inBuffer[this.inputOffset++] = buffer[bufferCounter++]; } } else { - for (; start < length && this.inputOffset < this.inBufferSize;) { - for (index = 0; index < this.mixerChannelCount && start < length; ++index) { - this.inBuffer[this.inputOffset++] = buffer[start++]; + for (var bufferCounter = 0; bufferCounter < length && this.inputOffset < this.inBufferSize;) { + for (index = 0; index < this.mixerChannelCount && bufferCounter < length; ++index) { + this.inBuffer[this.inputOffset++] = buffer[bufferCounter++]; } - start += this.channelCount - this.mixerChannelCount; + bufferCounter += this.channelCount - this.mixerChannelCount; } } } @@ -235,7 +185,7 @@ AudioBufferWrapper.prototype.shift = function () { AudioBufferWrapper.prototype.resampleRefill = function () { if (this.inputOffset > 0) { //Resample a chunk of audio: - var resampleLength = this.resampler.resampler(this.inputOffset); + var resampleLength = this.resampler.resampler(this.getSlice(this.inBuffer, this.inputOffset)); var resampledResult = this.resampler.outputBuffer; for (var index2 = 0; index2 < resampleLength;) { this.outBuffer[this.resampleBufferEnd++] = resampledResult[index2++]; @@ -252,17 +202,29 @@ AudioBufferWrapper.prototype.resampleRefill = function () { this.inputOffset = 0; } } -AudioBufferWrapper.prototype.setBufferSpace = function (bufferAmount) { - while (this.inputOffset < bufferAmount && this.inputOffset < this.inBufferSize) { - this.inBuffer[this.inputOffset++] = 0; - } -} AudioBufferWrapper.prototype.remainingBuffer = function () { return (Math.floor((this.resampledSamplesLeft() * this.resampler.ratioWeight) / this.mixerChannelCount) * this.mixerChannelCount) + this.inputOffset; } AudioBufferWrapper.prototype.resampledSamplesLeft = function () { return ((this.resampleBufferStart <= this.resampleBufferEnd) ? 0 : this.outBufferSize) + this.resampleBufferEnd - this.resampleBufferStart; } +AudioBufferWrapper.prototype.getSlice = function (buffer, lengthOf) { + //Typed array and normal array buffer section referencing: + try { + return buffer.subarray(0, lengthOf); + } + catch (error) { + try { + //Regular array pass: + buffer.length = lengthOf; + return buffer; + } + catch (error) { + //Nightly Firefox 4 used to have the subarray function named as slice: + return buffer.slice(0, lengthOf); + } + } +} function AudioSimpleBuffer(channelCount, bufferAmount) { this.channelCount = channelCount; this.bufferAmount = bufferAmount; @@ -275,9 +237,22 @@ AudioSimpleBuffer.prototype.push = function (data) { this.buffer[this.stackLength++] = data; } } -AudioSimpleBuffer.prototype.count = function () { - return this.stackLength; -} -AudioSimpleBuffer.prototype.reset = function () { +AudioSimpleBuffer.prototype.getSlice = function () { + var lengthOf = this.stackLength; this.stackLength = 0; + //Typed array and normal array buffer section referencing: + try { + return this.buffer.subarray(0, lengthOf); + } + catch (error) { + try { + //Regular array pass: + this.buffer.length = lengthOf; + return this.buffer; + } + catch (error) { + //Nightly Firefox 4 used to have the subarray function named as slice: + return this.buffer.slice(0, lengthOf); + } + } }