scale to midi note reporter

This commit is contained in:
Eric Rosenbaum 2016-08-17 16:45:01 -04:00
parent f8e460f1b4
commit 5e268d57d3
3 changed files with 44 additions and 2 deletions

View file

@ -325,6 +325,21 @@
</value> </value>
</block> </block>
<block type="sound_scalenotetomidinote">
<value name="NOTE">
<shadow type="math_number">
<field name="NUM">1</field>
</shadow>
</value>
<value name="ROOT">
<shadow type="sound_roots_menu">
</shadow>
</value>
<value name="SCALE">
<shadow type="sound_scales_menu">
</shadow>
</value>
</block>
<block type="sound_setkey"> <block type="sound_setkey">
<value name="ROOT"> <value name="ROOT">

View file

@ -17,6 +17,7 @@ Scratch3SoundBlocks.prototype.getPrimitives = function() {
'sound_stopallsounds': this.stopAllSounds, 'sound_stopallsounds': this.stopAllSounds,
'sound_playnote': this.playNote, 'sound_playnote': this.playNote,
'sound_playnoteforbeats': this.playNoteForBeats, 'sound_playnoteforbeats': this.playNoteForBeats,
'sound_scalenotetomidinote': this.scaleNoteToMidiNote,
'sound_playdrum': this.playDrum, 'sound_playdrum': this.playDrum,
'sound_playdrumforbeats': this.playDrumForBeats, 'sound_playdrumforbeats': this.playDrumForBeats,
'sound_setkey' : this.setKey, 'sound_setkey' : this.setKey,
@ -56,6 +57,31 @@ Scratch3SoundBlocks.prototype.playNote = function (args, util) {
self.postMessage({method: 'playnote', note:args.NOTE}); 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) { Scratch3SoundBlocks.prototype.playDrumForBeats = function (args, util) {
self.postMessage({method: 'playdrumforbeats', drum:args.DRUMTYPE, beats:args.BEATS}); self.postMessage({method: 'playdrumforbeats', drum:args.DRUMTYPE, beats:args.BEATS});
return new Promise(function(resolve) { return new Promise(function(resolve) {

View file

@ -115,8 +115,9 @@ function VirtualMachine () {
}; };
function playNoteForBeats(note, beats) { function playNoteForBeats(note, beats) {
var midiNote = scaleNoteToMidiNote(note, currentScale, rootNote); // var midiNote = scaleNoteToMidiNote(note, currentScale, rootNote);
var freq = midiToFreq(midiNote);
var freq = midiToFreq(note);
synth.triggerAttackRelease(freq, beats, quantizeUnit); synth.triggerAttackRelease(freq, beats, quantizeUnit);
} }