aboutsummaryrefslogtreecommitdiff
path: root/timely.h
blob: 7dd00eea0c291f8cc4a9e3d92daa7f957d40d4f8 (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
/*
 * 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 _LV2_TIMELY_H_
#define _LV2_TIMELY_H_

#include <math.h>

#include <lv2/lv2plug.in/ns/lv2core/lv2.h>
#include <lv2/lv2plug.in/ns/ext/urid/urid.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/time/time.h>

typedef enum _timely_mask_t timely_mask_t;
typedef struct _timely_t timely_t;
typedef void (*timely_cb_t)(timely_t *timely, int64_t frames, LV2_URID type,
	void *data);

enum _timely_mask_t {
	TIMELY_MASK_BAR_BEAT					= (1 << 0),
	TIMELY_MASK_BAR								= (1 << 1),
	TIMELY_MASK_BEAT_UNIT					= (1 << 2),
	TIMELY_MASK_BEATS_PER_BAR			= (1 << 3),
	TIMELY_MASK_BEATS_PER_MINUTE	= (1 << 4),
	TIMELY_MASK_FRAME							= (1 << 5),
	TIMELY_MASK_FRAMES_PER_SECOND	= (1 << 6),
	TIMELY_MASK_SPEED							= (1 << 7),
	TIMELY_MASK_BAR_BEAT_WHOLE		= (1 << 8),
	TIMELY_MASK_BAR_WHOLE					= (1 << 9)
};

struct _timely_t {
	struct {
		LV2_URID atom_object;
		LV2_URID atom_blank;
		LV2_URID atom_resource;

		LV2_URID time_position;
		LV2_URID time_barBeat;
		LV2_URID time_bar;
		LV2_URID time_beatUnit;
		LV2_URID time_beatsPerBar;
		LV2_URID time_beatsPerMinute;
		LV2_URID time_frame;
		LV2_URID time_framesPerSecond;
		LV2_URID time_speed;
	} urid;

	struct {
		float bar_beat;
		int64_t bar;

		int32_t beat_unit;
		float beats_per_bar;
		float beats_per_minute;

		int64_t frame;
		float frames_per_second;

		float speed;
	} pos;

	float multiplier;

	double frames_per_beat;
	double frames_per_bar;

	struct {
		double beat;
		double bar;
	} offset;

	bool first;
	timely_mask_t mask;
	timely_cb_t cb;
	void *data;
};

#define TIMELY_URI_BAR_BEAT(timely)						((timely)->urid.time_barBeat)
#define TIMELY_URI_BAR(timely)								((timely)->urid.time_bar)
#define TIMELY_URI_BEAT_UNIT(timely)					((timely)->urid.time_beatUnit)
#define TIMELY_URI_BEATS_PER_BAR(timely)			((timely)->urid.time_beatsPerBar)
#define TIMELY_URI_BEATS_PER_MINUTE(timely)		((timely)->urid.time_beatsPerMinute)
#define TIMELY_URI_FRAME(timely)							((timely)->urid.time_frame)
#define TIMELY_URI_FRAMES_PER_SECOND(timely)	((timely)->urid.time_framesPerSecond)
#define TIMELY_URI_SPEED(timely)							((timely)->urid.time_speed)

#define TIMELY_BAR_BEAT_RAW(timely)						((timely)->pos.bar_beat)
#define TIMELY_BAR_BEAT(timely)								(floor((timely)->pos.bar_beat) \
	+ (timely)->offset.beat / (timely)->frames_per_beat)
#define TIMELY_BAR(timely)										((timely)->pos.bar)
#define TIMELY_BEAT_UNIT(timely)							((timely)->pos.beat_unit)
#define TIMELY_BEATS_PER_BAR(timely)					((timely)->pos.beats_per_bar)
#define TIMELY_BEATS_PER_MINUTE(timely)				((timely)->pos.beats_per_minute)
#define TIMELY_FRAME(timely)									((timely)->pos.frame)
#define TIMELY_FRAMES_PER_SECOND(timely)			((timely)->pos.frames_per_second)
#define TIMELY_SPEED(timely)									((timely)->pos.speed)

#define TIMELY_FRAMES_PER_BEAT(timely)				((timely)->frames_per_beat)
#define TIMELY_FRAMES_PER_BAR(timely)					((timely)->frames_per_bar)

static inline void
_timely_deatomize_body(timely_t *timely, int64_t frames, uint32_t size,
	const LV2_Atom_Object_Body *body)
{
	const LV2_Atom_Float *bar_beat = NULL;
	const LV2_Atom_Long *bar = NULL;
	const LV2_Atom_Int *beat_unit = NULL;
	const LV2_Atom_Float *beats_per_bar = NULL;
	const LV2_Atom_Float *beats_per_minute = NULL;
	const LV2_Atom_Long *frame = NULL;
	const LV2_Atom_Float *frames_per_second = NULL;
	const LV2_Atom_Float *speed = NULL;

	lv2_atom_object_body_get(size, body,
		timely->urid.time_barBeat, &bar_beat,
		timely->urid.time_bar, &bar,
		timely->urid.time_beatUnit, &beat_unit,
		timely->urid.time_beatsPerBar, &beats_per_bar,
		timely->urid.time_beatsPerMinute, &beats_per_minute,
		timely->urid.time_frame, &frame,
		timely->urid.time_framesPerSecond, &frames_per_second,
		timely->urid.time_speed, &speed,
		0);

	// send speed first upon transport stop
	if(speed && (speed->body != timely->pos.speed) && (speed->body == 0.f) )
	{
		timely->pos.speed = speed->body;
		if(timely->mask & TIMELY_MASK_SPEED)
			timely->cb(timely, frames, timely->urid.time_speed, timely->data);
	}

	if(beat_unit)
	{
		const int32_t _beat_unit = beat_unit->body * timely->multiplier;
		if(_beat_unit != timely->pos.beat_unit)
		{
			timely->pos.beat_unit = _beat_unit;
			if(timely->mask & TIMELY_MASK_BEAT_UNIT)
				timely->cb(timely, frames, timely->urid.time_beatUnit, timely->data);
		}
	}

	if(beats_per_bar)
	{
		const float _beats_per_bar = beats_per_bar->body * timely->multiplier;
		if(_beats_per_bar != timely->pos.beats_per_bar)
		{
			timely->pos.beats_per_bar = _beats_per_bar;
			if(timely->mask & TIMELY_MASK_BEATS_PER_BAR)
				timely->cb(timely, frames, timely->urid.time_beatsPerBar, timely->data);
		}
	}

	if(beats_per_minute && (beats_per_minute->body != timely->pos.beats_per_minute) )
	{
		timely->pos.beats_per_minute = beats_per_minute->body;
		if(timely->mask & TIMELY_MASK_BEATS_PER_MINUTE)
			timely->cb(timely, frames, timely->urid.time_beatsPerMinute, timely->data);
	}

	if(frame && (frame->body != timely->pos.frame) )
	{
		timely->pos.frame = frame->body;
		if(timely->mask & TIMELY_MASK_FRAME)
			timely->cb(timely, frames, timely->urid.time_frame, timely->data);
	}

	if(frames_per_second && (frames_per_second->body != timely->pos.frames_per_second) )
	{
		timely->pos.frames_per_second = frames_per_second->body;
		if(timely->mask & TIMELY_MASK_FRAMES_PER_SECOND)
			timely->cb(timely, frames, timely->urid.time_framesPerSecond, timely->data);
	}

	if(bar && (bar->body != timely->pos.bar) )
	{
		timely->pos.bar = bar->body;
		if(timely->mask & TIMELY_MASK_BAR)
			timely->cb(timely, frames, timely->urid.time_bar, timely->data);
	}

	if(bar_beat)
	{
		const float _bar_beat = bar_beat->body * timely->multiplier;
		if(_bar_beat != timely->pos.bar_beat)
		{
			timely->pos.bar_beat = _bar_beat;
			if(timely->mask & TIMELY_MASK_BAR_BEAT)
				timely->cb(timely, frames, timely->urid.time_barBeat, timely->data);
		}
	}

	// send speed last upon transport start
	if(speed && (speed->body != timely->pos.speed) && (speed->body != 0.f) )
	{
		timely->pos.speed = speed->body;
		if(timely->mask & TIMELY_MASK_SPEED)
			timely->cb(timely, frames, timely->urid.time_speed, timely->data);
	}
}

static inline void
_timely_refresh(timely_t *timely)
{
	const float speed = (timely->pos.speed != 0.f)
		? timely->pos.speed
		: 1.f; // prevent divisions through zero later on

	timely->frames_per_beat = 240.0 * timely->pos.frames_per_second
		/ (timely->pos.beats_per_minute * timely->pos.beat_unit * speed);
	timely->frames_per_bar = timely->frames_per_beat * timely->pos.beats_per_bar;

	// bar
	timely->offset.bar = timely->pos.bar_beat * timely->frames_per_beat;

	// beat
	double integral;
	double beat_beat = modf(timely->pos.bar_beat, &integral);
	(void)integral;
	timely->offset.beat = beat_beat * timely->frames_per_beat;
}

static inline void
timely_init(timely_t *timely, LV2_URID_Map *map, double rate,
	timely_mask_t mask, timely_cb_t cb, void *data)
{
	assert(cb != NULL);

	timely->mask = mask;
	timely->cb = cb;
	timely->data = data;

	timely->urid.atom_object = map->map(map->handle, LV2_ATOM__Object);
	timely->urid.atom_blank = map->map(map->handle, LV2_ATOM__Blank);
	timely->urid.atom_resource = map->map(map->handle, LV2_ATOM__Resource);
	timely->urid.time_position = map->map(map->handle, LV2_TIME__Position);
	timely->urid.time_barBeat = map->map(map->handle, LV2_TIME__barBeat);
	timely->urid.time_bar = map->map(map->handle, LV2_TIME__bar);
	timely->urid.time_beatUnit = map->map(map->handle, LV2_TIME__beatUnit);
	timely->urid.time_beatsPerBar = map->map(map->handle, LV2_TIME__beatsPerBar);
	timely->urid.time_beatsPerMinute = map->map(map->handle, LV2_TIME__beatsPerMinute);
	timely->urid.time_frame = map->map(map->handle, LV2_TIME__frame);
	timely->urid.time_framesPerSecond = map->map(map->handle, LV2_TIME__framesPerSecond);
	timely->urid.time_speed = map->map(map->handle, LV2_TIME__speed);

	timely->multiplier = 1.f;

	timely->pos.speed = 0.f;
	timely->pos.bar_beat = 0.f;
	timely->pos.bar = 0;
	timely->pos.beat_unit = 4;
	timely->pos.beats_per_bar = 4.f;
	timely->pos.beats_per_minute = 120.f;
	timely->pos.frame = 0;
	timely->pos.frames_per_second = rate;

	_timely_refresh(timely);

	timely->first = true;
}

static inline void
timely_set_multiplier(timely_t *timely, float multiplier)
{
	const float mul = multiplier / timely->multiplier;

	timely->pos.bar_beat *= mul;
	timely->pos.beat_unit *= mul;
	timely->pos.beats_per_bar *= mul;

	timely->multiplier = multiplier;

	_timely_refresh(timely);

	timely->first = true;
}

static inline int
timely_advance_body(timely_t *timely, uint32_t size, uint32_t type,
	const LV2_Atom_Object_Body *body, uint32_t from, uint32_t to)
{
	if(timely->first)
	{
		timely->first = false;

		// send initial values
		if(timely->mask & TIMELY_MASK_SPEED)
			timely->cb(timely, 0, timely->urid.time_speed, timely->data);

		if(timely->mask & TIMELY_MASK_BEAT_UNIT)
			timely->cb(timely, 0, timely->urid.time_beatUnit, timely->data);

		if(timely->mask & TIMELY_MASK_BEATS_PER_BAR)
			timely->cb(timely, 0, timely->urid.time_beatsPerBar, timely->data);

		if(timely->mask & TIMELY_MASK_BEATS_PER_MINUTE)
			timely->cb(timely, 0, timely->urid.time_beatsPerMinute, timely->data);

		if(timely->mask & TIMELY_MASK_FRAME)
			timely->cb(timely, 0, timely->urid.time_frame, timely->data);

		if(timely->mask & TIMELY_MASK_FRAMES_PER_SECOND)
			timely->cb(timely, 0, timely->urid.time_framesPerSecond, timely->data);

		if(timely->mask & TIMELY_MASK_BAR)
			timely->cb(timely, 0, timely->urid.time_bar, timely->data);

		if(timely->mask & TIMELY_MASK_BAR_BEAT)
			timely->cb(timely, 0, timely->urid.time_barBeat, timely->data);
	}

	// are we rolling?
	if(timely->pos.speed != 0.f)
	{
		if( (timely->offset.bar == 0) && (timely->pos.bar == 0) )
		{
			if(timely->mask & (TIMELY_MASK_BAR | TIMELY_MASK_BAR_WHOLE) )
				timely->cb(timely, from, timely->urid.time_bar, timely->data);
		}

		if( (timely->offset.beat == 0) && (timely->pos.bar_beat == 0) )
		{
			if(timely->mask & (TIMELY_MASK_BAR_BEAT | TIMELY_MASK_BAR_BEAT_WHOLE) )
				timely->cb(timely, from, timely->urid.time_barBeat, timely->data);
		}

		unsigned update_frame = to;
		for(unsigned i=from; i<to; i++)
		{
			if(timely->offset.bar >= timely->frames_per_bar)
			{
				timely->pos.bar += 1;
				timely->offset.bar -= timely->frames_per_bar;

				if(timely->mask & TIMELY_MASK_FRAME)
					timely->cb(timely, (update_frame = i), timely->urid.time_frame, timely->data);

				if(timely->mask & TIMELY_MASK_BAR_WHOLE)
					timely->cb(timely, i, timely->urid.time_bar, timely->data);
			}

			if( (timely->offset.beat >= timely->frames_per_beat) )
			{
				timely->pos.bar_beat = floor(timely->pos.bar_beat) + 1;
				timely->offset.beat -= timely->frames_per_beat;

				if(timely->pos.bar_beat >= timely->pos.beats_per_bar)
					timely->pos.bar_beat -= timely->pos.beats_per_bar;

				if( (timely->mask & TIMELY_MASK_FRAME) && (update_frame != i) )
					timely->cb(timely, (update_frame = i), timely->urid.time_frame, timely->data);

				if(timely->mask & TIMELY_MASK_BAR_BEAT_WHOLE)
					timely->cb(timely, i, timely->urid.time_barBeat, timely->data);
			}

			timely->offset.bar += 1;
			timely->offset.beat += 1;
			timely->pos.frame += 1;
		}
	}

	// is this a time position event?
	if(  ( (type == timely->urid.atom_object)
			|| (type == timely->urid.atom_blank)
			|| (type == timely->urid.atom_resource) )
		&& body && (body->otype == timely->urid.time_position) )
	{
		_timely_deatomize_body(timely, to, size, body);
		_timely_refresh(timely);

		return 1; // handled a time position event
	}

	return 0; // did not handle a time position event
}

static inline int
timely_advance(timely_t *timely, const LV2_Atom_Object *obj,
	uint32_t from, uint32_t to)
{
	if(obj)
		return timely_advance_body(timely, obj->atom.size, obj->atom.type, &obj->body, from, to);

	return timely_advance_body(timely, 0, 0, NULL, from, to);
}

#endif // _LV2_TIMELY_H_