commit 3506acd641e4bb48d2302d03564ce88db96bd041
parent 190530f304f50e4c845f129e5b801f99b92bdddb
Author: zavok <an2qzavok@gmail.com>
Date: Tue, 5 Nov 2019 15:19:41 +0300
simplified midi message fetching routines
Diffstat:
M | midi.c | | | 77 | ++++++++++++++++++++++++++++++----------------------------------------------- |
M | midi.h | | | 2 | -- |
2 files changed, 30 insertions(+), 49 deletions(-)
diff --git a/midi.c b/midi.c
@@ -4,7 +4,7 @@
#include <sndio.h>
#include "midi.h"
-uint8_t mmessage[8];
+uint8_t mmessage[4];
uint8_t mlen;
uint8_t mi;
struct mio_hdl *mh;
@@ -22,59 +22,44 @@ enum {
};
void m_exec(void);
-void m_data(uint8_t);
-void m_status(uint8_t);
+void m_fillmess(uint8_t);
void
-m_status(uint8_t b)
+m_fillmess(uint8_t b)
{
- if (b < 0x80) return; /* not a status byte */
- uint8_t t, c;
- t = b & 0xf0;
- c = b & 0x0f;
- if (t == SYS_MESSAGE){
- return;
- }
- mmessage[0] = t;
- mmessage[1] = c;
- mi=2;
- switch (t){
- case NOTE_OFF:
- case NOTE_ON:
- case KEY_PRESSURE:
- case CTL_CHANGE:
- PITCH_WHEEL:
- mlen = 4;
- m_update = m_data;
- break;
- case PROG_CHANGE:
- case CHAN_PRESSURE:
- mlen = 3;
- m_update = m_data;
- break;
- SYS_MESSAGE:
- break;
- }
-}
-
-void
-m_data(uint8_t b)
-{
- mmessage[mi] = b;
- mi++;
- if (mi == mlen){
- mi = 0;
+ if (b >= 0x80){ /* status byte */
+ mmessage[0] = b & 0xf0;
+ mmessage[1] = b & 0x0f;
mlen = 0;
- m_exec();
- m_update = m_status;
- }
+ switch (mmessage[0]){
+ case NOTE_OFF:
+ case NOTE_ON:
+ case KEY_PRESSURE:
+ case CTL_CHANGE:
+ case PITCH_WHEEL:
+ mlen = 4;
+ break;
+ case PROG_CHANGE:
+ case CHAN_PRESSURE:
+ mlen = 3;
+ break;
+ case SYS_MESSAGE:
+ mlen = 2;
+ break;
+ }
+ mi = 2;
+ } else if (mi < mlen){
+ mmessage[mi] = b;
+ mi++;
+ }
+ if (mi == mlen) m_exec();
}
void
m_exec(void)
{
void (*f)(uint8_t*);
- switch(mmessage[0]){
+ switch(mmessage[0]&0xf0){
case NOTE_OFF:
f = m_vector.note_off;
break;
@@ -111,10 +96,8 @@ m_init(char *mdev)
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
@@ -122,5 +105,5 @@ m_read(void)
{
size_t i, n;
n = mio_read(mh, mbuf, 32);
- for (i=0; i<n; i++) m_update(mbuf[i]);
+ for (i=0; i<n; i++) m_fillmess(mbuf[i]);
}
diff --git a/midi.h b/midi.h
@@ -11,7 +11,5 @@ struct m_vector{
struct m_vector m_vector;
-void (*m_update)(uint8_t);
-
void m_init(char *mdev);
void m_read(void);