Browse Source

Start working on OSS backend

master
Damien Goutte-Gattat 10 years ago
parent
commit
bc338cff8a
  1. 130
      src/midi.c

130
src/midi.c

@ -27,7 +27,7 @@
#include <xmem.h>
#ifdef USE_ALSA_MIDI_API
#if USE_ALSA_MIDI_API
/* Linux ALSA API */
#include <alsa/asoundlib.h>
@ -147,8 +147,134 @@ midi_error(midi_io_t *midi)
return snd_strerror(midi->error);
}
#elif USE_OSS_MIDI_API
/* Open Sound System MIDI API */
#include <fcntl.h>
#include <soundcard.h>
#define MIDI_IO_BUFFER_SIZE 256
struct midi_io
{
int fd;
int error;
unsigned char buffer[MIDI_IO_BUFFER_SIZE];
size_t pos;
size_t len;
unsigned char status;
};
midi_io_t *
midi_open(const char *name)
{
midi_io_t *midi;
assert(name != NULL);
midi = xmalloc(sizeof(*midi));
midi->fd = -1;
midi->error = midi->pos = midi->len = midi->status = 0;
if ( (midi->fd = open(name, O_RDWR, 0)) == -1 ) {
free(midi);
midi = NULL;
}
return midi;
}
int
midi_close(midi_io_t *midi)
{
assert(midi != NULL);
close(midi->fd);
free(midi);
return 0;
}
ssize_t
midi_write(midi_io_t *midi, unsigned char *buffer, size_t len)
{
ssize_t n;
assert(midi != NULL);
assert(buffer != NULL);
/* Flush reading buffer. */
midi->pos = midi->len = 0;
if ( (n = write(midi->fd, buffer, len)) == -1 )
midi->error = errno;
else
midi->error = 0;
return n;
}
ssize_t
midi_read(midi_io_t *midi, unsigned char *buffer, size_t len)
{
ssize_t n;
assert(midi != NULL);
assert(buffer != NULL);
if ( (n = read(midi->fd, buffer, len)) == -1 )
midi->error = errno;
else
midi->error = 0;
return n;
}
int
midi_next(midi_io_t *midi)
{
unsigned char b;
assert(midi != NULL);
if ( midi->pos >= midi->len ) {
ssize_t n;
n = midi_read(midi, midi->buffer, MIDI_IO_BUFFER_SIZE);
if ( n < 0 )
return n;
midi->len = n;
midi->pos = 0;
}
b = (unsigned char) midi->buffer[midi->pos++];
if ( b >= 0x80 && b < 0xF8 ) /* Status byte? */
midi->status = b;
return b;
}
inline unsigned char
midi_status(midi_io_t *midi)
{
return midi->status;
}
const char *
midi_error(midi_io_t *midi)
{
assert(midi != NULL);
if ( midi->error == 0 )
return NULL;
else
return strerror(midi->error);
}
#else
#error "No MIDI API available"
/* TODO: Support for others MIDI APIs (OSS, JackMIDI?, WinAPI?, ...) */
/* TODO: Support for others MIDI APIs (JackMIDI?, WinAPI?, ...) */
#endif

Loading…
Cancel
Save