forked from ja-k-e/noise
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNoteToFrequency.js
39 lines (34 loc) · 990 Bytes
/
NoteToFrequency.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
export default class NoteToFrequency {
constructor() {
this.memo = {};
}
get(note) {
if (this.memo[note]) return this.memo[note];
const noteFrequencyMapping = {
C: -9,
"C#": -8,
D: -7,
"D#": -6,
E: -5,
F: -4,
"F#": -3,
G: -2,
"G#": -1,
A: 0,
"A#": 1,
B: 2,
};
const octave = parseInt(note.slice(-1));
const noteName = note.slice(0, -1);
if (!noteFrequencyMapping.hasOwnProperty(noteName)) {
throw new Error("Invalid note name: " + noteName);
}
// Calculate the half steps from A4 (440Hz)
const halfStepsFromA4 = 12 * (octave - 4) + noteFrequencyMapping[noteName];
// Calculate the frequency using the formula: 440Hz * 2^(n/12), where n is the number of half steps from A4
const frequency = 440 * Math.pow(2, halfStepsFromA4 / 12);
// Save this value so we don't need to recalculate later
this.memo[note] = frequency;
return frequency;
}
}