commit cd9a4a89bf37fefa23fc85f97ab421797e803e82
parent e1e23b060a8b2933eddb1a77d192a4467a06d324
Author: rpa <rpa@laika>
Date: Fri, 13 Jan 2023 17:18:53 +0000
wave: add missing util.c
Diffstat:
A | src/wave/util.c | | | 63 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1 file changed, 63 insertions(+), 0 deletions(-)
diff --git a/src/wave/util.c b/src/wave/util.c
@@ -0,0 +1,63 @@
+#include <u.h>
+#include <libc.h>
+
+#include "util.h"
+
+int
+rune2note(Rune r)
+{
+ const Rune *keys1 = L"zsxdcvgbhnjm,l.;/";
+ const Rune *keys2 = L"q2w3er5t6y7ui9o0p[";
+ Rune *rp;
+ if ((rp = runestrchr(keys1, r)) != nil) return rp - keys1;
+ if ((rp = runestrchr(keys2, r)) != nil) return rp - keys2 + 12;
+ return -1;
+}
+
+int note2freq(int n)
+{
+
+ /*
+ * Frequencies for equal-tempered scale
+ * from https://pages.mtu.edu/~suits/notefreqs.html
+ */
+
+ double freq[] = {
+ 261.63, // C-4
+ 277.18,
+ 293.66,
+ 311.13,
+ 329.63,
+ 349.23,
+ 369.99,
+ 392.00,
+ 415.30,
+ 440.00, // A-4
+ 466.16,
+ 493.88,
+ 523.25, // C-5
+ 554.37,
+ 587.33,
+ 622.25,
+ 659.25,
+ 698.46,
+ 739.99,
+ 783.99,
+ 830.61,
+ 880.00, // A-5
+ 932.33,
+ 987.77,
+ 1046.50,
+ 1108.73,
+ 1174.66,
+ 1244.51,
+ 1318.51,
+ 1396.91 // F-6
+ };
+
+ int s = sizeof(freq) / sizeof(double);
+ int fo = 1 + (n / s);
+ int fn = n % s;
+ double f = freq[fn];// / 16 * fo;
+ return f;
+}