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 11KB

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