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 if (_size + stamp_size + ev.size() >= _capacity) {
133 cerr << "MidiBuffer::push_back failed (buffer is full)" << endl;
137 uint8_t* const write_loc = _data + _size;
138 *((TimeType*)write_loc) = ev.time();
139 memcpy(write_loc + stamp_size, ev.buffer(), ev.size());
141 _size += stamp_size + ev.size();
148 /** Push an event into the buffer.
150 * Note that the raw MIDI pointed to by ev will be COPIED and unmodified.
151 * That is, the caller still owns it, if it needs freeing it's Not My Problem(TM).
153 * @return false if operation failed (not enough room)
156 MidiBuffer::push_back(const jack_midi_event_t& ev)
158 const size_t stamp_size = sizeof(TimeType);
159 if (_size + stamp_size + ev.size >= _capacity) {
160 cerr << "MidiBuffer::push_back failed (buffer is full)" << endl;
164 uint8_t* const write_loc = _data + _size;
165 *((TimeType*)write_loc) = ev.time;
166 memcpy(write_loc + stamp_size, ev.buffer, ev.size);
168 _size += stamp_size + ev.size;
175 /** Reserve space for a new event in the buffer.
177 * This call is for copying MIDI directly into the buffer, the data location
178 * (of sufficient size to write \a size bytes) is returned, or 0 on failure.
179 * This call MUST be immediately followed by a write to the returned data
180 * location, or the buffer will be corrupted and very nasty things will happen.
183 MidiBuffer::reserve(TimeType time, size_t size)
185 const size_t stamp_size = sizeof(TimeType);
186 if (_size + stamp_size + size >= _capacity) {
190 uint8_t* const write_loc = _data + _size;
191 *((TimeType*)write_loc) = time;
193 _size += stamp_size + size;
201 MidiBuffer::silence(nframes_t dur, nframes_t offset)
203 // FIXME use parameters
205 cerr << "WARNING: MidiBuffer::silence w/ offset != 0 (not implemented)" << endl;
211 /** Merge \a other into this buffer. Realtime safe. */
213 MidiBuffer::merge_in_place(const MidiBuffer &other)
215 if (other.size() == 0) {
224 if (_size + other.size() > _capacity) {
225 cerr << "MidiBuffer::merge failed (no space)" << endl;
229 cerr << "FIXME: MIDI BUFFER IN-PLACE MERGE" << endl;
233 /** Clear, and merge \a a and \a b into this buffer.
235 * \return true if complete merge was successful
238 MidiBuffer::merge(const MidiBuffer& a, const MidiBuffer& b)
250 cerr << "FIXME: MIDI BUFFER MERGE" << endl;