summaryrefslogtreecommitdiff
path: root/libs/ardour/rt_midibuffer.cc
blob: ec8a2852bdeb9f9df3224e3607c0b56130aed87f (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
/*
 * Copyright (C) 2019 Paul Davis <paul@linuxaudiosystems.com>
 *
 * 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.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include <iostream>

#include "pbd/malign.h"
#include "pbd/compose.h"
#include "pbd/error.h"
#include "pbd/debug.h"
#include "pbd/stacktrace.h"

#include "evoral/midi_util.h"

#include "ardour/debug.h"
#include "ardour/midi_buffer.h"
#include "ardour/midi_state_tracker.h"
#include "ardour/rt_midibuffer.h"

using namespace std;
using namespace ARDOUR;
using namespace PBD;

RTMidiBuffer::RTMidiBuffer ()
	: _size (0)
	, _capacity (0)
	, _data (0)
	, _pool_size (0)
	, _pool_capacity (0)
	, _pool (0)
{
}

RTMidiBuffer::~RTMidiBuffer()
{
	cache_aligned_free (_data);
	cache_aligned_free (_pool);
}

void
RTMidiBuffer::resize (size_t size)
{
	if (_data && size < _capacity) {

		if (_size < size) {
			/* truncate */
			_size = size;
		}

		return;
	}

	Item* old_data = _data;

	cache_aligned_malloc ((void**) &_data, size * sizeof (Item));

	if (_size) {
		memcpy (_data, old_data, _size * sizeof (Item));
		cache_aligned_free (old_data);
	}

	_capacity = size;
}

void
RTMidiBuffer::dump (uint32_t cnt)
{
	cerr << this << " total items: " << _size << " within " << _capacity << " blob pool: " << _pool_capacity << " used " << _pool_size << endl;

	for (uint32_t i = 0; i < _size && i < cnt; ++i) {

		Item* item = &_data[i];
		uint8_t* addr;
		uint32_t size;

		if (item->bytes[0]) {

			/* more than 3 bytes ... indirect */

			uint32_t offset = item->offset & ~(1<<(sizeof(uint8_t)-1));
			Blob* blob = reinterpret_cast<Blob*> (&_pool[offset]);

			size = blob->size;
			addr = blob->data;

		} else {

			/* MIDI data is in bytes[1..3] (variable depending on message type */
			size = Evoral::midi_event_size (item->bytes[1]);
			addr = &item->bytes[1];

		}

		cerr << i << " @ " << item->timestamp << " sz=" << size << '\t';

		cerr << hex;
		for (size_t j =0 ; j < size; ++j) {
			cerr << "0x" << hex << (int)addr[j] << dec << '/' << (int)addr[j] << ' ';
		}
		cerr << dec << endl;
	}
}

uint32_t
RTMidiBuffer::write (TimeType time, Evoral::EventType /*type*/, uint32_t size, const uint8_t* buf)
{
	/* This buffer stores only MIDI, we don't care about the value of "type" */

	if (_size == _capacity) {
		resize (_capacity + 1024); // XXX 1024 is completely arbitrary
	}

	_data[_size].timestamp = time;

	if (size > 3) {

		uint32_t off = store_blob (size, buf);

		/* non-zero MSbit indicates that the data (more than 3 bytes) is not inline */
		_data[_size].offset = (off | (1<<(sizeof(uint8_t)-1)));

	} else {

		assert ((int) size == Evoral::midi_event_size (buf[0]));

		/* zero MSbit indicates that the data (up to 3 bytes) is inline */
		_data[_size].bytes[0] = 0;

		switch (size) {
		case 3:
			_data[_size].bytes[3] = buf[2];
			/* fallthru */
		case 2:
			_data[_size].bytes[2] = buf[1];
			/* fallthru */
		case 1:
			_data[_size].bytes[1] = buf[0];
			break;
		}
	}

	++_size;

	return size;
}

static
bool
item_timestamp_earlier (ARDOUR::RTMidiBuffer::Item const & item, samplepos_t time)
{
	return item.timestamp < time;
}

uint32_t
RTMidiBuffer::read (MidiBuffer& dst, samplepos_t start, samplepos_t end, MidiStateTracker& tracker, samplecnt_t offset)
{
	Glib::Threads::RWLock::ReaderLock lm (_lock, Glib::Threads::TRY_LOCK);

	if (!lm.locked()) {
		return 0;
	}

	Item* iend = _data+_size;
	Item* item = lower_bound (_data, iend, start, item_timestamp_earlier);
	uint32_t count = 0;

#ifndef NDEBUG
	TimeType unadjusted_time;
#endif

	DEBUG_TRACE (DEBUG::MidiRingBuffer, string_compose ("read from %1 .. %2 .. initial index = %3 (time = %4) (range in list of  %7 %5..%6)\n", start, end, item - _data, item->timestamp, _data->timestamp, iend->timestamp, _size));

	while ((item < iend) && (item->timestamp < end)) {

		TimeType evtime = item->timestamp;

#ifndef NDEBUG
		unadjusted_time = evtime;
#endif
		/* Adjust event times to be relative to 'start', taking
		 * 'offset' into account.
		 */

		evtime -= start;
		evtime += offset;

		uint32_t size;
		uint8_t* addr;

		if (item->bytes[0]) {

			/* more than 3 bytes ... indirect */

			uint32_t offset = item->offset & ~(1<<(sizeof(uint8_t)-1));
			Blob* blob = reinterpret_cast<Blob*> (&_pool[offset]);

			size = blob->size;
			addr = blob->data;

		} else {

			size = Evoral::midi_event_size (item->bytes[1]);
			addr = &item->bytes[1];

		}

		if (!dst.push_back (evtime, size, addr)) {
			DEBUG_TRACE (DEBUG::MidiRingBuffer, string_compose ("MidiRingBuffer: overflow in destination MIDI buffer, stopped after %1 events, dst size = %2\n", count, dst.size()));
			break;
		}

		DEBUG_TRACE (DEBUG::MidiRingBuffer, string_compose ("read event sz %1 @ %2\n", size, unadjusted_time));
		tracker.track (addr);

		++item;
		++count;
	}

	DEBUG_TRACE (DEBUG::MidiRingBuffer, string_compose ("total events found for %1 .. %2 = %3\n", start, end, count));
	return count;
}

uint32_t
RTMidiBuffer::alloc_blob (uint32_t size)
{
	if (_pool_size + size > _pool_capacity) {
		uint8_t* old_pool = _pool;

		_pool_capacity += size * 4;

		cache_aligned_malloc ((void **) &_pool, (_pool_capacity * sizeof (Blob)));
		memcpy (_pool, old_pool, _pool_size * sizeof (Blob));
		cache_aligned_free (old_pool);
	}

	uint32_t offset = _pool_size;
	_pool_size += size;

	return offset;
}

uint32_t
RTMidiBuffer::store_blob (uint32_t size, uint8_t const * data)
{
	uint32_t offset = alloc_blob (size);
	uint8_t* addr = &_pool[offset];

	*(reinterpret_cast<uint32_t*> (addr)) = size;
	addr += sizeof (size);
	memcpy (addr, data, size);

	return offset;
}

void
RTMidiBuffer::clear ()
{
	/* mark main array as empty */
	_size = 0;
	/* free the entire current pool size, if any */
	_pool_size = 0;
}