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.

343 lines
14KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - Raw Material Software Limited
  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. A view of MIDI message data stored in a contiguous buffer.
  22. Instances of this class do *not* own the midi data bytes that they point to.
  23. Instead, they expect the midi data to live in a separate buffer that outlives
  24. the MidiMessageMetadata instance.
  25. */
  26. struct MidiMessageMetadata final
  27. {
  28. MidiMessageMetadata() noexcept = default;
  29. MidiMessageMetadata (const uint8* dataIn, int numBytesIn, int positionIn) noexcept
  30. : data (dataIn), numBytes (numBytesIn), samplePosition (positionIn)
  31. {
  32. }
  33. /** Constructs a new MidiMessage instance from the data that this object is viewing.
  34. Note that MidiMessage owns its data storage, whereas MidiMessageMetadata does not.
  35. */
  36. MidiMessage getMessage() const { return MidiMessage (data, numBytes, samplePosition); }
  37. /** Pointer to the first byte of a MIDI message. */
  38. const uint8* data = nullptr;
  39. /** The number of bytes in the MIDI message. */
  40. int numBytes = 0;
  41. /** The MIDI message's timestamp. */
  42. int samplePosition = 0;
  43. };
  44. //==============================================================================
  45. /**
  46. An iterator to move over contiguous raw MIDI data, which Allows iterating
  47. over a MidiBuffer using C++11 range-for syntax.
  48. In the following example, we log all three-byte messages in a midi buffer.
  49. @code
  50. void processBlock (AudioBuffer<float>&, MidiBuffer& midiBuffer) override
  51. {
  52. for (const MidiMessageMetadata metadata : midiBuffer)
  53. if (metadata.numBytes == 3)
  54. Logger::writeToLog (metadata.getMessage().getDescription());
  55. }
  56. @endcode
  57. */
  58. class JUCE_API MidiBufferIterator
  59. {
  60. using Ptr = const uint8*;
  61. public:
  62. MidiBufferIterator() = default;
  63. /** Constructs an iterator pointing at the message starting at the byte `dataIn`.
  64. `dataIn` must point to the start of a valid MIDI message. If it does not,
  65. calling other member functions on the iterator will result in undefined
  66. behaviour.
  67. */
  68. explicit MidiBufferIterator (const uint8* dataIn) noexcept
  69. : data (dataIn)
  70. {
  71. }
  72. using difference_type = std::iterator_traits<Ptr>::difference_type;
  73. using value_type = MidiMessageMetadata;
  74. using reference = MidiMessageMetadata;
  75. using pointer = void;
  76. using iterator_category = std::bidirectional_iterator_tag;
  77. /** Make this iterator point to the next message in the buffer. */
  78. MidiBufferIterator& operator++() noexcept;
  79. /** Create a copy of this object, make this iterator point to the next message in
  80. the buffer, then return the copy.
  81. */
  82. MidiBufferIterator operator++ (int) noexcept;
  83. /** Return true if this iterator points to the same message as another
  84. iterator instance, otherwise return false.
  85. */
  86. bool operator== (const MidiBufferIterator& other) const noexcept { return data == other.data; }
  87. /** Return false if this iterator points to the same message as another
  88. iterator instance, otherwise returns true.
  89. */
  90. bool operator!= (const MidiBufferIterator& other) const noexcept { return ! operator== (other); }
  91. /** Return an instance of MidiMessageMetadata which describes the message to which
  92. the iterator is currently pointing.
  93. */
  94. reference operator*() const noexcept;
  95. private:
  96. Ptr data = nullptr;
  97. };
  98. //==============================================================================
  99. /**
  100. Holds a sequence of time-stamped midi events.
  101. Analogous to the AudioBuffer, this holds a set of midi events with
  102. integer time-stamps. The buffer is kept sorted in order of the time-stamps.
  103. If you're working with a sequence of midi events that may need to be manipulated
  104. or read/written to a midi file, then MidiMessageSequence is probably a more
  105. appropriate container. MidiBuffer is designed for lower-level streams of raw
  106. midi data.
  107. @see MidiMessage
  108. @tags{Audio}
  109. */
  110. class JUCE_API MidiBuffer
  111. {
  112. public:
  113. //==============================================================================
  114. /** Creates an empty MidiBuffer. */
  115. MidiBuffer() noexcept = default;
  116. /** Creates a MidiBuffer containing a single midi message. */
  117. explicit MidiBuffer (const MidiMessage& message) noexcept;
  118. //==============================================================================
  119. /** Removes all events from the buffer. */
  120. void clear() noexcept;
  121. /** Removes all events between two times from the buffer.
  122. All events for which (start <= event position < start + numSamples) will
  123. be removed.
  124. */
  125. void clear (int start, int numSamples);
  126. /** Returns true if the buffer is empty.
  127. To actually retrieve the events, use a MidiBufferIterator object
  128. */
  129. bool isEmpty() const noexcept;
  130. /** Counts the number of events in the buffer.
  131. This is actually quite a slow operation, as it has to iterate through all
  132. the events, so you might prefer to call isEmpty() if that's all you need
  133. to know.
  134. */
  135. int getNumEvents() const noexcept;
  136. /** Adds an event to the buffer.
  137. The sample number will be used to determine the position of the event in
  138. the buffer, which is always kept sorted. The MidiMessage's timestamp is
  139. ignored.
  140. If an event is added whose sample position is the same as one or more events
  141. already in the buffer, the new event will be placed after the existing ones.
  142. To retrieve events, use a MidiBufferIterator object
  143. */
  144. void addEvent (const MidiMessage& midiMessage, int sampleNumber);
  145. /** Adds an event to the buffer from raw midi data.
  146. The sample number will be used to determine the position of the event in
  147. the buffer, which is always kept sorted.
  148. If an event is added whose sample position is the same as one or more events
  149. already in the buffer, the new event will be placed after the existing ones.
  150. The event data will be inspected to calculate the number of bytes in length that
  151. the midi event really takes up, so maxBytesOfMidiData may be longer than the data
  152. that actually gets stored. E.g. if you pass in a note-on and a length of 4 bytes,
  153. it'll actually only store 3 bytes. If the midi data is invalid, it might not
  154. add an event at all.
  155. To retrieve events, use a MidiBufferIterator object
  156. */
  157. void addEvent (const void* rawMidiData,
  158. int maxBytesOfMidiData,
  159. int sampleNumber);
  160. /** Adds some events from another buffer to this one.
  161. @param otherBuffer the buffer containing the events you want to add
  162. @param startSample the lowest sample number in the source buffer for which
  163. events should be added. Any source events whose timestamp is
  164. less than this will be ignored
  165. @param numSamples the valid range of samples from the source buffer for which
  166. events should be added - i.e. events in the source buffer whose
  167. timestamp is greater than or equal to (startSample + numSamples)
  168. will be ignored. If this value is less than 0, all events after
  169. startSample will be taken.
  170. @param sampleDeltaToAdd a value which will be added to the source timestamps of the events
  171. that are added to this buffer
  172. */
  173. void addEvents (const MidiBuffer& otherBuffer,
  174. int startSample,
  175. int numSamples,
  176. int sampleDeltaToAdd);
  177. /** Returns the sample number of the first event in the buffer.
  178. If the buffer's empty, this will just return 0.
  179. */
  180. int getFirstEventTime() const noexcept;
  181. /** Returns the sample number of the last event in the buffer.
  182. If the buffer's empty, this will just return 0.
  183. */
  184. int getLastEventTime() const noexcept;
  185. //==============================================================================
  186. /** Exchanges the contents of this buffer with another one.
  187. This is a quick operation, because no memory allocating or copying is done, it
  188. just swaps the internal state of the two buffers.
  189. */
  190. void swapWith (MidiBuffer&) noexcept;
  191. /** Preallocates some memory for the buffer to use.
  192. This helps to avoid needing to reallocate space when the buffer has messages
  193. added to it.
  194. */
  195. void ensureSize (size_t minimumNumBytes);
  196. /** Get a read-only iterator pointing to the beginning of this buffer. */
  197. MidiBufferIterator begin() const noexcept { return cbegin(); }
  198. /** Get a read-only iterator pointing one past the end of this buffer. */
  199. MidiBufferIterator end() const noexcept { return cend(); }
  200. /** Get a read-only iterator pointing to the beginning of this buffer. */
  201. MidiBufferIterator cbegin() const noexcept { return MidiBufferIterator (data.begin()); }
  202. /** Get a read-only iterator pointing one past the end of this buffer. */
  203. MidiBufferIterator cend() const noexcept { return MidiBufferIterator (data.end()); }
  204. /** Get an iterator pointing to the first event with a timestamp greater-than or
  205. equal-to `samplePosition`.
  206. */
  207. MidiBufferIterator findNextSamplePosition (int samplePosition) const noexcept;
  208. //==============================================================================
  209. /**
  210. Used to iterate through the events in a MidiBuffer.
  211. Note that altering the buffer while an iterator is using it will produce
  212. undefined behaviour.
  213. @see MidiBuffer
  214. */
  215. class JUCE_API Iterator
  216. {
  217. public:
  218. //==============================================================================
  219. /** Creates an Iterator for this MidiBuffer.
  220. This class has been deprecated in favour of MidiBufferIterator.
  221. */
  222. JUCE_DEPRECATED (Iterator (const MidiBuffer&) noexcept);
  223. /** Creates a copy of an iterator. */
  224. Iterator (const Iterator&) = default;
  225. /** Destructor. */
  226. ~Iterator() noexcept;
  227. //==============================================================================
  228. /** Repositions the iterator so that the next event retrieved will be the first
  229. one whose sample position is at greater than or equal to the given position.
  230. */
  231. void setNextSamplePosition (int samplePosition) noexcept;
  232. /** Retrieves a copy of the next event from the buffer.
  233. @param result on return, this will be the message. The MidiMessage's timestamp
  234. is set to the same value as samplePosition.
  235. @param samplePosition on return, this will be the position of the event, as a
  236. sample index in the buffer
  237. @returns true if an event was found, or false if the iterator has reached
  238. the end of the buffer
  239. */
  240. bool getNextEvent (MidiMessage& result,
  241. int& samplePosition) noexcept;
  242. /** Retrieves the next event from the buffer.
  243. @param midiData on return, this pointer will be set to a block of data containing
  244. the midi message. Note that to make it fast, this is a pointer
  245. directly into the MidiBuffer's internal data, so is only valid
  246. temporarily until the MidiBuffer is altered.
  247. @param numBytesOfMidiData on return, this is the number of bytes of data used by the
  248. midi message
  249. @param samplePosition on return, this will be the position of the event, as a
  250. sample index in the buffer
  251. @returns true if an event was found, or false if the iterator has reached
  252. the end of the buffer
  253. */
  254. bool getNextEvent (const uint8* &midiData,
  255. int& numBytesOfMidiData,
  256. int& samplePosition) noexcept;
  257. private:
  258. //==============================================================================
  259. const MidiBuffer& buffer;
  260. MidiBufferIterator iterator;
  261. };
  262. /** The raw data holding this buffer.
  263. Obviously access to this data is provided at your own risk. Its internal format could
  264. change in future, so don't write code that relies on it!
  265. */
  266. Array<uint8> data;
  267. private:
  268. JUCE_LEAK_DETECTOR (MidiBuffer)
  269. };
  270. } // namespace juce