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.

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