Skip to content

Commit ae63509

Browse files
committed
[AUDIO_WORKLET] Fix emscripten_audio_worklet_post_function_sig
It fixes emscripten_audio_worklet_post_function_sig, which was previously not worked, and enhances test to cover all emscripten_audio_worklet_post_function_*. In order to fix, it changes the old term `readAsmConstArgs` to the new term `readEmAsmArgs`, due to conflict of #16449 and #18218 Background history: 1. #16449 proposed, introducing AudioWorklet earlier 2. #18218 proposed, renaming readAsmConstArgs to readEmAsmArgs 3. #18218 merged earlier 4. #16449 merged later, using the old term `readAsmConstArgs` enhance the test for cover all emscripten_audio_worklet_post_function_*
1 parent 720e97f commit ae63509

File tree

2 files changed

+111
-16
lines changed

2 files changed

+111
-16
lines changed

src/lib/libwebaudio.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ let LibraryWebAudio = {
378378
emscripten_audio_worklet_post_function_3(audioContext, funcPtr, arg0, arg1, arg2);
379379
},
380380

381-
emscripten_audio_worklet_post_function_sig__deps: ['$readAsmConstArgs'],
381+
emscripten_audio_worklet_post_function_sig__deps: ['$readEmAsmArgs'],
382382
emscripten_audio_worklet_post_function_sig: (audioContext, funcPtr, sigPtr, varargs) => {
383383
#if ASSERTIONS
384384
assert(audioContext >= 0);
@@ -387,7 +387,7 @@ let LibraryWebAudio = {
387387
assert(UTF8ToString(sigPtr)[0] != 'v', 'Do NOT specify the return argument in the signature string for a call to emscripten_audio_worklet_post_function_sig(), just pass the function arguments.');
388388
assert(varargs);
389389
#endif
390-
(audioContext ? EmAudio[audioContext].audioWorklet.bootstrapMessage.port : globalThis['messagePort']).postMessage({'_wsc': funcPtr, 'x': readAsmConstArgs(sigPtr, varargs) });
390+
(audioContext ? EmAudio[audioContext].audioWorklet.bootstrapMessage.port : globalThis['messagePort']).postMessage({'_wsc': funcPtr, 'x': readEmAsmArgs(sigPtr, varargs) });
391391
}
392392
};
393393

test/webaudio/audioworklet_post_function.c

Lines changed: 109 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,123 @@
55
// and the Audio Worklet thread using the
66
// emscripten_audio_worklet_post_function_*() API.
77

8-
// This event will fire on the main thread.
9-
void MessageReceivedOnMainThread(int d, int e, int f) {
10-
emscripten_outf("MessageReceivedOnMainThread: d=%d, e=%d, f=%d", d, e, f);
11-
assert(!emscripten_current_thread_is_audio_worklet());
12-
assert(d == 1 && e == 2 && f == 3);
8+
// This test consists of two steps
9+
// 1. post function from main thread to audio worklet thread, with onAudioWorkletThread = true
10+
// 2. post function from audio worklet thread to main thread, with onAudioWorkletThread = false
11+
_Atomic bool onAudioWorkletThread = true;
12+
_Atomic uint32_t callbackCount = 0;
1313

14-
// test succeeded, were able to post a message from main thread to audio thread and back!
15-
emscripten_force_exit(0);
14+
// v, vi, vii, viii, vd, vdd, vddd, viiiiiidddddd
15+
// 8 callbacks be invoked twice, once with audio worklet thread, once with main thread.
16+
// Each callbacks check the correctivity of argument and running thread.
17+
void v() {
18+
assert(onAudioWorkletThread == emscripten_current_thread_is_audio_worklet());
19+
emscripten_out("v");
20+
++callbackCount;
1621
}
1722

18-
// This event will fire on the audio worklet thread.
19-
void MessageReceivedInAudioWorkletThread(int a, int b) {
20-
emscripten_outf("MessageReceivedInAudioWorkletThread: a=%d, b=%d", a, b);
21-
assert(emscripten_current_thread_is_audio_worklet());
22-
assert(a == 42 && b == 9000);
23-
emscripten_audio_worklet_post_function_viii(EMSCRIPTEN_AUDIO_MAIN_THREAD, MessageReceivedOnMainThread, /*d=*/1, /*e=*/2, /*f=*/3);
23+
void vi(int i) {
24+
assert(onAudioWorkletThread == emscripten_current_thread_is_audio_worklet());
25+
emscripten_out("vi");
26+
assert(i == 1);
27+
++callbackCount;
28+
}
29+
30+
void vii(int i, int j) {
31+
assert(onAudioWorkletThread == emscripten_current_thread_is_audio_worklet());
32+
emscripten_out("vii");
33+
assert(i == 2);
34+
assert(j == 3);
35+
++callbackCount;
36+
}
37+
38+
void viii(int i, int j, int k) {
39+
assert(onAudioWorkletThread == emscripten_current_thread_is_audio_worklet());
40+
emscripten_out("viii");
41+
assert(i == 4);
42+
assert(j == 5);
43+
assert(k == 6);
44+
++callbackCount;
45+
}
46+
47+
void vd(double i) {
48+
assert(onAudioWorkletThread == emscripten_current_thread_is_audio_worklet());
49+
emscripten_out("vd");
50+
assert(i == 1.5);
51+
++callbackCount;
52+
}
53+
54+
void vdd(double i, double j) {
55+
assert(onAudioWorkletThread == emscripten_current_thread_is_audio_worklet());
56+
emscripten_out("vdd");
57+
assert(i == 2.5);
58+
assert(j == 3.5);
59+
++callbackCount;
60+
}
61+
62+
void vddd(double i, double j, double k) {
63+
assert(onAudioWorkletThread == emscripten_current_thread_is_audio_worklet());
64+
emscripten_out("vddd");
65+
assert(i == 4.5);
66+
assert(j == 5.5);
67+
assert(k == 6.5);
68+
++callbackCount;
69+
}
70+
71+
void viiiiiidddddd(int a, int b, int c, int d, int e, int f, double g, double h, double i, double j, double k, double l) {
72+
assert(onAudioWorkletThread == emscripten_current_thread_is_audio_worklet());
73+
emscripten_out("viiiiiidddddd");
74+
assert(a == 10);
75+
assert(b == 11);
76+
assert(c == 12);
77+
assert(d == 13);
78+
assert(e == 14);
79+
assert(f == 15);
80+
assert(g == 16.5);
81+
assert(h == 17.5);
82+
assert(i == 18.5);
83+
assert(j == 19.5);
84+
assert(k == 20.5);
85+
assert(l == 21.5);
86+
++callbackCount;
87+
}
88+
89+
void sided_test_finished() {
90+
assert(onAudioWorkletThread == emscripten_current_thread_is_audio_worklet());
91+
92+
if (onAudioWorkletThread) { // for the first call, finishing checks for main -> audio worklet
93+
onAudioWorkletThread = false;
94+
95+
// These event will fire callbacks on main thread.
96+
emscripten_audio_worklet_post_function_v(EMSCRIPTEN_AUDIO_MAIN_THREAD, v);
97+
emscripten_audio_worklet_post_function_vi(EMSCRIPTEN_AUDIO_MAIN_THREAD, vi, 1);
98+
emscripten_audio_worklet_post_function_vii(EMSCRIPTEN_AUDIO_MAIN_THREAD, vii, 2, 3);
99+
emscripten_audio_worklet_post_function_viii(EMSCRIPTEN_AUDIO_MAIN_THREAD, viii, 4, 5, 6);
100+
emscripten_audio_worklet_post_function_vd(EMSCRIPTEN_AUDIO_MAIN_THREAD, vd, 1.5);
101+
emscripten_audio_worklet_post_function_vdd(EMSCRIPTEN_AUDIO_MAIN_THREAD, vdd, 2.5, 3.5);
102+
emscripten_audio_worklet_post_function_vddd(EMSCRIPTEN_AUDIO_MAIN_THREAD, vddd, 4.5, 5.5, 6.5);
103+
emscripten_audio_worklet_post_function_sig(EMSCRIPTEN_AUDIO_MAIN_THREAD, viiiiiidddddd, "iiiiiidddddd", 10, 11, 12, 13, 14, 15, 16.5, 17.5, 18.5, 19.5, 20.5, 21.5);
104+
emscripten_audio_worklet_post_function_v(EMSCRIPTEN_AUDIO_MAIN_THREAD, sided_test_finished);
105+
} else { // for the second call, finishing checks for audio worklet -> main
106+
assert(callbackCount == 16);
107+
emscripten_force_exit(0);
108+
}
24109
}
25110

26111
// This callback will fire when the audio worklet thread has been initialized.
27112
void WebAudioWorkletThreadInitialized(EMSCRIPTEN_WEBAUDIO_T audioContext, bool success, void *userData) {
28113
emscripten_out("WebAudioWorkletThreadInitialized");
29-
emscripten_audio_worklet_post_function_vii(audioContext, MessageReceivedInAudioWorkletThread, /*a=*/42, /*b=*/9000);
114+
115+
// These event will fire callbacks on audio worklet thread.
116+
emscripten_audio_worklet_post_function_v(audioContext, v);
117+
emscripten_audio_worklet_post_function_vi(audioContext, vi, 1);
118+
emscripten_audio_worklet_post_function_vii(audioContext, vii, 2, 3);
119+
emscripten_audio_worklet_post_function_viii(audioContext, viii, 4, 5, 6);
120+
emscripten_audio_worklet_post_function_vd(audioContext, vd, 1.5);
121+
emscripten_audio_worklet_post_function_vdd(audioContext, vdd, 2.5, 3.5);
122+
emscripten_audio_worklet_post_function_vddd(audioContext, vddd, 4.5, 5.5, 6.5);
123+
emscripten_audio_worklet_post_function_sig(audioContext, viiiiiidddddd, "iiiiiidddddd", 10, 11, 12, 13, 14, 15, 16.5, 17.5, 18.5, 19.5, 20.5, 21.5);
124+
emscripten_audio_worklet_post_function_v(audioContext, sided_test_finished);
30125
}
31126

32127
uint8_t wasmAudioWorkletStack[4096];

0 commit comments

Comments
 (0)