From a275a20fa2f9fdfbcc142f614dbcc0c495ea416c Mon Sep 17 00:00:00 2001 From: Carl Woffenden Date: Fri, 4 Oct 2024 14:56:22 +0200 Subject: [PATCH] Replace 'magic' 128 number with a define --- site/source/docs/api_reference/wasm_audio_worklets.rst | 5 +++-- system/include/emscripten/webaudio.h | 7 +++++-- test/webaudio/audio_worklet_tone_generator.c | 4 ++-- test/webaudio/audioworklet.c | 2 +- 4 files changed, 11 insertions(+), 7 deletions(-) diff --git a/site/source/docs/api_reference/wasm_audio_worklets.rst b/site/source/docs/api_reference/wasm_audio_worklets.rst index b7a24dc4f02d..edb5c2a3d68a 100644 --- a/site/source/docs/api_reference/wasm_audio_worklets.rst +++ b/site/source/docs/api_reference/wasm_audio_worklets.rst @@ -45,7 +45,8 @@ processing graph as AudioWorkletNodes. Once a class type is instantiated on the Web Audio graph and the graph is running, a C/C++ function pointer callback will be invoked for each 128 -samples of the processed audio stream that flows through the node. +samples (``WEBAUDIO_QUANTUM_SIZE``) of the processed audio stream that flows +through the node. This callback will be executed on a dedicated separate audio processing thread with real-time processing priority. Each Web Audio context will @@ -158,7 +159,7 @@ which resumes the audio context when the user clicks on the DOM Canvas element t void *userData) { for(int i = 0; i < numOutputs; ++i) - for(int j = 0; j < 128*outputs[i].numberOfChannels; ++j) + for(int j = 0; j < WEBAUDIO_QUANTUM_SIZE*outputs[i].numberOfChannels; ++j) outputs[i].data[j] = emscripten_random() * 0.2 - 0.1; // Warning: scale down audio volume by factor of 0.2, raw noise can be really loud otherwise return true; // Keep the graph output going diff --git a/system/include/emscripten/webaudio.h b/system/include/emscripten/webaudio.h index a3261c47ac1d..b1046dbf238b 100644 --- a/system/include/emscripten/webaudio.h +++ b/system/include/emscripten/webaudio.h @@ -97,17 +97,20 @@ void emscripten_create_wasm_audio_worklet_processor_async(EMSCRIPTEN_WEBAUDIO_T typedef int EMSCRIPTEN_AUDIO_WORKLET_NODE_T; +// Number of samples processed per channel in the AudioSampleFrame (fixed at 128 in the Web Audio API specification) +#define WEBAUDIO_QUANTUM_SIZE 128 + typedef struct AudioSampleFrame { const int numberOfChannels; - // An array of length numberOfChannels*128 elements, where data[channelIndex*128+i] locates the data of the i'th sample of channel channelIndex. + // An array of length numberOfChannels*128 elements (128 being WEBAUDIO_QUANTUM_SIZE), where data[channelIndex*128+i] locates the data of the i'th sample of channel channelIndex. float *data; } AudioSampleFrame; typedef struct AudioParamFrame { // Specifies the length of the input array data (in float elements). This will be guaranteed to either have - // a value of 1 or 128, depending on whether the audio parameter changed during this frame. + // a value of 1, for a parameter valid for the entire frame, or 128 (the WEBAUDIO_QUANTUM_SIZE), for a parameter that changes during the frame. int length; // An array of length specified in 'length'. float *data; diff --git a/test/webaudio/audio_worklet_tone_generator.c b/test/webaudio/audio_worklet_tone_generator.c index cfec0dc0ec87..9e9f910bb601 100644 --- a/test/webaudio/audio_worklet_tone_generator.c +++ b/test/webaudio/audio_worklet_tone_generator.c @@ -38,12 +38,12 @@ bool ProcessAudio(int numInputs, const AudioSampleFrame *inputs, int numOutputs, // Produce a sine wave tone of desired frequency to all output channels. for(int o = 0; o < numOutputs; ++o) - for(int i = 0; i < 128; ++i) + for(int i = 0; i < WEBAUDIO_QUANTUM_SIZE; ++i) { float s = emscripten_math_sin(phase); phase += phaseIncrement; for(int ch = 0; ch < outputs[o].numberOfChannels; ++ch) - outputs[o].data[ch*128 + i] = s * currentVolume; + outputs[o].data[ch*WEBAUDIO_QUANTUM_SIZE + i] = s * currentVolume; } // Range reduce to keep precision around zero. diff --git a/test/webaudio/audioworklet.c b/test/webaudio/audioworklet.c index 9240063a8648..20e7837cd066 100644 --- a/test/webaudio/audioworklet.c +++ b/test/webaudio/audioworklet.c @@ -40,7 +40,7 @@ bool ProcessAudio(int numInputs, const AudioSampleFrame *inputs, int numOutputs, // Produce noise in all output channels. for(int i = 0; i < numOutputs; ++i) - for(int j = 0; j < 128*outputs[i].numberOfChannels; ++j) + for(int j = 0; j < WEBAUDIO_QUANTUM_SIZE*outputs[i].numberOfChannels; ++j) outputs[i].data[j] = (rand() / (float)RAND_MAX * 2.0f - 1.0f) * 0.3f; // We generated audio and want to keep this processor going. Return false here to shut down.