File: jackwrap.c

package info (click to toggle)
x42-plugins 20161230-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 14,560 kB
  • ctags: 8,337
  • sloc: ansic: 93,211; cpp: 5,743; makefile: 3,113; objc: 948; sh: 75
file content (1789 lines) | stat: -rw-r--r-- 54,856 bytes parent folder | download | duplicates (3)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
/* x42 jack wrapper / minimal LV2 host
 *
 * Copyright (C) 2012-2014 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 <windows.h>
#include <pthread.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 <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <math.h>
#include <time.h>
#include <getopt.h>
#include <assert.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/ringbuffer.h>
#include <jack/midiport.h>
#endif
#undef pthread_t

#include "lv2/lv2plug.in/ns/lv2core/lv2.h"
#include "lv2/lv2plug.in/ns/extensions/ui/ui.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/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/worker/worker.h"

#include "./gl/xternalui.h"

#ifndef WIN32
#include <signal.h>
#include <pthread.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_URI_Map_Callback_Data callback_data, 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);
		}

		//printf("Delay changed %d -> %d\n", dly->c_dly, delay); // DEBUG
		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
	// TODO add systemic latency of plugin (currently no robtk plugins add latency)
	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;
	j_client = jack_client_open (client_name, JackNoStartServer, &status);
	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, jack_port_name (input_port[i]), ports[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, jack_port_name (midi_in), ports[0]);
		}
		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_URI_Map_Callback_Data callback_data, 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) {
	MIDI_Q3(0x90);
	return 0;
}

static int oscb_noteoff (const char *path, const char *types, lo_arg **argv, int argc, lo_message msg, void *user_data) {
	MIDI_Q3(0x80);
	return 0;
}

static int oscb_cc (const char *path, const char *types, lo_arg **argv, int argc, lo_message msg, void *user_data) {
	MIDI_Q3(0xb0);
	return 0;
}

static int oscb_pc (const char *path, const char *types, lo_arg **argv, int argc, lo_message msg, void *user_data) {
	osc_midi_event_t ev;
	ev.size = 2;
	ev.buffer[0] = 0xc0 | (argv[0]->i & 0x0f);
	ev.buffer[1] = argv[1]->i & 0x7f;
	ev.buffer[2] = 0x00;
	osc_queue_midi_event (&ev);
	return 0;
}

static int oscb_rawmidi (const char *path, const char *types, lo_arg **argv, int argc, lo_message msg, void *user_data) {
	osc_midi_event_t ev;
	ev.size = 3;
	ev.buffer[0] = argv[0]->m[1];
	ev.buffer[1] = argv[0]->m[2] & 0x7f;
	ev.buffer[2] = argv[0]->m[3] & 0x7f;
	osc_queue_midi_event (&ev);
	return 0;
}

static int oscb_parameter (const char *path, const char *types, lo_arg **argv, int argc, lo_message msg, void *user_data) {
	assert (argc == 2 && !strcmp (types, "if"));

	const uint32_t port = argv[0]->i;
	const float val = argv[1]->f;

	if (inst->nports_ctrl <= port) {
		fprintf (stderr, "OSC: Invalid Parameter 0 <= %d < %d\n", port, inst->nports_ctrl);
		return 0;
	}

	const uint32_t port_index = portmap_rctl[port];

	if (inst->ports[port_index].porttype != CONTROL_IN) {
		fprintf (stderr, "OSC: mapped port (%d) is not a control input.\n", port_index);
		return 0;
	}

	if (inst->ports[port_index].val_min < inst->ports[port_index].val_max) {
		if (val < inst->ports[port_index].val_min || val > inst->ports[port_index].val_max) {
			fprintf (stderr, "OSC: Value out of bounds %f <= %f <= %f\n",
					inst->ports[port_index].val_min, val, inst->ports[port_index].val_max);
			return 0;
		}
	}

	//fprintf (stdout, "OSC: %d,  %d -> %f\n", port, port_index, val);

	write_function (NULL, port_index, sizeof (float), 0, (const void*) &val);

	if (jack_ringbuffer_write_space (rb_osc_to_ui) >= sizeof(uint32_t) + sizeof(float)) {
		jack_ringbuffer_write (rb_osc_to_ui, (char *) &port_index, sizeof(uint32_t));
		jack_ringbuffer_write (rb_osc_to_ui, (char *) &val, sizeof(float));
	}

	return 0;
}

struct osc_command {
	const char *path;
	const char *typespec;
	lo_method_handler handler;
	const char *documentation;
};

static struct osc_command OSCC[] = {
	{"/x42/parameter",    "if",  &oscb_parameter, "Set Control Input value. control-port, value"},
	{"/x42/midi/raw",     "m",   &oscb_rawmidi,   "Send raw midi message to plugin"},
	{"/x42/midi/cc",      "iii", &oscb_cc,        "Send midi control-change: channel, parameter, value"},
	{"/x42/midi/pc",      "ii",  &oscb_pc,        "Send midi program-change: channel, program"},
	{"/x42/midi/noteon",  "iii", &oscb_noteon,    "Send midi note on: channel, key, velocity"},
	{"/x42/midi/noteoff", "iii", &oscb_noteoff,   "Send midi note off: channel, key, velocity"},
};

static void print_oscdoc (void) {
	printf("# X42 OSC methods. Format:\n");
	printf("# Path <space> Typespec <space> Documentation <newline>\n");
	printf("#######################################################\n");
	for (size_t i = 0; i < sizeof(OSCC) / sizeof(struct osc_command); ++i) {
		printf("%s %s %s\n", OSCC[i].path, OSCC[i].typespec, OSCC[i].documentation);
	}
}

static int start_osc_server (int osc_port) {
	char tmp[8];
	uint32_t port = (osc_port > 100 && osc_port < 60000) ? osc_port : 9988;

#ifdef _WIN32
	sprintf(tmp, "%d", port);
#else
	snprintf(tmp, sizeof(tmp), "%d", port);
#endif
	osc_server = lo_server_thread_new (tmp, oscb_error);

	if (!osc_server) {
		fprintf(stderr, "OSC failed to listen on port %s", tmp);
		return -1;
	} else {
		char *urlstr = lo_server_thread_get_url (osc_server);
		fprintf(stderr, "OSC server: %s\n", urlstr);
		free (urlstr);
	}

	for (size_t i = 0; i < sizeof(OSCC) / sizeof(struct osc_command); ++i) {
		lo_server_thread_add_method (osc_server, OSCC[i].path, OSCC[i].typespec, OSCC[i].handler, NULL);
	}

	lo_server_thread_start(osc_server);

	return 0;
}

static void stop_osc_server () {
	if (!osc_server) return;
	lo_server_thread_stop (osc_server);
	lo_server_thread_free (osc_server);
	fprintf(stderr, "OSC server shut down.\n");
	osc_server = NULL;
}
#endif


/******************************************************************************
 * MAIN
 */

static void cleanup(int sig) {
	if (j_client) {
		jack_client_close (j_client);
		j_client=NULL;
	}

	if (worker_requests) {
		pthread_mutex_lock (&worker_lock);
		pthread_cond_signal (&worker_ready);
		pthread_mutex_unlock (&worker_lock);

		pthread_join(worker_thread, NULL);

		jack_ringbuffer_free(worker_requests);
		jack_ringbuffer_free(worker_responses);
	}

#ifdef HAVE_LIBLO
	stop_osc_server ();
#endif

	if (plugin_dsp && plugin_instance && plugin_dsp->deactivate) {
		plugin_dsp->deactivate(plugin_instance);
	}
	if (plugin_gui && gui_instance && plugin_gui->cleanup) {
		plugin_gui->cleanup(gui_instance);
	}
	if (plugin_dsp && plugin_instance && plugin_dsp->cleanup) {
		plugin_dsp->cleanup(plugin_instance);
	}

	jack_ringbuffer_free(rb_ctrl_to_ui);
	jack_ringbuffer_free(rb_ctrl_from_ui);

	jack_ringbuffer_free(rb_atom_to_ui);
	jack_ringbuffer_free(rb_atom_from_ui);

#ifdef HAVE_LIBLO
	jack_ringbuffer_free(rb_osc_to_ui);
#endif

	free(input_port);
	free(output_port);

	if (delayline) {
		for (uint32_t i = 0; i < inst->nports_audio_in; i++) {
			free(delayline[i]);
		}
	}
	free(delayline);

	free(plugin_ports_pre);
	free(plugin_ports_post);
	free(portmap_a_in);
	free(portmap_a_out);
	free(portmap_ctrl);
	free(portmap_rctl);
	free(atom_in);
	free(atom_out);
	free_uri_map();
	fprintf(stderr, "bye.\n");
}

static void run_one(LV2_Atom_Sequence *data) {

#ifdef HAVE_LIBLO
	while (jack_ringbuffer_read_space(rb_osc_to_ui) >= sizeof(uint32_t) + sizeof(float)) {
		uint32_t idx;
		float val;
		jack_ringbuffer_read(rb_osc_to_ui, (char*) &idx, sizeof(uint32_t));
		jack_ringbuffer_read(rb_osc_to_ui, (char*) &val, sizeof(float));
		plugin_gui->port_event(gui_instance, idx, sizeof(float), 0, &val);
	}
#endif

	while (jack_ringbuffer_read_space(rb_ctrl_to_ui) >= sizeof(uint32_t) + sizeof(float)) {
		uint32_t idx;
		float val;
		jack_ringbuffer_read(rb_ctrl_to_ui, (char*) &idx, sizeof(uint32_t));
		jack_ringbuffer_read(rb_ctrl_to_ui, (char*) &val, sizeof(float));
		plugin_gui->port_event(gui_instance, idx, sizeof(float), 0, &val);
		if (idx == inst->latency_ctrl_port) {
			// jack client calls cannot be done in the DSP thread with jack1
			jack_recompute_total_latencies(j_client);
		}
	}

	while (jack_ringbuffer_read_space(rb_atom_to_ui) > sizeof(LV2_Atom)) {
		LV2_Atom a;
		jack_ringbuffer_read(rb_atom_to_ui, (char *) &a, sizeof(LV2_Atom));
		assert(a.size < inst->min_atom_bufsiz);
		jack_ringbuffer_read(rb_atom_to_ui, (char *) data, a.size);
		LV2_Atom_Event const* ev = (LV2_Atom_Event const*)((&(data)->body) + 1); // lv2_atom_sequence_begin
		while((const uint8_t*)ev < ((const uint8_t*) &(data)->body + (data)->atom.size)) {
			plugin_gui->port_event(gui_instance, portmap_atom_to_ui,
					ev->body.size, uri_atom_EventTransfer, &ev->body);
			ev = (LV2_Atom_Event const*) /* lv2_atom_sequence_next() */
				((const uint8_t*)ev + sizeof(LV2_Atom_Event) + ((ev->body.size + 7) & ~7));
		}
	}

	LV2_EXTERNAL_UI_RUN(extui);
}

#ifdef __APPLE__

static void osx_loop (CFRunLoopTimerRef timer, void *info) {
	if (client_state == Run) {
		run_one((LV2_Atom_Sequence*)info);
	}
	if (client_state == Exit) {
		rtk_osx_api_terminate();
	}
}

#else


static void main_loop(void) {
	struct timespec timeout;
	LV2_Atom_Sequence *data = (LV2_Atom_Sequence*) malloc(inst->min_atom_bufsiz * sizeof(uint8_t));

	pthread_mutex_lock (&gui_thread_lock);
	while (client_state != Exit) {
		run_one(data);

		if (client_state == Exit) break;

#ifdef _WIN32
		//Sleep(1000/UI_UPDATE_FPS);
#if (defined(__MINGW64__) || defined(__MINGW32__)) && __MSVCRT_VERSION__ >= 0x0601
		struct __timeb64 timebuffer;
		_ftime64(&timebuffer);
#else
		struct __timeb32 timebuffer;
		_ftime(&timebuffer);
#endif
		timeout.tv_nsec = timebuffer.millitm * 1000000;
		timeout.tv_sec = timebuffer.time;
#else // POSIX
		clock_gettime(CLOCK_REALTIME, &timeout);
#endif
		timeout.tv_nsec += 1000000000 / (UI_UPDATE_FPS);
		if (timeout.tv_nsec >= 1000000000) {timeout.tv_nsec -= 1000000000; timeout.tv_sec+=1;}
		pthread_cond_timedwait (&data_ready, &gui_thread_lock, &timeout);

	} /* while running */
	free(data);
	pthread_mutex_unlock (&gui_thread_lock);
}
#endif // APPLE RUNLOOP

static void catchsig (int sig) {
	fprintf(stderr,"caught signal - shutting down.\n");
	client_state=Exit;
	pthread_cond_signal (&data_ready);
}

static void on_external_ui_closed(void* controller) {
	catchsig(0);
}

#ifdef X42_MULTIPLUGIN
static void list_plugins (void) {
	unsigned int i;
	for (i = 0; i < sizeof(_plugins) / sizeof(RtkLv2Description); ++i) {
		const LV2_Descriptor* d = _plugins[i].lv2_descriptor(_plugins[i].dsp_descriptor_id);
		printf(" %2d   \"%s\" %s\n", i, _plugins[i].plugin_human_id, d->URI);
	}
}
#endif

static void print_usage (void) {
#ifdef X42_MULTIPLUGIN
	printf ("x42-%s - JACK %s\n\n", APPNAME, X42_MULTIPLUGIN_NAME);
	printf ("Usage: x42-%s [ OPTIONS ] [ Plugin-ID or URI ]\n\n", APPNAME);
	printf ("This is a standalone JACK application of a collection of LV2 plugins.\n"
		"Use ID -1, -l or --list for a dedicated list of included plugins.\n"
		"By default the first listed plugin (ID 0) is used.\n\n");
	printf ("List if available plugins: (ID \"Name\" URI)\n");
	list_plugins();

#else

#if defined X42_PLUGIN_STRUCT
	inst = & X42_PLUGIN_STRUCT;
#else
	inst = &_plugin;
#endif
	const LV2_Descriptor* d = inst->lv2_descriptor(inst->dsp_descriptor_id);

	printf ("x42-%s - JACK %s\n\n", APPNAME, inst->plugin_human_id);
	printf ("Usage: x42-%s [ OPTIONS ]\n\n", APPNAME);
	printf ("This is a standalone JACK application of the LV2 plugin:\n"
	        "\"%s\".\n", inst->plugin_human_id);
#endif

	printf ("\nUsage:\n"
"All control elements are operated in using the mouse:\n"
" Click+Drag     left/down: decrease, right/up: increase value. Hold the Ctrl key to increase sensitivity.\n"
" Shift+Click    reset to default value\n"
" Scroll-wheel    up/down by 1 step (smallest possible adjustment for given setting). Rapid continuous scrolling increases the step-size.\n"
"The application can be closed by sending a SIGTERM (CTRL+C) on the command-line of by closing the window.\n"
			);

	printf ("\nOptions:\n"
" -h, --help                Display this help and exit\n"
" -j, --jack-name <name>    Set the JACK client name\n"
"                           (defaults to plugin-name)\n"
#ifdef X42_MULTIPLUGIN
" -l, --list                Print list of available plugins and exit\n"
#endif
" -O <port>, --osc <port>   Listen for OSC messages on the given UDP port\n"
" -p <idx>:<val>, --port <idx>:<val>\n"
"                           Set initial value for given control port\n"
" -P, --portlist            Print control port list on startup\n"
" --osc-doc                 Print available OSC commands and exit\n"
" -V, --version             Print version information and exit\n"
			);

#ifdef X42_MULTIPLUGIN_URI
	printf ("\nSee also: <%s>\n", X42_MULTIPLUGIN_URI);
#else
	printf ("\nSee also: <%s>\n", d->URI);
#endif
	printf ("Website: <http://x42-plugins.com/>\n");
}

static void print_version (void) {
	printf ("x42-%s version %s\n\n", APPNAME, VERSION);
	printf ("\n"
		"Copyright (C) GPL 2013-2015 Robin Gareus <robin@gareus.org>\n"
		"This is free software; see the source for copying conditions.  There is NO\n"
		"warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\n");
}

static void dump_control_ports (void) {
	printf ("# Input Control Port List (%d ports)\n", inst->nports_ctrl);
	for (uint32_t i = 0; i < inst->nports_ctrl; ++i) {
		const uint32_t pi = portmap_rctl[i];
		if (inst->ports[pi].porttype != CONTROL_IN) {
			continue;
		}
		printf ("%3d: %s", i, inst->ports[pi].name);

		if (inst->ports[pi].val_min < inst->ports[pi].val_max) {
			printf (" (min: %.2f, max: %.2f)", inst->ports[pi].val_min, inst->ports[pi].val_max);
		}

		if (inst->ports[pi].val_default != nan) {
			printf (" [default: %.2f]", inst->ports[pi].val_default);
		}

		if (inst->ports[pi].doc) {
			printf (" -- %s", inst->ports[pi].doc);
		}
		printf ("\n");
	}
	printf ("### End Port List\n");
}

int main (int argc, char **argv) {
	int c;
	int rv = 0;
	int osc_port = 0;
	bool dump_ports = false;
	uint32_t c_ain  = 0;
	uint32_t c_aout = 0;
	uint32_t c_ctrl = 0;
	uint32_t n_pval = 0;
	struct PValue *pval  = NULL;
	char* jack_client_name = NULL;

	const struct option long_options[] = {
		{ "help",       no_argument,       0, 'h' },
		{ "jack-name",  required_argument, 0, 'j' },
		{ "list",       no_argument,       0, 'l' },
		{ "osc",        required_argument, 0, 'O' },
		{ "osc-doc",    no_argument,       0,  0x100 },
		{ "port",       required_argument, 0, 'p' },
		{ "portlist",   no_argument,       0, 'P' },
		{ "version",    no_argument,       0, 'V' },
	};

	const char *optstring = "hj:lO:p:PV1";

	if (optind < argc && !strncmp (argv[optind], "-psn_0", 6)) {++optind;}

	while ((c = getopt_long(argc, argv, optstring, long_options, NULL)) != -1) {
		char *tmp;
		switch (c) {
			case 'h':
				print_usage();
				return 0;
				break;
			case 'j':
				jack_client_name = optarg;
				break;
			case '1': // catch -1, backward compat
			case 'l':
#ifdef X42_MULTIPLUGIN
				list_plugins();
#endif
				return 0;
				break;
			case 'O':
				osc_port = atoi (optarg);
#ifndef HAVE_LIBLO
				fprintf (stderr, "This version was compiled without OSC support.\n");
#endif
				break;
			case 'p':
				if ((tmp = strchr (optarg, ':')) != NULL && *(++tmp)) {
					pval = (struct PValue*) realloc (pval, (n_pval + 1) * sizeof (struct PValue));
					pval[n_pval].port_idx = atoi (optarg);
					pval[n_pval].value = atof (tmp);
					++n_pval;
				}
				break;
			case 'P':
				dump_ports = true;
				break;
			case 'V':
				print_version();
				return 0;
				break;

			case 0x100:
#ifndef HAVE_LIBLO
				fprintf (stderr, "This version was compiled without OSC support.\n");
#else
				print_oscdoc();
#endif
				return 0;
				break;
			default:
				// silently ignore additional session options on OSX
#ifndef __APPLE__
				fprintf (stderr, "invalid argument.\n");
				print_usage();
				return(1);
#endif
				break;
		}
	}

	// TODO autoconnect option.
	// TODO allow to set initial params

#ifdef X42_MULTIPLUGIN
	inst = NULL;
	if (optind < argc && atoi (argv[optind]) < 0) {
		list_plugins();
		return 0;

	}
	if (optind < argc && strlen(argv[optind]) > 2 && atoi (argv[optind]) == 0) {
		unsigned int i;
		for (i = 0; i < sizeof(_plugins) / sizeof(RtkLv2Description); ++i) {
			const LV2_Descriptor* d = _plugins[i].lv2_descriptor(_plugins[i].dsp_descriptor_id);
			if (strstr(d->URI, argv[optind]) || strstr(_plugins[i].plugin_human_id, argv[optind])) {
				inst = &_plugins[i];
				break;
			}
		}
	}
	if (optind < argc && !inst && atoi (argv[optind]) >= 0) {
		unsigned int plugid = atoi (argv[optind]);
		if (plugid < (sizeof(_plugins) / sizeof(RtkLv2Description))) {
			inst = &_plugins[plugid];
		}
	}
	if (!inst) {
		inst = &_plugins[0];
	}
#elif defined X42_PLUGIN_STRUCT
	inst = & X42_PLUGIN_STRUCT;
#else
	inst = &_plugin;
#endif

#ifdef __APPLE__
	rtk_osx_api_init();
#endif

#ifdef USE_WEAK_JACK
	if (have_libjack()) {
		fprintf (stderr, "JACK is not available. http://jackaudio.org/\n");
#ifdef _WIN32
		MessageBox(NULL, TEXT(
					"JACK is not available.\n"
					"You must have the JACK Audio Connection Kit installed to use the tools. "
					"Please see http://jackaudio.org/ and http://jackaudio.org/faq/jack_on_windows.html"
					), TEXT("Error"), MB_ICONERROR | MB_OK);
#elif __APPLE__
		rtk_osx_api_err (
					"JACK is not available.\n"
					"You must have the JACK Audio Connection Kit installed to use the tools. "
					"Please see http://jackaudio.org/ and http://jackosx.com/"
				);
#endif
		return 1;
	}
#endif

#ifdef _WIN32
	pthread_win32_process_attach_np();
#endif
#if (defined _WIN32 && defined RTK_STATIC_INIT)
	glib_init_static();
	gobject_init_ctor();
#endif

	LV2_URID_Map uri_map               = { NULL, &uri_to_id };
	const LV2_Feature map_feature      = { LV2_URID__map, &uri_map};
	const LV2_Feature unmap_feature    = { LV2_URID__unmap, NULL };

	LV2_Worker_Schedule schedule       = { NULL, lv2_worker_schedule };
	const LV2_Feature schedule_feature = { LV2_WORKER__schedule, &schedule };

	const LV2_Feature* features[] = {
		&map_feature, &unmap_feature, &schedule_feature, NULL
	};

	const LV2_Feature external_lv_feature = { LV2_EXTERNAL_UI_URI, &extui_host};
	const LV2_Feature external_kx_feature = { LV2_EXTERNAL_UI_URI__KX__Host, &extui_host};
	LV2_Feature instance_feature          = { "http://lv2plug.in/ns/ext/instance-access", NULL };

	const LV2_Feature* ui_features[] = {
		&map_feature, &unmap_feature,
		&instance_feature,
		&external_lv_feature,
		&external_kx_feature,
		NULL
	};

	/* check sourced settings */
	assert ((inst->nports_midi_in + inst->nports_atom_in) <= 1);
	assert ((inst->nports_midi_out + inst->nports_atom_out) <= 1);
	assert (inst->plugin_human_id);
	assert (inst->nports_total > 0);

	extui_host.plugin_human_id = inst->plugin_human_id;

	// TODO check if allocs succeeded - OOM -> exit
	/* allocate data structure */
	portmap_a_in  = (uint32_t*) malloc(inst->nports_audio_in * sizeof(uint32_t));
	portmap_a_out = (uint32_t*) malloc(inst->nports_audio_out * sizeof(uint32_t));
	portmap_rctl  = (uint32_t*) malloc(inst->nports_ctrl  * sizeof(uint32_t));
	portmap_ctrl  = (uint32_t*) malloc(inst->nports_total * sizeof(uint32_t));

	plugin_ports_pre  = (float*) calloc(inst->nports_ctrl, sizeof(float));
	plugin_ports_post = (float*) calloc(inst->nports_ctrl, sizeof(float));

	atom_in = (LV2_Atom_Sequence*) malloc(inst->min_atom_bufsiz + sizeof(uint8_t));
	atom_out = (LV2_Atom_Sequence*) malloc(inst->min_atom_bufsiz + sizeof(uint8_t));

	rb_ctrl_to_ui = jack_ringbuffer_create((UPDATE_FREQ_RATIO) * inst->nports_ctrl * 2 * sizeof(float));
	rb_ctrl_from_ui = jack_ringbuffer_create((UPDATE_FREQ_RATIO) * inst->nports_ctrl * 2 * sizeof(float));

	rb_atom_to_ui = jack_ringbuffer_create((UPDATE_FREQ_RATIO) * inst->min_atom_bufsiz);
	rb_atom_from_ui = jack_ringbuffer_create((UPDATE_FREQ_RATIO) * inst->min_atom_bufsiz);

#ifdef HAVE_LIBLO
	rb_osc_to_ui = jack_ringbuffer_create((UPDATE_FREQ_RATIO) * inst->nports_ctrl * 2 * sizeof(float));
#endif

	/* resolve descriptors */
	plugin_dsp = inst->lv2_descriptor(inst->dsp_descriptor_id);
	plugin_gui = inst->lv2ui_descriptor(inst->gui_descriptor_id);

	if (!plugin_dsp) {
		fprintf (stderr, "cannot resolve LV2 descriptor\n");
		rv |= 2;
		goto out;
	}
	/* jack-open -> samlerate */
	if (init_jack(jack_client_name ? jack_client_name : extui_host.plugin_human_id)) {
		fprintf (stderr, "cannot connect to JACK.\n");
#ifdef _WIN32
		MessageBox (NULL, TEXT(
					"Cannot connect to JACK.\n"
					"Please start the JACK Server first."
					), TEXT("Error"), MB_ICONERROR | MB_OK);
#elif __APPLE__
		rtk_osx_api_err (
					"Cannot connect to JACK.\n"
					"Please start the JACK Server first."
				);
#else
		(void) system ("xmessage -button ok -center \"Cannot connect to JACK.\nPlease start the JACK Server first.\" &");
#endif
		rv |= 4;
		goto out;
	}

	/* init plugin */
	plugin_instance = plugin_dsp->instantiate(plugin_dsp, j_samplerate, NULL, features);
	if (!plugin_instance) {
		fprintf (stderr, "instantiation failed\n");
		rv |= 2;
		goto out;
	}

	/* connect ports */
	for (uint32_t p=0; p < inst->nports_total; ++p) {
		portmap_ctrl[p] = UINT32_MAX;
		switch (inst->ports[p].porttype) {
			case CONTROL_IN:
				plugin_ports_pre[c_ctrl] = inst->ports[p].val_default;
			case CONTROL_OUT:
				portmap_ctrl[p] = c_ctrl;
				portmap_rctl[c_ctrl] = p;
				plugin_dsp->connect_port(plugin_instance, p , &plugin_ports_pre[c_ctrl++]);
				break;
			case AUDIO_IN:
				portmap_a_in[c_ain++] = p;
				break;
			case AUDIO_OUT:
				portmap_a_out[c_aout++] = p;
				break;
			case MIDI_IN:
			case ATOM_IN:
				portmap_atom_from_ui = p;
				plugin_dsp->connect_port(plugin_instance, p , atom_in);
				break;
			case MIDI_OUT:
			case ATOM_OUT:
				portmap_atom_to_ui = p;
				plugin_dsp->connect_port(plugin_instance, p , atom_out);
				break;
			default:
				fprintf (stderr, "yet unsupported port..\n");
				break;
		}
	}

	assert(c_ain == inst->nports_audio_in);
	assert(c_aout == inst->nports_audio_out);
	assert(c_ctrl == inst->nports_ctrl);

	if (inst->nports_atom_out > 0 || inst->nports_atom_in > 0 || inst->nports_midi_in > 0 || inst->nports_midi_out > 0) {
		uri_atom_Sequence       = uri_to_id(NULL, LV2_ATOM__Sequence);
		uri_atom_EventTransfer  = uri_to_id(NULL, LV2_ATOM__eventTransfer);
		uri_midi_MidiEvent      = uri_to_id(NULL, LV2_MIDI__MidiEvent);
		uri_time_Position       = uri_to_id(NULL, LV2_TIME__Position);
		uri_time_frame          = uri_to_id(NULL, LV2_TIME__frame);
		uri_time_speed          = uri_to_id(NULL, LV2_TIME__speed);
		uri_time_bar            = uri_to_id(NULL, LV2_TIME__bar);
		uri_time_barBeat        = uri_to_id(NULL, LV2_TIME__barBeat);
		uri_time_beatUnit       = uri_to_id(NULL, LV2_TIME__beatUnit);
		uri_time_beatsPerBar    = uri_to_id(NULL, LV2_TIME__beatsPerBar);
		uri_time_beatsPerMinute = uri_to_id(NULL, LV2_TIME__beatsPerMinute);
		lv2_atom_forge_init(&lv2_forge, &uri_map);
	}

	if (dump_ports) {
		dump_control_ports();
	}

	// apply user settings
	for (uint32_t i = 0; i < n_pval; ++i) {
		if (pval[i].port_idx >= inst->nports_ctrl) {
			fprintf (stderr, "Invalid Parameter 0 <= %d < %d\n", pval[i].port_idx, inst->nports_ctrl);
			continue;
		}

		const uint32_t port_index = portmap_rctl[pval[i].port_idx];

		if (inst->ports[port_index].porttype != CONTROL_IN) {
			fprintf (stderr, "mapped port (%d) for port %d is not a control input.\n", port_index, pval[i].port_idx);
			continue;
		}

		if (inst->ports[port_index].val_min < inst->ports[port_index].val_max) {
			if (pval[i].value < inst->ports[port_index].val_min || pval[i].value > inst->ports[port_index].val_max) {
				fprintf (stderr, "Value for port %d out of bounds %f <= %f <= %f\n",
						pval[i].port_idx,
						inst->ports[port_index].val_min, pval[i].value, inst->ports[port_index].val_max);
				continue;
			}
		}

		//fprintf (stdout, "Port: %d,  %d -> %f\n", pval[i].port_idx, port_index, pval[i].value);
		plugin_ports_pre[pval[i].port_idx] = pval[i].value;
	}

	if (jack_portsetup()) {
		rv |= 12;
		goto out;
	}

	if (plugin_gui) {
	/* init plugin GUI */
	extui_host.ui_closed = on_external_ui_closed;
	instance_feature.data = plugin_instance;
	gui_instance = plugin_gui->instantiate(plugin_gui,
			plugin_dsp->URI, NULL,
			&write_function, controller,
			(void **)&extui, ui_features);

	}

	if (!gui_instance || !extui) {
#ifdef REQUIRE_UI
		fprintf(stderr, "Error: GUI initialization failed.\n");
		rv |= 2;
		goto out;
#else
		fprintf(stderr, "Warning: GUI initialization failed.\n");
#endif
	}

#ifndef _WIN32
	if (mlockall (MCL_CURRENT | MCL_FUTURE)) {
		fprintf(stderr, "Warning: Can not lock memory.\n");
	}
#endif

	if (gui_instance) {
		for (uint32_t p = 0; p < inst->nports_ctrl; p++) {
			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 (plugin_dsp->extension_data) {
		worker_iface = (LV2_Worker_Interface*) plugin_dsp->extension_data (LV2_WORKER__interface);
	}
	if (worker_iface) {
		worker_init();
	}

	if (plugin_dsp->activate) {
		plugin_dsp->activate(plugin_instance);
	}

	if (jack_activate (j_client)) {
		fprintf (stderr, "cannot activate client.\n");
		rv |= 20;
		goto out;
	}

	jack_portconnect(JACK_AUTOCONNECT);

#ifdef HAVE_LIBLO
	if (osc_port > 0) {
		start_osc_server (osc_port);
	}
#endif

#ifndef _WIN32
	signal (SIGHUP, catchsig);
	signal (SIGINT, catchsig);
#endif

	if (!gui_instance || !extui) {
		/* no GUI */
		while (client_state != Exit) {
			sleep (1);
		}
	} else {

		LV2_EXTERNAL_UI_SHOW(extui);

#ifdef __APPLE__
		LV2_Atom_Sequence *data = (LV2_Atom_Sequence*) malloc(inst->min_atom_bufsiz * sizeof(uint8_t));
		CFRunLoopRef runLoop = CFRunLoopGetCurrent();
		CFRunLoopTimerContext context = {0, data, NULL, NULL, NULL};
		CFRunLoopTimerRef timer = CFRunLoopTimerCreate(kCFAllocatorDefault, 0, 1.0/UI_UPDATE_FPS, 0, 0, &osx_loop, &context);
		CFRunLoopAddTimer(runLoop, timer, kCFRunLoopCommonModes);
		rtk_osx_api_run();
		free(data);
#else

		main_loop();
#endif

		LV2_EXTERNAL_UI_HIDE(extui);
	}

out:
	free (pval);
	cleanup(0);
#ifdef _WIN32
	pthread_win32_process_detach_np();
#endif
#if (defined _WIN32 && defined RTK_STATIC_INIT)
	glib_cleanup_static();
#endif
	return(rv);
}
/* vi:set ts=2 sts=2 sw=2: */