48
48
#ifdef INCLUDE_MIDI_DEVICE
49
49
//#define DEBUG_MIDI
50
50
51
+ #ifdef DEBUG_MIDI
52
+ #define LOG (...) do { printf(__VA_ARGS__); } while (0)
53
+ #else
54
+ #define LOG (...)
55
+ #endif
56
+
51
57
#ifdef TO_MACOS
52
58
53
59
// NOTE: this will be useful for higher level launchpad schemes:
@@ -209,7 +215,7 @@ static void MidiInProc(const MIDIPacketList *pktlist, void *refCon, void *connRe
209
215
210
216
MIDIPacket * packet = (MIDIPacket * )pktlist -> packet ; // remove const (!)
211
217
for (unsigned int j = 0 ; j < pktlist -> numPackets ; ++ j ) {
212
- //printf ("packet length: %u time: %llu\n", packet->length, packet->timeStamp);
218
+ //LOG ("packet length: %u time: %llu\n", packet->length, packet->timeStamp);
213
219
if (packet -> data [0 ] >= 0x80 && packet -> data [0 ] < 0xF0 ) {
214
220
Midi_Push_Buffer (midi_port .inp_buffer , ((u32 * )packet -> data )[0 ]);
215
221
// report timestamp in miliseconds like on Windows
@@ -256,17 +262,16 @@ static void PrintMidiDevices()
256
262
/*
257
263
***********************************************************************/
258
264
{
259
- #ifdef DEBUG_MIDI
260
- printf ("Init_MIDI: sizeof(Midi_Ports_Pool): %lu sizeof(REBMID): %lu\n" , sizeof (Midi_Ports_Pool ), sizeof (REBMID ));
261
- #endif
265
+ LOG ("Init_MIDI: sizeof(Midi_Ports_Pool): %lu sizeof(REBMID): %lu\n" , sizeof (Midi_Ports_Pool ), sizeof (REBMID ));
266
+
262
267
if (gClient ) return DR_DONE ;
263
268
264
269
MIDIClientCreate (CFSTR ("Rebol3 MIDI" ), MIDINotifyCallback , NULL , & gClient );
265
270
MIDIInputPortCreate (gClient , CFSTR ("Input port" ), MidiInProc , NULL , & gInPort );
266
271
MIDIOutputPortCreate (gClient , CFSTR ("Output port" ), & gOutPort );
267
- #ifdef DEBUG_MIDI
268
- printf ("in %u out %u\n" , gInPort , gOutPort );
269
- #endif
272
+
273
+ LOG ("in %u out %u\n" , gInPort , gOutPort );
274
+
270
275
Midi_Ports_Pool .count = MIDI_PORTS_ALLOC ;
271
276
Midi_Ports_Pool .ports = MAKE_MEM (MIDI_PORTS_ALLOC * sizeof (REBMID ));
272
277
CLEAR (Midi_Ports_Pool .ports , MIDI_PORTS_ALLOC * sizeof (REBMID ));
@@ -285,10 +290,17 @@ static void PrintMidiDevices()
285
290
REBCNT device_out = req -> midi .device_out ;
286
291
REBINT port_num ;
287
292
REBMID * midi_port = NULL ;
293
+
294
+ if (!device_in && !device_out ) {
295
+ LOG ("No devices!\n" );
296
+ req -> error = 1 ;
297
+ return DR_ERROR ;
298
+ }
288
299
289
300
port_num = Get_New_Midi_Port (& midi_port );
290
301
if (port_num < 0 ) {
291
- puts ("Failed to get new empty MIDI port!" );
302
+ LOG ("Failed to get new empty MIDI port!\n" );
303
+ req -> error = 2 ;
292
304
return DR_ERROR ;
293
305
}
294
306
@@ -302,7 +314,9 @@ static void PrintMidiDevices()
302
314
(device_in && device_in > MIDIGetNumberOfSources ())
303
315
|| (device_out && device_out > MIDIGetNumberOfDestinations ())
304
316
) {
305
- puts ("Some of the requested MIDI device IDs are out of range!" );
317
+
318
+ LOG ("Some of the requested MIDI device IDs are out of range!\n" );
319
+ req -> error = 3 ;
306
320
return DR_ERROR ;
307
321
}
308
322
@@ -311,7 +325,8 @@ static void PrintMidiDevices()
311
325
midi_port -> inp_id = device_in ;
312
326
midi_port -> inp_device = MIDIGetSource (device_in - 1 );
313
327
if (midi_port -> inp_device == 0 ) {
314
- printf ("MIDI failed to open input device %i\n" , device_in );
328
+ LOG ("MIDI failed to open input device %i\n" , device_in );
329
+ req -> error = 4 ;
315
330
return DR_ERROR ;
316
331
}
317
332
MIDIPortConnectSource (gInPort , midi_port -> inp_device , (void * )(REBU64 )port_num );
@@ -322,14 +337,15 @@ static void PrintMidiDevices()
322
337
//printf("opening %u => %0X\n", device_out, midi_port->out_device);
323
338
midi_port -> out_device = MIDIGetDestination (device_out - 1 );
324
339
if (midi_port -> out_device == 0 ) {
325
- printf ("MIDI failed to open output device %i\n" , device_out );
340
+ LOG ("MIDI failed to open output device %i\n" , device_out );
326
341
if (midi_port -> inp_device ) {
327
342
// closing already opened input device if any
328
343
//midiInStop(midi_port->inp_device);
329
344
midi_port -> started = 0 ;
330
345
MIDIPortDisconnectSource (gInPort , midi_port -> inp_device );
331
346
midi_port -> inp_device = 0 ;
332
347
}
348
+ req -> error = 5 ;
333
349
return DR_ERROR ;
334
350
}
335
351
}
@@ -356,9 +372,7 @@ static void PrintMidiDevices()
356
372
{
357
373
REBMID * midi_port = (REBMID * )req -> handle ;
358
374
359
- #ifdef DEBUG_MIDI
360
- printf ("Closing MIDI port: %0llX %0llX\n" , (REBU64 )req -> port , (REBU64 )midi_port );
361
- #endif
375
+ LOG ("Closing MIDI port: %0llX %0llX\n" , (REBU64 )req -> port , (REBU64 )midi_port );
362
376
363
377
if (midi_port -> inp_device ) {
364
378
//midiInStop(midi_port->inp_device);
@@ -431,7 +445,8 @@ static void PrintMidiDevices()
431
445
u8 type = data_out [0 ] & 0xF0 ;
432
446
//printf("writing... %u\n", type);
433
447
if (type == 0xF0 ) {
434
- puts ("sysex not yet supported!" );
448
+ LOG ("sysex not yet supported!\n" );
449
+ req -> error = 10 ;
435
450
return DR_ERROR ;
436
451
} else if (type >= 0x80 ) {
437
452
packet .timeStamp = mach_absolute_time ();
@@ -459,7 +474,7 @@ static void PrintMidiDevices()
459
474
/*
460
475
***********************************************************************/
461
476
{
462
- puts ("Poll_MIDI" );
477
+ LOG ("Poll_MIDI" );
463
478
return DR_DONE ;
464
479
}
465
480
@@ -482,7 +497,7 @@ static void PrintMidiDevices()
482
497
CFRunLoopRunInMode (kCFRunLoopDefaultMode , 0 , true);
483
498
484
499
#ifdef DEBUG_MIDI
485
- printf ("Query_MIDI sources: %i dests: %i \n" , MIDIGetNumberOfSources (), MIDIGetNumberOfDestinations ());
500
+ printf ("Query_MIDI sources: %lu dests: %lu \n" , MIDIGetNumberOfSources (), MIDIGetNumberOfDestinations ());
486
501
PrintMidiDevices ();
487
502
#endif
488
503
0 commit comments