summaryrefslogtreecommitdiff
path: root/libs/ardour/lv2_event_buffer.cc
blob: f1e5e4d0d13e972f54ecc0fbcc9731de02a0f693 (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
/*
    Copyright (C) 2009 Paul Davis
    Author: David Robillard

    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 2 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, write to the Free Software Foundation, Inc.,
    675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include <stdint.h>
#include <iostream>
#include "lv2/lv2plug.in/ns/ext/event/event.h"
#include "lv2/lv2plug.in/ns/ext/event/event-helpers.h"
#include "ardour/lv2_event_buffer.h"

using namespace std;

namespace ARDOUR {


/** Allocate a new event buffer.
 * \a capacity is in bytes (not number of events).
 */
LV2EventBuffer::LV2EventBuffer(size_t capacity)
	: _latest_frames(0)
	, _latest_subframes(0)
{
	if (capacity > UINT32_MAX) {
		cerr << "Event buffer size " << capacity << " too large, aborting." << endl;
		throw std::bad_alloc();
	}

	if (capacity == 0) {
		cerr << "ERROR: LV2 event buffer of size 0 created." << endl;
		capacity = 1024;
	}

#ifdef NO_POSIX_MEMALIGN
	_data = (LV2_Event_Buffer*)malloc(sizeof(LV2_Event_Buffer) + capacity);
	int ret = (_data != NULL) ? 0 : -1;
#else
	int ret = posix_memalign((void**)&_data, 16, sizeof(LV2_Event_Buffer) + capacity);
#endif

	if (ret != 0) {
		cerr << "Failed to allocate event buffer.  Aborting." << endl;
		exit(EXIT_FAILURE);
	}

	_data->event_count = 0;
	_data->capacity = (uint32_t)capacity;
	_data->size = 0;
	_data->data = reinterpret_cast<uint8_t*>(_data + 1);

	reset();
}


LV2EventBuffer::~LV2EventBuffer()
{
	free(_data);
}


/** Increment the read position by one event.
 *
 * \return true if increment was successful, or false if end of buffer reached.
 */
bool
LV2EventBuffer::increment() const
{
	if (lv2_event_is_valid(&_iter)) {
		lv2_event_increment(&_iter);
		return true;
	} else {
		return false;
	}
}


/** \return true iff the cursor is valid (ie get_event is safe)
 */
bool
LV2EventBuffer::is_valid() const
{
	return lv2_event_is_valid(&_iter);
}


/** Read an event from the current position in the buffer
 *
 * \return true if read was successful, or false if end of buffer reached
 */
bool
LV2EventBuffer::get_event(uint32_t* frames,
                          uint32_t* subframes,
                          uint16_t* type,
                          uint16_t* size,
                          uint8_t** data) const
{
	if (lv2_event_is_valid(&_iter)) {
		LV2_Event* ev = lv2_event_get(&_iter, data);
		*frames = ev->frames;
		*subframes = ev->subframes;
		*type = ev->type;
		*size = ev->size;
		return true;
	} else {
		return false;
	}
}


/** Append an event to the buffer.
 *
 * \a timestamp must be >= the latest event in the buffer.
 *
 * \return true on success
 */
bool
LV2EventBuffer::append(uint32_t       frames,
                       uint32_t       subframes,
                       uint16_t       type,
                       uint16_t       size,
                       const uint8_t* data)
{
#ifndef NDEBUG
	if (lv2_event_is_valid(&_iter)) {
		LV2_Event* last_event = lv2_event_get(&_iter, NULL);
		assert(last_event->frames < frames
				|| (last_event->frames == frames && last_event->subframes <= subframes));
	}
#endif

	/*cout << "Appending event type " << type << ", size " << size
		<< " @ " << frames << "." << subframes << endl;
	cout << "Buffer capacity " << _data->capacity << ", size " << _data->size << endl;*/

	if (!lv2_event_write(&_iter, frames, subframes, type, size, data)) {
		cerr << "ERROR: Failed to write event." << endl;
		return false;
	} else {
		_latest_frames = frames;
		_latest_subframes = subframes;
		return true;
	}
}


/** Append a buffer of events to the buffer.
 *
 * \a timestamp must be >= the latest event in the buffer.
 *
 * \return true on success
 */
bool
LV2EventBuffer::append(const LV2_Event_Buffer* /*buf*/)
{
	uint8_t** data = NULL;
	bool      ret  = true;

	LV2_Event_Iterator iter;
	for (lv2_event_begin(&iter, _data); lv2_event_is_valid(&iter); lv2_event_increment(&iter)) {
		LV2_Event* ev = lv2_event_get(&iter, data);

#ifndef NDEBUG
		assert((ev->frames > _latest_frames)
				|| (ev->frames == _latest_frames
					&& ev->subframes >= _latest_subframes));
#endif

		if (!(ret = append(ev->frames, ev->subframes, ev->type, ev->size, *data))) {
			cerr << "ERROR: Failed to write event." << endl;
			break;
		}

		_latest_frames = ev->frames;
		_latest_subframes = ev->subframes;
	}

	return ret;
}


} // namespace ARDOUR