aboutsummaryrefslogtreecommitdiff
path: root/tracker.h
blob: 39b96bcae34f284fe9a97cd1b261e16b4dfb5956 (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
/*
 * Copyright (c) 2016 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 _TRACKER_LV2_H
#define _TRACKER_LV2_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/log/log.h"
#include "lv2/lv2plug.in/ns/ext/log/logger.h"
#include "lv2/lv2plug.in/ns/ext/patch/patch.h"
#include "lv2/lv2plug.in/ns/ext/time/time.h"
#include "lv2/lv2plug.in/ns/ext/midi/midi.h"
#include "lv2/lv2plug.in/ns/ext/state/state.h"
#include "lv2/lv2plug.in/ns/ext/options/options.h"
#include "lv2/lv2plug.in/ns/ext/parameters/parameters.h"
#include "lv2/lv2plug.in/ns/extensions/ui/ui.h"
#include "lv2/lv2plug.in/ns/lv2core/lv2.h"

#define TRACKER_URI               "http://open-music-kontrollers.ch/lv2/tracker"
#define TRACKER_PREFIX            TRACKER_URI"#"

#define TRACKER__tracker          TRACKER_PREFIX"tracker"
#define TRACKER__tracker_ui       TRACKER_PREFIX"tracker_ui"

#define TRACKER__offset           TRACKER_PREFIX"offset"
#define TRACKER__beats            TRACKER_PREFIX"beats"
#define TRACKER__fractions        TRACKER_PREFIX"fractions"
#define TRACKER__repeats          TRACKER_PREFIX"repeats"
#define TRACKER__position         TRACKER_PREFIX"position"
#define TRACKER__mode             TRACKER_PREFIX"mode"
#define TRACKER__graph            TRACKER_PREFIX"graph"

#define MAX_TRACKS 16
#define MAX_NPROPS 7
#define GRAPH_SIZE 0x10000 // 64K
#define MODE_SIZE (sizeof(LV2_Atom_Int) * MAX_TRACKS)

#include <props.lv2/props.h>

typedef struct _plugstate_t plugstate_t;
typedef struct _atom_ser_t atom_ser_t;

struct _plugstate_t {
	int32_t offset;
	int32_t beats;
	int32_t fractions;
	int32_t repeats;
	double position;
	uint8_t mode [MODE_SIZE];
	uint8_t graph [GRAPH_SIZE];
};

struct _atom_ser_t {
	uint32_t size;
	uint32_t offset;
	union {
		const LV2_Atom *atom;
		const LV2_Atom_Event *ev;
		uint8_t *buf;
	};
};

extern const LV2UI_Descriptor tracker_tracker_ui;
extern const LV2UI_Descriptor tracker_sequencer_ui;

static LV2_Atom_Forge_Ref
_sink(LV2_Atom_Forge_Sink_Handle handle, const void *buf, uint32_t size)
{
	atom_ser_t *ser = handle;

	const LV2_Atom_Forge_Ref ref = ser->offset + 1;

	const uint32_t new_offset = ser->offset + size;
	if(new_offset > ser->size)
	{
		uint32_t new_size = ser->size << 1;
		while(new_offset > new_size)
			new_size <<= 1;

		if(!(ser->buf = realloc(ser->buf, new_size)))
			return 0; // realloc failed

		ser->size = new_size;
	}

	memcpy(ser->buf + ser->offset, buf, size);
	ser->offset = new_offset;

	return ref;
}

static LV2_Atom *
_deref(LV2_Atom_Forge_Sink_Handle handle, LV2_Atom_Forge_Ref ref)
{
	atom_ser_t *ser = handle;

	const uint32_t offset = ref - 1;

	return (LV2_Atom *)(ser->buf + offset);
}

#endif // _TRACKER_LV2_H