shithub: neindaw

Download patch

ref: fd3f0be9dc1524bf79cb6f7cb6938aec7e934ffd
parent: 262fbae1b70df3a7883a5c2b8df05e486622559b
author: Sigrid Haflínudóttir <ftrvxmtrx@gmail.com>
date: Sun May 10 19:18:20 EDT 2020

piper: more wip

--- a/piper/ay.c
+++ b/piper/ay.c
@@ -24,9 +24,10 @@
 	char path[1];
 };
 
-static void
+static int
 cmd(void *aux, Cmd *c)
 {
+	return 0;
 }
 
 static void *
--- a/piper/kick.c
+++ b/piper/kick.c
@@ -21,9 +21,35 @@
 	char path[1];
 };
 
-static void
+static int
 cmd(void *aux, Cmd *c)
 {
+	Kick *k;
+
+	k = aux;
+	switch (c->type) {
+	case CmdNote:
+		fprint(k->a.freq, "%g", c->note[0].freq);
+		fprint(k->a.release, "%g", c->note[0].dur);
+		if (c->numnotes > 1) {
+			fprint(k->b.freq, "%g", c->note[1].freq);
+			fprint(k->b.release, "%g", c->note[1].dur);
+			fprint(k->b.enable, "1");
+		} else {
+			fprint(k->b.enable, "0");
+		}
+		fprint(k->gate, "1");
+		break;
+
+	case CmdVol:
+		fprint(k->volume, "%g", c->vol);
+		break;
+
+	case CmdRaw:
+		break;
+	}
+
+	return 0;
 }
 
 static void *
--- a/piper/piper.c
+++ b/piper/piper.c
@@ -1,8 +1,20 @@
 #include <u.h>
 #include <libc.h>
 #include <thread.h>
+#include <bio.h>
 #include "piper.h"
 
+typedef struct Group Group;
+
+struct Group {
+	Synth *synth;
+	void **aux;
+	int numaux;
+};
+
+static Group *groups;
+static int numgroups;
+
 static Synth *synths[] = {
 	&ay_3_8910,
 	&kick_drum,
@@ -18,7 +30,10 @@
 void
 threadmain(int argc, char **argv)
 {
-	int i;
+	char *s;
+	Synth *synth;
+	Biobuf *b;
+	int i, j;
 
 	ARGBEGIN{
 	default:
@@ -28,7 +43,33 @@
 	if (argc < 1)
 		usage();
 
+	quotefmtinstall();
+
+	/* go through all groups */
 	for (i = 0; i < argc; i++) {
+		/* search for specific synth handler by its name */
+		s = smprint("%s/metadata", argv[i]);
+		if ((b = Bopen(s, OREAD)) == nil)
+			sysfatal("%r");
+		free(s);
+		synth = nil;
+		while ((s = Brdline(b, '\n')) != nil) {
+			if (strncmp(s, "name\t", 5) == 0) {
+				s[Blinelen(b)-1] = 0;
+				for (j = 0; j < nelem(synths) && strcmp(synths[j]->name, s+5) != 0; j++);
+				if (j >= nelem(synths))
+					sysfatal("unknown synth %q\n", s+5);
+				if ((groups = realloc(groups, sizeof(Group)*(numgroups+1))) == nil)
+					sysfatal("memory");
+				synth = synths[j];
+				memset(&groups[numgroups], 0, sizeof(Group));
+				groups[numgroups].synth = synth;
+				break;
+			}
+		}
+		Bterm(b);
+		if (synth == nil)
+			sysfatal("no name set in %s/metadata", argv[i]);
 	}
 
 	threadexitsall(nil);
--- a/piper/piper.h
+++ b/piper/piper.h
@@ -3,6 +3,8 @@
 
 enum {
 	CmdNote,
+	CmdVol,
+	CmdRaw,
 };
 
 struct Cmd {
@@ -10,8 +12,10 @@
 	union {
 		struct {
 			float freq;
-			int len;
+			float dur;
 		}note[3];
+		char *raw;
+		float vol;
 	};
 	int numnotes;
 };
@@ -18,7 +22,7 @@
 
 struct Synth {
 	char *name;
-	void (*cmd)(void *aux, Cmd *c);
+	int (*cmd)(void *aux, Cmd *c);
 	void *(*alloc)(char *path);
 };
 
@@ -25,5 +29,6 @@
 extern Synth ay_3_8910;
 extern Synth kick_drum;
 
-float note2freq(int octave, char note);
+int i36(uchar c);
+float note2freq(int octave, uchar note);
 int pathopen(char *path, char *fmt, ...);
--- a/piper/util.c
+++ b/piper/util.c
@@ -2,7 +2,7 @@
 #include <libc.h>
 #include "piper.h"
 
-static int b2i[] = {
+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,
@@ -13,8 +13,14 @@
 #include "a440.h"
 };
 
+int
+i36(uchar c)
+{
+	return b2i[c];
+}
+
 float
-note2freq(int octave, char note)
+note2freq(int octave, uchar note)
 {
 	note -= 'A';
 	if (octave < 0 || octave >= nelem(notes) || note >= nelem(notes[0]))