Skip to content

Commit

Permalink
Allow the number of channels to increase (or the audio chain to change)
Browse files Browse the repository at this point in the history
Typed views are recreated if needed but otherwise are reused.
  • Loading branch information
cwoffenden committed Oct 17, 2024
1 parent 24a90e4 commit d186eb5
Showing 1 changed file with 16 additions and 12 deletions.
28 changes: 16 additions & 12 deletions src/audio_worklet.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ function createWasmAudioWorkletProcessor(audioParams) {
// shouldn't be required (to be verified).
this.samplesPerChannel = opts['sc'];
// Typed views of the output buffers on the worklet's stack, which after
// creation should not change (since the stack is passed externally once).
this.outputViews = null;
// creation should only change if the audio chain changes.
this.outputViews = [];
}

static get parameterDescriptors() {
Expand All @@ -57,7 +57,7 @@ function createWasmAudioWorkletProcessor(audioParams) {
bytesPerChannel = this.samplesPerChannel * 4,
stackMemoryNeeded = (numInputs + numOutputs) * {{{ C_STRUCTS.AudioSampleFrame.__size__ }}},
oldStackPtr = stackSave(),
inputsPtr, outputsPtr, outputDataPtr, paramsPtr,
inputsPtr, outputsPtr, outputDataPtr, paramsPtr, requiredViews = 0,
didProduceAudio, paramArray;

// Calculate how much stack space is needed.
Expand Down Expand Up @@ -96,18 +96,22 @@ function createWasmAudioWorkletProcessor(audioParams) {
k += {{{ C_STRUCTS.AudioSampleFrame.__size__ / 4 }}};
// Reserve space for the output data
dataPtr += bytesPerChannel * i.length;
// How many output views are needed in total?
requiredViews += i.length;
}
if (!this.outputViews) {

// Verify we have enough views (it doesn't matter if we have too many, any
// excess won't be accessed) then also verify the views' start address
// hasn't changed.
// TODO: allocate space for outputDataPtr before any inputs?
k = outputDataPtr;
if (this.outputViews.length < requiredViews || (this.outputViews.length && this.outputViews[0].byteOffset != k << 2)) {
this.outputViews = [];
k = outputDataPtr;
for (/*which output*/ i of outputList) {
for (/*which channel*/ j of i) {
this.outputViews.push({
// dataPtr is the sanity check (to be implemented)
// dataSub is the one-time subarray into the heap
dataPtr: k,
dataSub: HEAPF32.subarray(k, k += this.samplesPerChannel)
});
this.outputViews.push(
HEAPF32.subarray(k, k += this.samplesPerChannel)
);
}
}
}
Expand All @@ -134,7 +138,7 @@ function createWasmAudioWorkletProcessor(audioParams) {
k = 0;
for (i of outputList) {
for (j of i) {
j.set(this.outputViews[k++].dataSub);
j.set(this.outputViews[k++]);
}
}
}
Expand Down

0 comments on commit d186eb5

Please sign in to comment.