aboutsummaryrefslogtreecommitdiff
path: root/chim_dump.c
blob: 303058ad50988de9506d836614787d46317b71d5 (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
/*
 * Copyright (c) 2017 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 <math.h>

#include <chim.h>
#include <props.h>

#include <osc.lv2/osc.h>
#include <osc.lv2/util.h>
#include <osc.lv2/endian.h>

#include <canvas.lv2/forge.h>
#include <canvas.lv2/forge.h>

#define MAX_NPROPS 0

typedef struct _plugstate_t plugstate_t;
typedef struct _plughandle_t plughandle_t;

struct _plugstate_t {
};

struct _plughandle_t {
	LV2_URID_Map *map;
	LV2_Atom_Forge forge;
	LV2_Atom_Forge_Ref ref;

	LV2_Log_Log *log;
	LV2_Log_Logger logger;

	const LV2_Atom_Sequence *event_in;
	LV2_Atom_Sequence *event_out;

	LV2_OSC_URID osc_urid;
	LV2_Canvas_URID canvas_urid;

	unsigned n;
	int16_t sens [160];
	bool needs_sync;

	plugstate_t state;
	plugstate_t stash;

	uint32_t overflow;
	uint32_t overflowsec;
	uint32_t counter;

	PROPS_T(props, MAX_NPROPS);
};

static const props_def_t defs [MAX_NPROPS] = {
	// empty
};

static LV2_Handle
instantiate(const LV2_Descriptor* descriptor, double rate,
	const char *bundle_path, const LV2_Feature *const *features)
{
	plughandle_t *handle = calloc(1, sizeof(plughandle_t));
	if(!handle)
		return NULL;
	mlock(handle, sizeof(plughandle_t));

	for(unsigned i=0; features[i]; i++)
	{
		if(!strcmp(features[i]->URI, LV2_URID__map))
			handle->map = features[i]->data;
		else if(!strcmp(features[i]->URI, LV2_LOG__log))
			handle->log = features[i]->data;
	}

	if(!handle->map)
	{
		fprintf(stderr,
			"%s: Host does not support urid:map\n", descriptor->URI);
		free(handle);
		return NULL;
	}

	if(handle->log)
		lv2_log_logger_init(&handle->logger, handle->map, handle->log);

	const float update_rate = 120.f; //FIXME read from options
	handle->overflow = rate / update_rate;
	handle->overflowsec = rate;

	lv2_atom_forge_init(&handle->forge, handle->map);
	lv2_osc_urid_init(&handle->osc_urid, handle->map);
	lv2_canvas_urid_init(&handle->canvas_urid, handle->map);

	if(!props_init(&handle->props, descriptor->URI,
		defs, MAX_NPROPS, &handle->state, &handle->stash,
		handle->map, handle))
	{
		fprintf(stderr, "failed to initialize property structure\n");
		free(handle);
		return NULL;
	}

	return handle;
}

static void
connect_port(LV2_Handle instance, uint32_t port, void *data)
{
	plughandle_t *handle = (plughandle_t *)instance;

	switch(port)
	{
		case 0:
			handle->event_in = (const void *)data;
			break;
		case 1:
			handle->event_out = data;
			break;
		default:
			break;
	}
}

static void
_meth(const char *path, const LV2_Atom_Tuple *arguments, void *data)
{
	plughandle_t *handle = data;

	if(strcmp(path, "/dump") != 0)
		return;

	int32_t fid;
	uint32_t sz;
	union {
		const uint8_t *u8;
		const int16_t *i16;
	} vec;

	const LV2_Atom *itm = lv2_atom_tuple_begin(arguments);
	itm = lv2_osc_int32_get(&handle->osc_urid, itm, &fid);
	itm = lv2_osc_blob_get(&handle->osc_urid, itm, &sz, &vec.u8);

	handle->n = sz / sizeof(int16_t);
	for(unsigned i=0; i<handle->n; i++)
		handle->sens[i] = be16toh(vec.i16[i]);

	handle->needs_sync = true;
}

static LV2_Atom_Forge_Ref
_render(plughandle_t *handle, uint32_t frames, const int16_t *sens, unsigned n)
{
	LV2_Atom_Forge *forge = &handle->forge;
	LV2_Canvas_URID *canvas_urid = &handle->canvas_urid;
	LV2_Atom_Forge_Frame frame [2];

	LV2_Atom_Forge_Ref ref = lv2_atom_forge_frame_time(forge, frames);;
	if(ref)
		ref = lv2_atom_forge_object(forge, &frame[0], 0, handle->props.urid.patch_set);
	{
		if(ref)
			ref = lv2_atom_forge_key(forge, handle->props.urid.patch_property);
		if(ref)
			ref = lv2_atom_forge_urid(forge, canvas_urid->Canvas_graph);

		if(ref)
			ref = lv2_atom_forge_key(forge, handle->props.urid.patch_value);
		if(ref)
			ref = lv2_atom_forge_tuple(forge, &frame[1]);
		{
			// draw background
			if(ref)
				ref = lv2_canvas_forge_style(forge, canvas_urid, 0x222222ff);
			if(ref)
				ref = lv2_canvas_forge_rectangle(forge, canvas_urid, 0.f, 0.f, 1.f, 1.f);
			if(ref)
				ref = lv2_canvas_forge_fill(forge, canvas_urid);

			// draw middle line
			if(ref)
				ref = lv2_canvas_forge_lineWidth(forge, canvas_urid, 0.001f);
			if(ref)
				ref = lv2_canvas_forge_style(forge, canvas_urid, 0x808080ff);
			if(ref)
				ref = lv2_canvas_forge_moveTo(forge, canvas_urid, 0.f, 0.5f);
			if(ref)
				ref = lv2_canvas_forge_lineTo(forge, canvas_urid, 1.f, 0.5f);
			if(ref)
				ref = lv2_canvas_forge_stroke(forge, canvas_urid);

			// upper/lower bound
			const float yy = (float)0x7f0 / 0xfff;
			const float y0 = 0.5f + yy;
			const float y1 = 0.5f - yy;
			if(ref)
				ref = lv2_canvas_forge_moveTo(forge, canvas_urid, 0.f, y0);
			if(ref)
				ref = lv2_canvas_forge_lineTo(forge, canvas_urid, 1.f, y0);
			if(ref)
				ref = lv2_canvas_forge_stroke(forge, canvas_urid);

			// lower bound
			if(ref)
				ref = lv2_canvas_forge_moveTo(forge, canvas_urid, 0.f, y1);
			if(ref)
				ref = lv2_canvas_forge_lineTo(forge, canvas_urid, 1.f, y1);
			if(ref)
				ref = lv2_canvas_forge_stroke(forge, canvas_urid);

			n = 160;
			if(n > 0)
			{
				// draw module boundaries
				const unsigned m = n / 16;
				const float Dx = 1.f / m;

				if(ref)
					ref = lv2_canvas_forge_save(forge, canvas_urid);
				if(ref)
					ref = lv2_canvas_forge_lineDash(forge, canvas_urid, 0.01f, 0.01f);

				for(unsigned i=1; i<m; i++)
				{
					const float x = i*Dx;

					if(ref)
						ref = lv2_canvas_forge_moveTo(forge, canvas_urid, x, 0.f);
					if(ref)
						ref = lv2_canvas_forge_lineTo(forge, canvas_urid, x, 1.f);
					if(ref)
						ref = lv2_canvas_forge_stroke(forge, canvas_urid);
				}

				if(ref)
					ref = lv2_canvas_forge_restore(forge, canvas_urid);

				// draw sensor histogram
				const float dx = 1.f / n;
				const float sx = dx / 2;

				if(ref)
					ref = lv2_canvas_forge_reset(forge, canvas_urid); // to get rid of line dash
				if(ref)
					ref = lv2_canvas_forge_lineWidth(forge, canvas_urid, sx);
				if(ref)
					ref = lv2_canvas_forge_style(forge, canvas_urid, 0xffffffff);

				for(unsigned i=0; i<n; i++)
				{
					const float x = (i + 0.5f)*dx;
					const float y = (float)sens[i] / 0xfff + 0.5f;
					const uint8_t a = fabsf((float)sens[i] / 0xfff) * 0xff;
					const uint8_t b = 0xff - a;
					uint64_t col;
					if(abs(sens[i]) >= 0x7f0)
						col =  0xff00ffff;
					else if (sens[i] < 0)
						col = (0xff << 24) | (b << 16) | (b << 8) | 0xff;
					else
						col = (b << 24) | (0xff << 16) | (b << 8) | 0xff;

					if(ref)
						ref = lv2_canvas_forge_moveTo(forge, canvas_urid, x, 0.5f);
					if(ref)
						ref = lv2_canvas_forge_lineTo(forge, canvas_urid, x, y);
					if(ref)
						ref = lv2_canvas_forge_style(forge, canvas_urid, col);
					if(ref)
						ref = lv2_canvas_forge_stroke(forge, canvas_urid);
				}
			}
		}
		if(ref)
			lv2_atom_forge_pop(forge, &frame[1]);
	}
	if(ref)
		lv2_atom_forge_pop(forge, &frame[0]);

	return ref;
}

static void
run(LV2_Handle instance, uint32_t nsamples)
{
	plughandle_t *handle = instance;

	const uint32_t capacity = handle->event_out->atom.size;
	lv2_atom_forge_set_buffer(&handle->forge, (uint8_t *)handle->event_out, capacity);
	LV2_Atom_Forge_Frame frame;
	handle->ref = lv2_atom_forge_sequence_head(&handle->forge, &frame, 0);

	props_idle(&handle->props, &handle->forge, 0, &handle->ref);

	LV2_ATOM_SEQUENCE_FOREACH(handle->event_in, ev)
	{
		const LV2_Atom *atom = &ev->body;
		const LV2_Atom_Object *obj = (const LV2_Atom_Object *)atom;

		if(!props_advance(&handle->props, &handle->forge, ev->time.frames, obj, &handle->ref))
		{
			if(lv2_osc_is_message_or_bundle_type(&handle->osc_urid, obj->body.otype))
			{
				lv2_osc_unroll(&handle->osc_urid, obj, _meth, handle);
			}
		}
	}

	handle->counter += nsamples;

	if(handle->counter >= handle->overflowsec) // update every sec
	{
		if(handle->ref)
			handle->ref = _render(handle, nsamples-1, handle->sens, handle->n);

		handle->counter -= handle->overflowsec;
	}
	else if(handle->needs_sync && (handle->counter >= handle->overflow) )
	{
		if(handle->ref)
			handle->ref = _render(handle, nsamples-1, handle->sens, handle->n);

		handle->counter -= handle->overflow;
		handle->needs_sync = false;
	}

	if(handle->ref)
		lv2_atom_forge_pop(&handle->forge, &frame);
	else
	{
		if(handle->log)
			lv2_log_trace(&handle->logger, "%s: output buffer overflow\n", __func__);
		lv2_atom_sequence_clear(handle->event_out);
	}
}

static void
cleanup(LV2_Handle instance)
{
	plughandle_t *handle = instance;

	munlock(handle, sizeof(plughandle_t));
	free(handle);
}

static LV2_State_Status
_state_save(LV2_Handle instance, LV2_State_Store_Function store,
	LV2_State_Handle state, uint32_t flags,
	const LV2_Feature *const *features)
{
	plughandle_t *handle = instance;

	return props_save(&handle->props, store, state, flags, features);
}

static LV2_State_Status
_state_restore(LV2_Handle instance, LV2_State_Retrieve_Function retrieve,
	LV2_State_Handle state, uint32_t flags,
	const LV2_Feature *const *features)
{
	plughandle_t *handle = instance;

	return props_restore(&handle->props, retrieve, state, flags, features);
}

static const LV2_State_Interface state_iface = {
	.save = _state_save,
	.restore = _state_restore
};

static const void*
extension_data(const char* uri)
{
	if(!strcmp(uri, LV2_STATE__interface))
		return &state_iface;

	return NULL;
}

const LV2_Descriptor chim_dump = {
	.URI						= CHIM_DUMP_URI,
	.instantiate		= instantiate,
	.connect_port		= connect_port,
	.activate				= NULL,
	.run						= run,
	.deactivate			= NULL,
	.cleanup				= cleanup,
	.extension_data	= extension_data
};