|
| 1 | +#include <stdio.h> |
| 2 | + |
1 | 3 | #include <emscripten/em_js.h> |
2 | 4 | #include <emscripten/webaudio.h> |
3 | 5 |
|
4 | | -#define AUDIO_STACK_SIZE 2048 |
| 6 | +#define AUDIO_STACK_SIZE 4096 |
| 7 | + |
| 8 | +#ifdef REPORT_RESULT |
| 9 | +void playedAndMixed(void* data) { |
| 10 | + REPORT_RESULT(0); |
| 11 | +} |
| 12 | +#endif |
5 | 13 |
|
6 | | -EM_JS(bool, addAudio, (EMSCRIPTEN_WEBAUDIO_T ctxHnd, const char* url, const char* label), { |
| 14 | +// Adds a button to play and stop an audio file |
| 15 | +EM_JS(bool, addAudio, (EMSCRIPTEN_WEBAUDIO_T ctxHnd, EMSCRIPTEN_AUDIO_WORKLET_NODE_T nodeHnd, int index, const char* url, const char* label), { |
7 | 16 | var context = emscriptenGetAudioObject(ctxHnd); |
8 | 17 | if (context) { |
9 | 18 | var audio = document.createElement('audio'); |
10 | 19 | audio.src = UTF8ToString(url); |
11 | 20 | audio.loop = true; |
12 | 21 | var track = context.createMediaElementSource(audio); |
13 | | - track.connect(context.destination); |
14 | | - |
| 22 | + |
| 23 | + var worklet = emscriptenGetAudioObject(nodeHnd); |
| 24 | + track.connect(worklet ? worklet : context.destination, 0, index); |
| 25 | + |
15 | 26 | var button = document.createElement('button'); |
16 | 27 | button.innerHTML = UTF8ToString(label); |
17 | 28 | button.onclick = () => { |
18 | 29 | if (context.state == 'suspended') { |
19 | 30 | context.resume(); |
20 | 31 | } |
21 | | - audio.play(); |
| 32 | + if (audio.paused) { |
| 33 | + audio.currentTime = 0; |
| 34 | + audio.play(); |
| 35 | + } else { |
| 36 | + audio.pause(); |
| 37 | + } |
| 38 | + |
22 | 39 | }; |
23 | | - |
24 | 40 | document.body.appendChild(button); |
25 | 41 | return true; |
26 | 42 | } |
27 | 43 | return false; |
28 | 44 | }); |
29 | 45 |
|
| 46 | +bool process(int numInputs, const AudioSampleFrame* inputs, int numOutputs, AudioSampleFrame* outputs, int numParams, const AudioParamFrame* params, void* data) { |
| 47 | + for (int o = 0; o < numOutputs; o++) { |
| 48 | + for (int n = outputs[o].samplesPerChannel * outputs[o].numberOfChannels - 1; n >= 0; n--) { |
| 49 | + outputs[o].data[n] = 0.0f; |
| 50 | + for (int i = 0; i < numInputs; i++) { |
| 51 | + outputs[o].data[n] += inputs[i].data[n] * 0.75f; |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | + return true; |
| 56 | +} |
| 57 | + |
| 58 | +void processorCreated(EMSCRIPTEN_WEBAUDIO_T context, bool success, void* data) { |
| 59 | + if (success) { |
| 60 | + printf("Audio worklet processor created\n"); |
| 61 | + |
| 62 | + int outputChannelCounts[1] = { 2 }; |
| 63 | + EmscriptenAudioWorkletNodeCreateOptions opts = { |
| 64 | + .numberOfInputs = 2, |
| 65 | + .numberOfOutputs = 1, |
| 66 | + .outputChannelCounts = outputChannelCounts |
| 67 | + }; |
| 68 | + EMSCRIPTEN_AUDIO_WORKLET_NODE_T worklet = emscripten_create_wasm_audio_worklet_node(context, "mixer", &opts, &process, NULL); |
| 69 | + emscripten_audio_node_connect(worklet, context, 0, 0); |
| 70 | + |
| 71 | + addAudio(context, worklet, 0, "audio_files/emscripten-beat.mp3", "Toggle Beat"); |
| 72 | + addAudio(context, worklet, 1, "audio_files/emscripten-bass.mp3", "Toggle Bass"); |
| 73 | + } else { |
| 74 | + printf("Audio worklet node creation failed\n"); |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +void initialised(EMSCRIPTEN_WEBAUDIO_T context, bool success, void* data) { |
| 79 | + if (success) { |
| 80 | + printf("Audio worklet initialised\n"); |
| 81 | + |
| 82 | + WebAudioWorkletProcessorCreateOptions opts = { |
| 83 | + .name = "mixer", |
| 84 | + }; |
| 85 | + emscripten_create_wasm_audio_worklet_processor_async(context, &opts, &processorCreated, NULL); |
| 86 | + } else { |
| 87 | + printf("Audio worklet failed to initialise\n"); |
| 88 | + } |
| 89 | +} |
| 90 | + |
30 | 91 | int main() { |
| 92 | + static char workletStack[AUDIO_STACK_SIZE]; |
31 | 93 | EMSCRIPTEN_WEBAUDIO_T context = emscripten_create_audio_context(NULL); |
| 94 | + emscripten_start_wasm_audio_worklet_thread_async(context, workletStack, sizeof(workletStack), initialised, NULL); |
32 | 95 |
|
33 | | - addAudio(context, "audio_files/emscripten-beat.mp3", "Play Beat"); |
34 | | - addAudio(context, "audio_files/emscripten-bass.mp3", "Play Bass"); |
| 96 | + return 0; |
35 | 97 | } |
0 commit comments