-
Notifications
You must be signed in to change notification settings - Fork 69
Expand file tree
/
Copy patheffects.c
More file actions
4499 lines (3802 loc) · 144 KB
/
Copy patheffects.c
File metadata and controls
4499 lines (3802 loc) · 144 KB
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
************************************************************************************************************************
*/
#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>
/* Jack */
#include <jack/jack.h>
#include <jack/midiport.h>
#include <jack/transport.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/midi/midi.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/time/time.h>
#include <lv2/lv2plug.in/ns/ext/worker/worker.h>
#include <lv2/lv2plug.in/ns/ext/patch/patch.h>
#include <lv2/lv2plug.in/ns/ext/event/event.h>
#include <lv2/lv2plug.in/ns/ext/state/state.h>
#include <lv2/lv2plug.in/ns/ext/presets/presets.h>
#include <lv2/lv2plug.in/ns/ext/options/options.h>
#include <lv2/lv2plug.in/ns/ext/port-props/port-props.h>
#include <lv2/lv2plug.in/ns/ext/buf-size/buf-size.h>
#include <lv2/lv2plug.in/ns/ext/parameters/parameters.h>
#include "mod-license.h"
#ifdef HAVE_CONTROLCHAIN
/* Control Chain */
#include <cc/cc_client.h>
#endif
#ifdef HAVE_HYLIA
/* Hylia / Link */
#include <hylia.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 LILV_URI_CV_PORT
#define LILV_URI_CV_PORT "http://lv2plug.in/ns/lv2core#CVPort"
#endif
#ifndef LV2_CORE__enabled
#define LV2_CORE__enabled LV2_CORE_PREFIX "enabled"
#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 "symap.h"
#include "sha1/sha1.h"
#include "rtmempool/list.h"
#include "rtmempool/rtmempool.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
// events
HINT_TRANSPORT = 1 << 0,
HINT_OLD_EVENT_API = 1 << 1,
};
enum PluginHints {
//HINT_TRANSPORT = 1 << 0,
HINT_TRIGGERS = 1 << 1,
HINT_OUTPUT_MONITORS = 1 << 2,
};
enum {
URI_MAP_FEATURE,
URID_MAP_FEATURE,
URID_UNMAP_FEATURE,
OPTIONS_FEATURE,
WORKER_FEATURE,
LICENSE_FEATURE,
BUF_SIZE_POWER2_FEATURE,
BUF_SIZE_FIXED_FEATURE,
BUF_SIZE_BOUNDED_FEATURE,
FEATURE_TERMINATOR
};
enum PostPonedEventType {
POSTPONED_PARAM_SET,
POSTPONED_OUTPUT_MONITOR,
POSTPONED_PROGRAM_LISTEN,
POSTPONED_MIDI_MAP,
POSTPONED_TRANSPORT
};
enum UpdatePositionFlag {
UPDATE_POSTION_SKIP,
UPDATE_POSTION_IF_CHANGED,
UPDATE_POSTION_FORCED,
};
/*
************************************************************************************************************************
* LOCAL DATA TYPES
************************************************************************************************************************
*/
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;
} port_t;
typedef struct PROPERTY_T {
const LilvNode* label;
const LilvNode* property;
} property_t;
typedef struct PROPERTY_EVENT_T {
uint32_t size;
uint8_t body[];
} property_event_t;
typedef struct PRESET_T {
const 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;
jack_ringbuffer_t *events_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 itenerating controls each cycle
enum PluginHints hints;
// virtual presets port
port_t presets_port;
float preset_value;
} effect_t;
typedef struct URIDS_T {
LV2_URID atom_Float;
LV2_URID atom_Double;
LV2_URID atom_Int;
LV2_URID atom_Long;
LV2_URID atom_eventTransfer;
LV2_URID bufsz_maxBlockLength;
LV2_URID bufsz_minBlockLength;
LV2_URID bufsz_nomimalBlockLength;
LV2_URID bufsz_sequenceSize;
LV2_URID log_Trace;
LV2_URID midi_MidiEvent;
LV2_URID param_sampleRate;
LV2_URID patch_Set;
LV2_URID patch_property;
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;
const port_t* port;
} midi_cc_t;
typedef struct ASSIGNMENT_T {
int effect_id;
const port_t *port;
int device_id;
int assignment_id;
} assignment_t;
typedef struct POSTPONED_PARAMETER_EVENT_T {
int effect_id;
const char* symbol;
float value;
} postponed_parameter_event_t;
typedef struct POSTPONED_PROGRAM_EVENT_T {
int8_t value;
} postponed_program_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_EVENT_T {
enum PostPonedEventType type;
union {
postponed_parameter_event_t parameter;
postponed_program_event_t program;
postponed_midi_map_event_t midi_map;
postponed_transport_event_t transport;
};
} 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_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_EVENTS {
int last_effect_id;
char last_symbol[MAX_CHAR_BUF_SIZE+1];
postponed_cached_symbol_list_data symbols;
} postponed_cached_events;
/*
************************************************************************************************************************
* LOCAL MACROS
************************************************************************************************************************
*/
#define UNUSED_PARAM(var) do { (void)(var); } while (0)
#define INSTANCE_IS_VALID(id) ((id >= 0) && (id < MAX_INSTANCES))
/*
************************************************************************************************************************
* 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;
/* Jack */
static jack_client_t *g_jack_global_client;
static jack_nframes_t g_sample_rate, g_block_length;
static const char **g_capture_ports, **g_playback_ports;
static size_t g_midi_buffer_size;
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 double g_transport_tick;
static bool g_processing_enabled;
/* LV2 and Lilv */
static LilvWorld *g_lv2_data;
static const LilvPlugins *g_plugins;
static LilvNode *g_sample_rate_node;
/* Global features */
static Symap* g_symap;
static urids_t g_urids;
static LV2_Atom_Forge g_lv2_atom_forge;
static LV2_URI_Map_Feature g_uri_map;
static LV2_URID_Map g_urid_map;
static LV2_URID_Unmap g_urid_unmap;
static LV2_Options_Option g_options[6];
static MOD_License_Feature g_license;
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};
static LV2_Feature g_options_feature = {LV2_OPTIONS__options, &g_options};
static LV2_Feature g_license_feature = {MOD_LICENSE__feature, &g_license};
static LV2_Feature g_buf_size_features[3] = {
{ LV2_BUF_SIZE__powerOf2BlockLength, NULL },
{ LV2_BUF_SIZE__fixedBlockLength, NULL },
{ LV2_BUF_SIZE__boundedBlockLength, NULL }
};
/* MIDI Learn */
static int g_midi_program_listen;
static pthread_mutex_t g_midi_learning_mutex;
#ifdef HAVE_HYLIA
static volatile bool hylia_enabled;
static hylia_t* g_hylia_instance;
static hylia_time_info_t g_hylia_timeinfo;
#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 RunPostPonedEvents(int ignored_effect_id);
static void* PostPonedEventsThread(void* arg);
static int ProcessPlugin(jack_nframes_t nframes, void *arg);
static float UpdateValueFromMidi(midi_cc_t* mcc, uint16_t mvalue, bool highres);
static void UpdateGlobalJackPosition(enum UpdatePositionFlag flag);
static int ProcessMidi(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 property_t *FindEffectPropertyByLabel(effect_t *effect, const char *label);
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 char* GetLicenseFile(MOD_License_Handle handle, const char *license_uri);
static void FreeLicenseData(MOD_License_Handle handle, char *license);
#ifdef HAVE_CONTROLCHAIN
static void CCDataUpdate(void* arg);
static void InitializeControlChainIfNeeded(void);
#endif
#ifdef HAVE_HYLIA
static uint32_t GetHyliaOutputLatency(void);
#endif
/*
************************************************************************************************************************
* LOCAL CONFIGURATION ERRORS
************************************************************************************************************************
*/
/*
************************************************************************************************************************
* LOCAL FUNCTIONS
************************************************************************************************************************
*/
static void InstanceDelete(int effect_id)
{
if (INSTANCE_IS_VALID(effect_id))
{
g_effects[effect_id].jack_client = NULL;
}
}
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 bool ShouldIgnorePostPonedEvent(postponed_parameter_event_t* ev, postponed_cached_events* cached_events)
{
// symbol must not be null
if (ev->symbol == NULL)
return false;
if (ev->effect_id == cached_events->last_effect_id &&
strncmp(ev->symbol, cached_events->last_symbol, MAX_CHAR_BUF_SIZE) == 0)
{
// 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->symbols.siblings)
{
postponed_cached_symbol_list_data* const psymbol = list_entry(it, postponed_cached_symbol_list_data, siblings);
if (ev->effect_id == psymbol->effect_id &&
strncmp(ev->symbol, psymbol->symbol, MAX_CHAR_BUF_SIZE) == 0)
{
// 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_symbol_list_data* const psymbol = malloc(sizeof(postponed_cached_symbol_list_data));
if (psymbol)
{
psymbol->effect_id = ev->effect_id;
strncpy(psymbol->symbol, ev->symbol, MAX_CHAR_BUF_SIZE);
psymbol->symbol[MAX_CHAR_BUF_SIZE] = '\0';
list_add_tail(&psymbol->siblings, &cached_events->symbols.siblings);
}
return false;
}
static void RunPostPonedEvents(int ignored_effect_id)
{
// local queue to where we'll save rtsafe list
struct list_head queue;
INIT_LIST_HEAD(&queue);
// move rtsafe list to our local queue, and clear it
pthread_mutex_lock(&g_rtsafe_mutex);
list_splice_init(&g_rtsafe_list, &queue);
pthread_mutex_unlock(&g_rtsafe_mutex);
if (list_empty(&queue))
{
// nothing to do
return;
}
// local buffer
char buf[MAX_CHAR_BUF_SIZE+1];
buf[MAX_CHAR_BUF_SIZE] = '\0';
// cached data, to make sure we only handle similar events once
bool got_midi_program = false;
bool got_transport = false;
postponed_cached_events cached_param_set, cached_output_mon;
postponed_cached_symbol_list_data *psymbol;
cached_param_set.last_effect_id = -1;
cached_output_mon.last_effect_id = -1;
cached_param_set.last_symbol[0] = '\0';
cached_param_set.last_symbol[MAX_CHAR_BUF_SIZE] = '\0';
cached_output_mon.last_symbol[0] = '\0';
cached_output_mon.last_symbol[MAX_CHAR_BUF_SIZE] = '\0';
INIT_LIST_HEAD(&cached_param_set.symbols.siblings);
INIT_LIST_HEAD(&cached_output_mon.symbols.siblings);
// itenerate backwards
struct list_head *it, *it2;
postponed_event_list_data* eventptr;
list_for_each_prev(it, &queue)
{
eventptr = list_entry(it, postponed_event_list_data, siblings);
switch (eventptr->event.type)
{
case POSTPONED_PARAM_SET:
if (eventptr->event.parameter.effect_id == ignored_effect_id)
continue;
if (ShouldIgnorePostPonedEvent(&eventptr->event.parameter, &cached_param_set))
continue;
snprintf(buf, MAX_CHAR_BUF_SIZE, "param_set %i %s %f", eventptr->event.parameter.effect_id,
eventptr->event.parameter.symbol,
eventptr->event.parameter.value);
socket_send_feedback(buf);
// save for fast checkup next time
cached_param_set.last_effect_id = eventptr->event.parameter.effect_id;
strncpy(cached_param_set.last_symbol, eventptr->event.parameter.symbol, MAX_CHAR_BUF_SIZE);
break;
case POSTPONED_OUTPUT_MONITOR:
if (eventptr->event.parameter.effect_id == ignored_effect_id)
continue;
if (ShouldIgnorePostPonedEvent(&eventptr->event.parameter, &cached_output_mon))
continue;
snprintf(buf, MAX_CHAR_BUF_SIZE, "output_set %i %s %f", eventptr->event.parameter.effect_id,
eventptr->event.parameter.symbol,
eventptr->event.parameter.value);
socket_send_feedback(buf);
// save for fast checkup next time
cached_output_mon.last_effect_id = eventptr->event.parameter.effect_id;
strncpy(cached_output_mon.last_symbol, eventptr->event.parameter.symbol, MAX_CHAR_BUF_SIZE);
break;
case POSTPONED_MIDI_MAP:
if (eventptr->event.midi_map.effect_id == ignored_effect_id)
continue;
snprintf(buf, MAX_CHAR_BUF_SIZE, "midi_mapped %i %s %i %i %f %f %f", eventptr->event.midi_map.effect_id,
eventptr->event.midi_map.symbol,
eventptr->event.midi_map.channel,
eventptr->event.midi_map.controller,
eventptr->event.midi_map.value,
eventptr->event.midi_map.minimum,
eventptr->event.midi_map.maximum);
socket_send_feedback(buf);
break;
case POSTPONED_PROGRAM_LISTEN:
if (got_midi_program)
continue;
snprintf(buf, MAX_CHAR_BUF_SIZE, "midi_program %i", eventptr->event.program.value);
socket_send_feedback(buf);
// ignore older midi program changes
got_midi_program = true;
break;
case POSTPONED_TRANSPORT:
if (got_transport)
continue;
snprintf(buf, MAX_CHAR_BUF_SIZE, "transport %i %f %f", eventptr->event.transport.rolling ? 1 : 0,
eventptr->event.transport.bpb,
eventptr->event.transport.bpm);
socket_send_feedback(buf);
// ignore older transport changes
got_transport = true;
break;
}
}
// cleanup memory
list_for_each_safe(it, it2, &cached_param_set.symbols.siblings)
{
psymbol = list_entry(it, postponed_cached_symbol_list_data, siblings);
free(psymbol);
}
list_for_each_safe(it, it2, &cached_output_mon.symbols.siblings)
{
psymbol = list_entry(it, postponed_cached_symbol_list_data, siblings);
free(psymbol);
}
// cleanup memory of rt queue, safely
list_for_each_safe(it, it2, &queue)
{
eventptr = list_entry(it, postponed_event_list_data, siblings);
rtsafe_memory_pool_deallocate(g_rtsafe_mem_pool, eventptr);
}
if (g_postevents_ready)
{
// report data finished to server
g_postevents_ready = false;
socket_send_feedback("data_finish");
}
}
static void* PostPonedEventsThread(void* arg)
{
while (g_postevents_running == 1)
{
if (sem_timedwait_secs(&g_postevents_semaphore, 1) != 0)
continue;
if (g_postevents_running == 1 && g_postevents_ready)
RunPostPonedEvents(-3); // as all effects are valid we set ignored_effect_id to -3
}
return NULL;
UNUSED_PARAM(arg);
}
static int ProcessPlugin(jack_nframes_t nframes, void *arg)
{
effect_t *effect;
const float *buffer_in;
float *buffer_out;
unsigned int i;
if (arg == NULL) return 0;
effect = arg;
if (!g_processing_enabled)
{
port_t *port;
for (i = 0; i < effect->output_audio_ports_count; i++)
{
memset(jack_port_get_buffer(effect->output_audio_ports[i]->jack_port, nframes),
0, (sizeof(float) * nframes));
}
for (i = 0; i < effect->output_cv_ports_count; i++)
{
memset(jack_port_get_buffer(effect->output_cv_ports[i]->jack_port, nframes),
0, (sizeof(float) * nframes));
}
for (i = 0; i < effect->output_event_ports_count; i++)
{
port = effect->output_event_ports[i];
if (port->jack_port && port->flow == FLOW_OUTPUT && port->type == TYPE_EVENT)
jack_midi_clear_buffer(jack_port_get_buffer(port->jack_port, nframes));
}
return 0;
}
/* transport */
uint8_t pos_buf[256];
LV2_Atom* lv2_pos = (LV2_Atom*)pos_buf;
if (effect->hints & HINT_TRANSPORT)
{
jack_position_t pos;
const bool rolling = (jack_transport_query(effect->jack_client, &pos) == JackTransportRolling);
if ((pos.valid & JackPositionBBT) == 0)
{
pos.beats_per_bar = g_transport_bpb;
pos.beats_per_minute = g_transport_bpm;
}
if (effect->transport_rolling != rolling ||
effect->transport_frame != pos.frame ||
effect->transport_bpb != (double)pos.beats_per_bar ||
effect->transport_bpm != pos.beats_per_minute ||
(effect->bypass < 0.5f && effect->was_bypassed))
{
effect->transport_rolling = rolling;
effect->transport_frame = pos.frame;
effect->transport_bpb = pos.beats_per_bar;
effect->transport_bpm = pos.beats_per_minute;
LV2_Atom_Forge forge = g_lv2_atom_forge;
lv2_atom_forge_set_buffer(&forge, pos_buf, sizeof(pos_buf));
LV2_Atom_Forge_Frame frame;
lv2_atom_forge_object(&forge, &frame, 0, g_urids.time_Position);
lv2_atom_forge_key(&forge, g_urids.time_speed);
lv2_atom_forge_float(&forge, rolling ? 1.0f : 0.0f);
lv2_atom_forge_key(&forge, g_urids.time_frame);
lv2_atom_forge_long(&forge, pos.frame);
if (pos.valid & JackPositionBBT)
{
lv2_atom_forge_key(&forge, g_urids.time_bar);
lv2_atom_forge_long(&forge, pos.bar - 1);
lv2_atom_forge_key(&forge, g_urids.time_barBeat);
lv2_atom_forge_float(&forge, pos.beat - 1 + (pos.tick / pos.ticks_per_beat));
lv2_atom_forge_key(&forge, g_urids.time_beat);
lv2_atom_forge_double(&forge, pos.beat - 1);
lv2_atom_forge_key(&forge, g_urids.time_beatUnit);
lv2_atom_forge_int(&forge, pos.beat_type);
lv2_atom_forge_key(&forge, g_urids.time_beatsPerBar);
lv2_atom_forge_float(&forge, pos.beats_per_bar);
lv2_atom_forge_key(&forge, g_urids.time_beatsPerMinute);
lv2_atom_forge_float(&forge, pos.beats_per_minute);
lv2_atom_forge_key(&forge, g_urids.time_ticksPerBeat);
lv2_atom_forge_double(&forge, pos.ticks_per_beat);
}
lv2_atom_forge_pop(&forge, &frame);
}
else
{
lv2_pos->size = 0;
}
}
if (effect->bpb_index >= 0)
{
*(effect->ports[effect->bpb_index]->buffer) = g_transport_bpb;
}
if (effect->bpm_index >= 0)
{
*(effect->ports[effect->bpm_index]->buffer) = g_transport_bpm;
}
if (effect->speed_index >= 0)
{
*(effect->ports[effect->speed_index]->buffer) = g_jack_rolling ? 1.0f : 0.0f;
}
/* Prepare midi/event ports */
for (i = 0; i < effect->input_event_ports_count; i++)
{
lv2_evbuf_reset(effect->input_event_ports[i]->evbuf, true);
if (effect->bypass > 0.5f)
{
// effect is now bypassed, but wasn't before
if (!effect->was_bypassed)
{
LV2_Evbuf_Iterator iter = lv2_evbuf_begin(effect->input_event_ports[i]->evbuf);
uint8_t bufNotesOff[3] = {
0xB0, // CC
0x7B, // all-notes-off
0
};
uint8_t bufSoundOff[3] = {
0xB0, // CC
0x78, // all-sound-off
0
};
int j=0;
do {
if (!lv2_evbuf_write(&iter, 0, 0, g_urids.midi_MidiEvent, 3, bufNotesOff))
break;
if (!lv2_evbuf_write(&iter, 0, 0, g_urids.midi_MidiEvent, 3, bufSoundOff))
break;
bufNotesOff[0] += 1;
bufSoundOff[0] += 1;
} while (++j < 16);
}
}
else
{
// non-bypassed, processing
LV2_Evbuf_Iterator iter = lv2_evbuf_begin(effect->input_event_ports[i]->evbuf);
/* Write Jack MIDI input */
void* buf = jack_port_get_buffer(effect->input_event_ports[i]->jack_port, nframes);
uint32_t j;
for (j = 0; j < jack_midi_get_event_count(buf); ++j)
{
jack_midi_event_t ev;
jack_midi_event_get(&ev, buf, j);
if (!lv2_evbuf_write(&iter, ev.time, 0, g_urids.midi_MidiEvent, ev.size, ev.buffer))
{
fprintf(stderr, "lv2 evbuf write failed\n");
}
}
/* Write time position */
if (lv2_pos->size > 0 && (effect->input_event_ports[i]->hints & HINT_TRANSPORT) != 0)