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
21 #include "evoral/RingBuffer.hpp"
22 #include "evoral/EventSink.hpp"
23 #include "evoral/types.hpp"
31 /** A RingBuffer of events (generic time-stamped binary "blobs").
33 * This packs a timestamp, size, and size bytes of data flat into the buffer.
34 * Useful for MIDI events, OSC messages, etc.
36 template<typename Time>
37 class EventRingBuffer : public Evoral::RingBuffer<uint8_t>, public Evoral::EventSink<Time> {
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(Time* time);
49 uint32_t write(Time time, EventType type, uint32_t size, const uint8_t* buf);
50 bool read (Time* time, EventType* type, uint32_t* size, uint8_t* buf);
54 template<typename Time>
56 EventRingBuffer<Time>::peek_time(Time* time)
58 bool success = RingBuffer<uint8_t>::full_peek(sizeof(Time), (uint8_t*)time);
63 template<typename Time>
65 EventRingBuffer<Time>::read(Time* time, EventType* type, uint32_t* size, uint8_t* buf)
67 bool success = RingBuffer<uint8_t>::full_read(sizeof(Time), (uint8_t*)time);
69 success = RingBuffer<uint8_t>::full_read(sizeof(EventType), (uint8_t*)type);
71 success = RingBuffer<uint8_t>::full_read(sizeof(uint32_t), (uint8_t*)size);
73 success = RingBuffer<uint8_t>::full_read(*size, buf);
79 template<typename Time>
81 EventRingBuffer<Time>::write(Time time, EventType type, uint32_t size, const uint8_t* buf)
83 if (write_space() < (sizeof(Time) + sizeof(EventType) + sizeof(uint32_t) + size)) {
86 RingBuffer<uint8_t>::write(sizeof(Time), (uint8_t*)&time);
87 RingBuffer<uint8_t>::write(sizeof(EventType), (uint8_t*)&type);
88 RingBuffer<uint8_t>::write(sizeof(uint32_t), (uint8_t*)&size);
89 RingBuffer<uint8_t>::write(size, buf);
97 #endif // EVORAL_EVENT_RING_BUFFER_HPP