-
Notifications
You must be signed in to change notification settings - Fork 23
/
zynmidirouter.c
2045 lines (1805 loc) · 61 KB
/
zynmidirouter.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
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
/*
* ******************************************************************
* ZYNTHIAN PROJECT: ZynMidiRouter Library
*
* MIDI router library: Implements the MIDI router & filter
*
* Copyright (C) 2015-2024 Fernando Moyano <[email protected]>
*
* ******************************************************************
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License, or any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* For a full copy of the GNU General Public License see the LICENSE.txt file.
*
* ******************************************************************
*/
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <jack/jack.h>
#include <jack/midiport.h>
#include "zynpot.h"
#include "zynmidirouter.h"
//-----------------------------------------------------------------------------
// Global variables
//-----------------------------------------------------------------------------
int tuning_pitchbend; // Global tunning, implemented using MIDI pitchbend messages
int active_chain; // Index of the active chain zmop
int active_midi_chan; // Flag to enable/disable active MIDI channel. When enable, active's chain MIDI channel is the active MIDI channel
int midi_master_chan; // MIDI Master channel. -1 to disable master channel.
int midi_system_events; // Flag to enable/disable system events globally
int midi_learning_mode; // To flag "MIDI learning" from UI => Is it needed?
int8_t global_transpose; // All incoming (zmip) notes are transposed
jack_nframes_t last_frame; // Index of last frame in each jack cycle
midi_filter_t midi_filter;
struct zmip_st zmips[MAX_NUM_ZMIPS];
struct zmop_st zmops[MAX_NUM_ZMOPS];
uint8_t event_buffer[JACK_MIDI_BUFFER_SIZE]; // Buffer for processing internal/direct MIDI events
jack_client_t * jack_client;
jack_ringbuffer_t * zynmidi_buffer;
//-----------------------------------------------------------------------------
// Library Initialization
//-----------------------------------------------------------------------------
int init_zynmidirouter() {
// Init global settings
active_chain = -1;
active_midi_chan = 0;
tuning_pitchbend = -1;
midi_master_chan =- 1;
midi_system_events = 1;
midi_learning_mode = 0;
global_transpose = 0;
if (!init_zynmidi_buffer())
return 0;
if (!init_midi_router()) {
end_zynmidi_buffer();
return 0;
}
if (!init_jack_midi("ZynMidiRouter")) {
end_midi_router();
end_zynmidi_buffer();
return 0;
}
return 1;
}
int end_zynmidirouter() {
if (!end_jack_midi())
return 0;
if (!end_midi_router())
return 0;
if (!end_zynmidi_buffer())
return 0;
return 1;
}
int init_midi_router() {
int i, j, k;
for (i = 0; i < 8; i++) {
for (j = 0; j < 16; j++) {
for (k = 0; k < 128; k++) {
midi_filter.event_map[i][j][k].type = THRU_EVENT;
midi_filter.event_map[i][j][k].chan = j;
midi_filter.event_map[i][j][k].num = k;
}
}
}
return 1;
}
int end_midi_router() {
return 1;
}
//-----------------------------------------------------------------------------
// Global settings management
//-----------------------------------------------------------------------------
void set_active_chain(int iz) {
if (iz >= NUM_ZMOP_CHAINS || iz < -1) {
fprintf(stderr, "ZynMidiRouter: Active chain (%d) is out of range!\n", iz);
return;
}
if (iz != active_chain) {
active_chain = iz;
}
}
int get_active_chain() {
return active_chain;
}
void set_active_midi_chan(int flag) {
active_midi_chan = flag;
}
int get_active_midi_chan() {
return active_midi_chan;
}
// Global tuning based in MIDI pitch-bending
void set_tuning_freq(double freq) {
if (freq == 440.0) {
tuning_pitchbend = -1;
// Clear pitchbend already applied
for (int i = 0; i < 16; ++i)
zmip_send_pitchbend_change(ZMIP_FAKE_UI, i, 0x2000); //!@todo Ideally reset only playing channels to current pitchbend offset
} else {
double pb = 6 * log((double)freq / 440.0) / log(2.0);
if (pb < 1.0 && pb > -1.0) {
tuning_pitchbend = ((int)(8192.0 * (1.0 + pb))) & 0x3FFF;
fprintf(stderr, "ZynMidiRouter: MIDI tuning frequency set to %f Hz (%d)\n", freq, tuning_pitchbend);
} else {
fprintf(stderr, "ZynMidiRouter: MIDI tuning frequency (%f) out of range!\n", freq);
}
}
}
int get_tuning_pitchbend() {
return tuning_pitchbend;
}
int get_tuned_pitchbend(int pb) {
int tpb = tuning_pitchbend + pb - 8192;
if (tpb < 0)
tpb = 0;
else if (tpb > 16383)
tpb = 16383;
return tpb;
}
void set_midi_master_chan(int chan) {
if (chan > 15 || chan < -1) {
fprintf(stderr, "ZynMidiRouter: MIDI Master channel (%d) is out of range!\n",chan);
return;
}
midi_master_chan = chan;
}
int get_midi_master_chan() {
return midi_master_chan;
}
// Enable/Disable System messages globally
void set_midi_system_events(int flag) {
midi_system_events = flag;
}
int get_midi_system_events() {
return midi_system_events;
}
// MIDI Learning Mode
void set_midi_learning_mode(int mlm) {
midi_learning_mode = mlm;
}
int get_midi_learning_mode() {
return midi_learning_mode;
}
// -----------------------------------------------------------------------------
// Core MIDI filter functions
// -----------------------------------------------------------------------------
int validate_midi_event(midi_event_t *ev) {
if (ev->type > 0xE) {
fprintf(stderr, "ZynMidiRouter: MIDI Event type (%d) is out of range!\n", ev->type);
return 0;
}
if (ev->chan > 15) {
fprintf(stderr, "ZynMidiRouter: MIDI Event channel (%d) is out of range!\n", ev->chan);
return 0;
}
if (ev->num > 127) {
fprintf(stderr, "ZynMidiRouter: MIDI Event num (%d) is out of range!\n", ev->num);
return 0;
}
return 1;
}
void set_midi_filter_event_map_st(midi_event_t *ev_from, midi_event_t *ev_to) {
if (validate_midi_event(ev_from) && validate_midi_event(ev_to)) {
//memcpy(&midi_filter.event_map[ev_from->type&0x7][ev_from->chan][ev_from->num],ev_to,sizeof(ev_to));
midi_event_t *event_map=&midi_filter.event_map[ev_from->type&0x7][ev_from->chan][ev_from->num];
event_map->type = ev_to->type;
event_map->chan = ev_to->chan;
event_map->num = ev_to->num;
}
}
void set_midi_filter_event_map(midi_event_type type_from, uint8_t chan_from, uint8_t num_from, midi_event_type type_to, uint8_t chan_to, uint8_t num_to) {
midi_event_t ev_from = { .type = type_from, .chan = chan_from, .num = num_from };
midi_event_t ev_to = { .type = type_to, .chan = chan_to, .num = num_to };
set_midi_filter_event_map_st(&ev_from, &ev_to);
}
void set_midi_filter_event_ignore_st(midi_event_t *ev_from) {
if (validate_midi_event(ev_from)) {
midi_filter.event_map[ev_from->type&0x7][ev_from->chan][ev_from->num].type = IGNORE_EVENT;
}
}
void set_midi_filter_event_ignore(midi_event_type type_from, uint8_t chan_from, uint8_t num_from) {
midi_event_t ev_from = { .type = type_from, .chan = chan_from, .num = num_from };
set_midi_filter_event_ignore_st(&ev_from);
}
midi_event_t *get_midi_filter_event_map_st(midi_event_t *ev_from) {
if (validate_midi_event(ev_from)) {
return &midi_filter.event_map[ev_from->type&0x7][ev_from->chan][ev_from->num];
}
return NULL;
}
midi_event_t *get_midi_filter_event_map(midi_event_type type_from, uint8_t chan_from, uint8_t num_from) {
midi_event_t ev_from = { .type = type_from, .chan = chan_from, .num = num_from };
return get_midi_filter_event_map_st(&ev_from);
}
void del_midi_filter_event_map_st(midi_event_t *ev_from) {
if (validate_midi_event(ev_from)) {
midi_filter.event_map[ev_from->type&0x7][ev_from->chan][ev_from->num].type = THRU_EVENT;
midi_filter.event_map[ev_from->type&0x7][ev_from->chan][ev_from->num].chan = ev_from->chan;
midi_filter.event_map[ev_from->type&0x7][ev_from->chan][ev_from->num].num = ev_from->num;
}
}
void del_midi_filter_event_map(midi_event_type type_from, uint8_t chan_from, uint8_t num_from) {
midi_event_t ev_from = { .type = type_from, .chan = chan_from, .num = num_from };
del_midi_filter_event_map_st(&ev_from);
}
void reset_midi_filter_event_map() {
int i, j, k;
for (i = 0; i < 8; i++) {
for (j = 0; j < 16; j++) {
for (k = 0; k < 128; k++) {
midi_filter.event_map[i][j][k].type = THRU_EVENT;
midi_filter.event_map[i][j][k].chan = j;
midi_filter.event_map[i][j][k].num = k;
}
}
}
}
// Simple CC mapping
void set_midi_filter_cc_map(uint8_t chan_from, uint8_t cc_from, uint8_t chan_to, uint8_t cc_to) {
set_midi_filter_event_map(CTRL_CHANGE, chan_from, cc_from, CTRL_CHANGE, chan_to, cc_to);
}
void set_midi_filter_cc_ignore(uint8_t chan_from, uint8_t cc_from) {
set_midi_filter_event_ignore(CTRL_CHANGE, chan_from, cc_from);
}
//TODO: It doesn't take into account if chan_from!=chan_to
uint8_t get_midi_filter_cc_map(uint8_t chan_from, uint8_t cc_from) {
midi_event_t *ev = get_midi_filter_event_map(CTRL_CHANGE, chan_from, cc_from);
return ev->num;
}
void del_midi_filter_cc_map(uint8_t chan_from, uint8_t cc_from) {
del_midi_filter_event_map(CTRL_CHANGE, chan_from, cc_from);
}
void reset_midi_filter_cc_map() {
int i, j;
for (i = 0; i < 16; i++) {
for (j = 0; j < 128; j++) {
del_midi_filter_event_map(CTRL_CHANGE, i, j);
}
}
}
// -----------------------------------------------------------------------------
// MIDI Input Ports management
// -----------------------------------------------------------------------------
int zmip_init(int iz, char *name, uint32_t flags) {
if (iz < 0 || iz >= MAX_NUM_ZMIPS) {
fprintf(stderr, "ZynMidiRouter: Bad index (%d) initializing input port '%s'.\n", iz, name);
return 0;
}
if (name != NULL) {
//Create Jack Output Port
zmips[iz].jport = jack_port_register(jack_client, name, JACK_DEFAULT_MIDI_TYPE, JackPortIsInput, 0);
if (zmips[iz].jport == NULL) {
fprintf(stderr, "ZynMidiRouter: Error creating jack midi input port '%s'.\n", name);
return 0;
}
} else {
zmips[iz].jport = NULL;
}
//Set initial values
zmips[iz].buffer = NULL;
zmips[iz].rbuffer = NULL;
zmips[iz].event.buffer = NULL;
zmips[iz].event.time = 0xFFFFFFFF;
zmips[iz].event_count = 0;
zmips[iz].flags = flags;
memset(zmips[iz].ctrl_mode, CTRL_MODE_ABS, 16 * 128);
memset(zmips[iz].ctrl_relmode_count, 0, 16 * 128);
memset(zmips[iz].last_ctrl_val, 0, 16 * 128);
// Create direct input ring-buffer
if (flags & FLAG_ZMIP_DIRECTIN) {
zmips[iz].rbuffer = jack_ringbuffer_create(JACK_MIDI_BUFFER_SIZE);
if (!zmips[iz].rbuffer) {
fprintf(stderr, "ZynMidiRouter: Error creating ZMIP ring-buffer.\n");
return 0;
}
// lock the buffer into memory, this is *NOT* realtime safe, do it before using the buffer!
if (jack_ringbuffer_mlock(zmips[iz].rbuffer)) {
fprintf(stderr, "ZynMidiRouter: Error locking memory for ZMIP ring-buffer.\n");
return 0;
}
}
return 1;
}
int zmip_end(int iz) {
if (iz < 0 || iz >= MAX_NUM_ZMIPS) {
fprintf(stderr, "ZynMidiRouter: Bad input port index (%d).\n", iz);
return 0;
}
zmips[iz].buffer = NULL;
if (zmips[iz].rbuffer) {
jack_ringbuffer_free(zmips[iz].rbuffer);
zmips[iz].rbuffer = NULL;
}
return 1;
}
int zmip_get_num_devs() {
return NUM_ZMIP_DEVS;
}
int zmip_get_seq_index() {
return ZMIP_SEQ;
}
int zmip_get_step_index() {
return ZMIP_STEP;
}
int zmip_get_int_index() {
return ZMIP_FAKE_INT;
}
int zmip_get_ctrl_index() {
return ZMIP_CTRL;
}
int zmip_set_flags(int iz, uint32_t flags) {
if (iz < 0 || iz >= MAX_NUM_ZMIPS) {
fprintf(stderr, "ZynMidiRouter: Bad input port index (%d).\n", iz);
return 0;
}
zmips[iz].flags = flags;
return 1;
}
uint32_t zmip_get_flags(int iz) {
if (iz < 0 || iz >= MAX_NUM_ZMIPS) {
fprintf(stderr, "ZynMidiRouter: Bad input port index (%d).\n", iz);
return 0;
}
return zmips[iz].flags;
}
int zmip_has_flags(int iz, uint32_t flags) {
if (iz < 0 || iz >= MAX_NUM_ZMIPS) {
fprintf(stderr, "ZynMidiRouter: Bad input port index (%d).\n", iz);
return 0;
}
return (zmips[iz].flags & flags) == flags;
}
int zmip_set_flag_cc_auto_mode(int iz, uint8_t flag) {
if (iz < 0 || iz >= MAX_NUM_ZMIPS) {
fprintf(stderr, "ZynMidiRouter: Bad input port index (%d).\n", iz);
return 0;
}
if (flag)
zmips[iz].flags |= (uint32_t)FLAG_ZMIP_CC_AUTO_MODE;
else
zmips[iz].flags &= ~(uint32_t)FLAG_ZMIP_CC_AUTO_MODE;
return 1;
}
int zmip_get_flag_cc_auto_mode(int iz) {
if (iz < 0 || iz >= MAX_NUM_ZMIPS) {
fprintf(stderr, "ZynMidiRouter: Bad output port index (%d).\n", iz);
return 0;
}
return (zmips[iz].flags & (uint32_t)FLAG_ZMIP_CC_AUTO_MODE) > 0;
}
int zmip_set_flag_active_chain(int iz, uint8_t flag) {
if (iz < 0 || iz >= MAX_NUM_ZMIPS) {
fprintf(stderr, "ZynMidiRouter: Bad input port number (%d).\n", iz);
return 0;
}
if (flag)
zmips[ZMIP_DEV0 + iz].flags |= (uint32_t)FLAG_ZMIP_ACTIVE_CHAIN;
else
zmips[ZMIP_DEV0 + iz].flags &= ~(uint32_t)FLAG_ZMIP_ACTIVE_CHAIN;
//fprintf(stderr, "ZynMidiRouter: Flags for zmip (%d) => %x\n", iz, zmips[ZMIP_DEV0 + iz].flags);
return 1;
}
int zmip_get_flag_active_chain(int iz) {
if (iz < 0 || iz >= MAX_NUM_ZMIPS) {
fprintf(stderr, "ZynMidiRouter: Bad input port number (%d).\n", iz);
return 0;
}
return zmips[ZMIP_DEV0 + iz].flags & (uint32_t)FLAG_ZMIP_ACTIVE_CHAIN;
}
//Route/unroute a MIDI input device (zmip) to *ALL* chain zmops
int zmip_set_route_chains(int iz, int route) {
if (iz < 0 || iz >= MAX_NUM_ZMIPS) {
fprintf(stderr, "ZynMidiRouter: Bad input port index (%d).\n", iz);
return 0;
}
for (int i = 0; i < ZMOP_CTRL; i++) {
if (!zmop_set_route_from(i, iz, route)) return 0;
}
return 1;
}
//-----------------------------------------------------------------------------
// MIDI Output Ports management
//-----------------------------------------------------------------------------
int zmop_init(int iz, char *name, uint32_t flags) {
if (iz < 0 || iz >= MAX_NUM_ZMOPS) {
fprintf(stderr, "ZynMidiRouter: Bad index (%d) initializing ouput port '%s'.\n", iz, name);
return 0;
}
if (name != NULL) {
// Create Jack Output Port
zmops[iz].jport = jack_port_register(jack_client, name, JACK_DEFAULT_MIDI_TYPE, JackPortIsOutput, 0);
if (zmops[iz].jport == NULL) {
fprintf(stderr, "ZynMidiRouter: Error creating jack midi output port '%s'.\n", name);
return 0;
}
} else {
zmops[iz].jport = NULL;
}
// Set initial values
zmops[iz].buffer = NULL;
zmops[iz].rbuffer = NULL;
zmops[iz].n_connections = 0;
zmops[iz].flags = flags;
zmops[iz].note_low = 0;
zmops[iz].note_high = 127;
zmops[iz].transpose_octave = 0;
zmops[iz].transpose_semitone = 0;
memset(zmops[iz].note_state, 0, 128);
memset(zmops[iz].note_transpose, 0, 128);
int i;
for (i = 0; i < 16; i++) {
zmops[iz].last_pb_val[i] = 8192;
}
for (i = 0; i < 128; i++) {
zmops[iz].cc_route[i] = 0;
}
// Create direct output ring-buffer
if (flags & FLAG_ZMOP_DIRECTOUT) {
zmops[iz].rbuffer = jack_ringbuffer_create(JACK_MIDI_BUFFER_SIZE);
if (!zmops[iz].rbuffer) {
fprintf(stderr, "ZynMidiRouter: Error creating ZMOP ring-buffer.\n");
return 0;
}
// lock the buffer into memory, this is *NOT* realtime safe, do it before using the buffer!
if (jack_ringbuffer_mlock(zmops[iz].rbuffer)) {
fprintf(stderr, "ZynMidiRouter: Error locking memory for ZMOP ring-buffer.\n");
return 0;
}
}
// Reset MIDI channels
zmop_reset_midi_chans(iz);
// Reset routes
for (i = 0; i < MAX_NUM_ZMIPS; i++) {
zmops[iz].route_from_zmips[i] = 0;
}
return 1;
}
int zmop_end(int iz) {
if (iz < 0 || iz >= MAX_NUM_ZMOPS) {
fprintf(stderr, "ZynMidiRouter: Bad output port index (%d).\n", iz);
return 0;
}
zmops[iz].buffer = NULL;
if (zmops[iz].rbuffer) {
jack_ringbuffer_free(zmops[iz].rbuffer);
zmops[iz].rbuffer = NULL;
}
return 1;
}
int zmop_get_num_chains() {
return NUM_ZMOP_CHAINS;
}
int zmop_get_num_devs() {
return NUM_ZMOP_DEVS;
}
// Flags management
int zmop_set_flags(int iz, uint32_t flags) {
if (iz < 0 || iz >= MAX_NUM_ZMOPS) {
fprintf(stderr, "ZynMidiRouter: Bad output port index (%d).\n", iz);
return 0;
}
zmops[iz].flags = flags;
return 1;
}
uint32_t zmop_get_flags(int iz) {
if (iz < 0 || iz >= MAX_NUM_ZMOPS) {
fprintf(stderr, "ZynMidiRouter: Bad output port index (%d).\n", iz);
return 0;
}
return zmops[iz].flags;
}
int zmop_has_flags(int iz, uint32_t flags) {
if (iz < 0 || iz >= MAX_NUM_ZMOPS) {
fprintf(stderr, "ZynMidiRouter: Bad output port index (%d).\n", iz);
return 0;
}
return (zmops[iz].flags & flags) == flags;
}
int zmop_set_flag_droppc(int iz, uint8_t flag) {
if (iz < 0 || iz >= MAX_NUM_ZMOPS) {
fprintf(stderr, "ZynMidiRouter: Bad output port index (%d).\n", iz);
return 0;
}
if (flag)
zmops[iz].flags |= (uint32_t)FLAG_ZMOP_DROPPC;
else
zmops[iz].flags &= ~(uint32_t)FLAG_ZMOP_DROPPC;
//fprintf(stderr, "ZynMidiRouter: Flags for zmop (%d) => %x\n", iz, zmops[iz].flags);
return 1;
}
int zmop_get_flag_droppc(int iz) {
if (iz < 0 || iz >= MAX_NUM_ZMOPS) {
fprintf(stderr, "ZynMidiRouter: Bad output port index (%d).\n", iz);
return 0;
}
return (zmops[iz].flags & (uint32_t)FLAG_ZMOP_DROPPC) > 0;
}
int zmop_set_flag_dropcc(int iz, uint8_t flag) {
if (iz < 0 || iz >= MAX_NUM_ZMOPS) {
fprintf(stderr, "ZynMidiRouter: Bad output port index (%d).\n", iz);
return 0;
}
if (flag)
zmops[iz].flags |= (uint32_t)FLAG_ZMOP_DROPCC;
else
zmops[iz].flags &= ~(uint32_t)FLAG_ZMOP_DROPCC;
return 1;
}
int zmop_get_flag_dropcc(int iz) {
if (iz < 0 || iz >= MAX_NUM_ZMOPS) {
fprintf(stderr, "ZynMidiRouter: Bad output port index (%d).\n", iz);
return 0;
}
return (zmops[iz].flags & (uint32_t)FLAG_ZMOP_DROPCC) > 0;
}
int zmop_set_flag_dropsys(int iz, uint8_t flag) {
if (iz < 0 || iz >= MAX_NUM_ZMOPS) {
fprintf(stderr, "ZynMidiRouter: Bad output port index (%d).\n", iz);
return 0;
}
if (flag)
zmops[iz].flags |= (uint32_t)FLAG_ZMOP_DROPSYS;
else
zmops[iz].flags &= ~(uint32_t)FLAG_ZMOP_DROPSYS;
return 1;
}
int zmop_get_flag_dropsys(int iz) {
if (iz < 0 || iz >= MAX_NUM_ZMOPS) {
fprintf(stderr, "ZynMidiRouter: Bad output port index (%d).\n", iz);
return 0;
}
return (zmops[iz].flags & (uint32_t)FLAG_ZMOP_DROPSYS) > 0;
}
int zmop_set_flag_dropsysex(int iz, uint8_t flag) {
if (iz < 0 || iz >= MAX_NUM_ZMOPS) {
fprintf(stderr, "ZynMidiRouter: Bad output port index (%d).\n", iz);
return 0;
}
if (flag)
zmops[iz].flags |= (uint32_t)FLAG_ZMOP_DROPSYSEX;
else
zmops[iz].flags &= ~(uint32_t)FLAG_ZMOP_DROPSYSEX;
return 1;
}
int zmop_get_flag_dropsysex(int iz) {
if (iz < 0 || iz >= MAX_NUM_ZMOPS) {
fprintf(stderr, "ZynMidiRouter: Bad output port index (%d).\n", iz);
return 0;
}
return (zmops[iz].flags & (uint32_t)FLAG_ZMOP_DROPSYSEX) > 0;
}
int zmop_set_flag_dropnote(int iz, uint8_t flag) {
if (iz < 0 || iz >= MAX_NUM_ZMOPS) {
fprintf(stderr, "ZynMidiRouter: Bad output port index (%d).\n", iz);
return 0;
}
if (flag)
zmops[iz].flags |= (uint32_t)FLAG_ZMOP_DROPNOTE;
else
zmops[iz].flags &= ~(uint32_t)FLAG_ZMOP_DROPNOTE;
return 1;
}
int zmop_get_flag_dropnote(int iz) {
if (iz < 0 || iz >= MAX_NUM_ZMOPS) {
fprintf(stderr, "ZynMidiRouter: Bad output port index (%d).\n", iz);
return 0;
}
return (zmops[iz].flags & (uint32_t)FLAG_ZMOP_DROPNOTE) > 0;
}
int zmop_set_flag_tuning(int iz, uint8_t flag) {
if (iz < 0 || iz >= MAX_NUM_ZMOPS) {
fprintf(stderr, "ZynMidiRouter: Bad output port index (%d).\n", iz);
return 0;
}
if (flag)
zmops[iz].flags |= (uint32_t)FLAG_ZMOP_TUNING;
else
zmops[iz].flags &= ~(uint32_t)FLAG_ZMOP_TUNING;
return 1;
}
int zmop_get_flag_tuning(int iz) {
if (iz < 0 || iz >= MAX_NUM_ZMOPS) {
fprintf(stderr, "ZynMidiRouter: Bad output port index (%d).\n", iz);
return 0;
}
return (zmops[iz].flags & (uint32_t)FLAG_ZMOP_TUNING) > 0;
}
int zmop_set_flag_chan_transfilter(int iz, uint8_t flag) {
if (iz < 0 || iz >= MAX_NUM_ZMOPS) {
fprintf(stderr, "ZynMidiRouter: Bad output port index (%d).\n", iz);
return 0;
}
if (flag)
zmops[iz].flags |= (uint32_t)FLAG_ZMOP_CHAN_TRANSFILTER;
else
zmops[iz].flags &= ~(uint32_t)FLAG_ZMOP_CHAN_TRANSFILTER;
//fprintf(stderr, "ZynMidiRouter: Flags for zmop (%d) => %x\n", iz, zmops[iz].flags);
return 1;
}
int zmop_get_flag_chan_transfilter(int iz) {
if (iz < 0 || iz >= MAX_NUM_ZMOPS) {
fprintf(stderr, "ZynMidiRouter: Bad output port index (%d).\n", iz);
return 0;
}
return (zmops[iz].flags & (uint32_t)FLAG_ZMOP_CHAN_TRANSFILTER) > 0;
}
// MIDI channel management
int zmop_reset_midi_chans(int iz) {
if (iz < 0 || iz >= MAX_NUM_ZMOPS) {
fprintf(stderr, "ZynMidiRouter: Bad output port index (%d).\n", iz);
return 0;
}
int i;
for (i = 0; i < 16; i++) {
zmops[iz].midi_chans[i] = -1;
}
zmops[iz].midi_chan = -1;
zmop_set_flag_chan_transfilter(iz, 1);
return 1;
}
int zmop_set_midi_chan(int iz, int midi_chan) {
if (iz < 0 || iz >= MAX_NUM_ZMOPS) {
fprintf(stderr, "ZynMidiRouter: Bad output port index (%d).\n", iz);
return 0;
}
if (midi_chan < 0 || midi_chan >= 16) {
fprintf(stderr, "ZynMidiRouter: Bad chan number (%d).\n", midi_chan);
return 0;
}
int i;
for (i = 0; i < 16; i++) {
zmops[iz].midi_chans[i] = -1;
}
zmops[iz].midi_chan = midi_chan;
zmops[iz].midi_chans[midi_chan] = midi_chan;
zmop_set_flag_chan_transfilter(iz, 1);
return 1;
}
int zmop_set_midi_chan_trans(int iz, int midi_chan, int midi_chan_trans) {
if (iz < 0 || iz >= MAX_NUM_ZMOPS) {
fprintf(stderr, "ZynMidiRouter: Bad output port index (%d).\n", iz);
return 0;
}
if (midi_chan < 0 || midi_chan >= 16) {
fprintf(stderr, "ZynMidiRouter: Bad chan number (%d).\n", midi_chan);
return 0;
}
if (midi_chan_trans < 0 || midi_chan_trans >= 16) {
fprintf(stderr, "ZynMidiRouter: Bad chan trans number (%d).\n", midi_chan_trans);
return 0;
}
int i;
for (i = 0; i < 16; i++) {
zmops[iz].midi_chans[i] = -1;
}
zmops[iz].midi_chan = midi_chan;
zmops[iz].midi_chans[midi_chan] = midi_chan_trans;
zmop_set_flag_chan_transfilter(iz, 1);
return 1;
}
int zmop_set_midi_chan_all(int iz) {
if (iz < 0 || iz >= MAX_NUM_ZMOPS) {
fprintf(stderr, "ZynMidiRouter: Bad output port index (%d).\n", iz);
return 0;
}
int i;
for (i = 0; i < 16; i ++) {
zmops[iz].midi_chans[i] = i;
}
zmops[iz].midi_chan = -1;
zmop_set_flag_chan_transfilter(iz, 0);
return 1;
}
int zmop_set_midi_chan_all_trans(int iz, int midi_chan) {
if (iz < 0 || iz >= MAX_NUM_ZMOPS) {
fprintf(stderr, "ZynMidiRouter: Bad output port index (%d).\n", iz);
return 0;
}
int i;
for (i = 0; i < 16; i ++) {
zmops[iz].midi_chans[i] = midi_chan;
}
zmops[iz].midi_chan = -1;
zmop_set_flag_chan_transfilter(iz, 0);
return 1;
}
int zmop_set_midi_chan_to(int iz, int midi_chan_from, int midi_chan_to) {
if (iz < 0 || iz >= MAX_NUM_ZMOPS) {
fprintf(stderr, "ZynMidiRouter: Bad output port index (%d).\n", iz);
return 0;
}
if (midi_chan_from < 0 || midi_chan_from >= 16) {
fprintf(stderr, "ZynMidiRouter: Bad chan_from number (%d).\n", midi_chan_from);
return 0;
}
if (midi_chan_to < -1 || midi_chan_to >= 16) {
midi_chan_to = -1;
}
zmops[iz].midi_chans[midi_chan_from] = midi_chan_to;
return 1;
}
int zmop_get_midi_chan_to(int iz, int midi_chan_from) {
if (iz < 0 || iz >= MAX_NUM_ZMOPS) {
fprintf(stderr, "ZynMidiRouter: Bad output port index (%d).\n", iz);
return -1;
}
if (midi_chan_from < 0 || midi_chan_from >= 16) {
fprintf(stderr, "ZynMidiRouter: Bad chan number (%d).\n", midi_chan_from);
return 0;
}
return zmops[iz].midi_chans[midi_chan_from];
}
int zmop_get_midi_chan_info(int iz, int *buffer) {
if (iz < 0 || iz >= MAX_NUM_ZMOPS) {
fprintf(stderr, "ZynMidiRouter: Bad output port index (%d).\n", iz);
return -1;
}
memcpy((void *)buffer, (void *)zmops[iz].midi_chans, 16 * sizeof(int));
return 1;
}
// Routing zmip => zmop
int zmop_reset_routes_from(int iz) {
if (iz<0 || iz>=MAX_NUM_ZMOPS) {
fprintf(stderr, "ZynMidiRouter: Bad output port index (%d).\n", iz);
return 0;
}
int i;
for (i = 0; i < MAX_NUM_ZMIPS; i++)
zmops[iz].route_from_zmips[i] = 0;
return 1;
}
int zmop_set_route_from(int izmop, int izmip, int route) {
if (izmop < 0 || izmop >= MAX_NUM_ZMOPS) {
fprintf(stderr, "ZynMidiRouter: Bad output port index (%d).\n", izmop);
return 0;
}
if (izmip < 0 || izmip >= MAX_NUM_ZMIPS) {
fprintf(stderr, "ZynMidiRouter: Bad input port index (%d).\n", izmip);
return 0;
}
zmops[izmop].route_from_zmips[izmip] = route;
return 1;
}
int zmop_get_route_from(int izmop, int izmip) {
if (izmop < 0 || izmop >= MAX_NUM_ZMOPS) {
fprintf(stderr, "ZynMidiRouter: Bad output port index (%d).\n", izmop);
return -1;
}
if (izmip < 0 || izmip >= MAX_NUM_ZMIPS) {
fprintf(stderr, "ZynMidiRouter: Bad output port index (%d).\n", izmip);
return -1;
}
return zmops[izmop].route_from_zmips[izmip];
}
int zmop_get_routes_info(int izmop, int *buffer) {
if (izmop < 0 || izmop >= MAX_NUM_ZMOPS) {
fprintf(stderr, "ZynMidiRouter: Bad output port index (%d).\n", izmop);
return -1;
}
memcpy((void *)buffer, (void *)zmops[izmop].route_from_zmips, MAX_NUM_ZMIPS * sizeof(int));
return 1;
}
int zmop_get_routes_info_all(int *buffer) {
int iz;
for (iz=0; iz<MAX_NUM_ZMIPS; iz++) {
memcpy((void *)buffer, (void *)zmops[iz].route_from_zmips, MAX_NUM_ZMIPS * sizeof(int));
buffer += MAX_NUM_ZMIPS;
}
return 1;
}
// Note range & Transpose
int set_global_transpose(int8_t transpose) {
global_transpose = transpose;
return global_transpose;
}
int8_t get_global_transpose() {
return global_transpose;
}
int zmop_set_note_low(int iz, uint8_t nlow) {
if (iz < 0 || iz >= MAX_NUM_ZMOPS) {
fprintf(stderr, "ZynMidiRouter: Bad output port index (%d).\n", iz);
return 0;
}
zmops[iz].note_low = nlow;
return 1;
}
int zmop_set_note_high(int iz, uint8_t nhigh) {
if (iz < 0 || iz >= MAX_NUM_ZMOPS) {
fprintf(stderr, "ZynMidiRouter: Bad output port index (%d).\n", iz);
return 0;
}
zmops[iz].note_high = nhigh;
return 1;
}
int zmop_set_transpose_octave(int iz, int8_t trans_oct) {
if (iz < 0 || iz >= MAX_NUM_ZMOPS) {
fprintf(stderr, "ZynMidiRouter: Bad output port index (%d).\n", iz);
return 0;
}
zmops[iz].transpose_octave = trans_oct;
return 1;
}
int zmop_set_transpose_semitone(int iz, int8_t trans_semi) {
if (iz < 0 || iz >= MAX_NUM_ZMOPS) {
fprintf(stderr, "ZynMidiRouter: Bad output port index (%d).\n", iz);
return 0;
}
zmops[iz].transpose_semitone = trans_semi;
return 1;
}
uint8_t zmop_get_note_low(int iz) {
if (iz < 0 || iz >= MAX_NUM_ZMOPS) {
fprintf(stderr, "ZynMidiRouter: Bad output port index (%d).\n", iz);
return 0;
}
return zmops[iz].note_low;
}
uint8_t zmop_get_note_high(int iz) {
if (iz < 0 || iz >= MAX_NUM_ZMOPS) {
fprintf(stderr, "ZynMidiRouter: Bad output port index (%d).\n", iz);
return 127;
}
return zmops[iz].note_high;
}
int8_t zmop_get_transpose_octave(int iz) {
if (iz < 0 || iz >= MAX_NUM_ZMOPS) {
fprintf(stderr, "ZynMidiRouter: Bad output port index (%d).\n", iz);
return 0;
}
return zmops[iz].transpose_octave;
}
int8_t zmop_get_transpose_semitone(int iz) {
if (iz < 0 || iz >= MAX_NUM_ZMOPS) {
fprintf(stderr, "ZynMidiRouter: Bad output port index (%d).\n", iz);
return 0;
}
return zmops[iz].transpose_semitone;
}
int zmop_set_note_range_transpose(int iz, uint8_t nlow, uint8_t nhigh, int8_t trans_oct, int8_t trans_semi) {
if (iz < 0 || iz >= MAX_NUM_ZMOPS) {
fprintf(stderr, "ZynMidiRouter: Bad output port index (%d).\n", iz);
return 0;
}
zmops[iz].note_low = nlow;
zmops[iz].note_high = nhigh;
zmops[iz].transpose_octave = trans_oct;
zmops[iz].transpose_semitone = trans_semi;
return 1;
}