From c0d57b4b6b043ccc3fda22fc4f2971e26617dc84 Mon Sep 17 00:00:00 2001 From: Eric Rosenbaum Date: Wed, 4 Jan 2017 18:37:55 -0500 Subject: [PATCH] separate out main audioengine from audioplayer for each clone --- src/blocks/scratch3_sound.js | 26 +++++++++++++------------- src/engine/runtime.js | 8 ++++++++ src/index.js | 8 ++++++++ src/sprites/rendered-target.js | 7 ++++--- 4 files changed, 33 insertions(+), 16 deletions(-) diff --git a/src/blocks/scratch3_sound.js b/src/blocks/scratch3_sound.js index bbb63e46f..15b273252 100644 --- a/src/blocks/scratch3_sound.js +++ b/src/blocks/scratch3_sound.js @@ -37,12 +37,12 @@ Scratch3SoundBlocks.prototype.getPrimitives = function () { Scratch3SoundBlocks.prototype.playSound = function (args, util) { var index = this._getSoundIndex(args.SOUND_MENU, util); - util.target.audioEngine.playSound(index); + util.target.audioPlayer.playSound(index); }; Scratch3SoundBlocks.prototype.playSoundAndWait = function (args, util) { var index = this._getSoundIndex(args.SOUND_MENU, util); - return util.target.audioEngine.playSound(index); + return util.target.audioPlayer.playSound(index); }; Scratch3SoundBlocks.prototype._getSoundIndex = function (soundName, util) { @@ -65,11 +65,11 @@ Scratch3SoundBlocks.prototype._getSoundIndex = function (soundName, util) { }; Scratch3SoundBlocks.prototype.stopAllSounds = function (args, util) { - util.target.audioEngine.stopAllSounds(); + util.target.audioPlayer.stopAllSounds(); }; Scratch3SoundBlocks.prototype.playNoteForBeats = function (args, util) { - util.target.audioEngine.playNoteForBeats(args.NOTE, args.BEATS); + util.target.audioPlayer.playNoteForBeats(args.NOTE, args.BEATS); return new Promise(function (resolve) { setTimeout(function () { resolve(); @@ -78,7 +78,7 @@ Scratch3SoundBlocks.prototype.playNoteForBeats = function (args, util) { }; Scratch3SoundBlocks.prototype.playDrumForBeats = function (args, util) { - util.target.audioEngine.playDrumForBeats(args.DRUM, args.BEATS); + util.target.audioPlayer.playDrumForBeats(args.DRUM, args.BEATS); return new Promise(function (resolve) { setTimeout(function () { resolve(); @@ -88,41 +88,41 @@ Scratch3SoundBlocks.prototype.playDrumForBeats = function (args, util) { Scratch3SoundBlocks.prototype.setInstrument = function (args, util) { var instNum = Cast.toNumber(args.INSTRUMENT); - return util.target.audioEngine.setInstrument(instNum); + return util.target.audioPlayer.setInstrument(instNum); }; Scratch3SoundBlocks.prototype.setEffect = function (args, util) { var value = Cast.toNumber(args.VALUE); - util.target.audioEngine.setEffect(args.EFFECT, value); + util.target.audioPlayer.setEffect(args.EFFECT, value); }; Scratch3SoundBlocks.prototype.changeEffect = function (args, util) { var value = Cast.toNumber(args.VALUE); - util.target.audioEngine.changeEffect(args.EFFECT, value); + util.target.audioPlayer.changeEffect(args.EFFECT, value); }; Scratch3SoundBlocks.prototype.clearEffects = function (args, util) { - util.target.audioEngine.clearEffects(); + util.target.audioPlayer.clearEffects(); }; Scratch3SoundBlocks.prototype.setVolume = function (args, util) { var value = Cast.toNumber(args.VOLUME); - util.target.audioEngine.setVolume(value); + util.target.audioPlayer.setVolume(value); }; Scratch3SoundBlocks.prototype.changeVolume = function (args, util) { var value = Cast.toNumber(args.VOLUME); - util.target.audioEngine.changeVolume(value); + util.target.audioPlayer.changeVolume(value); }; Scratch3SoundBlocks.prototype.setTempo = function (args, util) { var value = Cast.toNumber(args.TEMPO); - util.target.audioEngine.setTempo(value); + util.target.audioPlayer.setTempo(value); }; Scratch3SoundBlocks.prototype.changeTempo = function (args, util) { var value = Cast.toNumber(args.TEMPO); - util.target.audioEngine.changeTempo(value); + util.target.audioPlayer.changeTempo(value); }; Scratch3SoundBlocks.prototype.soundsMenu = function (args) { diff --git a/src/engine/runtime.js b/src/engine/runtime.js index 4e756ad20..560ef23f1 100644 --- a/src/engine/runtime.js +++ b/src/engine/runtime.js @@ -317,6 +317,14 @@ Runtime.prototype.attachRenderer = function (renderer) { this.renderer = renderer; }; +/** + * Attach the audio engine + * @param {!RenderWebGL} audioEngine The audio engine to attach + */ +Runtime.prototype.attachAudioEngine = function (audioEngine) { + this.audioEngine = audioEngine; +}; + // ----------------------------------------------------------------------------- // ----------------------------------------------------------------------------- diff --git a/src/index.js b/src/index.js index 24e1d67b7..7faf7fb9a 100644 --- a/src/index.js +++ b/src/index.js @@ -251,6 +251,14 @@ VirtualMachine.prototype.attachRenderer = function (renderer) { this.runtime.attachRenderer(renderer); }; +/** + * Set the audio engine for the VM/runtime + * @param {!AudioEngine} audioEngine The audio engine to attach + */ +VirtualMachine.prototype.attachAudioEngine = function (audioEngine) { + this.runtime.attachAudioEngine(audioEngine); +}; + /** * Handle a Blockly event for the current editing target. * @param {!Blockly.Event} e Any Blockly event. diff --git a/src/sprites/rendered-target.js b/src/sprites/rendered-target.js index fdb8197e1..628367b5e 100644 --- a/src/sprites/rendered-target.js +++ b/src/sprites/rendered-target.js @@ -49,11 +49,12 @@ var RenderedTarget = function (sprite, runtime) { }; /** - * Audio engine + * Audio player */ - this.audioEngine = null; + this.audioPlayer = null; if (this.runtime) { - this.audioEngine = new AudioEngine(this.sprite.sounds); + this.audioPlayer = this.runtime.audioEngine.createPlayer(); + this.audioPlayer.loadSounds(this.sprite.sounds); } };