From a00c38b091eec927f357dfe9c0795f15feeacc59 Mon Sep 17 00:00:00 2001 From: Alexandre Ratchov Date: Wed, 7 Nov 2012 11:38:49 +0100 Subject: [PATCH] make midi_in handle input in a single shot --- sndiod/midi.c | 95 ++++++++++++++++++++++++++++-------------------- sndiod/miofile.c | 3 +- sndiod/sock.c | 9 +---- 3 files changed, 58 insertions(+), 49 deletions(-) diff --git a/sndiod/midi.c b/sndiod/midi.c index 084d564..f579c9e 100644 --- a/sndiod/midi.c +++ b/sndiod/midi.c @@ -249,46 +249,12 @@ midi_fill(struct midi *oep) } } -int -midi_in(struct midi *iep) +void +midi_parse(struct midi *iep, unsigned char *idata, int icount) { - unsigned char c, *idata; - int i, icount, maxavail, avail; - struct midi *oep; + int i; + unsigned char c; - /* - * calculate the max message size we can process - */ - maxavail = MIDI_BUFSZ; - for (i = 0; i < MIDI_NEP ; i++) { - if ((iep->txmask & (1 << i)) == 0) - continue; - oep = midi_ep + i; - avail = oep->obuf.len - oep->obuf.used; - if (maxavail > avail) - maxavail = avail; - } - - /* - * in the works case output message is twice the - * input message (2-byte messages with running status) - */ - maxavail /= 2; - - idata = abuf_rgetblk(&iep->ibuf, &icount); - if (icount > maxavail) - icount = maxavail; -#ifdef DEBUG - if (log_level >= 4) { - midi_log(iep); - log_puts(": in:"); - for (i = 0; i < icount; i++) { - log_puts(" "); - log_putx(idata[i]); - } - log_puts("\n"); - } -#endif for (i = 0; i < icount; i++) { c = *idata++; if (c >= 0xf8) { @@ -327,8 +293,57 @@ midi_in(struct midi *iep) } } } - abuf_rdiscard(&iep->ibuf, icount); - return icount; +} + +int +midi_in(struct midi *iep) +{ + unsigned char *idata; + int i, icount, maxavail, avail, idone; + struct midi *oep; + + /* + * calculate the max message size we can process + */ + maxavail = MIDI_BUFSZ; + for (i = 0; i < MIDI_NEP ; i++) { + if ((iep->txmask & (1 << i)) == 0) + continue; + oep = midi_ep + i; + avail = oep->obuf.len - oep->obuf.used; + if (maxavail > avail) + maxavail = avail; + } + + /* + * in the works case output message is twice the + * input message (2-byte messages with running status) + */ + maxavail /= 2; + idone = 0; + for (;;) { + idata = abuf_rgetblk(&iep->ibuf, &icount); + if (icount > maxavail) + icount = maxavail; + if (icount == 0) + break; + maxavail -= icount; +#ifdef DEBUG + if (log_level >= 4) { + midi_log(iep); + log_puts(": in:"); + for (i = 0; i < icount; i++) { + log_puts(" "); + log_putx(idata[i]); + } + log_puts("\n"); + } +#endif + midi_parse(iep, idata, icount); + abuf_rdiscard(&iep->ibuf, icount); + idone += icount; + } + return idone; } void diff --git a/sndiod/miofile.c b/sndiod/miofile.c index a469dfd..030c290 100644 --- a/sndiod/miofile.c +++ b/sndiod/miofile.c @@ -99,8 +99,7 @@ port_mio_in(void *arg) if (n == 0) break; abuf_wcommit(&ep->ibuf, n); - while (midi_in(ep)) - ; /* nothing */ + midi_in(ep); if (n < count) break; } diff --git a/sndiod/sock.c b/sndiod/sock.c index 4749eca..0b7f84e 100644 --- a/sndiod/sock.c +++ b/sndiod/sock.c @@ -565,13 +565,8 @@ sock_rdata(struct sock *f) #endif if (f->slot) slot_write(f->slot); - if (f->midi) { - count = f->midi->ibuf.used; - while (midi_in(f->midi)) - ; /* nothing */ - count -= f->midi->ibuf.used; - f->fillpending += count; - } + if (f->midi) + f->fillpending += midi_in(f->midi); return 1; }