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)
39 MidiBuffer::~MidiBuffer()
45 MidiBuffer::resize(size_t size)
49 if (size < _capacity) {
57 cache_aligned_malloc ((void**) &_data, _capacity);
63 MidiBuffer::copy(const MidiBuffer& copy)
65 assert(_capacity >= copy._size);
67 memcpy(_data, copy._data, copy._size);
71 /** Read events from @a src starting at time @a offset into the START of this buffer, for
72 * time duration @a nframes. Relative time, where 0 = start of buffer.
74 * Note that offset and nframes refer to sample time, NOT buffer offsets or event counts.
77 MidiBuffer::read_from (const Buffer& src, nframes_t nframes, nframes_t dst_offset, nframes_t src_offset)
79 assert (src.type() == DataType::MIDI);
80 assert (&src != this);
82 const MidiBuffer& msrc = (MidiBuffer&) src;
84 assert (_capacity >= msrc.size());
86 if (dst_offset == 0) {
91 /* XXX use dst_offset somehow */
93 for (MidiBuffer::const_iterator i = msrc.begin(); i != msrc.end(); ++i) {
94 const Evoral::MIDIEvent<TimeType> ev(*i, false);
95 if (ev.time() >= src_offset && ev.time() < (nframes+src_offset)) {
100 _silent = src.silent();
103 /** Push an event into the buffer.
105 * Note that the raw MIDI pointed to by ev will be COPIED and unmodified.
106 * That is, the caller still owns it, if it needs freeing it's Not My Problem(TM).
108 * @return false if operation failed (not enough room)
111 MidiBuffer::push_back(const Evoral::MIDIEvent<TimeType>& ev)
113 const size_t stamp_size = sizeof(TimeType);
114 /*cerr << "MidiBuffer: pushing event " << " size: " << _size
115 << " event size: " << ev.size()
116 << " capacity: " << _capacity
117 << " stamp size: " << stamp_size << " \n";*/
119 if (_size + stamp_size + ev.size() >= _capacity) {
120 cerr << "MidiBuffer::push_back failed (buffer is full)" << endl;
124 if (!Evoral::midi_event_is_valid(ev.buffer(), ev.size())) {
125 cerr << "WARNING: MidiBuffer ignoring illegal MIDI event" << endl;
129 uint8_t* const write_loc = _data + _size;
130 *((TimeType*)write_loc) = ev.time();
131 memcpy(write_loc + stamp_size, ev.buffer(), ev.size());
133 _size += stamp_size + ev.size();
140 /** Push an event into the buffer.
142 * Note that the raw MIDI pointed to by ev will be COPIED and unmodified.
143 * That is, the caller still owns it, if it needs freeing it's Not My Problem(TM).
145 * @return false if operation failed (not enough room)
148 MidiBuffer::push_back(const jack_midi_event_t& ev)
150 const size_t stamp_size = sizeof(TimeType);
151 if (_size + stamp_size + ev.size >= _capacity) {
152 cerr << "MidiBuffer::push_back failed (buffer is full)" << endl;
156 if (!Evoral::midi_event_is_valid(ev.buffer, ev.size)) {
157 cerr << "WARNING: MidiBuffer ignoring illegal MIDI event" << endl;
161 uint8_t* const write_loc = _data + _size;
162 *((TimeType*)write_loc) = ev.time;
163 memcpy(write_loc + stamp_size, ev.buffer, ev.size);
165 _size += stamp_size + ev.size;
172 /** Reserve space for a new event in the buffer.
174 * This call is for copying MIDI directly into the buffer, the data location
175 * (of sufficient size to write \a size bytes) is returned, or 0 on failure.
176 * This call MUST be immediately followed by a write to the returned data
177 * location, or the buffer will be corrupted and very nasty things will happen.
180 MidiBuffer::reserve(TimeType time, size_t size)
182 const size_t stamp_size = sizeof(TimeType);
183 if (_size + stamp_size + size >= _capacity) {
188 uint8_t* write_loc = _data + _size;
189 *((TimeType*)write_loc) = time;
191 // move write_loc to begin of MIDI buffer data to write to
192 write_loc += stamp_size;
194 _size += stamp_size + size;
202 MidiBuffer::silence (nframes_t nframes, nframes_t offset)
204 /* XXX iterate over existing events, find all in range given by offset & nframes,
212 /** Merge \a other into this buffer. Realtime safe. */
214 MidiBuffer::merge_in_place(const MidiBuffer &other)
216 if (other.size() == 0) {
225 if (_size + other.size() > _capacity) {
226 cerr << "MidiBuffer::merge failed (no space)" << endl;
230 cerr << "FIXME: MIDI BUFFER IN-PLACE MERGE" << endl;
234 /** Clear, and merge \a a and \a b into this buffer.
236 * \return true if complete merge was successful
239 MidiBuffer::merge(const MidiBuffer& a, const MidiBuffer& b)
251 cerr << "FIXME: MIDI BUFFER MERGE" << endl;