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