shithub: monome

Download patch

ref: a007855f2e510b6bac417fbf0d21527e54158466
parent: b657fb427237c04806baab51f72bbf971d32339f
author: glenda <glenda@9front.local>
date: Thu Nov 26 22:09:47 EST 2020

initial commit

--- /dev/null
+++ b/balance.c
@@ -1,0 +1,93 @@
+#include <u.h>
+#include <libc.h>
+#include <draw.h>
+#include <event.h>
+#include <thread.h>
+
+#define RATE 44100
+
+int rate;
+double bal;
+Point c;
+
+void
+balance(unsigned char *buf, int len)
+{
+	short l, r;
+	int i;
+
+	for(i = 0; i < len; i += 4){
+		l = buf[i+0] | (buf[i+1] << 8);
+		r = buf[i+2] | (buf[i+3] << 8);
+
+		l *= 1.0 - bal;
+		r *= 1.0 + bal;
+
+		buf[i+0] = l & 0xFF;
+		buf[i+1] = (l >> 8) & 0xFF;
+		buf[i+2] = r & 0xFF;
+		buf[i+3] = (r >> 8) & 0xFF;
+	}
+}
+
+void
+doproc(void *unused)
+{
+	unsigned char *buf = malloc(rate * 4);
+	int r;
+
+	while((r = read(0, buf, rate * 4)) > 0){
+		balance(buf, r);
+		write(1, buf, r);
+	}
+}
+
+void
+redraw(Image *screen)
+{
+	int x = c.x + (c.x * bal);
+
+	draw(screen, screen->r, display->white, nil, ZP);
+
+	draw(screen, Rect(screen->r.min.x + x - 1, screen->r.min.y,
+					screen->r.min.x + x + 1, screen->r.max.y),
+				display->black, nil, ZP);
+}
+
+void
+eresized(int new)
+{
+	if(new && getwindow(display, Refnone) < 0)
+		fprint(2,"can't reattach to window");
+	c = divpt(subpt(screen->r.max, screen->r.min), 2);
+	redraw(screen);
+}
+
+void
+threadmain(int argc, char **argv)
+{
+	Mouse m;
+	Point xy;
+	rate = RATE;
+	bal = 0.0;
+
+	if (argc > 1)
+		rate = atoi(argv[1]);
+
+	if(initdraw(0, 0, "balance") < 0)
+		sysfatal("initdraw: %r");
+	einit(Emouse);
+
+	eresized(0);
+	proccreate(doproc, nil, mainstacksize);
+
+	for(;;m = emouse()){
+		if(m.buttons & 1){
+			xy = subpt(m.xy, screen->r.min);
+
+			bal = (xy.x - c.x) / (double)c.x;
+			
+			redraw(screen);
+		}
+	}
+}
--- /dev/null
+++ b/monome.c
@@ -1,0 +1,168 @@
+#include <u.h>
+#include <libc.h>
+#include <thread.h>
+#include <draw.h>
+#include <event.h>
+#include <keyboard.h>
+#include <fcall.h>
+#include <9p.h>
+
+int mainstacksize = 0x10000;
+
+Image *back;
+Rectangle buttons[16][16];
+char buttonstate[16][16];
+int redrawfds[2];
+
+void
+redraw(Image *screen)
+{
+	int i, j;
+
+	draw(screen, screen->r, back, nil, ZP);
+
+	for(i = 0; i < 16; i++)
+		for(j = 0; j < 16; j++)
+			draw(screen, rectaddpt(buttons[i][j], screen->r.min),
+				buttonstate[i][j]? display->white: display->black,
+				nil, ZP);
+}
+
+void
+resize(void)
+{
+	int fd = open("/dev/wctl", OWRITE);
+	if(fd >= 0){
+		fprint(fd, "resize -dx 650 -dy 650\n");
+		close(fd);
+	}
+}
+
+void
+eresized(int new)
+{
+	if(new && getwindow(display, Refnone) < 0)
+		fprint(2,"can't reattach to window");
+	resize();
+	redraw(screen);
+}
+
+void
+fswrite(Req *r)
+{
+	int i, j, k;
+	int count;
+
+	count = r->ifcall.count;
+
+	if (count < (6*16)) {
+		respond(r, "short write");
+		return;
+	}
+
+	for(i = 0; i < 16; i++){
+		k = atoi(&r->ifcall.data[i*6]);
+
+		for(j = 0; j < 16; j++){
+			buttonstate[i][j] = (k & (1<<j))? 1: 0;
+		}
+	}
+
+	write(redrawfds[1], "\0", 1);
+
+	r->ofcall.count = count;
+	respond(r, nil);
+}
+
+void
+fsread(Req *r)
+{
+	int i, j, k;
+	int count, off;
+	char buf[6*16+2];
+
+	count = r->ifcall.count;
+	off = r->ifcall.offset % (6*16+2);
+
+	if (count == 0) {
+		r->ofcall.count = 0;
+		respond(r, nil);
+		return;
+	}
+
+	for(i = 0; i < 16; i++){
+		k = 0;
+		for(j = 0; j < 16; j++){
+			k |= buttonstate[i][j] << j;
+		}
+
+		sprint(&buf[i*6], "%5d ", k);
+	}
+
+	buf[6*16+0] = '\n';
+	buf[6*16+1] = '\0';
+
+	if (count > (6*16+2))
+		count = 6*16+2;
+
+	memcpy(r->ofcall.data, buf + off, count);
+	r->ofcall.count = count;
+
+	respond(r, nil);
+}
+
+Srv fs = {
+	.write	= fswrite,
+	.read	= fsread,
+};
+
+void
+threadmain(int argc, char **argv)
+{
+	int i, j;
+	Event e;
+	Point xy;
+	int key;
+
+	if(initdraw(0, 0, "monome") < 0)
+		sysfatal("initdraw: %r");
+
+	back = allocimage(display, Rect(0,0,1,1), CMAP8, 1, 0x777777FF);
+	if (back == nil)
+		sysfatal("allocimage: %r");
+	for(i = 0; i < 16; i++) {
+		memset(buttonstate[i], '\0', 16);
+		for(j = 0; j < 16; j++)
+			buttons[i][j] = insetrect(Rect(i*40, j*40, (i+1)*40, (j+1)*40), 3);
+	}
+	eresized(0);
+	einit(Emouse|Ekeyboard);
+	pipe(redrawfds);
+	estart(0, redrawfds[0], 1);
+
+	fs.tree = alloctree(nil, nil, DMDIR|0777, nil);
+	createfile(fs.tree->root, "buttons", nil, 0666, nil);
+	threadpostmountsrv(&fs, "monome", "/mnt/monome", 0);
+
+	for(;;){
+READ:
+		redraw(screen);
+		switch(event(&e)){
+		case Emouse:
+			if(e.mouse.buttons & 1){
+				xy = subpt(e.mouse.xy, screen->r.min);
+				for(i = 0; i < 16; i++)
+					for(j = 0; j < 16; j++)
+						if(ptinrect(xy, buttons[i][j])){
+							buttonstate[i][j] = !buttonstate[i][j];
+							goto READ;
+						}
+			}
+			break;
+		case Ekeyboard:
+			if(e.kbdc == Kdel)
+				threadexitsall(nil);
+			break;
+		}
+	}
+}
--- /dev/null
+++ b/reverb.c
@@ -1,0 +1,111 @@
+#include <u.h>
+#include <libc.h>
+#include <draw.h>
+#include <event.h>
+#include <thread.h>
+
+#define RATE 44100
+
+int rate;
+double decay;
+int delay;
+Point c;
+
+void
+reverb(unsigned char *buf, short *tmp, int len)
+{
+	short l, r;
+	int i;
+	int dur = delay;
+
+	for(i = 0; i < len; i += 4){
+		l = buf[i+0] | (buf[i+1] << 8);
+		r = buf[i+2] | (buf[i+3] << 8);
+
+		l += tmp[(i/2+0+dur)%len] * decay;
+		r += tmp[(i/2+1+dur)%len] * decay;
+
+		tmp[(i/2+0)%len] = l;
+		tmp[(i/2+1)%len] = r;
+
+		buf[i+0] = l & 0xFF;
+		buf[i+1] = (l >> 8) & 0xFF;
+		buf[i+2] = r & 0xFF;
+		buf[i+3] = (r >> 8) & 0xFF;
+	}
+}
+
+void
+doproc(void *unused)
+{
+	unsigned char *buf = malloc(rate * 4);
+	short *tmp = malloc(rate * 2);
+	int r;
+
+	memset(tmp, 0, rate * 2);
+
+	while((r = read(0, buf, rate * 4)) > 0){
+		reverb(buf, tmp, r);
+		write(1, buf, r);
+	}
+}
+
+void
+redraw(Image *screen)
+{
+	int x = delay;
+
+	draw(screen, screen->r, display->white, nil, ZP);
+
+	draw(screen, Rect(screen->r.min.x + x - 1, screen->r.min.y,
+					screen->r.min.x + x + 1, screen->r.max.y - Dy(screen->r)/2),
+				display->black, nil, ZP);
+
+	x = decay * Dx(screen->r);
+
+	draw(screen, Rect(screen->r.min.x + x - 1, screen->r.min.y + Dy(screen->r)/2,
+					screen->r.min.x + x + 1, screen->r.max.y),
+				display->black, nil, ZP);
+}
+
+void
+eresized(int new)
+{
+	if(new && getwindow(display, Refnone) < 0)
+		fprint(2,"can't reattach to window");
+	c = divpt(subpt(screen->r.max, screen->r.min), 2);
+	redraw(screen);
+}
+
+void
+threadmain(int argc, char **argv)
+{
+	Mouse m;
+	Point xy;
+	rate = RATE;
+	delay = 0;
+	decay = 0.0;
+
+	if (argc > 1)
+		rate = atoi(argv[1]);
+
+	if(initdraw(0, 0, "reverb") < 0)
+		sysfatal("initdraw: %r");
+	einit(Emouse);
+
+	eresized(0);
+	proccreate(doproc, nil, mainstacksize);
+
+	for(;;m = emouse()){
+		if(m.buttons & 1){
+			xy = subpt(m.xy, screen->r.min);
+
+			if (xy.y > c.y)
+				decay = xy.x / (double)Dx(screen->r);
+			else
+				delay = xy.x;
+			
+			redraw(screen);
+		}
+	}
+}
--- /dev/null
+++ b/volume.c
@@ -1,0 +1,93 @@
+#include <u.h>
+#include <libc.h>
+#include <draw.h>
+#include <event.h>
+#include <thread.h>
+
+#define RATE 44100
+
+int rate;
+double vol;
+Point c;
+
+void
+volume(unsigned char *buf, int len)
+{
+	short l, r;
+	int i;
+
+	for(i = 0; i < len; i += 4){
+		l = buf[i+0] | (buf[i+1] << 8);
+		r = buf[i+2] | (buf[i+3] << 8);
+
+		l *= vol;
+		r *= vol;
+
+		buf[i+0] = l & 0xFF;
+		buf[i+1] = (l >> 8) & 0xFF;
+		buf[i+2] = r & 0xFF;
+		buf[i+3] = (r >> 8) & 0xFF;
+	}
+}
+
+void
+doproc(void *unused)
+{
+	unsigned char *buf = malloc(rate * 4);
+	int r;
+
+	while((r = read(0, buf, rate * 4)) > 0){
+		volume(buf, r);
+		write(1, buf, r);
+	}
+}
+
+void
+redraw(Image *screen)
+{
+	int x = c.x * vol;
+
+	draw(screen, screen->r, display->white, nil, ZP);
+
+	draw(screen, Rect(screen->r.min.x + x - 1, screen->r.min.y,
+					screen->r.min.x + x + 1, screen->r.max.y),
+				display->black, nil, ZP);
+}
+
+void
+eresized(int new)
+{
+	if(new && getwindow(display, Refnone) < 0)
+		fprint(2,"can't reattach to window");
+	c = divpt(subpt(screen->r.max, screen->r.min), 2);
+	redraw(screen);
+}
+
+void
+threadmain(int argc, char **argv)
+{
+	Mouse m;
+	Point xy;
+	rate = RATE;
+	vol = 1.0;
+
+	if (argc > 1)
+		rate = atoi(argv[1]);
+
+	if(initdraw(0, 0, "volume") < 0)
+		sysfatal("initdraw: %r");
+	einit(Emouse);
+
+	eresized(0);
+	proccreate(doproc, nil, mainstacksize);
+
+	for(;;m = emouse()){
+		if(m.buttons & 1){
+			xy = subpt(m.xy, screen->r.min);
+
+			vol = xy.x / (double)c.x;
+			
+			redraw(screen);
+		}
+	}
+}