ref: 03bbdd2116025795128eed80b55bfbb2fcdb71c2
parent: 9d432867df46d334be72101ccdb4662e9289eaf4
author: Sigrid Haflínudóttir <[email protected]>
date: Wed May 13 11:38:24 EDT 2020
put utils and a440 to the root dir
--- a/.gitignore
+++ b/.gitignore
@@ -1,4 +1,4 @@
*.6
6.*
*.dsp.c
-piper/a440.h
+a440.h
--- /dev/null
+++ b/a440.txt
@@ -1,0 +1,108 @@
+C0 16.35
+C#0 17.32
+D0 18.35
+D#0 19.45
+E0 20.60
+F0 21.83
+F#0 23.12
+G0 24.50
+G#0 25.96
+A0 27.50
+A#0 29.14
+B0 30.87
+C1 32.70
+C#1 34.65
+D1 36.71
+D#1 38.89
+E1 41.20
+F1 43.65
+F#1 46.25
+G1 49.00
+G#1 51.91
+A1 55.00
+A#1 58.27
+B1 61.74
+C2 65.41
+C#2 69.30
+D2 73.42
+D#2 77.78
+E2 82.41
+F2 87.31
+F#2 92.50
+G2 98.00
+G#2 103.83
+A2 110.00
+A#2 116.54
+B2 123.47
+C3 130.81
+C#3 138.59
+D3 146.83
+D#3 155.56
+E3 164.81
+F3 174.61
+F#3 185.00
+G3 196.00
+G#3 207.65
+A3 220.00
+A#3 233.08
+B3 246.94
+C4 261.63
+C#4 277.18
+D4 293.66
+D#4 311.13
+E4 329.63
+F4 349.23
+F#4 369.99
+G4 392.00
+G#4 415.30
+A4 440.00
+A#4 466.16
+B4 493.88
+C5 523.25
+C#5 554.37
+D5 587.33
+D#5 622.25
+E5 659.25
+F5 698.46
+F#5 739.99
+G5 783.99
+G#5 830.61
+A5 880.00
+A#5 932.33
+B5 987.77
+C6 1046.50
+C#6 1108.73
+D6 1174.66
+D#6 1244.51
+E6 1318.51
+F6 1396.91
+F#6 1479.98
+G6 1567.98
+G#6 1661.22
+A6 1760.00
+A#6 1864.66
+B6 1975.53
+C7 2093.00
+C#7 2217.46
+D7 2349.32
+D#7 2489.02
+E7 2637.02
+F7 2793.83
+F#7 2959.96
+G7 3135.96
+G#7 3322.44
+A7 3520.00
+A#7 3729.31
+B7 3951.07
+C8 4186.01
+C#8 4434.92
+D8 4698.63
+D#8 4978.03
+E8 5274.04
+F8 5587.65
+F#8 5919.91
+G8 6271.93
+G#8 6644.88
+A8 7040.00
+A#8 7458.62
+B8 7902.13
--- a/mkfile
+++ b/mkfile
@@ -15,7 +15,12 @@
default:V: all
-all install clean nuke:VQ: $BIN
+%.h: %.txt
+ ./notegen <$stem.txt >$stem.h
+
+CLEANFILES=a440.h
+
+all install clean nuke:VQ: a440.h $BIN
for (i in $LIBS $CMDS) @{
cd $i
mk $target
--- /dev/null
+++ b/notegen
@@ -1,0 +1,27 @@
+#!/bin/rc
+
+awk '
+ BEGIN {
+ octave = -1
+ }
+
+ /#/ {
+ $1 = tolower($1)
+ sub("#", "")
+ }
+
+ {
+ split($1, n, "")
+ if(n[2] != octave){
+ if(octave >= 0)
+ printf " },\n"
+ printf " [%d] = {\n", n[2]
+ }
+ octave = n[2]
+ printf " [''%s''-''A''] = %s,\n", n[1], $2
+ }
+
+ END {
+ printf " }\n"
+ }
+'
--- a/piper/a440.txt
+++ /dev/null
@@ -1,108 +1,0 @@
-C0 16.35
-C#0 17.32
-D0 18.35
-D#0 19.45
-E0 20.60
-F0 21.83
-F#0 23.12
-G0 24.50
-G#0 25.96
-A0 27.50
-A#0 29.14
-B0 30.87
-C1 32.70
-C#1 34.65
-D1 36.71
-D#1 38.89
-E1 41.20
-F1 43.65
-F#1 46.25
-G1 49.00
-G#1 51.91
-A1 55.00
-A#1 58.27
-B1 61.74
-C2 65.41
-C#2 69.30
-D2 73.42
-D#2 77.78
-E2 82.41
-F2 87.31
-F#2 92.50
-G2 98.00
-G#2 103.83
-A2 110.00
-A#2 116.54
-B2 123.47
-C3 130.81
-C#3 138.59
-D3 146.83
-D#3 155.56
-E3 164.81
-F3 174.61
-F#3 185.00
-G3 196.00
-G#3 207.65
-A3 220.00
-A#3 233.08
-B3 246.94
-C4 261.63
-C#4 277.18
-D4 293.66
-D#4 311.13
-E4 329.63
-F4 349.23
-F#4 369.99
-G4 392.00
-G#4 415.30
-A4 440.00
-A#4 466.16
-B4 493.88
-C5 523.25
-C#5 554.37
-D5 587.33
-D#5 622.25
-E5 659.25
-F5 698.46
-F#5 739.99
-G5 783.99
-G#5 830.61
-A5 880.00
-A#5 932.33
-B5 987.77
-C6 1046.50
-C#6 1108.73
-D6 1174.66
-D#6 1244.51
-E6 1318.51
-F6 1396.91
-F#6 1479.98
-G6 1567.98
-G#6 1661.22
-A6 1760.00
-A#6 1864.66
-B6 1975.53
-C7 2093.00
-C#7 2217.46
-D7 2349.32
-D#7 2489.02
-E7 2637.02
-F7 2793.83
-F#7 2959.96
-G7 3135.96
-G#7 3322.44
-A7 3520.00
-A#7 3729.31
-B7 3951.07
-C8 4186.01
-C#8 4434.92
-D8 4698.63
-D#8 4978.03
-E8 5274.04
-F8 5587.65
-F#8 5919.91
-G8 6271.93
-G#8 6644.88
-A8 7040.00
-A#8 7458.62
-B8 7902.13
--- a/piper/notegen
+++ /dev/null
@@ -1,27 +1,0 @@
-#!/bin/rc
-
-awk '
- BEGIN {
- octave = -1
- }
-
- /#/ {
- $1 = tolower($1)
- sub("#", "")
- }
-
- {
- split($1, n, "")
- if(n[2] != octave){
- if(octave >= 0)
- printf " },\n"
- printf " [%d] = {\n", n[2]
- }
- octave = n[2]
- printf " [''%s''-''A''] = %s,\n", n[1], $2
- }
-
- END {
- printf " }\n"
- }
-'
--- a/piper/util.c
+++ /dev/null
@@ -1,51 +1,0 @@
-#include <u.h>
-#include <libc.h>
-#include "piper.h"
-
-static int b2i[255] = {
- ['0'] = 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
- ['a'] = 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
- ['k'] = 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
- ['u'] = 30, 31, 32, 33, 34, 35,
-};
-
-static float notes[/* octave */]['g'-'A'+1 /* note */] = {
-#include "a440.h"
-};
-
-int
-i36(uchar c)
-{
- return b2i[c];
-}
-
-float
-note2freq(uchar note, uchar octave)
-{
- note -= 'A';
- octave -= '0';
- if (octave >= nelem(notes) || note >= nelem(notes[0]))
- return 0.0;
- return notes[octave][note];
-}
-
-int
-pathopen(char *path, char *fmt, ...)
-{
- va_list arg;
- char *s;
- int fd;
-
- va_start(arg, fmt);
- s = vsmprint(fmt, arg);
- va_end(arg);
-
- if (s == nil || (path = smprint("%s/%s", path, s)) == nil)
- sysfatal("memory");
- free(s);
- if ((fd = open(path, OWRITE)) < 0)
- fprint(2, "%s: %r\n", path);
- free(path);
-
- return fd;
-}
--- /dev/null
+++ b/util.c
@@ -1,0 +1,106 @@
+#include <u.h>
+#include <libc.h>
+
+int usensec = 0;
+
+static int b2i[255] = {
+ ['0'] = 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
+ ['a'] = 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
+ ['k'] = 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
+ ['u'] = 30, 31, 32, 33, 34, 35,
+ ['A'] = 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
+ ['K'] = 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
+ ['U'] = 30, 31, 32, 33, 34, 35,
+};
+
+static float notes[/* octave */]['g'-'A'+1 /* note */] = {
+#include "a440.h"
+};
+
+int
+i36(uchar c)
+{
+ return b2i[c];
+}
+
+float
+note2freq(uchar note, uchar octave)
+{
+ note -= 'A';
+ octave -= '0';
+ if (octave >= nelem(notes) || note >= nelem(notes[0]))
+ return 0.0;
+ return notes[octave][note];
+}
+
+int
+pathopen(char *path, char *fmt, ...)
+{
+ va_list arg;
+ char *s;
+ int fd;
+
+ va_start(arg, fmt);
+ s = vsmprint(fmt, arg);
+ va_end(arg);
+
+ if (s == nil || (path = smprint("%s/%s", path, s)) == nil)
+ sysfatal("memory");
+ free(s);
+ if ((fd = open(path, OWRITE)) < 0)
+ fprint(2, "%s: %r\n", path);
+ free(path);
+
+ return fd;
+}
+
+/*
+ * nsec() is wallclock and can be adjusted by timesync
+ * so need to use cycles() instead, but fall back to
+ * nsec() in case we can't
+ *
+ * "fasthz" is how many ticks there are in a second
+ * can be read from /dev/time
+ *
+ * perhaps using RDTSCP is even better
+ */
+uvlong
+nanosec(void)
+{
+ static uvlong fasthz, xstart;
+ uvlong x, div;
+ int f, n, i;
+ char tmp[128], *e;
+
+ if (fasthz == ~0ULL)
+ return nsec() - xstart;
+
+ if (fasthz == 0) {
+ fasthz = ~0ULL;
+ xstart = nsec();
+ if (usensec)
+ return 0;
+ if ((f = open("/dev/time", OREAD)) >= 0 && (n = read(f, tmp, sizeof(tmp)-1)) > 2) {
+ tmp[n] = 0;
+ e = tmp;
+ for (i = 0; i < 3; i++)
+ strtoll(e, &e, 10);
+ if ((fasthz = strtoll(e, nil, 10)) < 1)
+ fasthz = ~0ULL;
+ else
+ cycles(&xstart);
+ }
+ close(f);
+ if (fasthz == ~0ULL) {
+ fprint(2, "couldn't get fasthz, falling back to nsec()\n");
+ fprint(2, "you might want to disable aux/timesync\n");
+ return 0;
+ }
+ }
+ cycles(&x);
+ x -= xstart;
+
+ for (div = 1000000000ULL; x < 0x1999999999999999ULL && div > 1 ; div /= 10ULL, x *= 10ULL);
+
+ return x / (fasthz / div);
+}
--- /dev/null
+++ b/util.h
@@ -1,0 +1,6 @@
+extern int usensec;
+
+int i36(uchar c);
+float note2freq(uchar note, uchar octave);
+int pathopen(char *path, char *fmt, ...);
+uvlong nanosec(void);