Skip to content

Commit d4680ca

Browse files
committed
Allow the number of channels to increase (or the audio chain to change)
Typed views are recreated if needed but otherwise are reused.
1 parent 9b3dbb6 commit d4680ca

File tree

1 file changed

+16
-12
lines changed

1 file changed

+16
-12
lines changed

src/audio_worklet.js

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ function createWasmAudioWorkletProcessor(audioParams) {
3838
// shouldn't be required (to be verified).
3939
this.samplesPerChannel = opts['sc'];
4040
// Typed views of the output buffers on the worklet's stack, which after
41-
// creation should not change (since the stack is passed externally once).
42-
this.outputViews = null;
41+
// creation should only change if the audio chain changes.
42+
this.outputViews = [];
4343
}
4444

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

6363
// Calculate how much stack space is needed.
@@ -96,18 +96,22 @@ function createWasmAudioWorkletProcessor(audioParams) {
9696
k += {{{ C_STRUCTS.AudioSampleFrame.__size__ / 4 }}};
9797
// Reserve space for the output data
9898
dataPtr += bytesPerChannel * i.length;
99+
// How many output views are needed in total?
100+
requiredViews += i.length;
99101
}
100-
if (!this.outputViews) {
102+
103+
// Verify we have enough views (it doesn't matter if we have too many, any
104+
// excess won't be accessed) then also verify the views' start address
105+
// hasn't changed.
106+
// TODO: allocate space for outputDataPtr before any inputs?
107+
k = outputDataPtr;
108+
if (this.outputViews.length < requiredViews || (this.outputViews.length && this.outputViews[0].byteOffset != k << 2)) {
101109
this.outputViews = [];
102-
k = outputDataPtr;
103110
for (/*which output*/ i of outputList) {
104111
for (/*which channel*/ j of i) {
105-
this.outputViews.push({
106-
// dataPtr is the sanity check (to be implemented)
107-
// dataSub is the one-time subarray into the heap
108-
dataPtr: k,
109-
dataSub: HEAPF32.subarray(k, k += this.samplesPerChannel)
110-
});
112+
this.outputViews.push(
113+
HEAPF32.subarray(k, k += this.samplesPerChannel)
114+
);
111115
}
112116
}
113117
}
@@ -134,7 +138,7 @@ function createWasmAudioWorkletProcessor(audioParams) {
134138
k = 0;
135139
for (i of outputList) {
136140
for (j of i) {
137-
j.set(this.outputViews[k++].dataSub);
141+
j.set(this.outputViews[k++]);
138142
}
139143
}
140144
}

0 commit comments

Comments
 (0)