Audio plugin host https://kx.studio/carla
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.

juce_MidiBuffer.h 9.7KB

11 years ago
11 years ago
11 years ago
10 years ago
10 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. //==============================================================================
  20. /**
  21. Holds a sequence of time-stamped midi events.
  22. Analogous to the AudioSampleBuffer, 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. */
  30. class JUCE_API MidiBuffer
  31. {
  32. public:
  33. //==============================================================================
  34. /** Creates an empty MidiBuffer. */
  35. MidiBuffer() noexcept;
  36. /** Creates a MidiBuffer containing a single midi message. */
  37. explicit MidiBuffer (const MidiMessage& message) noexcept;
  38. /** Creates a copy of another MidiBuffer. */
  39. MidiBuffer (const MidiBuffer&) noexcept;
  40. /** Makes a copy of another MidiBuffer. */
  41. MidiBuffer& operator= (const MidiBuffer&) noexcept;
  42. /** Destructor */
  43. ~MidiBuffer();
  44. //==============================================================================
  45. /** Removes all events from the buffer. */
  46. void clear() noexcept;
  47. /** Removes all events between two times from the buffer.
  48. All events for which (start <= event position < start + numSamples) will
  49. be removed.
  50. */
  51. void clear (int start, int numSamples);
  52. /** Returns true if the buffer is empty.
  53. To actually retrieve the events, use a MidiBuffer::Iterator object
  54. */
  55. bool isEmpty() const noexcept;
  56. /** Counts the number of events in the buffer.
  57. This is actually quite a slow operation, as it has to iterate through all
  58. the events, so you might prefer to call isEmpty() if that's all you need
  59. to know.
  60. */
  61. int getNumEvents() const noexcept;
  62. /** Adds an event to the buffer.
  63. The sample number will be used to determine the position of the event in
  64. the buffer, which is always kept sorted. The MidiMessage's timestamp is
  65. ignored.
  66. If an event is added whose sample position is the same as one or more events
  67. already in the buffer, the new event will be placed after the existing ones.
  68. To retrieve events, use a MidiBuffer::Iterator object
  69. */
  70. void addEvent (const MidiMessage& midiMessage, int sampleNumber);
  71. /** Adds an event to the buffer from raw midi data.
  72. The sample number will be used to determine the position of the event in
  73. the buffer, which is always kept sorted.
  74. If an event is added whose sample position is the same as one or more events
  75. already in the buffer, the new event will be placed after the existing ones.
  76. The event data will be inspected to calculate the number of bytes in length that
  77. the midi event really takes up, so maxBytesOfMidiData may be longer than the data
  78. that actually gets stored. E.g. if you pass in a note-on and a length of 4 bytes,
  79. it'll actually only store 3 bytes. If the midi data is invalid, it might not
  80. add an event at all.
  81. To retrieve events, use a MidiBuffer::Iterator object
  82. */
  83. void addEvent (const void* rawMidiData,
  84. int maxBytesOfMidiData,
  85. int sampleNumber);
  86. /** Adds some events from another buffer to this one.
  87. @param otherBuffer the buffer containing the events you want to add
  88. @param startSample the lowest sample number in the source buffer for which
  89. events should be added. Any source events whose timestamp is
  90. less than this will be ignored
  91. @param numSamples the valid range of samples from the source buffer for which
  92. events should be added - i.e. events in the source buffer whose
  93. timestamp is greater than or equal to (startSample + numSamples)
  94. will be ignored. If this value is less than 0, all events after
  95. startSample will be taken.
  96. @param sampleDeltaToAdd a value which will be added to the source timestamps of the events
  97. that are added to this buffer
  98. */
  99. void addEvents (const MidiBuffer& otherBuffer,
  100. int startSample,
  101. int numSamples,
  102. int sampleDeltaToAdd);
  103. /** Returns the sample number of the first event in the buffer.
  104. If the buffer's empty, this will just return 0.
  105. */
  106. int getFirstEventTime() const noexcept;
  107. /** Returns the sample number of the last event in the buffer.
  108. If the buffer's empty, this will just return 0.
  109. */
  110. int getLastEventTime() const noexcept;
  111. //==============================================================================
  112. /** Exchanges the contents of this buffer with another one.
  113. This is a quick operation, because no memory allocating or copying is done, it
  114. just swaps the internal state of the two buffers.
  115. */
  116. void swapWith (MidiBuffer&) noexcept;
  117. /** Preallocates some memory for the buffer to use.
  118. This helps to avoid needing to reallocate space when the buffer has messages
  119. added to it.
  120. */
  121. void ensureSize (size_t minimumNumBytes);
  122. //==============================================================================
  123. /**
  124. Used to iterate through the events in a MidiBuffer.
  125. Note that altering the buffer while an iterator is using it isn't a
  126. safe operation.
  127. @see MidiBuffer
  128. */
  129. class JUCE_API Iterator
  130. {
  131. public:
  132. //==============================================================================
  133. /** Creates an Iterator for this MidiBuffer. */
  134. Iterator (const MidiBuffer&) noexcept;
  135. /** Destructor. */
  136. ~Iterator() noexcept;
  137. //==============================================================================
  138. /** Repositions the iterator so that the next event retrieved will be the first
  139. one whose sample position is at greater than or equal to the given position.
  140. */
  141. void setNextSamplePosition (int samplePosition) noexcept;
  142. /** Retrieves a copy of the next event from the buffer.
  143. @param result on return, this will be the message. The MidiMessage's timestamp
  144. is set to the same value as samplePosition.
  145. @param samplePosition on return, this will be the position of the event, as a
  146. sample index in the buffer
  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, as a
  160. sample index in the buffer
  161. @returns true if an event was found, or false if the iterator has reached
  162. the end of the buffer
  163. */
  164. bool getNextEvent (const uint8* &midiData,
  165. int& numBytesOfMidiData,
  166. int& samplePosition) noexcept;
  167. private:
  168. //==============================================================================
  169. const MidiBuffer& buffer;
  170. const uint8* data;
  171. JUCE_DECLARE_NON_COPYABLE (Iterator)
  172. };
  173. /** The raw data holding this buffer.
  174. Obviously access to this data is provided at your own risk. Its internal format could
  175. change in future, so don't write code that relies on it!
  176. */
  177. Array<uint8> data;
  178. private:
  179. JUCE_LEAK_DETECTOR (MidiBuffer)
  180. };
  181. #endif // JUCE_MIDIBUFFER_H_INCLUDED