util.c (999B)
1 #include <u.h> 2 #include <libc.h> 3 4 #include "util.h" 5 6 int 7 rune2note(Rune r) 8 { 9 const Rune *keys1 = L"zsxdcvgbhnjm,l.;/"; 10 const Rune *keys2 = L"q2w3er5t6y7ui9o0p["; 11 Rune *rp; 12 if ((rp = runestrchr(keys1, r)) != nil) return rp - keys1; 13 if ((rp = runestrchr(keys2, r)) != nil) return rp - keys2 + 12; 14 return -1; 15 } 16 17 double note2freq(int n) 18 { 19 20 // TODO: convert to proper caclucations 21 // instead of table lookup and messy pows 22 23 /* 24 * Frequencies for equal-tempered scale 25 * from https://pages.mtu.edu/~suits/notefreqs.html 26 */ 27 28 double freq[] = { 29 261.63, // C-4 30 277.18, 31 293.66, 32 311.13, 33 329.63, 34 349.23, 35 369.99, 36 392.00, 37 415.30, 38 440.00, // A-4 39 466.16, 40 493.88, 41 523.25, // C-5 42 554.37, 43 587.33, 44 622.25, 45 659.25, 46 698.46, 47 739.99, 48 783.99, 49 830.61, 50 880.00, // A-5 51 932.33, 52 987.77, 53 1046.50, 54 1108.73, 55 1174.66, 56 1244.51, 57 1318.51, 58 1396.91 // F-6 59 }; 60 61 int fo = 1 + (n / 12); 62 int fn = n % 12; 63 double f = freq[fn] / pow(2, 4) * pow(2, fo); 64 return f; 65 }