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