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 bool skip(size_t size);
107 void write(size_t size, const T* src);
110 mutable int _write_ptr;
111 mutable int _read_ptr;
113 size_t _size; ///< Size (capacity) in bytes
114 T* _buf; ///< size, event, size, event...
118 /** Peek at the ringbuffer (read w/o advancing read pointer).
120 * Note that a full read may not be done if the data wraps around.
121 * Caller must check return value and call again if necessary, or use the
122 * full_peek method which does this automatically.
126 MidiRingBufferBase<T>::peek(size_t size, T* dst)
128 const size_t priv_read_ptr = g_atomic_int_get(&_read_ptr);
130 const size_t read_size = (priv_read_ptr + size < _size)
132 : _size - priv_read_ptr;
134 memcpy(dst, &_buf[priv_read_ptr], read_size);
142 MidiRingBufferBase<T>::full_peek(size_t size, T* dst)
144 if (read_space() < size) {
148 const size_t read_size = peek(size, dst);
150 if (read_size < size) {
151 peek(size - read_size, dst + read_size);
158 /** Read from the ringbuffer.
160 * Note that a full read may not be done if the data wraps around.
161 * Caller must check return value and call again if necessary, or use the
162 * full_read method which does this automatically.
166 MidiRingBufferBase<T>::read(size_t size, T* dst)
168 const size_t priv_read_ptr = g_atomic_int_get(&_read_ptr);
170 const size_t read_size = (priv_read_ptr + size < _size)
172 : _size - priv_read_ptr;
174 memcpy(dst, &_buf[priv_read_ptr], read_size);
176 g_atomic_int_set(&_read_ptr, (priv_read_ptr + read_size) % _size);
184 MidiRingBufferBase<T>::full_read(size_t size, T* dst)
186 if (read_space() < size) {
190 const size_t read_size = read(size, dst);
192 if (read_size < size) {
193 read(size - read_size, dst + read_size);
202 MidiRingBufferBase<T>::skip(size_t size)
204 if (read_space() < size) {
205 std::cerr << "WARNING: Attempt to skip past end of MIDI ring buffer" << std::endl;
209 const size_t priv_read_ptr = g_atomic_int_get(&_read_ptr);
210 g_atomic_int_set(&_read_ptr, (priv_read_ptr + size) % _size);
218 MidiRingBufferBase<T>::write(size_t size, const T* src)
220 const size_t priv_write_ptr = g_atomic_int_get(&_write_ptr);
222 if (priv_write_ptr + size <= _size) {
223 memcpy(&_buf[priv_write_ptr], src, size);
224 g_atomic_int_set(&_write_ptr, (priv_write_ptr + size) % _size);
226 const size_t this_size = _size - priv_write_ptr;
227 assert(this_size < size);
228 assert(priv_write_ptr + this_size <= _size);
229 memcpy(&_buf[priv_write_ptr], src, this_size);
230 memcpy(&_buf[0], src+this_size, size - this_size);
231 g_atomic_int_set(&_write_ptr, size - this_size);
236 /* ******************************************************************** */
239 /** A MIDI RingBuffer.
241 * This is timestamps and MIDI packed sequentially into a single buffer, similarly
242 * to LV2 MIDI. The buffer looks like this:
244 * [timestamp][size][size bytes of raw MIDI][timestamp][size][etc..]
246 class MidiRingBuffer : public MidiRingBufferBase<uint8_t> {
248 /** @param size Size in bytes.
250 MidiRingBuffer(size_t size)
251 : MidiRingBufferBase<uint8_t>(size), _channel_mask(0x0000FFFF)
254 size_t write(double time, size_t size, const uint8_t* buf);
255 bool read(double* time, size_t* size, uint8_t* buf);
257 bool read_prefix(double* time, size_t* size);
258 bool read_contents(size_t size, uint8_t* buf);
260 size_t read(MidiBuffer& dst, nframes_t start, nframes_t end, nframes_t offset=0);
262 /** Set the channel filtering mode.
263 * @param mask If mode is FilterChannels, each bit represents a midi channel:
264 * bit 0 = channel 0, bit 1 = channel 1 etc. the read and write methods will only
265 * process events whose channel bit is 1.
266 * If mode is ForceChannel, mask is simply a channel number which all events will
267 * be forced to while reading.
269 void set_channel_mode(ChannelMode mode, uint16_t mask) {
270 g_atomic_int_set(&_channel_mask, ((uint16_t)mode << 16) | mask);
273 ChannelMode get_channel_mode() const {
274 return static_cast<ChannelMode>((g_atomic_int_get(&_channel_mask) & 0xFFFF0000) >> 16);
277 uint16_t get_channel_mask() const {
278 return static_cast<ChannelMode>((g_atomic_int_get(&_channel_mask) & 0x0000FFFF));
282 inline bool is_channel_event(uint8_t event_type_byte) {
283 // mask out channel information
284 event_type_byte &= 0xF0;
285 // midi channel events range from 0x80 to 0xE0
286 return (0x80 <= event_type_byte) && (event_type_byte <= 0xE0);
290 volatile uint32_t _channel_mask; // 16 bits mode, 16 bits mask
295 MidiRingBuffer::read(double* time, size_t* size, uint8_t* buf)
297 bool success = MidiRingBufferBase<uint8_t>::full_read(sizeof(double), (uint8_t*)time);
300 success = MidiRingBufferBase<uint8_t>::full_read(sizeof(size_t), (uint8_t*)size);
303 success = MidiRingBufferBase<uint8_t>::full_read(*size, buf);
310 /** Read the time and size of an event. This call MUST be immediately proceeded
311 * by a call to read_contents (or the read pointer will be garabage).
314 MidiRingBuffer::read_prefix(double* time, size_t* size)
316 bool success = MidiRingBufferBase<uint8_t>::full_read(sizeof(double), (uint8_t*)time);
318 success = MidiRingBufferBase<uint8_t>::full_read(sizeof(size_t), (uint8_t*)size);
325 /** Read the contenst of an event. This call MUST be immediately preceeded
326 * by a call to read_prefix (or the returned even will be garabage).
329 MidiRingBuffer::read_contents(size_t size, uint8_t* buf)
331 return MidiRingBufferBase<uint8_t>::full_read(size, buf);
336 MidiRingBuffer::write(double time, size_t size, const uint8_t* buf)
338 /*fprintf(stderr, "MRB %p write (t = %f) ", this, time);
339 for (size_t i = 0; i < size; ++i)
340 fprintf(stderr, "%X", (char)buf[i]);
341 fprintf(stderr, "\n");*/
345 // Don't write event if it doesn't match channel filter
346 if (is_channel_event(buf[0]) && get_channel_mode() == FilterChannels) {
347 uint8_t channel = buf[0] & 0x0F;
348 if ( !(get_channel_mask() & (1L << channel)) ) {
353 if (write_space() < (sizeof(double) + sizeof(size_t) + size)) {
356 MidiRingBufferBase<uint8_t>::write(sizeof(double), (uint8_t*)&time);
357 MidiRingBufferBase<uint8_t>::write(sizeof(size_t), (uint8_t*)&size);
358 if (is_channel_event(buf[0]) && get_channel_mode() == ForceChannel) {
359 assert(size == 2 || size == 3);
361 // Force event to channel
362 tmp_buf[0] = (buf[0] & 0xF0) | (get_channel_mask() & 0x0F);
367 MidiRingBufferBase<uint8_t>::write(size, tmp_buf);
369 MidiRingBufferBase<uint8_t>::write(size, buf);
377 /** Read a block of MIDI events from buffer.
379 * Timestamps of events returned are relative to start (ie event with stamp 0
380 * occurred at start), with offset added.
383 MidiRingBuffer::read(MidiBuffer& dst, nframes_t start, nframes_t end, nframes_t offset)
385 if (read_space() == 0)
393 //printf("---- MRB read %u .. %u + %u\n", start, end, offset);
395 while (read_space() > sizeof(double) + sizeof(size_t)) {
397 full_peek(sizeof(double), (uint8_t*)&ev_time);
403 bool success = MidiRingBufferBase<uint8_t>::full_read(sizeof(double), (uint8_t*)&ev_time);
405 success = MidiRingBufferBase<uint8_t>::full_read(sizeof(size_t), (uint8_t*)&ev_size);
409 std::cerr << "MRB: READ ERROR (time/size)" << std::endl;
414 success = full_peek(sizeof(uint8_t), &status);
415 assert(success); // If this failed, buffer is corrupt, all hope is lost
417 // Ignore event if it doesn't match channel filter
418 if (is_channel_event(status) && get_channel_mode() == FilterChannels) {
419 const uint8_t channel = status & 0x0F;
420 if ( !(get_channel_mask() & (1L << channel)) ) {
421 skip(ev_size); // Advance read pointer to next event
426 if (ev_time >= start) {
428 /*std::cerr << "MRB " << this << " - Reading event, time = "
429 << ev_time << " - " << start << " => " << ev_time - start
430 << ", size = " << ev_size << std::endl;*/
434 uint8_t* write_loc = dst.reserve(ev_time, ev_size);
435 if (write_loc == NULL) {
436 std::cerr << "MRB: Unable to reserve space in buffer, event skipped";
440 success = MidiRingBufferBase<uint8_t>::full_read(ev_size, write_loc);
443 if (is_channel_event(status) && get_channel_mode() == ForceChannel) {
444 write_loc[0] = (write_loc[0] & 0xF0) | (get_channel_mask() & 0x0F);
447 //printf("MRB - read event at time %lf\n", ev_time);
449 std::cerr << "MRB: READ ERROR (data)" << std::endl;
453 printf("MRB (start %u) - Skipping event at (too early) time %f\n", start, ev_time);
457 //printf("(R) read space: %zu\n", read_space());
463 } // namespace ARDOUR
465 #endif // __ardour_midi_ring_buffer_h__