aboutsummaryrefslogtreecommitdiff
path: root/bin/synthpod_bin.h
blob: a6111866a75921095dec3d3fad951f2512f9d1db (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
/*
 * 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.
 */

#ifndef SYNTHPOD_COMMON_H
#define SYNTHPOD_COMMON_H

#include <synthpod_app.h>

#	include <synthpod_ui.h>

#include <Eina.h>

#include <ext_urid.h>
#include <varchunk.h>
#include <lv2_osc.h>
#include <osc.h>

#include <synthpod_nsm.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/time/time.h>

#define SEQ_SIZE 0x2000
#define CHUNK_SIZE 0x10000

typedef enum _save_state_t save_state_t;

enum _save_state_t {
	SAVE_STATE_INTERNAL = 0,
	SAVE_STATE_NSM,
	SAVE_STATE_JACK
};

typedef struct _bin_t bin_t;

struct _bin_t {
	ext_urid_t *ext_urid;
	
	sp_app_t *app;
	sp_app_driver_t app_driver;
	
	varchunk_t *app_to_worker;
	varchunk_t *app_from_worker;
	varchunk_t *app_to_log;

	char *path;
	synthpod_nsm_t *nsm;

	bool has_gui;
	sp_ui_t *ui;
	sp_ui_driver_t ui_driver;
	
	varchunk_t *app_to_ui;
	varchunk_t *app_from_ui;
	
	Ecore_Animator *ui_anim;
	Evas_Object *win;
	
	volatile int worker_dead;
	Eina_Thread worker_thread;
	Eina_Semaphore worker_sem;
	bool worker_sem_needs_release;

	LV2_URID log_entry;
	LV2_URID log_error;
	LV2_URID log_note;
	LV2_URID log_trace;
	LV2_URID log_warning;
};

// non-rt ui-thread
static void
_ui_delete_request(void *data, Evas_Object *obj, void *event)
{
	elm_exit();	
}

static void
_ui_close(void *data)
{
	//printf("_ui_close\n");
	elm_exit();
}

	static void
_ui_opened(void *data, int status)
{
	bin_t *bin = data;

	//printf("_ui_opened: %i\n", status);
	synthpod_nsm_opened(bin->nsm, status);
}

// non-rt ui-thread
static Eina_Bool
_ui_animator(void *data)
{
	bin_t *bin = data;

	size_t size;
	const LV2_Atom *atom;
	while((atom = varchunk_read_request(bin->app_to_ui, &size)))
	{
		sp_ui_from_app(bin->ui, atom);
		varchunk_read_advance(bin->app_to_ui);
	}

	return EINA_TRUE; // continue animator
}

// non-rt worker-thread
static void *
_worker_thread(void *data, Eina_Thread thread)
{
	bin_t *bin = data;
	
	pthread_t self = pthread_self();

	struct sched_param schedp;
	memset(&schedp, 0, sizeof(struct sched_param));
	schedp.sched_priority = 60; //TODO make configurable
	
	if(schedp.sched_priority)
	{
		if(pthread_setschedparam(self, SCHED_RR, &schedp))
			fprintf(stderr, "pthread_setschedparam error\n");
	}

	while(!bin->worker_dead)
	{
		eina_semaphore_lock(&bin->worker_sem);

		size_t size;
		const void *body;
		while((body = varchunk_read_request(bin->app_to_worker, &size)))
		{
			sp_worker_from_app(bin->app, size, body);
			varchunk_read_advance(bin->app_to_worker);
		}

		const char *trace;
		while((trace = varchunk_read_request(bin->app_to_log, &size)))
		{
			fprintf(stderr, "[Trace] %s\n", trace);

			varchunk_read_advance(bin->app_to_log);
		}
	}

	return NULL;
}

// rt
static void *
_app_to_ui_request(size_t size, void *data)
{
	bin_t *bin = data;

	return varchunk_write_request(bin->app_to_ui, size);
}
static void
_app_to_ui_advance(size_t size, void *data)
{
	bin_t *bin = data;

	varchunk_write_advance(bin->app_to_ui, size);
}

// non-rt ui-thread
static void *
_ui_to_app_request(size_t size, void *data)
{
	bin_t *bin = data;

	void *ptr;
	do
	{
		ptr = varchunk_write_request(bin->app_from_ui, size);
	}
	while(!ptr); // wait until there is enough space

	return ptr;
}
static void
_ui_to_app_advance(size_t size, void *data)
{
	bin_t *bin = data;

	varchunk_write_advance(bin->app_from_ui, size);
}

// rt
static void *
_app_to_worker_request(size_t size, void *data)
{
	bin_t *bin = data;

	return varchunk_write_request(bin->app_to_worker, size);
}
static void
_app_to_worker_advance(size_t size, void *data)
{
	bin_t *bin = data;

	varchunk_write_advance(bin->app_to_worker, size);
	bin->worker_sem_needs_release = true;
}

// non-rt worker-thread
static void *
_worker_to_app_request(size_t size, void *data)
{
	bin_t *bin = data;

	void *ptr;
	do
	{
		ptr = varchunk_write_request(bin->app_from_worker, size);
	}
	while(!ptr); // wait until there is enough space

	return ptr;
}
static void
_worker_to_app_advance(size_t size, void *data)
{
	bin_t *bin = data;

	varchunk_write_advance(bin->app_from_worker, size);
}

// non-rt || rt with LV2_LOG__Trace
static int
_log_vprintf(void *data, LV2_URID type, const char *fmt, va_list args)
{
	bin_t *bin = data;

	if(type == bin->log_trace)
	{
		char *trace;
		if((trace = varchunk_write_request(bin->app_to_log, 1024)))
		{
			vsprintf(trace, fmt, args);

			size_t written = strlen(trace) + 1;
			varchunk_write_advance(bin->app_to_log, written);
			bin->worker_sem_needs_release = true;
		}
	}
	else // !log_trace
	{
		const char *type_str = NULL;
		if(type == bin->log_entry)
			type_str = "Entry";
		else if(type == bin->log_error)
			type_str = "Error";
		else if(type == bin->log_note)
			type_str = "Note";
		else if(type == bin->log_warning)
			type_str = "Warning";

		//TODO send to UI?

		fprintf(stderr, "[%s] ", type_str);
		vfprintf(stderr, fmt, args);
		fputc('\n', stderr);

		return 0;
	}

	return -1;
}

// non-rt || rt with LV2_LOG__Trace
static int
_log_printf(void *data, LV2_URID type, const char *fmt, ...)
{
  va_list args;
	int ret;

  va_start (args, fmt);
	ret = _log_vprintf(data, type, fmt, args);
  va_end(args);

	return ret;
}

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

	if(bin->win)
		evas_object_show(bin->win);
	
	return 0;
}

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

	if(bin->win)
		evas_object_hide(bin->win);

	return 0;
}

static void
bin_init(bin_t *bin)
{
	// varchunk init
	bin->app_to_ui = varchunk_new(CHUNK_SIZE);
	bin->app_from_ui = varchunk_new(CHUNK_SIZE);
	bin->app_to_worker = varchunk_new(CHUNK_SIZE);
	bin->app_from_worker = varchunk_new(CHUNK_SIZE);
	bin->app_to_log = varchunk_new(CHUNK_SIZE);

	bin->ext_urid = ext_urid_new();
	LV2_URID_Map *map = ext_urid_map_get(bin->ext_urid);
	LV2_URID_Unmap *unmap = ext_urid_unmap_get(bin->ext_urid);

	bin->log_entry = map->map(map->handle, LV2_LOG__Entry);
	bin->log_error = map->map(map->handle, LV2_LOG__Error);
	bin->log_note = map->map(map->handle, LV2_LOG__Note);
	bin->log_trace = map->map(map->handle, LV2_LOG__Trace);
	bin->log_warning = map->map(map->handle, LV2_LOG__Warning);
	
	bin->app_driver.map = map;
	bin->app_driver.unmap = unmap;
	bin->app_driver.log_printf = _log_printf;
	bin->app_driver.log_vprintf = _log_vprintf;
	bin->app_driver.to_ui_request = _app_to_ui_request;
	bin->app_driver.to_ui_advance = _app_to_ui_advance;
	bin->app_driver.to_worker_request = _app_to_worker_request;
	bin->app_driver.to_worker_advance = _app_to_worker_advance;
	bin->app_driver.to_app_request = _worker_to_app_request;
	bin->app_driver.to_app_advance = _worker_to_app_advance;

	bin->ui_driver.map = map;
	bin->ui_driver.unmap = unmap;
	bin->ui_driver.to_app_request = _ui_to_app_request;
	bin->ui_driver.to_app_advance = _ui_to_app_advance;
	bin->ui_driver.instance_access = 1; // enabled

	bin->ui_driver.opened = _ui_opened;
	bin->ui_driver.close = _ui_close;
}

static void
bin_run(bin_t *bin, char **argv, const synthpod_nsm_driver_t *nsm_driver)
{
	// create main window
	bin->ui_anim = ecore_animator_add(_ui_animator, bin);

	bin->win = NULL;
	if(bin->has_gui)
	{
		bin->win = elm_win_util_standard_add("synthpod", "Synthpod");
		if(bin->win)
		{
			evas_object_smart_callback_add(bin->win, "delete,request", _ui_delete_request, NULL);
			evas_object_resize(bin->win, 1280, 720);
			evas_object_show(bin->win);
		}
	}

	// ui init
	bin->ui = sp_ui_new(bin->win, NULL, &bin->ui_driver, bin, 1);

	// NSM init
	const char *exe = strrchr(argv[0], '/');
	exe = exe ? exe + 1 : argv[0]; // we only want the program name without path
	bin->nsm = synthpod_nsm_new(exe, argv[optind], nsm_driver, bin); //TODO check

	// init semaphores
	eina_semaphore_new(&bin->worker_sem, 0);

	// threads init
	Eina_Bool status = eina_thread_create(&bin->worker_thread,
		EINA_THREAD_URGENT, -1, _worker_thread, bin); //TODO

	// main loop
	elm_run();

	// ui deinit
	sp_ui_free(bin->ui);

	if(bin->win)
		evas_object_del(bin->win);

	if(bin->ui_anim)
		ecore_animator_del(bin->ui_anim);
}

static void
bin_stop(bin_t *bin)
{
	// threads deinit
	bin->worker_dead = 1; // atomic operation
	eina_semaphore_release(&bin->worker_sem, 1);
	eina_thread_join(bin->worker_thread);

	// NSM deinit
	synthpod_nsm_free(bin->nsm);

	// deinit semaphores
	eina_semaphore_free(&bin->worker_sem);

	if(bin->path)
		free(bin->path);
}

static void
bin_deinit(bin_t *bin)
{
	// synthpod deinit
	sp_app_free(bin->app);

	// ext_urid deinit
	ext_urid_free(bin->ext_urid);

	// varchunk deinit
	varchunk_free(bin->app_to_ui);
	varchunk_free(bin->app_from_ui);
	varchunk_free(bin->app_to_log);
	varchunk_free(bin->app_to_worker);
	varchunk_free(bin->app_from_worker);
}

static inline void
bin_process_pre(bin_t *bin, uint32_t nsamples, int paused)
{
	// read events from worker
	if(!paused) // aka not saving state
	{
		size_t size;
		const void *body;
		int n = 0;
		while((body = varchunk_read_request(bin->app_from_worker, &size))
			&& (n++ < 10) ) //FIXME limit to how many events?
		{
			sp_app_from_worker(bin->app, size, body);
			varchunk_read_advance(bin->app_from_worker);
		}
	}

	// run synthpod app pre
	sp_app_run_pre(bin->app, nsamples);

	// read events from UI
	if(!paused) // aka not saving state
	{
		size_t size;
		const LV2_Atom *atom;
		int n = 0;
		while((atom = varchunk_read_request(bin->app_from_ui, &size))
			&& (n++ < 10) ) //FIXME limit to how many events?
		{
			sp_app_from_ui(bin->app, atom);
			varchunk_read_advance(bin->app_from_ui);
		}
	}
	
	// run synthpod app post
	sp_app_run_post(bin->app, nsamples);
}

static inline void
bin_process_post(bin_t *bin)
{
	if(bin->worker_sem_needs_release)
	{
		eina_semaphore_release(&bin->worker_sem, 1);
		bin->worker_sem_needs_release = false;
	}
}

#endif