|
| 1 | +#include <assert.h> |
| 2 | +#include <string.h> |
| 3 | +#include <stdio.h> |
| 4 | + |
| 5 | +#include <emscripten/em_js.h> |
| 6 | +#include <emscripten/webaudio.h> |
| 7 | + |
| 8 | +// Tests processing two mono audio inputs being mixed to a single mono audio |
| 9 | +// output in process() (by adding the inputs together). |
| 10 | + |
| 11 | +// This needs to be big enough for the mono output, 2x inputs and the worker stack |
| 12 | +#define AUDIO_STACK_SIZE 2048 |
| 13 | + |
| 14 | +// REPORT_RESULT is defined when running in Emscripten test harness. |
| 15 | +#ifdef REPORT_RESULT |
| 16 | +// Count the mixed frames and return after 375 frames (1 second with the default 128 size) |
| 17 | +volatile int audioProcessedCount = 0; |
| 18 | +bool playedAndMixed(double time, void* data) { |
| 19 | + if (audioProcessedCount >= 375) { |
| 20 | + REPORT_RESULT(0); |
| 21 | + return false; |
| 22 | + } |
| 23 | + return true; |
| 24 | +} |
| 25 | +#endif |
| 26 | + |
| 27 | +// ID to the beat and bass loops |
| 28 | +EMSCRIPTEN_WEBAUDIO_T beatID = 0; |
| 29 | +EMSCRIPTEN_WEBAUDIO_T bassID = 0; |
| 30 | + |
| 31 | +// Creates a MediaElementAudioSourceNode with the supplied URL (which is |
| 32 | +// registered as an internal audio object and the ID returned). |
| 33 | +EM_JS(EMSCRIPTEN_WEBAUDIO_T, createTrack, (EMSCRIPTEN_WEBAUDIO_T ctxID, const char* url, bool looping), { |
| 34 | + var context = emscriptenGetAudioObject(ctxID); |
| 35 | + if (context) { |
| 36 | + var audio = document.createElement('audio'); |
| 37 | + audio.src = UTF8ToString(url); |
| 38 | + audio.loop = looping; |
| 39 | + var track = context.createMediaElementSource(audio); |
| 40 | + return emscriptenRegisterAudioObject(track); |
| 41 | + } |
| 42 | + return 0; |
| 43 | +}); |
| 44 | + |
| 45 | +// Toggles the play/pause of a MediaElementAudioSourceNode given its ID |
| 46 | +EM_JS(void, toggleTrack, (EMSCRIPTEN_WEBAUDIO_T srcID), { |
| 47 | + var source = emscriptenGetAudioObject(srcID); |
| 48 | + if (source) { |
| 49 | + var audio = source.mediaElement; |
| 50 | + if (audio) { |
| 51 | + if (audio.paused) { |
| 52 | + audio.currentTime = 0; |
| 53 | + audio.play(); |
| 54 | + } else { |
| 55 | + audio.pause(); |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | +}); |
| 60 | + |
| 61 | +// Callback to process and mix the audio tracks |
| 62 | +bool process(int numInputs, const AudioSampleFrame* inputs, int numOutputs, AudioSampleFrame* outputs, int numParams, const AudioParamFrame* params, void* data) { |
| 63 | +#ifdef REPORT_RESULT |
| 64 | + audioProcessedCount++; |
| 65 | +#endif |
| 66 | + // Single mono output |
| 67 | + assert(numOutputs == 1 && outputs[0].numberOfChannels == 1); |
| 68 | + for (int n = 0; n < numInputs; n++) { |
| 69 | + // And all inputs are also stereo |
| 70 | + assert(inputs[n].numberOfChannels == 1 || inputs[n].numberOfChannels == 0); |
| 71 | + // This should always be the case |
| 72 | + assert(inputs[n].samplesPerChannel == outputs[0].samplesPerChannel); |
| 73 | + } |
| 74 | + // We can now do a quick mix since we know the layouts |
| 75 | + if (numInputs > 0) { |
| 76 | + int totalSamples = outputs[0].samplesPerChannel * outputs[0].numberOfChannels; |
| 77 | + float* outputData = outputs[0].data; |
| 78 | + memcpy(outputData, inputs[0].data, totalSamples * sizeof(float)); |
| 79 | + for (int n = 1; n < numInputs; n++) { |
| 80 | + // It's possible to have an input with no channels |
| 81 | + if (inputs[n].numberOfChannels == 1) { |
| 82 | + float* inputData = inputs[n].data; |
| 83 | + for (int i = totalSamples - 1; i >= 0; i--) { |
| 84 | + outputData[i] += inputData[i]; |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + return true; |
| 90 | +} |
| 91 | + |
| 92 | +// Registered click even to (1) enable audio playback and (2) toggle playing the tracks |
| 93 | +bool onClick(int type, const EmscriptenMouseEvent* e, void* data) { |
| 94 | + EMSCRIPTEN_WEBAUDIO_T ctx = (EMSCRIPTEN_WEBAUDIO_T) (data); |
| 95 | + if (emscripten_audio_context_state(ctx) != AUDIO_CONTEXT_STATE_RUNNING) { |
| 96 | + printf("Resuming playback\n"); |
| 97 | + emscripten_resume_audio_context_sync(ctx); |
| 98 | + } |
| 99 | + printf("Toggling audio playback\n"); |
| 100 | + toggleTrack(beatID); |
| 101 | + toggleTrack(bassID); |
| 102 | + return false; |
| 103 | +} |
| 104 | + |
| 105 | +// Audio processor created, now register the audio callback |
| 106 | +void processorCreated(EMSCRIPTEN_WEBAUDIO_T context, bool success, void* data) { |
| 107 | + if (success) { |
| 108 | + printf("Audio worklet processor created\n"); |
| 109 | + printf("Click to toggle audio playback\n"); |
| 110 | + |
| 111 | + // Mono output, two inputs |
| 112 | + int outputChannelCounts[1] = { 1 }; |
| 113 | + EmscriptenAudioWorkletNodeCreateOptions opts = { |
| 114 | + .numberOfInputs = 2, |
| 115 | + .numberOfOutputs = 1, |
| 116 | + .outputChannelCounts = outputChannelCounts |
| 117 | + }; |
| 118 | + EMSCRIPTEN_AUDIO_WORKLET_NODE_T worklet = emscripten_create_wasm_audio_worklet_node(context, "mixer", &opts, &process, NULL); |
| 119 | + emscripten_audio_node_connect(worklet, context, 0, 0); |
| 120 | + |
| 121 | + // Create the two mono source nodes and connect them to the two inputs |
| 122 | + // Note: we can connect the sources to the same input and it'll get mixed for us, but that's not the point |
| 123 | + beatID = createTrack(context, "audio_files/emscripten-beat-mono.mp3", true); |
| 124 | + if (beatID) { |
| 125 | + emscripten_audio_node_connect(beatID, worklet, 0, 0); |
| 126 | + } |
| 127 | + bassID = createTrack(context, "audio_files/emscripten-bass-mono.mp3", true); |
| 128 | + if (bassID) { |
| 129 | + emscripten_audio_node_connect(bassID, worklet, 0, 1); |
| 130 | + } |
| 131 | + |
| 132 | + emscripten_set_click_callback(EMSCRIPTEN_EVENT_TARGET_DOCUMENT, (void*) (context), false, &onClick); |
| 133 | + |
| 134 | +#ifdef REPORT_RESULT |
| 135 | + emscripten_set_timeout_loop(&playedAndMixed, 16, NULL); |
| 136 | +#endif |
| 137 | + } else { |
| 138 | + printf("Audio worklet node creation failed\n"); |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +// Worklet thread inited, now create the audio processor |
| 143 | +void initialised(EMSCRIPTEN_WEBAUDIO_T context, bool success, void* data) { |
| 144 | + if (success) { |
| 145 | + printf("Audio worklet initialised\n"); |
| 146 | + |
| 147 | + WebAudioWorkletProcessorCreateOptions opts = { |
| 148 | + .name = "mixer", |
| 149 | + }; |
| 150 | + emscripten_create_wasm_audio_worklet_processor_async(context, &opts, &processorCreated, NULL); |
| 151 | + } else { |
| 152 | + printf("Audio worklet failed to initialise\n"); |
| 153 | + } |
| 154 | +} |
| 155 | + |
| 156 | +int main() { |
| 157 | + static char workletStack[AUDIO_STACK_SIZE]; |
| 158 | + EMSCRIPTEN_WEBAUDIO_T context = emscripten_create_audio_context(NULL); |
| 159 | + emscripten_start_wasm_audio_worklet_thread_async(context, workletStack, sizeof workletStack, &initialised, NULL); |
| 160 | + return 0; |
| 161 | +} |
0 commit comments