Skip to content

Commit

Permalink
Fix automatic run of interactive web audio tests.
Browse files Browse the repository at this point in the history
Audio contexts cannot start until human interacts with the page, e.g. by clicking a button.

Therefore interactive audioworklet tests require user to press a button.

However, after clicking a button, all later interactive tests will have the audio context access already unlocked, so user does not need to click a button on each test when running multiple audio worklet tests.

In such scenario, there was a bug that nothing played back from speakers, because the audio worklet was not connected to output until user clicks a button. But since this button click was not needed, the test would pass without actually playing back any audio.
  • Loading branch information
juj committed Sep 5, 2024
1 parent 791e366 commit 2c0ef00
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 7 deletions.
7 changes: 3 additions & 4 deletions test/webaudio/audio_worklet_tone_generator.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ void AudioWorkletProcessorCreated(EMSCRIPTEN_WEBAUDIO_T audioContext, EM_BOOL su

EM_ASM({
let audioContext = emscriptenGetAudioObject($0);
let audioWorkletNode = emscriptenGetAudioObject($1);
// Connect the audio worklet node to the graph.
audioWorkletNode.connect(audioContext.destination);

// Add a button on the page to toggle playback as a response to user click.
let startButton = document.createElement('button');
Expand All @@ -93,10 +96,6 @@ void AudioWorkletProcessorCreated(EMSCRIPTEN_WEBAUDIO_T audioContext, EM_BOOL su
startButton.onclick = () => {
if (audioContext.state != 'running') {
audioContext.resume();
let audioWorkletNode = emscriptenGetAudioObject($1);

// Connect the audio worklet node to the graph.
audioWorkletNode.connect(audioContext.destination);
} else {
audioContext.suspend();
}
Expand Down
4 changes: 2 additions & 2 deletions test/webaudio/audioworklet.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ EM_BOOL ProcessAudio(int numInputs, const AudioSampleFrame *inputs, int numOutpu
EM_JS(void, InitHtmlUi, (EMSCRIPTEN_WEBAUDIO_T audioContext, EMSCRIPTEN_AUDIO_WORKLET_NODE_T audioWorkletNode), {
audioContext = emscriptenGetAudioObject(audioContext);
audioWorkletNode = emscriptenGetAudioObject(audioWorkletNode);
// Connect the audio worklet node to the graph.
audioWorkletNode.connect(audioContext.destination);

// Add a button on the page to toggle playback as a response to user click.
let startButton = document.createElement('button');
Expand All @@ -59,8 +61,6 @@ EM_JS(void, InitHtmlUi, (EMSCRIPTEN_WEBAUDIO_T audioContext, EMSCRIPTEN_AUDIO_WO
startButton.onclick = () => {
if (audioContext.state != 'running') {
audioContext.resume();
// Connect the audio worklet node to the graph.
audioWorkletNode.connect(audioContext.destination);
} else {
audioContext.suspend();
}
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 @@ -29,12 +29,12 @@ EM_BOOL ProcessAudio(int numInputs, const AudioSampleFrame *inputs, int numOutpu
EM_JS(void, InitHtmlUi, (EMSCRIPTEN_WEBAUDIO_T audioContext, EMSCRIPTEN_AUDIO_WORKLET_NODE_T audioWorkletNode), {
audioContext = emscriptenGetAudioObject(audioContext);
audioWorkletNode = emscriptenGetAudioObject(audioWorkletNode);
audioWorkletNode.connect(audioContext.destination);
let startButton = document.createElement('button');
startButton.innerHTML = 'Start playback';
document.body.appendChild(startButton);

startButton.onclick = () => {
audioWorkletNode.connect(audioContext.destination);
audioContext.resume();
};
});
Expand Down

0 comments on commit 2c0ef00

Please sign in to comment.