-
Notifications
You must be signed in to change notification settings - Fork 0
/
noteToFreq.js
48 lines (39 loc) · 1.15 KB
/
noteToFreq.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
function parseNote(note){
if(["#", "b", "♯", "♭"].includes(note[1])){
return { letter: note.slice(0,2), octave: parseInt(note.slice(2))||null };
}else{
return { letter: note[0], octave: parseInt(note.slice(1))||null };
}
}
noteMap = [];
octave = -2;
notesSharp = "C C# D D# E F F# G G# A A# B".split(" ");
notesFlat = "C Db D Eb E F Gb G Ab A Bb B".split(" ");
notes = "CC CD DD DE EE FF FG GG GA AA AB BB".split(" ");
function normalizeLetter(letter){;
switch(letter[1]){
case "#":
case "♯":
return notes[notesSharp.indexOf(letter)];
break;
case "b":
case "♭":
return notes[notesFlat.indexOf(letter)];
break;
default: //undefined
return letter.repeat(2);
break;
}
}
while(octave++ < 9) noteMap.push(...(notes.map(letter=>(letter+octave))));
function noteToMidi(note){
const {letter,octave} = parseNote(note);
return noteMap.indexOf(normalizeLetter(letter)+octave);
}
function ntf(note, tune = "A4=440"){
tune = tune.split("=");
tune = {note: noteToMidi(tune[0], pitch: parseInt(tune[1])};
note = noteToMidi(note);
interval = note - tune.note;
return interval
}