Skip to content

Commit 8e1115a

Browse files
committed
Created a single source for querying the quantum size
A single JS function exists for the quantum size, the value is also passed to the processor node on creation.
1 parent 274cc56 commit 8e1115a

File tree

2 files changed

+26
-13
lines changed

2 files changed

+26
-13
lines changed

src/audio_worklet.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,15 @@ function createWasmAudioWorkletProcessor(audioParams) {
3131
let opts = args.processorOptions;
3232
this.callbackFunction = Module['wasmTable'].get(opts['cb']);
3333
this.userData = opts['ud'];
34+
// Plus the number of samples to process, fixed for the lifetime of the context that created this processor
35+
this.quantumSize = opts['qs'];
3436
}
3537

3638
static get parameterDescriptors() {
3739
return audioParams;
3840
}
3941

4042
process(inputList, outputList, parameters) {
41-
// hardcoding this until the Web Audio 1.1 API spec makes it into browsers and we can get the AudioWorkletProcessor's context quantum
42-
const quantumSize = 128;
43-
4443
// Marshal all inputs and parameters to the Wasm memory on the thread stack,
4544
// then perform the wasm audio worklet call,
4645
// and finally marshal audio output data back.
@@ -54,7 +53,7 @@ function createWasmAudioWorkletProcessor(audioParams) {
5453
didProduceAudio, paramArray;
5554

5655
// Calculate how much stack space is needed.
57-
const quantumBytes = quantumSize * 4;
56+
const quantumBytes = this.quantumSize * 4;
5857
for (i of inputList) stackMemoryNeeded += i.length * quantumBytes;
5958
for (i of outputList) stackMemoryNeeded += i.length * quantumBytes;
6059
for (i in parameters) stackMemoryNeeded += parameters[i].byteLength + {{{ C_STRUCTS.AudioParamFrame.__size__ }}}, ++numParams;
@@ -68,7 +67,7 @@ function createWasmAudioWorkletProcessor(audioParams) {
6867
for (i of inputList) {
6968
// Write the AudioSampleFrame struct instance
7069
HEAPU32[k + {{{ C_STRUCTS.AudioSampleFrame.numberOfChannels / 4 }}}] = i.length;
71-
HEAPU32[k + {{{ C_STRUCTS.AudioSampleFrame.quantumSize / 4 }}}] = quantumSize;
70+
HEAPU32[k + {{{ C_STRUCTS.AudioSampleFrame.quantumSize / 4 }}}] = this.quantumSize;
7271
HEAPU32[k + {{{ C_STRUCTS.AudioSampleFrame.data / 4 }}}] = dataPtr;
7372
k += {{{ C_STRUCTS.AudioSampleFrame.__size__ / 4 }}};
7473
// Marshal the input audio sample data for each audio channel of this input
@@ -85,7 +84,7 @@ function createWasmAudioWorkletProcessor(audioParams) {
8584
for (i of outputList) {
8685
// Write the AudioSampleFrame struct instance
8786
HEAPU32[k + {{{ C_STRUCTS.AudioSampleFrame.numberOfChannels / 4 }}}] = i.length;
88-
HEAPU32[k + {{{ C_STRUCTS.AudioSampleFrame.quantumSize / 4 }}}] = quantumSize;
87+
HEAPU32[k + {{{ C_STRUCTS.AudioSampleFrame.quantumSize / 4 }}}] = this.quantumSize;
8988
HEAPU32[k + {{{ C_STRUCTS.AudioSampleFrame.data / 4 }}}] = dataPtr;
9089
k += {{{ C_STRUCTS.AudioSampleFrame.__size__ / 4 }}};
9190
// Reserve space for the output data
@@ -114,7 +113,7 @@ function createWasmAudioWorkletProcessor(audioParams) {
114113
// not have one, so manually copy all bytes in)
115114
for (i of outputList) {
116115
for (j of i) {
117-
for (k = 0; k < quantumSize; ++k) {
116+
for (k = 0; k < this.quantumSize; ++k) {
118117
j[k] = HEAPF32[outputDataPtr++];
119118
}
120119
}

src/library_webaudio.js

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,21 @@ let LibraryWebAudio = {
3737
// Wasm handle ID.
3838
$emscriptenGetAudioObject: (objectHandle) => EmAudio[objectHandle],
3939

40-
// emscripten_create_audio_context() does not itself use
40+
// Performs the work of getting the AudioContext's quantum size.
41+
$emscriptenGetContextQuantumSize: (contextHandle) => {
42+
// TODO: in a future release this will be something like:
43+
// return EmAudio[contextHandle].renderQuantumSize || 128;
44+
// It comes two caveats: it needs the hint when generating the context adding to
45+
// emscripten_create_audio_context(), and altering the quantum requires a secure
46+
// context and fallback implementing. Until then we simply use the 1.0 API value:
47+
return 128;
48+
},
49+
50+
// emscripten_create_audio_context() does not itself use the
4151
// emscriptenGetAudioObject() function, but mark it as a dependency, because
4252
// the user will not be able to utilize the node unless they call
4353
// emscriptenGetAudioObject() on it on JS side to connect it to the graph, so
44-
// this avoids the user needing to manually do it on the command line.
54+
// this avoids the user needing to manually add the dependency on the command line.
4555
emscripten_create_audio_context__deps: ['$emscriptenRegisterAudioObject', '$emscriptenGetAudioObject'],
4656
emscripten_create_audio_context: (options) => {
4757
let ctx = window.AudioContext || window.webkitAudioContext;
@@ -264,6 +274,7 @@ let LibraryWebAudio = {
264274
});
265275
},
266276

277+
emscripten_create_wasm_audio_worklet_node__deps: ['$emscriptenGetContextQuantumSize'],
267278
emscripten_create_wasm_audio_worklet_node: (contextHandle, name, options, callback, userData) => {
268279
#if ASSERTIONS
269280
assert(contextHandle, `Called emscripten_create_wasm_audio_worklet_node() with a null Web Audio Context handle!`);
@@ -282,7 +293,11 @@ let LibraryWebAudio = {
282293
numberOfInputs: HEAP32[options],
283294
numberOfOutputs: HEAP32[options+1],
284295
outputChannelCount: HEAPU32[options+2] ? readChannelCountArray(HEAPU32[options+2]>>2, HEAP32[options+1]) : void 0,
285-
processorOptions: { 'cb': callback, 'ud': userData }
296+
processorOptions: {
297+
'cb': callback,
298+
'ud': userData,
299+
'qs': emscriptenGetContextQuantumSize(contextHandle)
300+
}
286301
} : void 0;
287302

288303
#if WEBAUDIO_DEBUG
@@ -293,14 +308,13 @@ let LibraryWebAudio = {
293308
},
294309
#endif // ~AUDIO_WORKLET
295310

311+
emscripten_audio_context_quantum_size__deps: ['$emscriptenGetContextQuantumSize'],
296312
emscripten_audio_context_quantum_size: (contextHandle) => {
297313
#if ASSERTIONS
298314
assert(EmAudio[contextHandle], `Called emscripten_audio_context_quantum_size() with an invalid Web Audio Context handle ${contextHandle}`);
299315
assert(EmAudio[contextHandle] instanceof (window.AudioContext || window.webkitAudioContext), `Called emscripten_audio_context_quantum_size() on handle ${contextHandle} that is not an AudioContext, but of type ${EmAudio[contextHandle]}`);
300316
#endif
301-
// TODO: once the Web Audio 1.1 API is implemented we can query the context for its renderQuantumSize
302-
// (With two caveats: it needs the hint when generating the context adding, plus it requires a secure context and fallback implementing)
303-
return 128;
317+
return emscriptenGetContextQuantumSize(contextHandle);
304318
},
305319

306320
emscripten_audio_node_connect: (source, destination, outputIndex, inputIndex) => {

0 commit comments

Comments
 (0)