-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path11-aux.js
48 lines (40 loc) · 1.13 KB
/
11-aux.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
// Auxiliary functions
const notenames = {
0: "C", 1: "C#", 2: "D", 3: "Eb", 4: "E", 5: "F",
6: "F#", 7: "G", 8: "Ab", 9: "A", 10: "Bb", 11: "B"
};
function interval(frequency, semitones) {
return frequency * Math.pow(2, semitones / 12);
}
function midiToFrequency(midinumber, concertA = 440) {
const A4 = 69;
if (midinumber === A4) {
return concertA;
}
let semitones = midinumber - A4;
return interval(440, semitones);
}
function frequencyToMidi(frequency) {
let midinumber = ((12 * Math.log(frequency / 220.0) / Math.log(2.0)) + 57.001);
return midinumber;
}
function noteFromFrequency(frequency, withOctave = false) {
const midinumber = frequencyToMidi(frequency);
const pitchclass = midinumber % 12;
let octave = (midinumber - pitchclass) / 12;
let notename = notenames[Math.round(pitchclass)];
if (withOctave) {
octave--;
notename += octave;
}
return notename;
}
function createThereminSound(waveform) {
return new Pizzicato.Sound({
source: 'wave',
options: {
type: waveform,
frequency: 220
}
});
}