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
|
|
|
var Promise = require('promise');
|
|
|
|
|
2016-08-09 15:40:50 -04:00
|
|
|
function Scratch3SoundBlocks(runtime) {
|
|
|
|
/**
|
|
|
|
* The runtime instantiating this block package.
|
|
|
|
* @type {Runtime}
|
|
|
|
*/
|
|
|
|
this.runtime = runtime;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieve the block primitives implemented by this package.
|
|
|
|
* @return {Object.<string, Function>} Mapping of opcode to Function.
|
|
|
|
*/
|
|
|
|
Scratch3SoundBlocks.prototype.getPrimitives = function() {
|
|
|
|
return {
|
2016-09-28 16:42:25 -04:00
|
|
|
'sound_play': this.playSound,
|
2016-09-27 17:09:53 -04:00
|
|
|
'sound_playuntildone': this.playSoundAndWait,
|
2016-08-11 16:47:01 -04:00
|
|
|
'sound_stopallsounds': this.stopAllSounds,
|
|
|
|
'sound_playnoteforbeats': this.playNoteForBeats,
|
2016-10-19 15:44:30 -04:00
|
|
|
'sound_playthereminforbeats': this.playThereminForBeats,
|
2016-08-11 16:47:01 -04:00
|
|
|
'sound_playdrumforbeats': this.playDrumForBeats,
|
2016-10-19 15:44:30 -04:00
|
|
|
'sound_setinstrumentto': this.setInstrument,
|
2016-08-11 16:47:01 -04:00
|
|
|
'sound_seteffectto' : this.setEffect,
|
|
|
|
'sound_changeeffectby' : this.changeEffect,
|
|
|
|
'sound_cleareffects' : this.clearEffects,
|
|
|
|
'sound_sounds_menu' : this.soundsMenu,
|
2016-08-09 15:40:50 -04:00
|
|
|
'sound_beats_menu' : this.beatsMenu,
|
2016-10-27 11:31:22 -04:00
|
|
|
'sound_effects_menu' : this.effectsMenu,
|
|
|
|
'sound_setvolumeto' : this.setVolume,
|
|
|
|
'sound_changevolumeby' : this.changeVolume,
|
|
|
|
'sound_sound_settempotobpm' : this.setTempo,
|
|
|
|
'sound_changetempoby' : this.changeTempo
|
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);
|
|
|
|
util.target.audioEngine.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);
|
2016-10-27 11:31:22 -04:00
|
|
|
return util.target.audioEngine.playSound(index);
|
2016-10-17 17:16:55 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
Scratch3SoundBlocks.prototype._getSoundIndex = function (soundName, util) {
|
2016-09-28 16:42:25 -04: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;
|
2016-10-24 17:00:51 -04:00
|
|
|
if (Number(soundName)) {
|
|
|
|
soundName = Number(soundName);
|
|
|
|
}
|
2016-09-28 16:42:25 -04:00
|
|
|
if (typeof soundName === 'number') {
|
2016-10-13 11:54:00 -04:00
|
|
|
var len = util.target.sprite.sounds.length;
|
|
|
|
index = MathUtil.wrapClamp(soundName,0,len-1);
|
2016-09-28 16:42:25 -04:00
|
|
|
} else {
|
|
|
|
index = util.target.getSoundIndexByName(soundName);
|
|
|
|
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) {
|
2016-10-12 13:17:51 -04:00
|
|
|
util.target.audioEngine.stopAllSounds();
|
2016-08-11 16:47:01 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
Scratch3SoundBlocks.prototype.playNoteForBeats = function (args, util) {
|
2016-10-12 13:17:51 -04:00
|
|
|
util.target.audioEngine.playNoteForBeats(args.NOTE, args.BEATS);
|
2016-09-13 17:52:54 -04:00
|
|
|
return new Promise(function(resolve) {
|
2016-10-13 11:54:00 -04:00
|
|
|
setTimeout(function() {
|
|
|
|
resolve();
|
|
|
|
}, (1000 * args.BEATS) );
|
|
|
|
});
|
2016-08-17 16:45:01 -04:00
|
|
|
};
|
|
|
|
|
2016-10-19 15:44:30 -04:00
|
|
|
Scratch3SoundBlocks.prototype.playThereminForBeats = function (args, util) {
|
|
|
|
util.target.audioEngine.playThereminForBeats(args.NOTE, args.BEATS);
|
|
|
|
return new Promise(function(resolve) {
|
|
|
|
setTimeout(function() {
|
|
|
|
resolve();
|
|
|
|
}, (1000 * args.BEATS) );
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2016-08-11 16:47:01 -04:00
|
|
|
Scratch3SoundBlocks.prototype.playDrumForBeats = function (args, util) {
|
2016-10-12 13:17:51 -04:00
|
|
|
util.target.audioEngine.playDrumForBeats(args.DRUMTYPE, args.BEATS);
|
2016-08-11 16:47:01 -04:00
|
|
|
return new Promise(function(resolve) {
|
|
|
|
setTimeout(function() {
|
|
|
|
resolve();
|
2016-08-16 16:32:35 -04:00
|
|
|
}, (1000 * args.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);
|
|
|
|
return util.target.audioEngine.setInstrument(instNum);
|
|
|
|
};
|
|
|
|
|
2016-08-11 16:47:01 -04:00
|
|
|
Scratch3SoundBlocks.prototype.setEffect = function (args, util) {
|
2016-10-27 11:31:22 -04:00
|
|
|
var value = Cast.toNumber(args.VALUE);
|
|
|
|
util.target.audioEngine.setEffect(args.EFFECT, value);
|
2016-08-11 16:47:01 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
Scratch3SoundBlocks.prototype.changeEffect = function (args, util) {
|
2016-10-27 11:31:22 -04:00
|
|
|
var value = Cast.toNumber(args.VALUE);
|
|
|
|
util.target.audioEngine.changeEffect(args.EFFECT, value);
|
2016-08-11 16:47:01 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
Scratch3SoundBlocks.prototype.clearEffects = function (args, util) {
|
2016-10-12 13:17:51 -04:00
|
|
|
util.target.audioEngine.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);
|
|
|
|
util.target.audioEngine.setVolume(value);
|
|
|
|
};
|
|
|
|
|
|
|
|
Scratch3SoundBlocks.prototype.changeVolume = function (args, util) {
|
|
|
|
var value = Cast.toNumber(args.VOLUME);
|
|
|
|
util.target.audioEngine.changeVolume(value);
|
|
|
|
};
|
|
|
|
|
|
|
|
Scratch3SoundBlocks.prototype.setTempo = function (args, util) {
|
|
|
|
var value = Cast.toNumber(args.TEMPO);
|
|
|
|
util.target.audioEngine.setTempo(value);
|
|
|
|
};
|
|
|
|
|
|
|
|
Scratch3SoundBlocks.prototype.changeTempo = function (args, util) {
|
|
|
|
var value = Cast.toNumber(args.TEMPO);
|
|
|
|
util.target.audioEngine.changeTempo(value);
|
|
|
|
};
|
|
|
|
|
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;
|