-
Notifications
You must be signed in to change notification settings - Fork 55
/
effects.c
7773 lines (6548 loc) · 256 KB
/
effects.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
/*
* This file is part of mod-host.
*
* mod-host 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 3 of the License, or
* (at your option) any later version.
*
* mod-host 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.
*
* You should have received a copy of the GNU General Public License
* along with mod-host. If not, see <http://www.gnu.org/licenses/>.
*
*/
/*
************************************************************************************************************************
* INCLUDE FILES
************************************************************************************************************************
*/
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <dlfcn.h>
#include <errno.h>
#include <limits.h>
#include <math.h>
#include <pthread.h>
#include <sys/stat.h>
/* Jack */
#include <jack/jack.h>
#include <jack/intclient.h>
#include <jack/metadata.h>
#include <jack/midiport.h>
#include <jack/transport.h>
#include <jack/uuid.h>
/* LV2 and Lilv */
#include <lilv/lilv.h>
#include <lv2/lv2plug.in/ns/ext/atom/atom.h>
#include <lv2/lv2plug.in/ns/ext/atom/forge.h>
#include <lv2/lv2plug.in/ns/ext/buf-size/buf-size.h>
#include <lv2/lv2plug.in/ns/ext/event/event.h>
#include <lv2/lv2plug.in/ns/ext/log/log.h>
#include <lv2/lv2plug.in/ns/ext/midi/midi.h>
#include <lv2/lv2plug.in/ns/ext/options/options.h>
#include <lv2/lv2plug.in/ns/ext/patch/patch.h>
#include <lv2/lv2plug.in/ns/ext/parameters/parameters.h>
#include <lv2/lv2plug.in/ns/ext/port-props/port-props.h>
#include <lv2/lv2plug.in/ns/ext/presets/presets.h>
#include <lv2/lv2plug.in/ns/ext/resize-port/resize-port.h>
#include <lv2/lv2plug.in/ns/ext/state/state.h>
#include <lv2/lv2plug.in/ns/ext/time/time.h>
#include <lv2/lv2plug.in/ns/ext/urid/urid.h>
#include <lv2/lv2plug.in/ns/ext/uri-map/uri-map.h>
#include <lv2/lv2plug.in/ns/ext/worker/worker.h>
#include "lv2/control-input-port-change-request.h"
#include "lv2/lv2-hmi.h"
#include "lv2/mod-license.h"
#ifdef HAVE_CONTROLCHAIN
/* Control Chain */
#include <cc_client.h>
#endif
#ifdef HAVE_HYLIA
/* Hylia / Link */
#include <hylia.h>
#endif
#include "mod-host.h"
#ifdef __MOD_DEVICES__
#include "sys_host.h"
#include "dsp/gate_core.h"
#endif
#ifndef HAVE_NEW_LILV
#define lilv_free(x) free(x)
#warning Your current lilv version does not support loading or unloading bundles
#endif
#ifndef LV2_BUF_SIZE__nominalBlockLength
#define LV2_BUF_SIZE__nominalBlockLength LV2_BUF_SIZE_PREFIX "nominalBlockLength"
#endif
#ifndef LV2_CORE__enabled
#define LV2_CORE__enabled LV2_CORE_PREFIX "enabled"
#endif
#ifndef LV2_STATE__threadSafeRestore
#define LV2_STATE__threadSafeRestore LV2_STATE_PREFIX "threadSafeRestore"
#endif
#ifndef LILV_URI_CV_PORT
#define LILV_URI_CV_PORT "http://lv2plug.in/ns/lv2core#CVPort"
#endif
#ifndef HAVE_LV2_STATE_FREE_PATH
// forwards compatibility with old lv2 headers
#define LV2_STATE__freePath LV2_STATE_PREFIX "freePath"
typedef void* LV2_State_Free_Path_Handle;
typedef struct {
LV2_State_Free_Path_Handle handle;
void (*free_path)(LV2_State_Free_Path_Handle handle, char* path);
} LV2_State_Free_Path;
#endif
#define LILV_NS_MOD "http://moddevices.com/ns/mod#"
#define KX_TIME__TicksPerBeat "http://kxstudio.sf.net/ns/lv2ext/props#TimePositionTicksPerBeat"
// custom jack flag used for cv
// needed because we prefer jack2 which doesn't have metadata yet
#define JackPortIsControlVoltage 0x100
/* Local */
#include "effects.h"
#include "monitor.h"
#include "socket.h"
#include "uridmap.h"
#include "lv2_evbuf.h"
#include "worker.h"
#include "state-paths.h"
#include "symap.h"
#include "monitor/monitor-client.h"
#include "sha1/sha1.h"
#include "rtmempool/list.h"
#include "rtmempool/rtmempool.h"
#include "filter.h"
/*
************************************************************************************************************************
* LOCAL DEFINES
************************************************************************************************************************
*/
#define REMOVE_ALL (-1)
#define GLOBAL_EFFECT_ID (9995)
#define ASSIGNMENT_UNUSED -1 // item was used before, so there might other valid items after this one
#define ASSIGNMENT_NULL -2 // item never used before, thus signaling the end of valid items
#define BYPASS_PORT_SYMBOL ":bypass"
#define PRESETS_PORT_SYMBOL ":presets"
#define BPB_PORT_SYMBOL ":bpb"
#define BPM_PORT_SYMBOL ":bpm"
#define ROLLING_PORT_SYMBOL ":rolling"
// use pitchbend as midi cc, with an invalid MIDI controller number
#define MIDI_PITCHBEND_AS_CC 131
// transport defaults
#define TRANSPORT_TICKS_PER_BEAT 1920.0
/*
************************************************************************************************************************
* LOCAL CONSTANTS
************************************************************************************************************************
*/
enum PortFlow {
FLOW_UNKNOWN,
FLOW_INPUT,
FLOW_OUTPUT
};
enum PortType {
TYPE_UNKNOWN,
TYPE_CONTROL,
TYPE_AUDIO,
TYPE_CV,
TYPE_EVENT
};
enum PortHints {
// controls
HINT_ENUMERATION = 1 << 0,
HINT_INTEGER = 1 << 1,
HINT_TOGGLE = 1 << 2,
HINT_TRIGGER = 1 << 3,
HINT_LOGARITHMIC = 1 << 4,
HINT_MONITORED = 1 << 5, // outputs only
// cv
HINT_CV_MOD = 1 << 0, // uses mod cvport
HINT_CV_RANGES = 1 << 0, // port info includes ranges
// events
HINT_TRANSPORT = 1 << 0,
HINT_MIDI_EVENT = 1 << 1,
HINT_OLD_EVENT_API = 1 << 2,
};
enum PluginHints {
//HINT_TRANSPORT = 1 << 0, // must match HINT_TRANSPORT set above
HINT_TRIGGERS = 1 << 1,
HINT_OUTPUT_MONITORS = 1 << 2,
HINT_HAS_MIDI_INPUT = 1 << 3,
HINT_HAS_STATE = 1 << 4,
HINT_STATE_UNSAFE = 1 << 5, // state restore needs mutex protection
};
enum TransportSyncMode {
TRANSPORT_SYNC_NONE,
TRANSPORT_SYNC_ABLETON_LINK,
TRANSPORT_SYNC_MIDI,
};
enum {
URI_MAP_FEATURE,
URID_MAP_FEATURE,
URID_UNMAP_FEATURE,
OPTIONS_FEATURE,
#ifdef __MOD_DEVICES__
HMI_WC_FEATURE,
#endif
LICENSE_FEATURE,
BUF_SIZE_POWER2_FEATURE,
BUF_SIZE_FIXED_FEATURE,
BUF_SIZE_BOUNDED_FEATURE,
LOG_FEATURE,
STATE_FREE_PATH_FEATURE,
STATE_MAKE_PATH_FEATURE,
CTRLPORT_REQUEST_FEATURE,
WORKER_FEATURE,
FEATURE_TERMINATOR
};
enum PostPonedEventType {
POSTPONED_PARAM_SET,
POSTPONED_OUTPUT_MONITOR,
POSTPONED_MIDI_PROGRAM_CHANGE,
POSTPONED_MIDI_MAP,
POSTPONED_TRANSPORT,
POSTPONED_JACK_MIDI_CONNECT,
POSTPONED_LOG_TRACE, // stack allocated, rt-safe
POSTPONED_LOG_MESSAGE, // heap allocated
POSTPONED_PROCESS_OUTPUT_BUFFER
};
enum UpdatePositionFlag {
UPDATE_POSITION_SKIP,
UPDATE_POSITION_IF_CHANGED,
UPDATE_POSITION_FORCED,
};
/*
************************************************************************************************************************
* LOCAL DATA TYPES
************************************************************************************************************************
*/
#ifdef __MOD_DEVICES__
typedef struct HMI_ADDRESSING_T hmi_addressing_t;
#endif
typedef struct PORT_T port_t;
typedef struct CV_SOURCE_T {
port_t *port;
jack_port_t *jack_port;
float source_min_value;
float source_max_value;
float source_diff_value;
float min_value;
float max_value;
float diff_value;
float prev_value;
} cv_source_t;
typedef struct PORT_T {
uint32_t index;
enum PortType type;
enum PortFlow flow;
enum PortHints hints;
const char* symbol;
jack_port_t *jack_port;
float *buffer;
uint32_t buffer_count;
LV2_Evbuf *evbuf;
float min_value;
float max_value;
float def_value;
float prev_value;
LilvScalePoints* scale_points;
cv_source_t* cv_source;
pthread_mutex_t cv_source_mutex;
#ifdef __MOD_DEVICES__
hmi_addressing_t* hmi_addressing;
#endif
} port_t;
typedef struct PROPERTY_T {
LilvNode* uri;
LilvNode* type;
bool monitored;
} property_t;
typedef struct PROPERTY_EVENT_T {
uint32_t size;
uint8_t body[];
} property_event_t;
typedef struct PRESET_T {
LilvNode *uri;
} preset_t;
typedef struct MONITOR_T {
int port_id;
int op;
float value;
float last_notified_value;
} monitor_t;
typedef struct EFFECT_T {
int instance;
jack_client_t *jack_client;
LilvInstance *lilv_instance;
const LilvPlugin *lilv_plugin;
const LV2_Feature **features;
port_t **ports;
uint32_t ports_count;
property_t **properties;
uint32_t properties_count;
port_t **audio_ports;
uint32_t audio_ports_count;
port_t **input_audio_ports;
uint32_t input_audio_ports_count;
port_t **output_audio_ports;
uint32_t output_audio_ports_count;
port_t **control_ports;
uint32_t control_ports_count;
port_t **input_control_ports;
uint32_t input_control_ports_count;
port_t **output_control_ports;
uint32_t output_control_ports_count;
port_t **cv_ports;
uint32_t cv_ports_count;
port_t **input_cv_ports;
uint32_t input_cv_ports_count;
port_t **output_cv_ports;
uint32_t output_cv_ports_count;
port_t **event_ports;
uint32_t event_ports_count;
port_t **input_event_ports;
uint32_t input_event_ports_count;
port_t **output_event_ports;
uint32_t output_event_ports_count;
// indexes of specially designated ports (-1 if unavailable)
int32_t control_index; // control/event input
int32_t enabled_index;
int32_t freewheel_index;
int32_t bpb_index;
int32_t bpm_index;
int32_t speed_index;
preset_t **presets;
uint32_t presets_count;
monitor_t **monitors;
uint32_t monitors_count;
worker_t worker;
const MOD_License_Interface *license_iface;
const LV2_State_Interface *state_iface;
#ifdef __MOD_DEVICES__
const LV2_HMI_PluginNotification *hmi_notif;
#endif
jack_ringbuffer_t *events_in_buffer;
jack_ringbuffer_t *events_out_buffer;
// previous transport state
bool transport_rolling;
uint32_t transport_frame;
double transport_bpb;
double transport_bpm;
// current and previous bypass state
port_t bypass_port;
float bypass;
bool was_bypassed;
// cached plugin information, avoids iterating controls each cycle
enum PluginHints hints;
// virtual presets port
port_t presets_port;
float preset_value;
// thread-safe state restore
pthread_mutex_t state_restore_mutex;
// state save/restore custom directory
const char* state_dir;
} effect_t;
typedef struct LILV_NODES_T {
LilvNode *atom_port;
LilvNode *audio;
LilvNode *control;
LilvNode *control_in;
LilvNode *cv;
LilvNode *default_;
LilvNode *enabled;
LilvNode *enumeration;
LilvNode *event;
LilvNode *freeWheeling;
LilvNode *hmi_interface;
LilvNode *input;
LilvNode *integer;
LilvNode *license_interface;
LilvNode *logarithmic;
LilvNode *maximum;
LilvNode *midiEvent;
LilvNode *minimum;
LilvNode *minimumSize;
LilvNode *mod_cvport;
LilvNode *mod_default;
LilvNode *mod_maximum;
LilvNode *mod_minimum;
LilvNode *output;
LilvNode *patch_readable;
LilvNode *patch_writable;
LilvNode *preferMomentaryOff;
LilvNode *preferMomentaryOn;
LilvNode *preset;
LilvNode *rawMIDIClockAccess;
LilvNode *rdfs_range;
LilvNode *sample_rate;
LilvNode *state_interface;
LilvNode *state_load_default_state;
LilvNode *state_thread_safe_restore;
LilvNode *timeBeatsPerBar;
LilvNode *timeBeatsPerMinute;
LilvNode *timePosition;
LilvNode *timeSpeed;
LilvNode *toggled;
LilvNode *trigger;
LilvNode *worker_interface;
} lilv_nodes_t;
typedef struct URIDS_T {
LV2_URID atom_Bool;
LV2_URID atom_Double;
LV2_URID atom_Float;
LV2_URID atom_Int;
LV2_URID atom_Long;
LV2_URID atom_Object;
LV2_URID atom_Path;
LV2_URID atom_String;
LV2_URID atom_Tuple;
LV2_URID atom_URI;
LV2_URID atom_Vector;
LV2_URID atom_eventTransfer;
LV2_URID bufsz_maxBlockLength;
LV2_URID bufsz_minBlockLength;
LV2_URID bufsz_nomimalBlockLength;
LV2_URID bufsz_sequenceSize;
LV2_URID log_Error;
LV2_URID log_Note;
LV2_URID log_Trace;
LV2_URID log_Warning;
LV2_URID midi_MidiEvent;
LV2_URID param_sampleRate;
LV2_URID patch_Get;
LV2_URID patch_Set;
LV2_URID patch_property;
LV2_URID patch_sequence;
LV2_URID patch_value;
LV2_URID time_Position;
LV2_URID time_bar;
LV2_URID time_barBeat;
LV2_URID time_beat;
LV2_URID time_beatUnit;
LV2_URID time_beatsPerBar;
LV2_URID time_beatsPerMinute;
LV2_URID time_ticksPerBeat;
LV2_URID time_frame;
LV2_URID time_speed;
} urids_t;
typedef struct MIDI_CC_T {
int8_t channel;
uint8_t controller;
float minimum;
float maximum;
int effect_id;
const char* symbol;
port_t* port;
} midi_cc_t;
typedef struct ASSIGNMENT_T {
int effect_id;
port_t *port;
int device_id;
int assignment_id;
int actuator_id;
int actuator_pair_id;
int assignment_pair_id;
bool supports_set_value;
} assignment_t;
typedef struct HMI_ADDRESSING_T {
int actuator_id;
uint8_t page;
uint8_t subpage;
} hmi_addressing_t;
typedef struct POSTPONED_PARAMETER_EVENT_T {
int effect_id;
const char* symbol;
float value;
} postponed_parameter_event_t;
typedef struct POSTPONED_MIDI_PROGRAM_CHANGE_EVENT_T {
int8_t program;
int8_t channel;
} postponed_midi_program_change_event_t;
typedef struct POSTPONED_MIDI_MAP_EVENT_T {
int effect_id;
const char* symbol;
int8_t channel;
uint8_t controller;
float value;
float minimum;
float maximum;
} postponed_midi_map_event_t;
typedef struct POSTPONED_TRANSPORT_EVENT_T {
bool rolling;
float bpb;
float bpm;
} postponed_transport_event_t;
typedef struct POSTPONED_JACK_MIDI_CONNECT_EVENT_T {
jack_port_id_t port;
} postponed_jack_midi_connect_event_t;
typedef struct POSTPONED_LOG_TRACE_EVENT_T {
char msg[32];
} postponed_log_trace_event_t;
typedef struct POSTPONED_LOG_MESSAGE_EVENT_T {
LogType type;
char *msg; // NOTE: heap allocated, needs to be freed on reader side
} postponed_log_message_event_t;
typedef struct POSTPONED_PROCESS_OUTPUT_BUFFER_EVENT_T {
int effect_id;
} postponed_process_output_buffer_event_t;
typedef struct POSTPONED_EVENT_T {
enum PostPonedEventType type;
union {
postponed_parameter_event_t parameter;
postponed_midi_program_change_event_t program_change;
postponed_midi_map_event_t midi_map;
postponed_transport_event_t transport;
postponed_jack_midi_connect_event_t jack_midi_connect;
postponed_log_trace_event_t log_trace;
postponed_log_message_event_t log_message;
postponed_process_output_buffer_event_t process_out_buf;
};
} postponed_event_t;
typedef struct POSTPONED_EVENT_LIST_DATA {
postponed_event_t event;
struct list_head siblings;
} postponed_event_list_data;
typedef struct POSTPONED_CACHED_EFFECT_LIST_DATA {
int effect_id;
struct list_head siblings;
} postponed_cached_effect_list_data;
typedef struct POSTPONED_CACHED_EFFECT_EVENTS {
int last_effect_id;
postponed_cached_effect_list_data effects;
} postponed_cached_effect_events;
typedef struct POSTPONED_CACHED_SYMBOL_LIST_DATA {
int effect_id;
char symbol[MAX_CHAR_BUF_SIZE+1];
struct list_head siblings;
} postponed_cached_symbol_list_data;
typedef struct POSTPONED_CACHED_SYMBOL_EVENTS {
int last_effect_id;
char last_symbol[MAX_CHAR_BUF_SIZE+1];
postponed_cached_symbol_list_data symbols;
} postponed_cached_symbol_events;
typedef struct RAW_MIDI_PORT_ITEM {
int instance;
jack_port_t* jack_port;
struct list_head siblings;
} raw_midi_port_item;
/*
************************************************************************************************************************
* LOCAL MACROS
************************************************************************************************************************
*/
#define UNUSED_PARAM(var) do { (void)(var); } while (0)
#define INSTANCE_IS_VALID(id) ((id >= 0) && (id < MAX_INSTANCES))
/* used to indicate if a parameter change was initiated from us */
#define MAGIC_PARAMETER_SEQ_NUMBER -1337
/*
************************************************************************************************************************
* LOCAL GLOBAL VARIABLES
************************************************************************************************************************
*/
static effect_t g_effects[MAX_INSTANCES];
static midi_cc_t g_midi_cc_list[MAX_MIDI_CC_ASSIGN], *g_midi_learning;
#ifdef HAVE_CONTROLCHAIN
/* Control Chain */
static cc_client_t *g_cc_client = NULL;
static assignment_t g_assignments_list[CC_MAX_DEVICES][CC_MAX_ASSIGNMENTS];
#endif
static struct list_head g_rtsafe_list;
static RtMemPool_Handle g_rtsafe_mem_pool;
static pthread_mutex_t g_rtsafe_mutex;
static volatile int g_postevents_running; // 0: stopped, 1: running, -1: stopped & about to close mod-host
static volatile bool g_postevents_ready;
static sem_t g_postevents_semaphore;
static pthread_t g_postevents_thread;
/* raw-midi-clock-access ports */
static struct list_head g_raw_midi_port_list;
static pthread_mutex_t g_raw_midi_port_mutex;
/* Jack */
static jack_client_t *g_jack_global_client;
static jack_nframes_t g_sample_rate, g_block_length, g_max_allowed_midi_delta;
static const char **g_capture_ports, **g_playback_ports;
static size_t g_midi_buffer_size;
#ifdef __MOD_DEVICES__
#ifdef _MOD_DEVICE_DWARF
static jack_port_t *g_audio_in1_port;
static jack_port_t *g_audio_in2_port;
static jack_port_t *g_audio_out1_port;
static jack_port_t *g_audio_out2_port;
#endif
#endif
static jack_port_t *g_midi_in_port;
static jack_position_t g_jack_pos;
static bool g_jack_rolling;
static volatile double g_transport_bpb;
static volatile double g_transport_bpm;
static volatile bool g_transport_reset;
static volatile enum TransportSyncMode g_transport_sync_mode;
static double g_transport_tick;
static bool g_aggregated_midi_enabled;
static bool g_processing_enabled;
static bool g_verbose_debug;
// Wall clock time since program startup
static uint64_t g_monotonic_frame_count = 0;
// Used for the MIDI Beat Clock Slave
static volatile uint64_t g_previous_midi_event_time = 0;
/* LV2 and Lilv */
static LilvWorld *g_lv2_data;
static const LilvPlugins *g_plugins;
static char *g_lv2_scratch_dir;
/* Global features */
static Symap* g_symap;
static lilv_nodes_t g_lilv_nodes;
static urids_t g_urids;
#ifdef __MOD_DEVICES__
static LV2_HMI_WidgetControl g_hmi_wc;
#endif
static MOD_License_Feature g_license;
static LV2_Atom_Forge g_lv2_atom_forge;
static LV2_Log_Log g_lv2_log;
static LV2_Options_Option g_options[6];
static LV2_State_Free_Path g_state_freePath;
static LV2_URI_Map_Feature g_uri_map;
static LV2_URID_Map g_urid_map;
static LV2_URID_Unmap g_urid_unmap;
static LV2_Feature g_buf_size_features[3] = {
{ LV2_BUF_SIZE__powerOf2BlockLength, NULL },
{ LV2_BUF_SIZE__fixedBlockLength, NULL },
{ LV2_BUF_SIZE__boundedBlockLength, NULL }
};
#ifdef __MOD_DEVICES__
static LV2_Feature g_hmi_wc_feature = { LV2_HMI__WidgetControl, &g_hmi_wc };
#endif
static LV2_Feature g_license_feature = { MOD_LICENSE__feature, &g_license };
static LV2_Feature g_lv2_log_feature = { LV2_LOG__log, &g_lv2_log };
static LV2_Feature g_options_feature = { LV2_OPTIONS__options, &g_options };
static LV2_Feature g_state_freePath_feature = { LV2_STATE__freePath, &g_state_freePath };
static LV2_Feature g_uri_map_feature = { LV2_URI_MAP_URI, &g_uri_map };
static LV2_Feature g_urid_map_feature = { LV2_URID__map, &g_urid_map };
static LV2_Feature g_urid_unmap_feature = { LV2_URID__unmap, &g_urid_unmap };
/* MIDI Learn */
static pthread_mutex_t g_midi_learning_mutex;
/* MIDI program monitoring */
static bool g_monitored_midi_programs[16];
#ifdef HAVE_HYLIA
static hylia_t* g_hylia_instance;
static hylia_time_info_t g_hylia_timeinfo;
#endif
#ifdef __MOD_DEVICES__
/* HMI integration */
static int g_hmi_shmfd;
static sys_serial_shm_data* g_hmi_data;
static pthread_t g_hmi_client_thread;
static pthread_mutex_t g_hmi_mutex;
static hmi_addressing_t g_hmi_addressings[MAX_HMI_ADDRESSINGS];
/* internal processing */
static int g_compressor_mode = 0;
static int g_compressor_release = 100;
#ifdef _MOD_DEVICE_DWARF
static int g_noisegate_channel = 0;
static int g_noisegate_decay = 10;
static int g_noisegate_threshold = -60;
static gate_t g_noisegate;
#endif
#endif
static const char* const g_bypass_port_symbol = BYPASS_PORT_SYMBOL;
static const char* const g_presets_port_symbol = PRESETS_PORT_SYMBOL;
static const char* const g_bpb_port_symbol = BPB_PORT_SYMBOL;
static const char* const g_bpm_port_symbol = BPM_PORT_SYMBOL;
static const char* const g_rolling_port_symbol = ROLLING_PORT_SYMBOL;
/*
************************************************************************************************************************
* LOCAL FUNCTION PROTOTYPES
************************************************************************************************************************
*/
static void InstanceDelete(int effect_id);
static int InstanceExist(int effect_id);
static void AllocatePortBuffers(effect_t* effect);
static int BufferSize(jack_nframes_t nframes, void* data);
static void FreeWheelMode(int starting, void* data);
static void PortRegistration(jack_port_id_t port_id, int reg, void* data);
static void RunPostPonedEvents(int ignored_effect_id);
static void* PostPonedEventsThread(void* arg);
#ifdef __MOD_DEVICES__
static void* HMIClientThread(void* arg);
#endif
static int ProcessPlugin(jack_nframes_t nframes, void *arg);
static bool SetPortValue(port_t *port, float value, int effect_id, bool is_bypass);
static float UpdateValueFromMidi(midi_cc_t* mcc, uint16_t mvalue, bool highres);
static bool UpdateGlobalJackPosition(enum UpdatePositionFlag flag, bool do_post);
static int ProcessGlobalClient(jack_nframes_t nframes, void *arg);
static void JackTimebase(jack_transport_state_t state, jack_nframes_t nframes,
jack_position_t* pos, int new_pos, void* arg);
static void JackThreadInit(void *arg);
static void GetFeatures(effect_t *effect);
static void TriggerJackTimebase(bool reset_to_zero);
static property_t *FindEffectPropertyByURI(effect_t *effect, const char *uri);
static port_t *FindEffectInputPortBySymbol(effect_t *effect, const char *control_symbol);
static port_t *FindEffectOutputPortBySymbol(effect_t *effect, const char *control_symbol);
static const void *GetPortValueForState(const char* symbol, void* user_data, uint32_t* size, uint32_t* type);
static int LoadPresets(effect_t *effect);
static void FreeFeatures(effect_t *effect);
static void FreePluginString(void* handle, char *str);
static void ConnectToAllHardwareMIDIPorts(void);
static void ConnectToMIDIThroughPorts(void);
#ifdef __MOD_DEVICES__
static void HMIWidgetsSetLed(LV2_HMI_WidgetControl_Handle handle,
LV2_HMI_Addressing addressing,
LV2_HMI_LED_Colour led_color,
int on_blink_time,
int off_blink_time);
static void HMIWidgetsSetLabel(LV2_HMI_WidgetControl_Handle handle,
LV2_HMI_Addressing addressing,
const char* label);
static void HMIWidgetsSetValue(LV2_HMI_WidgetControl_Handle handle,
LV2_HMI_Addressing addressing,
const char* value);
static void HMIWidgetsSetUnit(LV2_HMI_WidgetControl_Handle handle,
LV2_HMI_Addressing addressing,
const char* unit);
static void HMIWidgetsSetIndicator(LV2_HMI_WidgetControl_Handle handle,
LV2_HMI_Addressing addressing,
float indicator_poss);
#endif
static char *GetLicenseFile(MOD_License_Handle handle, const char *license_uri);
static int LogPrintf(LV2_Log_Handle handle, LV2_URID type, const char *fmt, ...);
static int LogVPrintf(LV2_Log_Handle handle, LV2_URID type, const char *fmt, va_list ap);
static LV2_ControlInputPort_Change_Status RequestControlPortChange(LV2_ControlInputPort_Change_Request_Handle handle,
uint32_t index, float value);
static char* MakePluginStatePathFromSratchDir(LV2_State_Make_Path_Handle handle, const char *path);
static char* MakePluginStatePathDuringLoadSave(LV2_State_Make_Path_Handle handle, const char *path);
#ifdef HAVE_CONTROLCHAIN
static void CCDataUpdate(void* arg);
static void InitializeControlChainIfNeeded(void);
static bool CheckCCDeviceProtocolVersion(int device_id, int major, int minor);
#endif
#ifdef HAVE_HYLIA
static uint32_t GetHyliaOutputLatency(void);
#endif
/*
************************************************************************************************************************
* LOCAL CONFIGURATION ERRORS
************************************************************************************************************************
*/
/*
************************************************************************************************************************
* LOCAL FUNCTIONS
************************************************************************************************************************
*/
#ifdef __APPLE__
// FIXME missing on macOS
static char* strchrnul(const char *s, int c)
{
char *r = strchr(s, c);
if (r != NULL)
*r = '\0';
return r;
}
#endif
static void InstanceDelete(int effect_id)
{
if (INSTANCE_IS_VALID(effect_id))
bzero(&g_effects[effect_id], sizeof(effect_t));
}
static int InstanceExist(int effect_id)
{
if (INSTANCE_IS_VALID(effect_id))
{
return (int)(g_effects[effect_id].jack_client != NULL);
}
return 0;
}
static void AllocatePortBuffers(effect_t* effect)
{
uint32_t i;
for (i = 0; i < effect->event_ports_count; i++)
{
lv2_evbuf_free(effect->event_ports[i]->evbuf);
effect->event_ports[i]->evbuf = lv2_evbuf_new(
g_midi_buffer_size,
(effect->event_ports[i]->hints & HINT_OLD_EVENT_API) ? LV2_EVBUF_EVENT : LV2_EVBUF_ATOM,
g_urid_map.map(g_urid_map.handle, LV2_ATOM__Chunk),
g_urid_map.map(g_urid_map.handle, LV2_ATOM__Sequence));
LV2_Atom_Sequence *buf;
buf = lv2_evbuf_get_buffer(effect->event_ports[i]->evbuf);
lilv_instance_connect_port(effect->lilv_instance, effect->event_ports[i]->index, buf);
}
}
static int BufferSize(jack_nframes_t nframes, void* data)
{
g_block_length = nframes;
g_midi_buffer_size = jack_port_type_get_buffer_size(g_jack_global_client, JACK_DEFAULT_MIDI_TYPE);
if (data)
{
effect_t *effect = data;
AllocatePortBuffers(effect);
}
#ifdef HAVE_HYLIA
else if (g_hylia_instance)
{
hylia_set_output_latency(g_hylia_instance, GetHyliaOutputLatency());
}
#endif
return SUCCESS;
}
static void FreeWheelMode(int starting, void* data)
{
effect_t *effect = data;
if (effect->freewheel_index >= 0)
{
*(effect->ports[effect->freewheel_index]->buffer) = starting ? 1.0f : 0.0f;
}
}
static void PortRegistration(jack_port_id_t port_id, int reg, void* data)
{
if (g_aggregated_midi_enabled || reg == 0)
return;
/* port flags to connect to */
static const int target_port_flags = JackPortIsTerminal|JackPortIsPhysical|JackPortIsOutput;
const jack_port_t* const port = jack_port_by_id(g_jack_global_client, port_id);
if ((jack_port_flags(port) & target_port_flags) != target_port_flags)
return;
if (strcmp(jack_port_type(port), JACK_DEFAULT_MIDI_TYPE) != 0)
return;
postponed_event_list_data* const posteventptr = rtsafe_memory_pool_allocate_atomic(g_rtsafe_mem_pool);
if (posteventptr == NULL)
return;
posteventptr->event.type = POSTPONED_JACK_MIDI_CONNECT;
posteventptr->event.jack_midi_connect.port = port_id;
pthread_mutex_lock(&g_rtsafe_mutex);
list_add_tail(&posteventptr->siblings, &g_rtsafe_list);
pthread_mutex_unlock(&g_rtsafe_mutex);
sem_post(&g_postevents_semaphore);
return;
UNUSED_PARAM(data);
}
static bool ShouldIgnorePostPonedEffectEvent(int effect_id, postponed_cached_effect_events* cached_events)
{
if (effect_id == cached_events->last_effect_id)
{
// already received this event, like just now
return true;
}
// we received some events, but last one was different
// we might be getting interleaved events, so let's check if it's there
struct list_head *it;
list_for_each(it, &cached_events->effects.siblings)
{
postponed_cached_effect_list_data* const peffect = list_entry(it, postponed_cached_effect_list_data, siblings);
if (effect_id == peffect->effect_id)
{
// haha! found you little bastard!
return true;
}
}
// we'll process this event because it's the "last" of its type
// also add it to the list of received events
postponed_cached_effect_list_data* const peffect = malloc(sizeof(postponed_cached_effect_list_data));
if (peffect)
{
peffect->effect_id = effect_id;
list_add_tail(&peffect->siblings, &cached_events->effects.siblings);
}
return false;
}
static bool ShouldIgnorePostPonedSymbolEvent(postponed_parameter_event_t* ev,
postponed_cached_symbol_events* cached_events)
{
// symbol must not be null
if (ev->symbol == NULL)
return false;
if (ev->effect_id == cached_events->last_effect_id &&