Skip to content

Commit

Permalink
Replace 'magic' 128 number with a define
Browse files Browse the repository at this point in the history
  • Loading branch information
cwoffenden committed Oct 4, 2024
1 parent dd47be4 commit a275a20
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 7 deletions.
5 changes: 3 additions & 2 deletions site/source/docs/api_reference/wasm_audio_worklets.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
7 changes: 5 additions & 2 deletions system/include/emscripten/webaudio.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions test/webaudio/audio_worklet_tone_generator.c
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion test/webaudio/audioworklet.c
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit a275a20

Please sign in to comment.