-
-
Notifications
You must be signed in to change notification settings - Fork 74
/
Copy pathNativeApi-macOS.c
972 lines (776 loc) · 31.2 KB
/
NativeApi-macOS.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
#include <CoreFoundation/CoreFoundation.h>
#include <CoreMIDI/CoreMIDI.h>
#include <pthread.h>
#include <mach/mach_time.h>
#include <mach/mach.h>
#include "NativeApi-Constants.h"
#define PROPERTY_VALUE_BUFFER_SIZE 256
#define SMALL_BUFFER_ERROR 10000
/* ================================
Common
================================ */
API_TYPE GetApiType()
{
return API_TYPE_MAC;
}
char CanCompareDevices()
{
return 1;
}
/* ================================
High-precision tick generator
================================ */
typedef struct
{
pthread_t thread;
char active;
CFRunLoopRef runLoopRef;
TGSESSION_OPENRESULT threadStartResult;
} TickGeneratorSessionHandle;
typedef struct
{
void (*callback)(void);
CFRunLoopTimerRef timerRef;
} TickGeneratorInfo;
void SessionCallback(CFRunLoopTimerRef timer, void *info)
{
}
void* TickGeneratorSessionThreadRoutine(void* data)
{
TickGeneratorSessionHandle* sessionHandle = (TickGeneratorSessionHandle*)data;
CFRunLoopTimerContext context = { 0, NULL, NULL, NULL, NULL };
CFRunLoopTimerRef timerRef = CFRunLoopTimerCreate(
NULL,
CFAbsoluteTimeGetCurrent() + 60,
60,
0,
0,
SessionCallback,
&context);
CFRunLoopRef runLoopRef = CFRunLoopGetCurrent();
CFRunLoopAddTimer(runLoopRef, timerRef, kCFRunLoopDefaultMode);
// Set realtime priority
// (thanks to https://stackoverflow.com/a/44310370/2975589)
mach_timebase_info_data_t timebase;
kern_return_t kr = mach_timebase_info(&timebase);
if (kr != KERN_SUCCESS)
{
sessionHandle->threadStartResult = TGSESSION_OPENRESULT_FAILEDTOGETTIMEBASEINFO;
return NULL;
}
struct thread_time_constraint_policy constraintPolicy;
constraintPolicy.period = 500 * 1000 * timebase.denom / timebase.numer; // Period over which we demand scheduling.
constraintPolicy.computation = 100 * 1000 * timebase.denom / timebase.numer; // Minimum time in a period where we must be running.
constraintPolicy.constraint = 100 * 1000 * timebase.denom / timebase.numer; // Maximum time between start and end of our computation in the period.
constraintPolicy.preemptible = FALSE;
thread_port_t threadId = pthread_mach_thread_np(pthread_self());
kr = thread_policy_set(threadId, THREAD_TIME_CONSTRAINT_POLICY, (thread_policy_t)&constraintPolicy, THREAD_TIME_CONSTRAINT_POLICY_COUNT);
if (kr != KERN_SUCCESS)
{
sessionHandle->threadStartResult = TGSESSION_OPENRESULT_FAILEDTOSETREALTIMEPRIORITY;
return NULL;
}
//
sessionHandle->active = 1;
sessionHandle->runLoopRef = runLoopRef;
CFRunLoopRun();
return NULL;
}
TGSESSION_OPENRESULT OpenTickGeneratorSession(void** handle)
{
TickGeneratorSessionHandle* sessionHandle = malloc(sizeof(TickGeneratorSessionHandle));
sessionHandle->threadStartResult = TGSESSION_OPENRESULT_OK;
sessionHandle->active = 0;
pthread_create(&sessionHandle->thread, NULL, TickGeneratorSessionThreadRoutine, sessionHandle);
while (sessionHandle->active == 0)
{
if (sessionHandle->threadStartResult != TGSESSION_OPENRESULT_OK)
return sessionHandle->threadStartResult;
}
*handle = sessionHandle;
return TGSESSION_OPENRESULT_OK;
}
void TimerCallback(CFRunLoopTimerRef timer, void *info)
{
TickGeneratorInfo* tickGeneratorInfo = (TickGeneratorInfo*)info;
tickGeneratorInfo->callback();
}
TG_STARTRESULT StartHighPrecisionTickGenerator_Mac(int interval, void* sessionHandle, void (*callback)(void), TickGeneratorInfo** info)
{
TickGeneratorSessionHandle* pSessionHandle = (TickGeneratorSessionHandle*)sessionHandle;
TickGeneratorInfo* tickGeneratorInfo = malloc(sizeof(TickGeneratorInfo));
tickGeneratorInfo->callback = callback;
double seconds = (double)interval / 1000.0;
CFRunLoopTimerContext context = { 0, tickGeneratorInfo, NULL, NULL, NULL };
CFRunLoopTimerRef timerRef = CFRunLoopTimerCreate(
NULL,
CFAbsoluteTimeGetCurrent() + seconds,
seconds,
0,
0,
TimerCallback,
&context);
tickGeneratorInfo->timerRef = timerRef;
CFRunLoopAddTimer(pSessionHandle->runLoopRef, timerRef, kCFRunLoopDefaultMode);
*info = tickGeneratorInfo;
return TG_STARTRESULT_OK;
}
TG_STOPRESULT StopHighPrecisionTickGenerator(TickGeneratorSessionHandle* sessionHandle, TickGeneratorInfo* tickGeneratorInfo)
{
CFRunLoopRemoveTimer(sessionHandle->runLoopRef, tickGeneratorInfo->timerRef, kCFRunLoopDefaultMode);
return TG_STOPRESULT_OK;
}
/* ================================
Devices common
================================ */
typedef struct
{
MIDIEndpointRef endpointRef;
} InputDeviceInfo;
typedef struct
{
MIDIEndpointRef endpointRef;
} OutputDeviceInfo;
OSStatus GetDevicePropertyValue(MIDIEndpointRef endpointRef, CFStringRef propertyID, char** value)
{
CFStringRef stringRef = NULL;
OSStatus status = MIDIObjectGetStringProperty(endpointRef, propertyID, &stringRef);
if (status == noErr)
{
char* buffer = malloc(PROPERTY_VALUE_BUFFER_SIZE * sizeof(char));
if (!CFStringGetCString(stringRef, buffer, PROPERTY_VALUE_BUFFER_SIZE, kCFStringEncodingUTF8))
return SMALL_BUFFER_ERROR;
*value = buffer;
}
return status;
}
OSStatus GetDeviceDriverVersion(MIDIEndpointRef endpointRef, int* value)
{
SInt32 driverVersion;
OSStatus status = MIDIObjectGetIntegerProperty(endpointRef, kMIDIPropertyDriverVersion, &driverVersion);
*value = driverVersion;
return status;
}
/* ================================
Session
================================ */
typedef void (*InputDeviceCallback)(void* info, char operation);
typedef void (*OutputDeviceCallback)(void* info, char operation);
typedef struct
{
char* name;
MIDIClientRef clientRef;
pthread_t thread;
char clientCreated;
OSStatus clientCreationStatus;
InputDeviceCallback inputDeviceCallback;
OutputDeviceCallback outputDeviceCallback;
} SessionHandle;
void HandleSource(MIDIEndpointRef source, InputDeviceCallback inputDeviceCallback, char operation)
{
InputDeviceInfo* inputDeviceInfo = malloc(sizeof(InputDeviceInfo));
inputDeviceInfo->endpointRef = source;
inputDeviceCallback(inputDeviceInfo, operation);
}
void HandleDestination(MIDIEndpointRef destination, OutputDeviceCallback outputDeviceCallback, char operation)
{
OutputDeviceInfo* outputDeviceInfo = malloc(sizeof(OutputDeviceInfo));
outputDeviceInfo->endpointRef = destination;
outputDeviceCallback(outputDeviceInfo, operation);
}
void HandleEntitySources(MIDIEntityRef entity, InputDeviceCallback inputDeviceCallback, char operation)
{
ItemCount _sourcesCount = MIDIEntityGetNumberOfSources(entity);
for (int i = 0; i < _sourcesCount; i++)
{
MIDIEndpointRef source = MIDIEntityGetSource(entity, i);
HandleSource(source, inputDeviceCallback, operation);
}
}
void HandleEntityDestinations(MIDIEntityRef entity, OutputDeviceCallback outputDeviceCallback, char operation)
{
ItemCount _destinationsCount = MIDIEntityGetNumberOfDestinations(entity);
for (int i = 0; i < _destinationsCount; i++)
{
MIDIEndpointRef destination = MIDIEntityGetDestination(entity, i);
HandleDestination(destination, outputDeviceCallback, operation);
}
}
void HandleEntity(MIDIEntityRef entity, InputDeviceCallback inputDeviceCallback, OutputDeviceCallback outputDeviceCallback, char operation)
{
HandleEntitySources(entity, inputDeviceCallback, operation);
HandleEntityDestinations(entity, outputDeviceCallback, operation);
}
void HandleDevice(MIDIDeviceRef device, InputDeviceCallback inputDeviceCallback, OutputDeviceCallback outputDeviceCallback, char operation)
{
ItemCount entitiesCount = MIDIDeviceGetNumberOfEntities(device);
for (int i = 0; i < entitiesCount; i++)
{
MIDIEntityRef entity = MIDIDeviceGetEntity(device, i);
HandleEntity(entity, inputDeviceCallback, outputDeviceCallback, operation);
}
}
void HandleNotification(const MIDINotification* message, SessionHandle* sessionHandle)
{
switch (message->messageID)
{
case kMIDIMsgObjectAdded:
case kMIDIMsgObjectRemoved:
{
char operation = message->messageID == kMIDIMsgObjectAdded ? 1 : 0;
MIDIObjectAddRemoveNotification* n = (MIDIObjectAddRemoveNotification*)message;
switch (n->childType)
{
case kMIDIObjectType_Device:
{
HandleDevice(n->child, sessionHandle->inputDeviceCallback, sessionHandle->outputDeviceCallback, operation);
break;
}
case kMIDIObjectType_Entity:
{
HandleEntity(n->child, sessionHandle->inputDeviceCallback, sessionHandle->outputDeviceCallback, operation);
break;
}
case kMIDIObjectType_Source:
{
HandleSource(n->child, sessionHandle->inputDeviceCallback, operation);
break;
}
case kMIDIObjectType_Destination:
{
HandleDestination(n->child, sessionHandle->outputDeviceCallback, operation);
break;
}
}
break;
}
}
}
void NotifyProc(const MIDINotification* message, void* refCon)
{
SessionHandle* sessionHandle = (SessionHandle*)refCon;
HandleNotification(message, sessionHandle);
}
void* ThreadProc(void* data)
{
SessionHandle* sessionHandle = (SessionHandle*)data;
CFStringRef nameRef = CFStringCreateWithCString(kCFAllocatorDefault, sessionHandle->name, kCFStringEncodingUTF8);
sessionHandle->clientCreationStatus = MIDIClientCreate(nameRef, NotifyProc, data, &sessionHandle->clientRef);
sessionHandle->clientCreated = 1;
CFRunLoopRun();
return NULL;
}
SESSION_OPENRESULT OpenSession_Mac(char* name, InputDeviceCallback inputDeviceCallback, OutputDeviceCallback outputDeviceCallback, void** handle)
{
SessionHandle* sessionHandle = malloc(sizeof(SessionHandle));
sessionHandle->name = name;
sessionHandle->inputDeviceCallback = inputDeviceCallback;
sessionHandle->outputDeviceCallback = outputDeviceCallback;
sessionHandle->clientCreated = 0;
sessionHandle->name = name;
pthread_create(&sessionHandle->thread, NULL, ThreadProc, sessionHandle);
while (sessionHandle->clientCreated == 0) {}
if (sessionHandle->clientCreationStatus != noErr)
{
switch (sessionHandle->clientCreationStatus)
{
case kMIDIServerStartErr: return SESSION_OPENRESULT_SERVERSTARTERROR;
case kMIDIWrongThread: return SESSION_OPENRESULT_WRONGTHREAD;
case kMIDINotPermitted: return SESSION_OPENRESULT_NOTPERMITTED;
}
return SESSION_OPENRESULT_UNKNOWNERROR;
}
*handle = sessionHandle;
return SESSION_OPENRESULT_OK;
}
SESSION_CLOSERESULT CloseSession(void* handle)
{
SessionHandle* sessionHandle = (SessionHandle*)handle;
free(sessionHandle);
return SESSION_CLOSERESULT_OK;
}
/* ================================
Input device
================================ */
typedef struct
{
InputDeviceInfo* info;
MIDIPortRef portRef;
} InputDeviceHandle;
int GetInputDevicesCount()
{
return (int)MIDIGetNumberOfSources();
}
IN_GETINFORESULT GetInputDeviceInfo(int deviceIndex, void** info)
{
InputDeviceInfo* inputDeviceInfo = malloc(sizeof(InputDeviceInfo));
MIDIEndpointRef endpointRef = MIDIGetSource(deviceIndex);
inputDeviceInfo->endpointRef = endpointRef;
*info = inputDeviceInfo;
return IN_GETINFORESULT_OK;
}
int GetInputDeviceHashCode(void* info)
{
InputDeviceInfo* inputDeviceInfo = (InputDeviceInfo*)info;
return inputDeviceInfo->endpointRef;
}
char AreInputDevicesEqual(void* info1, void* info2)
{
InputDeviceInfo* inputDeviceInfo1 = (InputDeviceInfo*)info1;
InputDeviceInfo* inputDeviceInfo2 = (InputDeviceInfo*)info2;
return (char)(inputDeviceInfo1->endpointRef == inputDeviceInfo2->endpointRef);
}
IN_GETPROPERTYRESULT GetInputDeviceStringPropertyValue(InputDeviceInfo* inputDeviceInfo, CFStringRef propertyID, char** value)
{
OSStatus status = GetDevicePropertyValue(inputDeviceInfo->endpointRef, propertyID, value);
if (status != noErr)
{
switch (status)
{
case kMIDIUnknownEndpoint: return IN_GETPROPERTYRESULT_UNKNOWNENDPOINT;
case SMALL_BUFFER_ERROR: return IN_GETPROPERTYRESULT_TOOLONG;
case kMIDIUnknownProperty: return IN_GETPROPERTYRESULT_UNKNOWNPROPERTY;
}
return IN_GETPROPERTYRESULT_UNKNOWNERROR;
}
return IN_GETPROPERTYRESULT_OK;
}
IN_GETPROPERTYRESULT GetInputDeviceIntPropertyValue(InputDeviceInfo* inputDeviceInfo, CFStringRef propertyID, int* value)
{
OSStatus status = MIDIObjectGetIntegerProperty(inputDeviceInfo->endpointRef, propertyID, value);
if (status != noErr)
{
switch (status)
{
case kMIDIUnknownEndpoint: return IN_GETPROPERTYRESULT_UNKNOWNENDPOINT;
case kMIDIUnknownProperty: return IN_GETPROPERTYRESULT_UNKNOWNPROPERTY;
}
return IN_GETPROPERTYRESULT_UNKNOWNERROR;
}
return IN_GETPROPERTYRESULT_OK;
}
IN_GETPROPERTYRESULT GetInputDeviceName(void* info, char** value)
{
InputDeviceInfo* inputDeviceInfo = (InputDeviceInfo*)info;
return GetInputDeviceStringPropertyValue(inputDeviceInfo, kMIDIPropertyDisplayName, value);
}
IN_GETPROPERTYRESULT GetInputDeviceManufacturer(void* info, char** value)
{
InputDeviceInfo* inputDeviceInfo = (InputDeviceInfo*)info;
return GetInputDeviceStringPropertyValue(inputDeviceInfo, kMIDIPropertyManufacturer, value);
}
IN_GETPROPERTYRESULT GetInputDeviceProduct(void* info, char** value)
{
InputDeviceInfo* inputDeviceInfo = (InputDeviceInfo*)info;
return GetInputDeviceStringPropertyValue(inputDeviceInfo, kMIDIPropertyModel, value);
}
IN_GETPROPERTYRESULT GetInputDeviceDriverVersion(void* info, int* value)
{
InputDeviceInfo* inputDeviceInfo = (InputDeviceInfo*)info;
return GetInputDeviceIntPropertyValue(inputDeviceInfo, kMIDIPropertyDriverVersion, value);
}
IN_GETPROPERTYRESULT GetInputDeviceUniqueId(void* info, int* value)
{
InputDeviceInfo* inputDeviceInfo = (InputDeviceInfo*)info;
return GetInputDeviceIntPropertyValue(inputDeviceInfo, kMIDIPropertyUniqueID, value);
}
IN_GETPROPERTYRESULT GetInputDeviceDriverOwner(void* info, char** value)
{
InputDeviceInfo* inputDeviceInfo = (InputDeviceInfo*)info;
return GetInputDeviceStringPropertyValue(inputDeviceInfo, kMIDIPropertyDriverOwner, value);
}
IN_OPENRESULT OpenInputDevice_Mac(void* info, void* sessionHandle, MIDIReadProc callback, void** handle)
{
InputDeviceInfo* inputDeviceInfo = (InputDeviceInfo*)info;
SessionHandle* pSessionHandle = (SessionHandle*)sessionHandle;
InputDeviceHandle* inputDeviceHandle = malloc(sizeof(InputDeviceHandle));
inputDeviceHandle->info = inputDeviceInfo;
*handle = inputDeviceHandle;
CFStringRef portNameRef = CFSTR("IN");
OSStatus status = MIDIInputPortCreate(pSessionHandle->clientRef, portNameRef, callback, NULL, &inputDeviceHandle->portRef);
if (status != noErr)
{
switch (status)
{
case kMIDIInvalidClient: return IN_OPENRESULT_INVALIDCLIENT;
case kMIDIWrongThread: return IN_OPENRESULT_WRONGTHREAD;
case kMIDINotPermitted: return IN_OPENRESULT_NOTPERMITTED;
}
return IN_OPENRESULT_UNKNOWNERROR;
}
return IN_OPENRESULT_OK;
}
IN_CLOSERESULT CloseInputDevice(void* handle)
{
InputDeviceHandle* inputDeviceHandle = (InputDeviceHandle*)handle;
free(inputDeviceHandle->info);
free(inputDeviceHandle);
return IN_CLOSERESULT_OK;
}
IN_CONNECTRESULT ConnectToInputDevice(void* handle)
{
InputDeviceHandle* inputDeviceHandle = (InputDeviceHandle*)handle;
OSStatus status = MIDIPortConnectSource(inputDeviceHandle->portRef, inputDeviceHandle->info->endpointRef, NULL);
if (status != noErr)
{
switch (status)
{
case kMIDIInvalidPort: return IN_CONNECTRESULT_INVALIDPORT;
case kMIDIWrongThread: return IN_CONNECTRESULT_WRONGTHREAD;
case kMIDINotPermitted: return IN_CONNECTRESULT_NOTPERMITTED;
case kMIDIUnknownEndpoint: return IN_CONNECTRESULT_UNKNOWNENDPOINT;
case kMIDIWrongEndpointType: return IN_CONNECTRESULT_WRONGENDPOINT;
}
return IN_CONNECTRESULT_UNKNOWNERROR;
}
return IN_CONNECTRESULT_OK;
}
IN_DISCONNECTRESULT DisconnectFromInputDevice(void* handle)
{
InputDeviceHandle* inputDeviceHandle = (InputDeviceHandle*)handle;
OSStatus status = MIDIPortDisconnectSource(inputDeviceHandle->portRef, inputDeviceHandle->info->endpointRef);
if (status != noErr)
{
switch (status)
{
case kMIDIInvalidPort: return IN_DISCONNECTRESULT_INVALIDPORT;
case kMIDIWrongThread: return IN_DISCONNECTRESULT_WRONGTHREAD;
case kMIDINotPermitted: return IN_DISCONNECTRESULT_NOTPERMITTED;
case kMIDIUnknownEndpoint: return IN_DISCONNECTRESULT_UNKNOWNENDPOINT;
case kMIDIWrongEndpointType: return IN_DISCONNECTRESULT_WRONGENDPOINT;
case kMIDINoConnection: return IN_DISCONNECTRESULT_NOCONNECTION;
}
return IN_DISCONNECTRESULT_UNKNOWNERROR;
}
return IN_DISCONNECTRESULT_OK;
}
IN_GETEVENTDATARESULT GetEventDataFromInputDevice(MIDIPacketList* packetList, int packetIndex, Byte** data, int* length, int* packetsCount)
{
*packetsCount = packetList->numPackets;
if (packetIndex == 0)
{
*data = packetList->packet[0].data;
*length = packetList->packet[0].length;
return IN_GETEVENTDATARESULT_OK;
}
MIDIPacket* packetPtr = packetList->packet;
for (int i = 0; i < packetIndex; i++)
{
packetPtr = MIDIPacketNext(packetPtr);
}
*data = packetPtr->data;
*length = packetPtr->length;
return IN_GETEVENTDATARESULT_OK;
}
char IsInputDevicePropertySupported(IN_PROPERTY property)
{
switch (property)
{
case IN_PROPERTY_PRODUCT:
case IN_PROPERTY_MANUFACTURER:
case IN_PROPERTY_DRIVERVERSION:
case IN_PROPERTY_UNIQUEID:
case IN_PROPERTY_DRIVEROWNER:
return 1;
}
return 0;
}
/* ================================
Output device
================================ */
typedef struct
{
OutputDeviceInfo* info;
MIDIPortRef portRef;
} OutputDeviceHandle;
int GetOutputDevicesCount()
{
return (int)MIDIGetNumberOfDestinations();
}
OUT_GETINFORESULT GetOutputDeviceInfo(int deviceIndex, void** info)
{
OutputDeviceInfo* outputDeviceInfo = malloc(sizeof(OutputDeviceInfo));
MIDIEndpointRef endpointRef = MIDIGetDestination(deviceIndex);
outputDeviceInfo->endpointRef = endpointRef;
*info = outputDeviceInfo;
return OUT_GETINFORESULT_OK;
}
int GetOutputDeviceHashCode(void* info)
{
OutputDeviceInfo* outputDeviceInfo = (OutputDeviceInfo*)info;
return outputDeviceInfo->endpointRef;
}
char AreOutputDevicesEqual(void* info1, void* info2)
{
OutputDeviceInfo* outputDeviceInfo1 = (OutputDeviceInfo*)info1;
OutputDeviceInfo* outputDeviceInfo2 = (OutputDeviceInfo*)info2;
return (char)(outputDeviceInfo1->endpointRef == outputDeviceInfo2->endpointRef);
}
OUT_GETPROPERTYRESULT GetOutputDeviceStringPropertyValue(OutputDeviceInfo* outputDeviceInfo, CFStringRef propertyID, char** value)
{
OSStatus status = GetDevicePropertyValue(outputDeviceInfo->endpointRef, propertyID, value);
if (status != noErr)
{
switch (status)
{
case kMIDIUnknownEndpoint: return OUT_GETPROPERTYRESULT_UNKNOWNENDPOINT;
case SMALL_BUFFER_ERROR: return OUT_GETPROPERTYRESULT_TOOLONG;
case kMIDIUnknownProperty: return OUT_GETPROPERTYRESULT_UNKNOWNPROPERTY;
}
return OUT_GETPROPERTYRESULT_UNKNOWNERROR;
}
return OUT_GETPROPERTYRESULT_OK;
}
OUT_GETPROPERTYRESULT GetOutputDeviceIntPropertyValue(OutputDeviceInfo* outputDeviceInfo, CFStringRef propertyID, int* value)
{
OSStatus status = MIDIObjectGetIntegerProperty(outputDeviceInfo->endpointRef, propertyID, value);
if (status != noErr)
{
switch (status)
{
case kMIDIUnknownEndpoint: return OUT_GETPROPERTYRESULT_UNKNOWNENDPOINT;
case kMIDIUnknownProperty: return OUT_GETPROPERTYRESULT_UNKNOWNPROPERTY;
}
return OUT_GETPROPERTYRESULT_UNKNOWNERROR;
}
return OUT_GETPROPERTYRESULT_OK;
}
OUT_GETPROPERTYRESULT GetOutputDeviceName(void* info, char** value)
{
OutputDeviceInfo* outputDeviceInfo = (OutputDeviceInfo*)info;
return GetOutputDeviceStringPropertyValue(outputDeviceInfo, kMIDIPropertyDisplayName, value);
}
OUT_GETPROPERTYRESULT GetOutputDeviceManufacturer(void* info, char** value)
{
OutputDeviceInfo* outputDeviceInfo = (OutputDeviceInfo*)info;
return GetOutputDeviceStringPropertyValue(outputDeviceInfo, kMIDIPropertyManufacturer, value);
}
OUT_GETPROPERTYRESULT GetOutputDeviceProduct(void* info, char** value)
{
OutputDeviceInfo* outputDeviceInfo = (OutputDeviceInfo*)info;
return GetOutputDeviceStringPropertyValue(outputDeviceInfo, kMIDIPropertyModel, value);
}
OUT_GETPROPERTYRESULT GetOutputDeviceDriverVersion(void* info, int* value)
{
OutputDeviceInfo* outputDeviceInfo = (OutputDeviceInfo*)info;
return GetOutputDeviceIntPropertyValue(outputDeviceInfo, kMIDIPropertyDriverVersion, value);
}
OUT_GETPROPERTYRESULT GetOutputDeviceUniqueId(void* info, int* value)
{
OutputDeviceInfo* outputDeviceInfo = (OutputDeviceInfo*)info;
return GetOutputDeviceIntPropertyValue(outputDeviceInfo, kMIDIPropertyUniqueID, value);
}
OUT_GETPROPERTYRESULT GetOutputDeviceDriverOwner(void* info, char** value)
{
OutputDeviceInfo* outputDeviceInfo = (OutputDeviceInfo*)info;
return GetOutputDeviceStringPropertyValue(outputDeviceInfo, kMIDIPropertyDriverOwner, value);
}
OUT_OPENRESULT OpenOutputDevice_Mac(void* info, void* sessionHandle, void** handle)
{
OutputDeviceInfo* outputDeviceInfo = (OutputDeviceInfo*)info;
SessionHandle* pSessionHandle = (SessionHandle*)sessionHandle;
OutputDeviceHandle* outputDeviceHandle = malloc(sizeof(OutputDeviceHandle));
outputDeviceHandle->info = outputDeviceInfo;
*handle = outputDeviceHandle;
CFStringRef portNameRef = CFSTR("OUT");
OSStatus result = MIDIOutputPortCreate(pSessionHandle->clientRef, portNameRef, &outputDeviceHandle->portRef);
if (result != noErr)
{
switch (result)
{
case kMIDIInvalidClient: return OUT_OPENRESULT_INVALIDCLIENT;
case kMIDIWrongThread: return OUT_OPENRESULT_WRONGTHREAD;
case kMIDINotPermitted: return OUT_OPENRESULT_NOTPERMITTED;
}
return OUT_OPENRESULT_UNKNOWNERROR;
}
return OUT_OPENRESULT_OK;
}
OUT_CLOSERESULT CloseOutputDevice(void* handle)
{
OutputDeviceHandle* outputDeviceHandle = (OutputDeviceHandle*)handle;
free(outputDeviceHandle->info);
free(outputDeviceHandle);
return OUT_CLOSERESULT_OK;
}
OUT_SENDSHORTRESULT SendShortEventToOutputDevice(void* handle, int message)
{
OutputDeviceHandle* outputDeviceHandle = (OutputDeviceHandle*)handle;
Byte data[3];
Byte statusByte = (Byte)(message & 0xFF);
data[0] = statusByte;
ByteCount dataSize = 1;
if (statusByte < 0xF8 && statusByte != 0xF6)
{
data[1] = (Byte)((message >> 8) & 0xFF);
dataSize++;
Byte channelStatus = (Byte)(statusByte >> 4);
if (channelStatus == 0x8 || channelStatus == 0x9 || channelStatus == 0xA || channelStatus == 0xB || channelStatus == 0xE || statusByte == 0xF2)
{
data[2] = (Byte)(message >> 16);
dataSize++;
}
}
Byte buffer[dataSize + (sizeof(MIDIPacketList))];
MIDIPacketList* packetList = (MIDIPacketList*)buffer;
MIDIPacket* packet = MIDIPacketListInit(packetList);
MIDIPacketListAdd(packetList, sizeof(buffer), packet, 0, dataSize, &data[0]);
OSStatus result = MIDISend(outputDeviceHandle->portRef, outputDeviceHandle->info->endpointRef, packetList);
if (result != noErr)
{
switch (result)
{
case kMIDIInvalidClient: return OUT_SENDSHORTRESULT_INVALIDCLIENT;
case kMIDIInvalidPort: return OUT_SENDSHORTRESULT_INVALIDPORT;
case kMIDIWrongEndpointType: return OUT_SENDSHORTRESULT_WRONGENDPOINT;
case kMIDIUnknownEndpoint: return OUT_SENDSHORTRESULT_UNKNOWNENDPOINT;
case kMIDIMessageSendErr: return OUT_SENDSHORTRESULT_COMMUNICATIONERROR;
case kMIDIServerStartErr: return OUT_SENDSHORTRESULT_SERVERSTARTERROR;
case kMIDIWrongThread: return OUT_SENDSHORTRESULT_WRONGTHREAD;
case kMIDINotPermitted: return OUT_SENDSHORTRESULT_NOTPERMITTED;
}
return OUT_SENDSHORTRESULT_UNKNOWNERROR;
}
return OUT_SENDSHORTRESULT_OK;
}
OUT_SENDSYSEXRESULT SendSysExEventToOutputDevice_Mac(void* handle, Byte* data, ByteCount dataSize)
{
OutputDeviceHandle* outputDeviceHandle = (OutputDeviceHandle*)handle;
Byte buffer[dataSize + (sizeof(MIDIPacketList))];
MIDIPacketList* packetList = (MIDIPacketList*)buffer;
MIDIPacket* packet = MIDIPacketListInit(packetList);
MIDIPacketListAdd(packetList, sizeof(buffer), packet, 0, dataSize, &data[0]);
OSStatus result = MIDISend(outputDeviceHandle->portRef, outputDeviceHandle->info->endpointRef, packetList);
if (result != noErr)
{
switch (result)
{
case kMIDIInvalidClient: return OUT_SENDSYSEXRESULT_INVALIDCLIENT;
case kMIDIInvalidPort: return OUT_SENDSYSEXRESULT_INVALIDPORT;
case kMIDIWrongEndpointType: return OUT_SENDSYSEXRESULT_WRONGENDPOINT;
case kMIDIUnknownEndpoint: return OUT_SENDSYSEXRESULT_UNKNOWNENDPOINT;
case kMIDIMessageSendErr: return OUT_SENDSYSEXRESULT_COMMUNICATIONERROR;
case kMIDIServerStartErr: return OUT_SENDSYSEXRESULT_SERVERSTARTERROR;
case kMIDIWrongThread: return OUT_SENDSYSEXRESULT_WRONGTHREAD;
case kMIDINotPermitted: return OUT_SENDSYSEXRESULT_NOTPERMITTED;
}
return OUT_SENDSYSEXRESULT_UNKNOWNERROR;
}
return OUT_SENDSYSEXRESULT_OK;
}
char IsOutputDevicePropertySupported(OUT_PROPERTY property)
{
switch (property)
{
case OUT_PROPERTY_PRODUCT:
case OUT_PROPERTY_MANUFACTURER:
case OUT_PROPERTY_DRIVERVERSION:
case OUT_PROPERTY_UNIQUEID:
case OUT_PROPERTY_DRIVEROWNER:
return 1;
}
return 0;
}
/* ================================
Virtual device
================================ */
typedef struct
{
InputDeviceInfo* inputDeviceInfo;
OutputDeviceInfo* outputDeviceInfo;
char* name;
} VirtualDeviceInfo;
VIRTUAL_OPENRESULT OpenVirtualDevice_Mac(char* name, void* sessionHandle, MIDIReadProc callback, void** info)
{
SessionHandle* pSessionHandle = (SessionHandle*)sessionHandle;
VirtualDeviceInfo* virtualDeviceInfo = malloc(sizeof(VirtualDeviceInfo));
virtualDeviceInfo->name = name;
CFStringRef nameRef = CFStringCreateWithCString(NULL, name, kCFStringEncodingUTF8);
MIDIEndpointRef sourceRef;
OSStatus status = MIDISourceCreate(pSessionHandle->clientRef, nameRef, &sourceRef);
if (status != noErr)
{
switch (status)
{
case kMIDIServerStartErr: return VIRTUAL_OPENRESULT_CREATESOURCE_SERVERSTARTERROR;
case kMIDIWrongThread: return VIRTUAL_OPENRESULT_CREATESOURCE_WRONGTHREAD;
case kMIDINotPermitted: return VIRTUAL_OPENRESULT_CREATESOURCE_NOTPERMITTED;
}
return VIRTUAL_OPENRESULT_CREATESOURCE_UNKNOWNERROR;
}
InputDeviceInfo* inputDeviceInfo = malloc(sizeof(InputDeviceInfo));
inputDeviceInfo->endpointRef = sourceRef;
virtualDeviceInfo->inputDeviceInfo = inputDeviceInfo;
MIDIEndpointRef destinationRef;
status = MIDIDestinationCreate(pSessionHandle->clientRef, nameRef, callback, inputDeviceInfo, &destinationRef);
if (status != noErr)
{
switch (status)
{
case kMIDIServerStartErr: return VIRTUAL_OPENRESULT_CREATEDESTINATION_SERVERSTARTERROR;
case kMIDIWrongThread: return VIRTUAL_OPENRESULT_CREATEDESTINATION_WRONGTHREAD;
case kMIDINotPermitted: return VIRTUAL_OPENRESULT_CREATEDESTINATION_NOTPERMITTED;
}
return VIRTUAL_OPENRESULT_CREATEDESTINATION_UNKNOWNERROR;
}
OutputDeviceInfo* outputDeviceInfo = malloc(sizeof(OutputDeviceInfo));
outputDeviceInfo->endpointRef = destinationRef;
virtualDeviceInfo->outputDeviceInfo = outputDeviceInfo;
*info = virtualDeviceInfo;
return VIRTUAL_OPENRESULT_OK;
}
VIRTUAL_CLOSERESULT CloseVirtualDevice(void* info)
{
VirtualDeviceInfo* virtualDeviceInfo = (VirtualDeviceInfo*)info;
OSStatus status = MIDIEndpointDispose(virtualDeviceInfo->inputDeviceInfo->endpointRef);
if (status != noErr)
{
switch (status)
{
case kMIDIUnknownEndpoint: return VIRTUAL_CLOSERESULT_DISPOSESOURCE_UNKNOWNENDPOINT;
case kMIDINotPermitted: return VIRTUAL_CLOSERESULT_DISPOSESOURCE_NOTPERMITTED;
}
return VIRTUAL_CLOSERESULT_DISPOSESOURCE_UNKNOWNERROR;
}
status = MIDIEndpointDispose(virtualDeviceInfo->outputDeviceInfo->endpointRef);
if (status != noErr)
{
switch (status)
{
case kMIDIUnknownEndpoint: return VIRTUAL_CLOSERESULT_DISPOSEDESTINATION_UNKNOWNENDPOINT;
case kMIDINotPermitted: return VIRTUAL_CLOSERESULT_DISPOSEDESTINATION_NOTPERMITTED;
}
return VIRTUAL_CLOSERESULT_DISPOSEDESTINATION_UNKNOWNERROR;
}
free(virtualDeviceInfo);
return VIRTUAL_CLOSERESULT_OK;
}
VIRTUAL_SENDBACKRESULT SendDataBackFromVirtualDevice(const MIDIPacketList *pktlist, void *readProcRefCon)
{
InputDeviceInfo* inputDeviceInfo = (InputDeviceInfo*)readProcRefCon;
OSStatus status = MIDIReceived(inputDeviceInfo->endpointRef, pktlist);
if (status != noErr)
{
switch (status)
{
case kMIDIUnknownEndpoint: return VIRTUAL_SENDBACKRESULT_UNKNOWNENDPOINT;
case kMIDINotPermitted: return VIRTUAL_SENDBACKRESULT_NOTPERMITTED;
case kMIDIWrongEndpointType: return VIRTUAL_SENDBACKRESULT_WRONGENDPOINT;
case kMIDIMessageSendErr: return VIRTUAL_SENDBACKRESULT_MESSAGESENDERROR;
case kMIDIServerStartErr: return VIRTUAL_SENDBACKRESULT_SERVERSTARTERROR;
case kMIDIWrongThread: return VIRTUAL_SENDBACKRESULT_WRONGTHREAD;
}
return VIRTUAL_SENDBACKRESULT_UNKNOWNERROR;
}
return VIRTUAL_SENDBACKRESULT_OK;
}
void* GetInputDeviceInfoFromVirtualDevice(void* info)
{
VirtualDeviceInfo* virtualDeviceInfo = (VirtualDeviceInfo*)info;
return virtualDeviceInfo->inputDeviceInfo;
}
void* GetOutputDeviceInfoFromVirtualDevice(void* info)
{
VirtualDeviceInfo* virtualDeviceInfo = (VirtualDeviceInfo*)info;
return virtualDeviceInfo->outputDeviceInfo;
}