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 @ " << ev.time()
115 << " size = " << ev.size() << endl;*/
117 if (_size + stamp_size + ev.size() >= _capacity) {
118 cerr << "MidiBuffer::push_back failed (buffer is full)" << endl;
122 if (!Evoral::midi_event_is_valid(ev.buffer(), ev.size())) {
123 cerr << "WARNING: MidiBuffer ignoring illegal MIDI event" << endl;
127 push_back(ev.time(), ev.size(), ev.buffer());
133 /** Push an event into the buffer.
134 * @return false if operation failed (not enough room)
137 MidiBuffer::push_back(TimeType time, size_t size, const uint8_t* data)
139 const size_t stamp_size = sizeof(TimeType);
140 /*cerr << "MidiBuffer: pushing event @ " << ev.time()
141 << " size = " << ev.size() << endl;*/
143 if (_size + stamp_size + size >= _capacity) {
144 cerr << "MidiBuffer::push_back failed (buffer is full)" << endl;
148 if (!Evoral::midi_event_is_valid(data, size)) {
149 cerr << "WARNING: MidiBuffer ignoring illegal MIDI event" << endl;
153 uint8_t* const write_loc = _data + _size;
154 *((TimeType*)write_loc) = time;
155 memcpy(write_loc + stamp_size, data, size);
157 _size += stamp_size + size;
164 /** Push an event into the buffer.
166 * Note that the raw MIDI pointed to by ev will be COPIED and unmodified.
167 * That is, the caller still owns it, if it needs freeing it's Not My Problem(TM).
169 * @return false if operation failed (not enough room)
172 MidiBuffer::push_back(const jack_midi_event_t& ev)
174 const size_t stamp_size = sizeof(TimeType);
175 if (_size + stamp_size + ev.size >= _capacity) {
176 cerr << "MidiBuffer::push_back failed (buffer is full)" << endl;
180 if (!Evoral::midi_event_is_valid(ev.buffer, ev.size)) {
181 cerr << "WARNING: MidiBuffer ignoring illegal MIDI event" << endl;
185 uint8_t* const write_loc = _data + _size;
186 *((TimeType*)write_loc) = ev.time;
187 memcpy(write_loc + stamp_size, ev.buffer, ev.size);
189 _size += stamp_size + ev.size;
196 /** Reserve space for a new event in the buffer.
198 * This call is for copying MIDI directly into the buffer, the data location
199 * (of sufficient size to write \a size bytes) is returned, or 0 on failure.
200 * This call MUST be immediately followed by a write to the returned data
201 * location, or the buffer will be corrupted and very nasty things will happen.
204 MidiBuffer::reserve(TimeType time, size_t size)
206 const size_t stamp_size = sizeof(TimeType);
207 if (_size + stamp_size + size >= _capacity) {
212 uint8_t* write_loc = _data + _size;
213 *((TimeType*)write_loc) = time;
215 // move write_loc to begin of MIDI buffer data to write to
216 write_loc += stamp_size;
218 _size += stamp_size + size;
226 MidiBuffer::silence (nframes_t nframes, nframes_t offset)
228 /* XXX iterate over existing events, find all in range given by offset & nframes,
236 /** Merge \a other into this buffer. Realtime safe. */
238 MidiBuffer::merge_in_place(const MidiBuffer &other)
240 if (other.size() == 0) {
249 if (_size + other.size() > _capacity) {
250 cerr << "MidiBuffer::merge failed (no space)" << endl;
254 cerr << "FIXME: MIDI BUFFER IN-PLACE MERGE" << endl;
258 /** Clear, and merge \a a and \a b into this buffer.
260 * \return true if complete merge was successful
263 MidiBuffer::merge(const MidiBuffer& a, const MidiBuffer& b)
275 cerr << "FIXME: MIDI BUFFER MERGE" << endl;