Skip to content

Commit 2014fbc

Browse files
committed
extract buffer size calculation into shared method
1 parent d423aec commit 2014fbc

11 files changed

+98
-144
lines changed

lib/p5.sound.js

Lines changed: 56 additions & 64 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: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
'use strict';
22

33
define(function (require) {
4-
var p5sound = require('master');
5-
var processorNames = require('./audioWorklet/processorNames');
4+
const p5sound = require('master');
5+
const { safeBufferSize } = require('helpers');
6+
const processorNames = require('./audioWorklet/processorNames');
67

78
/**
89
* Amplitude measures volume between 0.0 and 1.0.
@@ -47,7 +48,7 @@ define(function (require) {
4748
p5.Amplitude = function(smoothing) {
4849

4950
// Set to 2048 for now. In future iterations, this should be inherited or parsed from p5sound's default
50-
this.bufferSize = 2048;
51+
this.bufferSize = safeBufferSize(2048);
5152

5253
// set audio context
5354
this.audiocontext = p5sound.audiocontext;
@@ -72,17 +73,6 @@ define(function (require) {
7273
}
7374
}.bind(this);
7475

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-
8676
// for connections
8777
this.input = this._workletNode;
8878

src/audioWorklet/amplitudeProcessor.js

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@ class AmplitudeProcessor extends AudioWorkletProcessor {
1212
this.normalize = processorOptions.normalize || false;
1313
this.smoothing = processorOptions.smoothing || 0;
1414

15-
this.setBufferSize(processorOptions.bufferSize || 2048);
15+
this.bufferSize = processorOptions.bufferSize || 2048;
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));
1619

1720
this.stereoVol = [0, 0];
1821
this.stereoVolNorm = [0, 0];
@@ -25,19 +28,10 @@ class AmplitudeProcessor extends AudioWorkletProcessor {
2528
this.normalize = data.normalize;
2629
} else if (data.name === 'smoothing') {
2730
this.smoothing = Math.max(0, Math.min(1, data.smoothing));
28-
} else if (data.name === 'bufferSize') {
29-
this.setBufferSize(data.bufferSize);
3031
}
3132
};
3233
}
3334

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-
4135
// TO DO make this stereo / dependent on # of audio channels
4236
process(inputs, outputs) {
4337
const input = inputs[0];

src/audioWorklet/recorderProcessor.js

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,10 @@ 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);
2523
}
2624
};
2725
}
2826

29-
setBufferSize(bufferSize) {
30-
this.bufferSize = bufferSize;
31-
this.clear();
32-
}
33-
3427
process(inputs) {
3528
if (!this.recording) {
3629
return true;

src/audioWorklet/soundFileProcessor.js

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

99
const processorOptions = options.processorOptions || {};
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;
10+
this.bufferSize = processorOptions.bufferSize || 256;
2211
this.inputRingBuffer = new RingBuffer(this.bufferSize, 1);
2312
this.inputRingBufferArraySequence = [new Float32Array(this.bufferSize)];
2413
}

src/helpers.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22
define(function (require) {
33
var p5sound = require('master');
4+
var processorNames = require('./audioWorklet/processorNames');
45
/**
56
* @for p5
67
*/
@@ -301,10 +302,28 @@ define(function (require) {
301302
}
302303
}
303304

305+
function safeBufferSize(idealBufferSize) {
306+
let bufferSize = idealBufferSize;
307+
308+
// if the AudioWorkletNode is actually a ScriptProcessorNode created via polyfill,
309+
// make sure that our chosen buffer size isn't smaller than the buffer size automatically
310+
// selected by the polyfill
311+
// reference: https://github.com/GoogleChromeLabs/audioworklet-polyfill/issues/13#issuecomment-425014930
312+
let tempAudioWorkletNode = new AudioWorkletNode(p5sound.audiocontext, processorNames.soundFileProcessor);
313+
if (tempAudioWorkletNode instanceof ScriptProcessorNode) {
314+
bufferSize = tempAudioWorkletNode.bufferSize;
315+
}
316+
tempAudioWorkletNode.disconnect();
317+
tempAudioWorkletNode = null;
318+
319+
return bufferSize;
320+
}
321+
304322
return {
305323
convertToWav: convertToWav,
306324
midiToFreq: midiToFreq,
307-
noteToFreq: noteToFreq
325+
noteToFreq: noteToFreq,
326+
safeBufferSize: safeBufferSize
308327
};
309328

310329
});

src/soundRecorder.js

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ define(function (require) {
44

55
// inspiration: recorder.js, Tone.js & typedarray.org
66

7-
var p5sound = require('master');
8-
var convertToWav = require('helpers').convertToWav;
9-
var processorNames = require('./audioWorklet/processorNames');
10-
var ac = p5sound.audiocontext;
7+
const p5sound = require('master');
8+
const { convertToWav, safeBufferSize } = require('helpers');
9+
const processorNames = require('./audioWorklet/processorNames');
10+
const ac = p5sound.audiocontext;
1111

1212
/**
1313
* <p>Record sounds for playback and/or to save as a .wav file.
@@ -83,7 +83,7 @@ define(function (require) {
8383
this._inputChannels = 2;
8484
this._outputChannels = 2; // stereo output, even if input is mono
8585

86-
const workletBufferSize = 1024;
86+
const workletBufferSize = safeBufferSize(1024);
8787

8888
this._workletNode = new AudioWorkletNode(ac, processorNames.recorderProcessor, {
8989
outputChannelCount: [this._outputChannels],
@@ -103,17 +103,6 @@ define(function (require) {
103103
}
104104
}.bind(this);
105105

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-
117106
/**
118107
* callback invoked when the recording is over
119108
* @private

0 commit comments

Comments
 (0)