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)) {
96 cerr << "got note " << ev << endl;
100 _silent = src.silent();
104 MidiBuffer::merge_from (const Buffer& src, nframes_t /*nframes*/, nframes_t /*dst_offset*/, nframes_t /*src_offset*/)
106 const MidiBuffer* mbuf = dynamic_cast<const MidiBuffer*>(&src);
108 assert (mbuf != this);
110 /* XXX use nframes, and possible offsets */
111 merge_in_place (*mbuf);
114 /** Push an event into the buffer.
116 * Note that the raw MIDI pointed to by ev will be COPIED and unmodified.
117 * That is, the caller still owns it, if it needs freeing it's Not My Problem(TM).
119 * @return false if operation failed (not enough room)
122 MidiBuffer::push_back(const Evoral::MIDIEvent<TimeType>& ev)
124 const size_t stamp_size = sizeof(TimeType);
125 /*cerr << "MidiBuffer: pushing event @ " << ev.time()
126 << " size = " << ev.size() << endl;*/
128 if (_size + stamp_size + ev.size() >= _capacity) {
129 cerr << "MidiBuffer::push_back failed (buffer is full)" << endl;
133 if (!Evoral::midi_event_is_valid(ev.buffer(), ev.size())) {
134 cerr << "WARNING: MidiBuffer ignoring illegal MIDI event" << endl;
138 push_back(ev.time(), ev.size(), ev.buffer());
144 /** Push an event into the buffer.
145 * @return false if operation failed (not enough room)
148 MidiBuffer::push_back(TimeType time, size_t size, const uint8_t* data)
150 const size_t stamp_size = sizeof(TimeType);
151 /*cerr << "MidiBuffer: pushing event @ " << ev.time()
152 << " size = " << ev.size() << endl;*/
154 if (_size + stamp_size + size >= _capacity) {
155 cerr << "MidiBuffer::push_back failed (buffer is full)" << endl;
159 if (!Evoral::midi_event_is_valid(data, size)) {
160 cerr << "WARNING: MidiBuffer ignoring illegal MIDI event" << endl;
164 uint8_t* const write_loc = _data + _size;
165 *((TimeType*)write_loc) = time;
166 memcpy(write_loc + stamp_size, data, size);
168 _size += stamp_size + size;
175 /** Push an event into the buffer.
177 * Note that the raw MIDI pointed to by ev will be COPIED and unmodified.
178 * That is, the caller still owns it, if it needs freeing it's Not My Problem(TM).
180 * @return false if operation failed (not enough room)
183 MidiBuffer::push_back(const jack_midi_event_t& ev)
185 const size_t stamp_size = sizeof(TimeType);
186 if (_size + stamp_size + ev.size >= _capacity) {
187 cerr << "MidiBuffer::push_back failed (buffer is full)" << endl;
191 if (!Evoral::midi_event_is_valid(ev.buffer, ev.size)) {
192 cerr << "WARNING: MidiBuffer ignoring illegal MIDI event" << endl;
196 uint8_t* const write_loc = _data + _size;
197 *((TimeType*)write_loc) = ev.time;
198 memcpy(write_loc + stamp_size, ev.buffer, ev.size);
200 _size += stamp_size + ev.size;
207 /** Reserve space for a new event in the buffer.
209 * This call is for copying MIDI directly into the buffer, the data location
210 * (of sufficient size to write \a size bytes) is returned, or 0 on failure.
211 * This call MUST be immediately followed by a write to the returned data
212 * location, or the buffer will be corrupted and very nasty things will happen.
215 MidiBuffer::reserve(TimeType time, size_t size)
217 const size_t stamp_size = sizeof(TimeType);
218 if (_size + stamp_size + size >= _capacity) {
223 uint8_t* write_loc = _data + _size;
224 *((TimeType*)write_loc) = time;
226 // move write_loc to begin of MIDI buffer data to write to
227 write_loc += stamp_size;
229 _size += stamp_size + size;
237 MidiBuffer::silence (nframes_t /*nframes*/, nframes_t /*offset*/)
239 /* XXX iterate over existing events, find all in range given by offset & nframes,
247 /** Merge \a other into this buffer. Realtime safe. */
249 MidiBuffer::merge_in_place(const MidiBuffer &other)
251 if (other.size() == 0) {
260 if (_size + other.size() > _capacity) {
261 cerr << "MidiBuffer::merge failed (no space)" << endl;
265 cerr << "FIXME: MIDI BUFFER IN-PLACE MERGE" << endl;
269 /** Clear, and merge \a a and \a b into this buffer.
271 * \return true if complete merge was successful
274 MidiBuffer::merge(const MidiBuffer& a, const MidiBuffer& b)
286 cerr << "FIXME: MIDI BUFFER MERGE" << endl;