soundbank tests

This commit is contained in:
Corey Frang 2018-06-15 12:22:49 -04:00
parent 61dacfc915
commit c087cf326a
4 changed files with 60 additions and 49 deletions

View file

@ -88,6 +88,7 @@ class Scratch3SoundBlocks {
if (!soundState) { if (!soundState) {
soundState = Clone.simple(Scratch3SoundBlocks.DEFAULT_SOUND_STATE); soundState = Clone.simple(Scratch3SoundBlocks.DEFAULT_SOUND_STATE);
target.setCustomState(Scratch3SoundBlocks.STATE_KEY, soundState); target.setCustomState(Scratch3SoundBlocks.STATE_KEY, soundState);
target.soundEffects = soundState.effects;
} }
return soundState; return soundState;
} }
@ -139,20 +140,19 @@ class Scratch3SoundBlocks {
} }
playSound (args, util) { playSound (args, util) {
const index = this._getSoundIndex(args.SOUND_MENU, util); // Don't return the promise, it's the only difference for AndWait
if (index >= 0) { this.playSoundAndWait(args, util);
const soundId = util.target.sprite.sounds[index].soundId;
if (util.target.audioPlayer === null) return;
util.target.audioPlayer.playSound(soundId);
}
} }
playSoundAndWait (args, util) { playSoundAndWait (args, util) {
const index = this._getSoundIndex(args.SOUND_MENU, util); const index = this._getSoundIndex(args.SOUND_MENU, util);
if (index >= 0) { if (index >= 0) {
const soundId = util.target.sprite.sounds[index].soundId; const {target} = util;
if (util.target.audioPlayer === null) return; const {sprite} = target;
return util.target.audioPlayer.playSound(soundId); const {soundId} = sprite.sounds[index];
if (sprite.soundBank) {
return sprite.soundBank.playSound(target, soundId);
}
} }
} }
@ -199,8 +199,9 @@ class Scratch3SoundBlocks {
} }
_stopAllSoundsForTarget (target) { _stopAllSoundsForTarget (target) {
if (target.audioPlayer === null) return; if (target.sprite.soundBank) {
target.audioPlayer.stopAllSounds(); target.sprite.soundBank.stopAllSounds(target);
}
} }
setEffect (args, util) { setEffect (args, util) {
@ -224,23 +225,19 @@ class Scratch3SoundBlocks {
soundState.effects[effect] = value; soundState.effects[effect] = value;
} }
const effectRange = Scratch3SoundBlocks.EFFECT_RANGE[effect]; const {min, max} = Scratch3SoundBlocks.EFFECT_RANGE[effect];
soundState.effects[effect] = MathUtil.clamp(soundState.effects[effect], effectRange.min, effectRange.max); soundState.effects[effect] = MathUtil.clamp(soundState.effects[effect], min, max);
if (util.target.audioPlayer === null) return;
util.target.audioPlayer.setEffect(effect, soundState.effects[effect]);
this._syncEffectsForTarget(util.target);
// Yield until the next tick. // Yield until the next tick.
return Promise.resolve(); return Promise.resolve();
} }
_syncEffectsForTarget (target) { _syncEffectsForTarget (target) {
if (!target || !target.audioPlayer) return; if (!target || !target.sprite.soundBank) return;
const soundState = this._getSoundState(target); target.soundEffects = this._getSoundState(target).effects;
for (const effect in soundState.effects) {
if (!soundState.effects.hasOwnProperty(effect)) continue; target.sprite.soundBank.setEffects(target);
target.audioPlayer.setEffect(effect, soundState.effects[effect]);
}
} }
clearEffects (args, util) { clearEffects (args, util) {
@ -253,8 +250,7 @@ class Scratch3SoundBlocks {
if (!soundState.effects.hasOwnProperty(effect)) continue; if (!soundState.effects.hasOwnProperty(effect)) continue;
soundState.effects[effect] = 0; soundState.effects[effect] = 0;
} }
if (target.audioPlayer === null) return; this._syncEffectsForTarget(target);
target.audioPlayer.clearEffects();
} }
_clearEffectsForAllTargets () { _clearEffectsForAllTargets () {
@ -278,8 +274,7 @@ class Scratch3SoundBlocks {
_updateVolume (volume, util) { _updateVolume (volume, util) {
volume = MathUtil.clamp(volume, 0, 100); volume = MathUtil.clamp(volume, 0, 100);
util.target.volume = volume; util.target.volume = volume;
if (util.target.audioPlayer === null) return; this._syncEffectsForTarget(util.target);
util.target.audioPlayer.setVolume(util.target.volume);
// Yield until the next tick. // Yield until the next tick.
return Promise.resolve(); return Promise.resolve();

View file

@ -170,21 +170,30 @@ class RenderedTarget extends Target {
} }
} }
get audioPlayer () {
/* eslint-disable no-console */
console.warn('get audioPlayer deprecated, please update to use .sprite.soundBank methods');
console.warn(new Error('stack for debug').stack);
/* eslint-enable no-console */
const bank = this.sprite.soundBank;
const audioPlayerProxy = {
playSound: soundId => bank.play(this, soundId)
};
Object.defineProperty(this, 'audioPlayer', {
configurable: false,
enumerable: true,
writable: false,
value: audioPlayerProxy
});
return audioPlayerProxy;
}
/** /**
* Initialize the audio player for this sprite or clone. * Initialize the audio player for this sprite or clone.
*/ */
initAudio () { initAudio () {
this.audioPlayer = null;
if (this.runtime && this.runtime.audioEngine) {
this.audioPlayer = this.runtime.audioEngine.createPlayer();
// If this is a clone, it gets a reference to its parent's activeSoundPlayers object.
if (!this.isOriginal) {
const parent = this.sprite.clones[0];
if (parent && parent.audioPlayer) {
this.audioPlayer.activeSoundPlayers = parent.audioPlayer.activeSoundPlayers;
}
}
}
} }
/** /**
@ -1034,9 +1043,8 @@ class RenderedTarget extends Target {
*/ */
onStopAll () { onStopAll () {
this.clearEffects(); this.clearEffects();
if (this.audioPlayer) { if (this.sprite.soundBank) {
this.audioPlayer.stopAllSounds(); this.sprite.soundBank.stopAllSounds(this);
this.audioPlayer.clearEffects();
} }
} }
@ -1132,10 +1140,6 @@ class RenderedTarget extends Target {
this.runtime.requestRedraw(); this.runtime.requestRedraw();
} }
} }
if (this.audioPlayer) {
this.audioPlayer.stopAllSounds();
this.audioPlayer.dispose();
}
} }
} }

View file

@ -8,7 +8,8 @@ const StageLayering = require('../engine/stage-layering');
class Sprite { class Sprite {
/** /**
* Sprite to be used on the Scratch stage. * Sprite to be used on the Scratch stage.
* All clones of a sprite have shared blocks, shared costumes, shared variables. * All clones of a sprite have shared blocks, shared costumes, shared variables,
* shared sounds, etc.
* @param {?Blocks} blocks Shared blocks object for all clones of sprite. * @param {?Blocks} blocks Shared blocks object for all clones of sprite.
* @param {Runtime} runtime Reference to the runtime. * @param {Runtime} runtime Reference to the runtime.
* @constructor * @constructor
@ -47,6 +48,11 @@ class Sprite {
* @type {Array.<!RenderedTarget>} * @type {Array.<!RenderedTarget>}
*/ */
this.clones = []; this.clones = [];
this.soundBank = null;
if (this.runtime && this.runtime.audioEngine) {
this.soundBank = this.runtime.audioEngine.createBank();
}
} }
/** /**
@ -155,6 +161,12 @@ class Sprite {
return Promise.all(assetPromises).then(() => newSprite); return Promise.all(assetPromises).then(() => newSprite);
} }
dispose () {
if (this.soundBank) {
this.soundBank.dispose();
}
}
} }
module.exports = Sprite; module.exports = Sprite;

View file

@ -11,10 +11,10 @@ const util = {
{name: 'second name', soundId: 'second soundId'}, {name: 'second name', soundId: 'second soundId'},
{name: 'third name', soundId: 'third soundId'}, {name: 'third name', soundId: 'third soundId'},
{name: '6', soundId: 'fourth soundId'} {name: '6', soundId: 'fourth soundId'}
] ],
}, soundBank: {
audioPlayer: { playSound: (target, soundId) => (playedSound = soundId)
playSound: soundId => (playedSound = soundId) }
} }
} }
}; };