2 Copyright (C) 2006-2008 Paul Davis
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 #include "pbd/compose.h"
20 #include "pbd/enumwriter.h"
21 #include "pbd/error.h"
23 #include "ardour/debug.h"
24 #include "ardour/midi_ring_buffer.h"
25 #include "ardour/midi_buffer.h"
26 #include "ardour/event_type_map.h"
33 /** Read a block of MIDI events from this buffer into a MidiBuffer.
35 * Timestamps of events returned are relative to start (i.e. event with stamp 0
36 * occurred at start), with offset added.
40 MidiRingBuffer<T>::read(MidiBuffer& dst, framepos_t start, framepos_t end, framecnt_t offset, bool stop_on_overflow_in_dst)
42 if (this->read_space() == 0) {
49 const size_t prefix_size = sizeof(T) + sizeof(Evoral::EventType) + sizeof(uint32_t);
51 while (this->read_space() >= prefix_size) {
53 uint8_t peekbuf[prefix_size];
55 /* this cannot fail, because we've already verified that there
56 is prefix_space to read
58 this->peek (peekbuf, prefix_size);
60 ev_time = *(reinterpret_cast<T*>((uintptr_t)peekbuf));
61 ev_size = *(reinterpret_cast<uint32_t*>((uintptr_t)(peekbuf + sizeof(T) + sizeof (Evoral::EventType))));
63 if (this->read_space() < ev_size) {
68 DEBUG_TRACE (DEBUG::MidiDiskstreamIO, string_compose ("MRB event @ %1 past end @ %2\n", ev_time, end));
70 } else if (ev_time < start) {
71 DEBUG_TRACE (DEBUG::MidiDiskstreamIO, string_compose ("MRB event @ %1 before start @ %2\n", ev_time, start));
74 DEBUG_TRACE (DEBUG::MidiDiskstreamIO, string_compose ("MRB event @ %1 in range %2 .. %3\n", ev_time, start, end));
80 /* we're good to go ahead and read the data now but since we
81 * have the prefix data already, just skip over that
83 this->increment_read_ptr (prefix_size);
86 bool r = this->peek (&status, sizeof(uint8_t));
87 assert (r); // If this failed, buffer is corrupt, all hope is lost
89 /* lets see if we are going to be able to write this event into dst.
91 uint8_t* write_loc = dst.reserve (ev_time, ev_size);
93 if (stop_on_overflow_in_dst) {
94 DEBUG_TRACE (DEBUG::MidiDiskstreamIO, string_compose ("MidiRingBuffer: overflow in destination MIDI buffer, stopped after %1 events\n", count));
97 error << "MRB: Unable to reserve space in buffer, event skipped" << endmsg;
98 this->increment_read_ptr (ev_size); // Advance read pointer to next event
102 // write MIDI buffer contents
103 bool success = read_contents (ev_size, write_loc);
106 if (DEBUG_ENABLED (DEBUG::MidiDiskstreamIO)) {
108 DEBUG_STR_APPEND(a, string_compose ("wrote MidiEvent to Buffer (time=%1, start=%2 offset=%3)", ev_time, start, offset));
109 for (size_t i=0; i < ev_size; ++i) {
110 DEBUG_STR_APPEND(a,hex);
111 DEBUG_STR_APPEND(a,"0x");
112 DEBUG_STR_APPEND(a,(int)write_loc[i]);
113 DEBUG_STR_APPEND(a,' ');
115 DEBUG_STR_APPEND(a,'\n');
116 DEBUG_TRACE (DEBUG::MidiDiskstreamIO, DEBUG_STR(a).str());
121 _tracker.track(write_loc);
124 cerr << "WARNING: error reading event contents from MIDI ring" << endl;
133 MidiRingBuffer<T>::skip_to(framepos_t start)
135 if (this->read_space() == 0) {
142 const size_t prefix_size = sizeof(T) + sizeof(Evoral::EventType) + sizeof(uint32_t);
144 while (this->read_space() >= prefix_size) {
146 uint8_t peekbuf[prefix_size];
147 this->peek (peekbuf, prefix_size);
149 ev_time = *(reinterpret_cast<T*>((uintptr_t)peekbuf));
150 ev_size = *(reinterpret_cast<uint32_t*>((uintptr_t)(peekbuf + sizeof(T) + sizeof (Evoral::EventType))));
152 if (ev_time >= start) {
156 if (this->read_space() < ev_size) {
160 this->increment_read_ptr (prefix_size);
163 bool r = this->peek (&status, sizeof(uint8_t));
164 assert (r); // If this failed, buffer is corrupt, all hope is lost
168 /* TODO investigate and think:
170 * Does it makes sense to keep track of notes
171 * that are skipped (because they're either too late
172 * (underrun) or never used (read-ahead, loop) ?
174 * skip_to() is called on the rinbuffer between
175 * disk and process. it seems wrong to track them
176 * (a potential synth never sees skipped notes, either)
177 * but there may be more to this.
181 this->increment_read_ptr (ev_size);
183 // we only track note on/off, 8 bytes are plenty.
184 uint8_t write_loc[8];
185 bool success = read_contents (ev_size, write_loc);
187 _tracker.track(write_loc);
198 MidiRingBuffer<T>::flush (framepos_t /*start*/, framepos_t end)
200 const size_t prefix_size = sizeof(T) + sizeof(Evoral::EventType) + sizeof(uint32_t);
202 while (this->read_space() >= prefix_size) {
203 uint8_t peekbuf[prefix_size];
208 success = this->peek (peekbuf, prefix_size);
209 /* this cannot fail, because we've already verified that there
210 is prefix_space to read
214 ev_time = *(reinterpret_cast<T*>((uintptr_t)peekbuf));
216 if (ev_time >= end) {
220 ev_size = *(reinterpret_cast<uint32_t*>((uintptr_t)(peekbuf + sizeof(T) + sizeof (Evoral::EventType))));
221 this->increment_read_ptr (prefix_size);
222 this->increment_read_ptr (ev_size);
228 MidiRingBuffer<T>::dump(ostream& str)
232 if ((rspace = this->read_space()) == 0) {
233 str << "MRB::dump: empty\n";
238 Evoral::EventType ev_type;
241 RingBufferNPT<uint8_t>::rw_vector vec;
242 RingBufferNPT<uint8_t>::get_read_vector (&vec);
244 if (vec.len[0] == 0) {
248 str << this << ": Dump size = " << vec.len[0] + vec.len[1]
249 << " r@ " << RingBufferNPT<uint8_t>::get_read_ptr()
250 << " w@" << RingBufferNPT<uint8_t>::get_write_ptr() << endl;
253 uint8_t *buf = new uint8_t[vec.len[0] + vec.len[1]];
254 memcpy (buf, vec.buf[0], vec.len[0]);
257 memcpy (buf+vec.len[1], vec.buf[1], vec.len[1]);
261 const uint8_t* end = buf + vec.len[0] + vec.len[1];
265 memcpy (&ev_time, data, sizeof (T));
267 str << "\ttime " << ev_time;
270 str << "(incomplete)\n ";
274 memcpy (&ev_type, data, sizeof (ev_type));
275 data += sizeof (ev_type);
276 str << " type " << ev_type;
279 str << "(incomplete)\n";
283 memcpy (&ev_size, data, sizeof (ev_size));
284 data += sizeof (ev_size);
285 str << " size " << ev_size;
288 str << "(incomplete)\n";
292 for (uint32_t i = 0; i != ev_size && data < end; ++i) {
293 str << ' ' << hex << (int) data[i] << dec;
306 MidiRingBuffer<T>::reset_tracker ()
313 MidiRingBuffer<T>::resolve_tracker (MidiBuffer& dst, framepos_t t)
315 _tracker.resolve_notes (dst, t);
320 MidiRingBuffer<T>::resolve_tracker (Evoral::EventSink<framepos_t>& dst, framepos_t t)
322 _tracker.resolve_notes(dst, t);
325 template class MidiRingBuffer<framepos_t>;
327 } // namespace ARDOUR