/* 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; }
static int midoalsa_open(snd_rawmidi_t **handle_p, const char *dev_name) { int err; err = snd_rawmidi_open(NULL, handle_p, dev_name, SND_RAWMIDI_NONBLOCK | SND_RAWMIDI_SYNC); if (err) { S_printf("%s: unable to open %s for writing: %s\n", midoalsa_name, dev_name, snd_strerror(err)); return 0; } /* NONBLOCK flag is needed only so that open() not to block forever */ snd_rawmidi_nonblock(*handle_p, 0); return 1; }
/** * Opens MIDI device. This function modifies global input and output variables. * * \return FALSE on success, TRUE on error. **/ gboolean open_device() { int err; err = snd_rawmidi_open(&input, &output, device_port, SND_RAWMIDI_SYNC); if (err) { fprintf(stderr, "snd_rawmidi_open %s failed: %d\n", device_port, err); return TRUE; } err = snd_rawmidi_nonblock(output, 0); if (err) { fprintf(stderr, "snd_rawmidi_nonblock failed: %d\n", err); return TRUE; } snd_rawmidi_read(input, NULL, 0); /* trigger reading */ return FALSE; }
static PyObject *pyrm_open(PyObject *self, PyObject *args) { int status; const char *port; if(MidiIn!=NULL) return midiexcept("midi already openend", 0); if(!PyArg_ParseTuple(args, "s", &port)) return NULL; status = snd_rawmidi_open(&MidiIn, &MidiOut, port, SND_RAWMIDI_SYNC|SND_RAWMIDI_NONBLOCK); if(status<0) return midiexcept2("error opening midi port '%s': %s", port, status); status = snd_rawmidi_nonblock(MidiOut, 0); /* set blocking mode */ if(status<0) return midiexcept("error setting up midi: %s", status); Py_RETURN_NONE; }
MidiAlsaRaw::MidiAlsaRaw() : MidiClientRaw(), m_inputp( &m_input ), m_outputp( &m_output ), m_quit( false ) { int err; if( ( err = snd_rawmidi_open( m_inputp, m_outputp, probeDevice().toLatin1().constData(), 0 ) ) < 0 ) { printf( "cannot open MIDI-device: %s\n", snd_strerror( err ) ); return; } snd_rawmidi_read( m_input, NULL, 0 ); snd_rawmidi_nonblock( m_input, 1 ); m_npfds = snd_rawmidi_poll_descriptors_count( m_input ); m_pfds = new pollfd[m_npfds]; snd_rawmidi_poll_descriptors( m_input, m_pfds, m_npfds ); start( QThread::LowPriority ); }
int main(int argc, char** argv) { int i, j, k, opos, ipos, patsize; int err; int verbose = 0; snd_rawmidi_t *handle_in = NULL, *handle_out = NULL; unsigned char ibuf[512], obuf[512]; char *iname = "hw:0,0", *oname = "hw:0,0"; struct timeval start, end; long long diff; snd_rawmidi_status_t *istat, *ostat; for (i = 1 ; i<argc ; i++) { if (argv[i][0]=='-') { if (!strcmp(argv[i], "--help")) { usage(); return 0; } switch (argv[i][1]) { case 'h': usage(); return 0; case 'v': verbose = 1; break; case 'i': if (i + 1 < argc) iname = argv[++i]; break; case 'o': if (i + 1 < argc) oname = argv[++i]; break; } } } if (iname == NULL) iname = oname; if (oname == NULL) oname = iname; if (verbose) { fprintf(stderr, "Using: \n"); fprintf(stderr, " Input: %s Output: %s\n", iname, oname); } err = snd_rawmidi_open(&handle_in, NULL, iname, SND_RAWMIDI_NONBLOCK); if (err) { fprintf(stderr,"snd_rawmidi_open %s failed: %d\n",iname,err); exit(EXIT_FAILURE); } err = snd_rawmidi_open(NULL, &handle_out, oname, 0); if (err) { fprintf(stderr,"snd_rawmidi_open %s failed: %d\n",oname,err); exit(EXIT_FAILURE); } signal(SIGINT, sighandler); i = snd_rawmidi_read(handle_in, ibuf, sizeof(ibuf)); if (i > 0) { printf("Read ahead: %i\n", i); for (j = 0; j < i; j++) printf("%02x:", ibuf[j]); printf("\n"); exit(EXIT_FAILURE); } snd_rawmidi_nonblock(handle_in, 0); patsize = writepattern(handle_out, obuf); gettimeofday(&start, NULL); patsize = writepattern(handle_out, obuf); k = ipos = opos = err = 0; while (!stop) { i = snd_rawmidi_read(handle_in, ibuf, sizeof(ibuf)); for (j = 0; j < i; j++, ipos++) if (obuf[k] != ibuf[j]) { printf("ipos = %i, i[0x%x] != o[0x%x]\n", ipos, ibuf[j], obuf[k]); if (opos > 0) stop = 1; } else { printf("match success: ipos = %i, opos = %i [%i:0x%x]\n", ipos, opos, k, obuf[k]); k++; opos++; if (k >= patsize) { patsize = writepattern(handle_out, obuf); k = 0; } } } gettimeofday(&end, NULL); printf("End...\n"); snd_rawmidi_status_alloca(&istat); snd_rawmidi_status_alloca(&ostat); err = snd_rawmidi_status(handle_in, istat); if (err < 0) fprintf(stderr, "input stream status error: %d\n", err); err = snd_rawmidi_status(handle_out, ostat); if (err < 0) fprintf(stderr, "output stream status error: %d\n", err); printf("input.status.avail = %zi\n", snd_rawmidi_status_get_avail(istat)); printf("input.status.xruns = %zi\n", snd_rawmidi_status_get_xruns(istat)); printf("output.status.avail = %zi\n", snd_rawmidi_status_get_avail(ostat)); printf("output.status.xruns = %zi\n", snd_rawmidi_status_get_xruns(ostat)); diff = timediff(end, start); printf("Time diff: %Liusec (%Li bytes/sec)\n", diff, ((long long)opos * 1000000) / diff); if (verbose) { fprintf(stderr,"Closing\n"); } snd_rawmidi_drain(handle_in); snd_rawmidi_close(handle_in); snd_rawmidi_drain(handle_out); snd_rawmidi_close(handle_out); return 0; }
/* direction has to be either SND_RAWMIDI_STREAM_INPUT or SND_RAWMIDI_STREAM_OUTPUT. Returns 0 on success. Otherwise, MIDI_OUT_OF_MEMORY, MIDI_INVALID_ARGUMENT or a negative ALSA error code is returned. */ INT32 openMidiDevice(snd_rawmidi_stream_t direction, INT32 deviceIndex, MidiDeviceHandle** handle) { snd_rawmidi_t* native_handle; snd_midi_event_t* event_parser = NULL; int err; UINT32 deviceID; char devicename[100]; #ifdef ALSA_MIDI_USE_PLUGHW int usePlugHw = 1; #else int usePlugHw = 0; #endif TRACE0("> openMidiDevice()\n"); (*handle) = (MidiDeviceHandle*) calloc(sizeof(MidiDeviceHandle), 1); if (!(*handle)) { ERROR0("ERROR: openDevice: out of memory\n"); return MIDI_OUT_OF_MEMORY; } // TODO: iterate to get dev ID from index err = getMidiDeviceID(direction, deviceIndex, &deviceID); TRACE1(" openMidiDevice(): deviceID: %d\n", (int) deviceID); getDeviceStringFromDeviceID(devicename, deviceID, usePlugHw, ALSA_RAWMIDI); TRACE1(" openMidiDevice(): deviceString: %s\n", devicename); // finally open the device if (direction == SND_RAWMIDI_STREAM_INPUT) { err = snd_rawmidi_open(&native_handle, NULL, devicename, SND_RAWMIDI_NONBLOCK); } else if (direction == SND_RAWMIDI_STREAM_OUTPUT) { err = snd_rawmidi_open(NULL, &native_handle, devicename, SND_RAWMIDI_NONBLOCK); } else { ERROR0(" ERROR: openMidiDevice(): direction is neither SND_RAWMIDI_STREAM_INPUT nor SND_RAWMIDI_STREAM_OUTPUT\n"); err = MIDI_INVALID_ARGUMENT; } if (err < 0) { ERROR1("< ERROR: openMidiDevice(): snd_rawmidi_open() returned %d\n", err); free(*handle); (*handle) = NULL; return err; } /* We opened with non-blocking behaviour to not get hung if the device is used by a different process. Writing, however, should be blocking. So we change it here. */ if (direction == SND_RAWMIDI_STREAM_OUTPUT) { err = snd_rawmidi_nonblock(native_handle, 0); if (err < 0) { ERROR1(" ERROR: openMidiDevice(): snd_rawmidi_nonblock() returned %d\n", err); snd_rawmidi_close(native_handle); free(*handle); (*handle) = NULL; return err; } } if (direction == SND_RAWMIDI_STREAM_INPUT) { err = snd_midi_event_new(EVENT_PARSER_BUFSIZE, &event_parser); if (err < 0) { ERROR1(" ERROR: openMidiDevice(): snd_midi_event_new() returned %d\n", err); snd_rawmidi_close(native_handle); free(*handle); (*handle) = NULL; return err; } } (*handle)->deviceHandle = (void*) native_handle; (*handle)->startTime = getTimeInMicroseconds(); (*handle)->platformData = event_parser; TRACE0("< openMidiDevice(): succeeded\n"); return err; }