26namespace MidiBufferHelpers
28 inline int getEventTime (
const void*
const d)
noexcept
30 return readUnaligned<int32> (d);
33 inline uint16 getEventDataSize (
const void*
const d)
noexcept
35 return readUnaligned<uint16> (
static_cast<const char*
> (d) +
sizeof (int32));
38 inline uint16 getEventTotalSize (
const void*
const d)
noexcept
40 return (uint16) (getEventDataSize (d) +
sizeof (int32) +
sizeof (uint16));
43 static int findActualEventLength (
const uint8*
const data,
const int maxBytes)
noexcept
45 unsigned int byte = (
unsigned int) *data;
48 if (
byte == 0xf0 ||
byte == 0xf7)
50 const uint8* d = data + 1;
52 while (d < data + maxBytes)
56 size = (int) (d - data);
58 else if (
byte == 0xff)
68 size = jmin (maxBytes, n + 2 + bytesLeft);
71 else if (
byte >= 0x80)
79 static uint8* findEventAfter (uint8* d, uint8* endData,
const int samplePosition)
noexcept
81 while (d < endData && getEventTime (d) <= samplePosition)
82 d += getEventTotalSize (d);
102 addEvent (message, 0);
112 uint8*
const start = MidiBufferHelpers::findEventAfter (
data.
begin(),
data.
end(), startSample - 1);
113 uint8*
const end = MidiBufferHelpers::findEventAfter (start,
data.
end(), startSample + numSamples - 1);
125 const int numBytes = MidiBufferHelpers::findActualEventLength (
static_cast<const uint8*
> (newData), maxBytes);
129 const size_t newItemSize = (size_t) numBytes +
sizeof (int32) +
sizeof (uint16);
135 writeUnaligned<int32> (d, sampleNumber);
136 writeUnaligned<uint16> (d + 4,
static_cast<uint16
> (numBytes));
137 memcpy (d + 6, newData, (
size_t) numBytes);
142 const int startSample,
143 const int numSamples,
144 const int sampleDeltaToAdd)
149 const uint8* eventData;
150 int eventSize, position;
153 && (position < startSample + numSamples || numSamples < 0))
155 addEvent (eventData, eventSize, position + sampleDeltaToAdd);
162 const uint8*
const end =
data.
end();
164 for (
const uint8* d =
data.
begin(); d < end; ++n)
165 d += MidiBufferHelpers::getEventTotalSize (d);
180 const uint8*
const endData =
data.
end();
184 const uint8*
const nextOne = d + MidiBufferHelpers::getEventTotalSize (d);
186 if (nextOne >= endData)
187 return MidiBufferHelpers::getEventTime (d);
195 : buffer (b), data (b.data.begin())
204 const uint8*
const dataEnd = buffer.data.end();
206 while (
data < dataEnd && MidiBufferHelpers::getEventTime (
data) < samplePosition)
207 data += MidiBufferHelpers::getEventTotalSize (
data);
215 samplePosition = MidiBufferHelpers::getEventTime (
data);
216 const int itemSize = MidiBufferHelpers::getEventDataSize (
data);
218 midiData =
data +
sizeof (int32) +
sizeof (uint16);
219 data +=
sizeof (int32) +
sizeof (uint16) + (size_t) itemSize;
229 samplePosition = MidiBufferHelpers::getEventTime (
data);
230 const int itemSize = MidiBufferHelpers::getEventDataSize (
data);
231 result =
MidiMessage (
data +
sizeof (int32) +
sizeof (uint16), itemSize, samplePosition);
232 data +=
sizeof (int32) +
sizeof (uint16) + (size_t) itemSize;
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() const noexcept
Returns a pointer to the first 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.
ElementType * data() const noexcept
Returns a pointer to the first element in the array.
ElementType * end() const noexcept
Returns a pointer to the element which follows the last element in the array.
Used to iterate through the events in a MidiBuffer.
void setNextSamplePosition(int samplePosition) noexcept
Repositions the iterator so that the next event retrieved will be the first one whose sample position...
~Iterator() noexcept
Destructor.
Iterator(const MidiBuffer &) noexcept
Creates an Iterator for this MidiBuffer.
bool getNextEvent(MidiMessage &result, int &samplePosition) noexcept
Retrieves a copy of the next event from the buffer.
Holds a sequence of time-stamped midi events.
int getFirstEventTime() const noexcept
Returns the sample number of the first event in the buffer.
MidiBuffer & operator=(const MidiBuffer &) noexcept
Makes a copy of another MidiBuffer.
void addEvent(const MidiMessage &midiMessage, int sampleNumber)
Adds an event to 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.
MidiBuffer() noexcept
Creates an empty MidiBuffer.
int getNumEvents() const noexcept
Counts the number of events in the buffer.
bool isEmpty() const noexcept
Returns true if the buffer is empty.
void swapWith(MidiBuffer &) noexcept
Exchanges the contents of this buffer with another one.
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.
Encapsulates a MIDI message.
static int readVariableLengthVal(const uint8 *data, int &numBytesUsed) noexcept
Reads a midi variable-length integer.
const uint8 * getRawData() const noexcept
Returns a pointer to the raw midi data.
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...
int getRawDataSize() const noexcept
Returns the number of bytes of data in the message.