From 5e268d57d354059879d07de2a34082dfb33d8450 Mon Sep 17 00:00:00 2001 From: Eric Rosenbaum Date: Wed, 17 Aug 2016 16:45:01 -0400 Subject: [PATCH] scale to midi note reporter --- playground/index.html | 15 +++++++++++++++ src/blocks/scratch3_sound.js | 26 ++++++++++++++++++++++++++ src/worker.js | 5 +++-- 3 files changed, 44 insertions(+), 2 deletions(-) diff --git a/playground/index.html b/playground/index.html index 9782a7f6a..d233b1030 100644 --- a/playground/index.html +++ b/playground/index.html @@ -325,6 +325,21 @@ + + + + 1 + + + + + + + + + + + diff --git a/src/blocks/scratch3_sound.js b/src/blocks/scratch3_sound.js index 5f5e4b5d6..ecd326766 100644 --- a/src/blocks/scratch3_sound.js +++ b/src/blocks/scratch3_sound.js @@ -17,6 +17,7 @@ Scratch3SoundBlocks.prototype.getPrimitives = function() { 'sound_stopallsounds': this.stopAllSounds, 'sound_playnote': this.playNote, 'sound_playnoteforbeats': this.playNoteForBeats, + 'sound_scalenotetomidinote': this.scaleNoteToMidiNote, 'sound_playdrum': this.playDrum, 'sound_playdrumforbeats': this.playDrumForBeats, 'sound_setkey' : this.setKey, @@ -56,6 +57,31 @@ Scratch3SoundBlocks.prototype.playNote = function (args, util) { self.postMessage({method: 'playnote', note:args.NOTE}); }; +Scratch3SoundBlocks.prototype.scaleNoteToMidiNote = function (args, util) { + + var root = parseInt(args.ROOT) + 60; + + var scales = { + 'MAJOR' : [0,2,4,5,7,9,11], + 'MINOR' : [0,2,3,5,7,8,10], + 'PENTATONIC': [0, 2, 4, 7, 9], + 'CHROMATIC' : [0,1,2,3,4,5,6,7,8,9,10,11], + }; + + var scale = scales[args.SCALE]; + + var scaleNote = args.NOTE; + + var scaleIndex = (Math.round(scaleNote) - 1) % scale.length; + if (scaleIndex < 0) { + scaleIndex += scale.length; + } + var octave = Math.floor((scaleNote - 1) / scale.length); + var midiNote = root + (octave * 12) + scale[scaleIndex]; + + return midiNote; +}; + Scratch3SoundBlocks.prototype.playDrumForBeats = function (args, util) { self.postMessage({method: 'playdrumforbeats', drum:args.DRUMTYPE, beats:args.BEATS}); return new Promise(function(resolve) { diff --git a/src/worker.js b/src/worker.js index bb054d603..b427fd617 100644 --- a/src/worker.js +++ b/src/worker.js @@ -115,8 +115,9 @@ function VirtualMachine () { }; function playNoteForBeats(note, beats) { - var midiNote = scaleNoteToMidiNote(note, currentScale, rootNote); - var freq = midiToFreq(midiNote); + // var midiNote = scaleNoteToMidiNote(note, currentScale, rootNote); + + var freq = midiToFreq(note); synth.triggerAttackRelease(freq, beats, quantizeUnit); }