aboutsummaryrefslogtreecommitdiff
path: root/bin/synthpod_alsa.c
blob: 6ff54970f6cd86a130e5535dc513ab89e3a7a31b (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
/*
 * Copyright (c) 2015 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 <unistd.h>
#include <ctype.h>

#include <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;
		} 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;
	volatile int kill;
	Eina_Thread thread;

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

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

	osc_schedule_t 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(struct timespec *ntp)
{
	clock_gettime(CLOCK_REALTIME, 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;
}

static void *
_rt_thread(void *data, Eina_Thread thread)
{
	prog_t *handle = data;
	pcmi_t *pcmi = handle->pcmi;
	bin_t *bin = &handle->bin;
	sp_app_t *app = bin->app;
	
	pthread_t self = pthread_self();

	struct sched_param schedp;
	memset(&schedp, 0, sizeof(struct sched_param));
	schedp.sched_priority = 70; //TODO make configurable
	
	if(schedp.sched_priority)
	{
		if(pthread_setschedparam(self, SCHED_RR, &schedp))
			fprintf(stderr, "pthread_setschedparam error\n");
	}
		
	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 = nsamples * NANO_SECONDS / handle->srate;
	handle->cycle.cur_frames = 0; // initialize frame counter
	_ntp_now(&handle->nxt_ntp);

	pcmi_pcm_start(handle->pcmi);
	while(!handle->kill)
	{
		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(&nxt_ntp);
		_ntp_clone(&handle->nxt_ntp, &nxt_ntp);

		// increase cur_frames
		handle->cycle.cur_frames += na;
		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 = diff / nsamples;

		for( ; na >= nsamples;
				na -= nsamples,
				handle->cycle.ref_frames += nsamples,
				_ntp_add_nanos(&handle->nxt_ntp, nanos_per_period) )
		{
			const sp_app_system_source_t *sources = sp_app_get_system_sources(app);
			const sp_app_system_sink_t *sinks = sp_app_get_system_sinks(app);
	
			int paused = sp_app_paused(app);
			if(paused == 1) // aka loading state
			{
				pcmi_pcm_idle(pcmi, nsamples);

				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) && (capt_num < ncapt);
				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:
					{

						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;
						lv2_atom_forge_set_buffer(forge, seq_in, SEQ_SIZE);
						lv2_atom_forge_sequence_head(forge, &chan->midi.frame, 0);

						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;

							int64_t frames = 0;
							/* FIXME
							if( (sev->flags & (SND_SEQ_TIME_STAMP_MASK | SND_SEQ_TIME_MODE_MASK))
								== (SND_SEQ_TIME_STAMP_REAL | SND_SEQ_TIME_MODE_REL) )
							{
								frames = sev->time.time.tv_nsec * 1e-9 * handle->srate;
							}
							*/

							lv2_atom_forge_frame_time(forge, frames);
							lv2_atom_forge_atom(forge, len, handle->midi_MidiEvent);
							lv2_atom_forge_raw(forge, handle->m, len);
							lv2_atom_forge_pad(forge, len);
						}
						else
							fprintf(stderr, "event decode failed\n");
					}
					else
						fprintf(stderr, "no matching port for event\n");

					if(snd_seq_free_event(sev))
						fprintf(stderr, "event free failed\n");
				}
			}
						
			for(const sp_app_system_source_t *source=sources;
				(source->type != SYSTEM_PORT_NONE) && (capt_num < ncapt);
				source++)
			{
				chan_t *chan = source->sys_port;

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

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

			// 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) && (play_num < nplay);
				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:
					{

						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;

							snd_seq_event_t sev;
							snd_seq_ev_clear(&sev);
							if(snd_midi_event_encode(chan->midi.trans, LV2_ATOM_BODY_CONST(atom), atom->size, &sev) != atom->size)
								fprintf(stderr, "encode event failed\n");
						
							// relative timestamp
							struct snd_seq_real_time rtime = {
								.tv_sec = 0,
								.tv_nsec = ev->time.frames * nanos_per_period
							};
							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, 1, &rtime); // relative scheduling
							snd_seq_event_output(handle->seq, &sev);
						}

						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);
		}
	}
	pcmi_pcm_stop(handle->pcmi);

	return NULL;
}

static void *
_system_port_add(void *data, System_Port_Type type, const char *short_name,
	const char *pretty_name, int input)
{
	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;

				unsigned int caps = input
					? SND_SEQ_PORT_CAP_WRITE | SND_SEQ_PORT_CAP_SUBS_WRITE
					: SND_SEQ_PORT_CAP_READ | SND_SEQ_PORT_CAP_SUBS_READ;

				chan->midi.port = snd_seq_create_simple_port(handle->seq, short_name,
					caps, SND_SEQ_PORT_TYPE_APPLICATION);
				if(chan->midi.port < 0)
					fprintf(stderr, "could not create port\n");

				if(snd_midi_event_new(MIDI_SEQ_SIZE, &chan->midi.trans))
					fprintf(stderr, "could not create event\n");
				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;
		}
	}

	return chan;
}

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 || !handle->seq)
		return;

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

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

			break;
		}
	}

	free(chan);
}

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

	//printf("_ui_saved: %i\n", status);
	if(handle->save_state == SAVE_STATE_NSM)
	{
		synthpod_nsm_saved(bin->nsm, status);
	}
	handle->save_state = SAVE_STATE_INTERNAL;

	if(handle->kill)
	{
		elm_exit();
	}
}

static int
_alsa_init(prog_t *handle, const char *id)
{
	// init alsa sequencer
	if(snd_seq_open(&handle->seq, "hw", SND_SEQ_OPEN_DUPLEX, SND_SEQ_NONBLOCK))
		fprintf(stderr, "could not open sequencer\n");
	if(snd_seq_set_client_name(handle->seq, id))
		fprintf(stderr, "could not set name\n");
	handle->queue = snd_seq_alloc_queue(handle->seq);
	if(handle->queue < 0)
		fprintf(stderr, "could not allocate queue\n");
	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)
{
	handle->kill = 1;
	eina_thread_join(handle->thread);

	pcmi_free(handle->pcmi);
	handle->pcmi = NULL;
				
	if(snd_seq_drain_output(handle->seq))
		fprintf(stderr, "draining output failed\n");
	snd_seq_stop_queue(handle->seq, handle->queue, NULL);
	if(snd_seq_free_queue(handle->seq, handle->queue))
		fprintf(stderr, "freeing queue failed\n");
	if(snd_seq_close(handle->seq))
		fprintf(stderr, "close sequencer failed\n");
	handle->seq = NULL;
	handle->queue = 0;
}

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))
		return -1;

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

	// alsa activate
	Eina_Bool status = eina_thread_create(&handle->thread,
		EINA_THREAD_URGENT, -1, _rt_thread, handle); //TODO
	if(!status)
		fprintf(stderr, "creation of rt thread failed\n");

	sp_ui_bundle_load(bin->ui, bin->path, 1);

	return 0; // success
}

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

	handle->save_state = SAVE_STATE_NSM;
	sp_ui_bundle_save(bin->ui, bin->path, 1);

	return 0; // success
}

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

// rt
static int64_t
_osc_schedule_osc2frames(osc_schedule_handle_t instance, uint64_t timestamp)
{
	prog_t *handle = instance;

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

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

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

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

	int64_t frames = round(frames_d);

	return frames;
}

// rt
static uint64_t
_osc_schedule_frames2osc(osc_schedule_handle_t instance, int64_t frames)
{
	prog_t *handle = instance;

	volatile double diff = (double)(frames - handle->cycle.ref_frames + handle->cycle.cur_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;
}

EAPI_MAIN int
elm_main(int argc, char **argv);
	
EAPI_MAIN int
elm_main(int argc, char **argv)
{
	static prog_t handle;
	bin_t *bin = &handle.bin;

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

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

	bin->has_gui = true;

	fprintf(stderr,
		"Synthpod "SYNTHPOD_VERSION"\n"
		"Copyright (c) 2015 Hanspeter Portner (dev@open-music-kontrollers.ch)\n"
		"Released under Artistic License 2.0 by Open Music Kontrollers\n");
	
	int c;
	while((c = getopt(argc, argv, "vhGIO2xd:i:o:r:p:n:s:")) != -1)
	{
		switch(c)
		{
			case 'v':
				fprintf(stderr,
					"--------------------------------------------------------------------\n"
					"This is free software: you can redistribute it and/or modify\n"
					"it under the terms of the Artistic License 2.0 as published by\n"
					"The Perl Foundation.\n"
					"\n"
					"This source 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"
					"Artistic License 2.0 for more details.\n"
					"\n"
					"You should have received a copy of the Artistic License 2.0\n"
					"along the source as a COPYING file. If not, obtain it from\n"
					"http://www.perlfoundation.org/artistic_license_2_0.\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]                 disable GUI\n"
					"   [-I]                 disable capture\n"
					"   [-O]                 disable playback\n"
					"   [-2]                 force 2 channel mode\n"
					"   [-d]                 enable debugging\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\n"
					, argv[0]);
				return 0;
			case 'G':
				bin->has_gui = false;
				break;
			case 'I':
				handle.do_capt = 0;
				break;
			case 'O':
				handle.do_play = 0;
				break;
			case '2':
				handle.twochan = 1;
				break;
			case 'x':
				handle.debug = 1;
				break;
			case 'd':
				handle.io_name = optarg;
				break;
			case 'i':
				handle.capt_name = optarg;
				break;
			case 'o':
				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 '?':
				if( (optopt == 'd') || (optopt == 'i') || (optopt == 'o') || (optopt == 'r')
					  || (optopt == 'p') || (optopt == 'n') || (optopt == 's') )
					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);
	
	LV2_URID_Map *map = ext_urid_map_get(bin->ext_urid);
	
	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->ui_driver.saved = _ui_saved;

	bin->ui_driver.features = SP_UI_FEATURE_NEW | SP_UI_FEATURE_SAVE | SP_UI_FEATURE_CLOSE;
	if(synthpod_nsm_managed())
		bin->ui_driver.features |= SP_UI_FEATURE_IMPORT_FROM | SP_UI_FEATURE_EXPORT_TO;
	else
		bin->ui_driver.features |= SP_UI_FEATURE_OPEN | SP_UI_FEATURE_SAVE_AS;

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

	// stop
	bin_stop(bin);

	// deinit alsa
	_alsa_deinit(&handle);

	// deinit
	bin_deinit(bin);

	return 0;
}

ELM_MAIN()