@@ -53,19 +53,29 @@ bool process(int numInputs, const AudioSampleFrame* inputs, int numOutputs, Audi
53
53
return true;
54
54
}
55
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
+
56
66
// Registered keypress event to grow the heap by 2/3
57
67
bool onPress (int type , const EmscriptenKeyboardEvent * e , void * data ) {
58
68
if (!e -> repeat ) {
59
- size_t max = emscripten_get_heap_max ();
60
69
size_t size = emscripten_get_heap_size ();
61
- if (max == size ) {
70
+ if (emscripten_get_heap_max () == size ) {
62
71
emscripten_outf ("Cannot grow heap, rebuild with ALLOW_MEMORY_GROWTH? Heap is already %zu" , size );
63
72
} else {
64
- // Note we're leaking this on purpose
65
- if (!malloc ((size / 3 ) * 2 )) {
66
- emscripten_out ("Failed to malloc()" );
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 ();
67
78
}
68
- emscripten_outf ("Heap was %zu, now %zu (of %zu)" , size , emscripten_get_heap_size (), max );
69
79
}
70
80
}
71
81
return false;
@@ -76,7 +86,7 @@ void processorCreated(EMSCRIPTEN_WEBAUDIO_T context, bool success, void* data) {
76
86
assert (success && "Audio worklet failed in processorCreated()" );
77
87
emscripten_out ("Audio worklet processor created" );
78
88
emscripten_out ("Click to toggle audio playback" );
79
- emscripten_out ("Keypress to grow the heap" );
89
+ emscripten_out ("Keypress to grow the heap, [space] to grow from the audio worklet's thread " );
80
90
81
91
// Stereo output, two inputs
82
92
int outputChannelCounts [1 ] = { 2 };
@@ -102,7 +112,7 @@ void processorCreated(EMSCRIPTEN_WEBAUDIO_T context, bool success, void* data) {
102
112
// Register a click to start playback
103
113
emscripten_set_click_callback (EMSCRIPTEN_EVENT_TARGET_DOCUMENT , WA_2_VOIDP (context ), false, & onClick );
104
114
// And a keypress to alloc (and leak) to grow the heap
105
- emscripten_set_keypress_callback (EMSCRIPTEN_EVENT_TARGET_DOCUMENT , NULL , false, & onPress );
115
+ emscripten_set_keypress_callback (EMSCRIPTEN_EVENT_TARGET_DOCUMENT , WA_2_VOIDP ( context ) , false, & onPress );
106
116
107
117
#ifdef TEST_AND_EXIT
108
118
// Register the counter that exits the test after one second of mixing
0 commit comments