summaryrefslogtreecommitdiff
path: root/apps/midi.c
diff options
context:
space:
mode:
Diffstat (limited to 'apps/midi.c')
-rw-r--r--apps/midi.c29
1 files changed, 21 insertions, 8 deletions
diff --git a/apps/midi.c b/apps/midi.c
index 955796e..1a9bc94 100644
--- a/apps/midi.c
+++ b/apps/midi.c
@@ -35,6 +35,8 @@ int midi_init(midi_t* m, const char* device)
assert(m != NULL);
m->input_ = NULL;
+ memset(m->buf_, 0, sizeof(m->buf_));
+ m->read_idx_ = 0;
int ret = snd_rawmidi_open(&(m->input_), NULL, device, SND_RAWMIDI_NONBLOCK);
if(ret < 0) {
error(0, 0, "MIDI: cannot open port '%s': %s", device, snd_strerror(ret));
@@ -54,7 +56,17 @@ int midi_get_poll_fds(midi_t* m, struct pollfd *pfds, int npfds)
return snd_rawmidi_poll_descriptors(m->input_, pfds, npfds);
}
-int midi_handle_revents(midi_t* m, struct pollfd *pfds, int npfds)
+static int midi_handle_message(midi_t* m, mixer_t* x)
+{
+ int i;
+ printf("MIDI: ");
+ for (i = 0; i < sizeof(m->buf_); ++i)
+ printf("%02X%c", m->buf_[i], (i >= (sizeof(m->buf_)-1)) ? '\n' : ' ');
+
+ return 0;
+}
+
+int midi_handle_revents(midi_t* m, struct pollfd *pfds, int npfds, mixer_t* x)
{
int err;
unsigned short revents;
@@ -69,19 +81,20 @@ int midi_handle_revents(midi_t* m, struct pollfd *pfds, int npfds)
if(!(revents & POLLIN))
return 0;
- u_int8_t buf[256];
- int ret = snd_rawmidi_read(m->input_, buf, sizeof(buf));
+ int ret = snd_rawmidi_read(m->input_, &(m->buf_[m->read_idx_]), sizeof(m->buf_) - m->read_idx_);
if(ret == -EAGAIN)
return 0;
if(ret < 0) {
error(0, 0, "MIDI: cannot read from midi port: %s", snd_strerror(ret));
return -1;
}
-
- int i;
- printf("MIDI:%s", ret ? " ": " no data?\n");
- for (i = 0; i < ret; ++i)
- printf("%02X%c", buf[i], (i >= (ret-1)) ? '\n' : ' ');
+ m->read_idx_ += ret;
+ if(m->read_idx_ >= sizeof(m->buf_)) {
+ ret = midi_handle_message(m, x);
+ memset(m->buf_, 0, sizeof(m->buf_));
+ m->read_idx_ = 0;
+ return ret;
+ }
return 0;
}