Move MidiRingBuffer::read into .cc file to avoid the 2 hour dev cycle (no functional...
[ardour.git] / libs / ardour / midi_ring_buffer.cc
1 /*
2     Copyright (C) 2006-2008 Paul Davis
3
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.
8
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.
13
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.
17 */
18
19 #include "ardour/midi_ring_buffer.h"
20 #include "ardour/midi_buffer.h"
21 #include "ardour/event_type_map.h"
22
23 namespace ARDOUR {
24
25 /** Read a block of MIDI events from buffer.
26  *
27  * Timestamps of events returned are relative to start (i.e. event with stamp 0
28  * occurred at start), with offset added.
29  */
30 size_t
31 MidiRingBuffer::read(MidiBuffer& dst, nframes_t start, nframes_t end, nframes_t offset)
32 {
33         if (read_space() == 0) {
34                 //std::cerr << "MRB: NO READ SPACE" << std::endl;
35                 return 0;
36         }
37
38         Evoral::EventTime ev_time;
39         Evoral::EventType ev_type;
40         uint32_t          ev_size;
41
42         size_t count = 0;
43
44         //std::cerr << "MRB read " << start << " .. " << end << " + " << offset << std::endl;
45
46         while (read_space() >= sizeof(Evoral::EventTime) + sizeof(Evoral::EventType) + sizeof(uint32_t)) {
47
48                 full_peek(sizeof(Evoral::EventTime), (uint8_t*)&ev_time);
49
50                 if (ev_time > end) {
51                         //std::cerr << "MRB: PAST END (" << ev_time << " : " << end << ")" << std::endl;
52                         break;
53                 } else if (ev_time < start) {
54                         //std::cerr << "MRB (start " << start << ") - Skipping event at (too early) time " << ev_time << std::endl;
55                         break;
56                 }
57
58                 bool success = read_prefix(&ev_time, &ev_type, &ev_size);
59                 if (!success) {
60                         std::cerr << "WARNING: error reading event prefix from MIDI ring" << std::endl;
61                         continue;
62                 }
63
64                 // This event marks a loop happening. this means that
65                 // the next events timestamp will be non-monotonic.
66                 if (ev_type == LoopEventType) {
67                         ev_time -= start;
68                         ev_time += offset;
69                         Evoral::MIDIEvent loopevent(LoopEventType, ev_time); 
70                         dst.push_back(loopevent);
71
72                         
73                         // We can safely return, without reading the data, because
74                         // a LoopEvent does not have data.
75                         return count + 1;
76                 }
77
78                 uint8_t status;
79                 success = full_peek(sizeof(uint8_t), &status);
80                 assert(success); // If this failed, buffer is corrupt, all hope is lost
81
82                 // Ignore event if it doesn't match channel filter
83                 if (is_channel_event(status) && get_channel_mode() == FilterChannels) {
84                         const uint8_t channel = status & 0x0F;
85                         if ( !(get_channel_mask() & (1L << channel)) ) {
86                                 //std::cerr << "MRB skipping event due to channel mask" << std::endl;
87                                 skip(ev_size); // Advance read pointer to next event
88                                 continue;
89                         }
90                 }
91
92                 //std::cerr << "MRB " << this << " - Reading event, time = "
93                 //      << ev_time << " - " << start << " => " << ev_time - start
94                 //      << ", size = " << ev_size << std::endl;
95
96                 assert(ev_time >= start);
97                 ev_time -= start;
98                 ev_time += offset;
99
100                 uint8_t* write_loc = dst.reserve(ev_time, ev_size);
101                 if (write_loc == NULL) {
102                         //std::cerr << "MRB: Unable to reserve space in buffer, event skipped";
103                         continue;
104                 }
105
106                 success = Evoral::EventRingBuffer::full_read(ev_size, write_loc);
107
108                 if (success) {
109                         if (is_channel_event(status) && get_channel_mode() == ForceChannel) {
110                                 write_loc[0] = (write_loc[0] & 0xF0) | (get_channel_mask() & 0x0F);
111                         }
112                         ++count;
113                         //std::cerr << "MRB - read event at time " << ev_time << std::endl;
114                 } else {
115                         std::cerr << "WARNING: error reading event contents from MIDI ring" << std::endl;
116                 }
117         }
118         
119         //std::cerr << "MTB read space: " << read_space() << std::endl;
120
121         return count;
122 }
123
124
125 } // namespace ARDOUR
126