scratch-audio/src/SoundPlayer.js

38 lines
913 B
JavaScript
Raw Normal View History

2016-11-21 12:40:35 -05:00
var Tone = require('tone');
function SoundPlayer (outputNode) {
this.outputNode = outputNode;
this.buffer; // a Tone.Buffer
this.bufferSource;
this.playbackRate = 1;
}
SoundPlayer.prototype.setBuffer = function (buffer) {
this.buffer = buffer;
};
SoundPlayer.prototype.setPlaybackRate = function (playbackRate) {
this.playbackRate = playbackRate;
};
SoundPlayer.prototype.stop = function () {
if (this.bufferSource){
this.bufferSource.stop();
}
};
SoundPlayer.prototype.start = function () {
this.stop();
this.bufferSource = new Tone.BufferSource(this.buffer.get());
this.bufferSource.playbackRate.value = this.playbackRate;
this.bufferSource.connect(this.outputNode);
this.bufferSource.start();
};
SoundPlayer.prototype.onEnded = function (callback) {
this.bufferSource.onended = function () {callback();};
};
module.exports = SoundPlayer;