fix: piano now works with midi numbers

This commit is contained in:
Felix Roos
2022-06-07 01:38:12 +02:00
parent edf2fb9781
commit 1339bc3b94
2 changed files with 11 additions and 1 deletions
+6
View File
@@ -31,6 +31,12 @@ export const fromMidi = (n) => {
return Math.pow(2, (n - 69) / 12) * 440;
};
export const midi2note = (n) => {
const oct = Math.floor(n / 12) - 1;
const pc = ['C', 'Db', 'D', 'Eb', 'E', 'F', 'Gb', 'G', 'Ab', 'A', 'Bb', 'B'][n % 12];
return pc + oct;
};
// modulo that works with negative numbers e.g. mod(-1, 3) = 2
// const mod = (n: number, m: number): number => (n < 0 ? mod(n + m, m) : n % m);
export const mod = (n, m) => ((n % m) + m) % m;
+5 -1
View File
@@ -62,7 +62,11 @@ Pattern.prototype.tone = function (instrument) {
} else if (instrument instanceof NoiseSynth) {
instrument.triggerAttackRelease(hap.duration.valueOf(), time); // noise has no value
} else if (instrument instanceof Piano) {
note = getPlayableNoteValue(hap);
// note = getPlayableNoteValue(hap); // piano does not understand frequencies
note = hap.value;
if (typeof note === 'number') {
note = midi2note(note);
}
instrument.keyDown({ note, time, velocity });
instrument.keyUp({ note, time: time + hap.duration.valueOf(), velocity });
} else if (instrument instanceof Sampler) {