Skip to content

Commit f0b6713

Browse files
committed
Fix p5.SoundFile pause functionality on Firefox and Safari by moving pause position logic out of AudioWorklet processor
1 parent 0b33ccf commit f0b6713

File tree

6 files changed

+25
-42
lines changed

6 files changed

+25
-42
lines changed

lib/p5.sound.js

Lines changed: 13 additions & 14 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/audioWorklet/soundFileProcessor.js

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,12 @@
22
const processorNames = preval.require('./processorNames');
33

44
class SoundFileProcessor extends AudioWorkletProcessor {
5-
constructor() {
6-
super();
7-
8-
this.paused = false;
9-
10-
this.port.onmessage = (event) => {
11-
const data = event.data;
12-
if (data.name === 'play') {
13-
this.paused = false;
14-
} else if (data.name === 'pause') {
15-
this.paused = true;
16-
}
17-
};
18-
}
19-
205
process(inputs) {
216
const input = inputs[0];
227
const inputChannel = input[0];
238
const position = inputChannel[inputChannel.length - 1] || 0;
249

25-
if (!this.paused) {
26-
this.port.postMessage({ name: 'position', position: position });
27-
}
10+
this.port.postMessage({ name: 'position', position: position });
2811

2912
return true;
3013
}

src/soundfile.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,6 @@ define(function (require) {
369369

370370
// TO DO: if already playing, create array of buffers for easy stop()
371371
if (this.buffer) {
372-
373372
// reset the pause time (if it was paused)
374373
this._pauseTime = 0;
375374

@@ -439,7 +438,6 @@ define(function (require) {
439438
this._counterNode.loopEnd = cueEnd;
440439
}
441440

442-
this._workletNode.port.postMessage({ name: 'play' });
443441
};
444442

445443

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

541539
if (this.isPlaying() && this.buffer && this.bufferSourceNode) {
542-
this._workletNode.port.postMessage({ name: 'pause' });
540+
this._paused = true;
541+
this._playing = false;
543542

544543
this.pauseTime = this.currentTime();
545544
this.bufferSourceNode.stop(pTime);
546545
this._counterNode.stop(pTime);
547-
this._paused = true;
548-
this._playing = false;
549546

550547
this._pauseTime = this.currentTime();
551548
// TO DO: make sure play() still starts from orig start position
@@ -1247,14 +1244,18 @@ define(function (require) {
12471244
delete self._workletNode;
12481245
}
12491246
self._workletNode = new AudioWorkletNode(ac, processorNames.soundFileProcessor);
1250-
self._workletNode.port.onmessage = function(event) {
1247+
self._workletNode.port.onmessage = event => {
12511248
if (event.data.name === 'position') {
1249+
// event.data.position should only be 0 when paused
1250+
if (event.data.position === 0) {
1251+
return;
1252+
}
12521253
this._lastPos = event.data.position;
12531254

12541255
// do any callbacks that have been scheduled
12551256
this._onTimeUpdate(self._lastPos);
12561257
}
1257-
}.bind(self);
1258+
};
12581259

12591260
// create counter buffer of the same length as self.buffer
12601261
cNode.buffer = _createCounterBuffer( self.buffer );

0 commit comments

Comments
 (0)