mirror of
https://github.com/scratchfoundation/scratch-audio.git
synced 2024-12-22 14:02:29 -05:00
add isplaying property, cleanup
This commit is contained in:
parent
897fd68c90
commit
b5d513f2b2
1 changed files with 16 additions and 2 deletions
|
@ -1,10 +1,12 @@
|
|||
var Tone = require('tone');
|
||||
var log = require('./log');
|
||||
|
||||
function SoundPlayer (outputNode) {
|
||||
this.outputNode = outputNode;
|
||||
this.buffer; // a Tone.Buffer
|
||||
this.bufferSource;
|
||||
this.playbackRate = 1;
|
||||
this.isPlaying = false;
|
||||
}
|
||||
|
||||
SoundPlayer.prototype.setBuffer = function (buffer) {
|
||||
|
@ -13,25 +15,37 @@ SoundPlayer.prototype.setBuffer = function (buffer) {
|
|||
|
||||
SoundPlayer.prototype.setPlaybackRate = function (playbackRate) {
|
||||
this.playbackRate = playbackRate;
|
||||
if (this.bufferSource && this.bufferSource.playbackRate) {
|
||||
this.bufferSource.playbackRate.value = this.playbackRate;
|
||||
}
|
||||
};
|
||||
|
||||
SoundPlayer.prototype.stop = function () {
|
||||
if (this.bufferSource){
|
||||
if (this.isPlaying){
|
||||
this.bufferSource.stop();
|
||||
}
|
||||
};
|
||||
|
||||
SoundPlayer.prototype.start = function () {
|
||||
if (!this.buffer || !this.buffer.loaded) {
|
||||
log.warn('tried to play a sound that was not loaded yet');
|
||||
return;
|
||||
}
|
||||
|
||||
this.stop();
|
||||
|
||||
this.bufferSource = new Tone.BufferSource(this.buffer.get());
|
||||
this.bufferSource.playbackRate.value = this.playbackRate;
|
||||
this.bufferSource.connect(this.outputNode);
|
||||
this.bufferSource.start();
|
||||
this.isPlaying = true;
|
||||
};
|
||||
|
||||
SoundPlayer.prototype.onEnded = function (callback) {
|
||||
this.bufferSource.onended = function () {callback();};
|
||||
this.bufferSource.onended = function () {
|
||||
this.isPlaying = false;
|
||||
callback();
|
||||
};
|
||||
};
|
||||
|
||||
module.exports = SoundPlayer;
|
||||
|
|
Loading…
Reference in a new issue