Fix MIDI region loading.
[ardour.git] / libs / ardour / ardour / midi_ring_buffer.h
1 /*
2     Copyright (C) 2006 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 #ifndef __ardour_midi_ring_buffer_h__
20 #define __ardour_midi_ring_buffer_h__
21
22 #include <algorithm>
23 #include <ardour/types.h>
24 #include <ardour/buffer.h>
25
26 namespace ARDOUR {
27
28
29 /** A RingBuffer.
30  * Read/Write realtime safe.
31  * Single-reader Single-writer thread safe.
32  *
33  * This is Raul::RingBuffer, lifted for MIDIRingBuffer to inherit from as it works
34  * a bit differently than PBD::Ringbuffer.  This could/should be replaced with
35  * the PBD ringbuffer to decrease code size, but this code is tested and known to
36  * work, so here it sits for now...
37  *
38  * Ignore this class, use MidiRingBuffer.
39  */
40 template <typename T>
41 class MidiRingBufferBase {
42 public:
43
44         /** @param size Size in bytes.
45          */
46         MidiRingBufferBase(size_t size)
47                 : _size(size)
48                 , _buf(new T[size])
49         {
50                 reset();
51                 assert(read_space() == 0);
52                 assert(write_space() == size - 1);
53         }
54         
55         virtual ~MidiRingBufferBase() {
56                 delete[] _buf;
57         }
58
59         /** Reset(empty) the ringbuffer.
60          * NOT thread safe.
61          */
62         void reset() {
63                 g_atomic_int_set(&_write_ptr, 0);
64                 g_atomic_int_set(&_read_ptr, 0);
65         }
66
67         size_t write_space() const {
68                 
69                 const size_t w = g_atomic_int_get(&_write_ptr);
70                 const size_t r = g_atomic_int_get(&_read_ptr);
71                 
72                 if (w > r) {
73                         return ((r - w + _size) % _size) - 1;
74                 } else if(w < r) {
75                         return (r - w) - 1;
76                 } else {
77                         return _size - 1;
78                 }
79         }
80         
81         size_t read_space() const {
82                 
83                 const size_t w = g_atomic_int_get(&_write_ptr);
84                 const size_t r = g_atomic_int_get(&_read_ptr);
85                 
86                 if (w > r) {
87                         return w - r;
88                 } else {
89                         return (w - r + _size) % _size;
90                 }
91         }
92
93         size_t capacity() const { return _size; }
94
95         size_t peek(size_t size, T* dst);
96         bool   full_peek(size_t size, T* dst);
97
98         size_t read(size_t size, T* dst);
99         bool   full_read(size_t size, T* dst);
100         
101         void   write(size_t size, const T* src);
102
103 protected:
104         mutable gint _write_ptr;
105         mutable gint _read_ptr;
106         
107         size_t _size; ///< Size (capacity) in bytes
108         T*  _buf;  ///< size, event, size, event...
109 };
110
111
112 /** Peek at the ringbuffer (read w/o advancing read pointer).
113  *
114  * Note that a full read may not be done if the data wraps around.
115  * Caller must check return value and call again if necessary, or use the 
116  * full_peek method which does this automatically.
117  */
118 template<typename T>
119 size_t
120 MidiRingBufferBase<T>::peek(size_t size, T* dst)
121 {
122         const size_t priv_read_ptr = g_atomic_int_get(&_read_ptr);
123
124         const size_t read_size = (priv_read_ptr + size < _size)
125                         ? size
126                         : _size - priv_read_ptr;
127         
128         memcpy(dst, &_buf[priv_read_ptr], read_size);
129         
130         return read_size;
131 }
132
133
134 template<typename T>
135 bool
136 MidiRingBufferBase<T>::full_peek(size_t size, T* dst)
137 {
138         if (read_space() < size)
139                 return false;
140
141         const size_t read_size = peek(size, dst);
142         
143         if (read_size < size)
144                 peek(size - read_size, dst + read_size);
145
146         return true;
147 }
148
149
150 /** Read from the ringbuffer.
151  *
152  * Note that a full read may not be done if the data wraps around.
153  * Caller must check return value and call again if necessary, or use the 
154  * full_read method which does this automatically.
155  */
156 template<typename T>
157 size_t
158 MidiRingBufferBase<T>::read(size_t size, T* dst)
159 {
160         const size_t priv_read_ptr = g_atomic_int_get(&_read_ptr);
161
162         const size_t read_size = (priv_read_ptr + size < _size)
163                         ? size
164                         : _size - priv_read_ptr;
165         
166         memcpy(dst, &_buf[priv_read_ptr], read_size);
167         
168         g_atomic_int_set(&_read_ptr, (priv_read_ptr + read_size) % _size);
169
170         return read_size;
171 }
172
173
174 template<typename T>
175 bool
176 MidiRingBufferBase<T>::full_read(size_t size, T* dst)
177 {
178         if (read_space() < size)
179                 return false;
180
181         const size_t read_size = read(size, dst);
182         
183         if (read_size < size)
184                 read(size - read_size, dst + read_size);
185
186         return true;
187 }
188
189
190 template<typename T>
191 inline void
192 MidiRingBufferBase<T>::write(size_t size, const T* src)
193 {
194         const size_t priv_write_ptr = g_atomic_int_get(&_write_ptr);
195         
196         if (priv_write_ptr + size <= _size) {
197                 memcpy(&_buf[priv_write_ptr], src, size);
198         g_atomic_int_set(&_write_ptr, (priv_write_ptr + size) % _size);
199         } else {
200                 const size_t this_size = _size - priv_write_ptr;
201                 assert(this_size < size);
202                 assert(priv_write_ptr + this_size <= _size);
203                 memcpy(&_buf[priv_write_ptr], src, this_size);
204                 memcpy(&_buf[0], src+this_size, size - this_size);
205         g_atomic_int_set(&_write_ptr, size - this_size);
206         }
207 }
208
209
210 /* ******************************************************************** */
211         
212
213 /** A MIDI RingBuffer.
214  *
215  * This is timestamps and MIDI packed sequentially into a single buffer, similarly
216  * to LV2 MIDI.  The buffer looks like this:
217  *
218  * [timestamp][size][size bytes of raw MIDI][timestamp][size][etc..]
219  */
220 class MidiRingBuffer : public MidiRingBufferBase<Byte> {
221 public:
222
223         /** @param size Size in bytes.
224          */
225         MidiRingBuffer(size_t size)
226                 : MidiRingBufferBase<Byte>(size)
227         {}
228
229         size_t write(nframes_t time, size_t size, const Byte* buf);
230         bool   read(nframes_t time, size_t* size, Byte* buf);
231
232         size_t read(MidiBuffer& dst, nframes_t start, nframes_t end);
233 };
234
235
236 inline bool
237 MidiRingBuffer::read(nframes_t time, size_t* size, Byte* buf)
238 {
239         bool success = MidiRingBufferBase<Byte>::full_read(sizeof(nframes_t), (Byte*)time);
240         if (success)
241                 success = MidiRingBufferBase<Byte>::full_read(sizeof(size_t), (Byte*)size);
242         if (success)
243                 success = MidiRingBufferBase<Byte>::full_read(*size, buf);
244
245         return success;
246 }
247
248
249 inline size_t
250 MidiRingBuffer::write(nframes_t time, size_t size, const Byte* buf)
251 {
252         assert(size > 0);
253
254         if (write_space() < (sizeof(nframes_t) + sizeof(size_t) + size)) {
255                 return 0;
256         } else {
257                 MidiRingBufferBase<Byte>::write(sizeof(nframes_t), (Byte*)&time);
258                 MidiRingBufferBase<Byte>::write(sizeof(size_t), (Byte*)&size);
259                 MidiRingBufferBase<Byte>::write(size, buf);
260                 return size;
261         }
262 }
263
264
265 inline size_t
266 MidiRingBuffer::read(MidiBuffer& dst, nframes_t start, nframes_t end)
267 {
268         if (read_space() == 0)
269                 return 0;
270
271         MidiEvent ev;
272
273         size_t count = 0;
274
275         while (read_space() > sizeof(nframes_t) + sizeof(size_t)) {
276         
277                 full_peek(sizeof(nframes_t), (Byte*)&ev.time);
278         
279                 if (ev.time > end)
280                         break;
281
282                 bool success = MidiRingBufferBase<Byte>::full_read(sizeof(nframes_t), (Byte*)&ev.time);
283                 if (success)
284                         success = MidiRingBufferBase<Byte>::full_read(sizeof(size_t), (Byte*)&ev.size);
285                 
286                 if (!success) {
287                         cerr << "MRB: READ ERROR (time/size)" << endl;
288                         continue;
289                 }
290
291                 if (ev.time >= start) {
292                         Byte* write_loc = dst.reserve(ev.time, ev.size);
293                         success = MidiRingBufferBase<Byte>::full_read(ev.size, write_loc);
294                 
295                         if (!success)
296                                 cerr << "MRB: READ ERROR (data)" << endl;
297                         
298                         //printf("MRB - read %#X %d %d with time %u at index %zu\n",
299                         //      ev.buffer[0], ev.buffer[1], ev.buffer[2], ev.time,
300                         //      priv_read_ptr);
301                         //
302                 } else {
303                         printf("MRB - SKIPPING - %#X %d %d with time %u\n",
304                                         ev.buffer[0], ev.buffer[1], ev.buffer[2], ev.time);
305                         break;
306                 }
307
308                 ++count;
309
310                 assert(ev.time <= end);
311         }
312         
313         //printf("(R) read space: %zu\n", read_space());
314
315         return count;
316 }
317
318
319 } // namespace ARDOUR
320
321 #endif // __ardour_midi_ring_buffer_h__
322