2 * Copyright (C) 2008-2016 David Robillard <d@drobilla.net>
3 * Copyright (C) 2009-2017 Paul Davis <paul@linuxaudiosystems.com>
4 * Copyright (C) 2009 Hans Baier <hansfbaier@googlemail.com>
5 * Copyright (C) 2010-2011 Carl Hetherington <carl@carlh.net>
6 * Copyright (C) 2014-2015 Robin Gareus <robin@gareus.org>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #include "pbd/compose.h"
24 #include "pbd/enumwriter.h"
25 #include "pbd/error.h"
27 #include "ardour/debug.h"
28 #include "ardour/midi_ring_buffer.h"
29 #include "ardour/midi_buffer.h"
30 #include "ardour/event_type_map.h"
37 /** Read a block of MIDI events from this buffer into a MidiBuffer.
39 * Timestamps of events returned are relative to start (i.e. event with stamp 0
40 * occurred at start), with offset added.
44 MidiRingBuffer<T>::read (MidiBuffer& dst, samplepos_t start, samplepos_t end, samplecnt_t offset, bool stop_on_overflow_in_dst)
46 if (this->read_space() == 0) {
53 const size_t prefix_size = sizeof(T) + sizeof(Evoral::EventType) + sizeof(uint32_t);
55 while (this->read_space() >= prefix_size) {
57 uint8_t peekbuf[prefix_size];
59 /* this cannot fail, because we've already verified that there
60 is prefix_space to read
62 this->peek (peekbuf, prefix_size);
64 ev_time = *(reinterpret_cast<T*>((uintptr_t)peekbuf));
65 ev_size = *(reinterpret_cast<uint32_t*>((uintptr_t)(peekbuf + sizeof(T) + sizeof (Evoral::EventType))));
67 if (this->read_space() < ev_size) {
72 DEBUG_TRACE (DEBUG::MidiRingBuffer, string_compose ("MRB event @ %1 past end @ %2\n", ev_time, end));
74 } else if (ev_time < start) {
75 DEBUG_TRACE (DEBUG::MidiRingBuffer, string_compose ("MRB event @ %1 before start @ %2\n", ev_time, start));
78 DEBUG_TRACE (DEBUG::MidiRingBuffer, string_compose ("MRB event @ %1 in range %2 .. %3\n", ev_time, start, end));
84 /* we're good to go ahead and read the data now but since we
85 * have the prefix data already, just skip over that
87 this->increment_read_ptr (prefix_size);
90 bool r = this->peek (&status, sizeof(uint8_t));
91 assert (r); // If this failed, buffer is corrupt, all hope is lost
93 /* lets see if we are going to be able to write this event into dst.
95 uint8_t* write_loc = dst.reserve (ev_time, ev_size);
97 if (stop_on_overflow_in_dst) {
98 DEBUG_TRACE (DEBUG::MidiRingBuffer, string_compose ("MidiRingBuffer: overflow in destination MIDI buffer, stopped after %1 events\n", count));
101 error << "MRB: Unable to reserve space in buffer, event skipped" << endmsg;
102 this->increment_read_ptr (ev_size); // Advance read pointer to next event
106 // write MIDI buffer contents
108 bool success = read_contents (ev_size, write_loc);
110 if (DEBUG_ENABLED (DEBUG::MidiRingBuffer)) {
112 DEBUG_STR_APPEND(a, string_compose ("wrote MidiEvent to Buffer (time=%1, start=%2 offset=%3) ", ev_time, start, offset));
113 for (size_t i=0; i < ev_size; ++i) {
114 DEBUG_STR_APPEND(a,hex);
115 DEBUG_STR_APPEND(a,"0x");
116 DEBUG_STR_APPEND(a,(int)write_loc[i]);
117 DEBUG_STR_APPEND(a,' ');
119 DEBUG_STR_APPEND(a,'\n');
120 DEBUG_TRACE (DEBUG::MidiRingBuffer, DEBUG_STR(a).str());
124 _tracker.track(write_loc);
127 cerr << "WARNING: error reading event contents from MIDI ring" << endl;
136 MidiRingBuffer<T>::skip_to(samplepos_t start)
138 if (this->read_space() == 0) {
145 const size_t prefix_size = sizeof(T) + sizeof(Evoral::EventType) + sizeof(uint32_t);
147 while (this->read_space() >= prefix_size) {
149 uint8_t peekbuf[prefix_size];
150 this->peek (peekbuf, prefix_size);
152 ev_time = *(reinterpret_cast<T*>((uintptr_t)peekbuf));
153 ev_size = *(reinterpret_cast<uint32_t*>((uintptr_t)(peekbuf + sizeof(T) + sizeof (Evoral::EventType))));
155 if (ev_time >= start) {
159 if (this->read_space() < ev_size) {
163 this->increment_read_ptr (prefix_size);
166 bool r = this->peek (&status, sizeof(uint8_t));
167 assert (r); // If this failed, buffer is corrupt, all hope is lost
171 /* TODO investigate and think:
173 * Does it makes sense to keep track of notes
174 * that are skipped (because they're either too late
175 * (underrun) or never used (read-ahead, loop) ?
177 * skip_to() is called on the rinbuffer between
178 * disk and process. it seems wrong to track them
179 * (a potential synth never sees skipped notes, either)
180 * but there may be more to this.
184 this->increment_read_ptr (ev_size);
186 // we only track note on/off, 8 bytes are plenty.
187 uint8_t write_loc[8];
188 bool success = read_contents (ev_size, write_loc);
190 _tracker.track(write_loc);
201 MidiRingBuffer<T>::flush (samplepos_t /*start*/, samplepos_t end)
203 const size_t prefix_size = sizeof(T) + sizeof(Evoral::EventType) + sizeof(uint32_t);
205 while (this->read_space() >= prefix_size) {
206 uint8_t peekbuf[prefix_size];
211 success = this->peek (peekbuf, prefix_size);
212 /* this cannot fail, because we've already verified that there
213 is prefix_space to read
217 ev_time = *(reinterpret_cast<T*>((uintptr_t)peekbuf));
219 if (ev_time >= end) {
223 ev_size = *(reinterpret_cast<uint32_t*>((uintptr_t)(peekbuf + sizeof(T) + sizeof (Evoral::EventType))));
224 this->increment_read_ptr (prefix_size);
225 this->increment_read_ptr (ev_size);
231 MidiRingBuffer<T>::dump(ostream& str)
235 if ((rspace = this->read_space()) == 0) {
236 str << this << " MRB::dump: empty\n";
241 Evoral::EventType ev_type;
244 RingBufferNPT<uint8_t>::rw_vector vec;
245 RingBufferNPT<uint8_t>::get_read_vector (&vec);
247 if (vec.len[0] == 0) {
251 str << this << ": Dump size = " << vec.len[0] + vec.len[1]
252 << " r@ " << RingBufferNPT<uint8_t>::get_read_ptr()
253 << " w@" << RingBufferNPT<uint8_t>::get_write_ptr() << endl;
256 uint8_t *buf = new uint8_t[vec.len[0] + vec.len[1]];
257 memcpy (buf, vec.buf[0], vec.len[0]);
260 memcpy (buf+vec.len[1], vec.buf[1], vec.len[1]);
264 const uint8_t* end = buf + vec.len[0] + vec.len[1];
268 memcpy (&ev_time, data, sizeof (T));
270 str << "\ttime " << ev_time;
273 str << "(incomplete)\n ";
277 memcpy (&ev_type, data, sizeof (ev_type));
278 data += sizeof (ev_type);
279 str << " type " << ev_type;
282 str << "(incomplete)\n";
286 memcpy (&ev_size, data, sizeof (ev_size));
287 data += sizeof (ev_size);
288 str << " size " << ev_size;
291 str << "(incomplete)\n";
295 for (uint32_t i = 0; i != ev_size && data < end; ++i) {
296 str << ' ' << hex << (int) data[i] << dec;
309 MidiRingBuffer<T>::reset_tracker ()
316 MidiRingBuffer<T>::resolve_tracker (MidiBuffer& dst, samplepos_t t)
318 _tracker.resolve_notes (dst, t);
323 MidiRingBuffer<T>::resolve_tracker (Evoral::EventSink<samplepos_t>& dst, samplepos_t t)
325 _tracker.resolve_notes(dst, t);
328 template class MidiRingBuffer<samplepos_t>;
330 } // namespace ARDOUR