2017-04-17 15:10:04 -04:00
|
|
|
const MathUtil = require('../util/math-util');
|
|
|
|
const Cast = require('../util/cast');
|
|
|
|
const Clone = require('../util/clone');
|
2016-09-15 16:51:24 -04:00
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
class Scratch3SoundBlocks {
|
|
|
|
constructor (runtime) {
|
|
|
|
/**
|
|
|
|
* The runtime instantiating this block package.
|
|
|
|
* @type {Runtime}
|
|
|
|
*/
|
|
|
|
this.runtime = runtime;
|
2017-12-20 10:38:16 -05:00
|
|
|
|
|
|
|
// Clear sound effects on green flag and stop button events.
|
2018-10-02 13:04:54 -04:00
|
|
|
this.stopAllSounds = this.stopAllSounds.bind(this);
|
|
|
|
this._stopAllSoundsForTarget = this._stopAllSoundsForTarget.bind(this);
|
2017-12-20 10:38:16 -05:00
|
|
|
this._clearEffectsForAllTargets = this._clearEffectsForAllTargets.bind(this);
|
2017-12-20 10:56:44 -05:00
|
|
|
if (this.runtime) {
|
2018-10-02 13:04:54 -04:00
|
|
|
this.runtime.on('PROJECT_STOP_ALL', this.stopAllSounds);
|
2017-12-20 10:56:44 -05:00
|
|
|
this.runtime.on('PROJECT_STOP_ALL', this._clearEffectsForAllTargets);
|
2018-10-02 13:04:54 -04:00
|
|
|
this.runtime.on('STOP_FOR_TARGET', this._stopAllSoundsForTarget);
|
2017-12-20 10:56:44 -05:00
|
|
|
this.runtime.on('PROJECT_START', this._clearEffectsForAllTargets);
|
|
|
|
}
|
2018-01-09 15:56:58 -05:00
|
|
|
|
|
|
|
this._onTargetCreated = this._onTargetCreated.bind(this);
|
2018-01-09 19:22:33 -05:00
|
|
|
if (this.runtime) {
|
|
|
|
runtime.on('targetWasCreated', this._onTargetCreated);
|
|
|
|
}
|
2017-04-17 19:42:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The key to load & store a target's sound-related state.
|
|
|
|
* @type {string}
|
|
|
|
*/
|
|
|
|
static get STATE_KEY () {
|
|
|
|
return 'Scratch.sound';
|
|
|
|
}
|
|
|
|
|
2016-08-09 15:40:50 -04:00
|
|
|
/**
|
2017-04-17 19:42:48 -04:00
|
|
|
* The default sound-related state, to be used when a target has no existing sound state.
|
|
|
|
* @type {SoundState}
|
2016-08-09 15:40:50 -04:00
|
|
|
*/
|
2017-04-17 19:42:48 -04:00
|
|
|
static get DEFAULT_SOUND_STATE () {
|
|
|
|
return {
|
|
|
|
effects: {
|
|
|
|
pitch: 0,
|
2017-06-19 14:56:29 -04:00
|
|
|
pan: 0
|
2017-04-17 19:42:48 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The minimum and maximum MIDI note numbers, for clamping the input to play note.
|
|
|
|
* @type {{min: number, max: number}}
|
|
|
|
*/
|
|
|
|
static get MIDI_NOTE_RANGE () {
|
|
|
|
return {min: 36, max: 96}; // C2 to C7
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The minimum and maximum beat values, for clamping the duration of play note, play drum and rest.
|
|
|
|
* 100 beats at the default tempo of 60bpm is 100 seconds.
|
|
|
|
* @type {{min: number, max: number}}
|
|
|
|
*/
|
|
|
|
static get BEAT_RANGE () {
|
|
|
|
return {min: 0, max: 100};
|
|
|
|
}
|
|
|
|
|
2017-08-26 13:07:47 -04:00
|
|
|
/** The minimum and maximum tempo values, in bpm.
|
2017-04-17 19:42:48 -04:00
|
|
|
* @type {{min: number, max: number}}
|
|
|
|
*/
|
|
|
|
static get TEMPO_RANGE () {
|
|
|
|
return {min: 20, max: 500};
|
|
|
|
}
|
|
|
|
|
2017-08-26 13:07:47 -04:00
|
|
|
/** The minimum and maximum values for each sound effect.
|
2017-04-17 19:42:48 -04:00
|
|
|
* @type {{effect:{min: number, max: number}}}
|
|
|
|
*/
|
|
|
|
static get EFFECT_RANGE () {
|
|
|
|
return {
|
2018-01-31 16:44:54 -05:00
|
|
|
pitch: {min: -360, max: 360}, // -3 to 3 octaves
|
2017-08-26 13:07:47 -04:00
|
|
|
pan: {min: -100, max: 100} // 100% left to 100% right
|
2017-04-17 19:42:48 -04:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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.
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_getSoundState (target) {
|
|
|
|
let soundState = target.getCustomState(Scratch3SoundBlocks.STATE_KEY);
|
|
|
|
if (!soundState) {
|
|
|
|
soundState = Clone.simple(Scratch3SoundBlocks.DEFAULT_SOUND_STATE);
|
|
|
|
target.setCustomState(Scratch3SoundBlocks.STATE_KEY, soundState);
|
2018-06-22 09:33:08 -04:00
|
|
|
target.soundEffects = soundState.effects;
|
2017-04-17 19:42:48 -04:00
|
|
|
}
|
|
|
|
return soundState;
|
|
|
|
}
|
|
|
|
|
2018-01-09 15:56:58 -05:00
|
|
|
/**
|
|
|
|
* When a Target is cloned, clone the sound state.
|
|
|
|
* @param {Target} newTarget - the newly created target.
|
|
|
|
* @param {Target} [sourceTarget] - the target used as a source for the new clone, if any.
|
|
|
|
* @listens Runtime#event:targetWasCreated
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_onTargetCreated (newTarget, sourceTarget) {
|
|
|
|
if (sourceTarget) {
|
2018-01-22 16:38:45 -05:00
|
|
|
const soundState = sourceTarget.getCustomState(Scratch3SoundBlocks.STATE_KEY);
|
2018-01-09 15:56:58 -05:00
|
|
|
if (soundState && newTarget) {
|
|
|
|
newTarget.setCustomState(Scratch3SoundBlocks.STATE_KEY, Clone.simple(soundState));
|
|
|
|
this._syncEffectsForTarget(newTarget);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
/**
|
|
|
|
* Retrieve the block primitives implemented by this package.
|
|
|
|
* @return {object.<string, Function>} Mapping of opcode to Function.
|
|
|
|
*/
|
|
|
|
getPrimitives () {
|
|
|
|
return {
|
|
|
|
sound_play: this.playSound,
|
|
|
|
sound_playuntildone: this.playSoundAndWait,
|
|
|
|
sound_stopallsounds: this.stopAllSounds,
|
|
|
|
sound_seteffectto: this.setEffect,
|
|
|
|
sound_changeeffectby: this.changeEffect,
|
|
|
|
sound_cleareffects: this.clearEffects,
|
|
|
|
sound_sounds_menu: this.soundsMenu,
|
|
|
|
sound_beats_menu: this.beatsMenu,
|
|
|
|
sound_effects_menu: this.effectsMenu,
|
|
|
|
sound_setvolumeto: this.setVolume,
|
|
|
|
sound_changevolumeby: this.changeVolume,
|
2017-11-07 11:28:04 -05:00
|
|
|
sound_volume: this.getVolume
|
2017-04-17 19:42:48 -04:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-11-15 17:53:43 -05:00
|
|
|
getMonitored () {
|
|
|
|
return {
|
2018-05-08 14:09:18 -04:00
|
|
|
sound_volume: {
|
|
|
|
getId: () => 'volume'
|
|
|
|
}
|
2017-11-15 17:53:43 -05:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
playSound (args, util) {
|
2018-06-22 09:33:08 -04:00
|
|
|
// Don't return the promise, it's the only difference for AndWait
|
|
|
|
this.playSoundAndWait(args, util);
|
2017-04-17 19:42:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
playSoundAndWait (args, util) {
|
|
|
|
const index = this._getSoundIndex(args.SOUND_MENU, util);
|
|
|
|
if (index >= 0) {
|
2018-06-22 09:33:08 -04:00
|
|
|
const {target} = util;
|
|
|
|
const {sprite} = target;
|
|
|
|
const {soundId} = sprite.sounds[index];
|
|
|
|
if (sprite.soundBank) {
|
|
|
|
return sprite.soundBank.playSound(target, soundId);
|
|
|
|
}
|
2017-04-17 19:42:48 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_getSoundIndex (soundName, util) {
|
|
|
|
// if the sprite has no sounds, return -1
|
|
|
|
const len = util.target.sprite.sounds.length;
|
|
|
|
if (len === 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2017-07-06 14:28:11 -04:00
|
|
|
// look up by name first
|
|
|
|
const index = this.getSoundIndexByName(soundName, util);
|
|
|
|
if (index !== -1) {
|
2017-04-17 19:42:48 -04:00
|
|
|
return index;
|
|
|
|
}
|
|
|
|
|
2017-07-06 14:28:11 -04:00
|
|
|
// then try using the sound name as a 1-indexed index
|
|
|
|
const oneIndexedIndex = parseInt(soundName, 10);
|
|
|
|
if (!isNaN(oneIndexedIndex)) {
|
|
|
|
return MathUtil.wrapClamp(oneIndexedIndex - 1, 0, len - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// could not be found as a name or converted to index, return -1
|
|
|
|
return -1;
|
2017-04-17 19:42:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
getSoundIndexByName (soundName, util) {
|
|
|
|
const sounds = util.target.sprite.sounds;
|
|
|
|
for (let i = 0; i < sounds.length; i++) {
|
|
|
|
if (sounds[i].name === soundName) {
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// if there is no sound by that name, return -1
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2017-12-20 14:00:23 -05:00
|
|
|
stopAllSounds () {
|
|
|
|
if (this.runtime.targets === null) return;
|
|
|
|
const allTargets = this.runtime.targets;
|
|
|
|
for (let i = 0; i < allTargets.length; i++) {
|
|
|
|
this._stopAllSoundsForTarget(allTargets[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_stopAllSoundsForTarget (target) {
|
2018-06-22 09:33:08 -04:00
|
|
|
if (target.sprite.soundBank) {
|
|
|
|
target.sprite.soundBank.stopAllSounds(target);
|
|
|
|
}
|
2017-04-17 19:42:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
setEffect (args, util) {
|
2018-05-31 17:09:47 -04:00
|
|
|
return this._updateEffect(args, util, false);
|
2016-09-28 16:42:25 -04:00
|
|
|
}
|
2017-01-31 18:33:32 -05:00
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
changeEffect (args, util) {
|
2018-05-31 17:09:47 -04:00
|
|
|
return this._updateEffect(args, util, true);
|
2017-04-17 19:42:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
_updateEffect (args, util, change) {
|
|
|
|
const effect = Cast.toString(args.EFFECT).toLowerCase();
|
|
|
|
const value = Cast.toNumber(args.VALUE);
|
|
|
|
|
|
|
|
const soundState = this._getSoundState(util.target);
|
|
|
|
if (!soundState.effects.hasOwnProperty(effect)) return;
|
2016-08-09 15:40:50 -04:00
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
if (change) {
|
|
|
|
soundState.effects[effect] += value;
|
|
|
|
} else {
|
|
|
|
soundState.effects[effect] = value;
|
2017-01-31 18:33:32 -05:00
|
|
|
}
|
2017-04-17 19:42:48 -04:00
|
|
|
|
2018-06-22 09:33:08 -04:00
|
|
|
const {min, max} = Scratch3SoundBlocks.EFFECT_RANGE[effect];
|
|
|
|
soundState.effects[effect] = MathUtil.clamp(soundState.effects[effect], min, max);
|
2018-05-31 17:09:47 -04:00
|
|
|
|
2018-06-22 09:33:08 -04:00
|
|
|
this._syncEffectsForTarget(util.target);
|
2018-05-31 17:09:47 -04:00
|
|
|
// Yield until the next tick.
|
2018-06-06 09:53:45 -04:00
|
|
|
return Promise.resolve();
|
2017-04-17 19:42:48 -04:00
|
|
|
}
|
|
|
|
|
2018-01-09 15:56:58 -05:00
|
|
|
_syncEffectsForTarget (target) {
|
2018-06-22 09:33:08 -04:00
|
|
|
if (!target || !target.sprite.soundBank) return;
|
|
|
|
target.soundEffects = this._getSoundState(target).effects;
|
|
|
|
|
|
|
|
target.sprite.soundBank.setEffects(target);
|
2018-01-09 15:56:58 -05:00
|
|
|
}
|
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
clearEffects (args, util) {
|
2017-12-20 10:38:16 -05:00
|
|
|
this._clearEffectsForTarget(util.target);
|
|
|
|
}
|
|
|
|
|
|
|
|
_clearEffectsForTarget (target) {
|
|
|
|
const soundState = this._getSoundState(target);
|
2017-04-17 19:42:48 -04:00
|
|
|
for (const effect in soundState.effects) {
|
|
|
|
if (!soundState.effects.hasOwnProperty(effect)) continue;
|
|
|
|
soundState.effects[effect] = 0;
|
|
|
|
}
|
2018-06-22 09:33:08 -04:00
|
|
|
this._syncEffectsForTarget(target);
|
2017-12-20 10:38:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
_clearEffectsForAllTargets () {
|
2017-12-20 10:56:44 -05:00
|
|
|
if (this.runtime.targets === null) return;
|
2017-12-20 10:38:16 -05:00
|
|
|
const allTargets = this.runtime.targets;
|
|
|
|
for (let i = 0; i < allTargets.length; i++) {
|
|
|
|
this._clearEffectsForTarget(allTargets[i]);
|
|
|
|
}
|
2017-04-17 19:42:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
setVolume (args, util) {
|
|
|
|
const volume = Cast.toNumber(args.VOLUME);
|
2018-05-31 17:09:47 -04:00
|
|
|
return this._updateVolume(volume, util);
|
2017-04-17 19:42:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
changeVolume (args, util) {
|
2018-03-13 11:43:53 -04:00
|
|
|
const volume = Cast.toNumber(args.VOLUME) + util.target.volume;
|
2018-05-31 17:09:47 -04:00
|
|
|
return this._updateVolume(volume, util);
|
2017-04-17 19:42:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
_updateVolume (volume, util) {
|
|
|
|
volume = MathUtil.clamp(volume, 0, 100);
|
2018-03-13 11:43:53 -04:00
|
|
|
util.target.volume = volume;
|
2018-06-22 09:33:08 -04:00
|
|
|
this._syncEffectsForTarget(util.target);
|
2018-05-31 17:09:47 -04:00
|
|
|
|
|
|
|
// Yield until the next tick.
|
2018-06-06 09:53:45 -04:00
|
|
|
return Promise.resolve();
|
2017-04-17 19:42:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
getVolume (args, util) {
|
2018-03-13 11:43:53 -04:00
|
|
|
return util.target.volume;
|
2017-04-17 19:42:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
soundsMenu (args) {
|
|
|
|
return args.SOUND_MENU;
|
|
|
|
}
|
|
|
|
|
|
|
|
beatsMenu (args) {
|
|
|
|
return args.BEATS;
|
|
|
|
}
|
|
|
|
|
|
|
|
effectsMenu (args) {
|
|
|
|
return args.EFFECT;
|
2017-01-31 18:33:32 -05:00
|
|
|
}
|
2017-04-17 19:42:48 -04:00
|
|
|
}
|
2016-08-11 16:47:01 -04:00
|
|
|
|
2016-08-09 15:40:50 -04:00
|
|
|
module.exports = Scratch3SoundBlocks;
|