Skip to content

Commit 0b33ccf

Browse files
committed
Don't update soundFile position when paused
1 parent cc4abf7 commit 0b33ccf

File tree

6 files changed

+130
-122
lines changed

6 files changed

+130
-122
lines changed

lib/p5.sound.js

Lines changed: 105 additions & 106 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: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,29 @@
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+
520
process(inputs) {
621
const input = inputs[0];
722
const inputChannel = input[0];
823
const position = inputChannel[inputChannel.length - 1] || 0;
924

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

1229
return true;
1330
}

src/soundfile.js

Lines changed: 4 additions & 12 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;
@@ -440,6 +438,8 @@ define(function (require) {
440438
this._counterNode.loopStart = cueStart;
441439
this._counterNode.loopEnd = cueEnd;
442440
}
441+
442+
this._workletNode.port.postMessage({ name: 'play' });
443443
};
444444

445445

@@ -539,6 +539,8 @@ define(function (require) {
539539
var pTime = time + now;
540540

541541
if (this.isPlaying() && this.buffer && this.bufferSourceNode) {
542+
this._workletNode.port.postMessage({ name: 'pause' });
543+
542544
this.pauseTime = this.currentTime();
543545
this.bufferSourceNode.stop(pTime);
544546
this._counterNode.stop(pTime);
@@ -1753,16 +1755,6 @@ define(function (require) {
17531755
return new Blob([dataView], { type: 'audio/wav' });
17541756
};
17551757

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-
17661758
// event handler to remove references to the bufferSourceNode when it is done playing
17671759
function _clearOnEnd(e) {
17681760
const thisBufferSourceNode = e.target;

0 commit comments

Comments
 (0)