shithub: prismriver

Download patch

ref: b00b726e8f50ce92a60909bced989177bc028c73
parent: beb274816e4ac1c0910e8c7297f2a38c29f8fe37
author: Tevo <[email protected]>
date: Sun Aug 30 21:27:54 EDT 2020

Barebones keyboard demo

diff: cannot open b/demo/keyboard//null: file does not exist: 'b/demo/keyboard//null' diff: cannot open b/demo//null: file does not exist: 'b/demo//null'
--- /dev/null
+++ b/demo/keyboard/main.c
@@ -1,0 +1,82 @@
+#include <u.h>
+#include <libc.h>
+#include <draw.h>
+#include <event.h>
+#include <prismriver.h>
+
+void
+usage(void)
+{
+	print("Usage: %s [-o <output>] [-b <bufsize>]\n", argv0);
+	exits("usage");
+}
+
+//const char map[] = "zsxdcvgbhnjm";
+const char map[] = "qazwsxedcrfvtgbyhnujmik,ol.p;/[']";
+
+int indexof(char *str, char c)
+{
+	for(int i = 0; str[i] != '\0'; i++)
+		if(str[i] == c)
+			return i;
+	return -1;
+}
+
+void
+main(int argc, char** argv)
+{
+	int out = -2, cont = 1, bufsize = 4096;
+	Wavegen *w;
+	ulong clock = 0;
+	Rune e;
+
+	ARGBEGIN {
+	case 'o':
+		out = create(EARGF(usage()), OWRITE, 0644);
+		break;
+	case 'b':
+		bufsize = atoi(EARGF(usage()));
+		break;
+	default:
+		usage();
+	} ARGEND;
+
+	if(out == -2)
+		out = open("/dev/audio", OWRITE);
+
+	if(out == -1)
+		sysfatal("open: %r");
+
+	initdraw(nil, nil, "Prismsynth");
+	einit(Ekeyboard);
+
+	w = waveform(sine, 1.0, 0);
+
+	while(cont)
+		switch(e = ekbd())
+		{
+		case 0x1B:	/* Esc */
+			cont = 0;
+			break;
+		default:
+			{
+				Buffer *buf;
+				int i = indexof(map, (char)e);
+				if(i < 0)
+					continue;
+				buf = buffermap(nil, w, pitch(i, 4), &clock, 0, 0, bufsize);
+				play(buf, out);
+				destroybuffer(buf);
+			}
+		}
+
+	close(out);
+	exits(0);
+}
+
+void
+eresized(int new)
+{
+	if(new)
+		getwindow(display, Refnone);
+}
\ No newline at end of file
--- /dev/null
+++ b/demo/keyboard/mkfile
@@ -1,0 +1,15 @@
+</$objtype/mkfile
+
+OBJ=main.$O
+
+$O.out: $OBJ
+	$LD $OBJ
+
+%.$O: %.c
+	$CC $CFLAGS $stem.c
+
+clean:V:
+	rm -f [$OS].out *.[$OS]
+
+run:V: $O.out
+	$O.out