aboutsummaryrefslogtreecommitdiff
path: root/src/graph.c
blob: 0e1a41ec9fe2b3ae29b661b6e67e4ac8c869fe5b (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
/*
 * melisma - extensible and scriptable audio engine
 * Copyright (C) 2026  Ludvig Gunne Lindström
 *
 * 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 <https://www.gnu.org/licenses/>.
 */

#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <stdbool.h>

#include <lua.h>
#include <lauxlib.h>

#include "melisma.h"
#include "graph.h"
#include "mem.h"

#ifndef BUFFER_SIZE
#define BUFFER_SIZE 4096
#endif

struct ml_channel
{
	ml_node        *node;
	enum {IN,OUT}   dir;
	ml_channel     *src;
	ml_channel     *next;
	size_t          buf;
	int             refs;
};

struct ml_node
{
	ml_proc_fn   proc;
	void        *userp;
	ml_channel  *chans[2];
	ml_node     *next;
	bool         visited;
	bool         cycle;
	char        *label;
};

struct chunk
{
	char          *label;
	ml_proc_fn     proc;
	ml_proc_args   args;
	struct chunk  *next;
	float         *data[];
};

struct free_list
{
	size_t            buf;
	struct free_list *next;
};

static ml_node          *root;
static ml_node          *active;
static ml_node          *active_tail;
static struct free_list *free_list;
static size_t            counter;
static float            *buffers;
static struct chunk     *graph;

ml_node *ml_create_node(ml_proc_fn proc, void *userp)
{
	ml_node *node = scalloc(1,sizeof(*node));
	node->proc = proc;
	node->userp = userp;
	return node;
}

void ml_label_node(ml_node *node, const char *fmt, ...)
{
	va_list ap_alloc,ap_print;

	va_start(ap_alloc,fmt);
	va_copy(ap_print,ap_alloc);

	size_t len = 1 + vsnprintf(NULL,0,fmt,ap_alloc);
	va_end(ap_alloc);

	char *buf = smalloc(len);
	vsnprintf(buf,len,fmt,ap_print);

	node->label = buf;
}

void *ml_get_userp(ml_node *node)
{
	return node->userp;
}

ml_channel *add_channel(ml_node *node, int dir)
{
	ml_channel *chan = scalloc(1,sizeof(*chan));
	chan->dir = dir;
	chan->node = node;

	if (node->chans[dir] == NULL)
	{
		node->chans[dir] = chan;
	}
	else
	{
		ml_channel *insert = node->chans[dir];
		while (insert->next)
		{
			insert = insert->next;
		}
		insert->next = chan;
	}

	return chan;
}

ml_channel *ml_add_input(ml_node *node)
{
	return add_channel(node,IN);
}

ml_channel *ml_add_output(ml_node *node)
{
	return add_channel(node,OUT);
}

static ml_channel *get_channel(ml_node *node, size_t i, int dir)
{
	ml_channel *chan = node->chans[dir];

	while (i > 0)
	{
		if (chan == NULL)
		{
			break;
		}

		i--;
		chan = chan->next;
	}

	return chan;
}

ml_channel *ml_get_input(ml_node *node, size_t i)
{
	return get_channel(node,i,IN);
}

ml_channel *ml_get_output(ml_node *node, size_t i)
{
	return get_channel(node,i,OUT);
}

static size_t num_channels(ml_node *node, int dir)
{
	ml_channel *chan = node->chans[dir];
	size_t n = 0;

	while (chan != NULL)
	{
		n++;
		chan = chan->next;
	}

	return n;
}

size_t ml_num_inputs(ml_node *node)
{
	return num_channels(node,IN);
}

size_t ml_num_outputs(ml_node *node)
{
	return num_channels(node,OUT);
}

void ml_connect(ml_channel *src, ml_channel *dst)
{
	assert(src->dir == OUT);
	assert(dst->dir == IN);
	src->refs++;
	dst->src = src;
}

ml_node *ml_get_root_node(void)
{
	return root;
}

void init_graph(lua_State *s)
{
	root = ml_create_node(NULL,NULL);
	ml_label_node(root,"root");

	luaL_newmetatable(s,MELISMA_NODE_TYPENAME);
	luaL_newmetatable(s,MELISMA_INPUT_CHANNEL_TYPENAME);
	luaL_newmetatable(s,MELISMA_OUTPUT_CHANNEL_TYPENAME);
}

static const char *topo_sort(ml_node *node)
{
	if (node->visited)
	{
		return NULL;
	}

	if (node->cycle)
	{
		return "cycle in audio graph detected";
	}

	node->visited = true;
	node->cycle = true;

	ml_channel *in = node->chans[IN];

	while (in != NULL)
	{
		if (in->src == NULL)
		{
			return "disconnected input channel in audio graph detected";
		}

		const char *err = topo_sort(in->src->node);

		if (err != NULL)
		{
			return err;
		}

		in = in->next;
	}

	if (active_tail)
	{
		active_tail->next = node;
	}
	else
	{
		active = node;
	}

	active_tail = node;
	node->cycle = false;

	return NULL;
}

static size_t alloc_buf(void)
{
	if (free_list)
	{
		struct free_list *head = free_list;
		size_t buf = head->buf;
		free_list = head->next;
		free(head);
		return buf;
	}

	return counter++;
}

static void free_buf(size_t buf)
{
	struct free_list *head = scalloc(1,sizeof(*head));
	head->buf = buf;
	head->next = free_list;
	free_list = head;
}

static void assign_bufs()
{
	ml_node *node = active;

	while (node)
	{
		ml_channel *in, *out;
		in = node->chans[IN];
		out = node->chans[OUT];

		while (out)
		{
			out->buf = alloc_buf();
			out = out->next;
		}

		while (in)
		{
			if (--in->src->refs == 0)
			{
				free_buf(in->src->buf);
			}
			in = in->next;
		}

		node = node->next;
	}
}

static void make_chunks(void)
{
	struct chunk *chunk, *tail = NULL;
	struct ml_node *node = active;

	while (node)
	{
		size_t icnt = ml_num_inputs(node);
		size_t ocnt = ml_num_outputs(node);
		size_t nchans = icnt + ocnt;
		size_t size = sizeof(*chunk) + sizeof(chunk->data[0]) * nchans;

		chunk = scalloc(1,size);
		chunk->label = node->label ? strdup(node->label) : NULL;
		chunk->proc = node->proc;
		chunk->args.userp = node->userp;
		chunk->args.icnt = icnt;
		chunk->args.ocnt = ocnt;
		chunk->args.in = chunk->data;
		chunk->args.out = chunk->data + icnt;

		ml_channel *in, *out;
		size_t i = 0;

		in = node->chans[IN];

		while (in != NULL)
		{
			chunk->data[i++] = &buffers[BUFFER_SIZE*in->src->buf];
			in = in->next;
		}

		out = node->chans[OUT];

		while (out != NULL)
		{
			chunk->data[i++] = &buffers[BUFFER_SIZE*out->buf];
			out = out->next;
		}

		assert(i == nchans);

		if (tail == NULL)
		{
			graph = chunk;
		}
		else
		{
			tail->next = chunk;
		}

		tail = chunk;
		node = node->next;
	}
}

const char *build_graph(void)
{
	const char *err = topo_sort(root);

	if (err != NULL)
	{
		return err;
	}

	assign_bufs();

	buffers = malloc(sizeof(*buffers)*BUFFER_SIZE*counter);

	make_chunks();

	return NULL;
}

static void proc_once(size_t n)
{
	struct chunk *c = graph;
	while (c)
	{
		if (c->proc)
		{
			c->args.nframes = n;
			c->proc(&c->args);
		}

		c = c->next;
	}
}

void proc_graph(size_t n)
{
	while (n > 0)
	{
		size_t m = n > BUFFER_SIZE ? BUFFER_SIZE : n;
		proc_once(m);
		n -= m;
	}
}

static void add_to_layout(lua_State *s, int layout, ml_node *node)
{
	int top = lua_absindex(s,lua_gettop(s));

	lua_pushlightuserdata(s,node);
	lua_gettable(s,layout);

	if (!lua_isnil(s,-1))
	{
		return;
	}

	lua_newtable(s);
	int tbl = lua_absindex(s,lua_gettop(s));

	if (node->label)
	{
		lua_pushstring(s,node->label);
		lua_setfield(s,tbl,"label");
	}

	lua_newtable(s);
	int list = lua_absindex(s,lua_gettop(s));
	ml_channel *in = node->chans[IN];
	int i = 1;

	while (in)
	{
		add_to_layout(s,layout,in->src->node);
		lua_pushlightuserdata(s,in->src->node);
		lua_rawseti(s,list,i++);
		in = in->next;
	}

	lua_pushvalue(s,list);
	lua_setfield(s,tbl,"inputs");

	lua_pushlightuserdata(s,node);
	lua_pushvalue(s,tbl);
	lua_settable(s,layout);

	lua_settop(s,top);
}

int l_graph_layout(lua_State *s)
{
	lua_newtable(s);
	int layout = lua_absindex(s,lua_gettop(s));
	add_to_layout(s,layout,root);
	lua_pushvalue(s,layout);
	return 1;
}