aboutsummaryrefslogtreecommitdiff
path: root/plugins/synthpod_stereo.c
blob: b272b3ad51a85d12ae3c3bb8c50d867f1c2b1246 (plain)
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
/*
 * Copyright (c) 2015-2016 Hanspeter Portner (dev@open-music-kontrollers.ch)
 *
 * This is free software: you can redistribute it and/or modify
 * it under the terms of the Artistic License 2.0 as published by
 * The Perl Foundation.
 *
 * This source 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
 * Artistic License 2.0 for more details.
 *
 * You should have received a copy of the Artistic License 2.0
 * along the source as a COPYING file. If not, obtain it from
 * http://www.perlfoundation.org/artistic_license_2_0.
 */

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

#include <synthpod_lv2.h>
#include <synthpod_app.h>

#include <lv2/lv2plug.in/ns/ext/atom/atom.h>
#include <lv2/lv2plug.in/ns/ext/atom/forge.h>
#include <lv2/lv2plug.in/ns/ext/midi/midi.h>
#include <lv2/lv2plug.in/ns/ext/urid/urid.h>
#include <lv2/lv2plug.in/ns/ext/worker/worker.h>
#include <lv2/lv2plug.in/ns/ext/state/state.h>
#include <lv2/lv2plug.in/ns/ext/log/log.h>
#include <lv2/lv2plug.in/ns/ext/buf-size/buf-size.h>
#include <lv2/lv2plug.in/ns/ext/options/options.h>

#include <osc.lv2/osc.h>
#include <varchunk.h>

#define CHUNK_SIZE 0x100000
#define MAX_MSGS 10 //FIXME limit to how many events?

typedef enum _job_type_t job_type_t;
typedef struct _job_t job_t;
typedef struct _plughandle_t plughandle_t;

enum _job_type_t {
	JOB_TYPE_NONE = 0,
	JOB_TYPE_STASH
};

struct _job_t {
	job_type_t type;
	union {
		struct {
			char *bundle_path;
			LV2_Atom_Object *obj;
		} stash;
	};
};

struct _plughandle_t {
	sp_app_t *app;
	sp_app_driver_t driver;

	LV2_Worker_Schedule *schedule;
	LV2_Options_Option *opts;

	struct {
		struct {
			LV2_URID max_block_length;
			LV2_URID min_block_length;
			LV2_URID sequence_size;
		} bufsz;
		struct {
			LV2_URID event;
		} synthpod;
	} uri;

	struct {
		LV2_Atom_Forge event_out;
#if 0
		LV2_Atom_Forge com_in;
#endif
		LV2_Atom_Forge notify;
	} forge;

	struct {
		LV2_Atom_Forge_Ref event_out;
#if 0
		LV2_Atom_Forge_Ref com_in;
#endif
		LV2_Atom_Forge_Ref notify;
	} ref;

	struct {
		const LV2_Atom_Sequence *event_in;
		LV2_Atom_Sequence *event_out;

		const float *audio_in[2];
		float *audio_out[2];

		const float *input[4];
		float *output[4];

		const LV2_Atom_Sequence *control;
		LV2_Atom_Sequence *notify;
	} port;

	struct {
		LV2_Atom_Sequence *event_in;
		float *audio_in[2];
		float *input[4];
#if 0
		LV2_Atom_Sequence *com_in;
#endif
	} source;

	struct {
		const LV2_Atom_Sequence *event_out;
		const float *audio_out[2];
		const float *output[4];
		const LV2_Atom_Sequence *com_out;
	} sink;

	uint8_t buf [CHUNK_SIZE] _ATOM_ALIGNED;

	bool advance_worker;
	bool advance_ui;
	bool trigger_worker;
	varchunk_t *app_to_worker;
	varchunk_t *app_from_worker;
	varchunk_t *app_from_ui;
	varchunk_t *app_from_app;
};

static LV2_State_Status
_state_save(LV2_Handle instance, LV2_State_Store_Function store,
	LV2_State_Handle state, uint32_t flags,
	const LV2_Feature *const *features)
{
	plughandle_t *handle = instance;
	sp_app_t *app = handle->app;

	return sp_app_save(app, store, state, flags, features);
}

static LV2_State_Status
_state_restore(LV2_Handle instance, LV2_State_Retrieve_Function retrieve,
	LV2_State_Handle state, uint32_t flags,
	const LV2_Feature *const *features)
{
	plughandle_t *handle = instance;
	sp_app_t *app = handle->app;

	LV2_Worker_Schedule *schedule = NULL;
	LV2_State_Map_Path *map_path = NULL;

	for(int i=0; features[i]; i++)
	{
		if(!strcmp(features[i]->URI, LV2_WORKER__schedule))
			schedule = features[i]->data;
		else if(!strcmp(features[i]->URI, LV2_STATE__mapPath))
			map_path = features[i]->data;
	}

	if(!schedule)
	{
		return LV2_STATE_ERR_UNKNOWN;
	}

	if(!map_path)
	{
		return LV2_STATE_ERR_UNKNOWN;
	}

	char *bundle_path = map_path->absolute_path(map_path->handle, "");
	if(!bundle_path)
	{
		return LV2_STATE_ERR_UNKNOWN;
	}

	LV2_Atom_Object *obj = sp_app_stash(app, retrieve, state, flags, features);
	if(!obj)
	{

		return LV2_STATE_ERR_UNKNOWN;
	}

	const job_t job = {
		.type = JOB_TYPE_STASH,
		.stash = {
			.bundle_path = bundle_path,
			.obj = obj
		}
	};
	const LV2_Worker_Status stat = schedule->schedule_work(schedule->handle,
		sizeof(job_t), &job);

	if(stat == LV2_WORKER_SUCCESS)
	{
		return LV2_STATE_SUCCESS;
	}

	return LV2_STATE_ERR_UNKNOWN;
}
	
static const LV2_State_Interface state_iface = {
	.save = _state_save,
	.restore = _state_restore
};

// non-rt worker-thread
static LV2_Worker_Status
_work(LV2_Handle instance,
	LV2_Worker_Respond_Function respond,
	LV2_Worker_Respond_Handle target,
	uint32_t _size,
	const void *_body)
{
	plughandle_t *handle = instance;

	const job_t *job = _body;

	switch(job->type)
	{
		case JOB_TYPE_STASH:
		{
			if(  job->stash.obj
				&& job->stash.bundle_path
				&& strlen(job->stash.bundle_path) )
			{
				sp_app_apply(handle->app, job->stash.obj, job->stash.bundle_path);
			}

			free(job->stash.bundle_path);
			free(job->stash.obj);
		} break;
		case JOB_TYPE_NONE:
		default:
		{
			size_t size;
			const void *body;
			while((body = varchunk_read_request(handle->app_to_worker, &size)))
			{
				sp_worker_from_app(handle->app, size, body);
				varchunk_read_advance(handle->app_to_worker);
			}
		} break;
	}
	

	return LV2_WORKER_SUCCESS;
}

__realtime static LV2_Worker_Status
_work_response(LV2_Handle instance, uint32_t size, const void *body)
{
	//plughandle_t *handle = instance;

	return LV2_WORKER_SUCCESS;
}

__realtime static LV2_Worker_Status
_end_run(LV2_Handle instance)
{
	//plughandle_t *handle = instance;

	return LV2_WORKER_SUCCESS;
}

static const LV2_Worker_Interface work_iface = {
	.work = _work,
	.work_response = _work_response,
	.end_run = _end_run
};

__realtime static void *
_to_ui_request(size_t minimum, size_t *maximum, void *data)
{
	plughandle_t *handle = data;

	if(minimum <= CHUNK_SIZE)
	{
		*maximum = CHUNK_SIZE;
		return handle->buf;
	}

	return NULL;
}
__realtime static void
_to_ui_advance(size_t written, void *data)
{
	plughandle_t *handle = data;
	LV2_Atom_Forge *forge = &handle->forge.notify;
	LV2_Atom_Forge_Ref *ref = &handle->ref.notify;

	//printf("_to_ui_advance: %zu\n", written);

	if(forge->offset + written > forge->size)
		return; // buffer overflow

	if(*ref)
		*ref = lv2_atom_forge_frame_time(forge, 0);
	if(*ref)
		*ref = lv2_atom_forge_write(forge, handle->buf, written);
}

__realtime static void *
_to_worker_request(size_t minimum, size_t *maximum, void *data)
{
	plughandle_t *handle = data;

	return varchunk_write_request_max(handle->app_to_worker, minimum, maximum);
}
__realtime static void
_to_worker_advance(size_t written, void *data)
{
	plughandle_t *handle = data;

	varchunk_write_advance(handle->app_to_worker, written);
	handle->trigger_worker = true;
}

__non_realtime static void *
_to_app_request(size_t minimum, size_t *maximum, void *data)
{
	plughandle_t *handle = data;

	void *ptr;
	do
	{
		ptr = varchunk_write_request_max(handle->app_from_worker, minimum, maximum);
	}
	while(!ptr); // wait until there is enough space

	return ptr;
}
__non_realtime static void
_to_app_advance(size_t written, void *data)
{
	plughandle_t *handle = data;	

	varchunk_write_advance(handle->app_from_worker, written);
}

static LV2_Handle
instantiate(const LV2_Descriptor* descriptor, double rate,
	const char *bundle_path, const LV2_Feature *const *features)
{
	plughandle_t *handle = calloc(1, sizeof(plughandle_t));
	if(!handle)
		return NULL;
	mlock(handle, sizeof(plughandle_t));

	handle->driver.sample_rate = rate;
	handle->driver.seq_size = SEQ_SIZE;
	handle->driver.system_port_add = NULL;
	handle->driver.system_port_del = NULL;
	handle->driver.osc_sched = NULL;
	handle->driver.features = 0;
	handle->driver.num_slaves = 0;
	handle->driver.bad_plugins = false; //FIXME

	const LilvWorld *world = NULL;

	for(int i=0; features[i]; i++)
	{
		if(!strcmp(features[i]->URI, LV2_URID__map))
			handle->driver.map = (LV2_URID_Map *)features[i]->data;
		else if(!strcmp(features[i]->URI, LV2_URID__unmap))
			handle->driver.unmap = (LV2_URID_Unmap *)features[i]->data;
		else if(!strcmp(features[i]->URI, XPRESS__voiceMap))
			handle->driver.xmap = features[i]->data;
		else if(!strcmp(features[i]->URI, LV2_LOG__log))
			handle->driver.log = (LV2_Log_Log *)features[i]->data;
		else if(!strcmp(features[i]->URI, LV2_WORKER__schedule))
			handle->schedule = (LV2_Worker_Schedule *)features[i]->data;
		else if(!strcmp(features[i]->URI, LV2_OPTIONS__options))
			handle->opts = (LV2_Options_Option *)features[i]->data;
		else if(!strcmp(features[i]->URI, SYNTHPOD_PREFIX"world"))
			world = (const LilvWorld *)features[i]->data;
		else if(!strcmp(features[i]->URI, LV2_OSC__schedule))
			handle->driver.osc_sched = features[i]->data;
		else if(!strcmp(features[i]->URI, LV2_BUF_SIZE__fixedBlockLength))
			handle->driver.features |= SP_APP_FEATURE_FIXED_BLOCK_LENGTH;
		else if(!strcmp(features[i]->URI, LV2_BUF_SIZE__powerOf2BlockLength))
			handle->driver.features |= SP_APP_FEATURE_POWER_OF_2_BLOCK_LENGTH;
	}

	if(!handle->driver.xmap)
		handle->driver.xmap = &voice_map_fallback;

	if(!handle->driver.map)
	{
		fprintf(stderr,
			"%s: Host does not support urid:map\n", descriptor->URI);
		free(handle);
		return NULL;
	}
	
	if(!handle->schedule)
	{
		fprintf(stderr,
			"%s: Host does not support worker:schedule\n",
			descriptor->URI);
		free(handle);
		return NULL;
	}

	if(!handle->opts)
	{
		fprintf(stderr,
			"%s: Host does not support options:option\n",
			descriptor->URI);
		free(handle);
		return NULL;
	}

	// map URIs
	handle->uri.bufsz.max_block_length = handle->driver.map->map(handle->driver.map->handle,
		LV2_BUF_SIZE__maxBlockLength);
	handle->uri.bufsz.min_block_length = handle->driver.map->map(handle->driver.map->handle,
		LV2_BUF_SIZE__minBlockLength);
	handle->uri.bufsz.sequence_size = handle->driver.map->map(handle->driver.map->handle,
		LV2_BUF_SIZE__sequenceSize);

	handle->uri.synthpod.event = handle->driver.map->map(handle->driver.map->handle,
		SYNTHPOD_EVENT_URI);

	lv2_atom_forge_init(&handle->forge.event_out, handle->driver.map);
#if 0
	lv2_atom_forge_init(&handle->forge.com_in, handle->driver.map);
#endif
	lv2_atom_forge_init(&handle->forge.notify, handle->driver.map);

	for(LV2_Options_Option *opt = handle->opts;
		(opt->key != 0) && (opt->value != NULL);
		opt++)
	{
		if( (opt->key == handle->uri.bufsz.max_block_length)
				&& (opt->type == handle->forge.notify.Int) )
			handle->driver.max_block_size = *(int32_t *)opt->value;
		else if( (opt->key == handle->uri.bufsz.min_block_length)
				&& (opt->type == handle->forge.notify.Int) )
			handle->driver.min_block_size = *(int32_t *)opt->value;
		else if( (opt->key == handle->uri.bufsz.sequence_size)
				&& (opt->type == handle->forge.notify.Int) )
			handle->driver.seq_size = *(int32_t *)opt->value;
		//TODO handle more options
	}

	handle->advance_worker = true; //TODO reset in activate ?
	handle->advance_ui = true; //TODO reset in activate ?
	handle->app_to_worker = varchunk_new(CHUNK_SIZE, true);
	handle->app_from_worker = varchunk_new(CHUNK_SIZE, true);
	handle->app_from_ui = varchunk_new(CHUNK_SIZE, false);
	handle->app_from_app = varchunk_new(CHUNK_SIZE, false);

	handle->driver.to_ui_request = _to_ui_request;
	handle->driver.to_ui_advance = _to_ui_advance;
	handle->driver.to_worker_request = _to_worker_request;
	handle->driver.to_worker_advance = _to_worker_advance;
	handle->driver.to_app_request = _to_app_request;
	handle->driver.to_app_advance = _to_app_advance;

	handle->app = sp_app_new(world, &handle->driver, handle);
	if(!handle->app)
	{
		fprintf(stderr,
			"%s: creation of app failed\n",
			descriptor->URI);
		free(handle);
		return NULL;
	}
	sp_app_set_bundle_path(handle->app, bundle_path);

	return handle;
}

static void
connect_port(LV2_Handle instance, uint32_t port, void *data)
{
	plughandle_t *handle = instance;

	switch(port)
	{
		case 0:
			handle->port.event_in = (const LV2_Atom_Sequence *)data;
			break;
		case 1:
			handle->port.event_out = (LV2_Atom_Sequence *)data;
			break;

		case 2:
			handle->port.audio_in[0] = (const float *)data;
			break;
		case 3:
			handle->port.audio_in[1] = (const float *)data;
			break;

		case 4:
			handle->port.audio_out[0] = (float *)data;
			break;
		case 5:
			handle->port.audio_out[1] = (float *)data;
			break;
		
		case 6:
			handle->port.input[0] = (const float *)data;
			break;
		case 7:
			handle->port.input[1] = (const float *)data;
			break;
		case 8:
			handle->port.input[2] = (const float *)data;
			break;
		case 9:
			handle->port.input[3] = (const float *)data;
			break;
		
		case 10:
			handle->port.output[0] = (float *)data;
			break;
		case 11:
			handle->port.output[1] = (float *)data;
			break;
		case 12:
			handle->port.output[2] = (float *)data;
			break;
		case 13:
			handle->port.output[3] = (float *)data;
			break;

		case 14:
			handle->port.control = (const LV2_Atom_Sequence *)data;
			break;
		case 15:
			handle->port.notify = (LV2_Atom_Sequence *)data;
			break;

		default:
			break;
	}
}

static void
activate(LV2_Handle instance)
{
	plughandle_t *handle = instance;

	sp_app_activate(handle->app);
}

static inline void
_process_pre(plughandle_t *handle, uint32_t nsamples, bool bypassed)
{
	sp_app_t *app = handle->app;

	// drain worker buffer
	{
		size_t size;
		const void *body;
		unsigned n = 0;
		while((body = varchunk_read_request(handle->app_from_worker, &size))
			&& (n++ < MAX_MSGS) )
		{
			const bool advance = sp_app_from_worker(handle->app, size, body);
			if(!advance)
			{
				//fprintf(stderr, "plugin worker is blocked\n");
				break;
			}
			varchunk_read_advance(handle->app_from_worker);
		}
	}

	// run app pre
	if(!bypassed)
		sp_app_run_pre(app, nsamples);

	// drain events from UI ringbuffer
	{
		const LV2_Atom *atom;
		size_t size;
		unsigned n = 0;
		while((atom = varchunk_read_request(handle->app_from_ui, &size))
			&& (n++ < MAX_MSGS) )
		{
			handle->advance_ui = sp_app_from_ui(app, atom);
			if(!handle->advance_ui)
			{
				//fprintf(stderr, "plugin ui indirect is blocked\n");
				break;
			}
			varchunk_read_advance(handle->app_from_ui);
		}
	}

	// drain events from feedback ringbuffer
	{
		const LV2_Atom *atom;
		size_t size;
		unsigned n = 0;
		while((atom = varchunk_read_request(handle->app_from_app, &size))
			&& (n++ < MAX_MSGS) )
		{
			handle->advance_ui = sp_app_from_ui(app, atom);
			if(!handle->advance_ui)
			{
				//fprintf(stderr, "plugin feedback is blocked\n");
				break;
			}
			varchunk_read_advance(handle->app_from_app);
		}
	}

	//FIXME drain event from separate feedback ringbuffer

	// handle events from UI
	LV2_ATOM_SEQUENCE_FOREACH(handle->port.control, ev)
	{
		const LV2_Atom *atom = &ev->body;
		const LV2_Atom_Object *obj = (const LV2_Atom_Object *)atom;

		if(atom->type == handle->forge.notify.Object)
		{
			// copy com events to com buffer 
			if(sp_app_com_event(handle->app, obj->body.otype))
			{
				uint32_t size = obj->atom.size + sizeof(LV2_Atom);
#if 0
				if(handle->ref.com_in)
					handle->ref.com_in = lv2_atom_forge_frame_time(&handle->forge.com_in, ev->time.frames);
				if(handle->ref.com_in)
					handle->ref.com_in = lv2_atom_forge_raw(&handle->forge.com_in, obj, size);
				if(handle->ref.com_in)
					lv2_atom_forge_pad(&handle->forge.com_in, size);
#endif
			}

			// try do process events directly
			handle->advance_ui = sp_app_from_ui(app, atom);
			if(!handle->advance_ui) // queue event in ringbuffer instead
			{
				//fprintf(stderr, "plugin ui direct is blocked\n");

				void *ptr;
				size_t size = lv2_atom_total_size(atom);
				if((ptr = varchunk_write_request(handle->app_from_ui, size)))
				{
					memcpy(ptr, atom, size);
					varchunk_write_advance(handle->app_from_ui, size);
				}
				else
				{
					//fprintf(stderr, "app_from_ui ringbuffer full\n");
					//FIXME
				}
			}
		}
	}

	// run app post
	if(!bypassed)
		sp_app_run_post(app, nsamples);

	// write com events to feedback buffer
	if(handle->sink.com_out)
	{
		LV2_ATOM_SEQUENCE_FOREACH(handle->sink.com_out, ev)
		{
			const LV2_Atom *atom = &ev->body;

			void *ptr;
			size_t size = lv2_atom_total_size(atom);
			if((ptr = varchunk_write_request(handle->app_from_app, size)))
			{
				memcpy(ptr, atom, size);
				varchunk_write_advance(handle->app_from_app, size);
			}
			else
			{
				//FIXME
			}
		}
	}
}
		
static inline void
_process_post(plughandle_t *handle)
{
	if(handle->trigger_worker)
	{
		//fprintf(stderr, "work triggered\n");

		const job_t job = {
			.type = JOB_TYPE_NONE
		};
		handle->schedule->schedule_work(handle->schedule->handle,
			sizeof(job_t), &job); //FIXME check

		handle->trigger_worker = false;
	}
}

__realtime static void
run(LV2_Handle instance, uint32_t nsamples)
{
	plughandle_t *handle = instance;
	sp_app_t *app = handle->app;

	const size_t sample_buf_size = sizeof(float) * nsamples;

	// get input buffers
	handle->source.event_in = NULL;
	handle->source.audio_in[0] = NULL;
	handle->source.audio_in[1] = NULL;
	handle->source.input[0] = NULL;
	handle->source.input[1] = NULL;
	handle->source.input[2] = NULL;
	handle->source.input[3] = NULL;
	
	const sp_app_system_source_t *sources = sp_app_get_system_sources(app);
	int audio_ptr = 0;
	int control_ptr = 0;
	for(const sp_app_system_source_t *source=sources;
		source->type != SYSTEM_PORT_NONE;
		source++)
	{
		switch(source->type)
		{
			case SYSTEM_PORT_MIDI:
				handle->source.event_in = source->buf;
				break;
			case SYSTEM_PORT_AUDIO:
				handle->source.audio_in[audio_ptr++] = source->buf;
				break;
			case SYSTEM_PORT_CONTROL:
				handle->source.input[control_ptr++] = source->buf;
				break;
			case SYSTEM_PORT_COM:
#if 0
				handle->source.com_in = source->buf;
#endif
				break;

			case SYSTEM_PORT_CV:
			case SYSTEM_PORT_OSC:
			case SYSTEM_PORT_NONE:
				break;
		}
	}

	//TODO use __builtin_assume_aligned

	// fill input buffers
	if(handle->source.event_in)
		memcpy(handle->source.event_in, handle->port.event_in, SEQ_SIZE);
	if(handle->source.audio_in[0])
		memcpy(handle->source.audio_in[0], handle->port.audio_in[0], sample_buf_size);
	if(handle->source.audio_in[1])
		memcpy(handle->source.audio_in[1], handle->port.audio_in[1], sample_buf_size);
	if(handle->source.input[0])
		*handle->source.input[0] = *handle->port.input[0];
	if(handle->source.input[1])
		*handle->source.input[1] = *handle->port.input[1];
	if(handle->source.input[2])
		*handle->source.input[2] = *handle->port.input[2];
	if(handle->source.input[3])
		*handle->source.input[3] = *handle->port.input[3];

	// get output buffers
	const sp_app_system_sink_t *sinks = sp_app_get_system_sinks(app);

	// fill output buffers
	handle->sink.event_out = NULL;
	handle->sink.audio_out[0] = NULL;
	handle->sink.audio_out[1] = NULL;
	handle->sink.output[0] = NULL;
	handle->sink.output[1] = NULL;
	handle->sink.output[2] = NULL;
	handle->sink.output[3] = NULL;
	
	audio_ptr = 0;
	control_ptr = 0;
	for(const sp_app_system_sink_t *sink=sinks;
		sink->type != SYSTEM_PORT_NONE;
		sink++)
	{
		switch(sink->type)
		{
			case SYSTEM_PORT_MIDI:
				handle->sink.event_out = sink->buf;
				break;
			case SYSTEM_PORT_AUDIO:
				handle->sink.audio_out[audio_ptr++] = sink->buf;
				break;
			case SYSTEM_PORT_CONTROL:
				handle->sink.output[control_ptr++] = sink->buf;
				break;
			case SYSTEM_PORT_COM:
				handle->sink.com_out = sink->buf;
				break;

			case SYSTEM_PORT_CV:
			case SYSTEM_PORT_OSC:
			case SYSTEM_PORT_NONE:
				break;
		}
	}

	struct {
		LV2_Atom_Forge_Frame event_out;
#if 0
		LV2_Atom_Forge_Frame com_in;
#endif
		LV2_Atom_Forge_Frame notify;
	} frame;

	// prepare forge(s) & sequence(s)
	lv2_atom_forge_set_buffer(&handle->forge.event_out,
		(uint8_t *)handle->port.event_out, handle->port.event_out->atom.size);
	handle->ref.event_out = lv2_atom_forge_sequence_head(&handle->forge.event_out, &frame.event_out, 0);

#if 0
	lv2_atom_forge_set_buffer(&handle->forge.com_in,
		(uint8_t *)handle->source.com_in, SEQ_SIZE);
	handle->ref.com_in = lv2_atom_forge_sequence_head(&handle->forge.com_in, &frame.com_in, 0);
#endif
	
	lv2_atom_forge_set_buffer(&handle->forge.notify,
		(uint8_t *)handle->port.notify, handle->port.notify->atom.size);
	handle->ref.notify = lv2_atom_forge_sequence_head(&handle->forge.notify, &frame.notify, 0);

	if(sp_app_bypassed(app))
	{
		//fprintf(stderr, "plugin app is bypassed\n");

		memset(handle->port.audio_out[0], 0x0, nsamples*sizeof(float));
		memset(handle->port.audio_out[1], 0x0, nsamples*sizeof(float));
		
		*handle->port.output[0] = 0.f;
		*handle->port.output[1] = 0.f;
		*handle->port.output[2] = 0.f;
		*handle->port.output[3] = 0.f;

		_process_pre(handle, nsamples, true);
		_process_post(handle);

		// end sequence(s)
		if(handle->ref.event_out)
			lv2_atom_forge_pop(&handle->forge.event_out, &frame.event_out);
		else
			lv2_atom_sequence_clear(handle->port.event_out);

#if 0
		if(handle->ref.com_in)
			lv2_atom_forge_pop(&handle->forge.com_in, &frame.com_in);
		else
			lv2_atom_sequence_clear(handle->source.com_in);
#endif

		if(handle->ref.notify)
			lv2_atom_forge_pop(&handle->forge.notify, &frame.notify);
		else
			lv2_atom_sequence_clear(handle->port.notify);

		return;
	}

	_process_pre(handle, nsamples, false);
	_process_post(handle);

	// end sequence(s)
	if(handle->ref.event_out)
		lv2_atom_forge_pop(&handle->forge.event_out, &frame.event_out);
	else
		lv2_atom_sequence_clear(handle->port.event_out);

#if 0
	if(handle->ref.com_in)
		lv2_atom_forge_pop(&handle->forge.com_in, &frame.com_in);
	else
		lv2_atom_sequence_clear(handle->source.com_in);
#endif

	if(handle->ref.notify)
		lv2_atom_forge_pop(&handle->forge.notify, &frame.notify);
	else
		lv2_atom_sequence_clear(handle->port.notify);

	if(handle->sink.event_out)
		memcpy(handle->port.event_out, handle->sink.event_out, SEQ_SIZE);
	else
		memset(handle->port.event_out, 0x0, SEQ_SIZE);

	if(handle->sink.audio_out[0])
		memcpy(handle->port.audio_out[0], handle->sink.audio_out[0], sample_buf_size);
	else
		memset(handle->port.audio_out[0], 0x0, sample_buf_size);

	if(handle->sink.audio_out[1])
		memcpy(handle->port.audio_out[1], handle->sink.audio_out[1], sample_buf_size);
	else
		memset(handle->port.audio_out[1], 0x0, sample_buf_size);

	*handle->port.output[0] = handle->sink.output[0] ? *handle->sink.output[0] : 0.f;
	*handle->port.output[1] = handle->sink.output[1] ? *handle->sink.output[1] : 0.f;
	*handle->port.output[2] = handle->sink.output[2] ? *handle->sink.output[2] : 0.f;
	*handle->port.output[3] = handle->sink.output[3] ? *handle->sink.output[3] : 0.f;
}

static void
deactivate(LV2_Handle instance)
{
	plughandle_t *handle = instance;

	sp_app_deactivate(handle->app);
}

static void
cleanup(LV2_Handle instance)
{
	plughandle_t *handle = instance;

	sp_app_free(handle->app);

	varchunk_free(handle->app_to_worker);
	varchunk_free(handle->app_from_worker);
	varchunk_free(handle->app_from_ui);
	varchunk_free(handle->app_from_app);

	munlock(handle, sizeof(plughandle_t));
	free(handle);
}

__realtime static uint32_t
_opts_get(LV2_Handle instance, LV2_Options_Option *options)
{
	// we have no options

	return LV2_OPTIONS_ERR_BAD_KEY;
}

__realtime static uint32_t
_opts_set(LV2_Handle instance, const LV2_Options_Option *options)
{
	plughandle_t *handle = instance;

	// route options to all plugins
	return sp_app_options_set(handle->app, options);
}

static const LV2_Options_Interface opts_iface = {
	.get = _opts_get,
	.set = _opts_set
};

static const void*
extension_data(const char* uri)
{
	if(!strcmp(uri, LV2_WORKER__interface))
		return &work_iface;
	else if(!strcmp(uri, LV2_STATE__interface))
		return &state_iface;
	else if(!strcmp(uri, LV2_OPTIONS__interface))
		return &opts_iface;

	return NULL;
}

const LV2_Descriptor synthpod_stereo = {
	.URI						= SYNTHPOD_STEREO_URI,
	.instantiate		= instantiate,
	.connect_port		= connect_port,
	.activate				= activate,
	.run						= run,
	.deactivate			= deactivate,
	.cleanup				= cleanup,
	.extension_data	= extension_data
};