Skip to content

Commit a275a20

Browse files
committed
Replace 'magic' 128 number with a define
1 parent dd47be4 commit a275a20

File tree

4 files changed

+11
-7
lines changed

4 files changed

+11
-7
lines changed

site/source/docs/api_reference/wasm_audio_worklets.rst

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ processing graph as AudioWorkletNodes.
4545

4646
Once a class type is instantiated on the Web Audio graph and the graph is
4747
running, a C/C++ function pointer callback will be invoked for each 128
48-
samples of the processed audio stream that flows through the node.
48+
samples (``WEBAUDIO_QUANTUM_SIZE``) of the processed audio stream that flows
49+
through the node.
4950

5051
This callback will be executed on a dedicated separate audio processing
5152
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
158159
void *userData)
159160
{
160161
for(int i = 0; i < numOutputs; ++i)
161-
for(int j = 0; j < 128*outputs[i].numberOfChannels; ++j)
162+
for(int j = 0; j < WEBAUDIO_QUANTUM_SIZE*outputs[i].numberOfChannels; ++j)
162163
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
163164
164165
return true; // Keep the graph output going

system/include/emscripten/webaudio.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,17 +97,20 @@ void emscripten_create_wasm_audio_worklet_processor_async(EMSCRIPTEN_WEBAUDIO_T
9797

9898
typedef int EMSCRIPTEN_AUDIO_WORKLET_NODE_T;
9999

100+
// Number of samples processed per channel in the AudioSampleFrame (fixed at 128 in the Web Audio API specification)
101+
#define WEBAUDIO_QUANTUM_SIZE 128
102+
100103
typedef struct AudioSampleFrame
101104
{
102105
const int numberOfChannels;
103-
// An array of length numberOfChannels*128 elements, where data[channelIndex*128+i] locates the data of the i'th sample of channel channelIndex.
106+
// 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.
104107
float *data;
105108
} AudioSampleFrame;
106109

107110
typedef struct AudioParamFrame
108111
{
109112
// Specifies the length of the input array data (in float elements). This will be guaranteed to either have
110-
// a value of 1 or 128, depending on whether the audio parameter changed during this frame.
113+
// 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.
111114
int length;
112115
// An array of length specified in 'length'.
113116
float *data;

test/webaudio/audio_worklet_tone_generator.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,12 @@ bool ProcessAudio(int numInputs, const AudioSampleFrame *inputs, int numOutputs,
3838

3939
// Produce a sine wave tone of desired frequency to all output channels.
4040
for(int o = 0; o < numOutputs; ++o)
41-
for(int i = 0; i < 128; ++i)
41+
for(int i = 0; i < WEBAUDIO_QUANTUM_SIZE; ++i)
4242
{
4343
float s = emscripten_math_sin(phase);
4444
phase += phaseIncrement;
4545
for(int ch = 0; ch < outputs[o].numberOfChannels; ++ch)
46-
outputs[o].data[ch*128 + i] = s * currentVolume;
46+
outputs[o].data[ch*WEBAUDIO_QUANTUM_SIZE + i] = s * currentVolume;
4747
}
4848

4949
// Range reduce to keep precision around zero.

test/webaudio/audioworklet.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ bool ProcessAudio(int numInputs, const AudioSampleFrame *inputs, int numOutputs,
4040

4141
// Produce noise in all output channels.
4242
for(int i = 0; i < numOutputs; ++i)
43-
for(int j = 0; j < 128*outputs[i].numberOfChannels; ++j)
43+
for(int j = 0; j < WEBAUDIO_QUANTUM_SIZE*outputs[i].numberOfChannels; ++j)
4444
outputs[i].data[j] = (rand() / (float)RAND_MAX * 2.0f - 1.0f) * 0.3f;
4545

4646
// We generated audio and want to keep this processor going. Return false here to shut down.

0 commit comments

Comments
 (0)