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.

237 lines
9.6KB

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