aboutsummaryrefslogtreecommitdiff
path: root/vm.c
blob: a49f78b25dd7d7fc856d0f5fc4d67357ae8b6e8c (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
/*
 * Copyright (c) 2017 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 <stdlib.h>
#include <stdatomic.h>
#include <math.h>
#include <inttypes.h>

#include <vm.h>

#define SLOT_MAX  0x20
#define SLOT_MASK (SLOT_MAX - 1)

#define REG_MAX   0x20
#define REG_MASK  (REG_MAX - 1)

typedef struct _vm_stack_t vm_stack_t;
typedef struct _plughandle_t plughandle_t;

typedef double num_t;

struct _vm_stack_t {
	num_t slots [SLOT_MAX];
	num_t regs [REG_MAX];
	int ptr;
};

struct _plughandle_t {
	LV2_URID_Map *map;
	LV2_Atom_Forge forge;
	LV2_Atom_Forge_Ref ref;

	LV2_URID vm_graph;

	LV2_Log_Log *log;
	LV2_Log_Logger logger;

	const LV2_Atom_Sequence *control;
	LV2_Atom_Sequence *notify;
	const float *in [CTRL_MAX];
	float *out [CTRL_MAX];

	num_t in0 [CTRL_MAX];
	num_t out0 [CTRL_MAX];

	PROPS_T(props, MAX_NPROPS);
	plugstate_t state;
	plugstate_t stash;

	uint32_t graph_size;
	vm_api_impl_t api [OP_MAX];

	vm_stack_t stack;
	bool needs_recalc;
	bool needs_sync;
	vm_status_t status;

	int64_t off;

	vm_command_t cmds [ITEMS_MAX];

	timely_t timely;
};

static inline void
_stack_clear(vm_stack_t *stack)
{
	memset(stack->slots, 0x0, SLOT_MAX * sizeof(num_t));
	stack->ptr = 0;
}

static inline void
_stack_push(vm_stack_t *stack, num_t val)
{
	stack->ptr = (stack->ptr - 1) & SLOT_MASK;

	stack->slots[stack->ptr] = val;
}

static inline num_t
_stack_pop(vm_stack_t *stack)
{
	const num_t val = stack->slots[stack->ptr];

	stack->ptr = (stack->ptr + 1) & SLOT_MASK;

	return val;
}

static inline void
_stack_push_num(vm_stack_t *stack, const num_t *val, int num)
{
	for(int i = 0; i < num; i++)
		stack->slots[(stack->ptr - i - 1) & SLOT_MASK] = val[i];

	stack->ptr = (stack->ptr - num) & SLOT_MASK;
}

static inline void
_stack_pop_num(vm_stack_t *stack, num_t *val, int num)
{
	for(int i = 0; i < num; i++)
		val[i] = stack->slots[(stack->ptr + i) & SLOT_MASK];

	stack->ptr = (stack->ptr + num) & SLOT_MASK;
}

static inline num_t
_stack_peek(vm_stack_t *stack)
{
	return stack->slots[stack->ptr];
}

static void
_intercept_graph(void *data, LV2_Atom_Forge *forge, int64_t frames,
	props_event_t event, props_impl_t *impl)
{
	plughandle_t *handle = data;

	handle->graph_size = impl->value.size;
	handle->needs_recalc = true;

	handle->status = vm_deserialize(handle->api, &handle->forge, handle->cmds,
		impl->value.size, impl->value.body);

	handle->needs_sync = true;
	handle->needs_recalc = true;
}

static const props_def_t defs [MAX_NPROPS] = {
	{
		.property = VM__graph,
		.offset = offsetof(plugstate_t, graph),
		.type = LV2_ATOM__Tuple,
		.max_size = GRAPH_SIZE,
		.event_mask = PROP_EVENT_WRITE,
		.event_cb = _intercept_graph,
	}
};

static void
_cb(timely_t *timely, int64_t frames, LV2_URID type, void *data)
{
	plughandle_t *handle = data;

	if(handle->status & VM_STATUS_HAS_TIME)
		handle->needs_recalc = true;
}

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

	for(unsigned i=0; features[i]; i++)
	{
		if(!strcmp(features[i]->URI, LV2_URID__map))
			handle->map = features[i]->data;
		else if(!strcmp(features[i]->URI, LV2_LOG__log))
			handle->log = features[i]->data;
	}

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

	if(handle->log)
		lv2_log_logger_init(&handle->logger, handle->map, handle->log);

	handle->vm_graph = handle->map->map(handle->map->handle, VM__graph);

	lv2_atom_forge_init(&handle->forge, handle->map);
	vm_api_init(handle->api, handle->map);
	const timely_mask_t mask = TIMELY_MASK_BAR_BEAT
		| TIMELY_MASK_BAR
		| TIMELY_MASK_BEAT_UNIT
		| TIMELY_MASK_BEATS_PER_BAR
		| TIMELY_MASK_BEATS_PER_MINUTE
		| TIMELY_MASK_FRAME
		| TIMELY_MASK_FRAMES_PER_SECOND
		| TIMELY_MASK_SPEED
		| TIMELY_MASK_BAR_BEAT_WHOLE
		| TIMELY_MASK_BAR_WHOLE;
	timely_init(&handle->timely, handle->map, rate, mask, _cb, handle);

	if(!props_init(&handle->props, MAX_NPROPS, descriptor->URI, handle->map, handle))
	{
		fprintf(stderr, "props_init failed\n");
		free(handle);
		return NULL;
	}

	if(!props_register(&handle->props, defs, MAX_NPROPS, &handle->state, &handle->stash))
	{
		fprintf(stderr, "props_register failed\n");
		free(handle);
		return NULL;
	}

	handle->needs_recalc = true;

	return handle;
}

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

	switch(port)
	{
		case 0:
			handle->control = data;
			break;
		case 1:
			handle->notify = data;
			break;
		case 2:
		case 3:
		case 4:
		case 5:
		case 6:
		case 7:
		case 8:
		case 9:
			handle->in[port - 2] = data;
			break;
		case 10:
		case 11:
		case 12:
		case 13:
		case 14:
		case 15:
		case 16:
		case 17:
			handle->out[port - 10] = data;
			break;
	}
}

#define CLIP(a, v, b) fmin(fmax(a, v), b)

static void
run(LV2_Handle instance, uint32_t nsamples)
{
	plughandle_t *handle = instance;

	const uint32_t capacity = handle->notify->atom.size;
	LV2_Atom_Forge_Frame frame;
	lv2_atom_forge_set_buffer(&handle->forge, (uint8_t *)handle->notify, capacity);
	handle->ref = lv2_atom_forge_sequence_head(&handle->forge, &frame, 0);

	int64_t last_t = 0;
	LV2_ATOM_SEQUENCE_FOREACH(handle->control, ev)
	{
		const LV2_Atom *atom= &ev->body;
		const LV2_Atom_Object *obj = (const LV2_Atom_Object *)&ev->body;

		if(!timely_advance(&handle->timely, obj, last_t, ev->time.frames))
			props_advance(&handle->props, &handle->forge, ev->time.frames, obj, &handle->ref);

		last_t = ev->time.frames;
	}
	timely_advance(&handle->timely, NULL, last_t, nsamples);

	if(handle->needs_sync)
	{
		props_set(&handle->props, &handle->forge, nsamples - 1, handle->vm_graph, &handle->ref);
		handle->needs_sync = false;
	}

	for(unsigned i = 0; i < CTRL_MAX; i++)
	{
		const float in1 = CLIP(VM_MIN, *handle->in[i], VM_MAX);
		if(handle->in0[i] != in1)
		{
			handle->needs_recalc = true;
			handle->in0[i] = in1;

			LV2_Atom_Forge_Frame tup_frame;
			if(handle->ref)
				handle->ref = lv2_atom_forge_frame_time(&handle->forge, nsamples - 1);
			if(handle->ref)
				handle->ref = lv2_atom_forge_tuple(&handle->forge, &tup_frame);
			if(handle->ref)
				handle->ref = lv2_atom_forge_int(&handle->forge, i + 2);
			if(handle->ref)
				handle->ref = lv2_atom_forge_float(&handle->forge, in1);
			if(handle->ref)
				lv2_atom_forge_pop(&handle->forge, &tup_frame);
		}
	}

	if(handle->needs_recalc || (handle->status & VM_STATUS_HAS_RAND) )
	{
		_stack_clear(&handle->stack);

		for(unsigned i = 0; i < ITEMS_MAX; i++)
		{
			vm_command_t *cmd = &handle->cmds[i];
			bool terminate = false;

			switch(cmd->type)
			{
				case COMMAND_BOOL:
				{
					const num_t c = cmd->i32;
					_stack_push(&handle->stack, c);
				} break;
				case COMMAND_INT:	
				{
					const num_t c = cmd->i32;
					_stack_push(&handle->stack, c);
				} break;
				case COMMAND_FLOAT:
				{
					const num_t c = cmd->f32;
					_stack_push(&handle->stack, c);
				} break;
				case COMMAND_OPCODE:
				{
					switch(cmd->op)
					{
						case OP_CTRL:
						{
							const int idx = floor(_stack_pop(&handle->stack));
							const num_t c = handle->in0[idx & CTRL_MASK];
							_stack_push(&handle->stack, c);
						} break;
						case OP_PUSH:
						{
							const num_t c = _stack_peek(&handle->stack);
							_stack_push(&handle->stack, c);
						} break;
						case OP_POP:
						{
							const num_t c = _stack_pop(&handle->stack);
							(void)c;
						} break;
						case OP_SWAP:
						{
							num_t ab [2];
							_stack_pop_num(&handle->stack, ab, 2);
							_stack_push_num(&handle->stack, ab, 2);
						} break;
						case OP_STORE:
						{
							num_t ab [2];
							_stack_pop_num(&handle->stack, ab, 2);
							const int idx = floorf(ab[0]);
							handle->stack.regs[idx & REG_MASK] = ab[1];
						} break;
						case OP_LOAD:
						{
							const num_t a = _stack_pop(&handle->stack);
							const int idx = floorf(a);
							const num_t c = handle->stack.regs[idx & REG_MASK];
							_stack_push(&handle->stack, c);
						} break;
						case OP_BREAK:
						{
							const bool a = _stack_pop(&handle->stack);
							if(a)
								terminate = true;
						} break;
						/* dangerous
						case OP_GOTO:
						{
							const int idx = _stack_pop(&handle->stack);
							i = (idx - 1) & ITEMS_MASK;
						} break;
						*/

						case OP_RAND:
						{
							const num_t c = (num_t)rand() / RAND_MAX;
							_stack_push(&handle->stack, c);
						} break;

						case OP_ADD:
						{
							num_t ab [2];
							_stack_pop_num(&handle->stack, ab, 2);
							const num_t c = ab[1] + ab[0];
							_stack_push(&handle->stack, c);
						} break;
						case OP_SUB:
						{
							num_t ab [2];
							_stack_pop_num(&handle->stack, ab, 2);
							const num_t c = ab[1] - ab[0];
							_stack_push(&handle->stack, c);
						} break;
						case OP_MUL:
						{
							num_t ab [2];
							_stack_pop_num(&handle->stack, ab, 2);
							const num_t c = ab[1] * ab[0];
							_stack_push(&handle->stack, c);
						} break;
						case OP_DIV:
						{
							num_t ab [2];
							_stack_pop_num(&handle->stack, ab, 2);
							const num_t c = ab[0] == 0.0
								? 0.0
								: ab[1] / ab[0];
							_stack_push(&handle->stack, c);
						} break;
						case OP_MOD:
						{
							num_t ab [2];
							_stack_pop_num(&handle->stack, ab, 2);
							const num_t c = ab[0] == 0.0
								? 0.0
								: fmod(ab[1], ab[0]);
							_stack_push(&handle->stack, c);
						} break;
						case OP_POW:
						{
							num_t ab [2];
							_stack_pop_num(&handle->stack, ab, 2);
							const num_t c = pow(ab[1], ab[0]);
							_stack_push(&handle->stack, c);
						} break;

						case OP_NEG:
						{
							const num_t a = _stack_pop(&handle->stack);
							const num_t c = -a;
							_stack_push(&handle->stack, c);
						} break;
						case OP_ABS:
						{
							const num_t a = _stack_pop(&handle->stack);
							const num_t c = fabs(a);
							_stack_push(&handle->stack, c);
						} break;
						case OP_SQRT:
						{
							const num_t a = _stack_pop(&handle->stack);
							const num_t c = sqrt(a);
							_stack_push(&handle->stack, c);
						} break;
						case OP_CBRT:
						{
							const num_t a = _stack_pop(&handle->stack);
							const num_t c = cbrt(a);
							_stack_push(&handle->stack, c);
						} break;

						case OP_FLOOR:
						{
							const num_t a = _stack_pop(&handle->stack);
							const num_t c = floor(a);
							_stack_push(&handle->stack, c);
						} break;
						case OP_CEIL:
						{
							const num_t a = _stack_pop(&handle->stack);
							const num_t c = ceil(a);
							_stack_push(&handle->stack, c);
						} break;
						case OP_ROUND:
						{
							const num_t a = _stack_pop(&handle->stack);
							const num_t c = round(a);
							_stack_push(&handle->stack, c);
						} break;
						case OP_RINT:
						{
							const num_t a = _stack_pop(&handle->stack);
							const num_t c = rint(a);
							_stack_push(&handle->stack, c);
						} break;
						case OP_TRUNC:
						{
							const num_t a = _stack_pop(&handle->stack);
							const num_t c = trunc(a);
							_stack_push(&handle->stack, c);
						} break;
						case OP_MODF:
						{
							const num_t a = _stack_pop(&handle->stack);
							num_t d;
							const num_t c = modf(a, &d);
							_stack_push(&handle->stack, c);
							_stack_push(&handle->stack, d);
						} break;

						case OP_EXP:
						{
							const num_t a = _stack_pop(&handle->stack);
							const num_t c = exp(a);
							_stack_push(&handle->stack, c);
						} break;
						case OP_EXP_2:
						{
							const num_t a = _stack_pop(&handle->stack);
							const num_t c = exp2(a);
							_stack_push(&handle->stack, c);
						} break;
						case OP_LD_EXP:
						{
							num_t ab [2];
							_stack_pop_num(&handle->stack, ab, 2);
							const num_t c = ldexp(ab[1], ab[0]);
							_stack_push(&handle->stack, c);
						} break;
						case OP_FR_EXP:
						{
							const num_t a = _stack_pop(&handle->stack);
							int d;
							const num_t c = frexp(a, &d);
							_stack_push(&handle->stack, c);
							_stack_push(&handle->stack, d);
						} break;
						case OP_LOG:
						{
							const num_t a = _stack_pop(&handle->stack);
							const num_t c = log(a);
							_stack_push(&handle->stack, c);
						} break;
						case OP_LOG_2:
						{
							const num_t a = _stack_pop(&handle->stack);
							const num_t c = log2(a);
							_stack_push(&handle->stack, c);
						} break;
						case OP_LOG_10:
						{
							const num_t a = _stack_pop(&handle->stack);
							const num_t c = log10(a);
							_stack_push(&handle->stack, c);
						} break;

						case OP_PI:
						{
							num_t c = M_PI;
							_stack_push(&handle->stack, c);
						} break;
						case OP_SIN:
						{
							const num_t a = _stack_pop(&handle->stack);
							const num_t c = sin(a);
							_stack_push(&handle->stack, c);
						} break;
						case OP_COS:
						{
							const num_t a = _stack_pop(&handle->stack);
							const num_t c = cos(a);
							_stack_push(&handle->stack, c);
						} break;
						case OP_TAN:
						{
							const num_t a = _stack_pop(&handle->stack);
							const num_t c = tan(a);
							_stack_push(&handle->stack, c);
						} break;
						case OP_ASIN:
						{
							const num_t a = _stack_pop(&handle->stack);
							const num_t c = asin(a);
							_stack_push(&handle->stack, c);
						} break;
						case OP_ACOS:
						{
							const num_t a = _stack_pop(&handle->stack);
							const num_t c = acos(a);
							_stack_push(&handle->stack, c);
						} break;
						case OP_ATAN:
						{
							const num_t a = _stack_pop(&handle->stack);
							const num_t c = atan(a);
							_stack_push(&handle->stack, c);
						} break;
						case OP_ATAN2:
						{
							num_t ab [2];
							_stack_pop_num(&handle->stack, ab, 2);
							const num_t c = atan2(ab[1], ab[0]);
							_stack_push(&handle->stack, c);
						} break;
						case OP_SINH:
						{
							const num_t a = _stack_pop(&handle->stack);
							const num_t c = sinh(a);
							_stack_push(&handle->stack, c);
						} break;
						case OP_COSH:
						{
							const num_t a = _stack_pop(&handle->stack);
							const num_t c = cosh(a);
							_stack_push(&handle->stack, c);
						} break;
						case OP_TANH:
						{
							const num_t a = _stack_pop(&handle->stack);
							const num_t c = tanh(a);
							_stack_push(&handle->stack, c);
						} break;
						case OP_ASINH:
						{
							const num_t a = _stack_pop(&handle->stack);
							const num_t c = asinh(a);
							_stack_push(&handle->stack, c);
						} break;
						case OP_ACOSH:
						{
							const num_t a = _stack_pop(&handle->stack);
							const num_t c = acosh(a);
							_stack_push(&handle->stack, c);
						} break;
						case OP_ATANH:
						{
							const num_t a = _stack_pop(&handle->stack);
							const num_t c = atanh(a);
							_stack_push(&handle->stack, c);
						} break;

						case OP_EQ:
						{
							num_t ab [2];
							_stack_pop_num(&handle->stack, ab, 2);
							const bool c = ab[1] == ab[0];
							_stack_push(&handle->stack, c);
						} break;
						case OP_LT:
						{
							num_t ab [2];
							_stack_pop_num(&handle->stack, ab, 2);
							const bool c = ab[1] < ab[0];
							_stack_push(&handle->stack, c);
						} break;
						case OP_GT:
						{
							num_t ab [2];
							_stack_pop_num(&handle->stack, ab, 2);
							const bool c = ab[1] > ab[0];
							_stack_push(&handle->stack, c);
						} break;
						case OP_LE:
						{
							num_t ab [2];
							_stack_pop_num(&handle->stack, ab, 2);
							const bool c = ab[1] <= ab[0];
							_stack_push(&handle->stack, c);
						} break;
						case OP_GE:
						{
							num_t ab [2];
							_stack_pop_num(&handle->stack, ab, 2);
							const bool c = ab[1] >= ab[0];
							_stack_push(&handle->stack, c);
						} break;
						case OP_TER:
						{
							num_t ab [3];
							_stack_pop_num(&handle->stack, ab, 3);
							const bool c = ab[0];
							_stack_push(&handle->stack, c ? ab[2] : ab[1]);
						} break;
						case OP_MINI:
						{
							num_t ab [2];
							_stack_pop_num(&handle->stack, ab, 2);
							const num_t c = fmin(ab[1], ab[0]);
							_stack_push(&handle->stack, c);
						} break;
						case OP_MAXI:
						{
							num_t ab [2];
							_stack_pop_num(&handle->stack, ab, 2);
							const num_t c = fmax(ab[1], ab[0]);
							_stack_push(&handle->stack, c);
						} break;

						case OP_AND:
						{
							num_t ab [2];
							_stack_pop_num(&handle->stack, ab, 2);
							const bool c = ab[1] && ab[0];
							_stack_push(&handle->stack, c);
						} break;
						case OP_OR:
						{
							num_t ab [2];
							_stack_pop_num(&handle->stack, ab, 2);
							const bool c = ab[1] || ab[0];
							_stack_push(&handle->stack, c);
						} break;

						case OP_NOT:
						{
							const int a = _stack_pop(&handle->stack);
							const bool c = !a;
							_stack_push(&handle->stack, c);
						} break;
						case OP_BAND:
						{
							num_t ab [2];
							_stack_pop_num(&handle->stack, ab, 2);
							const unsigned a = ab[1];
							const unsigned b = ab[0];
							const unsigned c = a & b;
							_stack_push(&handle->stack, c);
						} break;
						case OP_BOR:
						{
							num_t ab [2];
							_stack_pop_num(&handle->stack, ab, 2);
							const unsigned a = ab[1];
							const unsigned b = ab[0];
							const unsigned c = a | b;
							_stack_push(&handle->stack, c);
						} break;
						case OP_BNOT:
						{
							const unsigned a = _stack_pop(&handle->stack);
							const unsigned c = ~a;
							_stack_push(&handle->stack, c);
						} break;
						case OP_LSHIFT:
						{
							num_t ab [2];
							_stack_pop_num(&handle->stack, ab, 2);
							const unsigned a = ab[1];
							const unsigned b = ab[0];
							const unsigned c = a <<  b;
							_stack_push(&handle->stack, c);
						} break;
						case OP_RSHIFT:
						{
							num_t ab [2];
							_stack_pop_num(&handle->stack, ab, 2);
							const unsigned a = ab[1];
							const unsigned b = ab[0];
							const unsigned c = a >>  b;
							_stack_push(&handle->stack, c);
						} break;

						// time
						case OP_BAR_BEAT:
						{
							const num_t c = TIMELY_BAR_BEAT(&handle->timely);
							_stack_push(&handle->stack, c);
						} break;
						case OP_BAR:
						{
							const num_t c = TIMELY_BAR(&handle->timely);
							_stack_push(&handle->stack, c);
						} break;
						case OP_BEAT:
						{
							const num_t bar = TIMELY_BAR(&handle->timely);
							const num_t beats_per_bar = TIMELY_BEATS_PER_BAR(&handle->timely);
							const num_t bar_beat = TIMELY_BAR_BEAT(&handle->timely);
							const num_t c = bar*beats_per_bar + bar_beat;
							_stack_push(&handle->stack, c);
						} break;
						case OP_BEAT_UNIT:
						{
							const num_t c = TIMELY_BEAT_UNIT(&handle->timely);
							_stack_push(&handle->stack, c);
						} break;
						case OP_BPB:
						{
							const num_t c = TIMELY_BEATS_PER_BAR(&handle->timely);
							_stack_push(&handle->stack, c);
						} break;
						case OP_BPM:
						{
							const num_t c = TIMELY_BEATS_PER_MINUTE(&handle->timely);
							_stack_push(&handle->stack, c);
						} break;
						case OP_FRAME:
						{
							const num_t c = TIMELY_FRAME(&handle->timely);
							_stack_push(&handle->stack, c);
						} break;
						case OP_FPS:
						{
							const num_t c = TIMELY_FRAMES_PER_SECOND(&handle->timely);
							_stack_push(&handle->stack, c);
						} break;
						case OP_SPEED:
						{
							const num_t c = TIMELY_SPEED(&handle->timely);
							_stack_push(&handle->stack, c);
						} break;

						case OP_NOP:
						{
							terminate = true;
						} break;
						case OP_MAX:
							break;
					}
				} break;
				case COMMAND_NOP:
				{
					terminate = true;
				} break;
				case COMMAND_MAX:
					break;
			}

			if(terminate)
				break;
		}

		_stack_pop_num(&handle->stack, handle->out0, CTRL_MAX);
#if 0
		for(unsigned i = 0; i < CTRL_MAX; i++)
			printf("out %u: %f\n", i, handle->out0[i]);
#endif

		handle->needs_recalc = false;
	}

	for(unsigned i = 0; i < CTRL_MAX; i++)
	{
		const float out1 = CLIP(VM_MIN, handle->out0[i], VM_MAX);

		if(*handle->out[i] != out1)
		{
			*handle->out[i] = out1;

			LV2_Atom_Forge_Frame tup_frame;
			if(handle->ref)
				handle->ref = lv2_atom_forge_frame_time(&handle->forge, nsamples - 1);
			if(handle->ref)
				handle->ref = lv2_atom_forge_tuple(&handle->forge, &tup_frame);
			if(handle->ref)
				handle->ref = lv2_atom_forge_int(&handle->forge, i + 10);
			if(handle->ref)
				handle->ref = lv2_atom_forge_float(&handle->forge, out1);
			if(handle->ref)
				lv2_atom_forge_pop(&handle->forge, &tup_frame);
		}
	}

	if(handle->ref)
		handle->ref = lv2_atom_forge_frame_time(&handle->forge, nsamples - 1);
	if(handle->ref)
		handle->ref = lv2_atom_forge_long(&handle->forge, handle->off);

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

	handle->off += nsamples;
}

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

	free(handle);
}

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;

	return props_save(&handle->props, 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;

	return props_restore(&handle->props, retrieve, state, flags, features);
}

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

static inline LV2_Worker_Status
_work(LV2_Handle instance, LV2_Worker_Respond_Function respond,
LV2_Worker_Respond_Handle worker, uint32_t size, const void *body)
{
	plughandle_t *handle = instance;

	return props_work(&handle->props, respond, worker, size, body);
}

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

	return props_work_response(&handle->props, size, body);
}

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

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

	return NULL;
}

static const LV2_Descriptor vm_control = {
	.URI            = VM_PREFIX"control",
	.instantiate    = instantiate,
	.connect_port   = connect_port,
	.activate       = NULL,
	.run            = run,
	.deactivate     = NULL,
	.cleanup        = cleanup,
	.extension_data = extension_data
};

LV2_SYMBOL_EXPORT const LV2_Descriptor*
lv2_descriptor(uint32_t index)
{
	switch(index)
	{
		case 0:
			return &vm_control;
		default:
			return NULL;
	}
}