Chapter 4. Examples

int midi_sysex(int card, int device, char *filename)
{
  int err, fd, count, size;
  snd_rawmidi_t *handle;
  char *buffer;

  buffer = (char *)malloc(64 * 1024);
  if (buffer == NULL)
    return -ENOMEM;
  if ((err = snd_rawmidi_open(&handle, card, device, SND_RAWMIDI_OPEN_OUTPUT)) < 0) {
    fprintf(stderr, "open failed: %s\n", snd_strerror(err));
    return err;
  }
  if ((err = snd_rawmidi_block_mode(handle, 1)) < 0) {
    fprintf(stderr, "block failed: %s\n", snd_strerror(err));
    snd_rawmidi_close(handle);
    return err;
  }
  fd = open(filename, O_RDONLY);
  if (fd < 0) {
    perror("open file");
    snd_rawmidi_close(handle);
    return;
  }
  count = read(fd, buffer, 64 * 1024);
  if (count <= 0) {
    perror("read from file");
    close(fd);
    snd_rawmidi_close(handle);
    return;
  }
  close(fd);
  size = snd_rawmidi_write(handle, buffer, count);
  printf("Bytes written %i from %i...\n", size, count);
  snd_rawmidi_close(handle);
  free(buffer);
  return size;
}