Browse Source

Add the sysex module

master
Damien Goutte-Gattat 10 years ago
parent
commit
7d27dc56d1
  1. 2
      src/Makefile.am
  2. 80
      src/sysex.c
  3. 38
      src/sysex.h

2
src/Makefile.am

@ -1,6 +1,6 @@
bin_PROGRAMS = asysex
asysex_SOURCES = asysex.c midi.h midi.c
asysex_SOURCES = asysex.c midi.h midi.c sysex.h sysex.c
AM_CPPFLAGS = -I$(top_srcdir)/lib
AM_LDFLAGS = -L$(top_builddir)/lib

80
src/sysex.c

@ -0,0 +1,80 @@
/*
* asysex - A SysEx Utility
* Copyright (C) 2012 Damien Goutte-Gattat
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <midi.h>
ssize_t
sysex_read(midi_io_t *midi, unsigned char *data, size_t len)
{
int byte, loop, n;
loop = 1;
n = 1;
while ( n < len && loop ) {
if ( (byte = midi_next(midi)) < 0 )
return byte;
if ( byte >= 0xF8 ) /* Ignore System Realtime messages. */
continue;
if ( midi_status(midi) == 0xF0 ) {
if ( byte == 0xF0 && n > 0 ) {
/* Discard previous, incomplete SysEx message. */
n = 0;
}
data[n++] = byte;
}
else if ( n > 0 ) {
/* We were reading a SysEx message, what happened? */
if ( byte == 0xF7 ) {
/* Normal termination of SysEx message,
* stop reading and return data to caller. */
data[n++] = byte;
loop = 0;
}
else if ( byte >= 0x80 && byte < 0xF7 ) {
/* Abnormal termination of SysEx message,
* abort reading and report failure to caller. */
n = 0;
loop = 0;
}
}
}
return n;
}
ssize_t
midi_query(midi_io_t *midi,
unsigned char *query,
size_t query_len,
unsigned char *reply,
size_t reply_len)
{
ssize_t n;
if ( (n = midi_write(midi, query, query_len)) > 0 )
n = sysex_read(midi, reply, reply_len);
return n;
}

38
src/sysex.h

@ -0,0 +1,38 @@
/*
* asysex - A SysEx Utility
* Copyright (C) 2012 Damien Goutte-Gattat
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef ICP20120401_SYSEX_H
#define ICP20120401_SYSEX_H
#include <midi.h>
#ifdef __cpluscplus
extern "C" {
#endif
ssize_t
sysex_read(midi_io_t *, unsigned char *, size_t);
ssize_t
sysex_query(midi_io_t *, unsigned char *, size_t, unsigned char *, size_t);
#ifdef __cplusplus
}
#endif
#endif /* !ICP20120401_SYSEX_H */
Loading…
Cancel
Save