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 = (MIDI::Event *) malloc(sizeof(MIDI::Event) * _capacity);
78 _data = (Byte *) malloc(sizeof(Byte) * _capacity * MAX_EVENT_SIZE);
80 posix_memalign((void**)&_events, CPU_CACHE_ALIGN, sizeof(MIDI::Event) * _capacity);
81 posix_memalign((void**)&_data, CPU_CACHE_ALIGN, sizeof(Byte) * _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 MIDI::Event& 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 MIDI::Event& ev)
141 if (_size == _capacity)
144 Byte* const write_loc = _data + (_size * MAX_EVENT_SIZE);
146 memcpy(write_loc, ev.buffer(), ev.size());
148 _events[_size].set_buffer(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 Byte* 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].size() = ev.size;
177 _events[_size].set_buffer(write_loc, false);
180 cerr << "MidiBuffer: pushed, size = " << _size << endl;
188 /** Reserve space for a new event in the buffer.
190 * This call is for copying MIDI directly into the buffer, the data location
191 * (of sufficient size to write \a size bytes) is returned, or 0 on failure.
192 * This call MUST be immediately followed by a write to the returned data
193 * location, or the buffer will be corrupted and very nasty things will happen.
196 MidiBuffer::reserve(double time, size_t size)
198 assert(size <= MAX_EVENT_SIZE);
200 if (_size == _capacity)
203 Byte* const write_loc = _data + (_size * MAX_EVENT_SIZE);
205 _events[_size].time() = time;
206 _events[_size].size() = size;
207 _events[_size].set_buffer(write_loc, false);
210 cerr << "MidiBuffer: reserved, size = " << _size << endl;
219 MidiBuffer::silence(nframes_t dur, nframes_t offset)
221 // FIXME use parameters
223 cerr << "WARNING: MidiBuffer::silence w/ offset != 0 (not implemented)" << endl;
225 memset(_events, 0, sizeof(MIDI::Event) * _capacity);
226 memset(_data, 0, sizeof(Byte) * _capacity * MAX_EVENT_SIZE);
232 /** Clear, and merge \a a and \a b into this buffer.
234 * FIXME: This is slow.
236 * \return true if complete merge was successful
239 MidiBuffer::merge(const MidiBuffer& a, const MidiBuffer& b)
243 // Die if a merge isn't necessary as it's expensive
244 assert(a.size() > 0 && b.size() > 0);
248 size_t count = a.size() + b.size();
250 while (count > 0 && a_index < a.size() && b_index < b.size()) {
252 if (size() == capacity()) {
253 cerr << "WARNING: MIDI buffer overrun, events lost!" << endl;
257 if (a_index == a.size()) {
258 push_back(a[a_index]);
260 } else if (b_index == b.size()) {
261 push_back(b[b_index]);
264 const MIDI::Event& a_ev = a[a_index];
265 const MIDI::Event& b_ev = b[b_index];
267 if (a_ev.time() <= b_ev.time()) {