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