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>
26 #include <evoral/EventSink.hpp>
31 /* FIXME: this is probably too much inlined code */
35 * Read/Write realtime safe.
36 * Single-reader Single-writer thread safe.
38 * This is Raul::RingBuffer, lifted for MIDIRingBuffer to inherit from as it works
39 * a bit differently than PBD::Ringbuffer. This could/should be replaced with
40 * the PBD ringbuffer to decrease code size, but this code is tested and known to
41 * work, so here it sits for now...
43 * Ignore this class, use MidiRingBuffer.
46 class MidiRingBufferBase {
49 /** @param size Size in bytes.
51 MidiRingBufferBase(size_t size)
56 assert(read_space() == 0);
57 assert(write_space() == size - 1);
60 virtual ~MidiRingBufferBase() {
64 /** Reset(empty) the ringbuffer.
68 g_atomic_int_set(&_write_ptr, 0);
69 g_atomic_int_set(&_read_ptr, 0);
72 size_t write_space() const {
74 const size_t w = g_atomic_int_get(&_write_ptr);
75 const size_t r = g_atomic_int_get(&_read_ptr);
78 return ((r - w + _size) % _size) - 1;
86 size_t read_space() const {
88 const size_t w = g_atomic_int_get(&_write_ptr);
89 const size_t r = g_atomic_int_get(&_read_ptr);
94 return (w - r + _size) % _size;
98 size_t capacity() const { return _size; }
100 size_t peek(size_t size, T* dst);
101 bool full_peek(size_t size, T* dst);
103 size_t read(size_t size, T* dst);
104 bool full_read(size_t size, T* dst);
106 bool skip(size_t size);
108 void write(size_t size, const T* src);
111 mutable int _write_ptr;
112 mutable int _read_ptr;
114 size_t _size; ///< Size (capacity) in bytes
115 T* _buf; ///< size, event, size, event...
119 /** Peek at the ringbuffer (read w/o advancing read pointer).
121 * Note that a full read may not be done if the data wraps around.
122 * Caller must check return value and call again if necessary, or use the
123 * full_peek method which does this automatically.
127 MidiRingBufferBase<T>::peek(size_t size, T* dst)
129 const size_t priv_read_ptr = g_atomic_int_get(&_read_ptr);
131 const size_t read_size = (priv_read_ptr + size < _size)
133 : _size - priv_read_ptr;
135 memcpy(dst, &_buf[priv_read_ptr], read_size);
143 MidiRingBufferBase<T>::full_peek(size_t size, T* dst)
145 if (read_space() < size) {
149 const size_t read_size = peek(size, dst);
151 if (read_size < size) {
152 peek(size - read_size, dst + read_size);
159 /** Read from the ringbuffer.
161 * Note that a full read may not be done if the data wraps around.
162 * Caller must check return value and call again if necessary, or use the
163 * full_read method which does this automatically.
167 MidiRingBufferBase<T>::read(size_t size, T* dst)
169 const size_t priv_read_ptr = g_atomic_int_get(&_read_ptr);
171 const size_t read_size = (priv_read_ptr + size < _size)
173 : _size - priv_read_ptr;
175 memcpy(dst, &_buf[priv_read_ptr], read_size);
177 g_atomic_int_set(&_read_ptr, (priv_read_ptr + read_size) % _size);
185 MidiRingBufferBase<T>::full_read(size_t size, T* dst)
187 if (read_space() < size) {
191 const size_t read_size = read(size, dst);
193 if (read_size < size) {
194 read(size - read_size, dst + read_size);
203 MidiRingBufferBase<T>::skip(size_t size)
205 if (read_space() < size) {
206 std::cerr << "WARNING: Attempt to skip past end of MIDI ring buffer" << std::endl;
210 const size_t priv_read_ptr = g_atomic_int_get(&_read_ptr);
211 g_atomic_int_set(&_read_ptr, (priv_read_ptr + size) % _size);
219 MidiRingBufferBase<T>::write(size_t size, const T* src)
221 const size_t priv_write_ptr = g_atomic_int_get(&_write_ptr);
223 if (priv_write_ptr + size <= _size) {
224 memcpy(&_buf[priv_write_ptr], src, size);
225 g_atomic_int_set(&_write_ptr, (priv_write_ptr + size) % _size);
227 const size_t this_size = _size - priv_write_ptr;
228 assert(this_size < size);
229 assert(priv_write_ptr + this_size <= _size);
230 memcpy(&_buf[priv_write_ptr], src, this_size);
231 memcpy(&_buf[0], src+this_size, size - this_size);
232 g_atomic_int_set(&_write_ptr, size - this_size);
237 /* ******************************************************************** */
240 /** A MIDI RingBuffer.
242 * This is timestamps and MIDI packed sequentially into a single buffer, similarly
243 * to LV2 MIDI. The buffer looks like this:
245 * [timestamp][size][size bytes of raw MIDI][timestamp][size][etc..]
247 class MidiRingBuffer : public MidiRingBufferBase<uint8_t>, public Evoral::EventSink {
249 /** @param size Size in bytes.
251 MidiRingBuffer(size_t size)
252 : MidiRingBufferBase<uint8_t>(size), _channel_mask(0x0000FFFF)
255 size_t write(double time, uint32_t size, const uint8_t* buf);
256 bool read(double* time, uint32_t* size, uint8_t* buf);
258 bool read_prefix(double* time, size_t* size);
259 bool read_contents(size_t size, uint8_t* buf);
261 size_t read(MidiBuffer& dst, nframes_t start, nframes_t end, nframes_t offset=0);
263 /** Set the channel filtering mode.
264 * @param mask If mode is FilterChannels, each bit represents a midi channel:
265 * bit 0 = channel 0, bit 1 = channel 1 etc. the read and write methods will only
266 * process events whose channel bit is 1.
267 * If mode is ForceChannel, mask is simply a channel number which all events will
268 * be forced to while reading.
270 void set_channel_mode(ChannelMode mode, uint16_t mask) {
271 g_atomic_int_set(&_channel_mask, ((uint16_t)mode << 16) | mask);
274 ChannelMode get_channel_mode() const {
275 return static_cast<ChannelMode>((g_atomic_int_get(&_channel_mask) & 0xFFFF0000) >> 16);
278 uint16_t get_channel_mask() const {
279 return static_cast<ChannelMode>((g_atomic_int_get(&_channel_mask) & 0x0000FFFF));
283 inline bool is_channel_event(uint8_t event_type_byte) {
284 // mask out channel information
285 event_type_byte &= 0xF0;
286 // midi channel events range from 0x80 to 0xE0
287 return (0x80 <= event_type_byte) && (event_type_byte <= 0xE0);
291 volatile uint32_t _channel_mask; // 16 bits mode, 16 bits mask
296 MidiRingBuffer::read(double* time, uint32_t* size, uint8_t* buf)
298 bool success = MidiRingBufferBase<uint8_t>::full_read(sizeof(double), (uint8_t*)time);
301 success = MidiRingBufferBase<uint8_t>::full_read(sizeof(size_t), (uint8_t*)size);
304 success = MidiRingBufferBase<uint8_t>::full_read(*size, buf);
311 /** Read the time and size of an event. This call MUST be immediately proceeded
312 * by a call to read_contents (or the read pointer will be garabage).
315 MidiRingBuffer::read_prefix(double* time, size_t* size)
317 bool success = MidiRingBufferBase<uint8_t>::full_read(sizeof(double), (uint8_t*)time);
319 success = MidiRingBufferBase<uint8_t>::full_read(sizeof(size_t), (uint8_t*)size);
326 /** Read the contenst of an event. This call MUST be immediately preceeded
327 * by a call to read_prefix (or the returned even will be garabage).
330 MidiRingBuffer::read_contents(size_t size, uint8_t* buf)
332 return MidiRingBufferBase<uint8_t>::full_read(size, buf);
337 MidiRingBuffer::write(double time, uint32_t size, const uint8_t* buf)
339 /*fprintf(stderr, "MRB %p write (t = %f) ", this, time);
340 for (size_t i = 0; i < size; ++i)
341 fprintf(stderr, "%X", (char)buf[i]);
342 fprintf(stderr, "\n");*/
346 // Don't write event if it doesn't match channel filter
347 if (is_channel_event(buf[0]) && get_channel_mode() == FilterChannels) {
348 uint8_t channel = buf[0] & 0x0F;
349 if ( !(get_channel_mask() & (1L << channel)) ) {
354 if (write_space() < (sizeof(double) + sizeof(size_t) + size)) {
357 MidiRingBufferBase<uint8_t>::write(sizeof(double), (uint8_t*)&time);
358 MidiRingBufferBase<uint8_t>::write(sizeof(size_t), (uint8_t*)&size);
359 if (is_channel_event(buf[0]) && get_channel_mode() == ForceChannel) {
360 assert(size == 2 || size == 3);
362 // Force event to channel
363 tmp_buf[0] = (buf[0] & 0xF0) | (get_channel_mask() & 0x0F);
368 MidiRingBufferBase<uint8_t>::write(size, tmp_buf);
370 MidiRingBufferBase<uint8_t>::write(size, buf);
378 /** Read a block of MIDI events from buffer.
380 * Timestamps of events returned are relative to start (ie event with stamp 0
381 * occurred at start), with offset added.
384 MidiRingBuffer::read(MidiBuffer& dst, nframes_t start, nframes_t end, nframes_t offset)
386 if (read_space() == 0)
394 //printf("---- MRB read %u .. %u + %u\n", start, end, offset);
396 while (read_space() > sizeof(double) + sizeof(size_t)) {
398 full_peek(sizeof(double), (uint8_t*)&ev_time);
404 bool success = MidiRingBufferBase<uint8_t>::full_read(sizeof(double), (uint8_t*)&ev_time);
406 success = MidiRingBufferBase<uint8_t>::full_read(sizeof(size_t), (uint8_t*)&ev_size);
410 std::cerr << "MRB: READ ERROR (time/size)" << std::endl;
415 success = full_peek(sizeof(uint8_t), &status);
416 assert(success); // If this failed, buffer is corrupt, all hope is lost
418 // Ignore event if it doesn't match channel filter
419 if (is_channel_event(status) && get_channel_mode() == FilterChannels) {
420 const uint8_t channel = status & 0x0F;
421 if ( !(get_channel_mask() & (1L << channel)) ) {
422 skip(ev_size); // Advance read pointer to next event
427 if (ev_time >= start) {
429 /*std::cerr << "MRB " << this << " - Reading event, time = "
430 << ev_time << " - " << start << " => " << ev_time - start
431 << ", size = " << ev_size << std::endl;*/
435 uint8_t* write_loc = dst.reserve(ev_time, ev_size);
436 if (write_loc == NULL) {
437 std::cerr << "MRB: Unable to reserve space in buffer, event skipped";
441 success = MidiRingBufferBase<uint8_t>::full_read(ev_size, write_loc);
444 if (is_channel_event(status) && get_channel_mode() == ForceChannel) {
445 write_loc[0] = (write_loc[0] & 0xF0) | (get_channel_mask() & 0x0F);
448 //printf("MRB - read event at time %lf\n", ev_time);
450 std::cerr << "MRB: READ ERROR (data)" << std::endl;
454 printf("MRB (start %u) - Skipping event at (too early) time %f\n", start, ev_time);
458 //printf("(R) read space: %zu\n", read_space());
464 } // namespace ARDOUR
466 #endif // __ardour_midi_ring_buffer_h__