Loading...
Searching...
No Matches
juce::MidiBuffer Class Reference

## Detailed Description

Holds a sequence of time-stamped midi events.

Analogous to the AudioBuffer, this holds a set of midi events with integer time-stamps. The buffer is kept sorted in order of the time-stamps.

If you're working with a sequence of midi events that may need to be manipulated or read/written to a midi file, then MidiMessageSequence is probably a more appropriate container. MidiBuffer is designed for lower-level streams of raw midi data.

See also
  MidiMessage

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

- juce_audio_basics/midi/juce_MidiBuffer.h

## Public Member Functions

| MidiBuffer () noexcept=default
| Creates an empty MidiBuffer.
| MidiBuffer (const MidiMessage &message) noexcept
| Creates a MidiBuffer containing a single midi message.
void | clear () noexcept
| Removes all events from the buffer.
void | clear (int start, int numSamples)
| Removes all events between two times from the buffer.
bool | isEmpty () const noexcept
| Returns true if the buffer is empty.
int | getNumEvents () const noexcept
| Counts the number of events in the buffer.
bool | addEvent (const MidiMessage &midiMessage, int sampleNumber)
| Adds an event to the buffer.
bool | addEvent (const void *rawMidiData, int maxBytesOfMidiData, int sampleNumber)
| Adds an event to the buffer from raw midi data.
void | addEvents (const MidiBuffer &otherBuffer, int startSample, int numSamples, int sampleDeltaToAdd)
| Adds some events from another buffer to this one.
int | getFirstEventTime () const noexcept
| Returns the sample number of the first event in the buffer.
int | getLastEventTime () const noexcept
| Returns the sample number of the last event in the buffer.
void | swapWith (MidiBuffer &) noexcept
| Exchanges the contents of this buffer with another one.
void | ensureSize (size_t minimumNumBytes)
| Preallocates some memory for the buffer to use.
MidiBufferIterator | begin () const noexcept
| Get a read-only iterator pointing to the beginning of this buffer.
MidiBufferIterator | end () const noexcept
| Get a read-only iterator pointing one past the end of this buffer.
MidiBufferIterator | cbegin () const noexcept
| Get a read-only iterator pointing to the beginning of this buffer.
MidiBufferIterator | cend () const noexcept
| Get a read-only iterator pointing one past the end of this buffer.
MidiBufferIterator | findNextSamplePosition (int samplePosition) const noexcept
| Get an iterator pointing to the first event with a timestamp greater-than or equal-to samplePosition.

## Public Attributes

Array< uint8 > | data
| The raw data holding this buffer.

## Constructors and Destructors

## MidiBuffer() [1/2]

juce::MidiBuffer::MidiBuffer | ( |  | ) |

default noexcept

Creates an empty MidiBuffer.

Referenced by addEvents(), findNextSamplePosition(), MidiBuffer(), and swapWith().

## MidiBuffer() [2/2]

juce::MidiBuffer::MidiBuffer | ( | const MidiMessage & | message | ) |

explicit noexcept

Creates a MidiBuffer containing a single midi message.

References MidiBuffer().

## Member Functions

## clear() [1/2]

void juce::MidiBuffer::clear | ( |  | ) |

noexcept

Removes all events from the buffer.

References clear().

Referenced by clear(), and clear().

## clear() [2/2]

void juce::MidiBuffer::clear | ( | int | start,
|  | int | numSamples )

Removes all events between two times from the buffer.

All events for which (start <= event position < start + numSamples) will be removed.

References clear().

## isEmpty()

bool juce::MidiBuffer::isEmpty | ( |  | ) | const

noexcept

Returns true if the buffer is empty.

To actually retrieve the events, use a MidiBufferIterator object

References isEmpty().

Referenced by isEmpty().

## getNumEvents()

int juce::MidiBuffer::getNumEvents | ( |  | ) | const

noexcept

Counts the number of events in the buffer.

This is actually quite a slow operation, as it has to iterate through all the events, so you might prefer to call isEmpty() if that's all you need to know.

References getNumEvents().

Referenced by getNumEvents().

## addEvent() [1/2]

bool juce::MidiBuffer::addEvent | ( | const MidiMessage & | midiMessage,
|  | int | sampleNumber )

Adds an event to the buffer.

The sample number will be used to determine the position of the event in the buffer, which is always kept sorted. The MidiMessage's timestamp is ignored.

If an event is added whose sample position is the same as one or more events already in the buffer, the new event will be placed after the existing ones.

To retrieve events, use a MidiBufferIterator object.

Returns true on success, or false on failure.

References addEvent().

Referenced by addEvent(), addEvent(), and juce::VSTMidiEventList::addEventsToMidiBuffer().

## addEvent() [2/2]

bool juce::MidiBuffer::addEvent | ( | const void * | rawMidiData,
|  | int | maxBytesOfMidiData,
|  | int | sampleNumber )

Adds an event to the buffer from raw midi data.

The sample number will be used to determine the position of the event in the buffer, which is always kept sorted.

If an event is added whose sample position is the same as one or more events already in the buffer, the new event will be placed after the existing ones.

The event data will be inspected to calculate the number of bytes in length that the midi event really takes up, so maxBytesOfMidiData may be longer than the data that actually gets stored. E.g. if you pass in a note-on and a length of 4 bytes, it'll actually only store 3 bytes. If the midi data is invalid, it might not add an event at all.

To retrieve events, use a MidiBufferIterator object.

Returns true on success, or false on failure.

References addEvent().

## addEvents()

void juce::MidiBuffer::addEvents | ( | const MidiBuffer & | otherBuffer,
|  | int | startSample,
|  | int | numSamples,
|  | int | sampleDeltaToAdd )

Adds some events from another buffer to this one.

Parameters

  otherBuffer | the buffer containing the events you want to add
  startSample | the lowest sample number in the source buffer for which events should be added. Any source events whose timestamp is less than this will be ignored
  numSamples | the valid range of samples from the source buffer for which events should be added - i.e. events in the source buffer whose timestamp is greater than or equal to (startSample + numSamples) will be ignored. If this value is less than 0, all events after startSample will be taken.
  sampleDeltaToAdd | a value which will be added to the source timestamps of the events that are added to this buffer

References addEvents(), and MidiBuffer().

Referenced by addEvents().

## getFirstEventTime()

int juce::MidiBuffer::getFirstEventTime | ( |  | ) | const

noexcept

Returns the sample number of the first event in the buffer.

If the buffer's empty, this will just return 0.

References getFirstEventTime().

Referenced by getFirstEventTime().

## getLastEventTime()

int juce::MidiBuffer::getLastEventTime | ( |  | ) | const

noexcept

Returns the sample number of the last event in the buffer.

If the buffer's empty, this will just return 0.

References getLastEventTime().

Referenced by getLastEventTime().

## swapWith()

void juce::MidiBuffer::swapWith | ( | MidiBuffer & |  | ) |

noexcept

Exchanges the contents of this buffer with another one.

This is a quick operation, because no memory allocating or copying is done, it just swaps the internal state of the two buffers.

References MidiBuffer(), and swapWith().

Referenced by swapWith().

## ensureSize()

void juce::MidiBuffer::ensureSize | ( | size_t | minimumNumBytes | ) |

Preallocates some memory for the buffer to use.

This helps to avoid needing to reallocate space when the buffer has messages added to it.

References ensureSize().

Referenced by ensureSize().

## begin()

MidiBufferIterator juce::MidiBuffer::begin | ( |  | ) | const

inline noexcept

Get a read-only iterator pointing to the beginning of this buffer.

References begin(), and cbegin().

Referenced by begin().

## end()

MidiBufferIterator juce::MidiBuffer::end | ( |  | ) | const

inline noexcept

Get a read-only iterator pointing one past the end of this buffer.

References cend().

## cbegin()

MidiBufferIterator juce::MidiBuffer::cbegin | ( |  | ) | const

inline noexcept

Get a read-only iterator pointing to the beginning of this buffer.

References data.

Referenced by begin().

## cend()

MidiBufferIterator juce::MidiBuffer::cend | ( |  | ) | const

inline noexcept

Get a read-only iterator pointing one past the end of this buffer.

References data.

Referenced by end().

## findNextSamplePosition()

MidiBufferIterator juce::MidiBuffer::findNextSamplePosition | ( | int | samplePosition | ) | const

noexcept

Get an iterator pointing to the first event with a timestamp greater-than or equal-to samplePosition.

References JUCE_API, and MidiBuffer().

## Member Data Documentation

## data

Array<uint8> juce::MidiBuffer::data

The raw data holding this buffer.

Obviously access to this data is provided at your own risk. Its internal format could change in future, so don't write code that relies on it!

Referenced by cbegin(), and cend().

### Purchase

Get JUCE

### Discover

What's New in JUCE Features

### Learn

Documentaion Tutorials Made with JUCE Resources

### Support

JUCE Forum Newsletter Archive

### About

Contact Us JUCE Legal JUCE Licensing FAQ

### Events

Audio Developer Conference
© Raw Material Software Limited