0da3ba68354065eaf5c5cb27197a14b8ef5694d8
[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, framepos_t start, framepos_t end, framecnt_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 (ev_time >= end) {
64                         DEBUG_TRACE (DEBUG::MidiDiskstreamIO, string_compose ("MRB event @ %1 past end @ %2\n", ev_time, end));
65                         break;
66                 } else if (ev_time < start) {
67                         DEBUG_TRACE (DEBUG::MidiDiskstreamIO, string_compose ("MRB event @ %1 before start @ %2\n", ev_time, start));
68                         break;
69                 } else {
70                         DEBUG_TRACE (DEBUG::MidiDiskstreamIO, string_compose ("MRB event @ %1 in range %2 .. %3\n", ev_time, start, end));
71                 }
72
73                 ev_time -= start;
74                 ev_time += offset;
75
76                 /* we're good to go ahead and read the data now but since we
77                  * have the prefix data already, just skip over that
78                  */
79                 this->increment_read_ptr (prefix_size);
80
81                 uint8_t status;
82                 bool r = this->peek (&status, sizeof(uint8_t)); 
83                 assert (r); // If this failed, buffer is corrupt, all hope is lost
84
85                 /* lets see if we are going to be able to write this event into dst.
86                  */
87                 uint8_t* write_loc = dst.reserve (ev_time, ev_size);
88                 if (write_loc == 0) {
89                         if (stop_on_overflow_in_dst) {
90                                 DEBUG_TRACE (DEBUG::MidiDiskstreamIO, string_compose ("MidiRingBuffer: overflow in destination MIDI buffer, stopped after %1 events\n", count));
91                                 break;
92                         }
93                         error << "MRB: Unable to reserve space in buffer, event skipped" << endmsg;
94                         this->increment_read_ptr (ev_size); // Advance read pointer to next event
95                         continue;
96                 }
97
98                 // write MIDI buffer contents
99                 bool success = read_contents (ev_size, write_loc);
100
101 #ifndef NDEBUG
102                 if (DEBUG::MidiDiskstreamIO && PBD::debug_bits) {
103                         DEBUG_STR_DECL(a);
104                         DEBUG_STR_APPEND(a, string_compose ("wrote MidiEvent to Buffer (time=%1, start=%2 offset=%3)", ev_time, start, offset));
105                         for (size_t i=0; i < ev_size; ++i) {
106                                 DEBUG_STR_APPEND(a,hex);
107                                 DEBUG_STR_APPEND(a,"0x");
108                                 DEBUG_STR_APPEND(a,(int)write_loc[i]);
109                                 DEBUG_STR_APPEND(a,' ');
110                         }
111                         DEBUG_STR_APPEND(a,'\n');
112                         DEBUG_TRACE (DEBUG::MidiDiskstreamIO, DEBUG_STR(a).str());
113                 }
114 #endif
115
116                 if (success) {
117                         _tracker.track(write_loc);
118                         ++count;
119                 } else {
120                         cerr << "WARNING: error reading event contents from MIDI ring" << endl;
121                 }
122         }
123
124         return count;
125 }
126
127 template<typename T>
128 void
129 MidiRingBuffer<T>::flush (framepos_t /*start*/, framepos_t end)
130 {
131         const size_t prefix_size = sizeof(T) + sizeof(Evoral::EventType) + sizeof(uint32_t);
132
133         while (this->read_space() >= prefix_size) {
134                 uint8_t  peekbuf[prefix_size];
135                 bool     success;
136                 uint32_t ev_size;
137                 T        ev_time;
138
139                 success = this->peek (peekbuf, prefix_size);
140                 /* this cannot fail, because we've already verified that there
141                    is prefix_space to read
142                 */
143                 assert (success);
144
145                 ev_time = *(reinterpret_cast<T*>((uintptr_t)peekbuf));
146                 
147                 if (ev_time >= end) {
148                         break;
149                 }
150
151                 ev_size = *(reinterpret_cast<uint32_t*>((uintptr_t)(peekbuf + sizeof(T) + sizeof (Evoral::EventType))));
152                 this->increment_read_ptr (prefix_size);
153                 this->increment_read_ptr (ev_size);
154         }
155 }
156
157 template<typename T>
158 void
159 MidiRingBuffer<T>::dump(ostream& str)
160 {
161         size_t rspace;
162
163         if ((rspace = this->read_space()) == 0) {
164                 str << "MRB::dump: empty\n";
165                 return;
166         }
167
168         T                 ev_time;
169         Evoral::EventType ev_type;
170         uint32_t          ev_size;
171
172         RingBufferNPT<uint8_t>::rw_vector vec;
173         RingBufferNPT<uint8_t>::get_read_vector (&vec);
174
175         if (vec.len[0] == 0) {
176                 return;
177         }
178
179         str << this << ": Dump size = " << vec.len[0] + vec.len[1]
180             << " r@ " << RingBufferNPT<uint8_t>::get_read_ptr()
181             << " w@" << RingBufferNPT<uint8_t>::get_write_ptr() << endl;
182
183
184         uint8_t *buf = new uint8_t[vec.len[0] + vec.len[1]];
185         memcpy (buf, vec.buf[0], vec.len[0]);
186
187         if (vec.len[1]) {
188                 memcpy (buf+vec.len[1], vec.buf[1], vec.len[1]);
189         }
190
191         uint8_t* data = buf;
192         const uint8_t* end = buf + vec.len[0] + vec.len[1];
193
194         while (data < end) {
195
196                 memcpy (&ev_time, data, sizeof (T));
197                 data += sizeof (T);
198                 str << "\ttime " << ev_time;
199
200                 if (data >= end) {
201                         str << "(incomplete)\n ";
202                         break;
203                 }
204
205                 memcpy (&ev_type, data, sizeof (ev_type));
206                 data += sizeof (ev_type);
207                 str << " type " << ev_type;
208
209                 if (data >= end) {
210                         str << "(incomplete)\n";
211                         break;
212                 }
213
214                 memcpy (&ev_size, data, sizeof (ev_size));
215                 data += sizeof (ev_size);
216                 str << " size " << ev_size;
217
218                 if (data >= end) {
219                         str << "(incomplete)\n";
220                         break;
221                 }
222
223                 for (uint32_t i = 0; i != ev_size && data < end; ++i) {
224                         str << ' ' << hex << (int) data[i] << dec;
225                 }
226
227                 data += ev_size;
228
229                 str << endl;
230         }
231
232         delete [] buf;
233 }
234
235 template<typename T>
236 void
237 MidiRingBuffer<T>::reset_tracker ()
238 {
239         _tracker.reset ();
240 }
241
242 template<typename T>
243 void
244 MidiRingBuffer<T>::loop_resolve (MidiBuffer& dst, framepos_t t)
245 {
246         _tracker.resolve_notes (dst, t);
247 }
248
249 template class MidiRingBuffer<framepos_t>;
250
251 }  // namespace ARDOUR