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)
45 MidiBuffer::~MidiBuffer()
51 MidiBuffer::resize(size_t size)
55 if (size < _capacity) {
64 #ifdef NO_POSIX_MEMALIGN
65 _data = (uint8_t*)malloc(_capacity);
67 posix_memalign((void**)&_data, CPU_CACHE_ALIGN, _capacity);
73 MidiBuffer::copy(const MidiBuffer& copy)
75 assert(_capacity >= copy._size);
77 memcpy(_data, copy._data, copy._size);
81 /** Read events from @a src starting at time @a offset into the START of this buffer, for
82 * time duration @a nframes. Relative time, where 0 = start of buffer.
84 * Note that offset and nframes refer to sample time, NOT buffer offsets or event counts.
87 MidiBuffer::read_from(const Buffer& src, nframes_t nframes, nframes_t offset)
89 assert(src.type() == DataType::MIDI);
92 const MidiBuffer& msrc = (MidiBuffer&)src;
94 assert(_capacity >= msrc.size());
101 for (MidiBuffer::const_iterator i = msrc.begin(); i != msrc.end(); ++i) {
102 const Evoral::MIDIEvent<TimeType> ev(*i, false);
103 /*cout << this << " MidiBuffer::read_from event type: " << int(ev.type())
104 << " time: " << ev.time() << " size: " << ev.size()
105 << " status: " << (int)*ev.buffer() << " buffer size: " << _size << endl;*/
106 if (ev.time() < offset) {
107 //cout << "MidiBuffer::read_from skipped event before " << offset << endl;
108 } else if (ev.time() < (nframes + offset)) {
109 //cout << "MidiBuffer::read_from appending event" << endl;
112 //cerr << "MidiBuffer::read_from skipped event after "
113 // << nframes << " + " << offset << endl;
117 _silent = src.silent();
121 /** Push an event into the buffer.
123 * Note that the raw MIDI pointed to by ev will be COPIED and unmodified.
124 * That is, the caller still owns it, if it needs freeing it's Not My Problem(TM).
126 * @return false if operation failed (not enough room)
129 MidiBuffer::push_back(const Evoral::MIDIEvent<TimeType>& ev)
131 const size_t stamp_size = sizeof(TimeType);
132 /*cerr << "MidiBuffer: pushing event " << " size: " << _size
133 << " event size: " << ev.size()
134 << " capacity: " << _capacity
135 << " stamp size: " << stamp_size << " \n";*/
137 if (_size + stamp_size + ev.size() >= _capacity) {
138 cerr << "MidiBuffer::push_back failed (buffer is full)"
143 uint8_t* const write_loc = _data + _size;
144 *((TimeType*)write_loc) = ev.time();
145 memcpy(write_loc + stamp_size, ev.buffer(), ev.size());
147 _size += stamp_size + ev.size();
154 /** Push an event into the buffer.
156 * Note that the raw MIDI pointed to by ev will be COPIED and unmodified.
157 * That is, the caller still owns it, if it needs freeing it's Not My Problem(TM).
159 * @return false if operation failed (not enough room)
162 MidiBuffer::push_back(const jack_midi_event_t& ev)
164 const size_t stamp_size = sizeof(TimeType);
165 if (_size + stamp_size + ev.size >= _capacity) {
166 cerr << "MidiBuffer::push_back failed (buffer is full)" << endl;
170 uint8_t* const write_loc = _data + _size;
171 *((TimeType*)write_loc) = ev.time;
172 memcpy(write_loc + stamp_size, ev.buffer, ev.size);
174 _size += stamp_size + ev.size;
181 /** Reserve space for a new event in the buffer.
183 * This call is for copying MIDI directly into the buffer, the data location
184 * (of sufficient size to write \a size bytes) is returned, or 0 on failure.
185 * This call MUST be immediately followed by a write to the returned data
186 * location, or the buffer will be corrupted and very nasty things will happen.
189 MidiBuffer::reserve(TimeType time, size_t size)
191 const size_t stamp_size = sizeof(TimeType);
192 if (_size + stamp_size + size >= _capacity) {
197 uint8_t* write_loc = _data + _size;
198 *((TimeType*)write_loc) = time;
200 // move write_loc to begin of MIDI buffer data to write to
201 write_loc += stamp_size;
203 _size += stamp_size + size;
211 MidiBuffer::silence(nframes_t dur, nframes_t offset)
213 // FIXME use parameters
215 cerr << "WARNING: MidiBuffer::silence w/ offset != 0 (not implemented)" << endl;
222 /** Merge \a other into this buffer. Realtime safe. */
224 MidiBuffer::merge_in_place(const MidiBuffer &other)
226 if (other.size() == 0) {
235 if (_size + other.size() > _capacity) {
236 cerr << "MidiBuffer::merge failed (no space)" << endl;
240 cerr << "FIXME: MIDI BUFFER IN-PLACE MERGE" << endl;
244 /** Clear, and merge \a a and \a b into this buffer.
246 * \return true if complete merge was successful
249 MidiBuffer::merge(const MidiBuffer& a, const MidiBuffer& b)
261 cerr << "FIXME: MIDI BUFFER MERGE" << endl;