summaryrefslogtreecommitdiff
path: root/libs/ardour/rt_midibuffer.cc
blob: c2fec01e592e0e74d661c3f3f305fe1efd5ef70c (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
/*
 * 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 "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_t capacity)
	: _size (0)
	, _capacity (0)
	, _data (0)
{
	if (capacity) {
		resize (capacity);
	}
}

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

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

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

		return;
	}

	uint8_t* old_data = _data;

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

	if (_size) {
		memcpy (_data, old_data, _size);
	}

	cache_aligned_free (old_data);
	_capacity = size;

	assert(_data);
}

void
RTMidiBuffer::dump (uint32_t cnt)
{
	for (Map::iterator iter = _map.begin(); iter != _map.end() && cnt; ++iter, --cnt) {

		uint8_t* addr = &_data[iter->second];
		TimeType evtime = iter->first;
		uint32_t size = *(reinterpret_cast<Evoral::EventType*>(addr));
		addr += sizeof (size);

		cerr << "@ " << evtime << " sz=" << size << '\t';

		cerr << hex;
		for (size_t i =0 ; i < size; ++i) {
			cerr << "0x" << hex << (int)addr[i] << dec << '/' << (int)addr[i] << ' ';
		}
		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" */

	const size_t bytes_to_merge = sizeof (size) + size;

	if (_size + bytes_to_merge > _capacity) {
		resize (_capacity + 8192); // XXX 8192 is completely arbitrary
	}

	_map.insert (make_pair (time, _size));

	uint8_t* addr = &_data[_size];

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

	_size += bytes_to_merge;

	return size;
}

uint32_t
RTMidiBuffer::read (MidiBuffer& dst, samplepos_t start, samplepos_t end, MidiStateTracker& tracker, samplecnt_t offset)
{
	Map::iterator iter = _map.lower_bound (start);
	uint32_t count = 0;
#ifndef NDEBUG
	TimeType unadjusted_time;
#endif

	DEBUG_TRACE (DEBUG::MidiRingBuffer, string_compose ("read from %1 .. %2\n", start, end));

	while ((iter != _map.end()) && (iter->first < end)) {

		/* the event consists of a size followed by bytes of MIDI
		 * data. It begins at _data[iter->second], which was stored in
		 * our map when we wrote the event into the data structure.
		 */

		uint8_t* addr = &_data[iter->second];
		TimeType evtime = iter->first;

#ifndef NDEBUG
		unadjusted_time = evtime;
#endif
		uint32_t size = *(reinterpret_cast<Evoral::EventType*>(addr));
		addr += sizeof (size);

		/* Adjust event times to be relative to 'start', taking
		 * 'offset' into account.
		 */

		evtime -= start;
		evtime += offset;

		uint8_t* write_loc = dst.reserve (evtime, size);

		if (write_loc == 0) {
			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));

		memcpy (write_loc, addr, size);
		tracker.track (addr);

		++iter;
		++count;
	}

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