commit 49130e2ffe3fcf7bc3f2047fc38f613670fbbb71
parent 6b918683b79ecfedb68a649f8a450910f4537475
Author: zavok <an2qzavok@gmail.com>
Date: Mon, 4 Nov 2019 13:40:20 +0300
moved midi stuff to separate file
Diffstat:
M | midi.c | | | 63 | ++++++++++++++++++++++++++++++++++++++++----------------------- |
M | midi.h | | | 4 | ++-- |
M | usynth.c | | | 63 | +++++++++++++++------------------------------------------------ |
3 files changed, 57 insertions(+), 73 deletions(-)
diff --git a/midi.c b/midi.c
@@ -1,4 +1,5 @@
#include <stdio.h>
+#include <stdlib.h>
#include <stdint.h>
#include <sndio.h>
#include "midi.h"
@@ -6,29 +7,36 @@
uint8_t mmessage[8];
uint8_t mlen;
uint8_t mi;
+struct mio_hdl *mh;
+uint8_t mbuf[32];
+
enum {
- NOTE_OFF 0x80,
- NOTE_ON 0x90,
- KEY_PRESSURE 0xa0,
- CTL_CHANGE 0xb0,
- PROG_CHANGE 0xc0,
- CHAN_PRESSURE 0xe0,
- PITCH_WHEEL 0xe0,
- SYS_MESSAGE 0xf0,
+ NOTE_OFF = 0x80,
+ NOTE_ON = 0x90,
+ KEY_PRESSURE = 0xa0,
+ CTL_CHANGE = 0xb0,
+ PROG_CHANGE = 0xc0,
+ CHAN_PRESSURE = 0xe0,
+ PITCH_WHEEL = 0xe0,
+ SYS_MESSAGE = 0xf0,
};
+void m_exec(void);
+void m_data(uint8_t);
+void m_status(uint8_t);
+
void
m_status(uint8_t b)
{
- if ((b && 0x80)==0) return; /* not a status byte */
- uint8_t t, ch;
+ if ((b & 0x80)==0) return; /* not a status byte */
+ uint8_t t, c;
t = b & 0xf0;
c = b & 0x0f;
if (t == SYS_MESSAGE){
return;
}
- mmesage[0] = t;
- mmesage[1] = c;
+ mmessage[0] = t;
+ mmessage[1] = c;
mi=2;
switch (t){
case NOTE_OFF:
@@ -42,11 +50,12 @@ m_status(uint8_t b)
}
}
-m_data(uint_t b)
+void
+m_data(uint8_t b)
{
mmessage[mi] = b;
mi++;
- if (mi = mlen){
+ if (mi == mlen){
mi = 0;
mlen = 0;
m_exec();
@@ -57,26 +66,34 @@ m_data(uint_t b)
void
m_exec(void)
{
- switch(mmesage[0]){
+ switch(mmessage[0]){
case NOTE_OFF:
+ m_vector.note_off(mmessage);
break;
case NOTE_ON:
+ m_vector.note_on(mmessage);
break;
}
}
void
-m_init(void)
+m_init(char *mdev)
{
-// char *mdev;
-// mdev = "rmidi/0";
-// mh = mio_open(mdev, MIO_IN, 1);
-// if (mh == 0){
-// fprintf(stderr, "error: couldn't open midi device %s\n" mdev);
-// exit(-1);
-// }
+ mh = mio_open(mdev, MIO_IN, 1);
+ if (mh == 0){
+ fprintf(stderr, "error: couldn't open midi device %s\n", mdev);
+ exit(-1);
+ }
m_update = m_status;
mlen = 0;
mi = 0;
+ printf("midi initialised\n");
}
+void
+m_read(void)
+{
+ size_t i, n;
+ n = mio_read(mh, mbuf, 32);
+ for (i=0; i<n; i++) m_update(mbuf[i]);
+}
diff --git a/midi.h b/midi.h
@@ -7,5 +7,5 @@ struct m_vector m_vector;
void (*m_update)(uint8_t);
-void m_init(void);
-
+void m_init(char *mdev);
+void m_read(void);
diff --git a/usynth.c b/usynth.c
@@ -7,22 +7,7 @@
#include "audio.h"
#include "fm.h"
-enum{
- STATUS,
- DATA,
-};
-
-struct mpstate{
- int s;
- uint8_t m[8];
- int dc;
-};
-
-struct mpstate mparser;
-
-
struct sio_hdl *sh;
-struct mio_hdl *mh;
size_t bs;
int16_t vol, dt;
@@ -75,56 +60,38 @@ sinit(void)
}
void
-mparse(uint8_t b){
- switch (mparser.s){
- case STATUS:
- if (b&0x80){
- mparser.m[0] = b;
- mparser.dc = 1;
- mparser.s = DATA;
- }
- return;
- case DATA:
- mparser.m[1] = b;
- mparser.dc--;
- mparser.s = STATUS;
- break;
- }
- if (mparser.dc == 0){
- uint8_t ctl = mparser.m[0]>>4;
- if (ctl == 9) {
- vol = 250;
- uint8_t note = mparser.m[1];
- dt = 44100.0 / (tune[note%12] * (double)(0x1<<(note/12)));
- }
- if (ctl == 8) vol = 0;
- }
+note_on(uint8_t *mm)
+{
+ dt = 44100 / (tune[mm[2]%12] * (1 << (mm[2]/12)));
+ vol = mm[3]*100;
+}
+
+void
+note_off(uint8_t *mm)
+{
+ vol = 0;
}
int
main(int argc, char **argv)
{
- char *mdev;
int64_t c;
int16_t *sbuf;
- uint8_t *mbuf;
vol = 0;
wt = 0;
rt = 0;
- mdev = "rmidi/0";
- mh = mio_open(mdev, MIO_IN, 1);
sinit();
+ m_init("rmidi/0");
+ m_vector.note_on = note_on;
+ m_vector.note_off = note_off;
sbuf = malloc(bs * 2 * sizeof(int16_t));
- mbuf = malloc(32);
c = 0;
dt = 100;
+ printf("entering main loop\n");
while (1){
size_t n;
int i;
- n = mio_read(mh, mbuf, 32);
- for (i=0; i < n; i++){
- mparse(mbuf[i]);
- }
+ m_read();
for (i=0; i<bs * 2; i+=2){
c++;
sbuf[i] = (c%dt > (dt/2))? vol : -vol;