/* alsa_rawmidi_init: * Inits the ALSA RawMIDI interface. */ static int alsa_rawmidi_init(int input, int voices) { int ret = -1, err; char tmp1[128], tmp2[128], temp[256]; #if ALLEGRO_ALSA_VERSION == 9 snd_rawmidi_info_t *info; const char *device = NULL; #else /* ALLEGRO_ALSA_VERSION == 5 */ snd_rawmidi_info_t info; int card = -1; int device = -1; #endif if (input) { ret = -1; } else { #if ALLEGRO_ALSA_VERSION == 9 device = get_config_string(uconvert_ascii("sound", tmp1), uconvert_ascii("alsa_rawmidi_device", tmp2), "default"); err = snd_rawmidi_open(NULL, &rawmidi_handle, device, 0); #else /* ALLEGRO_ALSA_VERSION == 5 */ card = get_config_int(uconvert_ascii("sound", tmp1), uconvert_ascii("alsa_rawmidi_card", tmp2), snd_defaults_rawmidi_card()); device = get_config_int(uconvert_ascii("sound", tmp1), uconvert_ascii("alsa_rawmidi_device", tmp2), snd_defaults_rawmidi_device()); err = snd_rawmidi_open(&rawmidi_handle, card, device, SND_RAWMIDI_OPEN_OUTPUT_APPEND); #endif if (err) { snprintf(temp, sizeof(temp), "Could not open card/rawmidi device: %s", snd_strerror(err)); ustrzcpy(allegro_error, ALLEGRO_ERROR_SIZE, get_config_text(temp)); ret = -1; } ret = 0; } if (rawmidi_handle) { #if ALLEGRO_ALSA_VERSION == 9 snd_rawmidi_nonblock(rawmidi_handle, 0); snd_rawmidi_info_malloc(&info); snd_rawmidi_info(rawmidi_handle, info); _al_sane_strncpy(alsa_rawmidi_desc, snd_rawmidi_info_get_name(info), sizeof(alsa_rawmidi_desc)); #else /* ALLEGRO_ALSA_VERSION == 5 */ snd_rawmidi_block_mode(rawmidi_handle, 1); snd_rawmidi_info(rawmidi_handle, &info); _al_sane_strncpy(alsa_rawmidi_desc, info.name, sizeof(alsa_rawmidi_desc)); #endif midi_alsa.desc = alsa_rawmidi_desc; alsa_rawmidi_errors = 0; } return ret; }
char *mus_midi_describe(void) { int i, err; snd_rawmidi_t *line; snd_rawmidi_info_t *info; char *buf = NULL; char one[256]; snd_rawmidi_info_malloc(&info); buf = (char *)CALLOC(1024, sizeof(char)); for (i = 0; i < 8; i++) { /* presumably this should use the card|device from the info struct, not a blind loop */ err = snd_rawmidi_open(&line, NULL, mus_midi_device_name(i << 16), SND_RAWMIDI_NONBLOCK); /* do the "devices" matter here? */ if (err < 0) continue; err = snd_rawmidi_info(line, info); if (err < 0) break; sprintf(one, "%s: card: %d, device: %d, stream: %d, flags: %x, id: %s, name: %s[%s; %d]\n", mus_midi_device_name(i << 16), snd_rawmidi_info_get_card(info), /* card number */ snd_rawmidi_info_get_device(info), /* device number */ snd_rawmidi_info_get_stream(info), /* snd_rawmidi_stream_t */ snd_rawmidi_info_get_flags(info), /* 1=out 2=in 4=dupex */ snd_rawmidi_info_get_id(info), /* hardware id (out in?) */ snd_rawmidi_info_get_name(info), snd_rawmidi_info_get_subdevice_name(info), snd_rawmidi_info_get_subdevices_count(info)); strcat(buf, one); } snd_rawmidi_info_free(info); return(buf); }
/* userData is assumed to be a pointer to ALSA_MIDIDeviceDescription. ALSA_MIDIDeviceDescription->index has to be set to the index of the device we want to get information of before this method is called the first time via iterateRawmidiDevices(). On each call of this method, ALSA_MIDIDeviceDescription->index is decremented. If it is equal to zero, we have reached the desired device, so action is taken. So after successful completion of iterateRawmidiDevices(), ALSA_MIDIDeviceDescription->index is zero. If it isn't, this is an indication of an error. */ static int deviceInfoIterator(UINT32 deviceID, snd_rawmidi_info_t *rawmidi_info, snd_ctl_card_info_t *cardinfo, void *userData) { char buffer[300]; ALSA_MIDIDeviceDescription* desc = (ALSA_MIDIDeviceDescription*)userData; #ifdef ALSA_MIDI_USE_PLUGHW int usePlugHw = 1; #else int usePlugHw = 0; #endif TRACE0("deviceInfoIterator\n"); initAlsaSupport(); if (desc->index == 0) { // we found the device with correct index desc->deviceID = deviceID; buffer[0]=' '; buffer[1]='['; // buffer[300] is enough to store the actual device string w/o overrun getDeviceStringFromDeviceID(&buffer[2], deviceID, usePlugHw, ALSA_RAWMIDI); strncat(buffer, "]", sizeof(buffer) - strlen(buffer) - 1); strncpy(desc->name, (cardinfo != NULL) ? snd_ctl_card_info_get_id(cardinfo) : snd_rawmidi_info_get_id(rawmidi_info), desc->strLen - strlen(buffer)); strncat(desc->name, buffer, desc->strLen - strlen(desc->name)); desc->description[0] = 0; if (cardinfo != NULL) { strncpy(desc->description, snd_ctl_card_info_get_name(cardinfo), desc->strLen); strncat(desc->description, ", ", desc->strLen - strlen(desc->description)); } strncat(desc->description, snd_rawmidi_info_get_id(rawmidi_info), desc->strLen - strlen(desc->description)); strncat(desc->description, ", ", desc->strLen - strlen(desc->description)); strncat(desc->description, snd_rawmidi_info_get_name(rawmidi_info), desc->strLen - strlen(desc->description)); TRACE2("Returning %s, %s\n", desc->name, desc->description); return FALSE; // do not continue iteration } desc->index--; return TRUE; }
static void midi_port_init(const alsa_rawmidi_t *midi, midi_port_t *port, snd_rawmidi_info_t *info, const alsa_id_t *id) { const char *name; char *c; port->id = *id; snprintf(port->dev, sizeof(port->dev), "hw:%d,%d,%d", id->id[0], id->id[1], id->id[3]); name = snd_rawmidi_info_get_subdevice_name(info); if (!strlen(name)) name = snd_rawmidi_info_get_name(info); snprintf(port->name, sizeof(port->name), "%s %s %s", port->id.id[2] ? "out":"in", port->dev, name); // replace all offending characters with '-' for (c=port->name; *c; ++c) if (!isalnum(*c)) *c = '-'; port->state = PORT_CREATED; }
void list_subdevice_info(snd_ctl_t *ctl, int card, int device) { snd_rawmidi_info_t *info; const char *name; const char *sub_name; int subs, subs_in, subs_out; int sub, in, out; int status; snd_rawmidi_info_alloca(&info); snd_rawmidi_info_set_device(info, device); snd_rawmidi_info_set_stream(info, SND_RAWMIDI_STREAM_INPUT); snd_ctl_rawmidi_info(ctl, info); subs_in = snd_rawmidi_info_get_subdevices_count(info); snd_rawmidi_info_set_stream(info, SND_RAWMIDI_STREAM_OUTPUT); snd_ctl_rawmidi_info(ctl, info); subs_out = snd_rawmidi_info_get_subdevices_count(info); subs = subs_in > subs_out ? subs_in : subs_out; sub = 0; in = out = 0; if ((status = is_output(ctl, card, device, sub)) < 0) { error("cannot get rawmidi information %d:%d: %s", card, device, snd_strerror(status)); return; } else if (status) out = 1; if (status == 0) { if ((status = is_input(ctl, card, device, sub)) < 0) { error("cannot get rawmidi information %d:%d: %s", card, device, snd_strerror(status)); return; } } else if (status) in = 1; if (status == 0) return; name = snd_rawmidi_info_get_name(info); sub_name = snd_rawmidi_info_get_subdevice_name(info); if (sub_name[0] == '\0') { if (subs == 1) { printf("%c%c hw:%d,%d %s\n", in ? 'I' : ' ', out ? 'O' : ' ', card, device, name); } else printf("%c%c hw:%d,%d %s (%d subdevices)\n", in ? 'I' : ' ', out ? 'O' : ' ', card, device, name, subs); } else { sub = 0; for (;;) { printf("%c%c hw:%d,%d,%d %s\n", in ? 'I' : ' ', out ? 'O' : ' ', card, device, sub, sub_name); if (++sub >= subs) break; in = is_input(ctl, card, device, sub); out = is_output(ctl, card, device, sub); snd_rawmidi_info_set_subdevice(info, sub); if (out) { snd_rawmidi_info_set_stream(info, SND_RAWMIDI_STREAM_OUTPUT); if ((status = snd_ctl_rawmidi_info(ctl, info)) < 0) { error("cannot get rawmidi information %d:%d:%d: %s", card, device, sub, snd_strerror(status)); break; } } else { snd_rawmidi_info_set_stream(info, SND_RAWMIDI_STREAM_INPUT); if ((status = snd_ctl_rawmidi_info(ctl, info)) < 0) { error("cannot get rawmidi information %d:%d:%d: %s", card, device, sub, snd_strerror(status)); break; } } sub_name = snd_rawmidi_info_get_subdevice_name(info); } } }