Keep bufferSource refs so onended fires on safari

This commit is contained in:
Eric Rosenbaum committed 2018-01-22 11:57:06 -05:00
1 parent ad4b31263c
commit 073ee84a91
1 file changed
+19
+19
View File
@@ -60,6 +60,15 @@ class Scratch3MusicBlocks {
*/
this._instrumentBufferArrays = [];
/**
* An array of audio bufferSourceNodes. Each time you play an instrument or drum sound,
* a bufferSourceNode is created. We keep references to them to make sure their onended
* events can fire.
* @type {Array}
* @private
*/
this._bufferSources = [];
this._loadAllSounds();
}
@@ -555,9 +564,14 @@ class Scratch3MusicBlocks {
bufferSource.buffer = this._drumBuffers[drumNum];
bufferSource.connect(outputNode);
bufferSource.start();
const bufferSourceIndex = this._bufferSources.length;
this._bufferSources.push(bufferSource);
this._concurrencyCounter++;
bufferSource.onended = () => {
this._concurrencyCounter--;
delete this._bufferSources[bufferSourceIndex];
};
}
@@ -638,6 +652,10 @@ class Scratch3MusicBlocks {
// Create the audio buffer to play the note, and set its pitch
const context = util.runtime.audioEngine.audioContext;
const bufferSource = context.createBufferSource();
const bufferSourceIndex = this._bufferSources.length;
this._bufferSources.push(bufferSource);
bufferSource.buffer = this._instrumentBufferArrays[inst][sampleIndex];
const sampleNote = sampleArray[sampleIndex];
bufferSource.playbackRate.value = this._ratioForPitchInterval(note - sampleNote);
@@ -667,6 +685,7 @@ class Scratch3MusicBlocks {
this._concurrencyCounter++;
bufferSource.onended = () => {
this._concurrencyCounter--;
delete this._bufferSources[bufferSourceIndex];
};
}