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 */
34 // FIXME: mirroring for MIDI buffers?
35 MidiBuffer::MidiBuffer(size_t capacity)
36 : Buffer(DataType::MIDI, capacity)
45 #ifdef NO_POSIX_MEMALIGN
46 _events = (MidiEvent *) malloc(sizeof(MidiEvent) * capacity);
47 _data = (Byte *) malloc(sizeof(Byte) * capacity * MAX_EVENT_SIZE);
49 posix_memalign((void**)&_events, CPU_CACHE_ALIGN, sizeof(MidiEvent) * capacity);
50 posix_memalign((void**)&_data, CPU_CACHE_ALIGN, sizeof(Byte) * capacity * MAX_EVENT_SIZE);
58 MidiBuffer::copy(const MidiBuffer& copy)
60 assert(_capacity >= copy._capacity);
63 for (size_t i=0; i < copy.size(); ++i)
67 MidiBuffer::~MidiBuffer()
74 /** Read events from @a src starting at time @a offset into the START of this buffer, for
75 * time direction @a nframes. Relative time, where 0 = start of buffer.
77 * Note that offset and nframes refer to sample time, NOT buffer offsets or event counts.
80 MidiBuffer::read_from(const Buffer& src, nframes_t nframes, nframes_t offset)
82 assert(src.type() == DataType::MIDI);
83 const MidiBuffer& msrc = (MidiBuffer&)src;
85 assert(_capacity >= src.size());
91 for (size_t i=0; i < src.size(); ++i) {
92 const MidiEvent& ev = msrc[i];
93 if (ev.time() >= offset && ev.time() < offset+nframes) {
94 //cerr << "MidiBuffer::read_from got event, " << ev.time() << endl;
97 //cerr << "MidiBuffer event out of range, " << ev.time() << endl;
101 _silent = src.silent();
105 /** Push an event into the buffer.
107 * Note that the raw MIDI pointed to by ev will be COPIED and unmodified.
108 * That is, the caller still owns it, if it needs freeing it's Not My Problem(TM).
110 * @return false if operation failed (not enough room)
113 MidiBuffer::push_back(const MidiEvent& ev)
115 if (_size == _capacity)
118 Byte* const write_loc = _data + (_size * MAX_EVENT_SIZE);
120 memcpy(write_loc, ev.buffer(), ev.size());
122 _events[_size].set_buffer(write_loc, false);
125 //cerr << "MidiBuffer: pushed, size = " << _size << endl;
133 /** Push an event into the buffer.
135 * Note that the raw MIDI pointed to by ev will be COPIED and unmodified.
136 * That is, the caller still owns it, if it needs freeing it's Not My Problem(TM).
138 * @return false if operation failed (not enough room)
141 MidiBuffer::push_back(const jack_midi_event_t& ev)
143 if (_size == _capacity)
146 Byte* const write_loc = _data + (_size * MAX_EVENT_SIZE);
148 memcpy(write_loc, ev.buffer, ev.size);
149 _events[_size].time() = (double)ev.time;
150 _events[_size].size() = ev.size;
151 _events[_size].set_buffer(write_loc, false);
154 //cerr << "MidiBuffer: pushed, size = " << _size << endl;
162 /** Reserve space for a new event in the buffer.
164 * This call is for copying MIDI directly into the buffer, the data location
165 * (of sufficient size to write \a size bytes) is returned, or NULL on failure.
166 * This call MUST be immediately followed by a write to the returned data
167 * location, or the buffer will be corrupted and very nasty things will happen.
170 MidiBuffer::reserve(double time, size_t size)
172 assert(size <= MAX_EVENT_SIZE);
174 if (_size == _capacity)
177 Byte* const write_loc = _data + (_size * MAX_EVENT_SIZE);
179 _events[_size].time() = time;
180 _events[_size].size() = size;
181 _events[_size].set_buffer(write_loc, false);
184 //cerr << "MidiBuffer: reserved, size = " << _size << endl;
193 MidiBuffer::silence(nframes_t dur, nframes_t offset)
195 // FIXME use parameters
197 //assert(dur == _capacity);
199 memset(_events, 0, sizeof(MidiEvent) * _capacity);
200 memset(_data, 0, sizeof(Byte) * _capacity * MAX_EVENT_SIZE);
206 /** Clear, and merge \a a and \a b into this buffer.
208 * FIXME: This is slow.
210 * \return true if complete merge was successful
213 MidiBuffer::merge(const MidiBuffer& a, const MidiBuffer& b)
217 // Die if a merge isn't necessary as it's expensive
218 assert(a.size() > 0 && b.size() > 0);
222 size_t count = a.size() + b.size();
224 while (count > 0 && a_index < a.size() && b_index < b.size()) {
226 if (size() == capacity()) {
227 cerr << "WARNING: MIDI buffer overrun, events lost!" << endl;
231 if (a_index == a.size()) {
232 push_back(a[a_index]);
234 } else if (b_index == b.size()) {
235 push_back(b[b_index]);
238 const MidiEvent& a_ev = a[a_index];
239 const MidiEvent& b_ev = b[b_index];
241 if (a_ev.time() <= b_ev.time()) {
257 } // namespace ARDOUR