Skip to content

Commit 03bb4a6

Browse files
committed
Add heap growing test
1 parent 2659582 commit 03bb4a6

File tree

2 files changed

+123
-0
lines changed

2 files changed

+123
-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_grow_heap(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_grow_heap.c', cflags=['-sAUDIO_WORKLET', '-sWASM_WORKERS', '-sALLOW_MEMORY_GROWTH'])
345+
339346

340347
class interactive64(interactive):
341348
def setUp(self):
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
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+
// Registered keypress event to grow the heap by 2/3
57+
bool onPress(int type, const EmscriptenKeyboardEvent* e, void* data) {
58+
if (!e->repeat) {
59+
size_t max = emscripten_get_heap_max();
60+
size_t size = emscripten_get_heap_size();
61+
if (max == size) {
62+
emscripten_outf("Cannot grow heap, rebuild with ALLOW_MEMORY_GROWTH? Heap is already %zu", size);
63+
} else {
64+
// Note we're leaking this on purpose
65+
if (!malloc((size / 3) * 2)) {
66+
emscripten_out("Failed to malloc()");
67+
}
68+
emscripten_outf("Heap was %zu, now %zu (of %zu)", size, emscripten_get_heap_size(), max);
69+
}
70+
}
71+
return false;
72+
}
73+
74+
// Audio processor created, now register the audio callback
75+
void processorCreated(EMSCRIPTEN_WEBAUDIO_T context, bool success, void* data) {
76+
assert(success && "Audio worklet failed in processorCreated()");
77+
emscripten_out("Audio worklet processor created");
78+
emscripten_out("Click to toggle audio playback");
79+
emscripten_out("Keypress to grow the heap");
80+
81+
// Stereo output, two inputs
82+
int outputChannelCounts[1] = { 2 };
83+
EmscriptenAudioWorkletNodeCreateOptions opts = {
84+
.numberOfInputs = 2,
85+
.numberOfOutputs = 1,
86+
.outputChannelCounts = outputChannelCounts
87+
};
88+
EMSCRIPTEN_AUDIO_WORKLET_NODE_T worklet = emscripten_create_wasm_audio_worklet_node(context, "mixer", &opts, &process, NULL);
89+
emscripten_audio_node_connect(worklet, context, 0, 0);
90+
91+
// Create the two stereo source nodes and connect them to the two inputs
92+
// Note: we can connect the sources to the same input and it'll get mixed for us, but that's not the point
93+
beatID = createTrack(context, "audio_files/emscripten-beat.mp3", true);
94+
if (beatID) {
95+
emscripten_audio_node_connect(beatID, worklet, 0, 0);
96+
}
97+
bassID = createTrack(context, "audio_files/emscripten-bass.mp3", true);
98+
if (bassID) {
99+
emscripten_audio_node_connect(bassID, worklet, 0, 1);
100+
}
101+
102+
// Register a click to start playback
103+
emscripten_set_click_callback(EMSCRIPTEN_EVENT_TARGET_DOCUMENT, WA_2_VOIDP(context), false, &onClick);
104+
// And a keypress to alloc (and leak) to grow the heap
105+
emscripten_set_keypress_callback(EMSCRIPTEN_EVENT_TARGET_DOCUMENT, NULL, false, &onPress);
106+
107+
#ifdef TEST_AND_EXIT
108+
// Register the counter that exits the test after one second of mixing
109+
emscripten_set_timeout_loop(&playedAndMixed, 16, NULL);
110+
#endif
111+
}
112+
113+
// This implementation has no custom start-up requirements
114+
EmscriptenStartWebAudioWorkletCallback getStartCallback(void) {
115+
return &initialised;
116+
}

0 commit comments

Comments
 (0)