The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

230 lines
9.5KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. The code included in this file is provided under the terms of the ISC license
  8. http://www.isc.org/downloads/software-support-policy/isc-license. Permission
  9. To use, copy, modify, and/or distribute this software for any purpose with or
  10. without fee is hereby granted provided that the above copyright notice and
  11. this permission notice appear in all copies.
  12. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  13. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  14. DISCLAIMED.
  15. ==============================================================================
  16. */
  17. #pragma once
  18. //==============================================================================
  19. /**
  20. Holds a sequence of time-stamped midi events.
  21. Analogous to the AudioSampleBuffer, this holds a set of midi events with
  22. integer time-stamps. The buffer is kept sorted in order of the time-stamps.
  23. If you're working with a sequence of midi events that may need to be manipulated
  24. or read/written to a midi file, then MidiMessageSequence is probably a more
  25. appropriate container. MidiBuffer is designed for lower-level streams of raw
  26. midi data.
  27. @see MidiMessage
  28. */
  29. class JUCE_API MidiBuffer
  30. {
  31. public:
  32. //==============================================================================
  33. /** Creates an empty MidiBuffer. */
  34. MidiBuffer() noexcept;
  35. /** Creates a MidiBuffer containing a single midi message. */
  36. explicit MidiBuffer (const MidiMessage& message) noexcept;
  37. /** Creates a copy of another MidiBuffer. */
  38. MidiBuffer (const MidiBuffer&) noexcept;
  39. /** Makes a copy of another MidiBuffer. */
  40. MidiBuffer& operator= (const MidiBuffer&) noexcept;
  41. /** Destructor */
  42. ~MidiBuffer();
  43. //==============================================================================
  44. /** Removes all events from the buffer. */
  45. void clear() noexcept;
  46. /** Removes all events between two times from the buffer.
  47. All events for which (start <= event position < start + numSamples) will
  48. be removed.
  49. */
  50. void clear (int start, int numSamples);
  51. /** Returns true if the buffer is empty.
  52. To actually retrieve the events, use a MidiBuffer::Iterator object
  53. */
  54. bool isEmpty() const noexcept;
  55. /** Counts the number of events in the buffer.
  56. This is actually quite a slow operation, as it has to iterate through all
  57. the events, so you might prefer to call isEmpty() if that's all you need
  58. to know.
  59. */
  60. int getNumEvents() const noexcept;
  61. /** Adds an event to the buffer.
  62. The sample number will be used to determine the position of the event in
  63. the buffer, which is always kept sorted. The MidiMessage's timestamp is
  64. ignored.
  65. If an event is added whose sample position is the same as one or more events
  66. already in the buffer, the new event will be placed after the existing ones.
  67. To retrieve events, use a MidiBuffer::Iterator object
  68. */
  69. void addEvent (const MidiMessage& midiMessage, int sampleNumber);
  70. /** Adds an event to the buffer from raw midi data.
  71. The sample number will be used to determine the position of the event in
  72. the buffer, which is always kept sorted.
  73. If an event is added whose sample position is the same as one or more events
  74. already in the buffer, the new event will be placed after the existing ones.
  75. The event data will be inspected to calculate the number of bytes in length that
  76. the midi event really takes up, so maxBytesOfMidiData may be longer than the data
  77. that actually gets stored. E.g. if you pass in a note-on and a length of 4 bytes,
  78. it'll actually only store 3 bytes. If the midi data is invalid, it might not
  79. add an event at all.
  80. To retrieve events, use a MidiBuffer::Iterator object
  81. */
  82. void addEvent (const void* rawMidiData,
  83. int maxBytesOfMidiData,
  84. int sampleNumber);
  85. /** Adds some events from another buffer to this one.
  86. @param otherBuffer the buffer containing the events you want to add
  87. @param startSample the lowest sample number in the source buffer for which
  88. events should be added. Any source events whose timestamp is
  89. less than this will be ignored
  90. @param numSamples the valid range of samples from the source buffer for which
  91. events should be added - i.e. events in the source buffer whose
  92. timestamp is greater than or equal to (startSample + numSamples)
  93. will be ignored. If this value is less than 0, all events after
  94. startSample will be taken.
  95. @param sampleDeltaToAdd a value which will be added to the source timestamps of the events
  96. that are added to this buffer
  97. */
  98. void addEvents (const MidiBuffer& otherBuffer,
  99. int startSample,
  100. int numSamples,
  101. int sampleDeltaToAdd);
  102. /** Returns the sample number of the first event in the buffer.
  103. If the buffer's empty, this will just return 0.
  104. */
  105. int getFirstEventTime() const noexcept;
  106. /** Returns the sample number of the last event in the buffer.
  107. If the buffer's empty, this will just return 0.
  108. */
  109. int getLastEventTime() const noexcept;
  110. //==============================================================================
  111. /** Exchanges the contents of this buffer with another one.
  112. This is a quick operation, because no memory allocating or copying is done, it
  113. just swaps the internal state of the two buffers.
  114. */
  115. void swapWith (MidiBuffer&) noexcept;
  116. /** Preallocates some memory for the buffer to use.
  117. This helps to avoid needing to reallocate space when the buffer has messages
  118. added to it.
  119. */
  120. void ensureSize (size_t minimumNumBytes);
  121. //==============================================================================
  122. /**
  123. Used to iterate through the events in a MidiBuffer.
  124. Note that altering the buffer while an iterator is using it isn't a
  125. safe operation.
  126. @see MidiBuffer
  127. */
  128. class JUCE_API Iterator
  129. {
  130. public:
  131. //==============================================================================
  132. /** Creates an Iterator for this MidiBuffer. */
  133. Iterator (const MidiBuffer&) noexcept;
  134. /** Destructor. */
  135. ~Iterator() noexcept;
  136. //==============================================================================
  137. /** Repositions the iterator so that the next event retrieved will be the first
  138. one whose sample position is at greater than or equal to the given position.
  139. */
  140. void setNextSamplePosition (int samplePosition) noexcept;
  141. /** Retrieves a copy of the next event from the buffer.
  142. @param result on return, this will be the message. The MidiMessage's timestamp
  143. is set to the same value as samplePosition.
  144. @param samplePosition on return, this will be the position of the event, as a
  145. sample index in the buffer
  146. @returns true if an event was found, or false if the iterator has reached
  147. the end of the buffer
  148. */
  149. bool getNextEvent (MidiMessage& result,
  150. int& samplePosition) noexcept;
  151. /** Retrieves the next event from the buffer.
  152. @param midiData on return, this pointer will be set to a block of data containing
  153. the midi message. Note that to make it fast, this is a pointer
  154. directly into the MidiBuffer's internal data, so is only valid
  155. temporarily until the MidiBuffer is altered.
  156. @param numBytesOfMidiData on return, this is the number of bytes of data used by the
  157. midi message
  158. @param samplePosition on return, this will be the position of the event, as a
  159. sample index in the buffer
  160. @returns true if an event was found, or false if the iterator has reached
  161. the end of the buffer
  162. */
  163. bool getNextEvent (const uint8* &midiData,
  164. int& numBytesOfMidiData,
  165. int& samplePosition) noexcept;
  166. private:
  167. //==============================================================================
  168. const MidiBuffer& buffer;
  169. const uint8* data;
  170. JUCE_DECLARE_NON_COPYABLE (Iterator)
  171. };
  172. /** The raw data holding this buffer.
  173. Obviously access to this data is provided at your own risk. Its internal format could
  174. change in future, so don't write code that relies on it!
  175. */
  176. Array<uint8> data;
  177. private:
  178. JUCE_LEAK_DETECTOR (MidiBuffer)
  179. };