2 Copyright (C) 2006-2007 Paul Davis
5 This program is free software; you can redistribute it and/or modify it
6 under the terms of the GNU General Public License as published by the Free
7 Software Foundation; either version 2 of the License, or (at your option)
10 This program is distributed in the hope that it will be useful, but WITHOUT
11 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 675 Mass Ave, Cambridge, MA 02139, USA.
21 #include <ardour/midi_buffer.h>
24 static const int CPU_CACHE_ALIGN = 64;
26 static const int CPU_CACHE_ALIGN = 16; /* arguably 32 on most arches, but it matters less */
30 using namespace ARDOUR;
33 // FIXME: mirroring for MIDI buffers?
34 MidiBuffer::MidiBuffer(size_t capacity)
35 : Buffer(DataType::MIDI, capacity)
38 // , _owns_data(false)
46 MidiBuffer::~MidiBuffer()
57 MidiBuffer::resize (size_t size)
61 if (size < _capacity) {
76 #ifdef NO_POSIX_MEMALIGN
77 _events = (Evoral::Event *) malloc(sizeof(Evoral::Event) * _capacity);
78 _data = (uint8_t *) malloc(sizeof(uint8_t) * _capacity * MAX_EVENT_SIZE);
80 posix_memalign((void**)&_events, CPU_CACHE_ALIGN, sizeof(Evoral::Event) * _capacity);
81 posix_memalign((void**)&_data, CPU_CACHE_ALIGN, sizeof(uint8_t) * _capacity * MAX_EVENT_SIZE);
88 MidiBuffer::copy(const MidiBuffer& copy)
90 assert(_capacity >= copy._capacity);
93 for (size_t i = 0; i < copy.size(); ++i)
98 /** Read events from @a src starting at time @a offset into the START of this buffer, for
99 * time direction @a nframes. Relative time, where 0 = start of buffer.
101 * Note that offset and nframes refer to sample time, NOT buffer offsets or event counts.
104 MidiBuffer::read_from(const Buffer& src, nframes_t nframes, nframes_t offset)
106 assert(src.type() == DataType::MIDI);
107 assert(&src != this);
109 const MidiBuffer& msrc = (MidiBuffer&)src;
111 assert(_capacity >= msrc.size());
117 for (size_t i=0; i < msrc.size(); ++i) {
118 const Evoral::MIDIEvent& ev = msrc[i];
119 if (ev.time() >= offset && ev.time() < offset+nframes) {
120 //cout << "MidiBuffer::read_from got event, " << int(ev.type()) << " time: " << ev.time() << " buffer size: " << _size << endl;
123 cerr << "MidiBuffer event out of range, " << ev.time() << endl;
127 _silent = src.silent();
131 /** Push an event into the buffer.
133 * Note that the raw MIDI pointed to by ev will be COPIED and unmodified.
134 * That is, the caller still owns it, if it needs freeing it's Not My Problem(TM).
136 * @return false if operation failed (not enough room)
139 MidiBuffer::push_back(const Evoral::MIDIEvent& ev)
141 if (_size == _capacity)
144 uint8_t* const write_loc = _data + (_size * MAX_EVENT_SIZE);
146 memcpy(write_loc, ev.buffer(), ev.size());
148 _events[_size].set_buffer(ev.size(), write_loc, false);
151 //cerr << "MidiBuffer: pushed, size = " << _size << endl;
159 /** Push an event into the buffer.
161 * Note that the raw MIDI pointed to by ev will be COPIED and unmodified.
162 * That is, the caller still owns it, if it needs freeing it's Not My Problem(TM).
164 * @return false if operation failed (not enough room)
167 MidiBuffer::push_back(const jack_midi_event_t& ev)
169 if (_size == _capacity)
172 uint8_t* const write_loc = _data + (_size * MAX_EVENT_SIZE);
174 memcpy(write_loc, ev.buffer, ev.size);
175 _events[_size].time() = (double)ev.time;
176 _events[_size].set_buffer(ev.size, write_loc, false);
179 //cerr << "MidiBuffer: pushed, size = " << _size << endl;
187 /** Reserve space for a new event in the buffer.
189 * This call is for copying MIDI directly into the buffer, the data location
190 * (of sufficient size to write \a size bytes) is returned, or 0 on failure.
191 * This call MUST be immediately followed by a write to the returned data
192 * location, or the buffer will be corrupted and very nasty things will happen.
195 MidiBuffer::reserve(double time, size_t size)
197 if (size > MAX_EVENT_SIZE) {
198 cerr << "WARNING: Failed to reserve " << size << " bytes for event";
202 if (_size == _capacity)
205 uint8_t* const write_loc = _data + (_size * MAX_EVENT_SIZE);
207 _events[_size].time() = time;
208 _events[_size].set_buffer(size, write_loc, false);
211 //cerr << "MidiBuffer: reserved, size = " << _size << endl;
220 MidiBuffer::silence(nframes_t dur, nframes_t offset)
222 // FIXME use parameters
224 cerr << "WARNING: MidiBuffer::silence w/ offset != 0 (not implemented)" << endl;
226 memset(_events, 0, sizeof(Evoral::Event) * _capacity);
227 memset(_data, 0, sizeof(uint8_t) * _capacity * MAX_EVENT_SIZE);
233 /** Clear, and merge \a a and \a b into this buffer.
235 * FIXME: This is slow.
237 * \return true if complete merge was successful
240 MidiBuffer::merge(const MidiBuffer& a, const MidiBuffer& b)
244 // Die if a merge isn't necessary as it's expensive
245 assert(a.size() > 0 && b.size() > 0);
249 size_t count = a.size() + b.size();
251 while (count > 0 && a_index < a.size() && b_index < b.size()) {
253 if (size() == capacity()) {
254 cerr << "WARNING: MIDI buffer overrun, events lost!" << endl;
258 if (a_index == a.size()) {
259 push_back(a[a_index]);
261 } else if (b_index == b.size()) {
262 push_back(b[b_index]);
265 const Evoral::MIDIEvent& a_ev = a[a_index];
266 const Evoral::MIDIEvent& b_ev = b[b_index];
268 if (a_ev.time() <= b_ev.time()) {