@@ -102,7 +102,9 @@ void readProc(const MIDIPacketList *newPackets, void *refCon,
102
102
DLLEXPORT void midiopen (char * device )
103
103
{
104
104
#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' )
106
108
{
107
109
AUNode synthNode , outNode ;
108
110
AudioComponentDescription cd ;
@@ -162,37 +164,74 @@ DLLEXPORT void midiopen(char *device)
162
164
}
163
165
else
164
166
{
165
- int index ;
166
- char name [255 ];
167
+ char * sourceName , * destName ;
167
168
CFStringRef displayName ;
169
+ char name [255 ];
168
170
void * conRef = NULL ;
169
171
170
172
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
+ }
174
204
175
- if (MIDIClientCreate (CFSTR ("Mplay " ), NULL , NULL , & client ) != noErr )
205
+ if (MIDIClientCreate (CFSTR ("libMidi " ), NULL , NULL , & client ) != noErr )
176
206
fatal ("cannot create MIDI client" );
177
207
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
+ }
189
219
190
- if ((src = MIDIGetSource (index )) == 0 )
191
- fprintf (stderr , "cannot get MIDI source\n" );
192
- else
220
+ if (sourceIndex >= 0 )
193
221
{
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 );
196
235
}
197
236
}
198
237
home :
@@ -295,7 +334,7 @@ DLLEXPORT void midiread(unsigned int *timeStamp, unsigned int *event)
295
334
{
296
335
* timeStamp = midiEvent [readIndex ].timeStamp ;
297
336
* event = midiEvent [readIndex ].data ;
298
- readIndex ++ ;
337
+ readIndex = ( readIndex + 1 ) % 256 ;
299
338
}
300
339
#endif
301
340
}
0 commit comments