2016-09-28 16:42:25 -04:00
|
|
|
var MathUtil = require('../util/math-util');
|
2016-10-19 15:44:30 -04:00
|
|
|
var Cast = require('../util/cast');
|
2016-09-15 16:51:24 -04:00
|
|
|
|
2017-01-03 23:41:49 -05:00
|
|
|
var Scratch3SoundBlocks = function (runtime) {
|
2016-08-09 15:40:50 -04:00
|
|
|
/**
|
|
|
|
* The runtime instantiating this block package.
|
|
|
|
* @type {Runtime}
|
|
|
|
*/
|
|
|
|
this.runtime = runtime;
|
2017-01-03 23:41:49 -05:00
|
|
|
};
|
2016-08-09 15:40:50 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieve the block primitives implemented by this package.
|
|
|
|
* @return {Object.<string, Function>} Mapping of opcode to Function.
|
|
|
|
*/
|
2017-01-03 23:41:49 -05:00
|
|
|
Scratch3SoundBlocks.prototype.getPrimitives = function () {
|
2016-08-09 15:40:50 -04:00
|
|
|
return {
|
2017-01-03 23:41:49 -05:00
|
|
|
sound_play: this.playSound,
|
|
|
|
sound_playuntildone: this.playSoundAndWait,
|
|
|
|
sound_stopallsounds: this.stopAllSounds,
|
|
|
|
sound_playnoteforbeats: this.playNoteForBeats,
|
|
|
|
sound_playdrumforbeats: this.playDrumForBeats,
|
2017-01-06 10:31:01 -05:00
|
|
|
sound_restforbeats: this.restForBeats,
|
2017-01-03 23:41:49 -05:00
|
|
|
sound_setinstrumentto: this.setInstrument,
|
|
|
|
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-01-09 15:48:02 -05:00
|
|
|
sound_volume: this.getVolume,
|
2017-01-06 10:31:01 -05:00
|
|
|
sound_settempotobpm: this.setTempo,
|
|
|
|
sound_changetempoby: this.changeTempo,
|
|
|
|
sound_tempo: this.getTempo
|
2016-08-09 15:40:50 -04:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
Scratch3SoundBlocks.prototype.playSound = function (args, util) {
|
2016-10-17 17:16:55 -04:00
|
|
|
var index = this._getSoundIndex(args.SOUND_MENU, util);
|
2017-01-30 10:45:15 -05:00
|
|
|
util.target.playSound(index);
|
2016-09-27 17:09:53 -04:00
|
|
|
};
|
|
|
|
|
2016-10-17 17:16:55 -04:00
|
|
|
Scratch3SoundBlocks.prototype.playSoundAndWait = function (args, util) {
|
|
|
|
var index = this._getSoundIndex(args.SOUND_MENU, util);
|
2017-01-30 10:45:15 -05:00
|
|
|
return util.target.playSound(index);
|
2016-10-17 17:16:55 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
Scratch3SoundBlocks.prototype._getSoundIndex = function (soundName, util) {
|
2017-01-30 10:56:50 -05:00
|
|
|
// if the sprite has no sounds, return 0
|
2017-01-03 23:41:49 -05:00
|
|
|
if (util.target.sprite.sounds.length === 0) {
|
2016-10-17 17:16:55 -04:00
|
|
|
return 0;
|
2016-09-28 16:42:25 -04:00
|
|
|
}
|
|
|
|
var index;
|
2017-01-03 23:41:49 -05:00
|
|
|
|
2017-01-30 10:56:50 -05:00
|
|
|
// if the sound name is a number, wrapclamp it to a sound index
|
2016-10-24 17:00:51 -04:00
|
|
|
if (Number(soundName)) {
|
|
|
|
soundName = Number(soundName);
|
2016-10-13 11:54:00 -04:00
|
|
|
var len = util.target.sprite.sounds.length;
|
2017-01-04 14:41:14 -05:00
|
|
|
index = MathUtil.wrapClamp(soundName, 1, len) - 1;
|
2016-09-28 16:42:25 -04:00
|
|
|
} else {
|
2017-01-30 10:56:50 -05:00
|
|
|
// else get the index for that sound of that name
|
2016-09-28 16:42:25 -04:00
|
|
|
index = util.target.getSoundIndexByName(soundName);
|
2017-01-30 10:56:50 -05:00
|
|
|
// use zero if there is no sound by that name
|
|
|
|
// to match scratch 2.0 behavior, we should instead play no sound in this case
|
2017-01-03 23:41:49 -05:00
|
|
|
if (index === -1) {
|
2016-10-17 17:16:55 -04:00
|
|
|
index = 0;
|
2016-09-28 16:42:25 -04:00
|
|
|
}
|
|
|
|
}
|
2016-10-17 17:16:55 -04:00
|
|
|
return index;
|
2016-08-09 15:40:50 -04:00
|
|
|
};
|
|
|
|
|
2016-08-11 16:47:01 -04:00
|
|
|
Scratch3SoundBlocks.prototype.stopAllSounds = function (args, util) {
|
2017-01-04 18:37:55 -05:00
|
|
|
util.target.audioPlayer.stopAllSounds();
|
2016-08-11 16:47:01 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
Scratch3SoundBlocks.prototype.playNoteForBeats = function (args, util) {
|
2017-01-10 18:00:33 -05:00
|
|
|
var note = Cast.toNumber(args.NOTE);
|
|
|
|
var beats = Cast.toNumber(args.BEATS);
|
2017-01-30 10:56:06 -05:00
|
|
|
return util.target.playNoteForBeats(note, beats);
|
2017-01-06 11:49:25 -05:00
|
|
|
};
|
2016-10-19 15:44:30 -04:00
|
|
|
|
2016-08-11 16:47:01 -04:00
|
|
|
Scratch3SoundBlocks.prototype.playDrumForBeats = function (args, util) {
|
2017-01-11 11:22:58 -05:00
|
|
|
var drum = Cast.toNumber(args.DRUM);
|
2017-01-11 11:23:39 -05:00
|
|
|
drum -= 1; // drums are one-indexed
|
2017-01-11 11:41:21 -05:00
|
|
|
drum = MathUtil.wrapClamp(drum, 0, this.runtime.audioEngine.numDrums);
|
2017-01-10 18:00:33 -05:00
|
|
|
var beats = Cast.toNumber(args.BEATS);
|
|
|
|
return util.target.audioPlayer.playDrumForBeats(drum, beats);
|
2017-01-06 10:31:01 -05:00
|
|
|
};
|
|
|
|
|
2017-01-30 10:56:31 -05:00
|
|
|
Scratch3SoundBlocks.prototype.restForBeats = function (args) {
|
2017-01-10 18:00:33 -05:00
|
|
|
var beats = Cast.toNumber(args.BEATS);
|
2017-01-30 10:56:31 -05:00
|
|
|
return this.runtime.audioEngine.waitForBeats(beats);
|
2016-08-11 16:47:01 -04:00
|
|
|
};
|
|
|
|
|
2016-10-19 15:44:30 -04:00
|
|
|
Scratch3SoundBlocks.prototype.setInstrument = function (args, util) {
|
|
|
|
var instNum = Cast.toNumber(args.INSTRUMENT);
|
2017-01-11 11:23:39 -05:00
|
|
|
instNum -= 1; // instruments are one-indexed
|
2017-01-11 11:41:21 -05:00
|
|
|
instNum = MathUtil.wrapClamp(instNum, 0, this.runtime.audioEngine.numInstruments);
|
2017-01-30 10:54:24 -05:00
|
|
|
util.target.setInstrument(instNum);
|
|
|
|
return this.runtime.audioEngine.instrumentPlayer.loadInstrument(instNum);
|
2016-10-19 15:44:30 -04:00
|
|
|
};
|
|
|
|
|
2016-08-11 16:47:01 -04:00
|
|
|
Scratch3SoundBlocks.prototype.setEffect = function (args, util) {
|
2017-01-30 10:53:12 -05:00
|
|
|
var effect = Cast.toString(args.EFFECT).toLowerCase();
|
2016-10-27 11:31:22 -04:00
|
|
|
var value = Cast.toNumber(args.VALUE);
|
2017-01-30 10:53:12 -05:00
|
|
|
util.target.setAudioEffect(effect, value);
|
2016-08-11 16:47:01 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
Scratch3SoundBlocks.prototype.changeEffect = function (args, util) {
|
2017-01-30 10:53:12 -05:00
|
|
|
var effect = Cast.toString(args.EFFECT).toLowerCase();
|
2016-10-27 11:31:22 -04:00
|
|
|
var value = Cast.toNumber(args.VALUE);
|
2017-01-30 10:53:12 -05:00
|
|
|
if (!util.target.audioEffects.hasOwnProperty(effect)) return;
|
|
|
|
var newValue = value + util.target.audioEffects[effect];
|
|
|
|
util.target.setAudioEffect(effect, newValue);
|
2016-08-11 16:47:01 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
Scratch3SoundBlocks.prototype.clearEffects = function (args, util) {
|
2017-01-04 18:37:55 -05:00
|
|
|
util.target.audioPlayer.clearEffects();
|
2016-08-11 16:47:01 -04:00
|
|
|
};
|
|
|
|
|
2016-10-27 11:31:22 -04:00
|
|
|
Scratch3SoundBlocks.prototype.setVolume = function (args, util) {
|
|
|
|
var value = Cast.toNumber(args.VOLUME);
|
2017-01-04 18:37:55 -05:00
|
|
|
util.target.audioPlayer.setVolume(value);
|
2016-10-27 11:31:22 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
Scratch3SoundBlocks.prototype.changeVolume = function (args, util) {
|
|
|
|
var value = Cast.toNumber(args.VOLUME);
|
2017-01-04 18:37:55 -05:00
|
|
|
util.target.audioPlayer.changeVolume(value);
|
2016-10-27 11:31:22 -04:00
|
|
|
};
|
|
|
|
|
2017-01-06 10:31:11 -05:00
|
|
|
Scratch3SoundBlocks.prototype.getVolume = function (args, util) {
|
|
|
|
return util.target.audioPlayer.currentVolume;
|
|
|
|
};
|
|
|
|
|
2017-01-09 15:47:29 -05:00
|
|
|
Scratch3SoundBlocks.prototype.setTempo = function (args) {
|
2016-10-27 11:31:22 -04:00
|
|
|
var value = Cast.toNumber(args.TEMPO);
|
2017-01-09 15:47:29 -05:00
|
|
|
this.runtime.audioEngine.setTempo(value);
|
2016-10-27 11:31:22 -04:00
|
|
|
};
|
|
|
|
|
2017-01-09 15:47:29 -05:00
|
|
|
Scratch3SoundBlocks.prototype.changeTempo = function (args) {
|
2016-10-27 11:31:22 -04:00
|
|
|
var value = Cast.toNumber(args.TEMPO);
|
2017-01-09 15:47:29 -05:00
|
|
|
this.runtime.audioEngine.changeTempo(value);
|
2016-10-27 11:31:22 -04:00
|
|
|
};
|
|
|
|
|
2017-01-09 15:47:29 -05:00
|
|
|
Scratch3SoundBlocks.prototype.getTempo = function () {
|
|
|
|
return this.runtime.audioEngine.currentTempo;
|
2017-01-06 10:31:01 -05:00
|
|
|
};
|
|
|
|
|
2016-10-13 11:54:00 -04:00
|
|
|
Scratch3SoundBlocks.prototype.soundsMenu = function (args) {
|
2016-08-11 16:47:01 -04:00
|
|
|
return args.SOUND_MENU;
|
|
|
|
};
|
|
|
|
|
2016-10-13 11:54:00 -04:00
|
|
|
Scratch3SoundBlocks.prototype.beatsMenu = function (args) {
|
|
|
|
return args.BEATS;
|
2016-08-09 15:40:50 -04:00
|
|
|
};
|
|
|
|
|
2016-10-13 11:54:00 -04:00
|
|
|
Scratch3SoundBlocks.prototype.effectsMenu = function (args) {
|
2016-08-11 16:47:01 -04:00
|
|
|
return args.EFFECT;
|
|
|
|
};
|
|
|
|
|
2016-08-09 15:40:50 -04:00
|
|
|
module.exports = Scratch3SoundBlocks;
|