Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
RishiRecon
GitHub Repository: RishiRecon/exploits
Path: blob/main/misc/emulator/gba/user_scripts/XAudioJS/XAudioServerMediaStreamWorker.js
28798 views
1
//This file is part of the XAudioJS library.
2
var XAudioJSResampledBuffer = [];
3
var XAudioJSOutputBuffer = [];
4
var XAudioJSResampleBufferStart = 0;
5
var XAudioJSResampleBufferEnd = 0;
6
var XAudioJSResampleBufferSize = 0;
7
var XAudioJSChannelsAllocated = 1;
8
//Message Receiver:
9
self.onmessage = function (event) {
10
var data = event.data;
11
switch (data[0]) {
12
case 0:
13
//Add new audio samples to our ring buffer:
14
var resampledResult = data[1];
15
var length = resampledResult.length;
16
for (var i = 0; i < length; ++i) {
17
XAudioJSResampledBuffer[XAudioJSResampleBufferEnd++] = resampledResult[i];
18
if (XAudioJSResampleBufferEnd == XAudioJSResampleBufferSize) {
19
XAudioJSResampleBufferEnd = 0;
20
}
21
if (XAudioJSResampleBufferStart == XAudioJSResampleBufferEnd) {
22
XAudioJSResampleBufferStart += XAudioJSChannelsAllocated;
23
if (XAudioJSResampleBufferStart == XAudioJSResampleBufferSize) {
24
XAudioJSResampleBufferStart = 0;
25
}
26
}
27
}
28
break;
29
case 1:
30
//Initialize:
31
XAudioJSResampleBufferSize = data[1];
32
XAudioJSChannelsAllocated = data[2];
33
XAudioJSResampledBuffer = new Float32Array(XAudioJSResampleBufferSize);
34
}
35
}
36
//MediaStream Polyfill Event:
37
self.onprocessmedia = function (event) {
38
//Get some buffer length computations:
39
var apiBufferLength = event.audioLength;
40
var apiBufferLengthAll = apiBufferLength * event.audioChannels;
41
if (apiBufferLengthAll > XAudioJSOutputBuffer.length) {
42
XAudioJSOutputBuffer = new Float32Array(apiBufferLengthAll);
43
}
44
//De-interleave the buffered audio while looping through our ring buffer:
45
var sampleFramesCount = Math.min(apiBufferLength, XAudioJSResampledSamplesLeft() / XAudioJSChannelsAllocated);
46
for (var sampleFramePosition = 0, channelOffset = 0; sampleFramePosition < sampleFramesCount; ++sampleFramePosition) {
47
for (channelOffset = sampleFramePosition; channelOffset < apiBufferLengthAll; channelOffset += apiBufferLength) {
48
XAudioJSOutputBuffer[channelOffset] = XAudioJSResampledBuffer[XAudioJSResampleBufferStart++];
49
if (XAudioJSResampleBufferStart == XAudioJSResampleBufferSize) {
50
XAudioJSResampleBufferStart = 0;
51
}
52
}
53
}
54
//Add some zero fill if we underran the required buffer fill amount:
55
while (sampleFramePosition < apiBufferLength) {
56
for (channelOffset = sampleFramePosition++; channelOffset < apiBufferLengthAll; channelOffset += apiBufferLength) {
57
XAudioJSOutputBuffer[channelOffset] = 0;
58
}
59
}
60
//Write some buffered audio:
61
event.writeAudio(XAudioJSOutputBuffer.subarray(0, apiBufferLengthAll));
62
//Request a buffer from the main thread:
63
self.postMessage(event.audioLength);
64
}
65
//Accessory function used to determine remaining samples in the ring buffer:
66
function XAudioJSResampledSamplesLeft() {
67
return ((XAudioJSResampleBufferStart <= XAudioJSResampleBufferEnd) ? 0 : XAudioJSResampleBufferSize) + XAudioJSResampleBufferEnd - XAudioJSResampleBufferStart;
68
}
69