Skip to content

Commit dd2add0

Browse files
authored
Fix Closure breaking AUDIO_WORKLET (emscripten-core#22472)
If building with `-sAUDIO_WORKLET` and `--closure=1` the generated glue JS will minimise the property names passed to the worklet's message handler: https://github.com/emscripten-core/emscripten/blob/9445d6a1c3e3872d6e909565301a47c9c4fbd35b/src/audio_worklet.js#L179 E.g. these are received as: ``` A: 3, B: 1, F: 0, s: "noise-generator" ``` No sound will be generated as a result, since calls to `emscripten_create_wasm_audio_worklet_processor_async()` will never call the passed function.
1 parent bf910d6 commit dd2add0

File tree

3 files changed

+25
-17
lines changed

3 files changed

+25
-17
lines changed

src/audio_worklet.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -168,18 +168,22 @@ class BootstrapMessages extends AudioWorkletProcessor {
168168
}
169169
#endif
170170
// Register a real AudioWorkletProcessor that will actually do audio processing.
171-
registerProcessor(d['_wpn'], createWasmAudioWorkletProcessor(d['audioParams']));
171+
// 'ap' being the audio params
172+
registerProcessor(d['_wpn'], createWasmAudioWorkletProcessor(d['ap']));
172173
#if WEBAUDIO_DEBUG
173-
console.log(`Registered a new WasmAudioWorkletProcessor "${d['_wpn']}" with AudioParams: ${d['audioParams']}`);
174+
console.log(`Registered a new WasmAudioWorkletProcessor "${d['_wpn']}" with AudioParams: ${d['ap']}`);
174175
#endif
175176
// Post a Wasm Call message back telling that we have now registered the
176-
// AudioWorkletProcessor class, and should trigger the user onSuccess
177-
// callback of the
178-
// emscripten_create_wasm_audio_worklet_processor_async() call.
179-
p.postMessage({'_wsc': d['callback'], 'x': [d['contextHandle'], 1/*EM_TRUE*/, d['userData']] }); // "WaSm Call"
180-
} else if (d['_wsc']) {
177+
// AudioWorkletProcessor, and should trigger the user onSuccess callback
178+
// of the emscripten_create_wasm_audio_worklet_processor_async() call.
179+
//
181180
// '_wsc' is short for 'wasm call', using an identifier that will never
182181
// conflict with user messages
182+
// 'cb' the callback function
183+
// 'ch' the context handle
184+
// 'ud' the passed user data
185+
p.postMessage({'_wsc': d['cb'], 'x': [d['ch'], 1/*EM_TRUE*/, d['ud']] });
186+
} else if (d['_wsc']) {
183187
Module['wasmTable'].get(d['_wsc'])(...d['x']);
184188
};
185189
}

src/library_webaudio.js

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,12 @@ let LibraryWebAudio = {
122122
},
123123

124124
#if AUDIO_WORKLET
125+
// emscripten_start_wasm_audio_worklet_thread_async() doesn't use stackAlloc,
126+
// etc., but the created worklet does.
125127
emscripten_start_wasm_audio_worklet_thread_async__deps: [
126128
'$_wasmWorkersID',
127-
'$_EmAudioDispatchProcessorCallback'],
129+
'$_EmAudioDispatchProcessorCallback',
130+
'$stackAlloc', '$stackRestore', '$stackSave'],
128131
emscripten_start_wasm_audio_worklet_thread_async: (contextHandle, stackLowestAddress, stackSize, callback, userData) => {
129132

130133
#if ASSERTIONS
@@ -249,14 +252,15 @@ let LibraryWebAudio = {
249252
#endif
250253

251254
EmAudio[contextHandle].audioWorklet.bootstrapMessage.port.postMessage({
252-
// '_wpn' == 'Worklet Processor Name', use a deliberately mangled name so
253-
// that this field won't accidentally be mixed with user submitted
254-
// messages.
255-
_wpn: UTF8ToString(HEAPU32[options]),
256-
audioParams,
257-
contextHandle,
258-
callback,
259-
userData
255+
// Deliberately mangled and short names used here ('_wpn', the 'Worklet
256+
// Processor Name' used as a 'key' to verify the message type so as to
257+
// not get accidentally mixed with user submitted messages, the remainder
258+
// for space saving reasons, abbreviated from their variable names).
259+
'_wpn': UTF8ToString(HEAPU32[options]),
260+
'ap': audioParams,
261+
'ch': contextHandle,
262+
'cb': callback,
263+
'ud': userData
260264
});
261265
},
262266

src/runtime_shared.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
if (!shouldExport) {
1414
if (MODULARIZE && EXPORT_ALL) {
1515
shouldExport = true;
16-
} else if (AUDIO_WORKLET && (x == 'HEAP32' || x == 'HEAPU32')) {
16+
} else if (AUDIO_WORKLET && (x == 'HEAPU32' || x == 'HEAPF32')) {
1717
// Export to the AudioWorkletGlobalScope the needed variables to access
1818
// the heap. AudioWorkletGlobalScope is unable to access global JS vars
1919
// in the compiled main JS file.

0 commit comments

Comments
 (0)