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.

238 lines
9.7KB

  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. namespace juce
  18. {
  19. //==============================================================================
  20. /**
  21. Holds a sequence of time-stamped midi events.
  22. Analogous to the AudioBuffer, this holds a set of midi events with
  23. integer time-stamps. The buffer is kept sorted in order of the time-stamps.
  24. If you're working with a sequence of midi events that may need to be manipulated
  25. or read/written to a midi file, then MidiMessageSequence is probably a more
  26. appropriate container. MidiBuffer is designed for lower-level streams of raw
  27. midi data.
  28. @see MidiMessage
  29. @tags{Audio}
  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&) noexcept;
  41. /** Makes a copy of another MidiBuffer. */
  42. MidiBuffer& operator= (const MidiBuffer&) 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&) 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 will produce
  127. undefined behaviour.
  128. @see MidiBuffer
  129. */
  130. class JUCE_API Iterator
  131. {
  132. public:
  133. //==============================================================================
  134. /** Creates an Iterator for this MidiBuffer. */
  135. Iterator (const MidiBuffer&) noexcept;
  136. /** Creates a copy of an iterator. */
  137. Iterator (const Iterator&) = default;
  138. // VS2013 requires this, even if it's unused.
  139. Iterator& operator= (const Iterator&) = delete;
  140. /** Destructor. */
  141. ~Iterator() noexcept;
  142. //==============================================================================
  143. /** Repositions the iterator so that the next event retrieved will be the first
  144. one whose sample position is at greater than or equal to the given position.
  145. */
  146. void setNextSamplePosition (int samplePosition) noexcept;
  147. /** Retrieves a copy of the next event from the buffer.
  148. @param result on return, this will be the message. The MidiMessage's timestamp
  149. is set to the same value as samplePosition.
  150. @param samplePosition on return, this will be the position of the event, as a
  151. sample index in the buffer
  152. @returns true if an event was found, or false if the iterator has reached
  153. the end of the buffer
  154. */
  155. bool getNextEvent (MidiMessage& result,
  156. int& samplePosition) noexcept;
  157. /** Retrieves the next event from the buffer.
  158. @param midiData on return, this pointer will be set to a block of data containing
  159. the midi message. Note that to make it fast, this is a pointer
  160. directly into the MidiBuffer's internal data, so is only valid
  161. temporarily until the MidiBuffer is altered.
  162. @param numBytesOfMidiData on return, this is the number of bytes of data used by the
  163. midi message
  164. @param samplePosition on return, this will be the position of the event, as a
  165. sample index in the buffer
  166. @returns true if an event was found, or false if the iterator has reached
  167. the end of the buffer
  168. */
  169. bool getNextEvent (const uint8* &midiData,
  170. int& numBytesOfMidiData,
  171. int& samplePosition) noexcept;
  172. private:
  173. //==============================================================================
  174. const MidiBuffer& buffer;
  175. const uint8* data;
  176. };
  177. /** The raw data holding this buffer.
  178. Obviously access to this data is provided at your own risk. Its internal format could
  179. change in future, so don't write code that relies on it!
  180. */
  181. Array<uint8> data;
  182. private:
  183. JUCE_LEAK_DETECTOR (MidiBuffer)
  184. };
  185. } // namespace juce