Skip to content

Commit

Permalink
Made the call generic to work with any audio node
Browse files Browse the repository at this point in the history
  • Loading branch information
cwoffenden committed Oct 4, 2024
1 parent 3aafd4b commit 5569cc9
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 14 deletions.
2 changes: 1 addition & 1 deletion site/source/docs/api_reference/wasm_audio_worklets.rst
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ which resumes the audio context when the user clicks on the DOM Canvas element t
"noise-generator", &options, &GenerateNoise, 0);
// Connect it to audio context destination
emscripten_audio_worklet_node_connect(audioContext, wasmAudioWorklet);
emscripten_audio_node_connect(wasmAudioWorklet, audioContext, 0, 0);
// Resume context on mouse click
emscripten_set_click_callback("canvas", (void*)audioContext, 0, OnCanvasClick);
Expand Down
2 changes: 1 addition & 1 deletion src/library_sigs.js
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,7 @@ sigs = {
emscripten_atomic_wait_async__sig: 'ipippd',
emscripten_atomics_is_lock_free__sig: 'ii',
emscripten_audio_context_state__sig: 'ii',
emscripten_audio_worklet_node_connect__sig: 'vii',
emscripten_audio_node_connect__sig: 'viiii',
emscripten_audio_worklet_post_function_sig__sig: 'vippp',
emscripten_audio_worklet_post_function_v__sig: 'vip',
emscripten_audio_worklet_post_function_vd__sig: 'vipd',
Expand Down
16 changes: 9 additions & 7 deletions src/library_webaudio.js
Original file line number Diff line number Diff line change
Expand Up @@ -293,17 +293,19 @@ let LibraryWebAudio = {
},
#endif // ~AUDIO_WORKLET

emscripten_audio_worklet_node_connect: (contextHandle, workletNode) => {
emscripten_audio_node_connect: (source, destination, outputIndex, inputIndex) => {
var srcNode = EmAudio[source];
var dstNode = EmAudio[destination];
#if ASSERTIONS
assert(EmAudio[contextHandle], `Called emscripten_audio_worklet_node_connect() with an invalid AudioContext handle ${contextHandle}!`);
assert(EmAudio[contextHandle] instanceof (window.AudioContext || window.webkitAudioContext), `Called emscripten_audio_worklet_node_connect() on context handle ${contextHandle} that is not an AudioContext, but of type ${typeof EmAudio[contextHandle]} (${EmAudio[contextHandle]})`);
assert(EmAudio[workletNode], `Called emscripten_audio_worklet_node_connect() with an invalid AudioWorkletNode handle ${workletNode}`);
assert(EmAudio[workletNode].connect, `Called emscripten_audio_worklet_node_connect() on a handle ${workletNode} that is not an AudioWorkletNode, but of type ${typeof EmAudio[workletNode]} (${EmAudio[workletNode]})`);
assert(srcNode, `Called emscripten_audio_node_connect() with an invalid AudioNode handle ${source}`);
assert(srcNode instanceof window.AudioNode, `Called emscripten_audio_node_connect() on handle ${source} that is not an AudiotNode, but of type ${srcNode}`);
assert(dstNode, `Called emscripten_audio_node_connect() with an invalid AudioNode handle ${destination}!`);
assert(dstNode instanceof (window.AudioContext || window.webkitAudioContext) || dstNode instanceof window.AudioNode, `Called emscripten_audio_node_connect() on handle ${destination} that is not an AudioContext or AudioNode, but of type ${dstNode}`);
#endif
#if WEBAUDIO_DEBUG
console.log(`Connecting worklet with node ID ${workletNode} to Web Audio context with ID ${contextHandle}`);
console.log(`Connecting audio node ID ${source} to audio node ID ${destination} (${srcNode} to ${dstNode})`);
#endif
EmAudio[workletNode].connect(EmAudio[contextHandle].destination);
srcNode.connect(dstNode.destination || dstNode, outputIndex, inputIndex);
},

emscripten_current_thread_is_audio_worklet: () => typeof AudioWorkletGlobalScope !== 'undefined',
Expand Down
5 changes: 3 additions & 2 deletions system/include/emscripten/webaudio.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,9 @@ typedef struct EmscriptenAudioWorkletNodeCreateOptions
// userData4: A custom userdata pointer to pass to the callback function. This value will be passed on to the call to the given EmscriptenWorkletNodeProcessCallback callback function.
EMSCRIPTEN_AUDIO_WORKLET_NODE_T emscripten_create_wasm_audio_worklet_node(EMSCRIPTEN_WEBAUDIO_T audioContext, const char *name, const EmscriptenAudioWorkletNodeCreateOptions *options, EmscriptenWorkletNodeProcessCallback processCallback, void *userData4);

// Connects the audio worklet node to the audio context
void emscripten_audio_worklet_node_connect(EMSCRIPTEN_WEBAUDIO_T audioContext, EMSCRIPTEN_AUDIO_WORKLET_NODE_T workletNode);
// Connects a node's output to a target, e.g., connect the worklet node to the context.
// For outputIndex and inputIndex, see the AudioNode.connect() documentation (setting 0 as the default values)
void emscripten_audio_node_connect(EMSCRIPTEN_WEBAUDIO_T source, EMSCRIPTEN_WEBAUDIO_T destination, int outputIndex, int inputIndex);

// Returns true if the current thread is executing a Wasm AudioWorklet, false otherwise.
// Note that calling this function can be relatively slow as it incurs a Wasm->JS transition,
Expand Down
2 changes: 1 addition & 1 deletion test/webaudio/audio_worklet_tone_generator.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ void AudioWorkletProcessorCreated(EMSCRIPTEN_WEBAUDIO_T audioContext, bool succe
EMSCRIPTEN_AUDIO_WORKLET_NODE_T wasmAudioWorklet = emscripten_create_wasm_audio_worklet_node(audioContext, "tone-generator", &options, &ProcessAudio, 0);

// Connect the audio worklet node to the graph.
emscripten_audio_worklet_node_connect(audioContext, wasmAudioWorklet);
emscripten_audio_node_connect(wasmAudioWorklet, audioContext, 0, 0);
EM_ASM({
// Add a button on the page to toggle playback as a response to user click.
let startButton = document.createElement('button');
Expand Down
2 changes: 1 addition & 1 deletion test/webaudio/audioworklet.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ void AudioWorkletProcessorCreated(EMSCRIPTEN_WEBAUDIO_T audioContext, bool succe
// Instantiate the noise-generator Audio Worklet Processor.
EMSCRIPTEN_AUDIO_WORKLET_NODE_T wasmAudioWorklet = emscripten_create_wasm_audio_worklet_node(audioContext, "noise-generator", &options, &ProcessAudio, 0);
// Connect the audio worklet node to the graph.
emscripten_audio_worklet_node_connect(audioContext, wasmAudioWorklet);
emscripten_audio_node_connect(wasmAudioWorklet, audioContext, 0, 0);

#ifdef REPORT_RESULT
emscripten_set_timeout_loop(main_thread_tls_access, 10, 0);
Expand Down
2 changes: 1 addition & 1 deletion test/webaudio/audioworklet_emscripten_futex_wake.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ void AudioWorkletProcessorCreated(EMSCRIPTEN_WEBAUDIO_T audioContext, bool succe
int outputChannelCounts[1] = { 1 };
EmscriptenAudioWorkletNodeCreateOptions options = { .numberOfInputs = 0, .numberOfOutputs = 1, .outputChannelCounts = outputChannelCounts };
EMSCRIPTEN_AUDIO_WORKLET_NODE_T wasmAudioWorklet = emscripten_create_wasm_audio_worklet_node(audioContext, "noise-generator", &options, &ProcessAudio, 0);
emscripten_audio_worklet_node_connect(audioContext, wasmAudioWorklet);
emscripten_audio_node_connect(wasmAudioWorklet, audioContext, 0, 0);
InitHtmlUi(audioContext);
}

Expand Down

0 comments on commit 5569cc9

Please sign in to comment.