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 "pbd/malign.h"
22 #include "ardour/midi_buffer.h"
25 using namespace ARDOUR;
27 // FIXME: mirroring for MIDI buffers?
28 MidiBuffer::MidiBuffer(size_t capacity)
29 : Buffer(DataType::MIDI, capacity)
38 MidiBuffer::~MidiBuffer()
44 MidiBuffer::resize(size_t size)
48 if (size < _capacity) {
56 cache_aligned_malloc ((void**) &_data, _capacity);
62 MidiBuffer::copy(const MidiBuffer& copy)
64 assert(_capacity >= copy._size);
66 memcpy(_data, copy._data, copy._size);
70 /** Read events from @a src starting at time @a offset into the START of this buffer, for
71 * time duration @a nframes. Relative time, where 0 = start of buffer.
73 * Note that offset and nframes refer to sample time, NOT buffer offsets or event counts.
76 MidiBuffer::read_from (const Buffer& src, nframes_t nframes, nframes_t dst_offset, nframes_t src_offset)
78 assert (src.type() == DataType::MIDI);
79 assert (&src != this);
81 const MidiBuffer& msrc = (MidiBuffer&) src;
83 assert (_capacity >= msrc.size());
85 if (dst_offset == 0) {
90 /* XXX use dst_offset somehow */
92 for (MidiBuffer::const_iterator i = msrc.begin(); i != msrc.end(); ++i) {
93 const Evoral::MIDIEvent<TimeType> ev(*i, false);
94 if (ev.time() >= src_offset && ev.time() < (nframes+src_offset)) {
99 _silent = src.silent();
102 /** Push an event into the buffer.
104 * Note that the raw MIDI pointed to by ev will be COPIED and unmodified.
105 * That is, the caller still owns it, if it needs freeing it's Not My Problem(TM).
107 * @return false if operation failed (not enough room)
110 MidiBuffer::push_back(const Evoral::MIDIEvent<TimeType>& ev)
112 const size_t stamp_size = sizeof(TimeType);
113 /*cerr << "MidiBuffer: pushing event @ " << ev.time()
114 << " size = " << ev.size() << endl;*/
116 if (_size + stamp_size + ev.size() >= _capacity) {
117 cerr << "MidiBuffer::push_back failed (buffer is full)" << endl;
121 if (!Evoral::midi_event_is_valid(ev.buffer(), ev.size())) {
122 cerr << "WARNING: MidiBuffer ignoring illegal MIDI event" << endl;
126 push_back(ev.time(), ev.size(), ev.buffer());
132 /** Push an event into the buffer.
133 * @return false if operation failed (not enough room)
136 MidiBuffer::push_back(TimeType time, size_t size, const uint8_t* data)
138 const size_t stamp_size = sizeof(TimeType);
139 /*cerr << "MidiBuffer: pushing event @ " << ev.time()
140 << " size = " << ev.size() << endl;*/
142 if (_size + stamp_size + size >= _capacity) {
143 cerr << "MidiBuffer::push_back failed (buffer is full)" << endl;
147 if (!Evoral::midi_event_is_valid(data, size)) {
148 cerr << "WARNING: MidiBuffer ignoring illegal MIDI event" << endl;
152 uint8_t* const write_loc = _data + _size;
153 *((TimeType*)write_loc) = time;
154 memcpy(write_loc + stamp_size, data, size);
156 _size += stamp_size + size;
163 /** Push an event into the buffer.
165 * Note that the raw MIDI pointed to by ev will be COPIED and unmodified.
166 * That is, the caller still owns it, if it needs freeing it's Not My Problem(TM).
168 * @return false if operation failed (not enough room)
171 MidiBuffer::push_back(const jack_midi_event_t& ev)
173 const size_t stamp_size = sizeof(TimeType);
174 if (_size + stamp_size + ev.size >= _capacity) {
175 cerr << "MidiBuffer::push_back failed (buffer is full)" << endl;
179 if (!Evoral::midi_event_is_valid(ev.buffer, ev.size)) {
180 cerr << "WARNING: MidiBuffer ignoring illegal MIDI event" << endl;
184 uint8_t* const write_loc = _data + _size;
185 *((TimeType*)write_loc) = ev.time;
186 memcpy(write_loc + stamp_size, ev.buffer, ev.size);
188 _size += stamp_size + ev.size;
195 /** Reserve space for a new event in the buffer.
197 * This call is for copying MIDI directly into the buffer, the data location
198 * (of sufficient size to write \a size bytes) is returned, or 0 on failure.
199 * This call MUST be immediately followed by a write to the returned data
200 * location, or the buffer will be corrupted and very nasty things will happen.
203 MidiBuffer::reserve(TimeType time, size_t size)
205 const size_t stamp_size = sizeof(TimeType);
206 if (_size + stamp_size + size >= _capacity) {
211 uint8_t* write_loc = _data + _size;
212 *((TimeType*)write_loc) = time;
214 // move write_loc to begin of MIDI buffer data to write to
215 write_loc += stamp_size;
217 _size += stamp_size + size;
225 MidiBuffer::silence (nframes_t nframes, nframes_t offset)
227 /* XXX iterate over existing events, find all in range given by offset & nframes,
235 /** Merge \a other into this buffer. Realtime safe. */
237 MidiBuffer::merge_in_place(const MidiBuffer &other)
239 if (other.size() == 0) {
248 if (_size + other.size() > _capacity) {
249 cerr << "MidiBuffer::merge failed (no space)" << endl;
253 cerr << "FIXME: MIDI BUFFER IN-PLACE MERGE" << endl;
257 /** Clear, and merge \a a and \a b into this buffer.
259 * \return true if complete merge was successful
262 MidiBuffer::merge(const MidiBuffer& a, const MidiBuffer& b)
274 cerr << "FIXME: MIDI BUFFER MERGE" << endl;