summaryrefslogtreecommitdiff
path: root/src/modules/rkt/rkt.c
blob: d8bab4b23322205fdf92a7ab29fd881a971eeaff (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
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#include "rocket/rocket/lib/device.h"
#include "rocket/rocket/lib/sync.h"
#include "rocket/rocket/lib/track.h"

#include "til.h"
#include "til_fb.h"
#include "til_module_context.h"
#include "til_settings.h"
#include "til_stream.h"
#include "til_tap.h"
#include "til_util.h"

#include "txt/txt.h"

#include "rkt.h"
#include "rkt_scener.h"

/* Copyright (C) 2023 - Vito Caputo <vcaputo@pengaru.com> */

/* This implements a rudimentary sequencing module varying
 * "tapped" variables of other modules on a timeline via
 * GNU Rocket (https://github.com/rocket/rocket)
 */

/* variadic helper wrapping librocket's sync_get_track() */
static const struct sync_track * sync_get_trackf(struct sync_device *device, const char *format, ...)
{
	char	buf[4096];
	size_t	len;
	va_list	ap;

	assert(device);
	assert(format);

	va_start(ap, format);
	len = vsnprintf(buf, sizeof(buf), format, ap);
	va_end(ap);

	if (len >= sizeof(buf))
		return NULL;

	return sync_get_track(device, buf);
}


static void rkt_sync_pause(void *context, int flag)
{
	rkt_context_t	*ctxt = context;

	if (flag)
		ctxt->paused = 1;
	else
		ctxt->paused = 0;
}


static void rkt_sync_set_row(void *context, int row)
{
	rkt_context_t	*ctxt = context;

	ctxt->rocket_row = row;
}


static int rkt_sync_is_playing(void *context)
{
	rkt_context_t	*ctxt = context;

	/* returns bool, 1 for is playing */
	return !ctxt->paused;
}


static struct sync_cb rkt_sync_cb = {
	rkt_sync_pause,
	rkt_sync_set_row,
	rkt_sync_is_playing,
};


typedef struct rkt_pipe_t {
	/* rocket basically only applies to floats, so we only need a float, its tap, and a sync track */
	til_tap_t		tap;

	union {
		float	f;
		double	d;
	} var;

	union {
		float	*f;
		double	*d;
	} ptr;

	const struct sync_track	*track;
} rkt_pipe_t;


int rkt_stream_pipe_ctor(void *context, til_stream_t *stream, const void *owner, const void *owner_foo, const char *parent_path, uint32_t parent_hash, const til_tap_t *tap, const void **res_owner, const void **res_owner_foo, const til_tap_t **res_driving_tap)
{
	rkt_context_t	*ctxt = context;
	rkt_pipe_t	*rkt_pipe;

	assert(stream);
	assert(tap);
	assert(res_owner);
	assert(res_owner_foo);
	assert(res_driving_tap);

	if (tap->type != TIL_TAP_TYPE_FLOAT &&
	    tap->type != TIL_TAP_TYPE_DOUBLE)
		return 0;	/* not interesting to us */

	/* assume pipe ownership, create driving tap and rocket track to stow @ owner_foo */

	rkt_pipe = calloc(1, sizeof(rkt_pipe_t));
	if (!rkt_pipe)
		return -ENOMEM;

	rkt_pipe->tap = til_tap_init(ctxt, tap->type, &rkt_pipe->ptr, 1, &rkt_pipe->var, tap->name);
	rkt_pipe->track = sync_get_trackf(ctxt->sync_device, "%s:%s", parent_path, tap->name);

	*res_owner = ctxt;
	*res_owner_foo = rkt_pipe;
	*res_driving_tap = rkt_pipe->track->num_keys ? &rkt_pipe->tap : tap;

	return 1;
}


static const til_stream_hooks_t	rkt_stream_hooks = {
	.pipe_ctor = rkt_stream_pipe_ctor,
	/* .pipe_dtor unneeded */
};


static int rkt_pipe_update(void *context, til_stream_pipe_t *pipe, const void *owner, const void *owner_foo, const til_tap_t *driving_tap)
{
	rkt_pipe_t	*rkt_pipe = (rkt_pipe_t *)owner_foo;
	rkt_context_t	*ctxt = context;
	double		val;

	/* just ignore pipes we don't own (they're not types we can drive w/rocket) */
	if (owner != ctxt)
		return 0;

	/* when there's no keys in the track, flag as inactive so someone else can drive */
	if (!rkt_pipe->track->num_keys) {
		rkt_pipe->tap.inactive = 1;

		return 0;
	}

	rkt_pipe->tap.inactive = 0;
	if (driving_tap != &rkt_pipe->tap)
		til_stream_pipe_set_driving_tap(pipe, &rkt_pipe->tap);

	/* otherwise get the current interpolated value from the rocket track @ owner_foo->track
	 * to update owner_foo->var.[fd], which _should_ be the driving tap.
	 */
	val = sync_get_val(rkt_pipe->track, ctxt->rocket_row);
	switch (rkt_pipe->tap.type) {
	case TIL_TAP_TYPE_FLOAT:
		rkt_pipe->var.f = val;
		break;
	case TIL_TAP_TYPE_DOUBLE:
		rkt_pipe->var.d = val;
		break;
	default:
		assert(0);
	}

	return 0;
}


static void rkt_update_rocket(rkt_context_t *ctxt, unsigned ticks)
{
	if (!ctxt->paused)
		ctxt->rocket_row += ((double)(ticks - ctxt->last_ticks)) * ctxt->rows_per_ms;

	ctxt->last_ticks = ticks;

	/* ctxt->rocket_row needs to be updated */
	sync_update(ctxt->sync_device, ctxt->rocket_row, &rkt_sync_cb, ctxt);
}


static til_module_context_t * rkt_create_context(const til_module_t *module, til_stream_t *stream, unsigned seed, unsigned ticks, unsigned n_cpus, til_setup_t *setup)
{
	rkt_setup_t	*s = (rkt_setup_t *)setup;
	rkt_context_t	*ctxt;

	ctxt = til_module_context_new(module, sizeof(rkt_context_t) + s->n_scenes * sizeof(*ctxt->scenes), stream, seed, ticks, n_cpus, setup);
	if (!ctxt)
		return NULL;

	if (s->n_scenes) {
		ctxt->scenes = calloc(s->n_scenes, sizeof(rkt_scene_t));
		if (!ctxt->scenes)
			return til_module_context_free(&ctxt->til_module_context);

		ctxt->n_scenes = s->n_scenes;
	}

	ctxt->sync_device = sync_create_device(s->base);
	if (!ctxt->sync_device)
		return til_module_context_free(&ctxt->til_module_context);

	if (s->connect) {
		/* XXX: it'd be better if we just reconnected periodically instead of hard failing */
		if (sync_tcp_connect(ctxt->sync_device, s->host, s->port))
			return til_module_context_free(&ctxt->til_module_context);
	}

	ctxt->scene_track = sync_get_trackf(ctxt->sync_device, "%s:scene", setup->path);
	if (!ctxt->scene_track)
		return til_module_context_free(&ctxt->til_module_context);

	/* set the stream hooks early so context creates can establish taps early */
	til_stream_set_hooks(stream, &rkt_stream_hooks, ctxt);

	for (size_t i = 0; i < ctxt->n_scenes; i++) {
		int	r;

		r = til_module_create_context(s->scenes[i].module, stream, rand_r(&seed), ticks, 0, s->scenes[i].setup, &ctxt->scenes[i].module_ctxt);
		if (r < 0)
			return til_module_context_free(&ctxt->til_module_context);
	}

	ctxt->rows_per_ms = s->rows_per_ms;
	ctxt->last_ticks = ticks;

	rkt_update_rocket(ctxt, ticks);

	/* are we running the scener too? */
	if (s->scener_listen)
		rkt_scener_startup(ctxt);

	return &ctxt->til_module_context;
}


static void rkt_destroy_context(til_module_context_t *context)
{
	rkt_context_t	*ctxt = (rkt_context_t *)context;

	rkt_scener_shutdown(ctxt);

	if (ctxt->sync_device)
		sync_destroy_device(ctxt->sync_device);

	for (size_t i = 0; i < ctxt->n_scenes; i++)
		til_module_context_free(ctxt->scenes[i].module_ctxt);

	free(context);
}


static void rkt_render_fragment(til_module_context_t *context, til_stream_t *stream, unsigned ticks, unsigned cpu, til_fb_fragment_t **fragment_ptr)
{
	rkt_context_t	*ctxt = (rkt_context_t *)context;

	rkt_update_rocket(ctxt, ticks);
	/* this is deliberately done before scener, so scener may override the scene shown */
	ctxt->scene = (unsigned)sync_get_val(ctxt->scene_track, ctxt->rocket_row);
	rkt_scener_update(ctxt);

	/* this drives our per-rocket-track updates, with the tracks registered as owner_foo on the pipes, respectively */
	til_stream_for_each_pipe(stream, rkt_pipe_update, ctxt);

	{
		unsigned	scene = ctxt->scene;

		if (scene < ctxt->n_scenes) {
			til_module_render(ctxt->scenes[scene].module_ctxt, stream, ticks, fragment_ptr);
		} else if (scene == 99999 && !((rkt_setup_t *)context->setup)->connect && !ctxt->scener) {
			/* 99999 is treated as an "end of sequence" scene, but only honored when connect=off (player mode) */
			til_stream_end(stream);
		} else {
			txt_t	*msg = txt_newf("%s: %s @ %u",
						context->setup->path,
						scene == 99999 ? "EXIT SCENE" : "NO SCENE",
						scene);

			/* TODO: creating/destroying this every frame is dumb, but
			 * as this is a diagnostic it's not so important.
			 *
			 * Once this module deals with disconnects and transparently reconnects, it'll need
			 * to show some connection status information as well... when that gets added this will
			 * likely get reworked to become part of that status text.
			 */
			til_fb_fragment_clear(*fragment_ptr);
			txt_render_fragment(msg, *fragment_ptr, 0xffffffff,
					    0, 0,
					    (txt_align_t){
						.horiz = TXT_HALIGN_LEFT,
						.vert = TXT_VALIGN_TOP,
					   });
			txt_free(msg);
		}
	}
}


static void rkt_setup_free(til_setup_t *setup)
{
	rkt_setup_t	*s = (rkt_setup_t *)setup;

	if (s) {
		for (size_t i = 0; i < s->n_scenes; i++)
			til_setup_free(s->scenes[i].setup);

		free((void *)s->base);
		free((void *)s->host);
		free(setup);
	}
}


static int rkt_setup(const til_settings_t *settings, til_setting_t **res_setting, const til_setting_desc_t **res_desc, til_setup_t **res_setup)
{
	const til_settings_t	*scenes_settings;
	const char		*bool_values[] = {
					"off",
					"on",
					NULL
				};
	const char		*scenes;
	const char		*base;
	const char		*bpm;
	const char		*rpb;
	const char		*connect;
	const char		*host;
	const char		*port;
	const char		*listen;
	const char		*listen_address;
	const char		*listen_port;
	int			r;

	/* This is largely taken from compose::layers, but might just go away when I add tables to rocket,
	 * or maybe they can coexist.
	 */
	r = til_settings_get_and_describe_value(settings,
						&(til_setting_spec_t){
							.name = "Comma-separated list of modules for scenes to sequence",
							.key = "scenes",
							.preferred = "compose,compose,compose,compose",
							.annotations = NULL,
							.as_nested_settings = 1,
						},
						&scenes,
						res_setting,
						res_desc);
	if (r)
		return r;

	assert(res_setting && *res_setting && (*res_setting)->value_as_nested_settings);
	scenes_settings = (*res_setting)->value_as_nested_settings;
	{
		til_setting_t	*scene_setting;

		for (size_t i = 0; til_settings_get_value_by_idx(scenes_settings, i, &scene_setting); i++) {
			if (!scene_setting->value_as_nested_settings) {
				r = til_setting_desc_new(	scenes_settings,
								&(til_setting_spec_t){
									.as_nested_settings = 1,
								}, res_desc);
				if (r < 0)
					return r;

				*res_setting = scene_setting;

				return 1;
			}
		}

		for (size_t i = 0; til_settings_get_value_by_idx(scenes_settings, i, &scene_setting); i++) {
			til_setting_t		*scene_module_setting;
			const char		*scene_module_name = til_settings_get_value_by_idx(scene_setting->value_as_nested_settings, 0, &scene_module_setting);
			const til_module_t	*scene_module;

			if (!scene_module_name || !scene_module_setting->desc) {
				r = til_setting_desc_new(	scene_setting->value_as_nested_settings,
								&(til_setting_spec_t){
									.name = "Scene module name",
									.preferred = "none",
									.as_label = 1,
								}, res_desc);
				if (r < 0)
					return r;

				*res_setting = scene_module_name ? scene_module_setting : NULL;

				return 1;
			}

			scene_module = til_lookup_module(scene_module_name);
			if (!scene_module) {
				*res_setting = scene_module_setting;

				return -EINVAL;
			}

			if (scene_module->setup) {
				r = scene_module->setup(scene_setting->value_as_nested_settings, res_setting, res_desc, NULL);
				if (r)
					return r;
			}
		}
	}

	r = til_settings_get_and_describe_value(settings,
						&(til_setting_spec_t){
							.name = "Rocket \"base\" label",
							.key = "base",
							.preferred = "tiller",
							.annotations = NULL,
						},
						&base,
						res_setting,
						res_desc);
	if (r)
		return r;

	r = til_settings_get_and_describe_value(settings,
						&(til_setting_spec_t){
							.name = "Beats per minute",
							.key = "bpm",
							.preferred = "125",
							.annotations = NULL,
						},
						&bpm,
						res_setting,
						res_desc);
	if (r)
		return r;

	r = til_settings_get_and_describe_value(settings,
						&(til_setting_spec_t){
							.name = "Rows per beat",
							.key = "rpb",
							.preferred = "8",
							.annotations = NULL,
						},
						&rpb,
						res_setting,
						res_desc);
	if (r)
		return r;

	r = til_settings_get_and_describe_value(settings,
						&(til_setting_spec_t){
							.name = "RocketEditor connection toggle",
							.key = "connect",
							/* TODO: regex */
							.preferred = bool_values[1],
							.values = bool_values,
							.annotations = NULL,
						},
						&connect,
						res_setting,
						res_desc);
	if (r)
		return r;

	if (!strcasecmp(connect, "on")) {
		r = til_settings_get_and_describe_value(settings,
							&(til_setting_spec_t){
								.name = "Editor host",
								.key = "host",
								.preferred = "localhost",
								/* TODO: regex */
								.annotations = NULL,
							},
							&host,
							res_setting,
							res_desc);
		if (r)
			return r;

		r = til_settings_get_and_describe_value(settings,
							&(til_setting_spec_t){
								.name = "Editor port",
								.key = "port",
								.preferred = TIL_SETTINGS_STR(SYNC_DEFAULT_PORT),
								/* TODO: regex */
								.annotations = NULL,
							},
							&port,
							res_setting,
							res_desc);
		if (r)
			return r;
	}

	r = til_settings_get_and_describe_value(settings,
						&(til_setting_spec_t){
							.name = "Scene editor listen toggle",
							.key = "listen",
							/* TODO: regex */
							.preferred = bool_values[1],
							.values = bool_values,
							.annotations = NULL,
						},
						&listen,
						res_setting,
						res_desc);
	if (r)
		return r;

	if (!strcasecmp(listen, "on")) {
		r = til_settings_get_and_describe_value(settings,
							&(til_setting_spec_t){
								.name = "Listen address",
								.key = "listen_address",
								.preferred = RKT_SCENER_DEFAULT_ADDRESS,
								/* TODO: regex */
								.annotations = NULL,
							},
							&listen_address,
							res_setting,
							res_desc);
		if (r)
			return r;

		r = til_settings_get_and_describe_value(settings,
							&(til_setting_spec_t){
								.name = "Listen port",
								.key = "listen_port",
								.preferred = TIL_SETTINGS_STR(RKT_SCENER_DEFAULT_PORT),
								/* TODO: regex */
								.annotations = NULL,
							},
							&listen_port,
							res_setting,
							res_desc);
		if (r)
			return r;
	}

	if (res_setup) {
		size_t			n_scenes = til_settings_get_count(scenes_settings);
		til_setting_t		*scene_setting;
		rkt_setup_t		*setup;
		unsigned		ibpm, irpb;

		setup = til_setup_new(settings, sizeof(*setup) + n_scenes * sizeof(*setup->scenes), rkt_setup_free);
		if (!setup)
			return -ENOMEM;

		if (!strcasecmp(listen, "on")) {
			setup->scener_listen = 1;

			setup->scener_address = strdup(listen_address);
			if (!setup->scener_address) {
				til_setup_free(&setup->til_setup);

				return -ENOMEM;
			}

			sscanf(listen_port, "%hu", &setup->scener_port); /* FIXME parse errors */

			/* XXX FIXME TODO: HACK ALERT: til_settings_t probably needs to be refcounted,
			 * and this should be taking a proper reference!  The only reason this can
			 * _remotely_ work today is rototiller doesn't free its settings until exiting,
			 * and rkt is HERMETIC - so all these should persist unless _rkt_ replaces them
			 * (like when editing).  But that seems like a rather fragile way to be, and
			 * the act of distinguishing the baked til_setup_t from til_settings_t has been
			 * specifically in part to allow releasing the latter's resources once the setup
			 * is baked.  But in rkt's case, at least in creative mode, it needs to allow
			 * live editing of the setup - which isn't possible on the baked til_setup_t, only
			 * the string-oriented til_settings_t.
			 *
			 * So what's happening here is a bit of an impedance mismatch, and for now I'm just
			 * going to cast these to non-const and get things more fleshed out before trying
			 * to change the til_settings API to better accomodate these new uses.
			 * XXX FIXME TODO
			 */
			setup->settings = (til_settings_t *)settings;
			setup->scenes_settings = (til_settings_t *)scenes_settings;
		}

		setup->n_scenes = n_scenes;

		for (size_t i = 0; til_settings_get_value_by_idx(scenes_settings, i, &scene_setting); i++) {
			const char		*scene_module_name = til_settings_get_value_by_idx(scene_setting->value_as_nested_settings, 0, NULL);
			const til_module_t	*scene_module = til_lookup_module(scene_module_name);

			if (!scene_module || !strcmp(scene_module_name, "rkt")) {
				til_setup_free(&setup->til_setup);

				return -EINVAL;
			}

			r = til_module_setup_finalize(scene_module, scene_setting->value_as_nested_settings, &setup->scenes[i].setup);
			if (r < 0) {
				til_setup_free(&setup->til_setup);

				return r;
			}

			setup->scenes[i].module = scene_module;
		}

		setup->base = strdup(base);
		if (!setup->base) {
			til_setup_free(&setup->til_setup);

			return -ENOMEM;
		}

		if (!strcasecmp(connect, "on")) {
			setup->connect = 1;

			setup->host = strdup(host);
			if (!setup->host) {
				til_setup_free(&setup->til_setup);

				return -ENOMEM;
			}

			sscanf(port, "%hu", &setup->port); /* FIXME parse errors */
		}

		sscanf(bpm, "%u", &ibpm);
		sscanf(rpb, "%u", &irpb);
		setup->rows_per_ms = ((double)(ibpm * irpb)) * (1.0 / (60.0 * 1000.0));

		*res_setup = &setup->til_setup;
	}

	return 0;
}


til_module_t	rkt_module = {
	.create_context = rkt_create_context,
	.destroy_context = rkt_destroy_context,
	.render_fragment = rkt_render_fragment,
	.name = "rkt",
	.description = "GNU Rocket module sequencer",
	.setup = rkt_setup,
	.flags = TIL_MODULE_HERMETIC | TIL_MODULE_EXPERIMENTAL,	/* this needs refinement esp. if rkt gets split into a player and editor */
};
© All Rights Reserved