Skip to content

Commit 8f9f6ad

Browse files
committed
Wrote a proper mixer
1 parent b9a7742 commit 8f9f6ad

File tree

1 file changed

+18
-9
lines changed

1 file changed

+18
-9
lines changed

test/webaudio/audioworklet_params_mixing.c

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,19 +47,28 @@ bool process(int numInputs, const AudioSampleFrame* inputs, int numOutputs, Audi
4747
// And for muted we need to fill the buffer with zeroes otherwise it repeats the last frame
4848
memset(outputData, 0, totalSamples * sizeof(float));
4949
}
50-
// Grab the mix level parameter (with either a length of 1 or the samples
51-
// per channel) and add any other inputs
52-
const AudioParamFrame* mixLevel = &params[0];
50+
// Grab the mix level parameter and expand it to have one entry per output
51+
// sample. This simplifies the mixer and smooths out browser differences.
52+
// Output and input buffers are stereo planar, so the mix data just repeats.
53+
float* const mixLevel = alloca(totalSamples * sizeof(float));
54+
if (params[0].length > 1) {
55+
// This is the regular path, one entry per sample by number of channels
56+
for (int ch = outputs[0].numberOfChannels - 1; ch >= 0; ch--) {
57+
memcpy(mixLevel + ch * outSamplesPerChannel, params[0].data, outSamplesPerChannel * sizeof(float));
58+
}
59+
} else {
60+
// Chrome will take this path when the k-rate parameter doesn't change
61+
float singleLevel = params[0].data[0];
62+
for (int n = totalSamples - 1; n >= 0; n--) {
63+
mixLevel[n] = singleLevel;
64+
}
65+
}
66+
// Now add another inputs with the mix level
5367
for (int n = 1; n < numInputs; n++) {
5468
if (inputs[n].numberOfChannels > 0) {
5569
float* inputData = inputs[n].data;
5670
for (int i = totalSamples - 1; i >= 0; i--) {
57-
// Output and input buffers are stereo planar in this example so we
58-
// need to get a mixLevel->data[] per channel, hence the quick % (and
59-
// as noticed in the wild, implementations have either one or all
60-
// entries, regardless of the param spec we passed in)
61-
float mixLevelValue = mixLevel->data[(mixLevel->length > 1) ? (i % outSamplesPerChannel) : 0];
62-
outputData[i] += inputData[i] * mixLevelValue;
71+
outputData[i] += inputData[i] * mixLevel[i];
6372
}
6473
}
6574
}

0 commit comments

Comments
 (0)