KORG microX utility.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

499 lines
9.9 KiB

/*
* 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>
#include <stdlib.h>
#include <assert.h>
#include <xmem.h>
#if USE_ALSA_MIDI_API
/* Linux ALSA API */
#include <alsa/asoundlib.h>
#define MIDI_IO_BUFFER_SIZE 256
struct midi_io
{
snd_rawmidi_t *in;
snd_rawmidi_t *out;
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->in = midi->out = NULL;
midi->error = midi->pos = midi->len = midi->status = 0;
if ( snd_rawmidi_open(&(midi->in), &(midi->out), name,
SND_RAWMIDI_NONBLOCK) < 0 ) {
free(midi);
midi = NULL;
}
else if ( snd_rawmidi_nonblock(midi->out, 0) < 0 ) {
midi_close(midi);
midi = NULL;
}
return midi;
}
int
midi_close(midi_io_t *midi)
{
assert(midi != NULL);
snd_rawmidi_close(midi->in);
snd_rawmidi_close(midi->out);
free(midi);
return 0;
}
ssize_t
midi_write(midi_io_t *midi, unsigned char *buffer, size_t len)
{
assert(midi != NULL);
assert(buffer != NULL);
/* Flush reading buffer. */
midi->pos = midi->len = 0;
midi->error = snd_rawmidi_write(midi->out, buffer, len);
return midi->error;
}
ssize_t
midi_read(midi_io_t *midi, unsigned char *buffer, size_t len)
{
assert(midi != NULL);
assert(buffer != NULL);
midi->error = snd_rawmidi_read(midi->in, buffer, len);
return midi->error;
}
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 snd_strerror(midi->error);
}
int
midi_list_ports(midi_list_port_cb cb, void *user)
{
int card, ret;
ret = 0;
card = -1;
while ( ret == 0 && snd_card_next(&card) >= 0 && card != -1 ) {
snd_ctl_t *ctl;
char name[32];
sprintf(name, "hw:%d", card);
if ( snd_ctl_open(&ctl, name, 0) >= 0 ) {
int device;
device = -1;
while ( ret == 0 && snd_ctl_rawmidi_next_device(ctl, &device) >= 0 && device >= 0 ) {
sprintf(name, "hw:%d,%d", card, device);
ret = cb(name, user);
}
snd_ctl_close(ctl);
}
}
return ret;
}
#elif USE_OSS_MIDI_API
/* Open Sound System MIDI API */
#include <fcntl.h>
#include <string.h>
#include <dirent.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);
}
static int
is_midi_device(const struct dirent *entry)
{
if ( strncmp(entry->d_name, "midi", 4) == 0 )
return 1;
return 0;
}
#include <stdio.h>
int
midi_list_ports(midi_list_port_cb cb, void *user)
{
int n, ret;
struct dirent **namelist;
char path[32];
ret = 0;
if ( (n = scandir("/dev", &namelist, is_midi_device, alphasort)) != -1 ) {
while ( n-- ) {
if ( ret == 0 ) {
snprintf(path, sizeof(path), "/dev/%s", namelist[n]->d_name);
ret = cb(path, user);
}
free(namelist[n]);
}
free(namelist);
}
return ret;
}
#else
#error "No MIDI API available"
/* TODO: Support for others MIDI APIs (JackMIDI?, WinAPI?, ...) */
#endif
int
midi_change_program(midi_io_t *midi,
unsigned char channel,
unsigned short bank,
unsigned char program)
{
unsigned char msg[] = { 0xB0, /* Control Change */
0x00, /* CC Bank Select MSB */
0x00, /* Bank number MSB */
0xB0, /* Control Change */
0x20, /* CC Bank Select LSB */
0x00, /* Bank number LSB */
0xC0, /* Program Change */
0x00 /* Program number */ };
assert(midi != NULL);
msg[0] |= (channel & 0x0F);
msg[3] |= (channel & 0x0F);
msg[6] |= (channel & 0x0F);
msg[2] |= ((bank >> 7) & 0x7F);
msg[5] |= (bank & 0x7F);
msg[7] |= (program & 0x7F);
return midi_write(midi, msg, sizeof(msg));
}
const char *midi_gm_patches[] = {
"Acoustic Grand Piano",
"Bright Acoustic Piano",
"Electric Grand Piano",
"Honky-tonk Piano",
"Electric Piano 1",
"Electric Piano 2",
"Harpsichord",
"Clavi",
"Celesta",
"Glockenspiel",
"Music Box",
"Vibraphone",
"Marimba",
"Xylophone",
"Tubular Bells",
"Dulcimer",
"Drawbar Organ",
"Percussive Organ",
"Rock Organ",
"Church Organ",
"Reed Organ",
"Accordion",
"Harmonica",
"Tango Accordion",
"Guitar (nylon)",
"Acoustic Guitar (steel)",
"Electric Guitar (jazz)",
"Electric Guitar (clean)",
"Electric Guitar (muted)",
"Overdriven Guitar",
"Distortion Guitar",
"Guitar harmonics",
"Acoustic Bass",
"Electric Bass (finger)",
"Electric Bass (pick)",
"Fretless Bass",
"Slap Bass 1",
"Slap Bass 2",
"Synth Bass 1",
"Synth Bass 2",
"Violin",
"Viola",
"Cello",
"Contrabass",
"Tremolo Strings",
"Pizzicato Strings",
"Orchestral Harp",
"Timpani",
"String Ensemble 1",
"String Ensemble 2",
"SynthStrings 1",
"SynthStrings 2",
"Choir Aahs",
"Voice Oohs",
"Synth Voice",
"Orchestra Hit",
"Trumpet",
"Trombone",
"Tuba",
"Muted Trumpet",
"French Horn",
"Brass Section",
"SynthBrass 1",
"SynthBrass 2",
"Soprano Sax",
"Alto Sax",
"Tenor Sax",
"Baritone Sax",
"Oboe",
"English Horn",
"Bassoon",
"Clarinet",
"Piccolo",
"Flute",
"Recorder",
"Pan Flute",
"Blown Bottle",
"Shakuhachi",
"Whistle",
"Ocarina",
"Lead 1 (square)",
"Lead 2 (sawtooth)",
"Lead 3 (calliope)",
"Lead 4 (chiff)",
"Lead 5 (charang)",
"Lead 6 (voice)",
"Lead 7 (fifths)",
"Lead 8 (bass+lead)",
"Pad 1 (new age)",
"Pad 2 (warm)",
"Pad 3 (polysynth)",
"Pad 4 (choir)",
"Pad 5 (bowed)",
"Pad 6 (metallic)",
"Pad 7 (halo)",
"Pad 8 (sweep)",
"FX 1 (rain)",
"FX 2 (soundtrack)",
"FX 3 (crystal)",
"FX 4 (atmosphere)",
"FX 5 (brightness)",
"FX 6 (goblins)",
"FX 7 (echoes)",
"FX 8 (sci-fi)",
"Sitar",
"Banjo",
"Shamisen",
"Koto",
"Kalimba",
"Bag pipe",
"Fiddle",
"Shanai",
"Tinkle Bell",
"Agogo",
"Steel Drums",
"Woodblock",
"Taiko drum",
"Melodic Tom",
"Synth Drum",
"Reverse Cymbal",
"Guitar Fret Noise",
"Breath Noise",
"Seashore",
"Bird Tweet",
"Telephone Ring",
"Helicopter",
"Applause",
"Gunshot"
};