22
22
begin to fire.
23
23
*/
24
24
25
- // REPORT_RESULT is defined when running in Emscripten test harness. You can
26
- // strip these out in your own project.
27
- #ifdef REPORT_RESULT
25
+ // TEST_AND_EXIT is defined when running in Emscripten test harness. You can
26
+ // strip these out in your own project (otherwise playback will end quickly) .
27
+ #ifdef TEST_AND_EXIT
28
28
_Thread_local int testTlsVariable = 1 ;
29
29
int lastTlsVariableValueInAudioThread = 1 ;
30
30
#endif
31
31
32
32
// This function will be called for every fixed-size buffer of audio samples to be processed.
33
33
bool ProcessAudio (int numInputs , const AudioSampleFrame * inputs , int numOutputs , AudioSampleFrame * outputs , int numParams , const AudioParamFrame * params , void * userData ) {
34
- #ifdef REPORT_RESULT
34
+ #ifdef TEST_AND_EXIT
35
+ // Only running in the test harness, see main_thread_tls_access()
35
36
assert (testTlsVariable == lastTlsVariableValueInAudioThread );
36
37
++ testTlsVariable ;
37
38
lastTlsVariableValueInAudioThread = testTlsVariable ;
@@ -63,14 +64,15 @@ EM_JS(void, InitHtmlUi, (EMSCRIPTEN_WEBAUDIO_T audioContext), {
63
64
};
64
65
});
65
66
66
- #ifdef REPORT_RESULT
67
+ #ifdef TEST_AND_EXIT
67
68
bool main_thread_tls_access (double time , void * userData ) {
68
69
// Try to mess the TLS variable on the main thread, with the expectation that
69
- // it should not change the TLS value on the AudioWorklet thread.
70
+ // it should not change the TLS value on the AudioWorklet thread, asserted in
71
+ // ProcessAudio().
70
72
testTlsVariable = (int )time ;
73
+ // Exit to the test harness after enough calls to ProcessAudio()
71
74
if (lastTlsVariableValueInAudioThread >= 100 ) {
72
- REPORT_RESULT (0 );
73
- return false;
75
+ emscripten_force_exit (EXIT_SUCCESS );
74
76
}
75
77
return true;
76
78
}
@@ -79,7 +81,11 @@ bool main_thread_tls_access(double time, void *userData) {
79
81
// This callback will fire after the Audio Worklet Processor has finished being
80
82
// added to the Worklet global scope.
81
83
void AudioWorkletProcessorCreated (EMSCRIPTEN_WEBAUDIO_T audioContext , bool success , void * userData ) {
82
- if (!success ) return ;
84
+ if (!success ) {
85
+ emscripten_out ("Stopped in AudioWorkletProcessorCreated" );
86
+ assert (0 );
87
+ return ;
88
+ }
83
89
84
90
// Specify the input and output node configurations for the Wasm Audio
85
91
// Worklet. A simple setup with single mono output channel here, and no
@@ -97,7 +103,8 @@ void AudioWorkletProcessorCreated(EMSCRIPTEN_WEBAUDIO_T audioContext, bool succe
97
103
// Connect the audio worklet node to the graph.
98
104
emscripten_audio_node_connect (wasmAudioWorklet , audioContext , 0 , 0 );
99
105
100
- #ifdef REPORT_RESULT
106
+ #ifdef TEST_AND_EXIT
107
+ // Schedule this to exit after ProcessAudio() has been called 100 times
101
108
emscripten_set_timeout_loop (main_thread_tls_access , 10 , 0 );
102
109
#endif
103
110
@@ -108,7 +115,11 @@ void AudioWorkletProcessorCreated(EMSCRIPTEN_WEBAUDIO_T audioContext, bool succe
108
115
// AudioWorklet global scope, and is now ready to begin adding Audio Worklet
109
116
// Processors.
110
117
void WebAudioWorkletThreadInitialized (EMSCRIPTEN_WEBAUDIO_T audioContext , bool success , void * userData ) {
111
- if (!success ) return ;
118
+ if (!success ) {
119
+ emscripten_out ("Stopped in WebAudioWorkletThreadInitialized" );
120
+ assert (0 );
121
+ return ;
122
+ }
112
123
113
124
WebAudioWorkletProcessorCreateOptions opts = {
114
125
.name = "noise-generator" ,
@@ -132,4 +143,9 @@ int main() {
132
143
// and kick off Audio Worklet scope initialization, which shares the Wasm
133
144
// Module and Memory to the AudioWorklet scope and initializes its stack.
134
145
emscripten_start_wasm_audio_worklet_thread_async (context , wasmAudioWorkletStack , sizeof (wasmAudioWorkletStack ), WebAudioWorkletThreadInitialized , 0 );
146
+
147
+ #ifdef TEST_AND_EXIT
148
+ // We're in the test harness and exiting is via main_thread_tls_access()
149
+ emscripten_exit_with_live_runtime ();
150
+ #endif
135
151
}
0 commit comments