mirror of
https://github.com/scratchfoundation/scratch-vm.git
synced 2025-01-12 07:45:41 -05:00
scale to midi note reporter
This commit is contained in:
parent
f8e460f1b4
commit
5e268d57d3
3 changed files with 44 additions and 2 deletions
|
@ -325,6 +325,21 @@
|
|||
</value>
|
||||
</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">
|
||||
<value name="ROOT">
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue