Path: blob/main/misc/emulator/gba/user_scripts/IodineGBAAudioGlueCode.js
28515 views
"use strict";1/*2Copyright (C) 2012-2014 Grant Galitz34Permission 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:56The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.78THE 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.9*/10function GlueCodeMixer() {11var parentObj = this;12this.audio = new XAudioServer(2, this.sampleRate, 0, this.bufferAmount, null, 1, function () {13//Disable audio in the callback here:14parentObj.disableAudio();15});16this.outputUnits = [];17this.outputUnitsValid = [];18setInterval(function(){parentObj.checkAudio();}, 16);19this.initializeBuffer();20}21GlueCodeMixer.prototype.sampleRate = 44100;22GlueCodeMixer.prototype.bufferAmount = 44100;23GlueCodeMixer.prototype.channelCount = 2;24GlueCodeMixer.prototype.initializeBuffer = function () {25this.buffer = new AudioSimpleBuffer(this.channelCount,26this.bufferAmount);27}28GlueCodeMixer.prototype.appendInput = function (inUnit) {29if (this.audio) {30for (var index = 0; index < this.outputUnits.length; index++) {31if (!this.outputUnits[index]) {32break;33}34}35this.outputUnits[index] = inUnit;36this.outputUnitsValid.push(inUnit);37inUnit.registerStackPosition(index);38}39else if (typeof inUnit.errorCallback == "function") {40inUnit.errorCallback();41}42}43GlueCodeMixer.prototype.unregister = function (stackPosition) {44this.outputUnits[stackPosition] = null;45this.outputUnitsValid = [];46for (var index = 0, length = this.outputUnits.length; index < length; ++index) {47if (this.outputUnits[index]) {48this.outputUnitsValid.push(this.outputUnits);49}50}51}52GlueCodeMixer.prototype.checkAudio = function () {53if (this.audio) {54var inputCount = this.outputUnitsValid.length;55for (var inputIndex = 0, output = 0; inputIndex < inputCount; ++inputIndex) {56this.outputUnitsValid[inputIndex].prepareShift();57}58for (var count = 0, requested = this.findLowestBufferCount(); count < requested; ++count) {59for (var inputIndex = 0, output = 0; inputIndex < inputCount; ++inputIndex) {60output += this.outputUnitsValid[inputIndex].shift();61}62this.buffer.push(output);63}64this.audio.writeAudioNoCallback(this.buffer.getSlice());65}66}67GlueCodeMixer.prototype.findLowestBufferCount = function () {68var count = 0;69for (var inputIndex = 0, inputCount = this.outputUnitsValid.length; inputIndex < inputCount; ++inputIndex) {70var tempCount = this.outputUnitsValid[inputIndex].buffer.remainingBuffer();71if (tempCount > 0) {72if (count > 0) {73count = Math.min(count, tempCount);74}75else {76count = tempCount;77}78}79}80return count;81}82GlueCodeMixer.prototype.disableAudio = function () {83this.audio = null;84}85function GlueCodeMixerInput(mixer) {86this.mixer = mixer;87}88GlueCodeMixerInput.prototype.initialize = function (channelCount, sampleRate, bufferAmount, startingVolume, errorCallback) {89this.channelCount = channelCount;90this.sampleRate = sampleRate;91this.bufferAmount = bufferAmount;92this.volume = startingVolume;93this.errorCallback = errorCallback;94this.buffer = new AudioBufferWrapper(this.channelCount,95this.mixer.channelCount,96this.bufferAmount,97this.sampleRate,98this.mixer.sampleRate);99100}101GlueCodeMixerInput.prototype.register = function (volume) {102this.mixer.appendInput(this);103}104GlueCodeMixerInput.prototype.changeVolume = function (volume) {105this.volume = volume;106}107GlueCodeMixerInput.prototype.prepareShift = function () {108this.buffer.resampleRefill();109}110GlueCodeMixerInput.prototype.shift = function () {111return this.buffer.shift() * this.volume;112}113GlueCodeMixerInput.prototype.push = function (buffer) {114this.buffer.push(buffer);115this.mixer.checkAudio();116}117GlueCodeMixerInput.prototype.remainingBuffer = function () {118return this.buffer.remainingBuffer() + (Math.floor((this.mixer.audio.remainingBuffer() * this.sampleRate / this.mixer.sampleRate) / this.mixer.channelCount) * this.mixer.channelCount);119}120GlueCodeMixerInput.prototype.registerStackPosition = function (stackPosition) {121this.stackPosition = stackPosition;122}123GlueCodeMixerInput.prototype.unregister = function () {124this.mixer.unregister(this.stackPosition);125}126function AudioBufferWrapper(channelCount,127mixerChannelCount,128bufferAmount,129sampleRate,130mixerSampleRate) {131this.channelCount = channelCount;132this.mixerChannelCount = mixerChannelCount;133this.bufferAmount = bufferAmount;134this.sampleRate = sampleRate;135this.mixerSampleRate = mixerSampleRate;136this.initialize();137}138AudioBufferWrapper.prototype.initialize = function () {139this.inBufferSize = this.bufferAmount * this.mixerChannelCount;140this.inBuffer = getFloat32Array(this.inBufferSize);141this.outBufferSize = (Math.ceil(this.inBufferSize * this.mixerSampleRate / this.sampleRate / this.mixerChannelCount) * this.mixerChannelCount) + this.mixerChannelCount;142this.outBuffer = getFloat32Array(this.outBufferSize);143this.resampler = new Resampler(this.sampleRate, this.mixerSampleRate, this.mixerChannelCount, this.outBufferSize, true);144this.inputOffset = 0;145this.resampleBufferStart = 0;146this.resampleBufferEnd = 0;147}148AudioBufferWrapper.prototype.push = function (buffer) {149var length = buffer.length;150if (this.channelCount < this.mixerChannelCount) {151for (var bufferCounter = 0; bufferCounter < length && this.inputOffset < this.inBufferSize;) {152for (var index = this.channelCount; index < this.mixerChannelCount; ++index) {153this.inBuffer[this.inputOffset++] = buffer[bufferCounter];154}155for (index = 0; index < this.channelCount && bufferCounter < length; ++index) {156this.inBuffer[this.inputOffset++] = buffer[bufferCounter++];157}158}159}160else if (this.channelCount == this.mixerChannelCount) {161for (var bufferCounter = 0; bufferCounter < length && this.inputOffset < this.inBufferSize;) {162this.inBuffer[this.inputOffset++] = buffer[bufferCounter++];163}164}165else {166for (var bufferCounter = 0; bufferCounter < length && this.inputOffset < this.inBufferSize;) {167for (index = 0; index < this.mixerChannelCount && bufferCounter < length; ++index) {168this.inBuffer[this.inputOffset++] = buffer[bufferCounter++];169}170bufferCounter += this.channelCount - this.mixerChannelCount;171}172}173}174AudioBufferWrapper.prototype.shift = function () {175var output = 0;176if (this.resampleBufferStart != this.resampleBufferEnd) {177output = this.outBuffer[this.resampleBufferStart++];178if (this.resampleBufferStart == this.outBufferSize) {179this.resampleBufferStart = 0;180}181}182return output;183}184AudioBufferWrapper.prototype.resampleRefill = function () {185if (this.inputOffset > 0) {186//Resample a chunk of audio:187var resampleLength = this.resampler.resampler(this.getSlice(this.inBuffer, this.inputOffset));188var resampledResult = this.resampler.outputBuffer;189for (var index2 = 0; index2 < resampleLength;) {190this.outBuffer[this.resampleBufferEnd++] = resampledResult[index2++];191if (this.resampleBufferEnd == this.outBufferSize) {192this.resampleBufferEnd = 0;193}194if (this.resampleBufferStart == this.resampleBufferEnd) {195this.resampleBufferStart += this.mixerChannelCount;196if (this.resampleBufferStart == this.outBufferSize) {197this.resampleBufferStart = 0;198}199}200}201this.inputOffset = 0;202}203}204AudioBufferWrapper.prototype.remainingBuffer = function () {205return (Math.floor((this.resampledSamplesLeft() * this.resampler.ratioWeight) / this.mixerChannelCount) * this.mixerChannelCount) + this.inputOffset;206}207AudioBufferWrapper.prototype.resampledSamplesLeft = function () {208return ((this.resampleBufferStart <= this.resampleBufferEnd) ? 0 : this.outBufferSize) + this.resampleBufferEnd - this.resampleBufferStart;209}210AudioBufferWrapper.prototype.getSlice = function (buffer, lengthOf) {211//Typed array and normal array buffer section referencing:212try {213return buffer.subarray(0, lengthOf);214}215catch (error) {216try {217//Regular array pass:218buffer.length = lengthOf;219return buffer;220}221catch (error) {222//Nightly Firefox 4 used to have the subarray function named as slice:223return buffer.slice(0, lengthOf);224}225}226}227function AudioSimpleBuffer(channelCount, bufferAmount) {228this.channelCount = channelCount;229this.bufferAmount = bufferAmount;230this.outBufferSize = this.channelCount * this.bufferAmount;231this.stackLength = 0;232this.buffer = getFloat32Array(this.outBufferSize);233}234AudioSimpleBuffer.prototype.push = function (data) {235if (this.stackLength < this.outBufferSize) {236this.buffer[this.stackLength++] = data;237}238}239AudioSimpleBuffer.prototype.getSlice = function () {240var lengthOf = this.stackLength;241this.stackLength = 0;242//Typed array and normal array buffer section referencing:243try {244return this.buffer.subarray(0, lengthOf);245}246catch (error) {247try {248//Regular array pass:249this.buffer.length = lengthOf;250return this.buffer;251}252catch (error) {253//Nightly Firefox 4 used to have the subarray function named as slice:254return this.buffer.slice(0, lengthOf);255}256}257}258259260