ref: 969f5bea56cff8f903f4535114b28ce2dc25d31b
parent: 44a4c5bb1420982b6453aba98810e14f4760ef78
author: Sigrid Haflínudóttir <ftrvxmtrx@gmail.com>
date: Sat Feb 22 06:51:29 EST 2020
plan9: add "midi" command and do not write to stdout by default
--- a/orca.man
+++ b/orca.man
@@ -194,6 +194,9 @@
.B udp NUMBER
Set UDP destination port.
.TP
+.TP
+.B midi PATH
+Set MIDI output path.
.B print
Print selected rectangle to
.IR stdout .
--- a/plan9.c
+++ b/plan9.c
@@ -59,6 +59,12 @@
static Point rulers = {8, 8};
static int rulerstyle = Sfancy, dotstyle = Sfancy;
+static char *ip, *udpport;
+static int udp = -1;
+
+static char *midipath;
+static int midi = -1;
+
static Rune *linebuf;
static Rune cursor = '@';
static vlong tick;
@@ -72,8 +78,6 @@
static bool altdown, shiftdown, ctldown, pause, forward;
static int mode = Minsert;
static long framedev; /* frame deviation in ms */
-static char *ip, *udpport;
-static int udp = -1;
static char *shellcmd;
static struct {
@@ -249,13 +253,15 @@
}
static void
-midiopen(void)
+midiopen(char *path)
{
if (midi >= 0) {
close(midi);
midi = -1;
}
- if (midi < 0 && (midi = open(midipath, OWRITE)) < 0) {
+ free(midipath);
+ midipath = strdup(path);
+ if (midi < 0 && midipath[0] && (midi = open(midipath, OWRITE)) < 0) {
fprint(2, "midi failed: %r\n");
/* FIXME display error */
}
@@ -262,13 +268,21 @@
}
static void
-netdial(void)
+netdial(char *newip, char *newudpport)
{
if (udp >= 0) {
close(udp);
udp = -1;
}
- if (udp < 0 && (udp = dial(netmkaddr(ip, "udp", udpport), nil, nil, nil)) < 0) {
+ if (newip != nil) {
+ free(ip);
+ ip = strdup(newip);
+ }
+ if (newudpport != nil) {
+ free(udpport);
+ udpport = strdup(newudpport);
+ }
+ if (udp < 0 && ip[0] && udpport[0] && (udp = dial(netmkaddr(ip, "udp", udpport), nil, nil, nil)) < 0) {
fprint(2, "udp failed: %r\n");
/* FIXME display error */
}
@@ -320,15 +334,12 @@
tick = MAX(0, tick+x);
else if (strcmp(s, "rewind") == 0)
tick = MAX(0, tick-x);
- else if (strcmp(s, "ip") == 0) {
- free(ip);
- ip = strdup(a);
- netdial();
- } else if (strcmp(s, "udp") == 0) {
- free(udpport);
- udpport = strdup(a);
- netdial();
- }
+ else if (strcmp(s, "ip") == 0)
+ netdial(a, nil);
+ else if (strcmp(s, "udp") == 0)
+ netdial(nil, a);
+ else if (strcmp(s, "midi") == 0)
+ midiopen(a);
/* FIXME color, find, select, inject, write, time */
}
@@ -349,7 +360,8 @@
u[1] = 0x90 | n->channel;
u[2] = (n->octave + 1)*12 + n->note;
u[3] = n->velocity;
- write(1, u, 4);
+ if (midi >= 0)
+ write(midi, u, 4);
off = n->channel*128 + u[2];
noteoff[off].u[1] = 0x80 | n->channel;
@@ -368,9 +380,9 @@
}
}
- for (i = 0; i < nelem(noteoff); i++) {
+ for (i = 0; midi >= 0 && i < nelem(noteoff); i++) {
if (noteoff[i].at > 0 && noteoff[i].at < tick) {
- write(1, noteoff[i].u, 4);
+ write(midi, noteoff[i].u, 4);
noteoff[i].at = 0;
}
}
@@ -1126,9 +1138,7 @@
move.y = 1;
oldbuttons = 0;
- ip = strdup("127.0.0.1");
- udpport = strdup("49160");
- netdial();
+ netdial("127.0.0.1", "49160");
for (;;) {
redraw(complete);