-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcmidi2.h
2766 lines (2458 loc) · 118 KB
/
cmidi2.h
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
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#ifndef CMIDI2_H_INCLUDED
#define CMIDI2_H_INCLUDED
#include <stdbool.h>
#include <stdint.h>
#include <memory.h>
#define MIDI_2_0_RESERVED 0
#define JR_TIMESTAMP_TICKS_PER_SECOND 31250
#ifdef __cplusplus
extern "C" {
#endif
enum cmidi2_status_code {
CMIDI2_STATUS_NOTE_OFF = 0x80,
CMIDI2_STATUS_NOTE_ON = 0x90,
CMIDI2_STATUS_PAF = 0xA0,
CMIDI2_STATUS_CC = 0xB0,
CMIDI2_STATUS_PROGRAM = 0xC0,
CMIDI2_STATUS_CAF = 0xD0,
CMIDI2_STATUS_PITCH_BEND = 0xE0,
CMIDI2_STATUS_PER_NOTE_RCC = 0x00,
CMIDI2_STATUS_PER_NOTE_ACC = 0x10,
CMIDI2_STATUS_RPN = 0x20,
CMIDI2_STATUS_NRPN = 0x30,
CMIDI2_STATUS_RELATIVE_RPN = 0x40,
CMIDI2_STATUS_RELATIVE_NRPN = 0x50,
CMIDI2_STATUS_PER_NOTE_PITCH_BEND = 0x60,
CMIDI2_STATUS_PER_NOTE_MANAGEMENT = 0xF0,
};
enum cmidi2_message_type {
CMIDI2_MESSAGE_TYPE_UTILITY = 0,
CMIDI2_MESSAGE_TYPE_SYSTEM = 1,
CMIDI2_MESSAGE_TYPE_MIDI_1_CHANNEL = 2,
CMIDI2_MESSAGE_TYPE_SYSEX7 = 3,
CMIDI2_MESSAGE_TYPE_MIDI_2_CHANNEL = 4,
CMIDI2_MESSAGE_TYPE_SYSEX8_MDS = 5,
CMIDI2_MESSAGE_TYPE_FLEX_DATA = 0xD,
CMIDI2_MESSAGE_TYPE_UMP_STREAM = 0xF,
};
enum cmidi2_ci_protocol_bytes {
CMIDI2_PROTOCOL_BYTES_TYPE = 1,
CMIDI2_PROTOCOL_BYTES_VERSION = 2,
CMIDI2_PROTOCOL_BYTES_EXTENSIONS = 3,
};
enum cmidi2_ci_protocol_values {
CMIDI2_PROTOCOL_TYPE_MIDI1 = 1,
CMIDI2_PROTOCOL_TYPE_MIDI2 = 2,
CMIDI2_PROTOCOL_VERSION_MIDI1 = 0,
CMIDI2_PROTOCOL_VERSION_MIDI2_V1 = 0,
};
enum cmidi2_ci_protocol_extensions {
CMIDI2_PROTOCOL_EXTENSIONS_JITTER = 1,
CMIDI2_PROTOCOL_EXTENSIONS_LARGER = 2, // only for MIDI 1.0 compat UMP
};
typedef struct cmidi2_ci_protocol_tag {
uint8_t protocol_type; // cmidi2_ci_protocol_bytes
uint8_t version; // cmidi2_ci_protocol_values
uint8_t extensions; // cmidi2_ci_protocol_extensions (flags)
uint8_t reserved1;
uint8_t reserved2;
} cmidi2_ci_protocol;
enum cmidi2_cc {
CMIDI2_CC_BANK_SELECT = 0x00,
CMIDI2_CC_MODULATION = 0x01,
CMIDI2_CC_BREATH = 0x02,
CMIDI2_CC_FOOT = 0x04,
CMIDI2_CC_PORTAMENTO_TIME = 0x05,
CMIDI2_CC_DTE_MSB = 0x06,
CMIDI2_CC_VOLUME = 0x07,
CMIDI2_CC_BALANCE = 0x08,
CMIDI2_CC_PAN = 0x0A,
CMIDI2_CC_EXPRESSION = 0x0B,
CMIDI2_CC_EFFECT_CONTROL_1 = 0x0C,
CMIDI2_CC_EFFECT_CONTROL_2 = 0x0D,
CMIDI2_CC_GENERAL_1 = 0x10,
CMIDI2_CC_GENERAL_2 = 0x11,
CMIDI2_CC_GENERAL_3 = 0x12,
CMIDI2_CC_GENERAL_4 = 0x13,
CMIDI2_CC_BANK_SELECT_LSB = 0x20,
CMIDI2_CC_MODULATION_LSB = 0x21,
CMIDI2_CC_BREATH_LSB = 0x22,
CMIDI2_CC_FOOT_LSB = 0x24,
CMIDI2_CC_PORTAMENTO_TIME_LSB = 0x25,
CMIDI2_CC_DTE_LSB = 0x26,
CMIDI2_CC_VOLUME_LSB = 0x27,
CMIDI2_CC_BALANCE_LSB = 0x28,
CMIDI2_CC_PAN_LSB = 0x2A,
CMIDI2_CC_EXPRESSION_LSB = 0x2B,
CMIDI2_CC_EFFECT1_LSB = 0x2C,
CMIDI2_CC_EFFECT2_LSB = 0x2D,
CMIDI2_CC_GENERAL_1_LSB = 0x30,
CMIDI2_CC_GENERAL_2_LSB = 0x31,
CMIDI2_CC_GENERAL_3_LSB = 0x32,
CMIDI2_CC_GENERAL_4_LSB = 0x33,
CMIDI2_CC_HOLD = 0x40,
CMIDI2_CC_PORTAMENTO_SWITCH = 0x41,
CMIDI2_CC_SOSTENUTO = 0x42,
CMIDI2_CC_SOFT_PEDAL = 0x43,
CMIDI2_CC_LEGATO = 0x44,
CMIDI2_CC_HOLD_2 = 0x45,
CMIDI2_CC_SOUND_CONTROLLER_1 = 0x46,
CMIDI2_CC_SOUND_CONTROLLER_2 = 0x47,
CMIDI2_CC_SOUND_CONTROLLER_3 = 0x48,
CMIDI2_CC_SOUND_CONTROLLER_4 = 0x49,
CMIDI2_CC_SOUND_CONTROLLER_5 = 0x4A,
CMIDI2_CC_SOUND_CONTROLLER_6 = 0x4B,
CMIDI2_CC_SOUND_CONTROLLER_7 = 0x4C,
CMIDI2_CC_SOUND_CONTROLLER_8 = 0x4D,
CMIDI2_CC_SOUND_CONTROLLER_9 = 0x4E,
CMIDI2_CC_SOUND_CONTROLLER_10 = 0x4F,
CMIDI2_CC_GENERAL_5 = 0x50,
CMIDI2_CC_GENERAL_6 = 0x51,
CMIDI2_CC_GENERAL_7 = 0x52,
CMIDI2_CC_GENERAL_8 = 0x53,
CMIDI2_CC_PORTAMENTO_CONTROL = 0x54,
CMIDI2_CC_RSD = 0x5B,
CMIDI2_CC_EFFECT_1 = 0x5B,
CMIDI2_CC_TREMOLO = 0x5C,
CMIDI2_CC_EFFECT_2 = 0x5C,
CMIDI2_CC_CSD = 0x5D,
CMIDI2_CC_EFFECT_3 = 0x5D,
CMIDI2_CC_CELESTE = 0x5E,
CMIDI2_CC_EFFECT_4 = 0x5E,
CMIDI2_CC_PHASER = 0x5F,
CMIDI2_CC_EFFECT_5 = 0x5F,
CMIDI2_CC_DTE_INCREMENT = 0x60,
CMIDI2_CC_DTE_DECREMENT = 0x61,
CMIDI2_CC_NRPN_LSB = 0x62,
CMIDI2_CC_NRPN_MSB = 0x63,
CMIDI2_CC_RPN_LSB = 0x64,
CMIDI2_CC_RPN_MSB = 0x65,
// CHANNEL MODE MESSAGES
CMIDI2_CC_ALL_SOUND_OFF = 0x78,
CMIDI2_CC_RESET_ALL_CONTROLLERS = 0x79,
CMIDI2_CC_LOCAL_CONTROL = 0x7A,
CMIDI2_CC_ALL_NOTES_OFF = 0x7B,
CMIDI2_CC_OMNI_MODE_OFF = 0x7C,
CMIDI2_CC_OMNI_MODE_ON = 0x7D,
CMIDI2_CC_POLY_MODE_ON_OFF = 0x7E,
CMIDI2_CC_POLY_MODE_ON = 0x7F,
};
enum cmidi2_rpn {
CMIDI2_RPN_PITCH_BEND_SENSITIVITY = 0,
CMIDI2_RPN_FINE_TUNING = 1,
CMIDI2_RPN_COARSE_TUNING = 2,
CMIDI2_RPN_TUNING_PROGRAM = 3,
CMIDI2_RPN_TUNING_BANK_SELECT = 4,
CMIDI2_RPN_MODULATION_DEPTH = 5,
};
enum cmidi2_meta_event_type {
CMIDI2_META_SEQUENCE_NUMBER = 0X00,
CMIDI2_META_TEXT = 0X01,
CMIDI2_META_COPYRIGHT = 0X02,
CMIDI2_META_TRACK_NAME = 0X03,
CMIDI2_META_INSTRUMENT_NAME = 0X04,
CMIDI2_META_LYRIC = 0X05,
CMIDI2_META_MARKER = 0X06,
CMIDI2_META_CUE = 0X07,
CMIDI2_META_CHANNEL_PREFIX = 0X20,
CMIDI2_META_END_OF_TRACK = 0X2F,
CMIDI2_META_TEMPO = 0X51,
CMIDI2_META_SMPTE_OFFSET = 0X54,
CMIDI2_META_TIME_SIGNATURE = 0X58,
CMIDI2_META_KEY_SIGNATURE = 0X59,
CMIDI2_META_SEQUENCER_SPECIFIC = 0X7F,
};
enum cmidi2_per_note_management_flags {
CMIDI2_PER_NOTE_MANAGEMENT_RESET = 1,
CMIDI2_PER_NOTE_MANAGEMENT_DETACH = 2,
};
enum cmidi2_note_attribute_type {
CMIDI2_ATTRIBUTE_TYPE_NONE = 0,
CMIDI2_ATTRIBUTE_TYPE_MANUFACTURER = 1,
CMIDI2_ATTRIBUTE_TYPE_PROFILE = 2,
CMIDI2_ATTRIBUTE_TYPE_PITCH7_9 = 3,
};
enum cmidi2_program_change_option_flags {
CMIDI2_PROGRAM_CHANGE_OPTION_NONE = 0,
CMIDI2_PROGRAM_CHANGE_OPTION_BANK_VALID = 1,
};
enum cmidi2_sysex_status {
CMIDI2_SYSEX_IN_ONE_UMP = 0,
CMIDI2_SYSEX_START = 0x10,
CMIDI2_SYSEX_CONTINUE = 0x20,
CMIDI2_SYSEX_END = 0x30,
};
enum cmidi2_mixed_data_set_status {
CMIDI2_MIXED_DATA_STATUS_HEADER = 0x80,
CMIDI2_MIXED_DATA_STATUS_PAYLOAD = 0x90,
};
enum cmidi2_system_message_status {
CMIDI2_SYSTEM_STATUS_MIDI_TIME_CODE = 0xF1,
CMIDI2_SYSTEM_STATUS_SONG_POSITION = 0xF2,
CMIDI2_SYSTEM_STATUS_SONG_SELECT = 0xF3,
CMIDI2_SYSTEM_STATUS_TUNE_REQUEST = 0xF6,
CMIDI2_SYSTEM_STATUS_TIMING_CLOCK = 0xF8,
CMIDI2_SYSTEM_STATUS_START = 0xFA,
CMIDI2_SYSTEM_STATUS_CONTINUE = 0xFB,
CMIDI2_SYSTEM_STATUS_STOP = 0xFC,
CMIDI2_SYSTEM_STATUS_ACTIVE_SENSING = 0xFE,
CMIDI2_SYSTEM_STATUS_RESET = 0xFF,
};
// TODO: remove this. Use cmidi2_utility_message_status instead
enum cmidi2_jr_timestamp_status {
CMIDI2_JR_CLOCK = 0x10,
CMIDI2_JR_TIMESTAMP = 0x20,
};
enum cmidi2_utility_message_status {
CMIDI2_UTILITY_STATUS_NOOP = 0,
CMIDI2_UTILITY_STATUS_JR_CLOCK = 0x10,
CMIDI2_UTILITY_STATUS_JR_TIMESTAMP = 0x20,
CMIDI2_UTILITY_STATUS_DCTPQ = 0x30,
CMIDI2_UTILITY_STATUS_DELTA_CLOCKSTAMP = 0x40,
};
enum cmidi2_flex_data_status_bank {
CMIDI2_FLEX_DATA_BANK_SETUP_AND_PERFORMANCE = 0,
CMIDI2_FLEX_DATA_BANK_METADATA_TEXT = 1,
CMIDI2_FLEX_DATA_BANK_PERFORMANCE_TEXT = 2
};
enum cmidi2_flex_data_setup_status {
CMIDI2_FLEX_DATA_STATUS_SET_TEMPO = 0,
CMIDI2_FLEX_DATA_STATUS_SET_TIME_SIGNATURE = 1,
CMIDI2_FLEX_DATA_STATUS_SET_METRONOME = 2,
CMIDI2_FLEX_DATA_STATUS_SET_KEY_SIGNATURE = 5,
CMIDI2_FLEX_DATA_STATUS_SET_CHORD_NAME = 6
};
enum cmidi2_flex_data_metadata_text_status {
CMIDI2_FLEX_DATA_STATUS_UNKNOWN_METADATA_TEXT = 0,
CMIDI2_FLEX_DATA_STATUS_PROJECT_NAME = 1,
CMIDI2_FLEX_DATA_STATUS_SONG_NAME = 2,
CMIDI2_FLEX_DATA_STATUS_CLIP_NAME = 3,
CMIDI2_FLEX_DATA_STATUS_COPYRIGHT_NAME = 4,
CMIDI2_FLEX_DATA_STATUS_COMPOSER_NAME = 5,
CMIDI2_FLEX_DATA_STATUS_LYRICIST_NAME = 6,
CMIDI2_FLEX_DATA_STATUS_ARRANGER_NAME = 7,
CMIDI2_FLEX_DATA_STATUS_PUBLISHER_NAME = 8,
CMIDI2_FLEX_DATA_STATUS_PRIMARY_PERFORMER_NAME = 9,
CMIDI2_FLEX_DATA_STATUS_ACCOMPANYING_PERFORMAER_NAME = 10,
CMIDI2_FLEX_DATA_STATUS_RECORDING_DATE = 11,
CMIDI2_FLEX_DATA_STATUS_RECORDING_LOCATION = 12
};
enum cmidi2_flex_data_performance_text_status {
CMIDI2_FLEX_DATA_STATUS_UNKNOWN_PERFORMANCE_TEXT = 0,
CMIDI2_FLEX_DATA_STATUS_LYRICS = 1,
CMIDI2_FLEX_DATA_STATUS_LYRICS_LANGUAGE = 2,
CMIDI2_FLEX_DATA_STATUS_RUBY = 3,
CMIDI2_FLEX_DATA_STATUS_RUBY_LANGUAGE = 4
};
enum cmidi2_ump_chord_name_sharps_flats {
CMIDI2_UMP_CHORD_NAME_DOUBLE_SHARP = 2,
CMIDI2_UMP_CHORD_NAME_SHARP = 1,
CMIDI2_UMP_CHORD_NAME_NATURAL = 0,
CMIDI2_UMP_CHORD_NAME_FLAT = 0xF,
CMIDI2_UMP_CHORD_NAME_DOUBLE_FLAT = 0xE,
};
enum cmidi2_ump_chord_name_tonic_note {
CMIDI2_UMP_CHORD_NAME_UNKNOWN = 0,
CMIDI2_UMP_CHORD_NAME_A = 1,
CMIDI2_UMP_CHORD_NAME_B = 2,
CMIDI2_UMP_CHORD_NAME_C = 3,
CMIDI2_UMP_CHORD_NAME_D = 4,
CMIDI2_UMP_CHORD_NAME_E = 5,
CMIDI2_UMP_CHORD_NAME_F = 6,
CMIDI2_UMP_CHORD_NAME_G = 7,
};
enum cmidi2_ump_chord_name_chord_type {
CMIDI2_UMP_CHORD_TYPE_UNKNOWN = 0,
CMIDI2_UMP_CHORD_TYPE_MAJOR = 1,
CMIDI2_UMP_CHORD_TYPE_MAJOR_6TH = 2,
CMIDI2_UMP_CHORD_TYPE_MAJOR_7TH = 3,
CMIDI2_UMP_CHORD_TYPE_MAJOR_9TH = 4,
CMIDI2_UMP_CHORD_TYPE_MAJOR_11TH = 5,
CMIDI2_UMP_CHORD_TYPE_MAJOR_13TH = 6,
CMIDI2_UMP_CHORD_TYPE_MINOR = 7,
CMIDI2_UMP_CHORD_TYPE_MINOR_6TH = 8,
CMIDI2_UMP_CHORD_TYPE_MINOR_7TH = 9,
CMIDI2_UMP_CHORD_TYPE_MINOR_9TH = 10,
CMIDI2_UMP_CHORD_TYPE_MINOR_11TH = 11,
CMIDI2_UMP_CHORD_TYPE_MINOR_13TH = 12,
CMIDI2_UMP_CHORD_TYPE_DOMINANT = 13,
CMIDI2_UMP_CHORD_TYPE_DOMINANT_9TH = 14,
CMIDI2_UMP_CHORD_TYPE_DOMINANT_11TH = 15,
CMIDI2_UMP_CHORD_TYPE_DOMINANT_13TH = 16,
CMIDI2_UMP_CHORD_TYPE_AUGMENTED = 17,
CMIDI2_UMP_CHORD_TYPE_AUGMENTED_7TH = 18,
CMIDI2_UMP_CHORD_TYPE_DIMINISHED = 19,
CMIDI2_UMP_CHORD_TYPE_DIMINISHED_7TH = 20,
CMIDI2_UMP_CHORD_TYPE_HALF_DIMINISHED = 21,
CMIDI2_UMP_CHORD_TYPE_MAJOR_MINOR = 22,
CMIDI2_UMP_CHORD_TYPE_MINOR_MAJOR = 22, // same
CMIDI2_UMP_CHORD_TYPE_PEDAL = 23,
CMIDI2_UMP_CHORD_TYPE_POWER = 24,
CMIDI2_UMP_CHORD_TYPE_SUSPENDED_2ND = 25,
CMIDI2_UMP_CHORD_TYPE_SUSPENDED_4TH = 26,
CMIDI2_UMP_CHORD_TYPE_7_SUSPENDED_4TH = 27,
};
static inline uint8_t cmidi2_ump_get_num_bytes(uint32_t data) {
switch (((data & 0xF0000000) >> 28) & 0xF) {
case CMIDI2_MESSAGE_TYPE_UTILITY:
case CMIDI2_MESSAGE_TYPE_SYSTEM:
case CMIDI2_MESSAGE_TYPE_MIDI_1_CHANNEL:
return 4;
case CMIDI2_MESSAGE_TYPE_MIDI_2_CHANNEL:
case CMIDI2_MESSAGE_TYPE_SYSEX7:
return 8;
case CMIDI2_MESSAGE_TYPE_SYSEX8_MDS:
return 16;
}
return 0xFF; /* wrong */
}
typedef struct cmidi2_ump128 {
uint32_t p1;
uint32_t p2;
uint32_t p3;
uint32_t p4;
} cmidi2_ump128_t;
typedef struct cmidi2_ump_version {
uint8_t major;
uint8_t minor;
} cmidi2_ump_version_t;
enum cmidi2_ump_stream_status {
CMIDI2_UMP_STREAM_STATUS_ENDPOINT_DISCOVERY = 0,
CMIDI2_UMP_STREAM_STATUS_ENDPOINT_INFO = 1,
CMIDI2_UMP_STREAM_STATUS_DEVICE_IDENTITY = 2,
CMIDI2_UMP_STREAM_STATUS_ENDPOINT_NAME = 3,
CMIDI2_UMP_STREAM_STATUS_PRODUCT_INSTANCE_ID = 4,
CMIDI2_UMP_STREAM_STATUS_STREAM_CONFIGURATION_REQUEST = 5,
CMIDI2_UMP_STREAM_STATUS_STREAM_CONFIGURATION_NOTIFICATION = 6,
CMIDI2_UMP_STREAM_STATUS_FUNCTION_BLOCK_DISCOVERY = 0x10,
CMIDI2_UMP_STREAM_STATUS_FUNCTION_BLOCK_INFO = 0x11,
CMIDI2_UMP_STREAM_STATUS_FUNCTION_BLOCK_NAME = 0x12,
CMIDI2_UMP_STREAM_STATUS_START_OF_CLIP = 0x20,
CMIDI2_UMP_STREAM_STATUS_END_OF_CLIP = 0x21,
};
enum cmidi2_ump_endpoint_filter_flags {
CMIDI2_UMP_ENDPOINT_FILTER_ENDPOINT_INFO = 1,
CMIDI2_UMP_ENDPOINT_FILTER_DEVICE_IDENTITY = 2,
CMIDI2_UMP_ENDPOINT_FILTER_ENDPOINT_NAME = 4,
CMIDI2_UMP_ENDPOINT_FILTER_PRODUCT_INSTANCE_ID = 8,
CMIDI2_UMP_ENDPOINT_FILTER_STREAM_CONFIGURATION = 0x10,
};
enum cmidi2_ump_function_block_discovery_flags {
CMIDI2_UMP_FUNCTION_BLOCK_FILTER_INFO = 1,
CMIDI2_UMP_FUNCTION_BLOCK_FILTER_NAME = 2,
};
// --------
// UMP generators
// 7.1 UMP Stream Messages
static inline cmidi2_ump128_t cmidi2_ump_endpoint_discovery(cmidi2_ump_version_t version, uint8_t filterBitmap) {
cmidi2_ump128_t ret = {
(uint32_t) ((CMIDI2_MESSAGE_TYPE_UMP_STREAM << 28) + (CMIDI2_UMP_STREAM_STATUS_ENDPOINT_DISCOVERY << 16) + (version.major << 8) + version.minor),
(uint32_t) (filterBitmap & 0x1F),
0,
0
};
return ret;
}
static inline cmidi2_ump128_t cmidi2_ump_endpoint_info_notification(cmidi2_ump_version_t version, bool isStaticFunctionBlock, uint8_t numFunctionBlocks, bool midi2Capable, bool midi1Capable, bool rxJR, bool txJR) {
cmidi2_ump128_t ret = {
(uint32_t) (CMIDI2_MESSAGE_TYPE_UMP_STREAM << 28) + (CMIDI2_UMP_STREAM_STATUS_ENDPOINT_INFO << 16) + (version.major << 8) + version.minor,
(uint32_t) (isStaticFunctionBlock ? 1 << 31 : 0) + (numFunctionBlocks << 24) + (midi2Capable ? 0x1000 : 0) + (midi1Capable ? 0x100 : 0) + (rxJR ? 2 : 0) + (txJR ? 1 : 0),
0,
0
};
return ret;
}
static inline cmidi2_ump128_t cmidi2_ump_device_identity_notification(uint32_t manufacturerIdIn7bitArray, uint8_t deviceFamilyLSB, uint8_t deviceFamilyMSB, uint8_t deviceFamilyModelLSB, uint8_t deviceFamilyModelMSB, uint32_t softwareRevisionIn7bitArray) {
(void) deviceFamilyModelMSB;
cmidi2_ump128_t ret = {
(uint32_t) (CMIDI2_MESSAGE_TYPE_UMP_STREAM << 28) + (CMIDI2_UMP_STREAM_STATUS_DEVICE_IDENTITY << 16),
manufacturerIdIn7bitArray,
(uint32_t) (deviceFamilyLSB << 24) + (deviceFamilyMSB << 16) + (deviceFamilyModelLSB << 8) + deviceFamilyMSB,
softwareRevisionIn7bitArray
};
return ret;
}
static inline cmidi2_ump128_t cmidi2_ump_internal_name_notification(uint8_t statusCode, const char name[14]) {
cmidi2_ump128_t ret = {
(uint32_t) (CMIDI2_MESSAGE_TYPE_UMP_STREAM << 28) + (statusCode << 16) + (name[0] << 8) + name[1],
(uint32_t) (name[2] << 24) + (name[3] << 16) + (name[4] << 8) + name[5],
(uint32_t) (name[6] << 24) + (name[7] << 16) + (name[8] << 8) + name[9],
(uint32_t) (name[10] << 24) + (name[11] << 16) + (name[12] << 8) + name[13]
};
return ret;
}
static inline cmidi2_ump128_t cmidi2_ump_endpoint_name_notification(const char name[14]) {
return cmidi2_ump_internal_name_notification( CMIDI2_UMP_STREAM_STATUS_ENDPOINT_NAME, name);
}
static inline cmidi2_ump128_t cmidi2_ump_product_instance_id_notification(const char id[14]) {
return cmidi2_ump_internal_name_notification( CMIDI2_UMP_STREAM_STATUS_PRODUCT_INSTANCE_ID, id);
}
static inline cmidi2_ump128_t cmidi2_ump_stream_configuration_request(uint8_t protocol, bool rxJR, bool txJR) {
cmidi2_ump128_t ret = {
(uint32_t) (CMIDI2_MESSAGE_TYPE_UMP_STREAM << 28) + (CMIDI2_UMP_STREAM_STATUS_STREAM_CONFIGURATION_REQUEST << 16) + (protocol << 8) + (rxJR ? 2 : 0) + (txJR ? 1 : 0),
0,
0,
0};
return ret;
}
static inline cmidi2_ump128_t cmidi2_ump_stream_configuration_notification(uint8_t protocol, bool rxJR, bool txJR) {
cmidi2_ump128_t ret = {
(uint32_t) (CMIDI2_MESSAGE_TYPE_UMP_STREAM << 28) + (CMIDI2_UMP_STREAM_STATUS_STREAM_CONFIGURATION_NOTIFICATION << 16) + (protocol << 8) + (rxJR ? 2 : 0) + (txJR ? 1 : 0),
0,
0,
0};
return ret;
}
static inline cmidi2_ump128_t cmidi2_ump_function_block_discovery(uint8_t numFunctionBlocks, uint8_t filter) {
cmidi2_ump128_t ret = {
(uint32_t) (CMIDI2_MESSAGE_TYPE_UMP_STREAM << 28) + (CMIDI2_UMP_STREAM_STATUS_FUNCTION_BLOCK_DISCOVERY << 16) + (numFunctionBlocks << 8) + filter,
0,
0,
0
};
return ret;
}
static inline cmidi2_ump128_t cmidi2_ump_function_block_info_notification(bool active, uint8_t numFunctionBlocks, uint8_t uiHint, uint8_t midi1, uint8_t direction, uint8_t firstGroup, uint8_t numSpannedGroup, uint8_t midiCIVersionFormat, uint8_t maxNumSysEx8Streams) {
cmidi2_ump128_t ret = {
(uint32_t) (CMIDI2_MESSAGE_TYPE_UMP_STREAM << 28) + (CMIDI2_UMP_STREAM_STATUS_FUNCTION_BLOCK_INFO << 16) + (active ? 0x8000 : 0) + (numFunctionBlocks << 8) + (uiHint << 4) + (midi1 << 2) + direction,
(uint32_t) (firstGroup << 24) + (numSpannedGroup << 16) + (midiCIVersionFormat << 8) + maxNumSysEx8Streams,
0,
0
};
return ret;
}
static inline cmidi2_ump128_t cmidi2_ump_function_block_name_notification(const char name[14]) {
return cmidi2_ump_internal_name_notification( CMIDI2_UMP_STREAM_STATUS_FUNCTION_BLOCK_NAME, name);
}
static inline cmidi2_ump128_t cmidi2_ump_start_of_clip() {
cmidi2_ump128_t ret = {(uint32_t) (CMIDI2_MESSAGE_TYPE_UMP_STREAM << 28) + (CMIDI2_UMP_STREAM_STATUS_START_OF_CLIP << 16), 0, 0, 0};
return ret;
}
static inline cmidi2_ump128_t cmidi2_ump_end_of_clip() {
cmidi2_ump128_t ret = {(uint32_t) (CMIDI2_MESSAGE_TYPE_UMP_STREAM << 28) + (CMIDI2_UMP_STREAM_STATUS_END_OF_CLIP << 16), 0, 0, 0};
return ret;
}
// 7.2 Utility Messages
static inline uint32_t cmidi2_ump_noop(uint8_t group) { return (group & 0xF) << 24; }
static inline uint32_t cmidi2_ump_jr_clock_direct(uint8_t group, uint32_t senderClockTime) {
return cmidi2_ump_noop(group) + (CMIDI2_UTILITY_STATUS_JR_CLOCK << 16) + senderClockTime;
}
static inline uint32_t cmidi2_ump_jr_clock(uint8_t group, double senderClockTime) {
uint16_t value = (uint16_t) (senderClockTime * JR_TIMESTAMP_TICKS_PER_SECOND);
return cmidi2_ump_noop(group) + (CMIDI2_UTILITY_STATUS_JR_CLOCK << 16) + value;
}
static inline uint32_t cmidi2_ump_jr_timestamp_direct(uint8_t group, uint16_t senderClockTimestamp) {
return cmidi2_ump_noop(group) + (CMIDI2_UTILITY_STATUS_JR_TIMESTAMP << 16) + senderClockTimestamp;
}
static inline uint32_t cmidi2_ump_jr_timestamp(uint8_t group, double senderClockTimestamp) {
uint16_t value = (uint16_t) (senderClockTimestamp * JR_TIMESTAMP_TICKS_PER_SECOND);
return cmidi2_ump_noop(group) + (CMIDI2_UTILITY_STATUS_JR_TIMESTAMP << 16) + value;
}
static inline uint32_t cmidi2_ump_dctpq(uint8_t group, uint32_t dctpq) {
return cmidi2_ump_noop(group) + (CMIDI2_UTILITY_STATUS_DCTPQ << 16) + dctpq;
}
static inline uint32_t cmidi2_ump_dcs(uint8_t group, uint32_t ticks) {
// Note that unlike JR timestamp delta clockstamps accepts ticks up to 20 bits.
return cmidi2_ump_noop(group) + (CMIDI2_UTILITY_STATUS_DELTA_CLOCKSTAMP << 16) + (ticks & 0xFFFFF);
}
// 7.6 System Common and System Real Time Messages
static inline int32_t cmidi2_ump_system_message(uint8_t group, uint8_t status, uint8_t midi1Byte2, uint8_t midi1Byte3) {
return (CMIDI2_MESSAGE_TYPE_SYSTEM << 28) + ((group & 0xF) << 24) + (status << 16) + ((midi1Byte2 & 0x7F) << 8) + (midi1Byte3 & 0x7F);
}
// 7.3 MIDI 1.0 Channel Voice Messages
static inline int32_t cmidi2_ump_midi1_message(uint8_t group, uint8_t code, uint8_t channel, uint8_t byte3, uint8_t byte4) {
return (CMIDI2_MESSAGE_TYPE_MIDI_1_CHANNEL << 28) + ((group & 0xF) << 24) + (((code & 0xF0) + (channel & 0xF)) << 16) + ((byte3 & 0x7F) << 8) + (byte4 & 0x7F);
}
static inline int32_t cmidi2_ump_midi1_note_off(uint8_t group, uint8_t channel, uint8_t note, uint8_t velocity) {
return cmidi2_ump_midi1_message(group, CMIDI2_STATUS_NOTE_OFF, channel, note & 0x7F, velocity & 0x7F);
}
static inline int32_t cmidi2_ump_midi1_note_on(uint8_t group, uint8_t channel, uint8_t note, uint8_t velocity) {
return cmidi2_ump_midi1_message(group, CMIDI2_STATUS_NOTE_ON, channel, note & 0x7F, velocity & 0x7F);
}
static inline int32_t cmidi2_ump_midi1_paf(uint8_t group, uint8_t channel, uint8_t note, uint8_t data) {
return cmidi2_ump_midi1_message(group, CMIDI2_STATUS_PAF, channel, note & 0x7F, data & 0x7F);
}
static inline int32_t cmidi2_ump_midi1_cc(uint8_t group, uint8_t channel, uint8_t index, uint8_t data) {
return cmidi2_ump_midi1_message(group, CMIDI2_STATUS_CC, channel, index & 0x7F, data & 0x7F);
}
static inline int32_t cmidi2_ump_midi1_program(uint8_t group, uint8_t channel, uint8_t program) {
return cmidi2_ump_midi1_message(group, CMIDI2_STATUS_PROGRAM, channel, program & 0x7F, MIDI_2_0_RESERVED);
}
static inline int32_t cmidi2_ump_midi1_caf(uint8_t group, uint8_t channel, uint8_t data) {
return cmidi2_ump_midi1_message(group, CMIDI2_STATUS_CAF, channel, data & 0x7F, MIDI_2_0_RESERVED);
}
static inline int32_t cmidi2_ump_midi1_pitch_bend_direct(uint8_t group, uint8_t channel, uint16_t data) {
return cmidi2_ump_midi1_message(group, CMIDI2_STATUS_PITCH_BEND, channel, data & 0x7F, (data >> 7) & 0x7F);
}
static inline int32_t cmidi2_ump_midi1_pitch_bend_split(uint8_t group, uint8_t channel, uint8_t dataLSB, uint8_t dataMSB) {
return cmidi2_ump_midi1_message(group, CMIDI2_STATUS_PITCH_BEND, channel, dataLSB & 0x7F, dataMSB & 0x7F);
}
static inline int32_t cmidi2_ump_midi1_pitch_bend(uint8_t group, uint8_t channel, int16_t data) {
data += 8192;
return cmidi2_ump_midi1_message(group, CMIDI2_STATUS_PITCH_BEND, channel, data & 0x7F, (data >> 7) & 0x7F);
}
// 7.4 MIDI 2.0 Channel Voice Messages
static inline int64_t cmidi2_ump_midi2_channel_message_8_8_16_16(
uint8_t group, uint8_t code, uint8_t channel, uint8_t byte3, uint8_t byte4,
uint16_t short1, uint16_t short2) {
return (((uint64_t) (CMIDI2_MESSAGE_TYPE_MIDI_2_CHANNEL << 28) + ((group & 0xF) << 24) + (((code & 0xF0) + (channel & 0xF)) << 16) + (byte3 << 8) + byte4) << 32) + ((uint64_t) short1 << 16) + short2;
}
static inline int64_t cmidi2_ump_midi2_channel_message_8_8_32(
uint8_t group, uint8_t code, uint8_t channel, uint8_t byte3, uint8_t byte4,
uint32_t rest) {
return (((uint64_t) (CMIDI2_MESSAGE_TYPE_MIDI_2_CHANNEL << 28) + ((group & 0xF) << 24) + (((code & 0xF0) + (channel & 0xF)) << 16) + (byte3 << 8) + byte4) << 32) + rest;
}
static inline uint16_t cmidi2_ump_pitch_7_9(double semitone) {
double actual = semitone < 0.0 ? 0.0 : semitone >= 128.0 ? 128.0 : semitone;
uint16_t dec = (uint16_t) actual;
double microtone = actual - dec;
return (dec << 9) + (int) (microtone * 512.0);
}
static inline uint16_t cmidi2_ump_pitch_7_9_split(uint8_t semitone, double microtone) {
uint16_t ret = (uint16_t) (semitone & 0x7F) << 9;
double actual = microtone < 0.0 ? 0.0 : microtone > 1.0 ? 1.0 : microtone;
ret += (int) (actual * 512.0);
return ret;
}
static inline int64_t cmidi2_ump_midi2_note_off(uint8_t group, uint8_t channel, uint8_t note, uint8_t attributeType, uint16_t velocity, uint16_t attributeData) {
return cmidi2_ump_midi2_channel_message_8_8_16_16(group, CMIDI2_STATUS_NOTE_OFF, channel, note & 0x7F, attributeType, velocity, attributeData);
}
static inline int64_t cmidi2_ump_midi2_note_on(uint8_t group, uint8_t channel, uint8_t note, uint8_t attributeType, uint16_t velocity, uint16_t attributeData) {
return cmidi2_ump_midi2_channel_message_8_8_16_16(group, CMIDI2_STATUS_NOTE_ON, channel, note & 0x7F, attributeType, velocity, attributeData);
}
static inline int64_t cmidi2_ump_midi2_paf(uint8_t group, uint8_t channel, uint8_t note, uint32_t data) {
return cmidi2_ump_midi2_channel_message_8_8_32(group, CMIDI2_STATUS_PAF, channel, note & 0x7F, MIDI_2_0_RESERVED, data);
}
static inline int64_t cmidi2_ump_midi2_per_note_rcc(uint8_t group, uint8_t channel, uint8_t note, uint8_t index, uint32_t data) {
return cmidi2_ump_midi2_channel_message_8_8_32(group, CMIDI2_STATUS_PER_NOTE_RCC, channel, note & 0x7F, index, data);
}
static inline int64_t cmidi2_ump_midi2_per_note_acc(uint8_t group, uint8_t channel, uint8_t note, uint8_t index, uint32_t data) {
return cmidi2_ump_midi2_channel_message_8_8_32(group, CMIDI2_STATUS_PER_NOTE_ACC, channel, note & 0x7F, index, data);
}
static inline int64_t cmidi2_ump_midi2_per_note_management(uint8_t group, uint8_t channel, uint8_t note, uint8_t optionFlags) {
return cmidi2_ump_midi2_channel_message_8_8_32(group, CMIDI2_STATUS_PER_NOTE_MANAGEMENT, channel, note & 0x7F, optionFlags & 3, 0);
}
static inline int64_t cmidi2_ump_midi2_cc(uint8_t group, uint8_t channel, uint8_t index, uint32_t data) {
return cmidi2_ump_midi2_channel_message_8_8_32(group, CMIDI2_STATUS_CC, channel, index & 0x7F, MIDI_2_0_RESERVED, data);
}
static inline int64_t cmidi2_ump_midi2_rpn(uint8_t group, uint8_t channel, uint8_t bankAkaMSB, uint8_t indexAkaLSB, uint32_t dataAkaDTE) {
return cmidi2_ump_midi2_channel_message_8_8_32(group, CMIDI2_STATUS_RPN, channel, bankAkaMSB & 0x7F, indexAkaLSB & 0x7F, dataAkaDTE);
}
static inline int64_t cmidi2_ump_midi2_nrpn(uint8_t group, uint8_t channel, uint8_t bankAkaMSB, uint8_t indexAkaLSB, uint32_t dataAkaDTE) {
return cmidi2_ump_midi2_channel_message_8_8_32(group, CMIDI2_STATUS_NRPN, channel, bankAkaMSB & 0x7F, indexAkaLSB & 0x7F, dataAkaDTE);
}
static inline int64_t cmidi2_ump_midi2_relative_rpn(uint8_t group, uint8_t channel, uint8_t bankAkaMSB, uint8_t indexAkaLSB, uint32_t dataAkaDTE) {
return cmidi2_ump_midi2_channel_message_8_8_32(group, CMIDI2_STATUS_RELATIVE_RPN, channel, bankAkaMSB & 0x7F, indexAkaLSB & 0x7F, dataAkaDTE);
}
static inline int64_t cmidi2_ump_midi2_relative_nrpn(uint8_t group, uint8_t channel, uint8_t bankAkaMSB, uint8_t indexAkaLSB, uint32_t dataAkaDTE) {
return cmidi2_ump_midi2_channel_message_8_8_32(group, CMIDI2_STATUS_RELATIVE_NRPN, channel, bankAkaMSB & 0x7F, indexAkaLSB & 0x7F, dataAkaDTE);
}
static inline int64_t cmidi2_ump_midi2_program(uint8_t group, uint8_t channel, uint8_t optionFlags, uint8_t program, uint8_t bankMSB, uint8_t bankLSB) {
return cmidi2_ump_midi2_channel_message_8_8_32(group, CMIDI2_STATUS_PROGRAM, channel, MIDI_2_0_RESERVED, optionFlags & 1,
((program & 0x7F) << 24) + (bankMSB << 8) + bankLSB);
}
static inline int64_t cmidi2_ump_midi2_caf(uint8_t group, uint8_t channel, uint32_t data) {
return cmidi2_ump_midi2_channel_message_8_8_32(group, CMIDI2_STATUS_CAF, channel, MIDI_2_0_RESERVED, MIDI_2_0_RESERVED, data);
}
static inline int64_t cmidi2_ump_midi2_pitch_bend_direct(uint8_t group, uint8_t channel, uint32_t unsignedData) {
return cmidi2_ump_midi2_channel_message_8_8_32(group, CMIDI2_STATUS_PITCH_BEND, channel, MIDI_2_0_RESERVED, MIDI_2_0_RESERVED, unsignedData);
}
static inline int64_t cmidi2_ump_midi2_pitch_bend(uint8_t group, uint8_t channel, int32_t data) {
return cmidi2_ump_midi2_pitch_bend_direct(group, channel, 0x80000000 + data);
}
static inline int64_t cmidi2_ump_midi2_per_note_pitch_bend_direct(uint8_t group, uint8_t channel, uint8_t note, uint32_t data) {
return cmidi2_ump_midi2_channel_message_8_8_32(group, CMIDI2_STATUS_PER_NOTE_PITCH_BEND, channel, note & 0x7F, MIDI_2_0_RESERVED, data);
}
static inline int64_t cmidi2_ump_midi2_per_note_pitch_bend(uint8_t group, uint8_t channel, uint8_t note, uint32_t data) {
return cmidi2_ump_midi2_per_note_pitch_bend_direct(group, channel, note, 0x80000000 + data);
}
// Common utility functions for sysex support
static inline uint8_t cmidi2_ump_get_byte_from_uint32(uint32_t src, uint8_t index) {
return (uint8_t) (src >> ((7 - index) * 8) & 0xFF);
}
static inline uint8_t cmidi2_ump_get_byte_from_uint64(uint64_t src, uint8_t index) {
return (uint8_t) (src >> ((7 - index) * 8) & 0xFF);
}
static inline uint8_t cmidi2_ump_sysex_get_num_packets(uint8_t numBytes, uint8_t radix) {
return numBytes <= radix ? 1 : numBytes / radix + (numBytes % radix ? 1 : 0);
}
static inline uint32_t cmidi2_ump_read_uint32_bytes_le(const void *sequence) {
const uint8_t *bytes = (const uint8_t*) sequence;
uint32_t ret = 0;
for (int i = 0; i < 4; i++)
ret += ((uint32_t) bytes[i]) << (i * 8);
return ret;
}
static inline uint32_t cmidi2_ump_read_uint32_bytes_be(const void *sequence) {
const uint8_t *bytes = (const uint8_t*) sequence;
uint32_t ret = 0;
for (int i = 0; i < 4; i++)
ret += ((uint32_t) bytes[i]) << ((3 - i) * 8);
return ret;
}
static inline bool cmidi2_util_is_platform_little_endian() {
int i = 1;
return *(char*) &i;
}
static inline uint32_t cmidi2_ump_read_uint32_bytes(const void *sequence) {
return cmidi2_util_is_platform_little_endian() ? cmidi2_ump_read_uint32_bytes_le(sequence) : cmidi2_ump_read_uint32_bytes_be(sequence);
}
static inline uint64_t cmidi2_ump_read_uint64_bytes_le(const void *sequence) {
return ((uint64_t) cmidi2_ump_read_uint32_bytes_le(sequence) << 32) + cmidi2_ump_read_uint32_bytes_le((const uint8_t*) sequence + 4);
}
static inline uint64_t cmidi2_ump_read_uint64_bytes_be(const void *sequence) {
return ((uint64_t) cmidi2_ump_read_uint32_bytes_be(sequence) << 32) + cmidi2_ump_read_uint32_bytes_be((const uint8_t*) sequence + 4);
}
static inline uint64_t cmidi2_ump_read_uint64_bytes(const void *sequence) {
return cmidi2_util_is_platform_little_endian() ? cmidi2_ump_read_uint64_bytes_le(sequence) : cmidi2_ump_read_uint64_bytes_be(sequence);
}
static inline void cmidi2_ump_sysex_get_packet_of(uint64_t* result1, uint64_t* result2, uint8_t group, uint8_t numBytes, const void* srcData, int32_t index,
enum cmidi2_message_type messageType, int radix, bool hasStreamId, uint8_t streamId) {
uint8_t dst8[16];
memset(dst8, 0, 16);
const uint8_t *src8 = (const uint8_t*) srcData;
dst8[0] = (messageType << 4) + (group & 0xF);
enum cmidi2_sysex_status status;
uint8_t size;
if (numBytes <= radix) {
status = CMIDI2_SYSEX_IN_ONE_UMP;
size = numBytes; // single packet message
} else if (index == 0) {
status = CMIDI2_SYSEX_START;
size = radix;
} else {
uint8_t isEnd = index == cmidi2_ump_sysex_get_num_packets(numBytes, radix) - 1;
if (isEnd) {
size = numBytes % radix ? numBytes % radix : radix;
status = CMIDI2_SYSEX_END;
} else {
size = radix;
status = CMIDI2_SYSEX_CONTINUE;
}
}
dst8[1] = status + size + (hasStreamId ? 1 : 0);
if (hasStreamId)
dst8[2] = streamId;
uint8_t dstOffset = hasStreamId ? 3 : 2;
for (uint8_t i = 0, j = index * radix; i < size; i++, j++)
dst8[i + dstOffset] = src8[j];
*result1 = cmidi2_ump_read_uint64_bytes_be(dst8);
if (result2)
*result2 = cmidi2_ump_read_uint64_bytes_be(dst8 + 8);
}
// 7.7 System Exclusive 7-Bit Messages
static inline uint64_t cmidi2_ump_sysex7_direct(uint8_t group, uint8_t status, uint8_t numBytes, uint8_t data1, uint8_t data2, uint8_t data3, uint8_t data4, uint8_t data5, uint8_t data6) {
return (((uint64_t) ((CMIDI2_MESSAGE_TYPE_SYSEX7 << 28) + ((group & 0xF) << 24) + ((status + numBytes) << 16))) << 32) +
((uint64_t) data1 << 40) + ((uint64_t) data2 << 32) + (data3 << 24) + (data4 << 16) + (data5 << 8) + data6;
}
static inline uint32_t cmidi2_ump_sysex7_get_sysex_length(const void* srcData) {
int i = 0;
const uint8_t* csrc = (const uint8_t*) srcData;
while (csrc[i] != 0xF7)
i++;
/* This function automatically detects if 0xF0 is prepended and reduce length if it is. */
return i - (csrc[0] == 0xF0 ? 1 : 0);
}
static inline uint8_t cmidi2_ump_sysex7_get_num_packets(uint8_t numSysex7Bytes) {
return cmidi2_ump_sysex_get_num_packets(numSysex7Bytes, 6);
}
static inline uint64_t cmidi2_ump_sysex7_get_packet_of(uint8_t group, uint8_t numBytes, const void* srcData, int32_t index) {
uint64_t result;
int srcOffset = numBytes > 0 && ((const uint8_t*) srcData)[0] == 0xF0 ? 1 : 0;
cmidi2_ump_sysex_get_packet_of(&result, NULL, group, numBytes, (const uint8_t*) srcData + srcOffset, index, CMIDI2_MESSAGE_TYPE_SYSEX7, 6, false, 0);
return result;
}
/* process() - more complicated function */
// This returns NULL for success, or anything else for failure.
typedef void*(*cmidi2_ump_handler_u64)(uint64_t data, void* context);
// This returns NULL for success, or anything else that `sendUMP` returns for failure.
static inline void* cmidi2_ump_sysex7_process(uint8_t group, void* sysex, cmidi2_ump_handler_u64 sendUMP, void* context)
{
int32_t length = cmidi2_ump_sysex7_get_sysex_length(sysex);
int32_t numPackets = cmidi2_ump_sysex7_get_num_packets(length);
for (int p = 0; p < numPackets; p++) {
int64_t ump = cmidi2_ump_sysex7_get_packet_of(group, length, sysex, p);
void *retCode = sendUMP(ump, context);
if (retCode != 0)
return retCode;
}
return NULL;
}
// 7.8 System Exclusive 8-Bit Messages
static inline int8_t cmidi2_ump_sysex8_get_num_packets(uint8_t numBytes) {
return cmidi2_ump_sysex_get_num_packets(numBytes, 13);
}
static inline void cmidi2_ump_sysex8_get_packet_of(uint8_t group, uint8_t streamId, uint8_t numBytes, const void* srcData, int32_t index, uint64_t* result1, uint64_t* result2) {
cmidi2_ump_sysex_get_packet_of(result1, result2, group, numBytes, srcData, index, CMIDI2_MESSAGE_TYPE_SYSEX8_MDS, 13, true, streamId);
}
/* process() - more complicated function */
// This returns NULL for success, or anything else for failure.
typedef void*(*cmidi2_ump_handler_u128)(uint64_t data1, uint64_t data2, size_t index, void* context);
// This returns NULL for success, or anything else that `sendUMP` returns for failure.
static inline void* cmidi2_ump_sysex8_process(uint8_t group, void* sysex, uint32_t length, uint8_t streamId, cmidi2_ump_handler_u128 sendUMP, void* context)
{
uint32_t numPackets = cmidi2_ump_sysex8_get_num_packets(length);
for (size_t p = 0; p < numPackets; p++) {
uint64_t result1, result2;
cmidi2_ump_sysex8_get_packet_of(group, streamId, length, sysex, p, &result1, &result2);
void* retCode = sendUMP(result1, result2, p, context);
if (retCode != 0)
return retCode;
}
return NULL;
}
// 7.9 Mixed Data Set Message
static inline uint16_t cmidi2_ump_mds_get_num_chunks(uint32_t numTotalBytesInMDS) {
uint32_t radix = 14 * 0x10000;
return numTotalBytesInMDS / radix + (numTotalBytesInMDS % radix ? 1 : 0);
}
// Returns -1 if input is out of range
static inline int32_t cmidi2_ump_mds_get_num_payloads(uint32_t numTotalBytesinChunk) {
if (numTotalBytesinChunk > 14 * 65535)
return -1;
return numTotalBytesinChunk / 14 + (numTotalBytesinChunk % 14 ? 1 : 0);
}
static inline void cmidi2_ump_mds_get_header(uint8_t group, uint8_t mdsId,
uint16_t numBytesInChunk, uint16_t numChunks, uint16_t chunkIndex,
uint16_t manufacturerId, uint16_t deviceId, uint16_t subId, uint16_t subId2,
uint64_t* result1, uint64_t* result2) {
uint8_t dst8[16];
memset(dst8, 0, 16);
dst8[0] = (CMIDI2_MESSAGE_TYPE_SYSEX8_MDS << 4) + (group & 0xF);
dst8[1] = CMIDI2_MIXED_DATA_STATUS_HEADER + mdsId;
*((uint16_t*) (void*) (dst8 + 2)) = numBytesInChunk;
*((uint16_t*) (void*) (dst8 + 4)) = numChunks;
*((uint16_t*) (void*) (dst8 + 6)) = chunkIndex;
*((uint16_t*) (void*) (dst8 + 8)) = manufacturerId;
*((uint16_t*) (void*) (dst8 + 10)) = deviceId;
*((uint16_t*) (void*) (dst8 + 12)) = subId;
*((uint16_t*) (void*) (dst8 + 14)) = subId2;
*result1 = cmidi2_ump_read_uint64_bytes_be(dst8);
if (result2)
*result2 = cmidi2_ump_read_uint64_bytes_be(dst8 + 8);
}
// srcData points to exact start of the source data.
static inline void cmidi2_ump_mds_get_payload_of(uint8_t group, uint8_t mdsId, uint16_t numBytes, const void* srcData, uint64_t* result1, uint64_t* result2) {
uint8_t dst8[16];
memset(dst8, 0, 16);
const uint8_t *src8 = (const uint8_t*) srcData;
dst8[0] = (CMIDI2_MESSAGE_TYPE_SYSEX8_MDS << 4) + (group & 0xF);
dst8[1] = CMIDI2_MIXED_DATA_STATUS_PAYLOAD + mdsId;
uint8_t radix = 14;
uint8_t size = numBytes < radix ? numBytes % radix : radix;
for (uint8_t i = 0; i < size; i++)
dst8[i + 2] = src8[i];
*result1 = cmidi2_ump_read_uint64_bytes_be(dst8);
if (result2)
*result2 = cmidi2_ump_read_uint64_bytes_be(dst8 + 8);
}
/* process() - more complicated function */
// This returns NULL for success, or anything else for failure.
typedef void*(*cmidi2_mds_handler)(uint64_t data1, uint64_t data2, size_t chunkId, size_t payloadId, void* context);
// This returns NULL for success, or anything else that `sendUMP` returns for failure.
static inline void* cmidi2_ump_mds_process(uint8_t group, uint8_t mdsId, void* data, uint32_t length, cmidi2_mds_handler sendUMP, void* context)
{
int32_t numChunks = cmidi2_ump_mds_get_num_chunks(length);
for (int c = 0; c < numChunks; c++) {
int32_t maxChunkSize = 14 * 65535;
int32_t chunkSize = c + 1 == numChunks ? (int32_t)(length % maxChunkSize) : maxChunkSize;
int32_t numPayloads = cmidi2_ump_mds_get_num_payloads(chunkSize);
for (int p = 0; p < numPayloads; p++) {
uint64_t result1, result2;
size_t offset = 14 * (65536 * c + p);
cmidi2_ump_mds_get_payload_of(group, mdsId, chunkSize, (uint8_t*) data + offset, &result1, &result2);
void* retCode = sendUMP(result1, result2, c, p, context);
if (retCode)
return retCode;
}
}
return NULL;
}
// 7.5 Flex Data Message
static inline uint16_t cmidi2_ump_flex_data_get_num_packets(uint32_t numTotalBytesInFlexData) {
return numTotalBytesInFlexData / 12 + (numTotalBytesInFlexData % 12 ? 1 : 0);
}
static inline void cmidi2_ump_flex_data_complete_packet(uint8_t group, uint8_t addressing, uint8_t channel,
uint8_t statusBank, uint8_t statusCode, uint32_t data1, uint32_t data2, uint32_t data3,
uint64_t* result1, uint64_t* result2) {
uint8_t dst8[4];
memset(dst8, 0, 4);
dst8[0] = (CMIDI2_MESSAGE_TYPE_FLEX_DATA << 4) + (group & 0xF);
dst8[1] = (CMIDI2_SYSEX_IN_ONE_UMP << 2) + ((addressing & 0x3) << 4) + (channel & 0xF);
dst8[2] = statusBank;
dst8[3] = statusCode;
*result1 = ((uint64_t) cmidi2_ump_read_uint32_bytes_be(dst8) << 32) + data1;
*result2 = ((uint64_t) data2 << 32) + data3;
}
static inline void cmidi2_ump_flex_data_get_packet_of(uint8_t group, uint8_t addressing, uint8_t channel,
uint8_t statusBank, uint8_t statusCode, uint16_t numBytes, const void* srcData, int32_t currentPacket,
uint64_t* result1, uint64_t* result2) {
uint8_t dst8[16];
memset(dst8, 0, 16);
const uint8_t *src8 = (const uint8_t*) srcData;
dst8[0] = (CMIDI2_MESSAGE_TYPE_FLEX_DATA << 4) + (group & 0xF);
const size_t radix = 12;
enum cmidi2_sysex_status format;
uint8_t size;
if (numBytes <= radix) {
format = CMIDI2_SYSEX_IN_ONE_UMP;
size = numBytes; // single packet message
} else if (currentPacket == 0) {
format = CMIDI2_SYSEX_START;
size = radix;
} else {
uint8_t isEnd = currentPacket == cmidi2_ump_sysex_get_num_packets(numBytes, radix) - 1;
if (isEnd) {
size = numBytes % radix ? numBytes % radix : radix;
format = CMIDI2_SYSEX_END;
} else {
size = radix;
format = CMIDI2_SYSEX_CONTINUE;
}
}
dst8[1] = (format << 2) + ((addressing & 0x3) << 4) + (channel & 0xF);
dst8[2] = statusBank;
dst8[3] = statusCode;
for (uint8_t i = 0; i < size; i++)
dst8[i + 4] = src8[i];
*result1 = cmidi2_ump_read_uint64_bytes_be(dst8);
*result2 = cmidi2_ump_read_uint64_bytes_be(dst8 + 8);
}
/* process() - more complicated function */
// This returns NULL for success, or anything else for failure.
typedef void* (*cmidi2_flex_data_handler)(uint64_t data1, uint64_t data2, void* context);
// This returns NULL for success, or anything else that `sendUMP` returns for failure.
// `text` is usually a null-terminated text string, but it may contain `\0` as Melisma in lyricText. Hence we still need `length`.
static inline void* cmidi2_ump_flex_data_process(uint8_t group, uint8_t addressing, uint8_t channel,
uint8_t statusBank, uint8_t statusCode, const char* text, uint32_t length, cmidi2_flex_data_handler sendUMP, void* context)
{
int32_t numPackets = cmidi2_ump_flex_data_get_num_packets(length);
for (int p = 0; p < numPackets; p++) {
uint64_t result1, result2;
cmidi2_ump_flex_data_get_packet_of(group, addressing, channel, statusBank, statusCode, length, text, p, &result1, &result2);
void *retCode = sendUMP(result1, result2, context);
if (retCode != 0)
return retCode;
}
return NULL;
}
// individual flex data message generators
static inline void cmidi2_ump_flex_data_set_tempo_direct(uint8_t group, uint8_t channel,
uint32_t tempoIn10NanosecondsPerQN,
uint64_t* result1, uint64_t* result2) {
cmidi2_ump_flex_data_complete_packet(group, 1, channel,
CMIDI2_FLEX_DATA_BANK_SETUP_AND_PERFORMANCE,
CMIDI2_FLEX_DATA_STATUS_SET_TEMPO,
tempoIn10NanosecondsPerQN, 0, 0,
result1, result2);
}
static inline void cmidi2_ump_flex_data_set_time_signature(uint8_t group, uint8_t channel,
uint8_t numerator, uint8_t denominator, uint8_t numberOf32thNotes,
uint64_t* result1, uint64_t* result2) {
cmidi2_ump_flex_data_complete_packet(group, 1, channel,
CMIDI2_FLEX_DATA_BANK_SETUP_AND_PERFORMANCE,
CMIDI2_FLEX_DATA_STATUS_SET_TIME_SIGNATURE,
(numerator << 24) + (denominator << 16) + (numberOf32thNotes << 8),0, 0,