From 106db6a0244e788b6821a2e36bf38d8b09d5bae2 Mon Sep 17 00:00:00 2001 From: Eric Rosenbaum Date: Mon, 20 Mar 2017 14:53:04 -0400 Subject: [PATCH] Clamp range for each audio effect --- src/blocks/scratch3_sound.js | 36 ++++++++++++++++++++++++++---------- 1 file changed, 26 insertions(+), 10 deletions(-) diff --git a/src/blocks/scratch3_sound.js b/src/blocks/scratch3_sound.js index 696ece4a8..c60e41fb0 100644 --- a/src/blocks/scratch3_sound.js +++ b/src/blocks/scratch3_sound.js @@ -51,6 +51,18 @@ Scratch3SoundBlocks.BEAT_RANGE = {min: 0, max: 100}; */ Scratch3SoundBlocks.TEMPO_RANGE = {min: 20, max: 500}; + /** The minimum and maximum values for each sound effect. + * @type {{effect:{min: number, max: number}}} + */ +Scratch3SoundBlocks.EFFECT_RANGE = { + pitch: {min: -600, max: 600}, // -5 to 5 octaves + pan: {min: -100, max: 100}, // 100% left to 100% right + echo: {min: 0, max: 100}, // 0 to max (75%) feedback + reverb: {min: 0, max: 100}, // wet/dry: 0 to 100% wet + fuzz: {min: 0, max: 100}, // wed/dry: 0 to 100% wet + robot: {min: 0, max: 600} // 0 to 5 octaves +}; + /** * @param {Target} target - collect sound state for this target. * @returns {SoundState} the mutable sound state associated with that target. This will be created if necessary. @@ -193,25 +205,29 @@ Scratch3SoundBlocks.prototype.setInstrument = function (args, util) { }; Scratch3SoundBlocks.prototype.setEffect = function (args, util) { - var effect = Cast.toString(args.EFFECT).toLowerCase(); - var value = Cast.toNumber(args.VALUE); - - var soundState = this._getSoundState(util.target); - if (!soundState.effects.hasOwnProperty(effect)) return; - - soundState.effects[effect] = value; - if (util.target.audioPlayer === null) return; - util.target.audioPlayer.setEffect(effect, soundState.effects[effect]); + this._updateEffect(args, util, false); }; Scratch3SoundBlocks.prototype.changeEffect = function (args, util) { + this._updateEffect(args, util, true); +}; + +Scratch3SoundBlocks.prototype._updateEffect = function (args, util, change) { var effect = Cast.toString(args.EFFECT).toLowerCase(); var value = Cast.toNumber(args.VALUE); var soundState = this._getSoundState(util.target); if (!soundState.effects.hasOwnProperty(effect)) return; - soundState.effects[effect] += value; + if (change) { + soundState.effects[effect] += value; + } else { + soundState.effects[effect] = value; + } + + var 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]); };