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_MidiMessageSequence.h 13KB

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