summaryrefslogtreecommitdiff
path: root/apps/midi.c
diff options
context:
space:
mode:
Diffstat (limited to 'apps/midi.c')
-rw-r--r--apps/midi.c42
1 files changed, 34 insertions, 8 deletions
diff --git a/apps/midi.c b/apps/midi.c
index 8110475..b923316 100644
--- a/apps/midi.c
+++ b/apps/midi.c
@@ -25,15 +25,16 @@
#include <stdio.h>
#include <error.h>
#include <poll.h>
+#include <assert.h>
#include "midi.h"
int midi_init(midi_t* m, const char* device)
{
- if(!m)
- return -1;
+ assert(m != NULL);
+ m->stop_ = 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));
@@ -43,10 +44,11 @@ int midi_init(midi_t* m, const char* device)
return 0;
}
-void midi_run(midi_t* m)
+static void* midi_run(void* p)
{
- if(!m)
- return;
+ assert(p != NULL);
+
+ midi_t* m = (midi_t*)p;
int npfds;
struct pollfd *pfds;
@@ -62,9 +64,10 @@ void midi_run(midi_t* m)
error(0, errno, "MIDI: poll failed");
break;
}
- /* if(err == 0) { */
- /* // TIMEOUT */
- /* } */
+ if(err == 0) {
+ if(m->stop_)
+ break;
+ }
unsigned short revents;
if((err = snd_rawmidi_poll_descriptors_revents(m->input_, pfds, npfds, &revents)) < 0) {
@@ -90,5 +93,28 @@ void midi_run(midi_t* m)
for (i = 0; i < ret; ++i)
printf("%02X%c", buf[i], (i >= (ret-1)) ? '\n' : ' ');
}
+ return NULL;
+}
+
+int midi_start(midi_t* m)
+{
+ assert(m != NULL);
+
+ int ret = pthread_create(&(m->thread_), NULL, midi_run, (void*)m);
+ if(ret)
+ error(0, ret, "faild to create midi thread");
+
+ return ret;
+}
+
+int midi_stop(midi_t* m)
+{
+ assert(m != NULL);
+
+ m->stop_ = 1;
+ int ret = pthread_join(m->thread_, NULL);
+ if(ret)
+ error(0, ret, "faild to join midi thread");
+ return ret;
}