scratch-audio/src/SoundPlayer.js

89 lines
2.4 KiB
JavaScript
Raw Normal View History

2017-04-17 11:22:16 -04:00
const Tone = require('tone');
const log = require('./log');
2016-11-21 12:40:35 -05:00
2017-02-02 14:53:17 -05:00
/**
* A SoundPlayer stores an audio buffer, and plays it
*/
2017-04-17 12:55:09 -04:00
class SoundPlayer {
constructor () {
this.outputNode = null;
this.buffer = new Tone.Buffer();
this.bufferSource = null;
this.playbackRate = 1;
this.isPlaying = false;
}
2016-11-21 12:40:35 -05:00
2017-04-17 12:55:09 -04:00
/**
* Connect the SoundPlayer to an output node
* @param {Tone.Gain} node - an output node to connect to
*/
connect (node) {
this.outputNode = node;
}
2016-11-21 12:40:35 -05:00
2017-04-17 12:55:09 -04:00
/**
* Set an audio buffer
* @param {Tone.Buffer} buffer Buffer to set
*/
setBuffer (buffer) {
this.buffer = buffer;
2016-11-21 15:57:34 -05:00
}
2016-11-21 12:40:35 -05:00
2017-04-17 12:55:09 -04:00
/**
* Set the playback rate for the sound
* @param {number} playbackRate - a ratio where 1 is normal playback, 0.5 is half speed, 2 is double speed, etc.
*/
setPlaybackRate (playbackRate) {
this.playbackRate = playbackRate;
if (this.bufferSource && this.bufferSource.playbackRate) {
this.bufferSource.playbackRate.value = this.playbackRate;
}
2016-11-21 12:40:35 -05:00
}
2017-04-17 12:55:09 -04:00
/**
* Stop the sound
*/
stop () {
if (this.bufferSource) {
this.bufferSource.stop();
}
this.isPlaying = false;
2016-11-21 15:57:34 -05:00
}
2017-04-17 12:55:09 -04:00
/**
* Start playing the sound
* The web audio framework requires a new audio buffer source node for each playback
*/
start () {
if (!this.buffer || !this.buffer.loaded) {
log.warn('tried to play a sound that was not loaded yet');
return;
}
this.bufferSource = Tone.context.createBufferSource();
this.bufferSource.buffer = this.buffer.get();
2017-04-17 12:55:09 -04:00
this.bufferSource.playbackRate.value = this.playbackRate;
this.bufferSource.connect(this.outputNode);
this.bufferSource.start();
2017-01-30 18:13:18 -05:00
2017-04-17 12:55:09 -04:00
this.isPlaying = true;
}
2016-11-21 12:40:35 -05:00
2017-04-17 12:55:09 -04:00
/**
* The sound has finished playing. This is called at the correct time even if the playback rate
* has been changed
* @return {Promise} a Promise that resolves when the sound finishes playing
*/
finished () {
const storedContext = this;
return new Promise(resolve => {
storedContext.bufferSource.onended = function () {
this.isPlaying = false;
resolve();
}.bind(storedContext);
});
}
}
2016-11-21 12:40:35 -05:00
module.exports = SoundPlayer;