-
Notifications
You must be signed in to change notification settings - Fork 204
/
Copy pathwasm-audio-helper.js
270 lines (239 loc) · 7.84 KB
/
wasm-audio-helper.js
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/**
* Copyright 2018 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
// Basic byte unit of WASM heap. (16 bit = 2 bytes)
const BYTES_PER_UNIT = Uint16Array.BYTES_PER_ELEMENT;
// Byte per audio sample. (32 bit float)
const BYTES_PER_SAMPLE = Float32Array.BYTES_PER_ELEMENT;
// The max audio channel on Chrome is 32.
const MAX_CHANNEL_COUNT = 32;
// WebAudio's render quantum size.
const RENDER_QUANTUM_FRAMES = 128;
/**
* A WASM HEAP wrapper for AudioBuffer class. This breaks down the AudioBuffer
* into an Array of Float32Array for the convinient WASM opearion.
*
* @class
* @dependency Module A WASM module generated by the emscripten glue code.
*/
class HeapAudioBuffer {
/**
* @constructor
* @param {object} wasmModule WASM module generated by Emscripten.
* @param {number} length Buffer frame length.
* @param {number} channelCount Number of channels.
* @param {number=} maxChannelCount Maximum number of channels.
*/
constructor(wasmModule, length, channelCount, maxChannelCount) {
// The |channelCount| must be greater than 0, and less than or equal to
// the maximum channel count.
this._isInitialized = false;
this._module = wasmModule;
this._length = length;
this._maxChannelCount = maxChannelCount
? Math.min(maxChannelCount, MAX_CHANNEL_COUNT)
: channelCount;
this._channelCount = channelCount;
this._allocateHeap();
this._isInitialized = true;
}
/**
* Allocates memory in the WASM heap and set up Float32Array views for the
* channel data.
*
* @private
*/
_allocateHeap() {
const channelByteSize = this._length * BYTES_PER_SAMPLE;
const dataByteSize = this._channelCount * channelByteSize;
this._dataPtr = this._module._malloc(dataByteSize);
this._channelData = [];
for (let i = 0; i < this._channelCount; ++i) {
let startByteOffset = this._dataPtr + i * channelByteSize;
let endByteOffset = startByteOffset + channelByteSize;
// Get the actual array index by dividing the byte offset by 2 bytes.
this._channelData[i] =
this._module.HEAPF32.subarray(startByteOffset >> BYTES_PER_UNIT,
endByteOffset >> BYTES_PER_UNIT);
}
}
/**
* Adapt the current channel count to the new input buffer.
*
* @param {number} newChannelCount The new channel count.
*/
adaptChannel(newChannelCount) {
if (newChannelCount < this._maxChannelCount) {
this._channelCount = newChannelCount;
}
}
/**
* Getter for the buffer length in frames.
*
* @return {?number} Buffer length in frames.
*/
get length() {
return this._isInitialized ? this._length : null;
}
/**
* Getter for the number of channels.
*
* @return {?number} Buffer length in frames.
*/
get numberOfChannels() {
return this._isInitialized ? this._channelCount : null;
}
/**
* Getter for the maxixmum number of channels allowed for the instance.
*
* @return {?number} Buffer length in frames.
*/
get maxChannelCount() {
return this._isInitialized ? this._maxChannelCount : null;
}
/**
* Returns a Float32Array object for a given channel index. If the channel
* index is undefined, it returns the reference to the entire array of channel
* data.
*
* @param {number|undefined} channelIndex Channel index.
* @return {?Array} a channel data array or an
* array of channel data.
*/
getChannelData(channelIndex) {
if (channelIndex >= this._channelCount) {
return null;
}
return typeof channelIndex === 'undefined'
? this._channelData : this._channelData[channelIndex];
}
/**
* Returns the base address of the allocated memory space in the WASM heap.
*
* @return {number} WASM Heap address.
*/
getHeapAddress() {
return this._dataPtr;
}
/**
* Frees the allocated memory space in the WASM heap.
*/
free() {
this._isInitialized = false;
this._module._free(this._dataPtr);
this._module._free(this._pointerArrayPtr);
this._channelData = null;
}
} // class HeapAudioBuffer
/**
* A JS FIFO implementation for the AudioWorklet. 3 assumptions for the
* simpler operation:
* 1. the push and the pull operation are done by 128 frames. (Web Audio
* API's render quantum size in the speficiation)
* 2. the channel count of input/output cannot be changed dynamically.
* The AudioWorkletNode should be configured with the `.channelCount = k`
* (where k is the channel count you want) and
* `.channelCountMode = explicit`.
* 3. This is for the single-thread operation. (obviously)
*
* @class
*/
class RingBuffer {
/**
* @constructor
* @param {number} length Buffer length in frames.
* @param {number} channelCount Buffer channel count.
*/
constructor(length, channelCount) {
this._readIndex = 0;
this._writeIndex = 0;
this._framesAvailable = 0;
this._channelCount = channelCount;
this._length = length;
this._channelData = [];
for (let i = 0; i < this._channelCount; ++i) {
this._channelData[i] = new Float32Array(length);
}
}
/**
* Getter for Available frames in buffer.
*
* @return {number} Available frames in buffer.
*/
get framesAvailable() {
return this._framesAvailable;
}
/**
* Push a sequence of Float32Arrays to buffer.
*
* @param {array} arraySequence A sequence of Float32Arrays.
*/
push(arraySequence) {
// The channel count of arraySequence and the length of each channel must
// match with this buffer obejct.
// Transfer data from the |arraySequence| storage to the internal buffer.
let sourceLength = arraySequence[0].length;
for (let i = 0; i < sourceLength; ++i) {
let writeIndex = (this._writeIndex + i) % this._length;
for (let channel = 0; channel < this._channelCount; ++channel) {
this._channelData[channel][writeIndex] = arraySequence[channel][i];
}
}
this._writeIndex += sourceLength;
if (this._writeIndex >= this._length) {
this._writeIndex = 0;
}
// For excessive frames, the buffer will be overwritten.
this._framesAvailable += sourceLength;
if (this._framesAvailable > this._length) {
this._framesAvailable = this._length;
}
}
/**
* Pull data out of buffer and fill a given sequence of Float32Arrays.
*
* @param {array} arraySequence An array of Float32Arrays.
*/
pull(arraySequence) {
// The channel count of arraySequence and the length of each channel must
// match with this buffer obejct.
// If the FIFO is completely empty, do nothing.
if (this._framesAvailable === 0) {
return;
}
let destinationLength = arraySequence[0].length;
// Transfer data from the internal buffer to the |arraySequence| storage.
for (let i = 0; i < destinationLength; ++i) {
let readIndex = (this._readIndex + i) % this._length;
for (let channel = 0; channel < this._channelCount; ++channel) {
arraySequence[channel][i] = this._channelData[channel][readIndex];
}
}
this._readIndex += destinationLength;
if (this._readIndex >= this._length) {
this._readIndex = 0;
}
this._framesAvailable -= destinationLength;
if (this._framesAvailable < 0) {
this._framesAvailable = 0;
}
}
} // class RingBuffer
export {
MAX_CHANNEL_COUNT,
RENDER_QUANTUM_FRAMES,
HeapAudioBuffer,
RingBuffer,
};