1 /* This file is part of Evoral.
2 * Copyright (C) 2008 David 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
23 #include "pbd/ringbufferNPT.h"
25 #include "evoral/visibility.h"
26 #include "evoral/EventSink.hpp"
27 #include "evoral/types.hpp"
33 /** A RingBuffer of events (generic time-stamped binary "blobs").
35 * This packs a timestamp, size, and size bytes of data flat into the buffer.
36 * Useful for MIDI events, OSC messages, etc.
38 * Note: the uint8_t template argument to RingBufferNPT<> indicates "byte
39 * oriented data", not anything particular linked to MIDI or any other
40 * possible interpretation of uint8_t.
42 template<typename Time>
43 class /*LIBEVORAL_API*/ EventRingBuffer : public PBD::RingBufferNPT<uint8_t>, public Evoral::EventSink<Time> {
46 /** @param capacity Ringbuffer capacity in bytes.
48 EventRingBuffer(size_t capacity) : PBD::RingBufferNPT<uint8_t>(capacity)
51 inline size_t capacity() const { return bufsize(); }
53 /** Peek at the ringbuffer (read w/o advancing read pointer).
54 * @return how much has been peeked (wraps around if read exceeds
55 * the end of the buffer):
57 * |===========--------------R=============================|
61 inline bool peek (uint8_t*, size_t size);
63 inline uint32_t write(Time time, EventType type, uint32_t size, const uint8_t* buf);
64 inline bool read (Time* time, EventType* type, uint32_t* size, uint8_t* buf);
67 template<typename Time>
69 EventRingBuffer<Time>::peek (uint8_t* buf, size_t size)
71 PBD::RingBufferNPT<uint8_t>::rw_vector vec;
73 get_read_vector (&vec);
75 if (vec.len[0] + vec.len[1] < size) {
80 memcpy (buf, vec.buf[0], min (vec.len[0], size));
83 if (vec.len[0] < size) {
85 memcpy (buf + vec.len[0], vec.buf[1], size - vec.len[0]);
92 template<typename Time>
94 EventRingBuffer<Time>::read(Time* time, EventType* type, uint32_t* size, uint8_t* buf)
96 if (PBD::RingBufferNPT<uint8_t>::read ((uint8_t*)time, sizeof (Time)) != sizeof (Time)) {
100 if (PBD::RingBufferNPT<uint8_t>::read ((uint8_t*)type, sizeof(EventType)) != sizeof (EventType)) {
104 if (PBD::RingBufferNPT<uint8_t>::read ((uint8_t*)size, sizeof(uint32_t)) != sizeof (uint32_t)) {
108 if (PBD::RingBufferNPT<uint8_t>::read (buf, *size) != *size) {
116 template<typename Time>
118 EventRingBuffer<Time>::write(Time time, EventType type, uint32_t size, const uint8_t* buf)
120 if (!buf || write_space() < (sizeof(Time) + sizeof(EventType) + sizeof(uint32_t) + size)) {
123 PBD::RingBufferNPT<uint8_t>::write ((uint8_t*)&time, sizeof(Time));
124 PBD::RingBufferNPT<uint8_t>::write ((uint8_t*)&type, sizeof(EventType));
125 PBD::RingBufferNPT<uint8_t>::write ((uint8_t*)&size, sizeof(uint32_t));
126 PBD::RingBufferNPT<uint8_t>::write (buf, size);
132 } // namespace Evoral
134 #endif // EVORAL_EVENT_RING_BUFFER_HPP