do not allow smf_source's reads to stomp on cached read_end position in parent class...
[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 using namespace std;
24
25 namespace ARDOUR {
26
27 /** Read a block of MIDI events from buffer into a MidiBuffer.
28  *
29  * Timestamps of events returned are relative to start (i.e. event with stamp 0
30  * occurred at start), with offset added.
31  */
32 template<typename T>
33 size_t
34 MidiRingBuffer<T>::read(MidiBuffer& dst, nframes_t start, nframes_t end, nframes_t offset)
35 {
36         if (this->read_space() == 0) {
37                 return 0;
38         }
39
40         T                 ev_time;
41         Evoral::EventType ev_type;
42         uint32_t          ev_size;
43
44         size_t count = 0;
45
46         while (this->read_space() >= sizeof(T) + sizeof(Evoral::EventType) + sizeof(uint32_t)) {
47
48                 this->full_peek(sizeof(T), (uint8_t*)&ev_time);
49                 
50                 if (ev_time > end) {
51                         // cerr << "MRB event @ " << ev_time << " past end @ " << end << endl;
52                         break;
53                 } else if (ev_time < start) {
54                         // cerr << "MRB event @ " << ev_time << " before start @ " << start << endl;
55                         break;
56                 }
57
58                 bool success = read_prefix(&ev_time, &ev_type, &ev_size);
59                 if (!success) {
60                         cerr << "WARNING: error reading event prefix from MIDI ring" << endl;
61                         continue;
62                 }
63
64                 // This event marks a loop end (i.e. the next event's timestamp will be non-monotonic)
65                 if (ev_type == LoopEventType) {
66                         /*ev_time -= start;
67                           ev_time += offset;*/
68                         // cerr << "MRB loop boundary @ " << ev_time << endl;
69
70                         // Return without reading data or writing to buffer (loop events have no data)
71                         // FIXME: This is not correct, loses events after the loop this cycle
72                         return count + 1;
73                 }
74
75                 uint8_t status;
76                 success = this->full_peek(sizeof(uint8_t), &status);
77                 assert(success); // If this failed, buffer is corrupt, all hope is lost
78
79                 // Ignore event if it doesn't match channel filter
80                 if (is_channel_event(status) && get_channel_mode() == FilterChannels) {
81                         const uint8_t channel = status & 0x0F;
82                         if (!(get_channel_mask() & (1L << channel))) {
83                                 //cerr << "MRB skipping event due to channel mask" << endl;
84                                 this->skip(ev_size); // Advance read pointer to next event
85                                 continue;
86                         }
87                 }
88                 
89                 assert(ev_time >= start);
90                 ev_time -= start;
91                 ev_time += offset;
92
93                 // write the timestamp to address (write_loc - 1)
94                 uint8_t* write_loc = dst.reserve(ev_time, ev_size);
95                 if (write_loc == NULL) {
96                         cerr << "MRB: Unable to reserve space in buffer, event skipped";
97                         continue;
98                 }
99                 
100                 // write MIDI buffer contents
101                 success = Evoral::EventRingBuffer<T>::full_read(ev_size, write_loc);
102                 
103 #if 0
104                 cerr << "wrote MidiEvent to Buffer: " << hex;
105                 for (size_t i=0; i < ev_size; ++i) {
106                         cerr << (int) write_loc[i] << ' ';
107                 }
108                 cerr << dec << endl;
109 #endif
110
111                 if (success) {
112                         if (is_channel_event(status) && get_channel_mode() == ForceChannel) {
113                                 write_loc[0] = (write_loc[0] & 0xF0) | (get_channel_mask() & 0x0F);
114                         }
115                         ++count;
116                 } else {
117                         cerr << "WARNING: error reading event contents from MIDI ring" << endl;
118                 }
119         }
120         
121         return count;
122 }
123 template<typename T>
124 void
125 MidiRingBuffer<T>::dump(ostream& str) 
126 {
127         size_t rspace;
128
129         if ((rspace = this->read_space()) == 0) {
130                 str << "MRB::dump: empty\n";
131                 return;
132         }
133
134         T                 ev_time;
135         Evoral::EventType ev_type;
136         uint32_t          ev_size;
137         size_t read_ptr = g_atomic_int_get (&this->_read_ptr);
138
139         str << "Dump @ " << read_ptr << endl;
140         
141         while (1) {
142                 uint8_t* wp;
143                 uint8_t* data;
144                 size_t write_ptr;
145
146 #define space(r,w) ((w > r) ? (w - r) : ((w - r + this->_size) % this->_size))
147                 
148                 write_ptr  = g_atomic_int_get (&this->_write_ptr);
149                 if (space (read_ptr, write_ptr) < sizeof (T)) {
150                         break;
151                 }
152
153                 wp = &this->_buf[read_ptr];
154                 memcpy (&ev_time, wp, sizeof (T)); 
155                 read_ptr = (read_ptr + sizeof (T)) % this->_size;
156                 str << "time " << ev_time;
157
158                 write_ptr  = g_atomic_int_get (&this->_write_ptr);
159                 if (space (read_ptr, write_ptr) < sizeof (ev_type)) {
160                         break;
161                 }
162
163                 wp = &this->_buf[read_ptr];
164                 memcpy (&ev_type, wp, sizeof (ev_type)); 
165                 read_ptr = (read_ptr + sizeof (ev_type)) % this->_size;
166                 str << " type " << ev_type;
167
168                 write_ptr  = g_atomic_int_get (&this->_write_ptr);
169                 if (space (read_ptr, write_ptr) < sizeof (ev_size)) {
170                         str << "!OUT!\n";
171                         break;
172                 }
173
174                 wp = &this->_buf[read_ptr];
175                 memcpy (&ev_size, wp, sizeof (ev_size)); 
176                 read_ptr = (read_ptr + sizeof (ev_size)) % this->_size;
177                 str << " size " << ev_size;
178
179                 write_ptr  = g_atomic_int_get (&this->_write_ptr);
180                 if (space (read_ptr, write_ptr) < ev_size) {
181                         str << "!OUT!\n";
182                         break;
183                 }
184                 
185                 data = new uint8_t[ev_size];
186                 
187                 wp = &this->_buf[read_ptr];
188                 memcpy (data, wp, ev_size); 
189                 read_ptr = (read_ptr + ev_size) % this->_size;
190
191                 for (uint32_t i = 0; i != ev_size; ++i) {
192                         str << ' ' << hex << (int) data[i] << dec;
193                 }
194
195                 str << endl;
196
197                 delete [] data;
198         }
199 }
200
201
202 template class MidiRingBuffer<nframes_t>;
203
204 } // namespace ARDOUR
205