Collect plugin runtime profile statistics.
[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/enumwriter.h"
21 #include "pbd/error.h"
22
23 #include "ardour/debug.h"
24 #include "ardour/midi_ring_buffer.h"
25 #include "ardour/midi_buffer.h"
26 #include "ardour/event_type_map.h"
27
28 using namespace std;
29 using namespace PBD;
30
31 namespace ARDOUR {
32
33 /** Read a block of MIDI events from this buffer into a MidiBuffer.
34  *
35  * Timestamps of events returned are relative to start (i.e. event with stamp 0
36  * occurred at start), with offset added.
37  */
38 template<typename T>
39 size_t
40 MidiRingBuffer<T>::read (MidiBuffer& dst, samplepos_t start, samplepos_t end, samplecnt_t offset, bool stop_on_overflow_in_dst)
41 {
42         if (this->read_space() == 0) {
43                 return 0;
44         }
45
46         T                 ev_time;
47         uint32_t          ev_size;
48         size_t            count = 0;
49         const size_t      prefix_size = sizeof(T) + sizeof(Evoral::EventType) + sizeof(uint32_t);
50
51         while (this->read_space() >= prefix_size) {
52
53                 uint8_t peekbuf[prefix_size];
54
55                 /* this cannot fail, because we've already verified that there
56                    is prefix_space to read
57                 */
58                 this->peek (peekbuf, prefix_size);
59
60                 ev_time = *(reinterpret_cast<T*>((uintptr_t)peekbuf));
61                 ev_size = *(reinterpret_cast<uint32_t*>((uintptr_t)(peekbuf + sizeof(T) + sizeof (Evoral::EventType))));
62
63                 if (this->read_space() < ev_size) {
64                         break;;
65                 }
66
67                 if (ev_time >= end) {
68                         DEBUG_TRACE (DEBUG::MidiRingBuffer, string_compose ("MRB event @ %1 past end @ %2\n", ev_time, end));
69                         break;
70                 } else if (ev_time < start) {
71                         DEBUG_TRACE (DEBUG::MidiRingBuffer, string_compose ("MRB event @ %1 before start @ %2\n", ev_time, start));
72                         break;
73                 } else {
74                         DEBUG_TRACE (DEBUG::MidiRingBuffer, string_compose ("MRB event @ %1 in range %2 .. %3\n", ev_time, start, end));
75                 }
76
77                 ev_time -= start;
78                 ev_time += offset;
79
80                 /* we're good to go ahead and read the data now but since we
81                  * have the prefix data already, just skip over that
82                  */
83                 this->increment_read_ptr (prefix_size);
84
85                 uint8_t status;
86                 bool r = this->peek (&status, sizeof(uint8_t));
87                 assert (r); // If this failed, buffer is corrupt, all hope is lost
88
89                 /* lets see if we are going to be able to write this event into dst.
90                  */
91                 uint8_t* write_loc = dst.reserve (ev_time, ev_size);
92                 if (write_loc == 0) {
93                         if (stop_on_overflow_in_dst) {
94                                 DEBUG_TRACE (DEBUG::MidiRingBuffer, string_compose ("MidiRingBuffer: overflow in destination MIDI buffer, stopped after %1 events\n", count));
95                                 break;
96                         }
97                         error << "MRB: Unable to reserve space in buffer, event skipped" << endmsg;
98                         this->increment_read_ptr (ev_size); // Advance read pointer to next event
99                         continue;
100                 }
101
102                 // write MIDI buffer contents
103
104                 bool success = read_contents (ev_size, write_loc);
105 #ifndef NDEBUG
106                 if (DEBUG_ENABLED (DEBUG::MidiRingBuffer)) {
107                         DEBUG_STR_DECL(a);
108                         DEBUG_STR_APPEND(a, string_compose ("wrote MidiEvent to Buffer (time=%1, start=%2 offset=%3) ", ev_time, start, offset));
109                         for (size_t i=0; i < ev_size; ++i) {
110                                 DEBUG_STR_APPEND(a,hex);
111                                 DEBUG_STR_APPEND(a,"0x");
112                                 DEBUG_STR_APPEND(a,(int)write_loc[i]);
113                                 DEBUG_STR_APPEND(a,' ');
114                         }
115                         DEBUG_STR_APPEND(a,'\n');
116                         DEBUG_TRACE (DEBUG::MidiRingBuffer, DEBUG_STR(a).str());
117                 }
118 #endif
119                 if (success) {
120                         _tracker.track(write_loc);
121                         ++count;
122                 } else {
123                         cerr << "WARNING: error reading event contents from MIDI ring" << endl;
124                 }
125         }
126
127         return count;
128 }
129
130 template<typename T>
131 size_t
132 MidiRingBuffer<T>::skip_to(samplepos_t start)
133 {
134         if (this->read_space() == 0) {
135                 return 0;
136         }
137
138         T                 ev_time;
139         uint32_t          ev_size;
140         size_t            count = 0;
141         const size_t      prefix_size = sizeof(T) + sizeof(Evoral::EventType) + sizeof(uint32_t);
142
143         while (this->read_space() >= prefix_size) {
144
145                 uint8_t peekbuf[prefix_size];
146                 this->peek (peekbuf, prefix_size);
147
148                 ev_time = *(reinterpret_cast<T*>((uintptr_t)peekbuf));
149                 ev_size = *(reinterpret_cast<uint32_t*>((uintptr_t)(peekbuf + sizeof(T) + sizeof (Evoral::EventType))));
150
151                 if (ev_time >= start) {
152                         return count;
153                 }
154
155                 if (this->read_space() < ev_size) {
156                         continue;
157                 }
158
159                 this->increment_read_ptr (prefix_size);
160
161                 uint8_t status;
162                 bool r = this->peek (&status, sizeof(uint8_t));
163                 assert (r); // If this failed, buffer is corrupt, all hope is lost
164
165                 ++count;
166
167                 /* TODO investigate and think:
168                  *
169                  * Does it makes sense to keep track of notes
170                  * that are skipped (because they're either too late
171                  * (underrun) or never used (read-ahead, loop) ?
172                  *
173                  * skip_to() is called on the rinbuffer between
174                  * disk and process. it seems wrong to track them
175                  * (a potential synth never sees skipped notes, either)
176                  * but there may be more to this.
177                  */
178
179                 if (ev_size >= 8) {
180                         this->increment_read_ptr (ev_size);
181                 } else {
182                         // we only track note on/off, 8 bytes are plenty.
183                         uint8_t write_loc[8];
184                         bool success = read_contents (ev_size, write_loc);
185                         if (success) {
186                                 _tracker.track(write_loc);
187                         }
188                 }
189         }
190         return count;
191 }
192
193
194
195 template<typename T>
196 void
197 MidiRingBuffer<T>::flush (samplepos_t /*start*/, samplepos_t end)
198 {
199         const size_t prefix_size = sizeof(T) + sizeof(Evoral::EventType) + sizeof(uint32_t);
200
201         while (this->read_space() >= prefix_size) {
202                 uint8_t  peekbuf[prefix_size];
203                 bool     success;
204                 uint32_t ev_size;
205                 T        ev_time;
206
207                 success = this->peek (peekbuf, prefix_size);
208                 /* this cannot fail, because we've already verified that there
209                    is prefix_space to read
210                 */
211                 assert (success);
212
213                 ev_time = *(reinterpret_cast<T*>((uintptr_t)peekbuf));
214
215                 if (ev_time >= end) {
216                         break;
217                 }
218
219                 ev_size = *(reinterpret_cast<uint32_t*>((uintptr_t)(peekbuf + sizeof(T) + sizeof (Evoral::EventType))));
220                 this->increment_read_ptr (prefix_size);
221                 this->increment_read_ptr (ev_size);
222         }
223 }
224
225 template<typename T>
226 void
227 MidiRingBuffer<T>::dump(ostream& str)
228 {
229         size_t rspace;
230
231         if ((rspace = this->read_space()) == 0) {
232                 str << this << " MRB::dump: empty\n";
233                 return;
234         }
235
236         T                 ev_time;
237         Evoral::EventType ev_type;
238         uint32_t          ev_size;
239
240         RingBufferNPT<uint8_t>::rw_vector vec;
241         RingBufferNPT<uint8_t>::get_read_vector (&vec);
242
243         if (vec.len[0] == 0) {
244                 return;
245         }
246
247         str << this << ": Dump size = " << vec.len[0] + vec.len[1]
248             << " r@ " << RingBufferNPT<uint8_t>::get_read_ptr()
249             << " w@" << RingBufferNPT<uint8_t>::get_write_ptr() << endl;
250
251
252         uint8_t *buf = new uint8_t[vec.len[0] + vec.len[1]];
253         memcpy (buf, vec.buf[0], vec.len[0]);
254
255         if (vec.len[1]) {
256                 memcpy (buf+vec.len[1], vec.buf[1], vec.len[1]);
257         }
258
259         uint8_t* data = buf;
260         const uint8_t* end = buf + vec.len[0] + vec.len[1];
261
262         while (data < end) {
263
264                 memcpy (&ev_time, data, sizeof (T));
265                 data += sizeof (T);
266                 str << "\ttime " << ev_time;
267
268                 if (data >= end) {
269                         str << "(incomplete)\n ";
270                         break;
271                 }
272
273                 memcpy (&ev_type, data, sizeof (ev_type));
274                 data += sizeof (ev_type);
275                 str << " type " << ev_type;
276
277                 if (data >= end) {
278                         str << "(incomplete)\n";
279                         break;
280                 }
281
282                 memcpy (&ev_size, data, sizeof (ev_size));
283                 data += sizeof (ev_size);
284                 str << " size " << ev_size;
285
286                 if (data >= end) {
287                         str << "(incomplete)\n";
288                         break;
289                 }
290
291                 for (uint32_t i = 0; i != ev_size && data < end; ++i) {
292                         str << ' ' << hex << (int) data[i] << dec;
293                 }
294
295                 data += ev_size;
296
297                 str << endl;
298         }
299
300         delete [] buf;
301 }
302
303 template<typename T>
304 void
305 MidiRingBuffer<T>::reset_tracker ()
306 {
307         _tracker.reset ();
308 }
309
310 template<typename T>
311 void
312 MidiRingBuffer<T>::resolve_tracker (MidiBuffer& dst, samplepos_t t)
313 {
314         _tracker.resolve_notes (dst, t);
315 }
316
317 template<typename T>
318 void
319 MidiRingBuffer<T>::resolve_tracker (Evoral::EventSink<samplepos_t>& dst, samplepos_t t)
320 {
321         _tracker.resolve_notes(dst, t);
322 }
323
324 template class MidiRingBuffer<samplepos_t>;
325
326 }  // namespace ARDOUR