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.

266 lines
11KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. #ifndef JUCE_MIDIMESSAGESEQUENCE_H_INCLUDED
  18. #define JUCE_MIDIMESSAGESEQUENCE_H_INCLUDED
  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. */
  26. class JUCE_API MidiMessageSequence
  27. {
  28. public:
  29. //==============================================================================
  30. /** Creates an empty midi sequence object. */
  31. MidiMessageSequence();
  32. /** Creates a copy of another sequence. */
  33. MidiMessageSequence (const MidiMessageSequence&);
  34. /** Replaces this sequence with another one. */
  35. MidiMessageSequence& operator= (const MidiMessageSequence&);
  36. /** Destructor. */
  37. ~MidiMessageSequence();
  38. //==============================================================================
  39. /** Structure used to hold midi events in the sequence.
  40. These structures act as 'handles' on the events as they are moved about in
  41. the list, and make it quick to find the matching note-offs for note-on events.
  42. @see MidiMessageSequence::getEventPointer
  43. */
  44. class MidiEventHolder
  45. {
  46. public:
  47. //==============================================================================
  48. /** Destructor. */
  49. ~MidiEventHolder();
  50. /** The message itself, whose timestamp is used to specify the event's time. */
  51. MidiMessage message;
  52. /** The matching note-off event (if this is a note-on event).
  53. If this isn't a note-on, this pointer will be nullptr.
  54. Use the MidiMessageSequence::updateMatchedPairs() method to keep these
  55. note-offs up-to-date after events have been moved around in the sequence
  56. or deleted.
  57. */
  58. MidiEventHolder* noteOffObject;
  59. private:
  60. //==============================================================================
  61. friend class MidiMessageSequence;
  62. MidiEventHolder (const MidiMessage&);
  63. JUCE_LEAK_DETECTOR (MidiEventHolder)
  64. };
  65. //==============================================================================
  66. /** Clears the sequence. */
  67. void clear();
  68. /** Returns the number of events in the sequence. */
  69. int getNumEvents() const noexcept;
  70. /** Returns a pointer to one of the events. */
  71. MidiEventHolder* getEventPointer (int index) const noexcept;
  72. /** Returns the time of the note-up that matches the note-on at this index.
  73. If the event at this index isn't a note-on, it'll just return 0.
  74. @see MidiMessageSequence::MidiEventHolder::noteOffObject
  75. */
  76. double getTimeOfMatchingKeyUp (int index) const noexcept;
  77. /** Returns the index of the note-up that matches the note-on at this index.
  78. If the event at this index isn't a note-on, it'll just return -1.
  79. @see MidiMessageSequence::MidiEventHolder::noteOffObject
  80. */
  81. int getIndexOfMatchingKeyUp (int index) const noexcept;
  82. /** Returns the index of an event. */
  83. int getIndexOf (MidiEventHolder* event) const noexcept;
  84. /** Returns the index of the first event on or after the given timestamp.
  85. If the time is beyond the end of the sequence, this will return the
  86. number of events.
  87. */
  88. int getNextIndexAtTime (double timeStamp) const noexcept;
  89. //==============================================================================
  90. /** Returns the timestamp of the first event in the sequence.
  91. @see getEndTime
  92. */
  93. double getStartTime() const noexcept;
  94. /** Returns the timestamp of the last event in the sequence.
  95. @see getStartTime
  96. */
  97. double getEndTime() const noexcept;
  98. /** Returns the timestamp of the event at a given index.
  99. If the index is out-of-range, this will return 0.0
  100. */
  101. double getEventTime (int index) const noexcept;
  102. //==============================================================================
  103. /** Inserts a midi message into the sequence.
  104. The index at which the new message gets inserted will depend on its timestamp,
  105. because the sequence is kept sorted.
  106. Remember to call updateMatchedPairs() after adding note-on events.
  107. @param newMessage the new message to add (an internal copy will be made)
  108. @param timeAdjustment an optional value to add to the timestamp of the message
  109. that will be inserted
  110. @see updateMatchedPairs
  111. */
  112. MidiEventHolder* addEvent (const MidiMessage& newMessage,
  113. double timeAdjustment = 0);
  114. /** Deletes one of the events in the sequence.
  115. Remember to call updateMatchedPairs() after removing events.
  116. @param index the index of the event to delete
  117. @param deleteMatchingNoteUp whether to also remove the matching note-off
  118. if the event you're removing is a note-on
  119. */
  120. void deleteEvent (int index, bool deleteMatchingNoteUp);
  121. /** Merges another sequence into this one.
  122. Remember to call updateMatchedPairs() after using this method.
  123. @param other the sequence to add from
  124. @param timeAdjustmentDelta an amount to add to the timestamps of the midi events
  125. as they are read from the other sequence
  126. @param firstAllowableDestTime events will not be added if their time is earlier
  127. than this time. (This is after their time has been adjusted
  128. by the timeAdjustmentDelta)
  129. @param endOfAllowableDestTimes events will not be added if their time is equal to
  130. or greater than this time. (This is after their time has
  131. been adjusted by the timeAdjustmentDelta)
  132. */
  133. void addSequence (const MidiMessageSequence& other,
  134. double timeAdjustmentDelta,
  135. double firstAllowableDestTime,
  136. double endOfAllowableDestTimes);
  137. //==============================================================================
  138. /** Makes sure all the note-on and note-off pairs are up-to-date.
  139. Call this after re-ordering messages or deleting/adding messages, and it
  140. will scan the list and make sure all the note-offs in the MidiEventHolder
  141. structures are pointing at the correct ones.
  142. */
  143. void updateMatchedPairs() noexcept;
  144. /** Forces a sort of the sequence.
  145. You may need to call this if you've manually modified the timestamps of some
  146. events such that the overall order now needs updating.
  147. */
  148. void sort() noexcept;
  149. //==============================================================================
  150. /** Copies all the messages for a particular midi channel to another sequence.
  151. @param channelNumberToExtract the midi channel to look for, in the range 1 to 16
  152. @param destSequence the sequence that the chosen events should be copied to
  153. @param alsoIncludeMetaEvents if true, any meta-events (which don't apply to a specific
  154. channel) will also be copied across.
  155. @see extractSysExMessages
  156. */
  157. void extractMidiChannelMessages (int channelNumberToExtract,
  158. MidiMessageSequence& destSequence,
  159. bool alsoIncludeMetaEvents) const;
  160. /** Copies all midi sys-ex messages to another sequence.
  161. @param destSequence this is the sequence to which any sys-exes in this sequence
  162. will be added
  163. @see extractMidiChannelMessages
  164. */
  165. void extractSysExMessages (MidiMessageSequence& destSequence) const;
  166. /** Removes any messages in this sequence that have a specific midi channel.
  167. @param channelNumberToRemove the midi channel to look for, in the range 1 to 16
  168. */
  169. void deleteMidiChannelMessages (int channelNumberToRemove);
  170. /** Removes any sys-ex messages from this sequence. */
  171. void deleteSysExMessages();
  172. /** Adds an offset to the timestamps of all events in the sequence.
  173. @param deltaTime the amount to add to each timestamp.
  174. */
  175. void addTimeToMessages (double deltaTime) noexcept;
  176. //==============================================================================
  177. /** Scans through the sequence to determine the state of any midi controllers at
  178. a given time.
  179. This will create a sequence of midi controller changes that can be
  180. used to set all midi controllers to the state they would be in at the
  181. specified time within this sequence.
  182. As well as controllers, it will also recreate the midi program number
  183. and pitch bend position.
  184. @param channelNumber the midi channel to look for, in the range 1 to 16. Controllers
  185. for other channels will be ignored.
  186. @param time the time at which you want to find out the state - there are
  187. no explicit units for this time measurement, it's the same units
  188. as used for the timestamps of the messages
  189. @param resultMessages an array to which midi controller-change messages will be added. This
  190. will be the minimum number of controller changes to recreate the
  191. state at the required time.
  192. */
  193. void createControllerUpdatesForTime (int channelNumber, double time,
  194. Array<MidiMessage>& resultMessages);
  195. //==============================================================================
  196. /** Swaps this sequence with another one. */
  197. void swapWith (MidiMessageSequence&) noexcept;
  198. private:
  199. //==============================================================================
  200. friend class MidiFile;
  201. OwnedArray<MidiEventHolder> list;
  202. JUCE_LEAK_DETECTOR (MidiMessageSequence)
  203. };
  204. #endif // JUCE_MIDIMESSAGESEQUENCE_H_INCLUDED