-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathjackwrap.c
1886 lines (1631 loc) · 55.8 KB
/
jackwrap.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
/* x42 jack wrapper / minimal LV2 host
*
* Copyright (C) 2012-2019 Robin Gareus
*
* 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
* (at your option) 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.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef UPDATE_FREQ_RATIO
#define UPDATE_FREQ_RATIO 60 // MAX # of audio-cycles per GUI-refresh
#endif
#ifndef JACK_AUTOCONNECT
#define JACK_AUTOCONNECT 0
#endif
#ifndef UI_UPDATE_FPS
#define UI_UPDATE_FPS 25
#endif
#ifndef MAXDELAY
#define MAXDELAY 192001 // delayline max possible delay
#endif
#ifndef MAXPERIOD
#define MAXPERIOD 8192 // delayline - max period size (jack-period)
#endif
///////////////////////////////////////////////////////////////////////////////
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#ifdef WIN32
#include <pthread.h>
#include <windows.h>
#define pthread_t //< override jack.h def
#endif
#ifdef __APPLE__
#include <CoreFoundation/CoreFoundation.h>
extern void rtk_osx_api_init (void);
extern void rtk_osx_api_terminate (void);
extern void rtk_osx_api_run (void);
extern void rtk_osx_api_err (const char* msg);
#endif
#include <assert.h>
#include <errno.h>
#include <getopt.h>
#include <math.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#if (defined _WIN32 && defined RTK_STATIC_INIT)
#include <glib-object.h>
#endif
#ifndef _WIN32
#include <sys/mman.h>
#else
#include <sys/timeb.h>
#endif
#ifdef USE_WEAK_JACK
#include "weakjack/weak_libjack.h"
#else
#include <jack/jack.h>
#include <jack/midiport.h>
#include <jack/ringbuffer.h>
#endif
#undef pthread_t
#ifdef HAVE_LV2_1_18_6
#include <lv2/atom/atom.h>
#include <lv2/atom/forge.h>
#include <lv2/core/lv2.h>
#include <lv2/midi/midi.h>
#include <lv2/time/time.h>
#include <lv2/uri-map/uri-map.h>
#include <lv2/urid/urid.h>
#include <lv2/ui/ui.h>
#include <lv2/worker/worker.h>
#else
#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/time/time.h>
#include <lv2/lv2plug.in/ns/ext/uri-map/uri-map.h>
#include <lv2/lv2plug.in/ns/ext/urid/urid.h>
#include <lv2/lv2plug.in/ns/ext/worker/worker.h>
#include <lv2/lv2plug.in/ns/extensions/ui/ui.h>
#include <lv2/lv2plug.in/ns/lv2core/lv2.h>
#endif
#include "./gl/xternalui.h"
#ifndef WIN32
#include <pthread.h>
#include <signal.h>
#endif
#define LV2_EXTERNAL_UI_RUN(ptr) (ptr)->run (ptr)
#define LV2_EXTERNAL_UI_SHOW(ptr) (ptr)->show (ptr)
#define LV2_EXTERNAL_UI_HIDE(ptr) (ptr)->hide (ptr)
#define nan NAN
#ifndef UINT32_MAX
#define UINT32_MAX (4294967295U)
#endif
static const LV2_Descriptor* plugin_dsp;
static const LV2UI_Descriptor* plugin_gui;
static LV2_Handle plugin_instance = NULL;
static LV2UI_Handle gui_instance = NULL;
static float* plugin_ports_pre = NULL;
static float* plugin_ports_post = NULL;
static LV2_Atom_Sequence* atom_in = NULL;
static LV2_Atom_Sequence* atom_out = NULL;
static jack_port_t** input_port = NULL;
static jack_port_t** output_port = NULL;
static jack_port_t* midi_in = NULL;
static jack_port_t* midi_out = NULL;
static jack_client_t* j_client = NULL;
static uint32_t j_samplerate = 48000;
static int _freewheeling = 0;
struct transport_position {
jack_nframes_t position;
float bpm;
bool rolling;
} j_transport = { 0, 0, false };
static jack_ringbuffer_t* rb_ctrl_to_ui = NULL;
static jack_ringbuffer_t* rb_ctrl_from_ui = NULL;
static jack_ringbuffer_t* rb_atom_to_ui = NULL;
static jack_ringbuffer_t* rb_atom_from_ui = NULL;
#ifdef HAVE_LIBLO
#include <lo/lo.h>
lo_server_thread osc_server = NULL;
static jack_ringbuffer_t* rb_osc_to_ui = NULL;
typedef struct _osc_midi_event {
size_t size;
uint8_t buffer[3];
} osc_midi_event_t;
#ifndef OSC_MIDI_QUEUE_SIZE
#define OSC_MIDI_QUEUE_SIZE (256)
#endif
static osc_midi_event_t event_queue[OSC_MIDI_QUEUE_SIZE];
static int queued_events_start = 0;
static int queued_events_end = 0;
#endif
static pthread_mutex_t gui_thread_lock = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t data_ready = PTHREAD_COND_INITIALIZER;
static uint32_t uri_midi_MidiEvent = 0;
static uint32_t uri_atom_Sequence = 0;
static uint32_t uri_atom_EventTransfer = 0;
static uint32_t uri_time_Position = 0;
static uint32_t uri_time_frame = 0;
static uint32_t uri_time_speed = 0;
static uint32_t uri_time_bar = 0;
static uint32_t uri_time_barBeat = 0;
static uint32_t uri_time_beatUnit = 0;
static uint32_t uri_time_beatsPerBar = 0;
static uint32_t uri_time_beatsPerMinute = 0;
static char** urimap = NULL;
static uint32_t urimap_len = 0;
enum PortType {
CONTROL_IN = 0,
CONTROL_OUT,
AUDIO_IN,
AUDIO_OUT,
MIDI_IN,
MIDI_OUT,
ATOM_IN,
ATOM_OUT
};
struct DelayBuffer {
jack_latency_range_t port_latency;
int wanted_delay;
int c_dly; // current delay
int w_ptr;
int r_ptr;
float out_buffer[MAXPERIOD];
float delay_buffer[MAXDELAY];
};
struct PValue {
uint32_t port_idx;
float value;
};
struct LV2Port {
const char* name;
enum PortType porttype;
float val_default;
float val_min;
float val_max;
const char* doc;
};
typedef struct _RtkLv2Description {
const LV2_Descriptor* (*lv2_descriptor) (uint32_t index);
const LV2UI_Descriptor* (*lv2ui_descriptor) (uint32_t index);
const uint32_t dsp_descriptor_id;
const uint32_t gui_descriptor_id;
const char* plugin_human_id;
const struct LV2Port* ports;
const uint32_t nports_total;
const uint32_t nports_audio_in;
const uint32_t nports_audio_out;
const uint32_t nports_midi_in;
const uint32_t nports_midi_out;
const uint32_t nports_atom_in;
const uint32_t nports_atom_out;
const uint32_t nports_ctrl;
const uint32_t nports_ctrl_in;
const uint32_t nports_ctrl_out;
const uint32_t min_atom_bufsiz;
const bool send_time_info;
const uint32_t latency_ctrl_port;
} RtkLv2Description;
static RtkLv2Description const* inst;
/* a simple state machine for this client */
static volatile enum {
Run,
Exit
} client_state = Run;
static struct lv2_external_ui_host extui_host;
static struct lv2_external_ui* extui = NULL;
static LV2UI_Controller controller = NULL;
static LV2_Atom_Forge lv2_forge;
static uint32_t* portmap_a_in;
static uint32_t* portmap_a_out;
static uint32_t* portmap_rctl;
static uint32_t* portmap_ctrl;
static uint32_t portmap_atom_to_ui = -1;
static uint32_t portmap_atom_from_ui = -1;
static uint32_t uri_to_id (LV2_URID_Map_Handle handle, const char* uri);
static jack_ringbuffer_t* worker_requests = NULL;
static jack_ringbuffer_t* worker_responses = NULL;
static pthread_t worker_thread;
static pthread_mutex_t worker_lock = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t worker_ready = PTHREAD_COND_INITIALIZER;
static LV2_Worker_Interface* worker_iface = NULL;
static pthread_mutex_t port_write_lock = PTHREAD_MUTEX_INITIALIZER;
static struct DelayBuffer** delayline = NULL;
static uint32_t worst_capture_latency = 0;
static uint32_t plugin_latency = 0;
/******************************************************************************
* Delayline for latency compensation
*/
#define FADE_LEN (16)
#define INCREMENT_PTRS \
dly->r_ptr = (dly->r_ptr + 1) % MAXDELAY; \
dly->w_ptr = (dly->w_ptr + 1) % MAXDELAY;
static float*
delay_port (struct DelayBuffer* dly, uint32_t n_samples, float* in)
{
uint32_t pos = 0;
const int delay = dly->wanted_delay;
const float* const input = in;
float* const output = dly->out_buffer;
if (dly->c_dly == delay && delay == 0) {
// only copy data into buffer in case delay time changes
for (; pos < n_samples; pos++) {
dly->delay_buffer[dly->w_ptr] = input[pos];
INCREMENT_PTRS;
}
return in;
}
// fade if delaytime changes
if (dly->c_dly != delay) {
const uint32_t fade_len = (n_samples >= FADE_LEN) ? FADE_LEN : n_samples / 2;
// fade out
for (; pos < fade_len; pos++) {
const float gain = (float)(fade_len - pos) / (float)fade_len;
dly->delay_buffer[dly->w_ptr] = input[pos];
output[pos] = dly->delay_buffer[dly->r_ptr] * gain;
INCREMENT_PTRS;
}
// update read pointer
dly->r_ptr += dly->c_dly - delay;
if (dly->r_ptr < 0) {
dly->r_ptr -= MAXDELAY * floor (dly->r_ptr / (float)MAXDELAY);
}
dly->r_ptr = dly->r_ptr % MAXDELAY;
dly->c_dly = delay;
// fade in
for (; pos < 2 * fade_len; pos++) {
const float gain = (float)(pos - fade_len) / (float)fade_len;
dly->delay_buffer[dly->w_ptr] = input[pos];
output[pos] = dly->delay_buffer[dly->r_ptr] * gain;
INCREMENT_PTRS;
}
}
for (; pos < n_samples; pos++) {
dly->delay_buffer[dly->w_ptr] = input[pos];
output[pos] = dly->delay_buffer[dly->r_ptr];
INCREMENT_PTRS;
}
return dly->out_buffer;
}
///////////////////////////
// GET INFO FROM LV2 TTL //
// see lv2ttl2c //
// define _plugin //
///////////////////////////
#include JACK_DESCRIPT ////
///////////////////////////
/******************************************************************************
* JACK
*/
static int
process (jack_nframes_t nframes, void* arg)
{
if (nframes > MAXPERIOD) {
static bool warned_max_period = false;
if (!warned_max_period) {
warned_max_period = true;
fprintf (stderr, "Jack Period Size > %d is not supported (current %d)\n", MAXPERIOD, nframes);
}
if (inst->nports_midi_out > 0) {
void* buf = jack_port_get_buffer (midi_out, nframes);
jack_midi_clear_buffer (buf);
}
for (uint32_t i = 0; i < inst->nports_audio_out; ++i) {
float* bp = (float*)jack_port_get_buffer (output_port[i], nframes);
memset (bp, 0, nframes * sizeof (float));
}
return 0;
}
while (jack_ringbuffer_read_space (rb_ctrl_from_ui) >= sizeof (uint32_t) + sizeof (float)) {
uint32_t idx;
jack_ringbuffer_read (rb_ctrl_from_ui, (char*)&idx, sizeof (uint32_t));
jack_ringbuffer_read (rb_ctrl_from_ui, (char*)&(plugin_ports_pre[idx]), sizeof (float));
}
/* Get Jack transport position */
jack_position_t pos;
const bool rolling = (jack_transport_query (j_client, &pos) == JackTransportRolling);
const bool transport_changed = (rolling != j_transport.rolling || pos.frame != j_transport.position || ((pos.valid & JackPositionBBT) && (pos.beats_per_minute != j_transport.bpm)));
/* atom buffers */
if (inst->nports_atom_in > 0 || inst->nports_midi_in > 0) {
/* start Atom sequence */
atom_in->atom.type = uri_atom_Sequence;
atom_in->atom.size = 8;
LV2_Atom_Sequence_Body* body = &atom_in->body;
body->unit = 0; // URID of unit of event time stamp LV2_ATOM__timeUnit ??
body->pad = 0; // unused
uint8_t* seq = (uint8_t*)(body + 1);
if (transport_changed && inst->send_time_info) {
uint8_t pos_buf[256];
LV2_Atom* lv2_pos = (LV2_Atom*)pos_buf;
lv2_atom_forge_set_buffer (&lv2_forge, pos_buf, sizeof (pos_buf));
LV2_Atom_Forge* forge = &lv2_forge;
LV2_Atom_Forge_Frame frame;
#ifdef HAVE_LV2_1_8
lv2_atom_forge_object (&lv2_forge, &frame, 1, uri_time_Position);
#else
lv2_atom_forge_blank (&lv2_forge, &frame, 1, uri_time_Position);
#endif
lv2_atom_forge_property_head (forge, uri_time_frame, 0);
lv2_atom_forge_long (forge, pos.frame);
lv2_atom_forge_property_head (forge, uri_time_speed, 0);
lv2_atom_forge_float (forge, rolling ? 1.0 : 0.0);
if (pos.valid & JackPositionBBT) {
lv2_atom_forge_property_head (forge, uri_time_barBeat, 0);
lv2_atom_forge_float (forge, pos.beat - 1 + (pos.tick / pos.ticks_per_beat));
lv2_atom_forge_property_head (forge, uri_time_bar, 0);
lv2_atom_forge_long (forge, pos.bar - 1);
lv2_atom_forge_property_head (forge, uri_time_beatUnit, 0);
lv2_atom_forge_int (forge, pos.beat_type);
lv2_atom_forge_property_head (forge, uri_time_beatsPerBar, 0);
lv2_atom_forge_float (forge, pos.beats_per_bar);
lv2_atom_forge_property_head (forge, uri_time_beatsPerMinute, 0);
lv2_atom_forge_float (forge, pos.beats_per_minute);
}
uint32_t size = lv2_pos->size;
uint32_t padded_size = ((sizeof (LV2_Atom_Event) + size) + 7) & (~7);
if (inst->min_atom_bufsiz > padded_size) {
//printf("send time..\n");
LV2_Atom_Event* aev = (LV2_Atom_Event*)seq;
aev->time.frames = 0;
aev->body.size = size;
aev->body.type = lv2_pos->type;
memcpy (LV2_ATOM_BODY (&aev->body), LV2_ATOM_BODY (lv2_pos), size);
atom_in->atom.size += padded_size;
seq += padded_size;
}
}
if (gui_instance) {
while (jack_ringbuffer_read_space (rb_atom_from_ui) > sizeof (LV2_Atom)) {
LV2_Atom a;
jack_ringbuffer_read (rb_atom_from_ui, (char*)&a, sizeof (LV2_Atom));
uint32_t padded_size = atom_in->atom.size + a.size + sizeof (int64_t);
if (inst->min_atom_bufsiz > padded_size) {
memset (seq, 0, sizeof (int64_t)); // LV2_Atom_Event->time
seq += sizeof (int64_t);
jack_ringbuffer_read (rb_atom_from_ui, (char*)seq, a.size);
seq += a.size;
atom_in->atom.size += a.size + sizeof (int64_t);
}
}
}
if (inst->nports_midi_in > 0) {
#ifdef HAVE_LIBLO
/*inject OSC midi events, use time 0 */
while (queued_events_end != queued_events_start) {
uint32_t size = event_queue[queued_events_end].size;
uint32_t padded_size = ((sizeof (LV2_Atom_Event) + size) + 7) & (~7);
if (inst->min_atom_bufsiz > padded_size) {
LV2_Atom_Event* aev = (LV2_Atom_Event*)seq;
aev->time.frames = 0; // time
aev->body.size = size;
aev->body.type = uri_midi_MidiEvent;
memcpy (LV2_ATOM_BODY (&aev->body), event_queue[queued_events_end].buffer, size);
atom_in->atom.size += padded_size;
seq += padded_size;
}
queued_events_end = (queued_events_end + 1) % OSC_MIDI_QUEUE_SIZE;
}
#endif
/* inject jack midi events */
void* buf = jack_port_get_buffer (midi_in, nframes);
for (uint32_t i = 0; i < jack_midi_get_event_count (buf); ++i) {
jack_midi_event_t ev;
jack_midi_event_get (&ev, buf, i);
uint32_t size = ev.size;
uint32_t padded_size = ((sizeof (LV2_Atom_Event) + size) + 7) & (~7);
if (inst->min_atom_bufsiz > padded_size) {
LV2_Atom_Event* aev = (LV2_Atom_Event*)seq;
aev->time.frames = ev.time;
aev->body.size = size;
aev->body.type = uri_midi_MidiEvent;
memcpy (LV2_ATOM_BODY (&aev->body), ev.buffer, size);
atom_in->atom.size += padded_size;
seq += padded_size;
}
}
}
}
if (inst->nports_atom_out > 0 || inst->nports_midi_out > 0) {
atom_out->atom.type = 0;
atom_out->atom.size = inst->min_atom_bufsiz;
}
/* make a backup copy, to see what was changed */
memcpy (plugin_ports_post, plugin_ports_pre, inst->nports_ctrl * sizeof (float));
/* expected transport state in next cycle */
j_transport.position = rolling ? pos.frame + nframes : pos.frame;
j_transport.bpm = pos.beats_per_minute;
j_transport.rolling = rolling;
/* [re] connect jack audio buffers */
for (uint32_t i = 0; i < inst->nports_audio_out; i++) {
plugin_dsp->connect_port (plugin_instance, portmap_a_out[i], jack_port_get_buffer (output_port[i], nframes));
}
for (uint32_t i = 0; i < inst->nports_audio_in; i++) {
delayline[i]->wanted_delay = worst_capture_latency - delayline[i]->port_latency.max;
plugin_dsp->connect_port (
plugin_instance, portmap_a_in[i],
delay_port (delayline[i], nframes, (float*)jack_port_get_buffer (input_port[i], nframes)));
}
/* run the plugin */
plugin_dsp->run (plugin_instance, nframes);
/* handle worker emit response - may amend Atom seq... */
if (worker_responses) {
uint32_t read_space = jack_ringbuffer_read_space (worker_responses);
while (read_space) {
uint32_t size = 0;
char worker_response[4096];
jack_ringbuffer_read (worker_responses, (char*)&size, sizeof (size));
jack_ringbuffer_read (worker_responses, worker_response, size);
worker_iface->work_response (plugin_instance, size, worker_response);
read_space -= sizeof (size) + size;
}
}
/* create port-events for change values */
if (gui_instance) {
for (uint32_t p = 0; p < inst->nports_ctrl; p++) {
if (inst->ports[portmap_rctl[p]].porttype != CONTROL_OUT)
continue;
if (plugin_ports_pre[p] != plugin_ports_post[p]) {
if (inst->latency_ctrl_port != UINT32_MAX && p == portmap_ctrl[inst->latency_ctrl_port]) {
plugin_latency = rintf (plugin_ports_pre[p]);
// TODO handle case if there's no GUI thread to call
// jack_recompute_total_latencies()
}
if (jack_ringbuffer_write_space (rb_ctrl_to_ui) >= sizeof (uint32_t) + sizeof (float)) {
jack_ringbuffer_write (rb_ctrl_to_ui, (char*)&portmap_rctl[p], sizeof (uint32_t));
jack_ringbuffer_write (rb_ctrl_to_ui, (char*)&plugin_ports_pre[p], sizeof (float));
}
}
}
}
if (inst->nports_midi_out > 0) {
void* buf = jack_port_get_buffer (midi_out, nframes);
jack_midi_clear_buffer (buf);
}
/* Atom sequence port-events */
if (inst->nports_atom_out + inst->nports_midi_out > 0 && atom_out->atom.size > sizeof (LV2_Atom)) {
if (gui_instance && jack_ringbuffer_write_space (rb_atom_to_ui) >= atom_out->atom.size + 2 * sizeof (LV2_Atom)) {
LV2_Atom a = { atom_out->atom.size + (uint32_t)sizeof (LV2_Atom), 0 };
jack_ringbuffer_write (rb_atom_to_ui, (char*)&a, sizeof (LV2_Atom));
jack_ringbuffer_write (rb_atom_to_ui, (char*)atom_out, a.size);
}
if (inst->nports_midi_out) {
void* buf = jack_port_get_buffer (midi_out, nframes);
LV2_Atom_Event const* ev = (LV2_Atom_Event const*)((&(atom_out)->body) + 1); // lv2_atom_sequence_begin
while ((const uint8_t*)ev < ((const uint8_t*)&(atom_out)->body + (atom_out)->atom.size)) {
if (ev->body.type == uri_midi_MidiEvent) {
jack_midi_event_write (buf, ev->time.frames, (const uint8_t*)(ev + 1), ev->body.size);
}
ev = (LV2_Atom_Event const*)/* lv2_atom_sequence_next() */
((const uint8_t*)ev + sizeof (LV2_Atom_Event) + ((ev->body.size + 7) & ~7));
}
}
}
/* signal worker end of process run */
if (worker_iface && worker_iface->end_run) {
worker_iface->end_run (plugin_instance);
}
/* wake up UI */
if (gui_instance && (
jack_ringbuffer_read_space (rb_ctrl_to_ui) >= sizeof (uint32_t) + sizeof (float)
|| jack_ringbuffer_read_space (rb_atom_to_ui) > sizeof (LV2_Atom)
#ifdef HAVE_LIBLO
|| jack_ringbuffer_read_space (rb_osc_to_ui) >= sizeof (uint32_t) + sizeof (float)
#endif
)
) {
if (pthread_mutex_trylock (&gui_thread_lock) == 0) {
pthread_cond_signal (&data_ready);
pthread_mutex_unlock (&gui_thread_lock);
}
}
return 0;
}
static void
jack_shutdown (void* arg)
{
fprintf (stderr, "recv. shutdown request from jackd.\n");
client_state = Exit;
pthread_cond_signal (&data_ready);
}
static int
jack_graph_order_cb (void* arg)
{
worst_capture_latency = 0;
for (uint32_t i = 0; i < inst->nports_audio_in; i++) {
jack_port_get_latency_range (input_port[i], JackCaptureLatency, &(delayline[i]->port_latency));
if (delayline[i]->port_latency.max > worst_capture_latency) {
worst_capture_latency = delayline[i]->port_latency.max;
}
}
return 0;
}
static void
jack_latency_cb (jack_latency_callback_mode_t mode, void* arg)
{
// assume 1 -> 1 map
jack_graph_order_cb (NULL); // update worst-case latency, delayline alignment
if (mode == JackCaptureLatency) {
for (uint32_t i = 0; i < inst->nports_audio_out; i++) {
jack_latency_range_t r;
if (i < inst->nports_audio_in) {
const uint32_t port_delay = worst_capture_latency - delayline[i]->port_latency.max;
jack_port_get_latency_range (input_port[i], JackCaptureLatency, &r);
r.min += port_delay;
r.max += port_delay;
} else {
r.min = r.max = 0;
}
r.min += plugin_latency;
r.max += plugin_latency;
jack_port_set_latency_range (output_port[i], JackCaptureLatency, &r);
}
} else { // JackPlaybackLatency
for (uint32_t i = 0; i < inst->nports_audio_in; i++) {
const uint32_t port_delay = worst_capture_latency - delayline[i]->port_latency.max;
jack_latency_range_t r;
if (i < inst->nports_audio_out) {
jack_port_get_latency_range (output_port[i], JackPlaybackLatency, &r);
} else {
r.min = r.max = 0;
}
r.min += port_delay + plugin_latency;
r.max += port_delay + plugin_latency;
jack_port_set_latency_range (input_port[i], JackPlaybackLatency, &r);
}
}
}
static void
jack_freewheel_cb (int onoff, void* arg)
{
_freewheeling = onoff;
}
static int
init_jack (const char* client_name)
{
jack_status_t status;
char* cn = strdup (client_name);
if (strlen (cn) >= (unsigned int)jack_client_name_size () - 1) {
cn[jack_client_name_size () - 1] = '\0';
}
j_client = jack_client_open (cn, JackNoStartServer, &status);
free (cn);
if (j_client == NULL) {
fprintf (stderr, "jack_client_open() failed, status = 0x%2.0x\n", status);
if (status & JackServerFailed) {
fprintf (stderr, "Unable to connect to JACK server\n");
}
return (-1);
}
if (status & JackServerStarted) {
fprintf (stderr, "JACK server started\n");
}
if (status & JackNameNotUnique) {
client_name = jack_get_client_name (j_client);
fprintf (stderr, "jack-client name: `%s'\n", client_name);
}
jack_set_process_callback (j_client, process, 0);
jack_set_graph_order_callback (j_client, jack_graph_order_cb, 0);
jack_set_latency_callback (j_client, jack_latency_cb, 0);
jack_set_freewheel_callback (j_client, jack_freewheel_cb, 0);
#ifndef WIN32
jack_on_shutdown (j_client, jack_shutdown, NULL);
#endif
j_samplerate = jack_get_sample_rate (j_client);
return (0);
}
static int
jack_portsetup (void)
{
/* Allocate data structures that depend on the number of ports. */
if (inst->nports_audio_in > 0) {
input_port = (jack_port_t**)malloc (sizeof (jack_port_t*) * inst->nports_audio_in);
delayline = (struct DelayBuffer**)calloc (inst->nports_audio_in, sizeof (struct DelayBuffer*));
}
for (uint32_t i = 0; i < inst->nports_audio_in; i++) {
if ((input_port[i] = jack_port_register (j_client,
inst->ports[portmap_a_in[i]].name,
JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0)) == 0) {
fprintf (stderr, "cannot register input port \"%s\"!\n", inst->ports[portmap_a_in[i]].name);
return (-1);
}
delayline[i] = (struct DelayBuffer*)calloc (1, sizeof (struct DelayBuffer));
}
if (inst->nports_audio_out > 0) {
output_port = (jack_port_t**)malloc (sizeof (jack_port_t*) * inst->nports_audio_out);
}
for (uint32_t i = 0; i < inst->nports_audio_out; i++) {
if ((output_port[i] = jack_port_register (j_client,
inst->ports[portmap_a_out[i]].name,
JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0)) == 0) {
fprintf (stderr, "cannot register output port \"%s\"!\n", inst->ports[portmap_a_out[i]].name);
return (-1);
}
}
if (inst->nports_midi_in) {
if ((midi_in = jack_port_register (j_client,
inst->ports[portmap_atom_from_ui].name,
JACK_DEFAULT_MIDI_TYPE, JackPortIsInput, 0)) == 0) {
fprintf (stderr, "cannot register midi input port \"%s\"!\n", inst->ports[portmap_atom_from_ui].name);
return (-1);
}
}
if (inst->nports_midi_out) {
if ((midi_out = jack_port_register (j_client,
inst->ports[portmap_atom_to_ui].name,
JACK_DEFAULT_MIDI_TYPE, JackPortIsOutput, 0)) == 0) {
fprintf (stderr, "cannot register midi output port \"%s\"!\n", inst->ports[portmap_atom_to_ui].name);
return (-1);
}
}
jack_graph_order_cb (NULL); // query port latencies
jack_recompute_total_latencies (j_client);
return (0);
}
static void
jack_portconnect (int which)
{
if (which & 1) { // connect audio input(s)
const char** ports = jack_get_ports (j_client, NULL, JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput | JackPortIsPhysical);
for (uint32_t i = 0; i < inst->nports_audio_in && ports && ports[i]; i++) {
if (jack_connect (j_client, ports[i], jack_port_name (input_port[i])))
break;
}
if (ports) {
jack_free (ports);
}
}
if (which & 2) { // connect audio outputs(s)
const char** ports = jack_get_ports (j_client, NULL, JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput | JackPortIsPhysical);
for (uint32_t i = 0; i < inst->nports_audio_out && ports && ports[i]; i++) {
if (jack_connect (j_client, jack_port_name (output_port[i]), ports[i]))
break;
}
if (ports) {
jack_free (ports);
}
}
if ((which & 4) && midi_in) { // midi in
const char** ports = jack_get_ports (j_client, NULL, JACK_DEFAULT_MIDI_TYPE, JackPortIsOutput | JackPortIsPhysical);
if (ports && ports[0]) {
jack_connect (j_client, ports[0], jack_port_name (midi_in));
}
if (ports) {
jack_free (ports);
}
}
if ((which & 8) && midi_out) { // midi out
const char** ports = jack_get_ports (j_client, NULL, JACK_DEFAULT_MIDI_TYPE, JackPortIsInput | JackPortIsPhysical);
if (ports && ports[0]) {
jack_connect (j_client, jack_port_name (midi_out), ports[0]);
}
if (ports) {
jack_free (ports);
}
}
}
/******************************************************************************
* LV2
*/
static uint32_t
uri_to_id (LV2_URID_Map_Handle handle, const char* uri)
{
for (uint32_t i = 0; i < urimap_len; ++i) {
if (!strcmp (urimap[i], uri)) {
//printf("Found mapped URI '%s' -> %d\n", uri, i + 1);
return i + 1;
}
}
//printf("map URI '%s' -> %d\n", uri, urimap_len + 1);
urimap = (char**)realloc (urimap, (urimap_len + 1) * sizeof (char*));
urimap[urimap_len] = strdup (uri);
return ++urimap_len;
}
static void
free_uri_map ()
{
for (uint32_t i = 0; i < urimap_len; ++i) {
free (urimap[i]);
}
free (urimap);
}
static void
write_function (
LV2UI_Controller controller,
uint32_t port_index,
uint32_t buffer_size,
uint32_t port_protocol,
const void* buffer)
{
if (buffer_size == 0)
return;
if (port_protocol != 0) {
if (jack_ringbuffer_write_space (rb_atom_from_ui) >= buffer_size + sizeof (LV2_Atom)) {
LV2_Atom a = { buffer_size, 0 };
jack_ringbuffer_write (rb_atom_from_ui, (char*)&a, sizeof (LV2_Atom));
jack_ringbuffer_write (rb_atom_from_ui, (char*)buffer, buffer_size);
}
return;
}
if (buffer_size != sizeof (float)) {
fprintf (stderr, "LV2Host: write_function() unsupported buffer\n");
return;
}
if (port_index >= inst->nports_total) {
fprintf (stderr, "LV2Host: write_function() invalid port\n");
return;
}
if (portmap_ctrl[port_index] == UINT32_MAX) {
fprintf (stderr, "LV2Host: write_function() unmapped port\n");
return;
}
if (inst->ports[port_index].porttype != CONTROL_IN) {
fprintf (stderr, "LV2Host: write_function() not a control input\n");
return;
}
if (jack_ringbuffer_write_space (rb_ctrl_from_ui) >= sizeof (uint32_t) + sizeof (float)) {
pthread_mutex_lock (&port_write_lock);
jack_ringbuffer_write (rb_ctrl_from_ui, (char*)&portmap_ctrl[port_index], sizeof (uint32_t));
jack_ringbuffer_write (rb_ctrl_from_ui, (char*)buffer, sizeof (float));
pthread_mutex_unlock (&port_write_lock);
}
}
// LV2 Worker
static LV2_Worker_Status
lv2_worker_respond (LV2_Worker_Respond_Handle unused,
uint32_t size,
const void* data)
{
jack_ringbuffer_write (worker_responses, (const char*)&size, sizeof (size));
jack_ringbuffer_write (worker_responses, (const char*)data, size);
return LV2_WORKER_SUCCESS;
}
static void*
worker_func (void* data)
{
pthread_mutex_lock (&worker_lock);
while (1) {
char buf[4096];
uint32_t size = 0;
if (jack_ringbuffer_read_space (worker_requests) <= sizeof (size)) {
pthread_cond_wait (&worker_ready, &worker_lock);
}
if (client_state == Exit)
break;
jack_ringbuffer_read (worker_requests, (char*)&size, sizeof (size));
if (size > 4096) {
fprintf (stderr, "Worker information is too large. Abort.\n");
break;
}
jack_ringbuffer_read (worker_requests, buf, size);
worker_iface->work (plugin_instance, lv2_worker_respond, NULL, size, buf);
}
pthread_mutex_unlock (&worker_lock);
return NULL;
}
static void
worker_init ()
{
worker_requests = jack_ringbuffer_create (4096);
worker_responses = jack_ringbuffer_create (4096);
jack_ringbuffer_mlock (worker_requests);
jack_ringbuffer_mlock (worker_responses);
pthread_create (&worker_thread, NULL, worker_func, NULL);
}
static LV2_Worker_Status
lv2_worker_schedule (LV2_Worker_Schedule_Handle unused,
uint32_t size,
const void* data)
{
if (_freewheeling) {
worker_iface->work (plugin_instance, lv2_worker_respond, NULL, size, data);
return LV2_WORKER_SUCCESS;
}
assert (worker_requests);
jack_ringbuffer_write (worker_requests, (const char*)&size, sizeof (size));
jack_ringbuffer_write (worker_requests, (const char*)data, size);
if (pthread_mutex_trylock (&worker_lock) == 0) {
pthread_cond_signal (&worker_ready);
pthread_mutex_unlock (&worker_lock);
}
return LV2_WORKER_SUCCESS;
}
/******************************************************************************
* OSC
*/
#ifdef HAVE_LIBLO
static void
osc_queue_midi_event (osc_midi_event_t* ev)
{
if (((queued_events_start + 1) % OSC_MIDI_QUEUE_SIZE) == queued_events_end) {
return;
}
memcpy (&event_queue[queued_events_start], ev, sizeof (osc_midi_event_t));
queued_events_start = (queued_events_start + 1) % OSC_MIDI_QUEUE_SIZE;
}
static void
oscb_error (int num, const char* m, const char* path)
{
fprintf (stderr, "liblo server error %d in path %s: %s\n", num, path, m);
}
#define MIDI_Q3(STATUS) \
osc_midi_event_t ev; \
ev.size = 3; \
ev.buffer[0] = STATUS | (argv[0]->i & 0x0f); \
ev.buffer[1] = argv[1]->i & 0x7f; \
ev.buffer[2] = argv[2]->i & 0x7f; \
osc_queue_midi_event (&ev);
static int
oscb_noteon (const char* path, const char* types, lo_arg** argv, int argc, lo_message msg, void* user_data)
{