Zrythm v2.0.0-alpha.1+31.4967fd053471
a highly automated and intuitive digital audio workstation
Loading...
Searching...
No Matches
zrythm::dsp::MidiEventBuffer Class Reference

Packed contiguous buffer for RT MIDI events. More...

#include <src/dsp/midi_event_buffer.h>

Data Structures

class  Iterator
 Forward iterator over packed events. More...

Public Member Functions

bool push_back (units::sample_u32_t time, std::span< const midi_byte_t > event_data) noexcept
 Append an event.
void clear () noexcept
 Reset buffer.
void reserve (size_t bytes=kMaxReserveBytes)
 Pre-allocate internal byte storage (and scratch/index buffers).
void swap (MidiEventBuffer &other) noexcept
bool empty () const noexcept
size_t size () const noexcept
 Number of events (not bytes).
size_t size_in_bytes () const noexcept
 Bytes used in storage.
size_t capacity () const noexcept
 Bytes reserved in storage.
MidiEventView front () const noexcept
Iterator begin () const noexcept
Iterator end () const noexcept
template<typename Comp>
void sort (Comp &&comp) noexcept
 Sort events using the given comparator on MidiEventView.
template<typename Pred>
void remove_if (Pred &&pred) noexcept
 Remove all events matching the predicate.

Static Public Member Functions

static MidiEventBuffer make_reserved ()
 Create a pre-allocated buffer suitable for test use.

Static Public Attributes

static constexpr size_t kHeaderSize = sizeof (uint16_t) + sizeof (uint32_t)
 Bytes per event header: 2 (size) + 4 (timestamp).
static constexpr size_t kDefaultReserveBytes = 0x1000
 Default pre-allocation.
static constexpr size_t kMaxReserveBytes = 0x4000
 Max bytes to hold in queues.

Detailed Description

Packed contiguous buffer for RT MIDI events.

Events are stored as: [uint16_t size][uint32_t timestamp][size bytes data]. Size-first layout allows easy chunk splitting — read 2 bytes to skip an event, or splice byte ranges directly.

Zero per-event heap allocation. Pre-allocate with reserve(), clear each cycle (no dealloc). Iteration yields non-owning MidiEventView objects.

RT-safety: reserve() must be called before any RT use. All internal storage (primary, scratch, index) is pre-allocated at that point. push_back(), sort(), remove_if(), and clear() never allocate as long as the pre-allocated capacity is not exceeded (asserted in debug).

Not thread-safe — one writer (RT thread), readers must synchronize externally.

Definition at line 73 of file midi_event_buffer.h.

Member Function Documentation

◆ begin()

Iterator zrythm::dsp::MidiEventBuffer::begin ( ) const
inlinenoexcept

Definition at line 225 of file midi_event_buffer.h.

◆ capacity()

size_t zrythm::dsp::MidiEventBuffer::capacity ( ) const
inlinenoexcept

Bytes reserved in storage.

Definition at line 217 of file midi_event_buffer.h.

◆ clear()

void zrythm::dsp::MidiEventBuffer::clear ( )
inlinenoexcept

Reset buffer.

No deallocation — storage is reused next cycle.

Definition at line 188 of file midi_event_buffer.h.

◆ empty()

bool zrythm::dsp::MidiEventBuffer::empty ( ) const
inlinenoexcept

Definition at line 211 of file midi_event_buffer.h.

◆ end()

Iterator zrythm::dsp::MidiEventBuffer::end ( ) const
inlinenoexcept

Definition at line 230 of file midi_event_buffer.h.

◆ front()

MidiEventView zrythm::dsp::MidiEventBuffer::front ( ) const
inlinenoexcept

Definition at line 219 of file midi_event_buffer.h.

◆ make_reserved()

MidiEventBuffer zrythm::dsp::MidiEventBuffer::make_reserved ( )
inlinestaticnodiscard

Create a pre-allocated buffer suitable for test use.

The returned buffer has enough storage reserved for typical test scenarios, avoiding allocations during push_back.

Definition at line 91 of file midi_event_buffer.h.

◆ push_back()

bool zrythm::dsp::MidiEventBuffer::push_back ( units::sample_u32_t time,
std::span< const midi_byte_t > event_data )
inlinenoexcept

Append an event.

memcpy's data into the internal byte storage.

Returns
true if the event was appended, false if capacity was exceeded (event dropped).

Callers should reserve() enough space during prepare_for_processing to avoid drops. Critical callers (e.g. panic processor) should check the return value.

Definition at line 165 of file midi_event_buffer.h.

◆ remove_if()

template<typename Pred>
void zrythm::dsp::MidiEventBuffer::remove_if ( Pred && pred)
inlinenoexcept

Remove all events matching the predicate.

Unpacks and repacks.

Definition at line 293 of file midi_event_buffer.h.

◆ reserve()

void zrythm::dsp::MidiEventBuffer::reserve ( size_t bytes = kMaxReserveBytes)
inline

Pre-allocate internal byte storage (and scratch/index buffers).

Definition at line 195 of file midi_event_buffer.h.

◆ size()

size_t zrythm::dsp::MidiEventBuffer::size ( ) const
inlinenoexcept

Number of events (not bytes).

Definition at line 213 of file midi_event_buffer.h.

◆ size_in_bytes()

size_t zrythm::dsp::MidiEventBuffer::size_in_bytes ( ) const
inlinenoexcept

Bytes used in storage.

Definition at line 215 of file midi_event_buffer.h.

◆ sort()

template<typename Comp>
void zrythm::dsp::MidiEventBuffer::sort ( Comp && comp)
inlinenoexcept

Sort events using the given comparator on MidiEventView.

Unpacks, sorts, and repacks. Not cheap — call only when needed.

Definition at line 242 of file midi_event_buffer.h.

◆ swap()

void zrythm::dsp::MidiEventBuffer::swap ( MidiEventBuffer & other)
inlinenoexcept

Definition at line 203 of file midi_event_buffer.h.

Field Documentation

◆ kDefaultReserveBytes

size_t zrythm::dsp::MidiEventBuffer::kDefaultReserveBytes = 0x1000
staticconstexpr

Default pre-allocation.

Definition at line 80 of file midi_event_buffer.h.

◆ kHeaderSize

size_t zrythm::dsp::MidiEventBuffer::kHeaderSize = sizeof (uint16_t) + sizeof (uint32_t)
staticconstexpr

Bytes per event header: 2 (size) + 4 (timestamp).

Definition at line 77 of file midi_event_buffer.h.

◆ kMaxReserveBytes

size_t zrythm::dsp::MidiEventBuffer::kMaxReserveBytes = 0x4000
staticconstexpr

Max bytes to hold in queues.

Definition at line 83 of file midi_event_buffer.h.


The documentation for this class was generated from the following file: