diff --git a/src/virtual-machine.js b/src/virtual-machine.js index 4e0a2bb11..e2b18f2a3 100644 --- a/src/virtual-machine.js +++ b/src/virtual-machine.js @@ -414,12 +414,34 @@ class VirtualMachine extends EventEmitter { * Update a sound buffer. * @param {int} soundIndex - the index of the sound to be updated. * @param {AudioBuffer} newBuffer - new audio buffer for the audio engine. + * @param {ArrayBuffer} soundEncoding - the new (wav) encoded sound to be stored */ - updateSoundBuffer (soundIndex, newBuffer) { - const id = this.editingTarget.sprite.sounds[soundIndex].soundId; + updateSoundBuffer (soundIndex, newBuffer, soundEncoding) { + 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); } + // Update sound in runtime + if (soundEncoding) { + // Now that we updated the sound, the format should also be updated + // so that the sound can eventually be decoded the right way. + // Sounds that were formerly 'adpcm', but were updated in sound editor + // will not get decoded by the audio engine correctly unless the format + // is updated as below. + sound.format = ''; + const storage = this.runtime.storage; + sound.assetId = storage.builtinHelper.cache( + storage.AssetType.Sound, + storage.DataFormat.WAV, + soundEncoding + ); + sound.md5 = `${sound.assetId}.${sound.dataFormat}`; + } + // If soundEncoding is null, it's because gui had a problem + // encoding the updated sound. We don't want to store anything in this + // case, and gui should have logged an error. + this.emitTargetsUpdate(); }