Skip to content

Commit

Permalink
add decoded SoundPlayer's to a Sprite's SoundBank (scratchfoundation#…
Browse files Browse the repository at this point in the history
  • Loading branch information
mzgoddard authored and gnarf committed Jun 21, 2018
1 parent 30b8cb8 commit c268bba
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 11 deletions.
20 changes: 13 additions & 7 deletions src/import/load-sound.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});
};
Expand All @@ -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);
Expand All @@ -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);
});
};

Expand Down
2 changes: 1 addition & 1 deletion src/serialization/sb3.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
});
Expand Down
6 changes: 3 additions & 3 deletions src/virtual-machine.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
Expand All @@ -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;
}
Expand All @@ -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) {
Expand Down

0 comments on commit c268bba

Please sign in to comment.