From c268bbae47ce5e86c47894b2d813645820793590 Mon Sep 17 00:00:00 2001 From: "Michael \"Z\" Goddard" Date: Thu, 21 Jun 2018 17:23:33 -0400 Subject: [PATCH] add decoded SoundPlayer's to a Sprite's SoundBank (#1260) --- src/import/load-sound.js | 20 +++++++++++++------- src/serialization/sb3.js | 2 +- src/virtual-machine.js | 6 +++--- 3 files changed, 17 insertions(+), 11 deletions(-) diff --git a/src/import/load-sound.js b/src/import/load-sound.js index e13db14d25..8f4f7a1a66 100644 --- a/src/import/load-sound.js +++ b/src/import/load-sound.js @@ -8,27 +8,32 @@ const log = require('../util/log'); * @property {Buffer} data - sound data will be written here once loaded. * @param {!Asset} soundAsset - the asset loaded from storage. * @param {!Runtime} runtime - Scratch runtime, used to access the storage module. + * @param {Sprite} sprite - Scratch sprite to add sounds to. * @returns {!Promise} - a promise which will resolve to the sound when ready. */ -const loadSoundFromAsset = function (sound, soundAsset, runtime) { +const loadSoundFromAsset = function (sound, soundAsset, runtime, sprite) { sound.assetId = soundAsset.assetId; if (!runtime.audioEngine) { log.error('No audio engine present; cannot load sound asset: ', sound.md5); return Promise.resolve(sound); } - return runtime.audioEngine.decodeSound(Object.assign( + return runtime.audioEngine.decodeSoundPlayer(Object.assign( {}, sound, {data: soundAsset.data} - )).then(soundId => { - sound.soundId = soundId; + )).then(soundPlayer => { + sound.soundId = soundPlayer.id; // Set the sound sample rate and sample count based on the // the audio buffer from the audio engine since the sound // gets resampled by the audio engine - const soundBuffer = runtime.audioEngine.getSoundBuffer(soundId); + const soundBuffer = soundPlayer.buffer; sound.rate = soundBuffer.sampleRate; sound.sampleCount = soundBuffer.length; + if (sprite.soundBank !== null) { + sprite.soundBank.addSoundPlayer(soundPlayer); + } + return sound; }); }; @@ -39,9 +44,10 @@ const loadSoundFromAsset = function (sound, soundAsset, runtime) { * @property {string} md5 - the MD5 and extension of the sound to be loaded. * @property {Buffer} data - sound data will be written here once loaded. * @param {!Runtime} runtime - Scratch runtime, used to access the storage module. + * @param {Sprite} sprite - Scratch sprite to add sounds to. * @returns {!Promise} - a promise which will resolve to the sound when ready. */ -const loadSound = function (sound, runtime) { +const loadSound = function (sound, runtime, sprite) { if (!runtime.storage) { log.error('No storage module present; cannot load sound asset: ', sound.md5); return Promise.resolve(sound); @@ -52,7 +58,7 @@ const loadSound = function (sound, runtime) { return runtime.storage.load(runtime.storage.AssetType.Sound, md5, ext) .then(soundAsset => { sound.dataFormat = ext; - return loadSoundFromAsset(sound, soundAsset, runtime); + return loadSoundFromAsset(sound, soundAsset, runtime, sprite); }); }; diff --git a/src/serialization/sb3.js b/src/serialization/sb3.js index 4f129c1715..0ab76a15df 100644 --- a/src/serialization/sb3.js +++ b/src/serialization/sb3.js @@ -800,7 +800,7 @@ const parseScratchObject = function (object, runtime, extensions, zip) { // any translation that needs to happen will happen in the process // of building up the costume object into an sb3 format return deserializeSound(sound, runtime, zip) - .then(() => loadSound(sound, runtime)); + .then(() => loadSound(sound, runtime, sprite)); // Only attempt to load the sound after the deserialization // process has been completed. }); diff --git a/src/virtual-machine.js b/src/virtual-machine.js index 767641eece..9e93b79d2a 100644 --- a/src/virtual-machine.js +++ b/src/virtual-machine.js @@ -525,7 +525,7 @@ class VirtualMachine extends EventEmitter { * @returns {?Promise} - a promise that resolves when the sound has been decoded and added */ addSound (soundObject) { - return loadSound(soundObject, this.runtime).then(() => { + return loadSound(soundObject, this.runtime, this.editingTarget.sprite).then(() => { this.editingTarget.addSound(soundObject); this.emitTargetsUpdate(); }); @@ -549,7 +549,7 @@ class VirtualMachine extends EventEmitter { getSoundBuffer (soundIndex) { const id = this.editingTarget.sprite.sounds[soundIndex].soundId; if (id && this.runtime && this.runtime.audioEngine) { - return this.runtime.audioEngine.getSoundBuffer(id); + return this.editingTarget.sprite.soundBank.getSoundPlayer(id).buffer; } return null; } @@ -564,7 +564,7 @@ class VirtualMachine extends EventEmitter { const sound = this.editingTarget.sprite.sounds[soundIndex]; const id = sound ? sound.soundId : null; if (id && this.runtime && this.runtime.audioEngine) { - this.runtime.audioEngine.updateSoundBuffer(id, newBuffer); + this.editingTarget.sprite.soundBank.getSoundPlayer(id).buffer = newBuffer; } // Update sound in runtime if (soundEncoding) {