From e60a68ede522e56b3a52ddad25ab1704b587e9c2 Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Sat, 10 Oct 2015 06:20:46 +0200 Subject: midi is now using a thread as well --- apps/dolmetschctl.c | 8 ++++++-- apps/midi.c | 42 ++++++++++++++++++++++++++++++++++-------- apps/midi.h | 6 +++++- apps/osc.c | 23 ++++++++++++++--------- apps/osc.h | 4 ++-- 5 files changed, 61 insertions(+), 22 deletions(-) (limited to 'apps') diff --git a/apps/dolmetschctl.c b/apps/dolmetschctl.c index 6e92b2c..fb8f30d 100644 --- a/apps/dolmetschctl.c +++ b/apps/dolmetschctl.c @@ -51,8 +51,12 @@ int main(int argc, char* argv[]) if(midi_init(&m, "hw:2,0,0")) return -1; - osc_start(&o); - midi_run(&m); + if(osc_start(&o) || midi_start(&m)) + return -1; + + sleep(10); + + midi_stop(&m); osc_stop(&o); return 0; 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 #include #include +#include #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; } diff --git a/apps/midi.h b/apps/midi.h index 91634a8..2c0153b 100644 --- a/apps/midi.h +++ b/apps/midi.h @@ -24,12 +24,16 @@ #define DOLMETSCHCTL_midi_h_INCLUDED #include +#include typedef struct { + pthread_t thread_; snd_rawmidi_t* input_; + u_int8_t stop_; } midi_t; int midi_init(midi_t* m, const char* device); -void midi_run(midi_t* m); +int midi_start(midi_t* m); +int midi_stop(midi_t* m); #endif diff --git a/apps/osc.c b/apps/osc.c index 701481f..66b281a 100644 --- a/apps/osc.c +++ b/apps/osc.c @@ -24,6 +24,7 @@ #include #include +#include #include "osc.h" @@ -51,27 +52,31 @@ static int generic_handler(const char *path, const char *types, lo_arg ** argv, int osc_init(osc_t* o, const char* port) { - if(!o) - return -1; + assert(o != NULL); - o->st_ = lo_server_thread_new(port, print_error); // TODO: what happends when this fails? + // TODO: what happends when this fails? + o->st_ = lo_server_thread_new(port, print_error); lo_server_thread_add_method(o->st_, NULL, NULL, generic_handler, NULL); return 0; } -void osc_start(osc_t* o) +int osc_start(osc_t* o) { - if(!o) - return; + assert(o != NULL); + // TODO: what happends when this fails? lo_server_thread_start(o->st_); + + return 0; } -void osc_stop(osc_t* o) +int osc_stop(osc_t* o) { - if(!o) - return; + assert(o != NULL); + // TODO: what happends when this fails? lo_server_thread_free(o->st_); + + return 0; } diff --git a/apps/osc.h b/apps/osc.h index d9163e2..7a03cb7 100644 --- a/apps/osc.h +++ b/apps/osc.h @@ -30,7 +30,7 @@ typedef struct { } osc_t; int osc_init(osc_t* o, const char* port); -void osc_start(osc_t* o); -void osc_stop(osc_t* o); +int osc_start(osc_t* o); +int osc_stop(osc_t* o); #endif -- cgit v1.2.3