Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 55 additions & 16 deletions src/sources/SoundFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ import { p5soundSource } from "../core/p5soundSource";
* </code>
* </div>
*/
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,
Expand All @@ -52,7 +52,7 @@ function loadSound (path) {
);
return player;

} else{
} else {
return new Promise((resolve) => {
let player = new p5.SoundFile(
path,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -140,11 +150,7 @@ class SoundFile extends p5soundSource {
* </div>
*/
start() {
this.node.playbackRate = this.speed;
this.playing = true;
if (!this.paused) {
this.node.start();
}
this.play();
}

/**
Expand Down Expand Up @@ -175,10 +181,34 @@ class SoundFile extends p5soundSource {
* </div>
*/
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);
}
}

Expand Down Expand Up @@ -221,8 +251,11 @@ class SoundFile extends p5soundSource {
* </div>
*/
stop() {
this.node.stop();
this._activePlayers.forEach(p => {
if (p.state === 'started') p.stop();
});
this.playing = false;
this.paused = false;
}

/**
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -320,6 +353,7 @@ class SoundFile extends p5soundSource {
*/
loop(value = true) {
this.node.loop = value;
this._activePlayers.forEach(p => p.loop = value);
}

/**
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -465,6 +503,7 @@ class SoundFile extends p5soundSource {
}
this.node.playbackRate = value;
this.speed = value;
this._activePlayers.forEach(p => p.playbackRate = value);
}

/**
Expand Down Expand Up @@ -564,7 +603,7 @@ class SoundFile extends p5soundSource {
* </div>
*/
jump(value) {
this.node.seek(value);
this._activePlayers.forEach(p => p.seek(value));
}

/**
Expand Down Expand Up @@ -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
Expand Down