fix another semantically-significant statement buried in an assert() macro
[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 "pbd/compose.h"
20 #include "pbd/error.h"
21
22 #include "ardour/debug.h"
23 #include "ardour/midi_ring_buffer.h"
24 #include "ardour/midi_buffer.h"
25 #include "ardour/event_type_map.h"
26
27 using namespace std;
28 using namespace PBD;
29
30 namespace ARDOUR {
31
32 /** Read a block of MIDI events from this buffer into a MidiBuffer.
33  *
34  * Timestamps of events returned are relative to start (i.e. event with stamp 0
35  * occurred at start), with offset added.
36  */
37 template<typename T>
38 size_t
39 MidiRingBuffer<T>::read(MidiBuffer& dst, framepos_t start, framepos_t end, framecnt_t offset, bool stop_on_overflow_in_dst)
40 {
41         if (this->read_space() == 0) {
42                 return 0;
43         }
44
45         T                 ev_time;
46         uint32_t          ev_size;
47         size_t            count = 0;
48         const size_t      prefix_size = sizeof(T) + sizeof(Evoral::EventType) + sizeof(uint32_t);
49
50         while (this->read_space() >= prefix_size) {
51
52                 uint8_t peekbuf[prefix_size];
53
54                 /* this cannot fail, because we've already verified that there
55                    is prefix_space to read
56                 */
57                 this->peek (peekbuf, prefix_size);
58
59                 ev_time = *((T*) peekbuf);
60                 ev_size = *((uint32_t*)(peekbuf + sizeof(T) + sizeof (Evoral::EventType)));
61
62                 if (ev_time >= end) {
63                         DEBUG_TRACE (DEBUG::MidiDiskstreamIO, string_compose ("MRB event @ %1 past end @ %2\n", ev_time, end));
64                         break;
65                 } else if (ev_time < start) {
66                         DEBUG_TRACE (DEBUG::MidiDiskstreamIO, string_compose ("MRB event @ %1 before start @ %2\n", ev_time, start));
67                         break;
68                 } else {
69                         DEBUG_TRACE (DEBUG::MidiDiskstreamIO, string_compose ("MRB event @ %1 in range %2 .. %3\n", ev_time, start, end));
70                 }
71
72                 ev_time -= start;
73                 ev_time += offset;
74
75                 /* we're good to go ahead and read the data now but since we
76                  * have the prefix data already, just skip over that
77                  */
78                 this->increment_read_ptr (prefix_size);
79
80                 uint8_t status;
81                 bool r = this->peek (&status, sizeof(uint8_t)); 
82                 assert (r); // If this failed, buffer is corrupt, all hope is lost
83
84                 // Ignore event if it doesn't match channel filter
85                 if (is_channel_event(status) && get_channel_mode() == FilterChannels) {
86                         const uint8_t channel = status & 0x0F;
87                         if (!(get_channel_mask() & (1L << channel))) {
88                                 DEBUG_TRACE (DEBUG::MidiDiskstreamIO, string_compose ("MRB skipping event (%3 bytes) due to channel mask (mask = %1 chn = %2)\n",
89                                                                                       get_channel_mask(), (int) channel, ev_size));
90                                 this->increment_read_ptr (ev_size); // Advance read pointer to next event
91                                 continue;
92                         }
93                 }
94
95                 /* lets see if we are going to be able to write this event into dst.
96                  */
97                 uint8_t* write_loc = dst.reserve (ev_time, ev_size);
98                 if (write_loc == 0) {
99                         if (stop_on_overflow_in_dst) {
100                                 DEBUG_TRACE (DEBUG::MidiDiskstreamIO, string_compose ("MidiRingBuffer: overflow in destination MIDI buffer, stopped after %1 events\n", count));
101                                 break;
102                         }
103                         error << "MRB: Unable to reserve space in buffer, event skipped" << endmsg;
104                         this->increment_read_ptr (ev_size); // Advance read pointer to next event
105                         continue;
106                 }
107
108                 // write MIDI buffer contents
109                 bool success = read_contents (ev_size, write_loc);
110
111 #ifndef NDEBUG
112                 if (DEBUG::MidiDiskstreamIO && PBD::debug_bits) {
113                         DEBUG_STR_DECL(a);
114                         DEBUG_STR_APPEND(a, string_compose ("wrote MidiEvent to Buffer (time=%1, start=%2 offset=%3)", ev_time, start, offset));
115                         for (size_t i=0; i < ev_size; ++i) {
116                                 DEBUG_STR_APPEND(a,hex);
117                                 DEBUG_STR_APPEND(a,"0x");
118                                 DEBUG_STR_APPEND(a,(int)write_loc[i]);
119                                 DEBUG_STR_APPEND(a,' ');
120                         }
121                         DEBUG_STR_APPEND(a,'\n');
122                         DEBUG_TRACE (DEBUG::MidiDiskstreamIO, DEBUG_STR(a).str());
123                 }
124 #endif
125
126                 if (success) {
127
128                         if (is_note_on(write_loc[0]) ) {
129                                 _tracker.add (write_loc[1], write_loc[0] & 0xf);
130                         } else if (is_note_off(write_loc[0])) {
131                                 _tracker.remove (write_loc[1], write_loc[0] & 0xf);
132                         }
133                         
134                         if (is_channel_event(status) && get_channel_mode() == ForceChannel) {
135                                 write_loc[0] = (write_loc[0] & 0xF0) | (get_channel_mask() & 0x0F);
136                         }
137                         ++count;
138                 } else {
139                         cerr << "WARNING: error reading event contents from MIDI ring" << endl;
140                 }
141         }
142
143         return count;
144 }
145
146 template<typename T>
147 void
148 MidiRingBuffer<T>::flush (framepos_t /*start*/, framepos_t end)
149 {
150         const size_t prefix_size = sizeof(T) + sizeof(Evoral::EventType) + sizeof(uint32_t);
151
152         while (this->read_space() >= prefix_size) {
153                 uint8_t  peekbuf[prefix_size];
154                 bool     success;
155                 uint32_t ev_size;
156                 T        ev_time;
157
158                 success = this->peek (peekbuf, prefix_size);
159                 /* this cannot fail, because we've already verified that there
160                    is prefix_space to read
161                 */
162                 assert (success);
163
164                 ev_time = *((T*) peekbuf);
165                 
166                 if (ev_time >= end) {
167                         break;
168                 }
169
170                 ev_size = *((uint32_t*)(peekbuf + sizeof(T) + sizeof (Evoral::EventType)));
171                 this->increment_read_ptr (prefix_size);
172                 this->increment_read_ptr (ev_size);
173         }
174 }
175
176 template<typename T>
177 void
178 MidiRingBuffer<T>::dump(ostream& str)
179 {
180         size_t rspace;
181
182         if ((rspace = this->read_space()) == 0) {
183                 str << "MRB::dump: empty\n";
184                 return;
185         }
186
187         T                 ev_time;
188         Evoral::EventType ev_type;
189         uint32_t          ev_size;
190
191         RingBufferNPT<uint8_t>::rw_vector vec;
192         RingBufferNPT<uint8_t>::get_read_vector (&vec);
193
194         if (vec.len[0] == 0) {
195                 return;
196         }
197
198         str << this << ": Dump size = " << vec.len[0] + vec.len[1]
199             << " r@ " << RingBufferNPT<uint8_t>::get_read_ptr()
200             << " w@" << RingBufferNPT<uint8_t>::get_write_ptr() << endl;
201
202
203         uint8_t *buf = new uint8_t[vec.len[0] + vec.len[1]];
204         memcpy (buf, vec.buf[0], vec.len[0]);
205
206         if (vec.len[1]) {
207                 memcpy (buf+vec.len[1], vec.buf[1], vec.len[1]);
208         }
209
210         uint8_t* data = buf;
211         const uint8_t* end = buf + vec.len[0] + vec.len[1];
212
213         while (data < end) {
214
215                 memcpy (&ev_time, data, sizeof (T));
216                 data += sizeof (T);
217                 str << "\ttime " << ev_time;
218
219                 if (data >= end) {
220                         str << "(incomplete)\n ";
221                         break;
222                 }
223
224                 memcpy (&ev_type, data, sizeof (ev_type));
225                 data += sizeof (ev_type);
226                 str << " type " << ev_type;
227
228                 if (data >= end) {
229                         str << "(incomplete)\n";
230                         break;
231                 }
232
233                 memcpy (&ev_size, data, sizeof (ev_size));
234                 data += sizeof (ev_size);
235                 str << " size " << ev_size;
236
237                 if (data >= end) {
238                         str << "(incomplete)\n";
239                         break;
240                 }
241
242                 for (uint32_t i = 0; i != ev_size && data < end; ++i) {
243                         str << ' ' << hex << (int) data[i] << dec;
244                 }
245
246                 data += ev_size;
247
248                 str << endl;
249         }
250
251         delete [] buf;
252 }
253
254 template<typename T>
255 void
256 MidiRingBuffer<T>::reset_tracker ()
257 {
258         _tracker.reset ();
259 }
260
261 template<typename T>
262 void
263 MidiRingBuffer<T>::loop_resolve (MidiBuffer& dst, framepos_t t)
264 {
265         _tracker.resolve_notes (dst, t);
266 }
267
268 template class MidiRingBuffer<framepos_t>;
269
270 }  // namespace ARDOUR