stew

a monorepo of some sort
Log | Files | Refs

commit 60f1065a5f1d9ec64e66e1112d9e4ca6ec6f1a9d
parent 381578e0ded33a6404accc6004b90268f3cfc8ff
Author: rpa <rpa@laika>
Date:   Sat, 21 Jan 2023 22:35:51 +0000

src/midi: a midi library

Diffstat:
Asrc/midi/dump.c | 42++++++++++++++++++++++++++++++++++++++++++
Asrc/midi/midi.c | 14++++++++++++++
Asrc/midi/midi.h | 39+++++++++++++++++++++++++++++++++++++++
Asrc/midi/mkfile | 5+++++
4 files changed, 100 insertions(+), 0 deletions(-)

diff --git a/src/midi/dump.c b/src/midi/dump.c @@ -0,0 +1,42 @@ +#include <u.h> +#include <libc.h> +#include "midi.h" + +void +noteon(MidiMes m) +{ + print("ch %d NoteOn %d, vel %d\n", m.channel, m.param[0], + m.param[1]); +} + +void +noteoff(MidiMes m) +{ + print("ch %d NoteOff %d, vel %d\n", m.channel, m.param[0], + m.param[1]); +} + + +void +main(void) +{ + char buf[256]; + int i, n; + MidiMes m; + while ((n = read(0, buf, 256)) > 0) { + midifill(&m, buf, 4); + switch(m.status) { + case NOTE_ON: + noteon(m); + break; + case NOTE_OFF: + noteoff(m); + break; + default: + print("%x %x | %x %x | %02x %02x\n", + m.cn, m.cin, m.status, m.channel, + m.param[0], m.param[1]); + } + } + print("%r\n"); +} diff --git a/src/midi/midi.c b/src/midi/midi.c @@ -0,0 +1,14 @@ +#include <u.h> +#include <libc.h> +#include "midi.h" +char * +midifill(MidiMes *m, char *p, int) +{ + m->cn = p[0] >> 8; + m->cin = p[0] & 0x0f; + m->status = p[1] & 0xf0; + m->channel = p[1] & 0x0f; + m->param[0] = p[2]; + m->param[1] = p[3]; + return p + 4; +} diff --git a/src/midi/midi.h b/src/midi/midi.h @@ -0,0 +1,39 @@ +enum { + NOTE_OFF = 0x80, + NOTE_ON = 0x90, + KEY_PRESSURE = 0xa0, + CTL_CHANGE = 0xb0, + PROG_CHANGE = 0xc0, + CHAN_PRESSURE = 0xd0, + PITCH_WHEEL = 0xe0, + SYS_MESSAGE = 0xf0, + + CIN_Misc = 0x0, + CIN_Cable = 0x1, + CIN_2Sys = 0x2, + CIN_3Sys = 0x3, + CIN_SysEx = 0x4, + CIN_1Sys = 0x5, + CIN_2SysEx = 0x6, + CIN_3SysEx = 0x7, + CIN_NoteOff = 0x8, + CIN_NoteOn = 0x9, + CIN_Poly = 0xA, + CIN_Ctl = 0xB, + CIN_Prog = 0xC, + CIN_Press = 0xD, + CIN_Bend = 0xE, + CIN_Byte = 0xF, +}; + +typedef struct MidiMes MidiMes; + +struct MidiMes { + int cn; /* Cable Number */ + int cin; /* Code Index Number */ + int status; + int channel; + int param[2]; +}; + +char * midifill(MidiMes *, char *, int); diff --git a/src/midi/mkfile b/src/midi/mkfile @@ -0,0 +1,5 @@ +</$objtype/mkfile +TARG=dump +HFILES=midi.h +OFILES=midi.$O +</sys/src/cmd/mkmany