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.

241 lines
9.6KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. #ifndef JUCE_MIDIBUFFER_H_INCLUDED
  18. #define JUCE_MIDIBUFFER_H_INCLUDED
  19. #include "juce_MidiMessage.h"
  20. //==============================================================================
  21. /**
  22. Holds a sequence of time-stamped midi events.
  23. Analogous to the AudioSampleBuffer, this holds a set of midi events with
  24. integer time-stamps. The buffer is kept sorted in order of the time-stamps.
  25. If you're working with a sequence of midi events that may need to be manipulated
  26. or read/written to a midi file, then MidiMessageSequence is probably a more
  27. appropriate container. MidiBuffer is designed for lower-level streams of raw
  28. midi data.
  29. @see MidiMessage
  30. */
  31. class JUCE_API MidiBuffer
  32. {
  33. public:
  34. //==============================================================================
  35. /** Creates an empty MidiBuffer. */
  36. MidiBuffer() noexcept;
  37. /** Creates a MidiBuffer containing a single midi message. */
  38. explicit MidiBuffer (const MidiMessage& message) noexcept;
  39. /** Creates a copy of another MidiBuffer. */
  40. MidiBuffer (const MidiBuffer& other) noexcept;
  41. /** Makes a copy of another MidiBuffer. */
  42. MidiBuffer& operator= (const MidiBuffer& other) noexcept;
  43. /** Destructor */
  44. ~MidiBuffer();
  45. //==============================================================================
  46. /** Removes all events from the buffer. */
  47. void clear() noexcept;
  48. /** Removes all events between two times from the buffer.
  49. All events for which (start <= event position < start + numSamples) will
  50. be removed.
  51. */
  52. void clear (int start, int numSamples);
  53. /** Returns true if the buffer is empty.
  54. To actually retrieve the events, use a MidiBuffer::Iterator object
  55. */
  56. bool isEmpty() const noexcept;
  57. /** Counts the number of events in the buffer.
  58. This is actually quite a slow operation, as it has to iterate through all
  59. the events, so you might prefer to call isEmpty() if that's all you need
  60. to know.
  61. */
  62. int getNumEvents() const noexcept;
  63. /** Adds an event to the buffer.
  64. The sample number will be used to determine the position of the event in
  65. the buffer, which is always kept sorted. The MidiMessage's timestamp is
  66. ignored.
  67. If an event is added whose sample position is the same as one or more events
  68. already in the buffer, the new event will be placed after the existing ones.
  69. To retrieve events, use a MidiBuffer::Iterator object
  70. */
  71. void addEvent (const MidiMessage& midiMessage, int sampleNumber);
  72. /** Adds an event to the buffer from raw midi data.
  73. The sample number will be used to determine the position of the event in
  74. the buffer, which is always kept sorted.
  75. If an event is added whose sample position is the same as one or more events
  76. already in the buffer, the new event will be placed after the existing ones.
  77. The event data will be inspected to calculate the number of bytes in length that
  78. the midi event really takes up, so maxBytesOfMidiData may be longer than the data
  79. that actually gets stored. E.g. if you pass in a note-on and a length of 4 bytes,
  80. it'll actually only store 3 bytes. If the midi data is invalid, it might not
  81. add an event at all.
  82. To retrieve events, use a MidiBuffer::Iterator object
  83. */
  84. void addEvent (const void* rawMidiData,
  85. int maxBytesOfMidiData,
  86. int sampleNumber);
  87. /** Adds some events from another buffer to this one.
  88. @param otherBuffer the buffer containing the events you want to add
  89. @param startSample the lowest sample number in the source buffer for which
  90. events should be added. Any source events whose timestamp is
  91. less than this will be ignored
  92. @param numSamples the valid range of samples from the source buffer for which
  93. events should be added - i.e. events in the source buffer whose
  94. timestamp is greater than or equal to (startSample + numSamples)
  95. will be ignored. If this value is less than 0, all events after
  96. startSample will be taken.
  97. @param sampleDeltaToAdd a value which will be added to the source timestamps of the events
  98. that are added to this buffer
  99. */
  100. void addEvents (const MidiBuffer& otherBuffer,
  101. int startSample,
  102. int numSamples,
  103. int sampleDeltaToAdd);
  104. /** Returns the sample number of the first event in the buffer.
  105. If the buffer's empty, this will just return 0.
  106. */
  107. int getFirstEventTime() const noexcept;
  108. /** Returns the sample number of the last event in the buffer.
  109. If the buffer's empty, this will just return 0.
  110. */
  111. int getLastEventTime() const noexcept;
  112. //==============================================================================
  113. /** Exchanges the contents of this buffer with another one.
  114. This is a quick operation, because no memory allocating or copying is done, it
  115. just swaps the internal state of the two buffers.
  116. */
  117. void swapWith (MidiBuffer& other) noexcept;
  118. /** Preallocates some memory for the buffer to use.
  119. This helps to avoid needing to reallocate space when the buffer has messages
  120. added to it.
  121. */
  122. void ensureSize (size_t minimumNumBytes);
  123. //==============================================================================
  124. /**
  125. Used to iterate through the events in a MidiBuffer.
  126. Note that altering the buffer while an iterator is using it isn't a
  127. safe operation.
  128. @see MidiBuffer
  129. */
  130. class JUCE_API Iterator
  131. {
  132. public:
  133. //==============================================================================
  134. /** Creates an Iterator for this MidiBuffer. */
  135. Iterator (const MidiBuffer& buffer) noexcept;
  136. /** Destructor. */
  137. ~Iterator() noexcept;
  138. //==============================================================================
  139. /** Repositions the iterator so that the next event retrieved will be the first
  140. one whose sample position is at greater than or equal to the given position.
  141. */
  142. void setNextSamplePosition (int samplePosition) noexcept;
  143. /** Retrieves a copy of the next event from the buffer.
  144. @param result on return, this will be the message (the MidiMessage's timestamp
  145. is not set)
  146. @param samplePosition on return, this will be the position of the event
  147. @returns true if an event was found, or false if the iterator has reached
  148. the end of the buffer
  149. */
  150. bool getNextEvent (MidiMessage& result,
  151. int& samplePosition) noexcept;
  152. /** Retrieves the next event from the buffer.
  153. @param midiData on return, this pointer will be set to a block of data containing
  154. the midi message. Note that to make it fast, this is a pointer
  155. directly into the MidiBuffer's internal data, so is only valid
  156. temporarily until the MidiBuffer is altered.
  157. @param numBytesOfMidiData on return, this is the number of bytes of data used by the
  158. midi message
  159. @param samplePosition on return, this will be the position of the event
  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. private:
  173. //==============================================================================
  174. friend class MidiBuffer::Iterator;
  175. MemoryBlock data;
  176. int bytesUsed;
  177. uint8* getData() const noexcept;
  178. uint8* findEventAfter (uint8*, int samplePosition) const noexcept;
  179. JUCE_LEAK_DETECTOR (MidiBuffer)
  180. };
  181. #endif // JUCE_MIDIBUFFER_H_INCLUDED