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>
30 * Read/Write realtime safe.
31 * Single-reader Single-writer thread safe.
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...
38 * Ignore this class, use MidiRingBuffer.
41 class MidiRingBufferBase {
44 /** @param size Size in bytes.
46 MidiRingBufferBase(size_t size)
51 assert(read_space() == 0);
52 assert(write_space() == size - 1);
55 virtual ~MidiRingBufferBase() {
59 /** Reset(empty) the ringbuffer.
63 g_atomic_int_set(&_write_ptr, 0);
64 g_atomic_int_set(&_read_ptr, 0);
67 size_t write_space() const {
69 const size_t w = g_atomic_int_get(&_write_ptr);
70 const size_t r = g_atomic_int_get(&_read_ptr);
73 return ((r - w + _size) % _size) - 1;
81 size_t read_space() const {
83 const size_t w = g_atomic_int_get(&_write_ptr);
84 const size_t r = g_atomic_int_get(&_read_ptr);
89 return (w - r + _size) % _size;
93 size_t capacity() const { return _size; }
95 size_t peek(size_t size, T* dst);
96 bool full_peek(size_t size, T* dst);
98 size_t read(size_t size, T* dst);
99 bool full_read(size_t size, T* dst);
101 void write(size_t size, const T* src);
104 mutable gint _write_ptr;
105 mutable gint _read_ptr;
107 size_t _size; ///< Size (capacity) in bytes
108 T* _buf; ///< size, event, size, event...
112 /** Peek at the ringbuffer (read w/o advancing read pointer).
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.
120 MidiRingBufferBase<T>::peek(size_t size, T* dst)
122 const size_t priv_read_ptr = g_atomic_int_get(&_read_ptr);
124 const size_t read_size = (priv_read_ptr + size < _size)
126 : _size - priv_read_ptr;
128 memcpy(dst, &_buf[priv_read_ptr], read_size);
136 MidiRingBufferBase<T>::full_peek(size_t size, T* dst)
138 if (read_space() < size)
141 const size_t read_size = peek(size, dst);
143 if (read_size < size)
144 peek(size - read_size, dst + read_size);
150 /** Read from the ringbuffer.
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.
158 MidiRingBufferBase<T>::read(size_t size, T* dst)
160 const size_t priv_read_ptr = g_atomic_int_get(&_read_ptr);
162 const size_t read_size = (priv_read_ptr + size < _size)
164 : _size - priv_read_ptr;
166 memcpy(dst, &_buf[priv_read_ptr], read_size);
168 g_atomic_int_set(&_read_ptr, (priv_read_ptr + read_size) % _size);
176 MidiRingBufferBase<T>::full_read(size_t size, T* dst)
178 if (read_space() < size)
181 const size_t read_size = read(size, dst);
183 if (read_size < size)
184 read(size - read_size, dst + read_size);
192 MidiRingBufferBase<T>::write(size_t size, const T* src)
194 const size_t priv_write_ptr = g_atomic_int_get(&_write_ptr);
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);
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);
210 /* ******************************************************************** */
213 /** A MIDI RingBuffer.
215 * This is timestamps and MIDI packed sequentially into a single buffer, similarly
216 * to LV2 MIDI. The buffer looks like this:
218 * [timestamp][size][size bytes of raw MIDI][timestamp][size][etc..]
220 class MidiRingBuffer : public MidiRingBufferBase<Byte> {
223 /** @param size Size in bytes.
225 MidiRingBuffer(size_t size)
226 : MidiRingBufferBase<Byte>(size)
229 size_t write(nframes_t time, size_t size, const Byte* buf);
230 bool read(nframes_t time, size_t* size, Byte* buf);
232 size_t read(MidiBuffer& dst, nframes_t start, nframes_t end);
237 MidiRingBuffer::read(nframes_t time, size_t* size, Byte* buf)
239 bool success = MidiRingBufferBase<Byte>::full_read(sizeof(nframes_t), (Byte*)time);
241 success = MidiRingBufferBase<Byte>::full_read(sizeof(size_t), (Byte*)size);
243 success = MidiRingBufferBase<Byte>::full_read(*size, buf);
250 MidiRingBuffer::write(nframes_t time, size_t size, const Byte* buf)
254 if (write_space() < (sizeof(nframes_t) + sizeof(size_t) + size)) {
257 MidiRingBufferBase<Byte>::write(sizeof(nframes_t), (Byte*)&time);
258 MidiRingBufferBase<Byte>::write(sizeof(size_t), (Byte*)&size);
259 MidiRingBufferBase<Byte>::write(size, buf);
266 MidiRingBuffer::read(MidiBuffer& dst, nframes_t start, nframes_t end)
268 if (read_space() == 0)
275 while (read_space() > sizeof(nframes_t) + sizeof(size_t)) {
277 full_peek(sizeof(nframes_t), (Byte*)&ev.time);
282 bool success = MidiRingBufferBase<Byte>::full_read(sizeof(nframes_t), (Byte*)&ev.time);
284 success = MidiRingBufferBase<Byte>::full_read(sizeof(size_t), (Byte*)&ev.size);
287 cerr << "MRB: READ ERROR (time/size)" << endl;
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);
296 cerr << "MRB: READ ERROR (data)" << endl;
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,
303 printf("MRB - SKIPPING - %#X %d %d with time %u\n",
304 ev.buffer[0], ev.buffer[1], ev.buffer[2], ev.time);
310 assert(ev.time <= end);
313 //printf("(R) read space: %zu\n", read_space());
319 } // namespace ARDOUR
321 #endif // __ardour_midi_ring_buffer_h__