mirror of
https://github.com/scratchfoundation/scratch-audio.git
synced 2025-01-03 11:35:49 -05:00
use soundplayer object
This commit is contained in:
parent
7aca02b89a
commit
ec558d9821
1 changed files with 11 additions and 20 deletions
31
src/index.js
31
src/index.js
|
@ -113,16 +113,12 @@ AudioEngine.prototype.loadSounds = function (sounds) {
|
||||||
};
|
};
|
||||||
|
|
||||||
AudioEngine.prototype.playSound = function (index) {
|
AudioEngine.prototype.playSound = function (index) {
|
||||||
// if the soundplayer exists and its buffer has loaded
|
this.soundPlayers[index].start();
|
||||||
if (this.soundPlayers[index].buffer && this.soundPlayers[index].buffer.loaded) {
|
|
||||||
// play the sound
|
|
||||||
this.soundPlayers[index].start();
|
|
||||||
|
|
||||||
var storedContext = this;
|
var storedContext = this;
|
||||||
return new Promise(function (resolve) {
|
return new Promise(function (resolve) {
|
||||||
storedContext.soundPlayers[index].onEnded(resolve);
|
storedContext.soundPlayers[index].onEnded(resolve);
|
||||||
});
|
});
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
AudioEngine.prototype.playNoteForBeats = function (note, beats) {
|
AudioEngine.prototype.playNoteForBeats = function (note, beats) {
|
||||||
|
@ -187,15 +183,12 @@ AudioEngine.prototype.stopAllSounds = function () {
|
||||||
// for (var i = 0; i<this.drumSamplers.length; i++) {
|
// for (var i = 0; i<this.drumSamplers.length; i++) {
|
||||||
// this.drumSamplers[i].triggerRelease();
|
// this.drumSamplers[i].triggerRelease();
|
||||||
// }
|
// }
|
||||||
|
|
||||||
// stop sounds triggered with playSound
|
// stop sounds triggered with playSound
|
||||||
if (this.soundPlayers && this.soundPlayers.length > 0) {
|
for (var i=0; i<this.soundPlayers.length; i++) {
|
||||||
for (var i=0; i<this.soundPlayers.length; i++) {
|
this.soundPlayers[i].stop();
|
||||||
var bufferSource = this.soundPlayers[i].bufferSource;
|
|
||||||
if (bufferSource) {
|
|
||||||
bufferSource.stop();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// stop soundfont notes
|
// stop soundfont notes
|
||||||
if (this.instrument) {
|
if (this.instrument) {
|
||||||
this.instrument.stop();
|
this.instrument.stop();
|
||||||
|
@ -277,6 +270,7 @@ AudioEngine.prototype._setPitchShift = function (value) {
|
||||||
if (!this.soundPlayers) {
|
if (!this.soundPlayers) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
var ratio = this._getPitchRatio();
|
var ratio = this._getPitchRatio();
|
||||||
this._setPlaybackRateForAllSoundPlayers(ratio);
|
this._setPlaybackRateForAllSoundPlayers(ratio);
|
||||||
|
|
||||||
|
@ -284,10 +278,7 @@ AudioEngine.prototype._setPitchShift = function (value) {
|
||||||
|
|
||||||
AudioEngine.prototype._setPlaybackRateForAllSoundPlayers = function (rate) {
|
AudioEngine.prototype._setPlaybackRateForAllSoundPlayers = function (rate) {
|
||||||
for (var i=0; i<this.soundPlayers.length; i++) {
|
for (var i=0; i<this.soundPlayers.length; i++) {
|
||||||
var s = this.soundPlayers[i].bufferSource;
|
this.soundPlayers[i].setPlaybackRate(rate);
|
||||||
if (s && s.playbackRate) {
|
|
||||||
s.playbackRate.value = rate;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue