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 "ardour/midi_ring_buffer.h"
20 #include "ardour/midi_buffer.h"
21 #include "ardour/event_type_map.h"
27 /** Read a block of MIDI events from buffer.
29 * Timestamps of events returned are relative to start (i.e. event with stamp 0
30 * occurred at start), with offset added.
33 MidiRingBuffer::read(MidiBuffer& dst, nframes_t start, nframes_t end, nframes_t offset)
35 if (read_space() == 0) {
36 //cerr << "MRB: NO READ SPACE" << endl;
40 Evoral::EventTime ev_time;
41 Evoral::EventType ev_type;
46 //cerr << "MRB read " << start << " .. " << end << " + " << offset << endl;
48 while (read_space() >= sizeof(Evoral::EventTime) + sizeof(Evoral::EventType) + sizeof(uint32_t)) {
50 full_peek(sizeof(Evoral::EventTime), (uint8_t*)&ev_time);
53 //cerr << "MRB: PAST END (" << ev_time << " : " << end << ")" << endl;
55 } else if (ev_time < start) {
56 //cerr << "MRB (start " << start << ") - Skipping event at (too early) time " << ev_time << endl;
60 bool success = read_prefix(&ev_time, &ev_type, &ev_size);
62 cerr << "WARNING: error reading event prefix from MIDI ring" << endl;
66 // This event marks a loop happening. this means that
67 // the next events timestamp will be non-monotonic.
68 if (ev_type == LoopEventType) {
71 Evoral::MIDIEvent loopevent(LoopEventType, ev_time);
72 dst.push_back(loopevent);
74 // We can safely return, without reading the data, because
75 // a LoopEvent does not have data.
80 success = full_peek(sizeof(uint8_t), &status);
81 assert(success); // If this failed, buffer is corrupt, all hope is lost
83 // Ignore event if it doesn't match channel filter
84 if (is_channel_event(status) && get_channel_mode() == FilterChannels) {
85 const uint8_t channel = status & 0x0F;
86 if ( !(get_channel_mask() & (1L << channel)) ) {
87 //cerr << "MRB skipping event due to channel mask" << endl;
88 skip(ev_size); // Advance read pointer to next event
93 //cerr << "MRB " << this << " - Reading event, time = "
94 // << ev_time << " - " << start << " => " << ev_time - start
95 // << ", size = " << ev_size << endl;
97 assert(ev_time >= start);
101 uint8_t* write_loc = dst.reserve(ev_time, ev_size);
102 if (write_loc == NULL) {
103 //cerr << "MRB: Unable to reserve space in buffer, event skipped";
107 success = Evoral::EventRingBuffer::full_read(ev_size, write_loc);
110 if (is_channel_event(status) && get_channel_mode() == ForceChannel) {
111 write_loc[0] = (write_loc[0] & 0xF0) | (get_channel_mask() & 0x0F);
114 //cerr << "MRB - read event at time " << ev_time << endl;
116 //cerr << "WARNING: error reading event contents from MIDI ring" << endl;
120 //cerr << "MTB read space: " << read_space() << endl;
126 } // namespace ARDOUR