Skip to content

Commit 4d3a383

Browse files
authored
Merge pull request #380 from oshoham/amplitude-getlevel-bugfix
Bugfixes for p5.Amplitude and p5.Soundfile for browsers without AudioWorklet support
2 parents 0dacdc7 + b32e460 commit 4d3a383

File tree

8 files changed

+148
-152
lines changed

8 files changed

+148
-152
lines changed

examples/pause_soundfile/sketch.js

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
// ====================
22
// DEMO: pause sound when the user presses a key, resume on release
33
// ====================
4+
'use strict';
45

56
var soundFile;
7+
var audioContextStarted = false;
68

79
function preload() {
810
// create a SoundFile
@@ -14,16 +16,26 @@ function setup() {
1416
createCanvas(400, 400);
1517
background(0, 255, 0);
1618

17-
soundFile.loop();
19+
userStartAudio().then(function() {
20+
soundFile.loop();
21+
audioContextStarted = true;
22+
});
23+
1824
createP('Press any key to pause. Resume when the key is released')
1925
}
2026

2127
function keyTyped() {
22-
soundFile.pause();
23-
background(255, 0, 0);
28+
if (!audioContextStarted) {
29+
return;
30+
}
31+
soundFile.pause();
32+
background(255, 0, 0);
2433
}
2534

2635
function keyReleased() {
27-
soundFile.play();
28-
background(0, 255, 0);
36+
if (!audioContextStarted) {
37+
return;
38+
}
39+
soundFile.play();
40+
background(0, 255, 0);
2941
}

lib/p5.sound.js

Lines changed: 108 additions & 110 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: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,10 @@ define(function (require) {
5353
this.audiocontext = p5sound.audiocontext;
5454
this._workletNode = new AudioWorkletNode(this.audiocontext, processorNames.amplitudeProcessor, {
5555
outputChannelCount: [1],
56-
parameterData: { smoothing: smoothing || 0 },
57-
processorOptions: { normalize: false }
56+
processorOptions: {
57+
normalize: false,
58+
smoothing: smoothing || 0
59+
}
5860
});
5961

6062
this._workletNode.port.onmessage = function(event) {
@@ -256,7 +258,7 @@ define(function (require) {
256258
*/
257259
p5.Amplitude.prototype.smooth = function(s) {
258260
if (s >= 0 && s < 1) {
259-
this._workletNode.parameters.get('smoothing').value = s;
261+
this._workletNode.port.postMessage({ name: 'smoothing', smoothing: s });
260262
} else {
261263
console.log('Error: smoothing must be between 0 and 1');
262264
}

src/audioWorklet/amplitudeProcessor.js

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,11 @@
22
const processorNames = preval.require('./processorNames');
33

44
class AmplitudeProcessor extends AudioWorkletProcessor {
5-
static get parameterDescriptors() {
6-
return [
7-
{
8-
name: 'smoothing',
9-
defaultValue: 0,
10-
minValue: 0,
11-
maxValue: 1,
12-
automationRate: 'k-rate'
13-
}
14-
];
15-
}
16-
175
constructor(options) {
186
super();
197

208
const processorOptions = options.processorOptions || {};
9+
this.smoothing = processorOptions.smoothing || 0;
2110
this.normalize = processorOptions.normalize || false;
2211

2312
this.stereoVol = [0, 0];
@@ -29,15 +18,17 @@ class AmplitudeProcessor extends AudioWorkletProcessor {
2918
const data = event.data;
3019
if (data.name === 'toggleNormalize') {
3120
this.normalize = data.normalize;
21+
} else if (data.name === 'smoothing') {
22+
this.smoothing = Math.max(0, Math.min(1, data.smoothing));
3223
}
3324
};
3425
}
3526

3627
// TO DO make this stereo / dependent on # of audio channels
37-
process(inputs, outputs, parameters) {
28+
process(inputs, outputs) {
3829
const input = inputs[0];
3930
const output = outputs[0];
40-
const smoothing = parameters.smoothing;
31+
const smoothing = this.smoothing;
4132

4233
for (let channel = 0; channel < input.length; ++channel) {
4334
const inputBuffer = input[channel];

src/soundfile.js

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,6 @@ define(function (require) {
140140
this._whileLoading = function() {};
141141
}
142142

143-
this._onAudioProcess = _onAudioProcess.bind(this);
144143
this._clearOnEnd = _clearOnEnd.bind(this);
145144
};
146145

@@ -351,7 +350,6 @@ define(function (require) {
351350
return;
352351
}
353352

354-
var self = this;
355353
var now = p5sound.audiocontext.currentTime;
356354
var cueStart, cueEnd;
357355
var time = startTime || 0;
@@ -371,7 +369,6 @@ define(function (require) {
371369

372370
// TO DO: if already playing, create array of buffers for easy stop()
373371
if (this.buffer) {
374-
375372
// reset the pause time (if it was paused)
376373
this._pauseTime = 0;
377374

@@ -440,6 +437,7 @@ define(function (require) {
440437
this._counterNode.loopStart = cueStart;
441438
this._counterNode.loopEnd = cueEnd;
442439
}
440+
443441
};
444442

445443

@@ -539,11 +537,12 @@ define(function (require) {
539537
var pTime = time + now;
540538

541539
if (this.isPlaying() && this.buffer && this.bufferSourceNode) {
540+
this._paused = true;
541+
this._playing = false;
542+
542543
this.pauseTime = this.currentTime();
543544
this.bufferSourceNode.stop(pTime);
544545
this._counterNode.stop(pTime);
545-
this._paused = true;
546-
this._playing = false;
547546

548547
this._pauseTime = this.currentTime();
549548
// TO DO: make sure play() still starts from orig start position
@@ -1245,14 +1244,18 @@ define(function (require) {
12451244
delete self._workletNode;
12461245
}
12471246
self._workletNode = new AudioWorkletNode(ac, processorNames.soundFileProcessor);
1248-
self._workletNode.port.onmessage = function(event) {
1247+
self._workletNode.port.onmessage = event => {
12491248
if (event.data.name === 'position') {
1249+
// event.data.position should only be 0 when paused
1250+
if (event.data.position === 0) {
1251+
return;
1252+
}
12501253
this._lastPos = event.data.position;
12511254

12521255
// do any callbacks that have been scheduled
12531256
this._onTimeUpdate(self._lastPos);
12541257
}
1255-
}.bind(self);
1258+
};
12561259

12571260
// create counter buffer of the same length as self.buffer
12581261
cNode.buffer = _createCounterBuffer( self.buffer );
@@ -1753,16 +1756,6 @@ define(function (require) {
17531756
return new Blob([dataView], { type: 'audio/wav' });
17541757
};
17551758

1756-
// event handler to keep track of current position
1757-
function _onAudioProcess(processEvent) {
1758-
var inputBuffer = processEvent.inputBuffer.getChannelData(0);
1759-
1760-
this._lastPos = inputBuffer[inputBuffer.length - 1] || 0;
1761-
1762-
// do any callbacks that have been scheduled
1763-
this._onTimeUpdate(self._lastPos);
1764-
}
1765-
17661759
// event handler to remove references to the bufferSourceNode when it is done playing
17671760
function _clearOnEnd(e) {
17681761
const thisBufferSourceNode = e.target;

0 commit comments

Comments
 (0)