Skip to content

Commit b4a7e77

Browse files
authored
[AUDIO_WORKLET] Add interactive heap growing test NFC (#24931)
This adds an interactive test to force growing the heap during playback: ``` test/runner interactive.test_audio_worklet_memory_growth ``` Tested with `interactive64` and `interactive_2gb` (for `interactive64_4gb` the heap is already at the browser's max in testing so can't be grown). It works by alloc'ing and leaking 2/3 of the current size until it can no longer do so. Emscripten regrows its wasm memory in the process, invalidating any data views (see #24891). **Edit: test can now grow from both the main and audio thread.**
1 parent a14f6c8 commit b4a7e77

File tree

2 files changed

+133
-0
lines changed

2 files changed

+133
-0
lines changed

test/test_interactive.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,13 @@ def test_audio_worklet_params_mixing(self):
336336
shutil.copy(test_file('webaudio/audio_files/emscripten-bass.mp3'), 'audio_files/')
337337
self.btest_exit('webaudio/audioworklet_params_mixing.c', cflags=['-sAUDIO_WORKLET', '-sWASM_WORKERS'])
338338

339+
# Tests an AudioWorklet with a growable heap
340+
def test_audio_worklet_memory_growth(self):
341+
os.mkdir('audio_files')
342+
shutil.copy(test_file('webaudio/audio_files/emscripten-beat.mp3'), 'audio_files/')
343+
shutil.copy(test_file('webaudio/audio_files/emscripten-bass.mp3'), 'audio_files/')
344+
self.btest_exit('webaudio/audioworklet_memory_growth.c', cflags=['-sAUDIO_WORKLET', '-sWASM_WORKERS', '-sALLOW_MEMORY_GROWTH'])
345+
339346

340347
class interactive64(interactive):
341348
def setUp(self):
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
#include <assert.h>
2+
#include <string.h>
3+
4+
#include <emscripten/em_js.h>
5+
#include <emscripten/heap.h>
6+
#include <emscripten/webaudio.h>
7+
8+
// Tests processing two stereo audio inputs being mixed to a single stereo audio
9+
// output in process() (by adding the inputs together).
10+
11+
// This needs to be big enough for the stereo output, 2x inputs and the worker stack
12+
#define AUDIO_STACK_SIZE 4096
13+
14+
// Shared file playback and bootstrap
15+
#include "audioworklet_test_shared.inc"
16+
17+
// Callback to process and mix the audio tracks
18+
bool process(int numInputs, const AudioSampleFrame* inputs, int numOutputs, AudioSampleFrame* outputs, int numParams, const AudioParamFrame* params, void* data) {
19+
#ifdef TEST_AND_EXIT
20+
audioProcessedCount++;
21+
#endif
22+
23+
// Single stereo output
24+
assert(numOutputs == 1 && outputs[0].numberOfChannels == 2);
25+
for (int n = 0; n < numInputs; n++) {
26+
// And all inputs are also stereo
27+
assert(inputs[n].numberOfChannels == 2 || inputs[n].numberOfChannels == 0);
28+
// This should always be the case
29+
assert(inputs[n].samplesPerChannel == outputs[0].samplesPerChannel);
30+
}
31+
// We can now do a quick mix since we know the layouts
32+
if (numInputs > 0) {
33+
int totalSamples = outputs[0].samplesPerChannel * outputs[0].numberOfChannels;
34+
// Simple copy of the first input's audio data, checking that we have
35+
// channels (since a muted input has zero channels).
36+
float* outputData = outputs[0].data;
37+
if (inputs[0].numberOfChannels > 0) {
38+
memcpy(outputData, inputs[0].data, totalSamples * sizeof(float));
39+
} else {
40+
// And for muted we need to fill the buffer with zeroes otherwise it repeats the last frame
41+
memset(outputData, 0, totalSamples * sizeof(float));
42+
}
43+
// Now add another inputs
44+
for (int n = 1; n < numInputs; n++) {
45+
if (inputs[n].numberOfChannels > 0) {
46+
float* inputData = inputs[n].data;
47+
for (int i = totalSamples - 1; i >= 0; i--) {
48+
outputData[i] += inputData[i];
49+
}
50+
}
51+
}
52+
}
53+
return true;
54+
}
55+
56+
void doGrow() {
57+
size_t max = emscripten_get_heap_max();
58+
size_t preGrow = emscripten_get_heap_size();
59+
// Note we're leaking this on purpose
60+
if (!malloc((preGrow / 3) * 2)) {
61+
emscripten_out("Failed to malloc()");
62+
}
63+
emscripten_outf("Heap was %zu, now %zu (of %zu)", preGrow, emscripten_get_heap_size(), max);
64+
}
65+
66+
// Registered keypress event to grow the heap by 2/3
67+
bool onPress(int type, const EmscriptenKeyboardEvent* e, void* data) {
68+
if (!e->repeat) {
69+
size_t size = emscripten_get_heap_size();
70+
if (emscripten_get_heap_max() == size) {
71+
emscripten_outf("Cannot grow heap, rebuild with ALLOW_MEMORY_GROWTH? Heap is already %zu", size);
72+
} else {
73+
if (e->charCode == 32) {
74+
emscripten_out("Growing from audio worklet (see Console)");
75+
emscripten_audio_worklet_post_function_v(VOIDP_2_WA(data), &doGrow);
76+
} else {
77+
doGrow();
78+
}
79+
}
80+
}
81+
return false;
82+
}
83+
84+
// Audio processor created, now register the audio callback
85+
void processorCreated(EMSCRIPTEN_WEBAUDIO_T context, bool success, void* data) {
86+
assert(success && "Audio worklet failed in processorCreated()");
87+
emscripten_out("Audio worklet processor created");
88+
emscripten_out("Click to toggle audio playback");
89+
emscripten_out("Keypress to grow the heap, [space] to grow from the audio worklet's thread");
90+
91+
// Stereo output, two inputs
92+
int outputChannelCounts[1] = { 2 };
93+
EmscriptenAudioWorkletNodeCreateOptions opts = {
94+
.numberOfInputs = 2,
95+
.numberOfOutputs = 1,
96+
.outputChannelCounts = outputChannelCounts
97+
};
98+
EMSCRIPTEN_AUDIO_WORKLET_NODE_T worklet = emscripten_create_wasm_audio_worklet_node(context, "mixer", &opts, &process, NULL);
99+
emscripten_audio_node_connect(worklet, context, 0, 0);
100+
101+
// Create the two stereo source nodes and connect them to the two inputs
102+
// Note: we can connect the sources to the same input and it'll get mixed for us, but that's not the point
103+
beatID = createTrack(context, "audio_files/emscripten-beat.mp3", true);
104+
if (beatID) {
105+
emscripten_audio_node_connect(beatID, worklet, 0, 0);
106+
}
107+
bassID = createTrack(context, "audio_files/emscripten-bass.mp3", true);
108+
if (bassID) {
109+
emscripten_audio_node_connect(bassID, worklet, 0, 1);
110+
}
111+
112+
// Register a click to start playback
113+
emscripten_set_click_callback(EMSCRIPTEN_EVENT_TARGET_DOCUMENT, WA_2_VOIDP(context), false, &onClick);
114+
// And a keypress to alloc (and leak) to grow the heap
115+
emscripten_set_keypress_callback(EMSCRIPTEN_EVENT_TARGET_DOCUMENT, WA_2_VOIDP(context), false, &onPress);
116+
117+
#ifdef TEST_AND_EXIT
118+
// Register the counter that exits the test after one second of mixing
119+
emscripten_set_timeout_loop(&playedAndMixed, 16, NULL);
120+
#endif
121+
}
122+
123+
// This implementation has no custom start-up requirements
124+
EmscriptenStartWebAudioWorkletCallback getStartCallback(void) {
125+
return &initialised;
126+
}

0 commit comments

Comments
 (0)