if (!(revents & POLLIN)) continue;
// something has arrived
ret = snd_rawmidi_read(input, buf, 64);
- g_print("%s: raw read returned %d\n", __FUNCTION__,ret);
if (ret == 0) continue;
if (ret < 0) {
fprintf(stderr,"cannot read from port \"%s\": %s\n", port, snd_strerror(ret));
snd_rawmidi_read(midi_input[index], NULL, 0); /* trigger reading */
- ret = pthread_create(&midi_thead_id[index], NULL, midi_thread, (void *) index);
+ ret = pthread_create(&midi_thread_id[index], NULL, midi_thread, (void *) index);
if (ret < 0) {
g_print("%s: Failed to create MIDI read thread\n",__FUNCTION__);
if((ret = snd_rawmidi_close(midi_input[index])) < 0) {
//
// wait for thread to complete
//
- ret=pthread_join(midi_devices[index].midi_thread_id, NULL);
+ ret=pthread_join(midi_thread_id[index], NULL);
if (ret != 0) {
g_print("%s: cannot join: %s\n", __FUNCTION__, strerror(ret));
}
sprintf(portname,"hw:%d,%d,%d", card, device, sub);
devnam=subnam;
}
-
//
// If the name was already present at the same position, just keep
// it and do nothing.
int match = 1;
if (midi_devices[n_midi_devices].name == NULL) {
midi_devices[n_midi_devices].name=g_new(gchar,strlen(devnam)+1);
+ strcpy(midi_devices[n_midi_devices].name, devnam);
match = 0;
} else {
- if (strcmp(devnam, midi_devices[n_midi_devices].name) {
+ if (strcmp(devnam, midi_devices[n_midi_devices].name)) {
g_free(midi_devices[n_midi_devices].name);
midi_devices[n_midi_devices].name=g_new(gchar,strlen(devnam)+1);
+ strcpy(midi_devices[n_midi_devices].name, devnam);
match = 0;
}
}
if (midi_port[n_midi_devices] == NULL) {
midi_port[n_midi_devices]=g_new(gchar,strlen(portname)+1);
+ strcpy(midi_port[n_midi_devices], portname);
match = 0;
} else {
- if (strcmp(midi_port[n_midi_devices], portname) {
+ if (strcmp(midi_port[n_midi_devices], portname)) {
g_free(midi_port[n_midi_devices]);
midi_port[n_midi_devices]=g_new(gchar,strlen(portname)+1);
+ strcpy(midi_port[n_midi_devices], portname);
match = 0;
}
}
// the same as before. In this case, just let the thread
// proceed
//
- if (match == 0 && midi_index[n_midi_devices].active) {
+ if (match == 0 && midi_devices[n_midi_devices].active) {
close_midi_device(n_midi_devices);
}
n_midi_devices++;
}
for(int i=0;i<n_midi_devices;i++) {
- g_print("%s: %d: %s %s\n",__FUNCTION__,i,midi_devices[i].name,midi_devices[i].port);
+ g_print("%s: %d: %s %s\n",__FUNCTION__,i,midi_devices[i].name,midi_port[i]);
}
}
#endif
midi_devices[index].active=0;
}
-int register_midi_device(int index) {
+void register_midi_device(int index) {
OSStatus osret;
g_print("%s: index=%d\n",__FUNCTION__,index);
//
// Register a callback routine for the device
//
- if (index < 0 || index > MAX_MIDI_DEVICES) return -1;
+ if (index < 0 || index > MAX_MIDI_DEVICES) return;
myClients[index]=0;
myMIDIports[index] = 0;
osret=MIDIClientCreate(CFSTR("piHPSDR"),NULL,NULL, &myClients[index]);
if (osret !=0) {
g_print("%s: MIDIClientCreate failed with ret=%d\n", __FUNCTION__, (int) osret);
- return -1;
+ return;
}
osret=MIDIInputPortCreate(myClients[index], CFSTR("FromMIDI"), ReadMIDIdevice, NULL, &myMIDIports[index]);
if (osret !=0) {
g_print("%s: MIDIInputPortCreate failed with ret=%d\n", __FUNCTION__, (int) osret);
- return -1;
+ return;
}
osret=MIDIPortConnectSource(myMIDIports[index] ,MIDIGetSource(index), NULL);
if (osret != 0) {
g_print("%s: MIDIPortConnectSource failed with ret=%d\n", __FUNCTION__, (int) osret);
- return -1;
+ return;
}
//
// Now we have successfully opened the device.
//
midi_devices[index].active=1;
- return 0;
+ return;
}
void get_midi_devices() {
if (midi_devices[i].active) {
//
// If device was marked "active" in the props file, open (register) it
+ // Note this flag is hi-jacked, so clear it before opening. It will be set
+ // if the MIDI device has been opened successfully
//
- if (register_midi_device(i) == 0) {
- g_print("%s: MIDI device %s (index=%d) registered\n", __FUNCTION__, midi_devices[i].name, i);
- } else {
- g_print("%s: MIDI device %s (index=%d) could not be opened\n", __FUNCTION__, midi_devices[i].name, i);
- }
+ midi_devices[i].active=0;
+ register_midi_device(i);
}
}
#endif