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