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 10KB

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