aboutsummaryrefslogtreecommitdiff
path: root/bin/synthpod_alsa.c
blob: 9caa05906b8e8b32a159e6cc983503a59b10652d (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
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
/*
 * Copyright (c) 2015-2016 Hanspeter Portner (dev@open-music-kontrollers.ch)
 *
 * 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 3 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.
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <ctype.h>
#include <math.h>

#if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__DragonFly__) || defined(__OpenBSD__)
#	include <pthread_np.h>
typedef cpuset_t cpu_set_t;
#endif

#include <alsa/asoundlib.h>
#include <pcmi.h>

#include <synthpod_bin.h>

#define MIDI_SEQ_SIZE 2048
#define NANO_SECONDS 1000000000

typedef enum _chan_type_t chan_type_t;
typedef struct _prog_t prog_t;
typedef struct _chan_t chan_t;

enum _chan_type_t {
	CHAN_TYPE_PCMI,
	CHAN_TYPE_MIDI
};

struct _chan_t {
	chan_type_t type;

	union {
		struct {
			//TODO
		} audio;
		struct {
			int port;
			snd_midi_event_t *trans;

			LV2_Atom_Forge forge;
			LV2_Atom_Forge_Frame frame;
			LV2_Atom_Forge_Ref ref;
			LV2_Atom_Sequence *seq_in;
			int64_t last;
		} midi;
	};
};

struct _prog_t {
	bin_t bin;
	
	LV2_Atom_Forge forge;
	
	LV2_URID midi_MidiEvent;

	pcmi_t *pcmi;
	snd_seq_t *seq;
	int queue;
	uint8_t m [MIDI_SEQ_SIZE];

	save_state_t save_state;
	atomic_int kill;
	pthread_t thread;

	uint32_t srate;
	uint32_t frsize;
	uint32_t nfrags;
	bool twochan;
	bool debug;
	bool do_play;
	bool do_capt;
	uint32_t seq_size;

	const char *io_name;
	const char *play_name;
	const char *capt_name;

	LV2_OSC_Schedule osc_sched;
	struct timespec cur_ntp;
	struct timespec nxt_ntp;
	struct {
		uint64_t cur_frames;
		uint64_t ref_frames;
		double dT;
		double dTm1;
	} cycle;
};

static inline void
_ntp_now(cross_clock_t *clk, struct timespec *ntp)
{
	cross_clock_gettime(clk, ntp);
	ntp->tv_sec += JAN_1970; // convert NTP to OSC time
}

static inline void
_ntp_clone(struct timespec *dst, struct timespec *src)
{
	dst->tv_sec = src->tv_sec;
	dst->tv_nsec = src->tv_nsec;
}

static inline void
_ntp_add_nanos(struct timespec *ntp, uint64_t nanos)
{
	ntp->tv_nsec += nanos;
	while(ntp->tv_nsec >= NANO_SECONDS) // has overflowed
	{
		ntp->tv_sec += 1;
		ntp->tv_nsec -= NANO_SECONDS;
	}
}

static inline double
_ntp_diff(struct timespec *from, struct timespec *to)
{
	double diff = to->tv_sec;
	diff -= from->tv_sec;
	diff += 1e-9 * to->tv_nsec;
	diff -= 1e-9 * from->tv_nsec;

	return diff;
}

__realtime static inline void 
_process(prog_t *handle)
{
	pcmi_t *pcmi = handle->pcmi;
	bin_t *bin = &handle->bin;
	sp_app_t *app = bin->app;

	const uint32_t nsamples = handle->frsize;
	int nplay = pcmi_nplay(pcmi);
	int ncapt = pcmi_ncapt(pcmi);
	int play_num;
	int capt_num;

	const uint64_t nanos_per_period = (uint64_t)nsamples * NANO_SECONDS / handle->srate;
	handle->cycle.cur_frames = 0; // initialize frame counter
	_ntp_now(&bin->clk_real, &handle->nxt_ntp);

	snd_seq_queue_status_t *stat = NULL;
	const snd_seq_real_time_t *real_time = NULL;
	snd_seq_real_time_t ref_time;
	snd_seq_queue_status_malloc(&stat);

	pcmi_pcm_start(handle->pcmi);
	while(!atomic_load_explicit(&handle->kill, memory_order_relaxed))
	{
		uint32_t na = pcmi_pcm_wait(pcmi);

		// current time is next time from last cycle
		_ntp_clone(&handle->cur_ntp, &handle->nxt_ntp);

		// extrapolate new nxt_ntp
		struct timespec nxt_ntp;
		_ntp_now(&bin->clk_real, &nxt_ntp);
		_ntp_clone(&handle->nxt_ntp, &nxt_ntp);

		// reset ref_frames
		handle->cycle.ref_frames = handle->cycle.cur_frames;

		// calculate apparent period
		_ntp_add_nanos(&nxt_ntp, nanos_per_period);
		double diff = _ntp_diff(&handle->cur_ntp, &nxt_ntp);

		// calculate apparent samples per period
		handle->cycle.dT = nsamples / diff;
		handle->cycle.dTm1 = 1.0 / handle->cycle.dT;

		// get ALSA sequencer reference timestamp
		snd_seq_get_queue_status(handle->seq, handle->queue, stat);
		real_time = snd_seq_queue_status_get_real_time(stat);
		ref_time.tv_sec = real_time->tv_sec;
		ref_time.tv_nsec = real_time->tv_nsec;

		uint32_t pos = 0;
		for( ; na >= nsamples;
				na -= nsamples,
				handle->cycle.ref_frames += nsamples,
				pos += nsamples,
				_ntp_add_nanos(&handle->nxt_ntp, nanos_per_period) )
		{
			const sp_app_system_source_t *sources = sp_app_get_system_sources(app);
	
			if(sp_app_bypassed(app))
			{
				//const sp_app_system_sink_t *sinks = sp_app_get_system_sinks(app);

				//fprintf(stderr, "app is bypassed\n");

				pcmi_pcm_idle(pcmi, nsamples);

				bin_process_pre(bin, nsamples, true);
				bin_process_post(bin);

				continue;
			}

			// fill input buffers
			if(ncapt)
				pcmi_capt_init(pcmi, nsamples);
			capt_num = 0;
			for(const sp_app_system_source_t *source=sources;
				source->type != SYSTEM_PORT_NONE;
				source++)
			{
				chan_t *chan = source->sys_port;

				switch(source->type)
				{
					case SYSTEM_PORT_NONE:
					case SYSTEM_PORT_CONTROL:
					case SYSTEM_PORT_CV:
					case SYSTEM_PORT_OSC:
						break;

					case SYSTEM_PORT_AUDIO:
					{

						if(capt_num < ncapt)
							pcmi_capt_chan(pcmi, capt_num++, source->buf, nsamples);

						break;
					}
					
					case SYSTEM_PORT_MIDI:
					{
						void *seq_in = source->buf;

						// initialize LV2 event port
						LV2_Atom_Forge *forge = &chan->midi.forge;
						chan->midi.seq_in = seq_in; // needed for lv2_atom_sequence_clear
						lv2_atom_forge_set_buffer(forge, seq_in, SEQ_SIZE);
						chan->midi.ref = lv2_atom_forge_sequence_head(forge, &chan->midi.frame, 0);
						chan->midi.last = 0;

						break;
					}

					case SYSTEM_PORT_COM:
					{
						void *seq_in = source->buf;

						LV2_Atom_Forge *forge = &handle->forge;
						LV2_Atom_Forge_Frame frame;
						lv2_atom_forge_set_buffer(forge, seq_in, SEQ_SIZE);
						LV2_Atom_Forge_Ref ref = lv2_atom_forge_sequence_head(forge, &frame, 0);

						const LV2_Atom_Object *obj;
						size_t size;
						while((obj = varchunk_read_request(bin->app_from_com, &size)))
						{
							if(ref)
								ref = lv2_atom_forge_frame_time(forge, 0);
							if(ref)
								ref = lv2_atom_forge_write(forge, obj, size);

							varchunk_read_advance(bin->app_from_com);
						}
						if(ref)
							lv2_atom_forge_pop(forge, &frame);
						else
							lv2_atom_sequence_clear(seq_in);

						break;
					}
				}
			}

			// read incoming MIDI and dispatch to corresponding MIDI port
			{
				snd_seq_event_t *sev;
				while(snd_seq_event_input_pending(handle->seq, 1) > 0)
				{
					chan_t *chan = NULL;

					// get event
					snd_seq_event_input(handle->seq, &sev);

					// search for matching port
					for(const sp_app_system_source_t *source=sources;
						source->type != SYSTEM_PORT_NONE;
						source++)
					{
						if(source->type == SYSTEM_PORT_MIDI)
						{
							chan_t *_chan = source->sys_port;

							if(_chan->midi.port == sev->dest.port)
							{
								chan = _chan;
								break; // right port found, break out of loop 
							}
						}
					}

					if(chan)
					{
						long len;
						if((len = snd_midi_event_decode(chan->midi.trans, handle->m,
							MIDI_SEQ_SIZE, sev)) > 0)
						{
							LV2_Atom_Forge *forge = &chan->midi.forge;

							const bool is_real = snd_seq_ev_is_real(sev);
							const bool is_abs = snd_seq_ev_is_abstime(sev);
							assert(is_real);

							volatile double dd = sev->time.time.tv_sec;
							if(is_abs) // calculate relative time difference
								dd -= ref_time.tv_sec;
							dd += sev->time.time.tv_nsec * 1e-9;
							if(is_abs) // calculate relative time difference
								dd -= ref_time.tv_nsec * 1e-9;

							int64_t frames = dd * handle->srate - pos;
							if(frames < 0)
								frames = 0;
							else if(frames >= nsamples)
								frames = nsamples - 1; //TODO report this

							if(frames < chan->midi.last)
								frames = chan->midi.last; // frame time must be increasing
							else
								chan->midi.last = frames;

							// fix up noteOn(vel=0) -> noteOff(vel=0)
							if(  (len == 3) && ( (handle->m[0] & 0xf0) == 0x90)
								&& (handle->m[2] == 0x0) )
							{
								handle->m[0] = 0x80 | (handle->m[0] & 0x0f);
								handle->m[2] = 0x0;
							}

							if(chan->midi.ref)
								chan->midi.ref = lv2_atom_forge_frame_time(forge, frames);
							if(chan->midi.ref)
								chan->midi.ref = lv2_atom_forge_atom(forge, len, handle->midi_MidiEvent);
							if(chan->midi.ref)
								chan->midi.ref = lv2_atom_forge_write(forge, handle->m, len);
						}
						else
						{
							bin_log_trace(bin, "%s: MIDI event decode failed\n", __func__);
						}
					}
					else
					{
						bin_log_trace(bin, "%s: no matching port for MIDI event\n", __func__);
					}

					if(snd_seq_free_event(sev))
					{
						bin_log_trace(bin, "%s: MIDI event free failed\n", __func__);
					}
				}
			}
						
			for(const sp_app_system_source_t *source=sources;
				source->type != SYSTEM_PORT_NONE;
				source++)
			{
				chan_t *chan = source->sys_port;

				if(source->type == SYSTEM_PORT_MIDI)
				{
					LV2_Atom_Forge *forge = &chan->midi.forge;

					// finalize LV2 event port
					if(chan->midi.ref)
						lv2_atom_forge_pop(forge, &chan->midi.frame);
					else
						lv2_atom_sequence_clear(chan->midi.seq_in);
				}
			}
			if(ncapt)
				pcmi_capt_done(pcmi, nsamples);
	
			bin_process_pre(bin, nsamples, false);

			const sp_app_system_sink_t *sinks = sp_app_get_system_sinks(app);

			// fill output buffers
			if(nplay)
				pcmi_play_init(pcmi, nsamples);
			play_num = 0;
			for(const sp_app_system_sink_t *sink=sinks;
				sink->type != SYSTEM_PORT_NONE;
				sink++)
			{
				chan_t *chan = sink->sys_port;

				switch(sink->type)
				{
					case SYSTEM_PORT_NONE:
					case SYSTEM_PORT_CONTROL:
					case SYSTEM_PORT_CV:
					case SYSTEM_PORT_OSC:
						break;

					case SYSTEM_PORT_AUDIO:
					{

						if(play_num < nplay)
							pcmi_play_chan(pcmi, play_num++, sink->buf, nsamples);

						break;
					}

					case SYSTEM_PORT_MIDI:
					{
						const LV2_Atom_Sequence *seq_out = sink->buf;

						LV2_ATOM_SEQUENCE_FOREACH(seq_out, ev)
						{
							const LV2_Atom *atom = &ev->body;

							if(atom->type != handle->midi_MidiEvent)
								continue; // ignore non-MIDI events

							snd_seq_event_t sev;
							snd_seq_ev_clear(&sev);
							long consumed;
							const uint8_t *buf = LV2_ATOM_BODY_CONST(atom);
							if( (consumed = snd_midi_event_encode(chan->midi.trans, buf, atom->size, &sev)) != atom->size)
							{
								bin_log_trace(bin, "%s: MIDI encode event failed: %li\n", __func__, consumed);
								continue;
							}

							// absolute timestamp
							volatile double dd = (double)(ev->time.frames + pos) / handle->srate;
							double sec;
							double nsec = modf(dd, &sec);
							struct snd_seq_real_time rtime = {
								.tv_sec = ref_time.tv_sec + sec,
								.tv_nsec = ref_time.tv_nsec + nsec
							};
							while(rtime.tv_nsec >= NANO_SECONDS) // handle overflow
							{
								rtime.tv_sec += 1;
								rtime.tv_nsec -= NANO_SECONDS;
							}

							// schedule midi
							snd_seq_ev_set_source(&sev, chan->midi.port);
							snd_seq_ev_set_subs(&sev); // set broadcasting to subscribers
							snd_seq_ev_schedule_real(&sev, handle->queue, 0, &rtime); // absolute scheduling
							snd_seq_event_output(handle->seq, &sev);
							//snd_seq_drain_output(handle->seq);
						}

						break;
					}

					case SYSTEM_PORT_COM:
					{
						const LV2_Atom_Sequence *seq_out = sink->buf;

						LV2_ATOM_SEQUENCE_FOREACH(seq_out, ev)
						{
							const LV2_Atom *atom = &ev->body;
							
							// try do process events directly
							bin->advance_ui = sp_app_from_ui(bin->app, atom);
							if(!bin->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(bin->app_from_app, size)))
								{
									memcpy(ptr, atom, size);
									varchunk_write_advance(bin->app_from_app, size);
								}
								else
								{
									bin_log_trace(bin, "%s: app_from_app ringbuffer full\n", __func__);
									//FIXME
								}
							}
						}
						break;
					}
				}
			}
			snd_seq_drain_output(handle->seq); //TODO is this rt-safe?

			// clear unused output channels
			while(play_num<nplay)
			{
				pcmi_clear_chan(pcmi, play_num++, nsamples);
			}

			if(nplay)
				pcmi_play_done(pcmi, nsamples);
		
			bin_process_post(bin);
		}

		// increase cur_frames
		handle->cycle.cur_frames = handle->cycle.ref_frames;
		//sched_yield();
	}
	pcmi_pcm_stop(handle->pcmi);
	
	snd_seq_queue_status_free(stat);
}

__non_realtime static void *
_rt_thread(void *data)
{
	prog_t *handle = data;
	bin_t *bin = &handle->bin;

	bin->dsp_thread = pthread_self();

	if(handle->bin.audio_prio)
	{
		struct sched_param schedp;
		memset(&schedp, 0, sizeof(struct sched_param));
		schedp.sched_priority = handle->bin.audio_prio;

		if(schedp.sched_priority)
		{
			if(pthread_setschedparam(bin->dsp_thread, SCHED_FIFO, &schedp))
				bin_log_error(bin, "%s: pthread_setschedparam failed\n", __func__);
		}
	}

	if(handle->bin.cpu_affinity)
	{
		cpu_set_t cpuset;
		CPU_ZERO(&cpuset);
		CPU_SET(0, &cpuset);
		if(pthread_setaffinity_np(bin->dsp_thread, sizeof(cpu_set_t), &cpuset))
			bin_log_error(bin, "%s: pthread_setaffinity_np failed\n", __func__);
	}

	_process(handle);

	return NULL;
}

__non_realtime static void *
_system_port_add(void *data, system_port_t type, const char *short_name,
	const char *pretty_name, const char *designation, bool input, uint32_t order)
{
	bin_t *bin = data;
	prog_t *handle = (void *)bin - offsetof(prog_t, bin);

	chan_t *chan = NULL;

	switch(type)
	{
		case SYSTEM_PORT_NONE:
		{
			// skip
			break;
		}

		case SYSTEM_PORT_CONTROL:
		{
			// unsupported, skip
			break;
		}

		case SYSTEM_PORT_AUDIO:
		{
			chan = calloc(1, sizeof(chan_t));
			if(chan)
			{
				chan->type = CHAN_TYPE_PCMI;
				//TODO
			}

			break;
		}
		case SYSTEM_PORT_CV:
		{
			// unsupported, skip
			break;
		}

		case SYSTEM_PORT_MIDI:
		{
			chan = calloc(1, sizeof(chan_t));
			if(chan)
			{
				chan->type = CHAN_TYPE_MIDI;
				memcpy(&chan->midi.forge, &handle->forge, sizeof(LV2_Atom_Forge)); // initialize forge

				snd_seq_port_info_t *pinfo;
				snd_seq_port_info_malloc(&pinfo);

				snd_seq_port_info_set_name(pinfo, short_name);
				snd_seq_port_info_set_capability(pinfo, input
					? SND_SEQ_PORT_CAP_WRITE | SND_SEQ_PORT_CAP_SUBS_WRITE
					: SND_SEQ_PORT_CAP_READ | SND_SEQ_PORT_CAP_SUBS_READ);
				snd_seq_port_info_set_type(pinfo, SND_SEQ_PORT_TYPE_MIDI_GENERIC | SND_SEQ_PORT_TYPE_APPLICATION);
				snd_seq_port_info_set_midi_channels(pinfo, 16);
				snd_seq_port_info_set_midi_voices(pinfo, 64);
				snd_seq_port_info_set_synth_voices(pinfo, 0);
				snd_seq_port_info_set_timestamping(pinfo, 1);
				snd_seq_port_info_set_timestamp_real(pinfo, 1);
				snd_seq_port_info_set_timestamp_queue(pinfo, handle->queue);

				snd_seq_create_port(handle->seq, pinfo);
				chan->midi.port = snd_seq_port_info_get_port(pinfo);
				if(chan->midi.port < 0)
					bin_log_error(bin, "%s: could not create MIDI port\n", __func__);

				snd_seq_port_info_free(pinfo);

				if(snd_midi_event_new(MIDI_SEQ_SIZE, &chan->midi.trans))
					bin_log_error(bin, "%s: could not create MIDI event translator\n", __func__);
				snd_midi_event_init(chan->midi.trans);
				if(input)
					snd_midi_event_reset_encode(chan->midi.trans);
				else
					snd_midi_event_reset_decode(chan->midi.trans);
				snd_midi_event_no_status(chan->midi.trans, 1);
			}

			break;
		}
		case SYSTEM_PORT_OSC:
		{
			// unsupported, skip
			break;
		}
		case SYSTEM_PORT_COM:
		{
			// unsupported, skip
			break;
		}
	}

	return chan;
}

__non_realtime static void
_system_port_del(void *data, void *sys_port)
{
	bin_t *bin = data;
	prog_t *handle = (void *)bin - offsetof(prog_t, bin);

	chan_t *chan = sys_port;

	if(!chan)
		return;

	switch(chan->type)
	{
		case CHAN_TYPE_PCMI:
		{
			//TODO

			break;
		}
		case CHAN_TYPE_MIDI:
		{
			snd_midi_event_free(chan->midi.trans);
			if(handle->seq)
				snd_seq_delete_simple_port(handle->seq, chan->midi.port);

			break;
		}
	}

	free(chan);
}

__non_realtime static void
_saved(bin_t *bin, int status)
{
	prog_t *handle = (void *)bin - offsetof(prog_t, bin);

	if(handle->save_state == SAVE_STATE_NSM)
	{
		nsmc_saved(bin->nsm, status);
	}
	handle->save_state = SAVE_STATE_INTERNAL;

	if(atomic_load_explicit(&handle->kill, memory_order_relaxed))
	{
		bin_quit(bin);
	}
}

static int
_alsa_init(prog_t *handle, const char *id)
{
	bin_t *bin = &handle->bin;

	// init alsa sequencer
	if(snd_seq_open(&handle->seq, "hw", SND_SEQ_OPEN_DUPLEX, SND_SEQ_NONBLOCK))
		bin_log_error(bin, "%s: could not open sequencer\n", __func__);
	if(snd_seq_set_client_name(handle->seq, id))
		bin_log_error(bin, "%s: could not set name\n", __func__);
	handle->queue = snd_seq_alloc_queue(handle->seq);
	if(handle->queue < 0)
		bin_log_error(bin, "%s: could not allocate queue\n", __func__);
	snd_seq_start_queue(handle->seq, handle->queue, NULL);

	// init alsa pcm
	handle->pcmi = pcmi_new(handle->play_name, handle->capt_name,
		handle->srate, handle->frsize, handle->nfrags, handle->twochan, handle->debug);
	if(!handle->pcmi)
		return -1;
	pcmi_printinfo(handle->pcmi);

	return 0;
}

static void
_alsa_deinit(prog_t *handle)
{
	bin_t *bin = &handle->bin;

	if(handle->thread)
	{
		atomic_store_explicit(&handle->kill, 1, memory_order_relaxed);
		pthread_join(handle->thread, NULL);
	}

	if(handle->pcmi)
	{
		pcmi_free(handle->pcmi);

		handle->pcmi = NULL;
	}
	
	if(handle->seq)
	{
		if(snd_seq_drain_output(handle->seq))
			bin_log_error(bin, "%s: draining output failed\n", __func__);
		snd_seq_stop_queue(handle->seq, handle->queue, NULL);
		if(snd_seq_free_queue(handle->seq, handle->queue))
			bin_log_error(bin, "%s: freeing queue failed\n", __func__);
		if(snd_seq_close(handle->seq))
			bin_log_error(bin, "%s: close sequencer failed\n", __func__);

		handle->seq = NULL;
		handle->queue = 0;
	}
}

__non_realtime static int 
_open(const char *path, const char *name, const char *id, void *data)
{
	bin_t *bin = data;
	prog_t *handle = (void *)bin - offsetof(prog_t, bin);
	(void)name;

	if(bin->path)
		free(bin->path);
	bin->path = strdup(path);

	// alsa init
	if(_alsa_init(handle, id))
	{
		nsmc_opened(bin->nsm, -1);
		return -1;
	}

	// synthpod init
	bin->app_driver.sample_rate = handle->srate;
	bin->app_driver.update_rate = handle->bin.update_rate;
	bin->app_driver.max_block_size = handle->frsize;
	bin->app_driver.min_block_size = 1;
	bin->app_driver.seq_size = handle->seq_size;
	bin->app_driver.num_periods = handle->nfrags;
	
	// app init
	bin->app = sp_app_new(NULL, &bin->app_driver, bin);

	// alsa activate
	atomic_init(&handle->kill, 0);
	if(pthread_create(&handle->thread, NULL, _rt_thread, handle))
		bin_log_error(bin, "%s: creation of realtime thread failed\n", __func__);

	bin_bundle_load(bin, bin->path);
	nsmc_opened(bin->nsm, 0);

	return 0; // success
}

__non_realtime static int
_save(void *data)
{
	bin_t *bin = data;
	prog_t *handle = (void *)bin - offsetof(prog_t, bin);

	handle->save_state = SAVE_STATE_NSM;
	bin_bundle_save(bin, bin->path);
	_saved(bin, 0);

	return 0; // success
}

__non_realtime static int
_show(void *data)
{
	bin_t *bin = data;

	return bin_show(bin);
}

__non_realtime static int
_hide(void *data)
{
	bin_t *bin = data;

	return bin_hide(bin);
}

static const nsmc_driver_t nsm_driver = {
	.open = _open,
	.save = _save,
	.show = _show,
	.hide = _hide
};

// rt
__realtime static double
_osc_schedule_osc2frames(LV2_OSC_Schedule_Handle instance, uint64_t timestamp)
{
	prog_t *handle = instance;

	if(timestamp == 1ULL)
		return 0; // inject at start of period

	const uint64_t time_sec = timestamp >> 32;
	const uint64_t time_frac = timestamp & 0xffffffff;

	const double diff = (time_sec - handle->cur_ntp.tv_sec)
		+ time_frac * 0x1p-32
		- handle->cur_ntp.tv_nsec * 1e-9;

	const double frames = diff * handle->cycle.dT
		- handle->cycle.ref_frames
		+ handle->cycle.cur_frames;

	return frames;
}

// rt
__realtime static uint64_t
_osc_schedule_frames2osc(LV2_OSC_Schedule_Handle instance, double frames)
{
	prog_t *handle = instance;

	double diff = (frames - handle->cycle.cur_frames + handle->cycle.ref_frames)
		* handle->cycle.dTm1;
	diff += handle->cur_ntp.tv_nsec * 1e-9;
	diff += handle->cur_ntp.tv_sec;

	double time_sec_d;
	double time_frac_d = modf(diff, &time_sec_d);

	uint64_t time_sec = time_sec_d;
	uint64_t time_frac = time_frac_d * 0x1p32;
	if(time_frac >= 0x100000000ULL) // illegal overflow
		time_frac = 0xffffffffULL;

	uint64_t timestamp = (time_sec << 32) | time_frac;

	return timestamp;
}

int
main(int argc, char **argv)
{
	mlockall(MCL_CURRENT | MCL_FUTURE);

	static prog_t handle;
	bin_t *bin = &handle.bin;

	handle.srate = 48000;
	handle.frsize = 1024;
	handle.nfrags = 3;
	handle.twochan = false;
	handle.debug = false;
	handle.do_play = true;
	handle.do_capt = true;
	handle.seq_size = SEQ_SIZE;

	const char *def = "hw:0";
	handle.io_name = def;
	handle.play_name = NULL;
	handle.capt_name = NULL;

	bin->audio_prio = 70;
	bin->worker_prio = 60;
	bin->num_slaves = sysconf(_SC_NPROCESSORS_ONLN) - 1;
	bin->bad_plugins = false;
	bin->has_gui = false;
	bin->kill_gui = false;
	snprintf(bin->socket_path, sizeof(bin->socket_path), "shm:///synthpod-%i", getpid());
	bin->update_rate = 25;
	bin->cpu_affinity = false;

	fprintf(stderr,
		"Synthpod "SYNTHPOD_VERSION"\n"
		"Copyright (c) 2015-2016 Hanspeter Portner (dev@open-music-kontrollers.ch)\n"
		"Released under GNU General Public License 3 by Open Music Kontrollers\n");

	// read local configuration if present
	/*FIXME
	Efreet_Ini *ini = _read_config(&handle);
	*/
	
	int c;
	while((c = getopt(argc, argv, "vhgGbkKBaAIOtTxXy:Yw:Wl:d:i:o:r:p:n:s:c:f:")) != -1)
	{
		switch(c)
		{
			case 'v':
				fprintf(stderr,
					"--------------------------------------------------------------------\n"
					"This program is free software; you can redistribute it and/or modify\n"
					"it under the terms of the GNU General Public License as published by\n"
					"the Free Software Foundation; either version 3 of the License, or\n"
					"(at your option) any later version.\n"
					"\n"
					"This program is distributed in the hope that it will be useful,\n"
					"but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
					"MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n"
					"GNU General Public License for more details.\n"
					"\n"
					"You should have received a copy of the GNU General Public License\n"
					"along with this program.  If not, see http://www.gnu.org/licenses.\n\n");
				return 0;
			case 'h':
				fprintf(stderr,
					"--------------------------------------------------------------------\n"
					"USAGE\n"
					"   %s [OPTIONS] [BUNDLE_PATH]\n"
					"\n"
					"OPTIONS\n"
					"   [-v]                 print version and full license information\n"
					"   [-h]                 print usage information\n"
					"   [-g]                 load GUI\n"
					"   [-G]                 do NOT load GUI (default)\n"
					"   [-k]                 kill DSP with GUI\n"
					"   [-K]                 do NOT kill DSP with GUI (default)\n"
					"   [-b]                 enable bad plugins\n"
					"   [-B]                 disable bad plugins (default)\n"
					"   [-a]                 enable CPU affinity\n"
					"   [-A]                 disable CPU affinity (default)\n"
					"   [-I]                 disable capture\n"
					"   [-O]                 disable playback\n"
					"   [-t]                 force 2 channel mode\n"
					"   [-T]                 do NOT force 2 channel mode (default)\n"
					"   [-x]                 notify about XRuns\n"
					"   [-X]                 do NOT notify about XRuns (default)\n"
					"   [-y] audio-priority  audio thread realtime priority (70)\n"
					"   [-Y]                 do NOT use audio thread realtime priority\n"
					"   [-w] worker-priority worker thread realtime priority (60)\n"
					"   [-W]                 do NOT use worker thread realtime priority\n"
					"   [-l] link-path       socket link path (shm:///synthpod)\n"
					"   [-d] device          capture/playback device (\"hw:0\")\n"
					"   [-i] capture-device  capture device (\"hw:0\")\n"
					"   [-o] playback-device playback device (\"hw:0\")\n"
					"   [-r] sample-rate     sample rate (48000)\n"
					"   [-p] sample-period   frames per period (1024)\n"
					"   [-n] period-number   number of periods of playback latency (3)\n"
					"   [-s] sequence-size   minimum sequence size (8192)\n"
					"   [-c] slave-cores     number of slave cores (auto)\n"
					"   [-f] update-rate     GUI update rate (25)\n\n"
					, argv[0]);
				return 0;
			case 'g': 
				bin->has_gui = true;
				break;
			case 'G':
				bin->has_gui = false;
				break;
			case 'k': 
				bin->kill_gui = true;
				break;
			case 'K':
				bin->kill_gui = false;
				break;
			case 'b':
				bin->bad_plugins = true;
				break;
			case 'B':
				bin->bad_plugins = false;
				break;
			case 'a':
				bin->cpu_affinity = true;
				break;
			case 'A':
				bin->cpu_affinity = false;
				break;
			case 'I':
				handle.do_capt = false;
				break;
			case 'O':
				handle.do_play = false;
				break;
			case 't':
				handle.twochan = true;
				break;
			case 'T':
				handle.twochan = false;
				break;
			case 'x':
				handle.debug = true;
				break;
			case 'X':
				handle.debug = false;
				break;
			case 'y':
				bin->audio_prio = atoi(optarg);
				break;
			case 'Y':
				bin->audio_prio = 0;
				break;
			case 'w':
				bin->worker_prio = atoi(optarg);
				break;
			case 'W':
				bin->worker_prio = 0;
				break;
			case 'l':
				snprintf(bin->socket_path, sizeof(bin->socket_path), "%s", optarg);
				break;
			case 'd':
				handle.do_capt = optarg != NULL;
				handle.do_play = optarg != NULL;
				handle.io_name = optarg;
				break;
			case 'i':
				handle.do_capt = optarg != NULL;
				handle.capt_name = optarg;
				break;
			case 'o':
				handle.do_play = optarg != NULL;
				handle.play_name = optarg;
				break;
			case 'r':
				handle.srate = atoi(optarg);
				break;
			case 'p':
				handle.frsize = atoi(optarg);
				break;
			case 'n':
				handle.nfrags = atoi(optarg);
				break;
			case 's':
				handle.seq_size = MAX(SEQ_SIZE, atoi(optarg));
				break;
			case 'c':
				if(atoi(optarg) < bin->num_slaves)
					bin->num_slaves = atoi(optarg);
				break;
			case 'f':
				bin->update_rate = atoi(optarg);
				break;
			case '?':
				if( (optopt == 'd') || (optopt == 'i') || (optopt == 'o') || (optopt == 'r')
					|| (optopt == 'p') || (optopt == 'n') || (optopt == 's') || (optopt == 'c')
					|| (optopt == 'l') || (optopt == 'f') )
					fprintf(stderr, "Option `-%c' requires an argument.\n", optopt);
				else if(isprint(optopt))
					fprintf(stderr, "Unknown option `-%c'.\n", optopt);
				else
					fprintf(stderr, "Unknown option character `\\x%x'.\n", optopt);
				return -1;
			default:
				return -1;
		}
	}

	if(!handle.capt_name && handle.do_capt)
		handle.capt_name = handle.io_name;
	if(!handle.play_name && handle.do_play)
		handle.play_name = handle.io_name;
	
	bin_init(bin, handle.srate);
	
	LV2_URID_Map *map = bin->map;
	
	lv2_atom_forge_init(&handle.forge, map);
	
	handle.midi_MidiEvent = map->map(map->handle, LV2_MIDI__MidiEvent);
	
	bin->app_driver.system_port_add = _system_port_add;
	bin->app_driver.system_port_del = _system_port_del;
	
	handle.osc_sched.osc2frames = _osc_schedule_osc2frames;
	handle.osc_sched.frames2osc = _osc_schedule_frames2osc;
	handle.osc_sched.handle = &handle;
	bin->app_driver.osc_sched = &handle.osc_sched;
	bin->app_driver.features = SP_APP_FEATURE_FIXED_BLOCK_LENGTH; // always true for ALSA
  if(handle.frsize && !(handle.frsize & (handle.frsize - 1))) // check for powerOf2
		bin->app_driver.features |= SP_APP_FEATURE_POWER_OF_2_BLOCK_LENGTH;

	// run
	bin_run(bin, argv, &nsm_driver, NULL, NULL);

	// stop
	bin_stop(bin);

	// deinit alsa
	_alsa_deinit(&handle);

	// deinit
	bin_deinit(bin);

	/*FIXME
	if(ini)
		efreet_ini_free(ini);
	*/

	munlockall();

	return 0;
}