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__
24 #include <ardour/types.h>
25 #include <ardour/buffer.h>
30 /* FIXME: this is probably too much inlined code */
34 * Read/Write realtime safe.
35 * Single-reader Single-writer thread safe.
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...
42 * Ignore this class, use MidiRingBuffer.
45 class MidiRingBufferBase {
48 /** @param size Size in bytes.
50 MidiRingBufferBase(size_t size)
55 assert(read_space() == 0);
56 assert(write_space() == size - 1);
59 virtual ~MidiRingBufferBase() {
63 /** Reset(empty) the ringbuffer.
67 g_atomic_int_set(&_write_ptr, 0);
68 g_atomic_int_set(&_read_ptr, 0);
71 size_t write_space() const {
73 const size_t w = g_atomic_int_get(&_write_ptr);
74 const size_t r = g_atomic_int_get(&_read_ptr);
77 return ((r - w + _size) % _size) - 1;
85 size_t read_space() const {
87 const size_t w = g_atomic_int_get(&_write_ptr);
88 const size_t r = g_atomic_int_get(&_read_ptr);
93 return (w - r + _size) % _size;
97 size_t capacity() const { return _size; }
99 size_t peek(size_t size, T* dst);
100 bool full_peek(size_t size, T* dst);
102 size_t read(size_t size, T* dst);
103 bool full_read(size_t size, T* dst);
105 void write(size_t size, const T* src);
108 mutable gint _write_ptr;
109 mutable gint _read_ptr;
111 size_t _size; ///< Size (capacity) in bytes
112 T* _buf; ///< size, event, size, event...
116 /** Peek at the ringbuffer (read w/o advancing read pointer).
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.
124 MidiRingBufferBase<T>::peek(size_t size, T* dst)
126 const size_t priv_read_ptr = g_atomic_int_get(&_read_ptr);
128 const size_t read_size = (priv_read_ptr + size < _size)
130 : _size - priv_read_ptr;
132 memcpy(dst, &_buf[priv_read_ptr], read_size);
140 MidiRingBufferBase<T>::full_peek(size_t size, T* dst)
142 if (read_space() < size)
145 const size_t read_size = peek(size, dst);
147 if (read_size < size)
148 peek(size - read_size, dst + read_size);
154 /** Read from the ringbuffer.
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.
162 MidiRingBufferBase<T>::read(size_t size, T* dst)
164 const size_t priv_read_ptr = g_atomic_int_get(&_read_ptr);
166 const size_t read_size = (priv_read_ptr + size < _size)
168 : _size - priv_read_ptr;
170 memcpy(dst, &_buf[priv_read_ptr], read_size);
172 g_atomic_int_set(&_read_ptr, (priv_read_ptr + read_size) % _size);
180 MidiRingBufferBase<T>::full_read(size_t size, T* dst)
182 if (read_space() < size)
185 const size_t read_size = read(size, dst);
187 if (read_size < size)
188 read(size - read_size, dst + read_size);
196 MidiRingBufferBase<T>::write(size_t size, const T* src)
198 const size_t priv_write_ptr = g_atomic_int_get(&_write_ptr);
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);
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);
214 /* ******************************************************************** */
217 /** A MIDI RingBuffer.
219 * This is timestamps and MIDI packed sequentially into a single buffer, similarly
220 * to LV2 MIDI. The buffer looks like this:
222 * [timestamp][size][size bytes of raw MIDI][timestamp][size][etc..]
224 class MidiRingBuffer : public MidiRingBufferBase<Byte> {
227 /** @param size Size in bytes.
229 MidiRingBuffer(size_t size)
230 : MidiRingBufferBase<Byte>(size), _channel_mask(0xFFFF), _force_channel(-1)
233 size_t write(double time, size_t size, const Byte* buf);
234 bool read(double* time, size_t* size, Byte* buf);
236 bool read_prefix(double* time, size_t* size);
237 bool read_contents(size_t size, Byte* buf);
239 size_t read(MidiBuffer& dst, nframes_t start, nframes_t end, nframes_t offset=0);
242 * @param channel_mask each bit in channel_mask represents a midi channel: bit 0 = channel 0,
243 * bit 1 = channel 1 etc. the read and write methods will only allow
244 * events to pass, whose channel bit is 1.
246 void set_channel_mask(uint16_t channel_mask) { g_atomic_int_set(&_channel_mask, channel_mask); }
247 uint16_t get_channel_mask() { return g_atomic_int_get(&_channel_mask); }
250 * @param channel if negative, forcing channels is deactivated and filtering channels
251 * is activated, if positive, the LSB of channel is the channel number
252 * of the channel all events are forced into and filtering is deactivated
254 void set_force_channel(int8_t channel) { g_atomic_int_set(&_force_channel, channel); }
255 int8_t get_force_channel() { return g_atomic_int_get(&_force_channel); }
258 inline bool is_channel_event(Byte event_type_byte) {
259 // mask out channel information
260 event_type_byte &= 0xF0;
261 // midi channel events range from 0x80 to 0xE0
262 return (0x80 <= event_type_byte) && (event_type_byte <= 0xE0);
266 volatile uint16_t _channel_mask;
267 volatile int8_t _force_channel;
272 MidiRingBuffer::read(double* time, size_t* size, Byte* buf)
274 bool success = MidiRingBufferBase<Byte>::full_read(sizeof(double), (Byte*)time);
276 success = MidiRingBufferBase<Byte>::full_read(sizeof(size_t), (Byte*)size);
278 success = MidiRingBufferBase<Byte>::full_read(*size, buf);
284 /** Read the time and size of an event. This call MUST be immediately proceeded
285 * by a call to read_contents (or the read pointer will be garabage).
288 MidiRingBuffer::read_prefix(double* time, size_t* size)
290 bool success = MidiRingBufferBase<Byte>::full_read(sizeof(double), (Byte*)time);
292 success = MidiRingBufferBase<Byte>::full_read(sizeof(size_t), (Byte*)size);
298 /** Read the contenst of an event. This call MUST be immediately preceeded
299 * by a call to read_prefix (or the returned even will be garabage).
302 MidiRingBuffer::read_contents(size_t size, Byte* buf)
304 return MidiRingBufferBase<Byte>::full_read(size, buf);
309 MidiRingBuffer::write(double time, size_t size, const Byte* buf)
311 //printf("MRB - write %#X %d %d with time %lf\n",
312 // buf[0], buf[1], buf[2], time);
316 if(is_channel_event(buf[0]) && (g_atomic_int_get(&_force_channel) < 0)) {
317 // filter events for channels
318 Byte channel_nr = buf[0] & 0x0F;
319 if( !(g_atomic_int_get(&_channel_mask) & (1L << channel_nr)) ) {
324 if (write_space() < (sizeof(double) + sizeof(size_t) + size)) {
327 MidiRingBufferBase<Byte>::write(sizeof(double), (Byte*)&time);
328 MidiRingBufferBase<Byte>::write(sizeof(size_t), (Byte*)&size);
329 if(is_channel_event(buf[0]) && (g_atomic_int_get(&_force_channel) >= 0)) {
332 //force event into channel
333 tmp_buf[0] = (buf[0] & 0xF0) | (g_atomic_int_get(&_force_channel) & 0x0F);
336 MidiRingBufferBase<Byte>::write(size, tmp_buf);
338 MidiRingBufferBase<Byte>::write(size, buf);
346 /** Read a block of MIDI events from buffer.
348 * Timestamps of events returned are relative to start (ie event with stamp 0
349 * occurred at start), with offset added.
352 MidiRingBuffer::read(MidiBuffer& dst, nframes_t start, nframes_t end, nframes_t offset)
354 if (read_space() == 0)
361 //printf("MRB - read %u .. %u + %u\n", start, end, offset);
363 while (read_space() > sizeof(double) + sizeof(size_t)) {
365 full_peek(sizeof(double), (Byte*)&ev.time());
370 bool success = MidiRingBufferBase<Byte>::full_read(sizeof(double), (Byte*)&ev.time());
372 success = MidiRingBufferBase<Byte>::full_read(sizeof(size_t), (Byte*)&ev.size());
376 std::cerr << "MRB: READ ERROR (time/size)" << std::endl;
380 Byte first_event_byte;
382 success = full_peek(sizeof(Byte), &first_event_byte);
385 // could this ever happen???
387 std::cerr << "MRB: PEEK ERROR (first event byte)" << std::endl;
391 // filter events for channels
392 // filtering is only active, if forcing channels is not active
393 if(is_channel_event(first_event_byte) && (g_atomic_int_get(&_force_channel) < 0)) {
394 Byte channel_nr = first_event_byte & 0x0F;
395 if( !(g_atomic_int_get(&_channel_mask) & (1L << channel_nr)) ) {
400 if (ev.time() >= start) {
402 // TODO: Right now there come MIDI Events with empty buffer
404 std::cerr << "MidiRingBuffer::read WARNING: Skipping MIDI Event with NULL buffer pointer "
405 << " and length " << int(ev.size()) << std::endl;
409 Byte* write_loc = dst.reserve(ev.time(), ev.size());
411 success = MidiRingBufferBase<Byte>::full_read(ev.size(), write_loc);
414 if(is_channel_event(first_event_byte) && (g_atomic_int_get(&_force_channel) >= 0)) {
415 write_loc[0] = (write_loc[0] & 0xF0) | (g_atomic_int_get(&_force_channel) & 0x0F);
418 //printf("MRB - read event at time %lf\n", ev.time());
420 std::cerr << "MRB: READ ERROR (data)" << std::endl;
424 printf("MRB - SKIPPING EVENT AT TIME %f\n", ev.time());
428 //printf("(R) read space: %zu\n", read_space());
434 } // namespace ARDOUR
436 #endif // __ardour_midi_ring_buffer_h__