diff --git a/playground/index.html b/playground/index.html index bf2a21813..79bd85f5d 100644 --- a/playground/index.html +++ b/playground/index.html @@ -248,17 +248,43 @@ + - - + + + + - - + + + + + + + + + + + + 1 + + + + + + + + + 1 + + + + @@ -266,11 +292,11 @@ - - 0.25 + + @@ -279,6 +305,14 @@ + + + + 1 + + + + @@ -291,17 +325,41 @@ - - - - - - - - - - + + + + + + + + + + + + + + + + + + 100 + + + + + + + + + + + 10 + + + + + diff --git a/playground/sounds/boing.mp3 b/playground/sounds/boing.mp3 new file mode 100644 index 000000000..9542bd23f Binary files /dev/null and b/playground/sounds/boing.mp3 differ diff --git a/playground/sounds/cave.mp3 b/playground/sounds/cave.mp3 new file mode 100644 index 000000000..b5643a240 Binary files /dev/null and b/playground/sounds/cave.mp3 differ diff --git a/playground/sounds/drip_drop.mp3 b/playground/sounds/drip_drop.mp3 new file mode 100644 index 000000000..29177a85e Binary files /dev/null and b/playground/sounds/drip_drop.mp3 differ diff --git a/playground/sounds/drum_machine.mp3 b/playground/sounds/drum_machine.mp3 new file mode 100644 index 000000000..69892f360 Binary files /dev/null and b/playground/sounds/drum_machine.mp3 differ diff --git a/playground/sounds/eggs.mp3 b/playground/sounds/eggs.mp3 new file mode 100644 index 000000000..fa371f1d5 Binary files /dev/null and b/playground/sounds/eggs.mp3 differ diff --git a/playground/sounds/hi_tun_tabla.mp3 b/playground/sounds/hi_tun_tabla.mp3 new file mode 100644 index 000000000..78e4b6475 Binary files /dev/null and b/playground/sounds/hi_tun_tabla.mp3 differ diff --git a/playground/sounds/high_conga.mp3 b/playground/sounds/high_conga.mp3 new file mode 100644 index 000000000..0308959bc Binary files /dev/null and b/playground/sounds/high_conga.mp3 differ diff --git a/playground/sounds/lo_gliss_tabla.mp3 b/playground/sounds/lo_gliss_tabla.mp3 new file mode 100644 index 000000000..baabe4240 Binary files /dev/null and b/playground/sounds/lo_gliss_tabla.mp3 differ diff --git a/playground/sounds/meow.mp3 b/playground/sounds/meow.mp3 new file mode 100644 index 000000000..a16da03be Binary files /dev/null and b/playground/sounds/meow.mp3 differ diff --git a/playground/sounds/sidestick_snare.mp3 b/playground/sounds/sidestick_snare.mp3 new file mode 100644 index 000000000..816010372 Binary files /dev/null and b/playground/sounds/sidestick_snare.mp3 differ diff --git a/playground/sounds/small_cowbell.mp3 b/playground/sounds/small_cowbell.mp3 new file mode 100644 index 000000000..dbfd4e267 Binary files /dev/null and b/playground/sounds/small_cowbell.mp3 differ diff --git a/playground/sounds/snare_drum.mp3 b/playground/sounds/snare_drum.mp3 new file mode 100644 index 000000000..9dc6db7ab Binary files /dev/null and b/playground/sounds/snare_drum.mp3 differ diff --git a/playground/sounds/splash cymbal.mp3 b/playground/sounds/splash cymbal.mp3 new file mode 100644 index 000000000..3026cb559 Binary files /dev/null and b/playground/sounds/splash cymbal.mp3 differ diff --git a/playground/sounds/zoop.mp3 b/playground/sounds/zoop.mp3 new file mode 100644 index 000000000..f8af12d28 Binary files /dev/null and b/playground/sounds/zoop.mp3 differ diff --git a/src/blocks/scratch3_sound.js b/src/blocks/scratch3_sound.js index 0c78f0276..300b02fd2 100644 --- a/src/blocks/scratch3_sound.js +++ b/src/blocks/scratch3_sound.js @@ -13,20 +13,38 @@ function Scratch3SoundBlocks(runtime) { Scratch3SoundBlocks.prototype.getPrimitives = function() { return { 'sound_playsound': this.playSound, - 'sound_playnoteforbeats': this.playNote, + 'sound_playwithpitch': this.playSoundWithPitch, + 'sound_stopallsounds': this.stopAllSounds, + 'sound_playnote': this.playNote, + 'sound_playnoteforbeats': this.playNoteForBeats, + 'sound_playdrum': this.playDrum, + 'sound_playdrumforbeats': this.playDrumForBeats, 'sound_setkey' : this.setKey, + 'sound_seteffectto' : this.setEffect, + 'sound_changeeffectby' : this.changeEffect, + 'sound_cleareffects' : this.clearEffects, 'sound_scales_menu' : this.scalesMenu, + 'sound_sounds_menu' : this.soundsMenu, 'sound_roots_menu' : this.rootsMenu, 'sound_beats_menu' : this.beatsMenu, + 'sound_effects_menu' : this.effectsMenu, }; }; Scratch3SoundBlocks.prototype.playSound = function (args, util) { - self.postMessage({method: 'beep'}); + self.postMessage({method: 'playsound', soundnum:args.SOUND_NUM}); }; -Scratch3SoundBlocks.prototype.playNote = function (args, util) { - self.postMessage({method: 'playnote', note:args.NOTE, beats:args.BEATS}); +Scratch3SoundBlocks.prototype.playSoundWithPitch = function (args, util) { + self.postMessage({method: 'playsoundwithpitch', soundnum:args.SOUND_NUM, pitch:args.PITCH}); +}; + +Scratch3SoundBlocks.prototype.stopAllSounds = function (args, util) { + self.postMessage({method: 'stopallsounds'}); +}; + +Scratch3SoundBlocks.prototype.playNoteForBeats = function (args, util) { + self.postMessage({method: 'playnoteforbeats', note:args.NOTE, beats:args.BEATS}); return new Promise(function(resolve) { setTimeout(function() { resolve(); @@ -34,10 +52,43 @@ Scratch3SoundBlocks.prototype.playNote = function (args, util) { }); }; +Scratch3SoundBlocks.prototype.playNote = function (args, util) { + self.postMessage({method: 'playnote', note:args.NOTE}); +}; + +Scratch3SoundBlocks.prototype.playDrumForBeats = function (args, util) { + self.postMessage({method: 'playdrumforbeats', drum:args.DRUMTYPE, beats:args.BEATS}); + return new Promise(function(resolve) { + setTimeout(function() { + resolve(); + }, 1000 * args.BEATS); + }); +}; + +Scratch3SoundBlocks.prototype.playDrum = function (args, util) { + self.postMessage({method: 'playdrum', drum:args.DRUMTYPE}); +}; + Scratch3SoundBlocks.prototype.setKey = function (args, util) { self.postMessage({method: 'setkey', root:args.ROOT, scale:args.SCALE}); }; +Scratch3SoundBlocks.prototype.setEffect = function (args, util) { + self.postMessage({method: 'seteffect', effect:args.EFFECT, value:args.VALUE}); +}; + +Scratch3SoundBlocks.prototype.changeEffect = function (args, util) { + self.postMessage({method: 'changeeffect', effect:args.EFFECT, value:args.VALUE}); +}; + +Scratch3SoundBlocks.prototype.clearEffects = function (args, util) { + self.postMessage({method: 'cleareffects'}); +}; + +Scratch3SoundBlocks.prototype.soundsMenu = function (args, util) { + return args.SOUND_MENU; +}; + Scratch3SoundBlocks.prototype.scalesMenu = function (args, util) { return args.SCALE; }; @@ -50,4 +101,8 @@ Scratch3SoundBlocks.prototype.beatsMenu = function (args, util) { return args.BEATS; }; +Scratch3SoundBlocks.prototype.effectsMenu = function (args, util) { + return args.EFFECT; +}; + module.exports = Scratch3SoundBlocks; diff --git a/src/worker.js b/src/worker.js index b154664c2..1a92532d4 100644 --- a/src/worker.js +++ b/src/worker.js @@ -13,10 +13,72 @@ function VirtualMachine () { instance.vmWorker = new Worker('../vm.js'); // MUSIC STUFF by ericr - var options = {modulationEnvelope:{attack:0.1}}; + + // tone setup + + var tone = new Tone(); + + // effects + + var delay = new Tone.FeedbackDelay(0.25, 0.5); + delay.wet.value = 0; + + var pitchShift = new Tone.PitchShift(); + + var panner = new Tone.Panner(0); + + var reverb = new Tone.Freeverb(); + reverb.wet.value = 0; + + Tone.Master.chain(delay, pitchShift, panner, reverb); + + // synth setup for play note block + var synth = new Tone.PolySynth(6, Tone.Synth).toMaster(); - var tone = new Tone(); + // drum sounds + + var drumFileNames = ['high_conga', 'small_cowbell', 'snare_drum', 'splash cymbal']; + var drumSamplers = loadSoundFiles(drumFileNames); + + // sounds + + var soundFileNames = ['meow', 'boing', 'cave', 'drip_drop', 'drum_machine', 'eggs', 'zoop']; + var soundSamplers = loadSoundFiles(soundFileNames); + + // polyphonic samplers + + function loadSoundFiles(filenames) { + var samplers = []; + + for (var name of filenames) { + + var myVoices = []; + for (var i=0; i<6; i++) { + var p = new Tone.Sampler('sounds/' + name + '.mp3').toMaster(); + myVoices.push(p); + } + + var polySampler = { + voices : myVoices, + currentVoice : 0, + nextVoice : function() {return this.voices[this.currentVoice++ % this.voices.length];}, + stopAllVoices : function() {for (var i=0;i