|
| 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