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"
21 #include "ardour/debug.h"
22 #include "ardour/midi_ring_buffer.h"
23 #include "ardour/midi_buffer.h"
24 #include "ardour/event_type_map.h"
27 using namespace ARDOUR;
30 /** Read a block of MIDI events from buffer into a MidiBuffer.
32 * Timestamps of events returned are relative to start (i.e. event with stamp 0
33 * occurred at start), with offset added.
37 MidiRingBuffer<T>::read(MidiBuffer& dst, nframes_t start, nframes_t end, nframes_t offset)
39 if (this->read_space() == 0) {
44 Evoral::EventType ev_type;
47 /* If we see the end of a loop during this read, we must write the events after it
48 to the MidiBuffer with adjusted times. The situation is as follows:
50 session frames----------------------------->
53 start_of_loop start end_of_loop
55 The MidiDiskstream::read method which will have happened before this checks for
56 loops ending, and helpfully inserts a magic LoopEvent into the ringbuffer. After this,
57 the MidiDiskstream continues to write events with their proper session frame times,
58 so after the LoopEvent event times will go backwards (ie non-monotonically).
60 Once we hit end_of_loop, we need to fake it to make it look as though the loop has been
61 immediately repeated. Say that an event E after the end_of_loop in the ringbuffer
62 has time E_t, which is a time in session frames. Its offset from the start
63 of the loop will be E_t - start_of_loop. Its `faked' time will therefore be
64 end_of_loop + E_t - start_of_loop. And so its port-buffer-relative time (for
65 writing to the MidiBuffer) will be end_of_loop + E_t - start_of_loop - start.
67 The subtraction of start is already taken care of, so if we see a LoopEvent, we'll
68 set up loop_offset to equal end_of_loop - start_of_loop, so that given an event
69 time E_t in the ringbuffer we can get the port-buffer-relative time as
73 frameoffset_t loop_offset = 0;
77 while (this->read_space() >= sizeof(T) + sizeof(Evoral::EventType) + sizeof(uint32_t)) {
79 this->full_peek(sizeof(T), (uint8_t*)&ev_time);
81 if (ev_time + loop_offset >= end) {
82 DEBUG_TRACE (DEBUG::MidiDiskstreamIO, string_compose ("MRB event @ %1 past end @ %2\n", ev_time, end));
84 } else if (ev_time + loop_offset < start) {
85 DEBUG_TRACE (DEBUG::MidiDiskstreamIO, string_compose ("MRB event @ %1 before start @ %2\n", ev_time, start));
88 DEBUG_TRACE (DEBUG::MidiDiskstreamIO, string_compose ("MRB event @ %1 in range %2 .. %3\n", ev_time, start, end));
91 bool success = read_prefix(&ev_time, &ev_type, &ev_size);
93 cerr << "WARNING: error reading event prefix from MIDI ring" << endl;
97 // This event marks a loop end (i.e. the next event's timestamp will be non-monotonic)
98 if (ev_type == LoopEventType) {
99 cerr << "MRB loop boundary @ " << ev_time << endl;
101 assert (ev_size == sizeof (nframes_t));
102 nframes_t loop_start;
103 read_contents (ev_size, (uint8_t *) &loop_start);
105 loop_offset = ev_time - loop_start;
109 ev_time += loop_offset;
112 success = this->full_peek(sizeof(uint8_t), &status);
113 assert(success); // If this failed, buffer is corrupt, all hope is lost
115 // Ignore event if it doesn't match channel filter
116 if (is_channel_event(status) && get_channel_mode() == FilterChannels) {
117 const uint8_t channel = status & 0x0F;
118 if (!(get_channel_mask() & (1L << channel))) {
119 // cerr << "MRB skipping event due to channel mask" << endl;
120 this->skip(ev_size); // Advance read pointer to next event
125 assert(ev_time >= start);
130 // write the timestamp to address (write_loc - 1)
131 uint8_t* write_loc = dst.reserve(ev_time, ev_size);
132 if (write_loc == NULL) {
133 cerr << "MRB: Unable to reserve space in buffer, event skipped";
134 this->skip (ev_size); // Advance read pointer to next event
138 // write MIDI buffer contents
139 success = read_contents (ev_size, write_loc);
143 DEBUG_STR_APPEND(a, string_compose ("wrote MidiEvent to Buffer (time=%1, start=%2 offset=%3)", ev_time, start, offset));
144 for (size_t i=0; i < ev_size; ++i) {
145 DEBUG_STR_APPEND(a,hex);
146 DEBUG_STR_APPEND(a,"0x");
147 DEBUG_STR_APPEND(a,(int)write_loc[i]);
148 DEBUG_STR_APPEND(a,' ');
150 DEBUG_STR_APPEND(a,'\n');
151 DEBUG_TRACE (DEBUG::MidiDiskstreamIO, DEBUG_STR(a).str());
155 if (is_channel_event(status) && get_channel_mode() == ForceChannel) {
156 write_loc[0] = (write_loc[0] & 0xF0) | (get_channel_mask() & 0x0F);
160 cerr << "WARNING: error reading event contents from MIDI ring" << endl;
168 MidiRingBuffer<T>::dump(ostream& str)
172 if ((rspace = this->read_space()) == 0) {
173 str << "MRB::dump: empty\n";
178 Evoral::EventType ev_type;
180 size_t read_ptr = g_atomic_int_get (&this->_read_ptr);
182 str << "Dump @ " << read_ptr << endl;
189 #define space(r,w) ((w > r) ? (w - r) : ((w - r + this->_size) % this->_size))
191 write_ptr = g_atomic_int_get (&this->_write_ptr);
192 if (space (read_ptr, write_ptr) < sizeof (T)) {
196 wp = &this->_buf[read_ptr];
197 memcpy (&ev_time, wp, sizeof (T));
198 read_ptr = (read_ptr + sizeof (T)) % this->_size;
199 str << "time " << ev_time;
201 write_ptr = g_atomic_int_get (&this->_write_ptr);
202 if (space (read_ptr, write_ptr) < sizeof (ev_type)) {
206 wp = &this->_buf[read_ptr];
207 memcpy (&ev_type, wp, sizeof (ev_type));
208 read_ptr = (read_ptr + sizeof (ev_type)) % this->_size;
209 str << " type " << ev_type;
211 write_ptr = g_atomic_int_get (&this->_write_ptr);
212 if (space (read_ptr, write_ptr) < sizeof (ev_size)) {
217 wp = &this->_buf[read_ptr];
218 memcpy (&ev_size, wp, sizeof (ev_size));
219 read_ptr = (read_ptr + sizeof (ev_size)) % this->_size;
220 str << " size " << ev_size;
222 write_ptr = g_atomic_int_get (&this->_write_ptr);
223 if (space (read_ptr, write_ptr) < ev_size) {
228 data = new uint8_t[ev_size];
230 wp = &this->_buf[read_ptr];
231 memcpy (data, wp, ev_size);
232 read_ptr = (read_ptr + ev_size) % this->_size;
234 for (uint32_t i = 0; i != ev_size; ++i) {
235 str << ' ' << hex << (int) data[i] << dec;
245 template class MidiRingBuffer<nframes_t>;