Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use new scratch-audio SoundBank in vm / sounds #1239

Merged
merged 2 commits into from
Jun 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 21 additions & 26 deletions src/blocks/scratch3_sound.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ class Scratch3SoundBlocks {
if (!soundState) {
soundState = Clone.simple(Scratch3SoundBlocks.DEFAULT_SOUND_STATE);
target.setCustomState(Scratch3SoundBlocks.STATE_KEY, soundState);
target.soundEffects = soundState.effects;
}
return soundState;
}
Expand Down Expand Up @@ -139,20 +140,19 @@ class Scratch3SoundBlocks {
}

playSound (args, util) {
const index = this._getSoundIndex(args.SOUND_MENU, util);
if (index >= 0) {
const soundId = util.target.sprite.sounds[index].soundId;
if (util.target.audioPlayer === null) return;
util.target.audioPlayer.playSound(soundId);
}
// Don't return the promise, it's the only difference for AndWait
this.playSoundAndWait(args, util);
}

playSoundAndWait (args, util) {
const index = this._getSoundIndex(args.SOUND_MENU, util);
if (index >= 0) {
const soundId = util.target.sprite.sounds[index].soundId;
if (util.target.audioPlayer === null) return;
return util.target.audioPlayer.playSound(soundId);
const {target} = util;
const {sprite} = target;
const {soundId} = sprite.sounds[index];
if (sprite.soundBank) {
return sprite.soundBank.playSound(target, soundId);
}
}
}

Expand Down Expand Up @@ -199,8 +199,9 @@ class Scratch3SoundBlocks {
}

_stopAllSoundsForTarget (target) {
if (target.audioPlayer === null) return;
target.audioPlayer.stopAllSounds();
if (target.sprite.soundBank) {
target.sprite.soundBank.stopAllSounds(target);
}
}

setEffect (args, util) {
Expand All @@ -224,23 +225,19 @@ class Scratch3SoundBlocks {
soundState.effects[effect] = value;
}

const effectRange = Scratch3SoundBlocks.EFFECT_RANGE[effect];
soundState.effects[effect] = MathUtil.clamp(soundState.effects[effect], effectRange.min, effectRange.max);

if (util.target.audioPlayer === null) return;
util.target.audioPlayer.setEffect(effect, soundState.effects[effect]);
const {min, max} = Scratch3SoundBlocks.EFFECT_RANGE[effect];
soundState.effects[effect] = MathUtil.clamp(soundState.effects[effect], min, max);

this._syncEffectsForTarget(util.target);
// Yield until the next tick.
return Promise.resolve();
}

_syncEffectsForTarget (target) {
if (!target || !target.audioPlayer) return;
const soundState = this._getSoundState(target);
for (const effect in soundState.effects) {
if (!soundState.effects.hasOwnProperty(effect)) continue;
target.audioPlayer.setEffect(effect, soundState.effects[effect]);
}
if (!target || !target.sprite.soundBank) return;
target.soundEffects = this._getSoundState(target).effects;

target.sprite.soundBank.setEffects(target);
}

clearEffects (args, util) {
Expand All @@ -253,8 +250,7 @@ class Scratch3SoundBlocks {
if (!soundState.effects.hasOwnProperty(effect)) continue;
soundState.effects[effect] = 0;
}
if (target.audioPlayer === null) return;
target.audioPlayer.clearEffects();
this._syncEffectsForTarget(target);
}

_clearEffectsForAllTargets () {
Expand All @@ -278,8 +274,7 @@ class Scratch3SoundBlocks {
_updateVolume (volume, util) {
volume = MathUtil.clamp(volume, 0, 100);
util.target.volume = volume;
if (util.target.audioPlayer === null) return;
util.target.audioPlayer.setVolume(util.target.volume);
this._syncEffectsForTarget(util.target);

// Yield until the next tick.
return Promise.resolve();
Expand Down
43 changes: 25 additions & 18 deletions src/sprites/rendered-target.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,21 +170,30 @@ class RenderedTarget extends Target {
}
}

get audioPlayer () {

This comment was marked as abuse.

/* 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.
*/
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;
}
}
}
}

/**
Expand Down Expand Up @@ -1034,9 +1043,8 @@ class RenderedTarget extends Target {
*/
onStopAll () {
this.clearEffects();
if (this.audioPlayer) {
this.audioPlayer.stopAllSounds();
this.audioPlayer.clearEffects();
if (this.sprite.soundBank) {
this.sprite.soundBank.stopAllSounds(this);
}
}

Expand Down Expand Up @@ -1122,6 +1130,9 @@ class RenderedTarget extends Target {
dispose () {
this.runtime.changeCloneCounter(-1);
this.runtime.stopForTarget(this);
if (this.sprite.soundBank) {
this.sprite.soundBank.stopAllSounds(this);
}
this.sprite.removeClone(this);
if (this.renderer && this.drawableID !== null) {
this.renderer.destroyDrawable(this.drawableID, this.isStage ?
Expand All @@ -1132,10 +1143,6 @@ class RenderedTarget extends Target {
this.runtime.requestRedraw();
}
}
if (this.audioPlayer) {
this.audioPlayer.stopAllSounds();
this.audioPlayer.dispose();
}
}
}

Expand Down
14 changes: 13 additions & 1 deletion src/sprites/sprite.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ const StageLayering = require('../engine/stage-layering');
class Sprite {
/**
* 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 {Runtime} runtime Reference to the runtime.
* @constructor
Expand Down Expand Up @@ -47,6 +48,11 @@ class Sprite {
* @type {Array.<!RenderedTarget>}
*/
this.clones = [];

this.soundBank = null;
if (this.runtime && this.runtime.audioEngine) {
this.soundBank = this.runtime.audioEngine.createBank();
}
}

/**
Expand Down Expand Up @@ -155,6 +161,12 @@ class Sprite {

return Promise.all(assetPromises).then(() => newSprite);
}

dispose () {

This comment was marked as abuse.

if (this.soundBank) {
this.soundBank.dispose();
}
}
}

module.exports = Sprite;
8 changes: 4 additions & 4 deletions test/unit/blocks_sounds.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ const util = {
{name: 'second name', soundId: 'second soundId'},
{name: 'third name', soundId: 'third soundId'},
{name: '6', soundId: 'fourth soundId'}
]
},
audioPlayer: {
playSound: soundId => (playedSound = soundId)
],
soundBank: {
playSound: (target, soundId) => (playedSound = soundId)
}
}
}
};
Expand Down