diff --git a/src/sources/SoundFile.js b/src/sources/SoundFile.js index 183e0e9..6c2fb1c 100644 --- a/src/sources/SoundFile.js +++ b/src/sources/SoundFile.js @@ -40,9 +40,9 @@ import { p5soundSource } from "../core/p5soundSource"; * * */ -function loadSound (path) { +function loadSound(path) { if (typeof path === 'string') path = encodeURI(path); - if(self._incrementPreload && self._decrementPreload){ + if (self._incrementPreload && self._decrementPreload) { self._incrementPreload(); let player = new p5.SoundFile( path, @@ -52,7 +52,7 @@ function loadSound (path) { ); return player; - } else{ + } else { return new Promise((resolve) => { let player = new p5.SoundFile( path, @@ -110,8 +110,18 @@ class SoundFile extends p5soundSource { this.playing = false; this.speed = 1; this.paused = false; + this._playMode = 'sustain'; + this._activePlayers = []; + } + /** + * Set the play mode for the soundfile. + * @method playMode + * @for SoundFile + * @param {String} mode 'sustain', 'restart', or 'untilDone' + */ + playMode(mode) { + this._playMode = mode; } - /** * Start the soundfile. Same as the play() method. * @method start @@ -140,11 +150,7 @@ class SoundFile extends p5soundSource { * */ start() { - this.node.playbackRate = this.speed; - this.playing = true; - if (!this.paused) { - this.node.start(); - } + this.play(); } /** @@ -175,10 +181,34 @@ class SoundFile extends p5soundSource { * */ play() { - this.node.playbackRate = this.speed; + if (this._playMode === 'untilDone' && this.playing) return; + if (this._playMode === 'restart') this.stop(); + this.playing = true; if (!this.paused) { - this.node.start(); + let player = new TonePlayer(this.node.buffer).connect(this.output); + player.playbackRate = this.speed; + player.loop = this.node.loop; + + if (this.node.loopStart !== undefined) player.loopStart = this.node.loopStart; + if (this.node.loopEnd !== undefined) player.loopEnd = this.node.loopEnd; + + player.onstop = () => { + const idx = this._activePlayers.indexOf(player); + if (idx !== -1) this._activePlayers.splice(idx, 1); + player.dispose(); + + if (this._activePlayers.length === 0) { + this.playing = false; + if (this.node.onstop) this.node.onstop(); + } + }; + + this._activePlayers.push(player); + player.start(); + } else { + this.paused = false; + this._activePlayers.forEach(p => p.playbackRate = this.speed); } } @@ -221,8 +251,11 @@ class SoundFile extends p5soundSource { * */ stop() { - this.node.stop(); + this._activePlayers.forEach(p => { + if (p.state === 'started') p.stop(); + }); this.playing = false; + this.paused = false; } /** @@ -264,7 +297,7 @@ class SoundFile extends p5soundSource { */ pause() { //no such pause method in Tone.js need to find workaround - this.node.playbackRate = 0; + this._activePlayers.forEach(p => p.playbackRate = 0); this.playing = false; this.paused = true; } @@ -320,6 +353,7 @@ class SoundFile extends p5soundSource { */ loop(value = true) { this.node.loop = value; + this._activePlayers.forEach(p => p.loop = value); } /** @@ -365,8 +399,12 @@ class SoundFile extends p5soundSource { loopPoints(startTime = 0, duration = this.node.buffer.duration) { this.node.loopStart = startTime; this.node.loopEnd = startTime + duration; + this._activePlayers.forEach(p => { + p.loopStart = startTime; + p.loopEnd = startTime + duration; + }); } - + /** * Change the path for the soundfile. * @method setPath @@ -465,6 +503,7 @@ class SoundFile extends p5soundSource { } this.node.playbackRate = value; this.speed = value; + this._activePlayers.forEach(p => p.playbackRate = value); } /** @@ -564,7 +603,7 @@ class SoundFile extends p5soundSource { * */ jump(value) { - this.node.seek(value); + this._activePlayers.forEach(p => p.seek(value)); } /** @@ -678,7 +717,7 @@ class SoundFile extends p5soundSource { onended(callback) { this.node.onstop = callback; } - + /** * Return the number of samples in a sound file. * @method frames