scratch-audio/src/SoundPlayer.js

92 lines
2.4 KiB
JavaScript
Raw Normal View History

2017-04-17 11:22:16 -04:00
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 {
/**
* @param {AudioContext} audioContext - a webAudio context
* @constructor
*/
constructor (audioContext) {
this.audioContext = audioContext;
2017-04-17 12:55:09 -04:00
this.outputNode = null;
2017-06-20 16:50:02 -04:00
this.buffer = null;
2017-04-17 12:55:09 -04:00
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
2017-06-20 16:50:02 -04:00
* @param {GainNode} node - an output node to connect to
2017-04-17 12:55:09 -04:00
*/
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
2017-06-20 16:50:02 -04:00
* @param {AudioBuffer} buffer - Buffer to set
2017-04-17 12:55:09 -04:00
*/
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 () {
2017-08-22 11:33:18 -04:00
if (this.bufferSource && this.isPlaying) {
2017-04-17 12:55:09 -04:00
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 () {
2017-06-20 16:50:02 -04:00
if (!this.buffer) {
2017-04-17 12:55:09 -04:00
log.warn('tried to play a sound that was not loaded yet');
return;
}
this.bufferSource = this.audioContext.createBufferSource();
2017-06-20 16:50:02 -04:00
this.bufferSource.buffer = this.buffer;
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 () {
return new Promise(resolve => {
2017-06-21 11:23:32 -04:00
this.bufferSource.onended = () => {
2017-04-17 12:55:09 -04:00
this.isPlaying = false;
resolve();
2017-06-21 11:23:32 -04:00
};
2017-04-17 12:55:09 -04:00
});
}
}
2016-11-21 12:40:35 -05:00
module.exports = SoundPlayer;