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/EventSink.hpp"
26
#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
* Note: the uint8_t template argument to RingBufferNPT<> indicates "byte
38
* oriented data", not anything particular linked to MIDI or any other
39
* possible interpretation of uint8_t.
41
template<typename Time>
42
class EventRingBuffer : public PBD::RingBufferNPT<uint8_t>, public Evoral::EventSink<Time> {
45
/** @param capacity Ringbuffer capacity in bytes.
47
EventRingBuffer(size_t capacity) : PBD::RingBufferNPT<uint8_t>(capacity)
50
inline size_t capacity() const { return bufsize(); }
52
/** Peek at the ringbuffer (read w/o advancing read pointer).
53
* @return how much has been peeked (wraps around if read exceeds
54
* the end of the buffer):
56
* |===========--------------R=============================|
60
inline bool peek (uint8_t*, size_t size);
62
inline uint32_t write(Time time, EventType type, uint32_t size, const uint8_t* buf);
63
inline bool read (Time* time, EventType* type, uint32_t* size, uint8_t* buf);
66
template<typename Time>
68
EventRingBuffer<Time>::peek (uint8_t* buf, size_t size)
70
PBD::RingBufferNPT<uint8_t>::rw_vector vec;
72
get_read_vector (&vec);
74
if (vec.len[0] + vec.len[1] < size) {
79
memcpy (buf, vec.buf[0], min (vec.len[0], size));
82
if (vec.len[0] < size) {
84
memcpy (buf + vec.len[0], vec.buf[1], size - vec.len[0]);
91
template<typename Time>
93
EventRingBuffer<Time>::read(Time* time, EventType* type, uint32_t* size, uint8_t* buf)
95
if (PBD::RingBufferNPT<uint8_t>::read ((uint8_t*)time, sizeof (Time)) != sizeof (Time)) {
99
if (PBD::RingBufferNPT<uint8_t>::read ((uint8_t*)type, sizeof(EventType)) != sizeof (EventType)) {
103
if (PBD::RingBufferNPT<uint8_t>::read ((uint8_t*)size, sizeof(uint32_t)) != sizeof (uint32_t)) {
107
if (PBD::RingBufferNPT<uint8_t>::read (buf, *size) != *size) {
115
template<typename Time>
117
EventRingBuffer<Time>::write(Time time, EventType type, uint32_t size, const uint8_t* buf)
119
if (!buf || write_space() < (sizeof(Time) + sizeof(EventType) + sizeof(uint32_t) + size)) {
122
PBD::RingBufferNPT<uint8_t>::write ((uint8_t*)&time, sizeof(Time));
123
PBD::RingBufferNPT<uint8_t>::write ((uint8_t*)&type, sizeof(EventType));
124
PBD::RingBufferNPT<uint8_t>::write ((uint8_t*)&size, sizeof(uint32_t));
125
PBD::RingBufferNPT<uint8_t>::write (buf, size);
131
} // namespace Evoral
133
#endif // EVORAL_EVENT_RING_BUFFER_HPP