Skip to content

Commit

Permalink
Fix p5.SoundFile pause functionality on Firefox and Safari by moving …
Browse files Browse the repository at this point in the history
…pause position logic out of AudioWorklet processor
  • Loading branch information
oshoham committed Aug 16, 2019
1 parent 0b33ccf commit ce48b1b
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 42 deletions.
28 changes: 14 additions & 14 deletions lib/p5.sound.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/p5.sound.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion lib/p5.sound.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion lib/p5.sound.min.js.map

Large diffs are not rendered by default.

19 changes: 1 addition & 18 deletions src/audioWorklet/soundFileProcessor.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,12 @@
const processorNames = preval.require('./processorNames');

class SoundFileProcessor extends AudioWorkletProcessor {
constructor() {
super();

this.paused = false;

this.port.onmessage = (event) => {
const data = event.data;
if (data.name === 'play') {
this.paused = false;
} else if (data.name === 'pause') {
this.paused = true;
}
};
}

process(inputs) {
const input = inputs[0];
const inputChannel = input[0];
const position = inputChannel[inputChannel.length - 1] || 0;

if (!this.paused) {
this.port.postMessage({ name: 'position', position: position });
}
this.port.postMessage({ name: 'position', position: position });

return true;
}
Expand Down
16 changes: 9 additions & 7 deletions src/soundfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,6 @@ define(function (require) {

// TO DO: if already playing, create array of buffers for easy stop()
if (this.buffer) {

// reset the pause time (if it was paused)
this._pauseTime = 0;

Expand Down Expand Up @@ -439,7 +438,6 @@ define(function (require) {
this._counterNode.loopEnd = cueEnd;
}

this._workletNode.port.postMessage({ name: 'play' });
};


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

if (this.isPlaying() && this.buffer && this.bufferSourceNode) {
this._workletNode.port.postMessage({ name: 'pause' });
this._paused = true;
this._playing = false;

this.pauseTime = this.currentTime();
this.bufferSourceNode.stop(pTime);
this._counterNode.stop(pTime);
this._paused = true;
this._playing = false;

this._pauseTime = this.currentTime();
// TO DO: make sure play() still starts from orig start position
Expand Down Expand Up @@ -1247,14 +1244,19 @@ define(function (require) {
delete self._workletNode;
}
self._workletNode = new AudioWorkletNode(ac, processorNames.soundFileProcessor);
self._workletNode.port.onmessage = function(event) {
self._workletNode.port.onmessage = event => {
if (event.data.name === 'position') {
// event.data.position should only be 0 when paused, but may sometimes be 0
// slightly before this._paused is set to true
if (this._paused || event.data.position === 0) {
return;
}
this._lastPos = event.data.position;

// do any callbacks that have been scheduled
this._onTimeUpdate(self._lastPos);
}
}.bind(self);
};

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

0 comments on commit ce48b1b

Please sign in to comment.