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.

316 lines
14KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - 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 sequence of timestamped midi messages.
  22. This allows the sequence to be manipulated, and also to be read from and
  23. written to a standard midi file.
  24. @see MidiMessage, MidiFile
  25. @tags{Audio}
  26. */
  27. class JUCE_API MidiMessageSequence
  28. {
  29. public:
  30. //==============================================================================
  31. /** Creates an empty midi sequence object. */
  32. MidiMessageSequence();
  33. /** Creates a copy of another sequence. */
  34. MidiMessageSequence (const MidiMessageSequence&);
  35. /** Replaces this sequence with another one. */
  36. MidiMessageSequence& operator= (const MidiMessageSequence&);
  37. /** Move constructor */
  38. MidiMessageSequence (MidiMessageSequence&&) noexcept;
  39. /** Move assignment operator */
  40. MidiMessageSequence& operator= (MidiMessageSequence&&) noexcept;
  41. //==============================================================================
  42. /** Structure used to hold midi events in the sequence.
  43. These structures act as 'handles' on the events as they are moved about in
  44. the list, and make it quick to find the matching note-offs for note-on events.
  45. @see MidiMessageSequence::getEventPointer
  46. */
  47. class MidiEventHolder
  48. {
  49. public:
  50. //==============================================================================
  51. /** The message itself, whose timestamp is used to specify the event's time. */
  52. MidiMessage message;
  53. /** The matching note-off event (if this is a note-on event).
  54. If this isn't a note-on, this pointer will be nullptr.
  55. Use the MidiMessageSequence::updateMatchedPairs() method to keep these
  56. note-offs up-to-date after events have been moved around in the sequence
  57. or deleted.
  58. */
  59. MidiEventHolder* noteOffObject = nullptr;
  60. private:
  61. //==============================================================================
  62. friend class MidiMessageSequence;
  63. MidiEventHolder (const MidiMessage&);
  64. MidiEventHolder (MidiMessage&&);
  65. JUCE_LEAK_DETECTOR (MidiEventHolder)
  66. };
  67. //==============================================================================
  68. /** Clears the sequence. */
  69. void clear();
  70. /** Returns the number of events in the sequence. */
  71. int getNumEvents() const noexcept;
  72. /** Returns a pointer to one of the events. */
  73. MidiEventHolder* getEventPointer (int index) const noexcept;
  74. /** Iterator for the list of MidiEventHolders */
  75. MidiEventHolder** begin() noexcept;
  76. /** Iterator for the list of MidiEventHolders */
  77. MidiEventHolder* const* begin() const noexcept;
  78. /** Iterator for the list of MidiEventHolders */
  79. MidiEventHolder** end() noexcept;
  80. /** Iterator for the list of MidiEventHolders */
  81. MidiEventHolder* const* end() const noexcept;
  82. /** Returns the time of the note-up that matches the note-on at this index.
  83. If the event at this index isn't a note-on, it'll just return 0.
  84. @see MidiMessageSequence::MidiEventHolder::noteOffObject
  85. */
  86. double getTimeOfMatchingKeyUp (int index) const noexcept;
  87. /** Returns the index of the note-up that matches the note-on at this index.
  88. If the event at this index isn't a note-on, it'll just return -1.
  89. @see MidiMessageSequence::MidiEventHolder::noteOffObject
  90. */
  91. int getIndexOfMatchingKeyUp (int index) const noexcept;
  92. /** Returns the index of an event. */
  93. int getIndexOf (const MidiEventHolder* event) const noexcept;
  94. /** Returns the index of the first event on or after the given timestamp.
  95. If the time is beyond the end of the sequence, this will return the
  96. number of events.
  97. */
  98. int getNextIndexAtTime (double timeStamp) const noexcept;
  99. //==============================================================================
  100. /** Returns the timestamp of the first event in the sequence.
  101. @see getEndTime
  102. */
  103. double getStartTime() const noexcept;
  104. /** Returns the timestamp of the last event in the sequence.
  105. @see getStartTime
  106. */
  107. double getEndTime() const noexcept;
  108. /** Returns the timestamp of the event at a given index.
  109. If the index is out-of-range, this will return 0.0
  110. */
  111. double getEventTime (int index) const noexcept;
  112. //==============================================================================
  113. /** Inserts a midi message into the sequence.
  114. The index at which the new message gets inserted will depend on its timestamp,
  115. because the sequence is kept sorted.
  116. Remember to call updateMatchedPairs() after adding note-on events.
  117. @param newMessage the new message to add (an internal copy will be made)
  118. @param timeAdjustment an optional value to add to the timestamp of the message
  119. that will be inserted
  120. @see updateMatchedPairs
  121. */
  122. MidiEventHolder* addEvent (const MidiMessage& newMessage, double timeAdjustment = 0);
  123. /** Inserts a midi message into the sequence.
  124. The index at which the new message gets inserted will depend on its timestamp,
  125. because the sequence is kept sorted.
  126. Remember to call updateMatchedPairs() after adding note-on events.
  127. @param newMessage the new message to add (an internal copy will be made)
  128. @param timeAdjustment an optional value to add to the timestamp of the message
  129. that will be inserted
  130. @see updateMatchedPairs
  131. */
  132. MidiEventHolder* addEvent (MidiMessage&& newMessage, double timeAdjustment = 0);
  133. /** Deletes one of the events in the sequence.
  134. Remember to call updateMatchedPairs() after removing events.
  135. @param index the index of the event to delete
  136. @param deleteMatchingNoteUp whether to also remove the matching note-off
  137. if the event you're removing is a note-on
  138. */
  139. void deleteEvent (int index, bool deleteMatchingNoteUp);
  140. /** Merges another sequence into this one.
  141. Remember to call updateMatchedPairs() after using this method.
  142. @param other the sequence to add from
  143. @param timeAdjustmentDelta an amount to add to the timestamps of the midi events
  144. as they are read from the other sequence
  145. @param firstAllowableDestTime events will not be added if their time is earlier
  146. than this time. (This is after their time has been adjusted
  147. by the timeAdjustmentDelta)
  148. @param endOfAllowableDestTimes events will not be added if their time is equal to
  149. or greater than this time. (This is after their time has
  150. been adjusted by the timeAdjustmentDelta)
  151. */
  152. void addSequence (const MidiMessageSequence& other,
  153. double timeAdjustmentDelta,
  154. double firstAllowableDestTime,
  155. double endOfAllowableDestTimes);
  156. /** Merges another sequence into this one.
  157. Remember to call updateMatchedPairs() after using this method.
  158. @param other the sequence to add from
  159. @param timeAdjustmentDelta an amount to add to the timestamps of the midi events
  160. as they are read from the other sequence
  161. */
  162. void addSequence (const MidiMessageSequence& other,
  163. double timeAdjustmentDelta);
  164. //==============================================================================
  165. /** Makes sure all the note-on and note-off pairs are up-to-date.
  166. Call this after re-ordering messages or deleting/adding messages, and it
  167. will scan the list and make sure all the note-offs in the MidiEventHolder
  168. structures are pointing at the correct ones.
  169. */
  170. void updateMatchedPairs() noexcept;
  171. /** Forces a sort of the sequence.
  172. You may need to call this if you've manually modified the timestamps of some
  173. events such that the overall order now needs updating.
  174. */
  175. void sort() noexcept;
  176. //==============================================================================
  177. /** Copies all the messages for a particular midi channel to another sequence.
  178. @param channelNumberToExtract the midi channel to look for, in the range 1 to 16
  179. @param destSequence the sequence that the chosen events should be copied to
  180. @param alsoIncludeMetaEvents if true, any meta-events (which don't apply to a specific
  181. channel) will also be copied across.
  182. @see extractSysExMessages
  183. */
  184. void extractMidiChannelMessages (int channelNumberToExtract,
  185. MidiMessageSequence& destSequence,
  186. bool alsoIncludeMetaEvents) const;
  187. /** Copies all midi sys-ex messages to another sequence.
  188. @param destSequence this is the sequence to which any sys-exes in this sequence
  189. will be added
  190. @see extractMidiChannelMessages
  191. */
  192. void extractSysExMessages (MidiMessageSequence& destSequence) const;
  193. /** Removes any messages in this sequence that have a specific midi channel.
  194. @param channelNumberToRemove the midi channel to look for, in the range 1 to 16
  195. */
  196. void deleteMidiChannelMessages (int channelNumberToRemove);
  197. /** Removes any sys-ex messages from this sequence. */
  198. void deleteSysExMessages();
  199. /** Adds an offset to the timestamps of all events in the sequence.
  200. @param deltaTime the amount to add to each timestamp.
  201. */
  202. void addTimeToMessages (double deltaTime) noexcept;
  203. //==============================================================================
  204. /** Scans through the sequence to determine the state of any midi controllers at
  205. a given time.
  206. This will create a sequence of midi controller changes that can be
  207. used to set all midi controllers to the state they would be in at the
  208. specified time within this sequence.
  209. As well as controllers, it will also recreate the midi program number
  210. and pitch bend position.
  211. This function has special handling for the "bank select" and "data entry"
  212. controllers (0x00, 0x20, 0x06, 0x26, 0x60, 0x61, 0x62, 0x63, 0x64, 0x65).
  213. If the sequence contains multiple bank select and program change messages,
  214. only the bank select messages immediately preceding the final program change
  215. message will be kept.
  216. All "data increment" and "data decrement" messages will be retained. Some hardware will
  217. ignore the requested increment/decrement values, so retaining all messages is the only
  218. way to ensure compatibility with all hardware.
  219. "Parameter number" changes will be slightly condensed. Only the parameter number
  220. events immediately preceding each data entry event will be kept. The parameter number
  221. will also be set to its final value at the end of the sequence, if necessary.
  222. @param channelNumber the midi channel to look for, in the range 1 to 16. Controllers
  223. for other channels will be ignored.
  224. @param time the time at which you want to find out the state - there are
  225. no explicit units for this time measurement, it's the same units
  226. as used for the timestamps of the messages
  227. @param resultMessages an array to which midi controller-change messages will be added. This
  228. will be the minimum number of controller changes to recreate the
  229. state at the required time.
  230. */
  231. void createControllerUpdatesForTime (int channelNumber, double time,
  232. Array<MidiMessage>& resultMessages);
  233. //==============================================================================
  234. /** Swaps this sequence with another one. */
  235. void swapWith (MidiMessageSequence&) noexcept;
  236. private:
  237. //==============================================================================
  238. friend class MidiFile;
  239. OwnedArray<MidiEventHolder> list;
  240. MidiEventHolder* addEvent (MidiEventHolder*, double);
  241. JUCE_LEAK_DETECTOR (MidiMessageSequence)
  242. };
  243. } // namespace juce