Skip to content

Commit d423aec

Browse files
committed
Make sure that polyfill-ed worketl nodes never have a smaller buffer size than the one chosen by the polyfill
1 parent 226a237 commit d423aec

11 files changed

+115
-19
lines changed

lib/p5.sound.js

Lines changed: 44 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/p5.sound.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/p5.sound.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/p5.sound.min.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/amplitude.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,17 @@ define(function (require) {
7272
}
7373
}.bind(this);
7474

75+
// if the AudioWorkletNode is actually a ScriptProcessorNode created via polyfill,
76+
// make sure that our chosen buffer size isn't smaller than the buffer size automatically
77+
// selected by the polyfill
78+
// reference: https://github.com/GoogleChromeLabs/audioworklet-polyfill/issues/13#issuecomment-425014930
79+
if (this._workletNode instanceof ScriptProcessorNode) {
80+
this._workletNode.port.postMessage({
81+
name: 'bufferSize',
82+
bufferSize: Math.max(this.bufferSize, this._workletNode.bufferSize)
83+
});
84+
}
85+
7586
// for connections
7687
this.input = this._workletNode;
7788

src/audioWorklet/amplitudeProcessor.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,8 @@ class AmplitudeProcessor extends AudioWorkletProcessor {
1111
this.numInputChannels = processorOptions.numInputChannels || 2;
1212
this.normalize = processorOptions.normalize || false;
1313
this.smoothing = processorOptions.smoothing || 0;
14-
this.bufferSize = processorOptions.bufferSize || 2048;
1514

16-
this.inputRingBuffer = new RingBuffer(this.bufferSize, this.numInputChannels);
17-
this.outputRingBuffer = new RingBuffer(this.bufferSize, this.numOutputChannels);
18-
this.inputRingBufferArraySequence = new Array(this.numInputChannels).fill(null).map(() => new Float32Array(this.bufferSize));
15+
this.setBufferSize(processorOptions.bufferSize || 2048);
1916

2017
this.stereoVol = [0, 0];
2118
this.stereoVolNorm = [0, 0];
@@ -28,10 +25,19 @@ class AmplitudeProcessor extends AudioWorkletProcessor {
2825
this.normalize = data.normalize;
2926
} else if (data.name === 'smoothing') {
3027
this.smoothing = Math.max(0, Math.min(1, data.smoothing));
28+
} else if (data.name === 'bufferSize') {
29+
this.setBufferSize(data.bufferSize);
3130
}
3231
};
3332
}
3433

34+
setBufferSize(bufferSize) {
35+
this.bufferSize = bufferSize;
36+
this.inputRingBuffer = new RingBuffer(this.bufferSize, this.numInputChannels);
37+
this.outputRingBuffer = new RingBuffer(this.bufferSize, this.numOutputChannels);
38+
this.inputRingBufferArraySequence = new Array(this.numInputChannels).fill(null).map(() => new Float32Array(this.bufferSize));
39+
}
40+
3541
// TO DO make this stereo / dependent on # of audio channels
3642
process(inputs, outputs) {
3743
const input = inputs[0];

src/audioWorklet/recorderProcessor.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,17 @@ class RecorderProcessor extends AudioWorkletProcessor {
2020
this.record(data.duration);
2121
} else if (data.name === 'stop') {
2222
this.stop();
23+
} else if (data.name === 'bufferSize') {
24+
this.setBufferSize(data.bufferSize);
2325
}
2426
};
2527
}
2628

29+
setBufferSize(bufferSize) {
30+
this.bufferSize = bufferSize;
31+
this.clear();
32+
}
33+
2734
process(inputs) {
2835
if (!this.recording) {
2936
return true;

src/audioWorklet/soundFileProcessor.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,18 @@ class SoundFileProcessor extends AudioWorkletProcessor {
77
super();
88

99
const processorOptions = options.processorOptions || {};
10-
this.bufferSize = processorOptions.bufferSize || 256;
10+
this.setBufferSize(processorOptions.bufferSize || 256);
11+
12+
this.port.onmessage = (event) => {
13+
const data = event.data;
14+
if (data.name === 'bufferSize') {
15+
this.setBufferSize(data.bufferSize);
16+
}
17+
};
18+
}
19+
20+
setBufferSize(bufferSize) {
21+
this.bufferSize = bufferSize;
1122
this.inputRingBuffer = new RingBuffer(this.bufferSize, 1);
1223
this.inputRingBufferArraySequence = [new Float32Array(this.bufferSize)];
1324
}

src/customAudioNode.js

Whitespace-only changes.

src/soundRecorder.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,13 @@ define(function (require) {
8383
this._inputChannels = 2;
8484
this._outputChannels = 2; // stereo output, even if input is mono
8585

86+
const workletBufferSize = 1024;
87+
8688
this._workletNode = new AudioWorkletNode(ac, processorNames.recorderProcessor, {
8789
outputChannelCount: [this._outputChannels],
8890
processorOptions: {
8991
numInputChannels: this._inputChannels,
90-
bufferSize: 1024
92+
bufferSize: workletBufferSize
9193
}
9294
});
9395

@@ -101,6 +103,17 @@ define(function (require) {
101103
}
102104
}.bind(this);
103105

106+
// if the AudioWorkletNode is actually a ScriptProcessorNode created via polyfill,
107+
// make sure that our chosen buffer size isn't smaller than the buffer size automatically
108+
// selected by the polyfill
109+
// reference: https://github.com/GoogleChromeLabs/audioworklet-polyfill/issues/13#issuecomment-425014930
110+
if (this._workletNode instanceof ScriptProcessorNode) {
111+
this._workletNode.port.postMessage({
112+
name: 'bufferSize',
113+
bufferSize: Math.max(workletBufferSize, this._workletNode.bufferSize)
114+
});
115+
}
116+
104117
/**
105118
* callback invoked when the recording is over
106119
* @private

0 commit comments

Comments
 (0)