Skip to content

Commit 828473f

Browse files
committed
FIX: provide error numbers instead of hard-coded traces from the MIDI device
1 parent cae3efe commit 828473f

File tree

2 files changed

+60
-29
lines changed

2 files changed

+60
-29
lines changed

src/os/osx/dev-midi.c

+32-17
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@
4848
#ifdef INCLUDE_MIDI_DEVICE
4949
//#define DEBUG_MIDI
5050

51+
#ifdef DEBUG_MIDI
52+
#define LOG(...) do { printf(__VA_ARGS__); } while (0)
53+
#else
54+
#define LOG(...)
55+
#endif
56+
5157
#ifdef TO_MACOS
5258

5359
// NOTE: this will be useful for higher level launchpad schemes:
@@ -209,7 +215,7 @@ static void MidiInProc(const MIDIPacketList *pktlist, void *refCon, void *connRe
209215

210216
MIDIPacket *packet = (MIDIPacket *)pktlist->packet; // remove const (!)
211217
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);
213219
if (packet->data[0] >= 0x80 && packet->data[0] < 0xF0) {
214220
Midi_Push_Buffer(midi_port.inp_buffer, ((u32*)packet->data)[0]);
215221
// report timestamp in miliseconds like on Windows
@@ -256,17 +262,16 @@ static void PrintMidiDevices()
256262
/*
257263
***********************************************************************/
258264
{
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+
262267
if (gClient) return DR_DONE;
263268

264269
MIDIClientCreate(CFSTR("Rebol3 MIDI"), MIDINotifyCallback, NULL, &gClient);
265270
MIDIInputPortCreate(gClient, CFSTR("Input port"), MidiInProc, NULL, &gInPort);
266271
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+
270275
Midi_Ports_Pool.count = MIDI_PORTS_ALLOC;
271276
Midi_Ports_Pool.ports = MAKE_MEM(MIDI_PORTS_ALLOC * sizeof(REBMID));
272277
CLEAR(Midi_Ports_Pool.ports, MIDI_PORTS_ALLOC * sizeof(REBMID));
@@ -285,10 +290,17 @@ static void PrintMidiDevices()
285290
REBCNT device_out = req->midi.device_out;
286291
REBINT port_num;
287292
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+
}
288299

289300
port_num = Get_New_Midi_Port(&midi_port);
290301
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;
292304
return DR_ERROR;
293305
}
294306

@@ -302,7 +314,9 @@ static void PrintMidiDevices()
302314
(device_in && device_in > MIDIGetNumberOfSources())
303315
|| (device_out && device_out > MIDIGetNumberOfDestinations())
304316
) {
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;
306320
return DR_ERROR;
307321
}
308322

@@ -311,7 +325,8 @@ static void PrintMidiDevices()
311325
midi_port->inp_id = device_in;
312326
midi_port->inp_device = MIDIGetSource(device_in-1);
313327
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;
315330
return DR_ERROR;
316331
}
317332
MIDIPortConnectSource(gInPort, midi_port->inp_device, (void*)(REBU64)port_num);
@@ -322,14 +337,15 @@ static void PrintMidiDevices()
322337
//printf("opening %u => %0X\n", device_out, midi_port->out_device);
323338
midi_port->out_device = MIDIGetDestination(device_out-1);
324339
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);
326341
if (midi_port->inp_device) {
327342
// closing already opened input device if any
328343
//midiInStop(midi_port->inp_device);
329344
midi_port->started = 0;
330345
MIDIPortDisconnectSource(gInPort, midi_port->inp_device);
331346
midi_port->inp_device = 0;
332347
}
348+
req->error = 5;
333349
return DR_ERROR;
334350
}
335351
}
@@ -356,9 +372,7 @@ static void PrintMidiDevices()
356372
{
357373
REBMID *midi_port= (REBMID *)req->handle;
358374

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);
362376

363377
if (midi_port->inp_device) {
364378
//midiInStop(midi_port->inp_device);
@@ -431,7 +445,8 @@ static void PrintMidiDevices()
431445
u8 type = data_out[0] & 0xF0;
432446
//printf("writing... %u\n", type);
433447
if (type == 0xF0) {
434-
puts("sysex not yet supported!");
448+
LOG("sysex not yet supported!\n");
449+
req->error = 10;
435450
return DR_ERROR;
436451
} else if (type >= 0x80) {
437452
packet.timeStamp = mach_absolute_time();
@@ -459,7 +474,7 @@ static void PrintMidiDevices()
459474
/*
460475
***********************************************************************/
461476
{
462-
puts("Poll_MIDI");
477+
LOG("Poll_MIDI");
463478
return DR_DONE;
464479
}
465480

@@ -482,7 +497,7 @@ static void PrintMidiDevices()
482497
CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0, true);
483498

484499
#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());
486501
PrintMidiDevices();
487502
#endif
488503

src/os/win32/dev-midi.c

+28-12
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@
4848
#ifdef INCLUDE_MIDI_DEVICE
4949
//#define DEBUG_MIDI
5050

51+
#ifdef DEBUG_MIDI
52+
#define LOG(...) do { printf(__VA_ARGS__); } while (0)
53+
#else
54+
#define LOG(...)
55+
#endif
56+
5157
// NOTE: this will be useful for higher level launchpad schemes:
5258
// https://github.com/FMMT666/launchpad.py/blob/master/launchpad_py/launchpad.py
5359
// (code includes led/key mapping values)
@@ -155,9 +161,8 @@ static void CALLBACK MidiInProc(HMIDIIN hMidiIn, UINT wMsg, DWORD dwInstance, DW
155161
evt.model = EVM_MIDI;
156162
evt.port = midi_port.port;
157163

158-
#ifdef DEBUG_MIDI
159-
printf("MidiInProc... port: %0X\n", midi_port.port);
160-
#endif
164+
LOG("MidiInProc... port: %0X\n", midi_port.port);
165+
161166
switch (wMsg) {
162167
case MIM_DATA:
163168
status = (0xF0 & dwParam1);
@@ -242,8 +247,8 @@ static void CALLBACK MidiOutProc(HMIDIOUT hMidiOut, UINT wMsg, DWORD dwInstance,
242247
case MM_MOM_CLOSE:
243248
return; // don't report these
244249
default:
245-
printf("MidiOutProc... port: %0p\n", midi_port.port);
246-
printf("dwInstance=%u dwParam1=%08x, dwParam2=%08x wMsg=%08x\n", dwInstance, dwParam1, dwParam2, wMsg);
250+
LOG("MidiOutProc... port: %0p\n", midi_port.port);
251+
LOG("dwInstance=%u dwParam1=%08x, dwParam2=%08x wMsg=%08x\n", dwInstance, dwParam1, dwParam2, wMsg);
247252
}
248253
return;
249254
}
@@ -325,9 +330,16 @@ static void PrintMidiDevices()
325330
UINT nMidiDeviceNum;
326331
#endif
327332

333+
if(!device_in && !device_out) {
334+
LOG("No devices!\n");
335+
req->error = 1;
336+
return DR_ERROR;
337+
}
338+
328339
port_num = Get_New_Midi_Port(&midi_port);
329340
if (port_num < 0) {
330-
puts("Failed to get new empty MIDI port!");
341+
LOG("Failed to get new empty MIDI port!\n");
342+
req->error = 2;
331343
return DR_ERROR;
332344
}
333345

@@ -341,7 +353,8 @@ static void PrintMidiDevices()
341353
(device_in && device_in > midiInGetNumDevs())
342354
|| (device_out && device_out > midiOutGetNumDevs())
343355
) {
344-
puts("Some of the requested MIDI device IDs are out of range!");
356+
LOG("Some of the requested MIDI device IDs are out of range!\n");
357+
req->error = 3;
345358
return DR_ERROR;
346359
}
347360

@@ -351,7 +364,8 @@ static void PrintMidiDevices()
351364
midi_port->inp_id = device_in;
352365
rv = midiInOpen(&midi_port->inp_device, device_in - 1, (DWORD_PTR)MidiInProc, (DWORD_PTR)port_num, CALLBACK_FUNCTION);
353366
if (rv != MMSYSERR_NOERROR) {
354-
printf("midiInOpen() failed...rv=%d\n", rv);
367+
LOG("midiInOpen() failed...rv=%d\n", rv);
368+
req->error = 4;
355369
return DR_ERROR;
356370
}
357371
midiInStart(midi_port->inp_device);
@@ -367,13 +381,14 @@ static void PrintMidiDevices()
367381
CALLBACK_FUNCTION);
368382

369383
if (rv != MMSYSERR_NOERROR) {
370-
printf("midiOutOpen() failed...rv=%d\n", rv);
384+
LOG("midiOutOpen() failed...rv=%d\n", rv);
371385
if (midi_port->inp_device) {
372386
// closing already opened input device if any
373387
midiInStop(midi_port->inp_device);
374388
midiInClose(midi_port->inp_device);
375389
midi_port->inp_device = NULL;
376390
}
391+
req->error = 5;
377392
return DR_ERROR;
378393
}
379394
}
@@ -459,7 +474,8 @@ static void PrintMidiDevices()
459474
while (data_out < tail) {
460475
u8 type = data_out[0] & 0xF0;
461476
if (type == 0xF0) {
462-
puts("sysex not yet supported!");
477+
LOG("sysex not yet supported!\n");
478+
req->error = 10;
463479
return DR_ERROR;
464480
} else if (type >= 0x80) {
465481
rv = midiOutShortMsg(midi_port->out_device, ((DWORD*)data_out)[0]);
@@ -481,7 +497,7 @@ static void PrintMidiDevices()
481497
/*
482498
***********************************************************************/
483499
{
484-
puts("Poll_MIDI");
500+
LOG("Poll_MIDI\n");
485501
return DR_DONE;
486502
}
487503

@@ -527,7 +543,7 @@ static void PrintMidiDevices()
527543
/*
528544
***********************************************************************/
529545
{
530-
//printf("Quit_MIDI\n");
546+
//LOG("Quit_MIDI\n");
531547
for (REBCNT n = 0; n < Midi_Ports_Pool.count; n++) {
532548
if (Midi_Ports_Pool.ports[n].port != NULL) {
533549
req->handle = &Midi_Ports_Pool.ports[n];

0 commit comments

Comments
 (0)