1 /* This file is part of Evoral.
2 * Copyright (C) 2008 Dave Robillard <http://drobilla.net>
4 * Evoral is free software; you can redistribute it and/or modify it under the
5 * terms of the GNU General Public License as published by the Free Software
6 * Foundation; either version 2 of the License, or (at your option) any later
9 * Evoral is distributed in the hope that it will be useful, but WITHOUT ANY
10 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 #ifndef EVORAL_EVENT_RING_BUFFER_HPP
19 #define EVORAL_EVENT_RING_BUFFER_HPP
22 #include <evoral/RingBuffer.hpp>
23 #include <evoral/EventSink.hpp>
24 #include <evoral/types.hpp>
32 /** A RingBuffer of events (generic time-stamped binary "blobs").
34 * This packs a timestamp, size, and size bytes of data flat into the buffer.
35 * Useful for MIDI events, OSC messages, etc.
37 class EventRingBuffer : public Evoral::RingBuffer<uint8_t>, public Evoral::EventSink {
40 /** @param capacity Ringbuffer capacity in bytes.
42 EventRingBuffer(size_t capacity) : RingBuffer<uint8_t>(capacity)
45 size_t capacity() const { return _size; }
47 bool peek_time(EventTime* time);
49 uint32_t write(EventTime time, EventType type, uint32_t size, const uint8_t* buf);
50 bool read (EventTime* time, EventType* type, uint32_t* size, uint8_t* buf);
55 EventRingBuffer::peek_time(EventTime* time)
57 bool success = RingBuffer<uint8_t>::full_peek(sizeof(EventTime), (uint8_t*)time);
63 EventRingBuffer::read(EventTime* time, EventType* type, uint32_t* size, uint8_t* buf)
65 bool success = RingBuffer<uint8_t>::full_read(sizeof(EventTime), (uint8_t*)time);
67 success = RingBuffer<uint8_t>::full_read(sizeof(EventType), (uint8_t*)type);
69 success = RingBuffer<uint8_t>::full_read(sizeof(uint32_t), (uint8_t*)size);
71 success = RingBuffer<uint8_t>::full_read(*size, buf);
78 EventRingBuffer::write(EventTime time, EventType type, uint32_t size, const uint8_t* buf)
80 if (write_space() < (sizeof(EventTime) + sizeof(EventType) + sizeof(uint32_t) + size)) {
83 RingBuffer<uint8_t>::write(sizeof(EventTime), (uint8_t*)&time);
84 RingBuffer<uint8_t>::write(sizeof(EventType), (uint8_t*)&type);
85 RingBuffer<uint8_t>::write(sizeof(uint32_t), (uint8_t*)&size);
86 RingBuffer<uint8_t>::write(size, buf);
94 #endif // EVORAL_EVENT_RING_BUFFER_HPP