26namespace MidiBufferHelpers
28 inline int getEventTime (
const void* d)
noexcept
33 inline uint16 getEventDataSize (
const void* d)
noexcept
38 inline uint16 getEventTotalSize (
const void* d)
noexcept
43 static int findActualEventLength (
const uint8* data,
int maxBytes)
noexcept
45 auto byte = (
unsigned int) *data;
47 if (
byte == 0xf0 ||
byte == 0xf7)
52 if (data[i++] == 0xf7)
75 while (d <
endData && getEventTime (d) <= samplePosition)
76 d += getEventTotalSize (d);
85 data +=
sizeof (
int32) +
sizeof (
uint16) +
size_t (MidiBufferHelpers::getEventDataSize (data));
99 MidiBufferHelpers::getEventDataSize (data),
100 MidiBufferHelpers::getEventTime (data) };
106 addEvent (message, 0);
116 auto start = MidiBufferHelpers::findEventAfter (
data.
begin(),
data.
end(), startSample - 1);
117 auto end = MidiBufferHelpers::findEventAfter (start,
data.
end(), startSample + numSamples - 1);
129 auto numBytes = MidiBufferHelpers::findActualEventLength (
static_cast<const uint8*
> (
newData),
maxBytes);
162 if (
metadata.samplePosition >= startSample + numSamples && numSamples >= 0)
175 d += MidiBufferHelpers::getEventTotalSize (d);
194 auto nextOne = d + MidiBufferHelpers::getEventTotalSize (d);
197 return MidiBufferHelpers::getEventTime (d);
207 return metadata.samplePosition >= samplePosition;
212JUCE_BEGIN_IGNORE_WARNINGS_GCC_LIKE (
"-Wdeprecated-declarations")
213JUCE_BEGIN_IGNORE_WARNINGS_MSVC (4996)
216 : buffer (b), iterator (b.data.
begin())
220void MidiBuffer::Iterator::setNextSamplePosition (
int samplePosition)
noexcept
225bool MidiBuffer::Iterator::getNextEvent (
const uint8*&
midiData,
int& numBytes,
int& samplePosition)
noexcept
227 if (iterator == buffer.cend())
233 samplePosition =
metadata.samplePosition;
237bool MidiBuffer::Iterator::getNextEvent (MidiMessage& result,
int& samplePosition)
noexcept
239 if (iterator == buffer.cend())
244 samplePosition =
metadata.samplePosition;
248JUCE_END_IGNORE_WARNINGS_MSVC
249JUCE_END_IGNORE_WARNINGS_GCC_LIKE
261 void runTest()
override
263 beginTest (
"Clear messages");
270 buffer.addEvent (message, 0);
271 buffer.addEvent (message, 10);
272 buffer.addEvent (message, 20);
273 buffer.addEvent (message, 30);
279 buffer.clear (10, 0);
280 expectEquals (buffer.getNumEvents(), 4);
285 buffer.clear (10, 1);
286 expectEquals (buffer.getNumEvents(), 3);
291 buffer.clear (10, 10);
292 expectEquals (buffer.getNumEvents(), 3);
297 buffer.clear (10, 20);
298 expectEquals (buffer.getNumEvents(), 2);
303 buffer.clear (10, 30);
304 expectEquals (buffer.getNumEvents(), 1);
309 buffer.clear (10, 300);
310 expectEquals (buffer.getNumEvents(), 1);
void ensureStorageAllocated(int minNumElements)
Increases the array's internal storage to hold a minimum number of elements.
void clearQuick()
Removes all elements from the array without freeing the array's allocated storage.
int size() const noexcept
Returns the current number of elements in the array.
void removeRange(int startIndex, int numberToRemove)
Removes a range of elements from the array.
ElementType * begin() noexcept
Returns a pointer to the first element in the array.
ElementType * end() noexcept
Returns a pointer to the element which follows the last element in the array.
void insertMultiple(int indexToInsertAt, ParameterType newElement, int numberOfTimesToInsertIt)
Inserts multiple copies of an element into the array at a given position.
An iterator to move over contiguous raw MIDI data, which Allows iterating over a MidiBuffer using C++...
MidiBufferIterator & operator++() noexcept
Make this iterator point to the next message in the buffer.
reference operator*() const noexcept
Return an instance of MidiMessageMetadata which describes the message to which the iterator is curren...
Holds a sequence of time-stamped midi events.
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 ensureSize(size_t minimumNumBytes)
Preallocates some memory for the buffer to use.
int getNumEvents() const noexcept
Counts the number of events in the buffer.
MidiBufferIterator findNextSamplePosition(int samplePosition) const noexcept
Get an iterator pointing to the first event with a timestamp greater-than or equal-to samplePosition.
bool isEmpty() const noexcept
Returns true if the buffer is empty.
void swapWith(MidiBuffer &) noexcept
Exchanges the contents of this buffer with another one.
bool addEvent(const MidiMessage &midiMessage, int sampleNumber)
Adds an event to the buffer.
MidiBuffer() noexcept=default
Creates an empty MidiBuffer.
void clear() noexcept
Removes all events from the buffer.
void addEvents(const MidiBuffer &otherBuffer, int startSample, int numSamples, int sampleDeltaToAdd)
Adds some events from another buffer to this one.
Array< uint8 > data
The raw data holding this buffer.
MidiBufferIterator end() const noexcept
Get a read-only iterator pointing one past the end of this buffer.
Encapsulates a MIDI message.
const uint8 * getRawData() const noexcept
Returns a pointer to the raw midi data.
static MidiMessage noteOn(int channel, int noteNumber, float velocity) noexcept
Creates a key-down message (using a floating-point velocity).
static int getMessageLengthFromFirstByte(uint8 firstByte) noexcept
Based on the first byte of a short midi message, this uses a lookup table to return the message lengt...
static VariableLengthValue readVariableLengthValue(const uint8 *data, int maxBytesToUse) noexcept
Reads a midi variable-length integer, with protection against buffer overflow.
int getRawDataSize() const noexcept
Returns the number of bytes of data in the message.
unsigned short uint16
A platform-independent 16-bit unsigned integer type.
constexpr Type jmin(Type a, Type b)
Returns the smaller of two values.
signed int int32
A platform-independent 32-bit signed integer type.
Type unalignedPointerCast(void *ptr) noexcept
Casts a pointer to another type via void*, which suppresses the cast-align warning which sometimes ar...
unsigned char uint8
A platform-independent 8-bit unsigned integer type.
RangedDirectoryIterator begin(const RangedDirectoryIterator &it)
Returns the iterator that was passed in.