2 Copyright (C) 2006 Paul Davis
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.
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.
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.
19 #ifndef __ardour_midi_ring_buffer_h__
20 #define __ardour_midi_ring_buffer_h__
23 #include <ardour/types.h>
24 #include <ardour/buffer.h>
29 /* FIXME: this is probably too much inlined code */
33 * Read/Write realtime safe.
34 * Single-reader Single-writer thread safe.
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...
41 * Ignore this class, use MidiRingBuffer.
44 class MidiRingBufferBase {
47 /** @param size Size in bytes.
49 MidiRingBufferBase(size_t size)
54 assert(read_space() == 0);
55 assert(write_space() == size - 1);
58 virtual ~MidiRingBufferBase() {
62 /** Reset(empty) the ringbuffer.
66 g_atomic_int_set(&_write_ptr, 0);
67 g_atomic_int_set(&_read_ptr, 0);
70 size_t write_space() const {
72 const size_t w = g_atomic_int_get(&_write_ptr);
73 const size_t r = g_atomic_int_get(&_read_ptr);
76 return ((r - w + _size) % _size) - 1;
84 size_t read_space() const {
86 const size_t w = g_atomic_int_get(&_write_ptr);
87 const size_t r = g_atomic_int_get(&_read_ptr);
92 return (w - r + _size) % _size;
96 size_t capacity() const { return _size; }
98 size_t peek(size_t size, T* dst);
99 bool full_peek(size_t size, T* dst);
101 size_t read(size_t size, T* dst);
102 bool full_read(size_t size, T* dst);
104 void write(size_t size, const T* src);
107 mutable gint _write_ptr;
108 mutable gint _read_ptr;
110 size_t _size; ///< Size (capacity) in bytes
111 T* _buf; ///< size, event, size, event...
115 /** Peek at the ringbuffer (read w/o advancing read pointer).
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.
123 MidiRingBufferBase<T>::peek(size_t size, T* dst)
125 const size_t priv_read_ptr = g_atomic_int_get(&_read_ptr);
127 const size_t read_size = (priv_read_ptr + size < _size)
129 : _size - priv_read_ptr;
131 memcpy(dst, &_buf[priv_read_ptr], read_size);
139 MidiRingBufferBase<T>::full_peek(size_t size, T* dst)
141 if (read_space() < size)
144 const size_t read_size = peek(size, dst);
146 if (read_size < size)
147 peek(size - read_size, dst + read_size);
153 /** Read from the ringbuffer.
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.
161 MidiRingBufferBase<T>::read(size_t size, T* dst)
163 const size_t priv_read_ptr = g_atomic_int_get(&_read_ptr);
165 const size_t read_size = (priv_read_ptr + size < _size)
167 : _size - priv_read_ptr;
169 memcpy(dst, &_buf[priv_read_ptr], read_size);
171 g_atomic_int_set(&_read_ptr, (priv_read_ptr + read_size) % _size);
179 MidiRingBufferBase<T>::full_read(size_t size, T* dst)
181 if (read_space() < size)
184 const size_t read_size = read(size, dst);
186 if (read_size < size)
187 read(size - read_size, dst + read_size);
195 MidiRingBufferBase<T>::write(size_t size, const T* src)
197 const size_t priv_write_ptr = g_atomic_int_get(&_write_ptr);
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);
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);
213 /* ******************************************************************** */
216 /** A MIDI RingBuffer.
218 * This is timestamps and MIDI packed sequentially into a single buffer, similarly
219 * to LV2 MIDI. The buffer looks like this:
221 * [timestamp][size][size bytes of raw MIDI][timestamp][size][etc..]
223 class MidiRingBuffer : public MidiRingBufferBase<Byte> {
226 /** @param size Size in bytes.
228 MidiRingBuffer(size_t size)
229 : MidiRingBufferBase<Byte>(size)
232 size_t write(double time, size_t size, const Byte* buf);
233 bool read(double* time, size_t* size, Byte* buf);
235 size_t read(MidiBuffer& dst, nframes_t start, nframes_t end, nframes_t offset=0);
240 MidiRingBuffer::read(double* time, size_t* size, Byte* buf)
242 bool success = MidiRingBufferBase<Byte>::full_read(sizeof(double), (Byte*)time);
244 success = MidiRingBufferBase<Byte>::full_read(sizeof(size_t), (Byte*)size);
246 success = MidiRingBufferBase<Byte>::full_read(*size, buf);
253 MidiRingBuffer::write(double time, size_t size, const Byte* buf)
257 if (write_space() < (sizeof(double) + sizeof(size_t) + size)) {
260 MidiRingBufferBase<Byte>::write(sizeof(double), (Byte*)&time);
261 MidiRingBufferBase<Byte>::write(sizeof(size_t), (Byte*)&size);
262 MidiRingBufferBase<Byte>::write(size, buf);
268 /** Read a block of MIDI events from buffer.
270 * Timestamps of events returned are relative to start (ie event with stamp 0
271 * occurred at start), with offset added.
274 MidiRingBuffer::read(MidiBuffer& dst, nframes_t start, nframes_t end, nframes_t offset)
276 if (read_space() == 0)
283 while (read_space() > sizeof(double) + sizeof(size_t)) {
285 full_peek(sizeof(double), (Byte*)&ev.time);
290 bool success = MidiRingBufferBase<Byte>::full_read(sizeof(double), (Byte*)&ev.time);
292 success = MidiRingBufferBase<Byte>::full_read(sizeof(size_t), (Byte*)&ev.size);
295 cerr << "MRB: READ ERROR (time/size)" << endl;
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);
304 cerr << "MRB: READ ERROR (data)" << endl;
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,
311 printf("MRB - SKIPPING EVENT (with time %f)\n", ev.time);
317 assert(ev.time <= end);
321 //printf("(R) read space: %zu\n", read_space());
327 } // namespace ARDOUR
329 #endif // __ardour_midi_ring_buffer_h__