summaryrefslogtreecommitdiff
path: root/libs/ardour/ardour/midi_ring_buffer.h
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2008-09-22 16:28:02 +0000
committerDavid Robillard <d@drobilla.net>2008-09-22 16:28:02 +0000
commita2d2f738cb63dbf0fb89e0a00c424ce883fb7888 (patch)
tree8ff8b9067a8884566b023de2dabedc2b57b856ab /libs/ardour/ardour/midi_ring_buffer.h
parentff2d51ddd8288ec967efab2cb8192f43c893909e (diff)
Move event specific ringbuffer stuff to evoral.
Sane event type interface between evoral and libardour (no more shared magic numbers). Cleanup Evoral::Sequence iterator, fix bugs, probably introduce new ones. Move MIDI specific event functions to Evoral::MIDIEvent (is-a Evoral::Event). git-svn-id: svn://localhost/ardour2/branches/3.0@3785 d708f5d6-7413-0410-9779-e7cbd77b26cf
Diffstat (limited to 'libs/ardour/ardour/midi_ring_buffer.h')
-rw-r--r--libs/ardour/ardour/midi_ring_buffer.h347
1 files changed, 41 insertions, 306 deletions
diff --git a/libs/ardour/ardour/midi_ring_buffer.h b/libs/ardour/ardour/midi_ring_buffer.h
index db0c3029a8..ae82dc14ab 100644
--- a/libs/ardour/ardour/midi_ring_buffer.h
+++ b/libs/ardour/ardour/midi_ring_buffer.h
@@ -24,239 +24,30 @@
#include <ardour/types.h>
#include <ardour/buffer.h>
#include <evoral/EventSink.hpp>
+#include <evoral/EventRingBuffer.hpp>
namespace ARDOUR {
-/* FIXME: this is probably too much inlined code */
-
-
-/** A RingBuffer.
- * Read/Write realtime safe.
- * Single-reader Single-writer thread safe.
- *
- * This is Raul::RingBuffer, lifted for MIDIRingBuffer to inherit from as it works
- * a bit differently than PBD::Ringbuffer. This could/should be replaced with
- * the PBD ringbuffer to decrease code size, but this code is tested and known to
- * work, so here it sits for now...
- *
- * Ignore this class, use MidiRingBuffer.
- */
-template <typename T>
-class MidiRingBufferBase {
-public:
-
- /** @param size Size in bytes.
- */
- MidiRingBufferBase(size_t size)
- : _size(size)
- , _buf(new T[size])
- {
- reset();
- assert(read_space() == 0);
- assert(write_space() == size - 1);
- }
-
- virtual ~MidiRingBufferBase() {
- delete[] _buf;
- }
-
- /** Reset(empty) the ringbuffer.
- * NOT thread safe.
- */
- void reset() {
- g_atomic_int_set(&_write_ptr, 0);
- g_atomic_int_set(&_read_ptr, 0);
- }
-
- size_t write_space() const {
-
- const size_t w = g_atomic_int_get(&_write_ptr);
- const size_t r = g_atomic_int_get(&_read_ptr);
-
- if (w > r) {
- return ((r - w + _size) % _size) - 1;
- } else if (w < r) {
- return (r - w) - 1;
- } else {
- return _size - 1;
- }
- }
-
- size_t read_space() const {
-
- const size_t w = g_atomic_int_get(&_write_ptr);
- const size_t r = g_atomic_int_get(&_read_ptr);
-
- if (w > r) {
- return w - r;
- } else {
- return (w - r + _size) % _size;
- }
- }
-
- size_t capacity() const { return _size; }
-
- size_t peek(size_t size, T* dst);
- bool full_peek(size_t size, T* dst);
-
- size_t read(size_t size, T* dst);
- bool full_read(size_t size, T* dst);
-
- bool skip(size_t size);
-
- void write(size_t size, const T* src);
-
-protected:
- mutable int _write_ptr;
- mutable int _read_ptr;
-
- size_t _size; ///< Size (capacity) in bytes
- T* _buf; ///< size, event, size, event...
-};
-
-
-/** Peek at the ringbuffer (read w/o advancing read pointer).
- *
- * Note that a full read may not be done if the data wraps around.
- * Caller must check return value and call again if necessary, or use the
- * full_peek method which does this automatically.
- */
-template<typename T>
-size_t
-MidiRingBufferBase<T>::peek(size_t size, T* dst)
-{
- const size_t priv_read_ptr = g_atomic_int_get(&_read_ptr);
-
- const size_t read_size = (priv_read_ptr + size < _size)
- ? size
- : _size - priv_read_ptr;
-
- memcpy(dst, &_buf[priv_read_ptr], read_size);
-
- return read_size;
-}
-
-
-template<typename T>
-bool
-MidiRingBufferBase<T>::full_peek(size_t size, T* dst)
-{
- if (read_space() < size) {
- return false;
- }
-
- const size_t read_size = peek(size, dst);
-
- if (read_size < size) {
- peek(size - read_size, dst + read_size);
- }
-
- return true;
-}
-
-
-/** Read from the ringbuffer.
- *
- * Note that a full read may not be done if the data wraps around.
- * Caller must check return value and call again if necessary, or use the
- * full_read method which does this automatically.
- */
-template<typename T>
-size_t
-MidiRingBufferBase<T>::read(size_t size, T* dst)
-{
- const size_t priv_read_ptr = g_atomic_int_get(&_read_ptr);
-
- const size_t read_size = (priv_read_ptr + size < _size)
- ? size
- : _size - priv_read_ptr;
-
- memcpy(dst, &_buf[priv_read_ptr], read_size);
-
- g_atomic_int_set(&_read_ptr, (priv_read_ptr + read_size) % _size);
-
- return read_size;
-}
-
-
-template<typename T>
-bool
-MidiRingBufferBase<T>::full_read(size_t size, T* dst)
-{
- if (read_space() < size) {
- return false;
- }
-
- const size_t read_size = read(size, dst);
-
- if (read_size < size) {
- read(size - read_size, dst + read_size);
- }
-
- return true;
-}
-
-
-template<typename T>
-bool
-MidiRingBufferBase<T>::skip(size_t size)
-{
- if (read_space() < size) {
- std::cerr << "WARNING: Attempt to skip past end of MIDI ring buffer" << std::endl;
- return false;
- }
-
- const size_t priv_read_ptr = g_atomic_int_get(&_read_ptr);
- g_atomic_int_set(&_read_ptr, (priv_read_ptr + size) % _size);
-
- return true;
-}
-
-
-template<typename T>
-inline void
-MidiRingBufferBase<T>::write(size_t size, const T* src)
-{
- const size_t priv_write_ptr = g_atomic_int_get(&_write_ptr);
-
- if (priv_write_ptr + size <= _size) {
- memcpy(&_buf[priv_write_ptr], src, size);
- g_atomic_int_set(&_write_ptr, (priv_write_ptr + size) % _size);
- } else {
- const size_t this_size = _size - priv_write_ptr;
- assert(this_size < size);
- assert(priv_write_ptr + this_size <= _size);
- memcpy(&_buf[priv_write_ptr], src, this_size);
- memcpy(&_buf[0], src+this_size, size - this_size);
- g_atomic_int_set(&_write_ptr, size - this_size);
- }
-}
-
-
-/* ******************************************************************** */
-
-
-/** A MIDI RingBuffer.
+/** A RingBuffer for (MIDI) events.
*
- * This is timestamps and MIDI packed sequentially into a single buffer, similarly
- * to LV2 MIDI. The buffer looks like this:
+ * This is simply a wrapper around a raw ringbuffer which writes/reads events
+ * as flat placked blobs.
+ * The buffer looks like this:
*
- * [timestamp][size][size bytes of raw MIDI][timestamp][size][etc..]
+ * [timestamp][type][size][size bytes of raw MIDI][timestamp][type][size](etc...)
*/
-class MidiRingBuffer : public MidiRingBufferBase<uint8_t>, public Evoral::EventSink {
+class MidiRingBuffer : public Evoral::EventRingBuffer {
public:
/** @param size Size in bytes.
*/
MidiRingBuffer(size_t size)
- : MidiRingBufferBase<uint8_t>(size), _channel_mask(0x0000FFFF)
+ : Evoral::EventRingBuffer(size)
+ , _channel_mask(0x0000FFFF)
{}
- size_t write(double time, uint32_t size, const uint8_t* buf);
- bool read(double* time, uint32_t* size, uint8_t* buf);
-
- bool read_prefix(double* time, size_t* size);
- bool read_contents(size_t size, uint8_t* buf);
+ inline bool read_prefix(EventTime* time, EventType* type, uint32_t* size);
+ inline bool read_contents(uint32_t size, uint8_t* buf);
size_t read(MidiBuffer& dst, nframes_t start, nframes_t end, nframes_t offset=0);
@@ -292,32 +83,17 @@ private:
};
-inline bool
-MidiRingBuffer::read(double* time, uint32_t* size, uint8_t* buf)
-{
- bool success = MidiRingBufferBase<uint8_t>::full_read(sizeof(double), (uint8_t*)time);
-
- if (success) {
- success = MidiRingBufferBase<uint8_t>::full_read(sizeof(size_t), (uint8_t*)size);
- }
- if (success) {
- success = MidiRingBufferBase<uint8_t>::full_read(*size, buf);
- }
-
- return success;
-}
-
-
/** Read the time and size of an event. This call MUST be immediately proceeded
* by a call to read_contents (or the read pointer will be garabage).
*/
inline bool
-MidiRingBuffer::read_prefix(double* time, size_t* size)
+MidiRingBuffer::read_prefix(EventTime* time, EventType* type, uint32_t* size)
{
- bool success = MidiRingBufferBase<uint8_t>::full_read(sizeof(double), (uint8_t*)time);
- if (success) {
- success = MidiRingBufferBase<uint8_t>::full_read(sizeof(size_t), (uint8_t*)size);
- }
+ bool success = Evoral::EventRingBuffer::full_read(sizeof(EventTime), (uint8_t*)time);
+ if (success)
+ success = Evoral::EventRingBuffer::full_read(sizeof(EventType), (uint8_t*)type);
+ if (success)
+ success = Evoral::EventRingBuffer::full_read(sizeof(uint32_t), (uint8_t*)size);
return success;
}
@@ -327,51 +103,9 @@ MidiRingBuffer::read_prefix(double* time, size_t* size)
* by a call to read_prefix (or the returned even will be garabage).
*/
inline bool
-MidiRingBuffer::read_contents(size_t size, uint8_t* buf)
-{
- return MidiRingBufferBase<uint8_t>::full_read(size, buf);
-}
-
-
-inline size_t
-MidiRingBuffer::write(double time, uint32_t size, const uint8_t* buf)
+MidiRingBuffer::read_contents(uint32_t size, uint8_t* buf)
{
- /*fprintf(stderr, "MRB %p write (t = %f) ", this, time);
- for (size_t i = 0; i < size; ++i)
- fprintf(stderr, "%X", (char)buf[i]);
- fprintf(stderr, "\n");*/
-
- assert(size > 0);
-
- // Don't write event if it doesn't match channel filter
- if (is_channel_event(buf[0]) && get_channel_mode() == FilterChannels) {
- uint8_t channel = buf[0] & 0x0F;
- if ( !(get_channel_mask() & (1L << channel)) ) {
- return 0;
- }
- }
-
- if (write_space() < (sizeof(double) + sizeof(size_t) + size)) {
- return 0;
- } else {
- MidiRingBufferBase<uint8_t>::write(sizeof(double), (uint8_t*)&time);
- MidiRingBufferBase<uint8_t>::write(sizeof(size_t), (uint8_t*)&size);
- if (is_channel_event(buf[0]) && get_channel_mode() == ForceChannel) {
- assert(size == 2 || size == 3);
- uint8_t tmp_buf[3];
- // Force event to channel
- tmp_buf[0] = (buf[0] & 0xF0) | (get_channel_mask() & 0x0F);
- tmp_buf[1] = buf[1];
- if (size == 3) {
- tmp_buf[2] = buf[2];
- }
- MidiRingBufferBase<uint8_t>::write(size, tmp_buf);
- } else {
- MidiRingBufferBase<uint8_t>::write(size, buf);
- }
- return size;
- }
-
+ return Evoral::EventRingBuffer::full_read(size, buf);
}
@@ -383,31 +117,31 @@ MidiRingBuffer::write(double time, uint32_t size, const uint8_t* buf)
inline size_t
MidiRingBuffer::read(MidiBuffer& dst, nframes_t start, nframes_t end, nframes_t offset)
{
- if (read_space() == 0)
+ if (read_space() == 0) {
+ //std::cerr << "MRB: NO READ SPACE" << std::endl;
return 0;
+ }
- double ev_time;
- uint32_t ev_size;
+ EventTime ev_time;
+ EventType ev_type;
+ uint32_t ev_size;
size_t count = 0;
- //printf("---- MRB read %u .. %u + %u\n", start, end, offset);
+ //std::cerr << "MRB read " << start << " .. " << end << " + " << offset << std::endl;
- while (read_space() > sizeof(double) + sizeof(size_t)) {
+ while (read_space() > sizeof(EventTime) + sizeof(EventType) + sizeof(uint32_t)) {
- full_peek(sizeof(double), (uint8_t*)&ev_time);
+ full_peek(sizeof(EventTime), (uint8_t*)&ev_time);
if (ev_time > end) {
+ //std::cerr << "MRB: PAST END (" << ev_time << " : " << end << ")" << std::endl;
break;
}
- bool success = MidiRingBufferBase<uint8_t>::full_read(sizeof(double), (uint8_t*)&ev_time);
- if (success) {
- success = MidiRingBufferBase<uint8_t>::full_read(sizeof(size_t), (uint8_t*)&ev_size);
- }
-
+ bool success = read_prefix(&ev_time, &ev_type, &ev_size);
if (!success) {
- std::cerr << "MRB: READ ERROR (time/size)" << std::endl;
+ //std::cerr << "MRB: READ ERROR (time/type/size)" << std::endl;
continue;
}
@@ -419,6 +153,7 @@ MidiRingBuffer::read(MidiBuffer& dst, nframes_t start, nframes_t end, nframes_t
if (is_channel_event(status) && get_channel_mode() == FilterChannels) {
const uint8_t channel = status & 0x0F;
if ( !(get_channel_mask() & (1L << channel)) ) {
+ //std::cerr << "MRB skipping event due to channel mask" << std::endl;
skip(ev_size); // Advance read pointer to next event
continue;
}
@@ -426,36 +161,36 @@ MidiRingBuffer::read(MidiBuffer& dst, nframes_t start, nframes_t end, nframes_t
if (ev_time >= start) {
- /*std::cerr << "MRB " << this << " - Reading event, time = "
- << ev_time << " - " << start << " => " << ev_time - start
- << ", size = " << ev_size << std::endl;*/
+ //std::cerr << "MRB " << this << " - Reading event, time = "
+ // << ev_time << " - " << start << " => " << ev_time - start
+ // << ", size = " << ev_size << std::endl;
ev_time -= start;
uint8_t* write_loc = dst.reserve(ev_time, ev_size);
if (write_loc == NULL) {
- std::cerr << "MRB: Unable to reserve space in buffer, event skipped";
+ //std::cerr << "MRB: Unable to reserve space in buffer, event skipped";
continue;
}
- success = MidiRingBufferBase<uint8_t>::full_read(ev_size, write_loc);
+ success = Evoral::EventRingBuffer::full_read(ev_size, write_loc);
if (success) {
if (is_channel_event(status) && get_channel_mode() == ForceChannel) {
write_loc[0] = (write_loc[0] & 0xF0) | (get_channel_mask() & 0x0F);
}
++count;
- //printf("MRB - read event at time %lf\n", ev_time);
+ //std::cerr << "MRB - read event at time " << ev_time << std::endl;
} else {
- std::cerr << "MRB: READ ERROR (data)" << std::endl;
+ //std::cerr << "MRB: READ ERROR (data)" << std::endl;
}
} else {
- printf("MRB (start %u) - Skipping event at (too early) time %f\n", start, ev_time);
+ //std::cerr << "MRB (start " << start << ") - Skipping event at (too early) time " << ev_time << std::endl;
}
}
- //printf("(R) read space: %zu\n", read_space());
+ //std::cerr << "MTB read space: " << read_space() << std::endl;
return count;
}