Skip to content

Commit ebffe48

Browse files
committed
Improve device selection (Midi In/Out)
1 parent 7bff5b6 commit ebffe48

File tree

3 files changed

+74
-24
lines changed

3 files changed

+74
-24
lines changed

src/lib/libmidi.dylib

75 Bytes
Binary file not shown.

src/libmidi.c

Lines changed: 63 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,9 @@ void readProc(const MIDIPacketList *newPackets, void *refCon,
102102
DLLEXPORT void midiopen(char *device)
103103
{
104104
#ifdef __APPLE__
105-
if (strcmp(device, "dlss") == 0 || strcmp(device, "scva") == 0 || MIDIGetDestination(0) == 0 )
105+
int sourceIndex = 0, destIndex = 0;
106+
107+
if (strcmp(device, "dlss") == 0 || strcmp(device, "scva") == 0 || *device == '\0')
106108
{
107109
AUNode synthNode, outNode;
108110
AudioComponentDescription cd;
@@ -162,37 +164,74 @@ DLLEXPORT void midiopen(char *device)
162164
}
163165
else
164166
{
165-
int index;
166-
char name[255];
167+
char *sourceName, *destName;
167168
CFStringRef displayName;
169+
char name[255];
168170
void *conRef = NULL;
169171

170172
if (*device != '\0')
171-
index = atoi(device);
172-
else
173-
index = MIDIGetNumberOfDestinations() - 1;
173+
{
174+
int index;
175+
176+
sourceName = strtok(device, ":");
177+
sourceIndex = -1;
178+
for (index = 0; sourceName != NULL && index < MIDIGetNumberOfSources(); index++)
179+
{
180+
dest = MIDIGetSource(index);
181+
MIDIObjectGetStringProperty(dest, kMIDIPropertyDisplayName, &displayName);
182+
CFStringGetCString(displayName, name, 255, kCFStringEncodingASCII);
183+
if (strcmp(sourceName, name) == 0)
184+
{
185+
sourceIndex = index;
186+
break;
187+
}
188+
}
189+
190+
destName = strtok(NULL, ":");
191+
destIndex = -1;
192+
for (index = 0; destName != NULL && index < MIDIGetNumberOfDestinations(); index++)
193+
{
194+
dest = MIDIGetDestination(index);
195+
MIDIObjectGetStringProperty(dest, kMIDIPropertyDisplayName, &displayName);
196+
CFStringGetCString(displayName, name, 255, kCFStringEncodingASCII);
197+
if (strcmp(destName, name) == 0)
198+
{
199+
destIndex = index;
200+
break;
201+
}
202+
}
203+
}
174204

175-
if (MIDIClientCreate(CFSTR("Mplay"), NULL, NULL, &client) != noErr)
205+
if (MIDIClientCreate(CFSTR("libMidi"), NULL, NULL, &client) != noErr)
176206
fatal("cannot create MIDI client");
177207

178-
if (MIDIOutputPortCreate(client, CFSTR("Output port"), &output_port) != noErr)
179-
fatal("cannot create MIDI output port");
180-
if ((dest = MIDIGetDestination(index)) == 0)
181-
fatal("cannot get MIDI destination");
182-
183-
MIDIObjectGetStringProperty(dest, kMIDIPropertyDisplayName, &displayName);
184-
CFStringGetCString(displayName, name, 255, kCFStringEncodingASCII);
185-
printf("MIDI destination: %s\n", name);
186-
187-
if (MIDIInputPortCreate(client, CFSTR("Input port"), readProc, NULL, &input_port) != noErr)
188-
fatal("cannot create MIDI input port");
208+
if (destIndex >= 0)
209+
{
210+
if (MIDIOutputPortCreate(client, CFSTR("Output port"), &output_port) != noErr)
211+
fatal("cannot create MIDI output port");
212+
if ((dest = MIDIGetDestination(destIndex)) == 0)
213+
fatal("cannot get MIDI destination");
214+
215+
MIDIObjectGetStringProperty(dest, kMIDIPropertyDisplayName, &displayName);
216+
CFStringGetCString(displayName, name, 255, kCFStringEncodingASCII);
217+
printf("MIDI destination: %s\n", name);
218+
}
189219

190-
if ((src = MIDIGetSource(index)) == 0)
191-
fprintf(stderr, "cannot get MIDI source\n");
192-
else
220+
if (sourceIndex >= 0)
193221
{
194-
if (MIDIPortConnectSource(input_port, src, conRef) != noErr)
195-
fatal("cannot connect MIDI input source");
222+
if (MIDIInputPortCreate(client, CFSTR("Input port"), readProc, NULL, &input_port) != noErr)
223+
fatal("cannot create MIDI input port");
224+
if ((src = MIDIGetSource(sourceIndex)) == 0)
225+
fprintf(stderr, "cannot get MIDI source\n");
226+
else
227+
{
228+
if (MIDIPortConnectSource(input_port, src, conRef) != noErr)
229+
fatal("cannot connect MIDI input source");
230+
}
231+
232+
MIDIObjectGetStringProperty(src, kMIDIPropertyDisplayName, &displayName);
233+
CFStringGetCString(displayName, name, 255, kCFStringEncodingASCII);
234+
printf("MIDI source: %s\n", name);
196235
}
197236
}
198237
home:
@@ -295,7 +334,7 @@ DLLEXPORT void midiread(unsigned int *timeStamp, unsigned int *event)
295334
{
296335
*timeStamp = midiEvent[readIndex].timeStamp;
297336
*event = midiEvent[readIndex].data;
298-
readIndex++;
337+
readIndex = (readIndex + 1) % 256;
299338
}
300339
#endif
301340
}

src/midi.jl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,17 @@ function mididataset1(address, data)
2828
end
2929
export mididataset1
3030

31+
function midiread()
32+
timestamp = Cuint[0]
33+
event = Cuint[0]
34+
ccall((:midiread, libmidi),
35+
Nothing,
36+
(Ptr{UInt32}, Ptr{UInt32}),
37+
timestamp, event)
38+
return timestamp[1], event[1]
39+
end
40+
export midiread
41+
3142
function midiclose()
3243
ccall((:midiclose, libmidi),
3344
Nothing,

0 commit comments

Comments
 (0)