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

9 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
8 years ago
10 years ago
10 years ago
10 years ago
9 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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. /** Merges another sequence into this one.
  138. Remember to call updateMatchedPairs() after using this method.
  139. @param other the sequence to add from
  140. @param timeAdjustmentDelta an amount to add to the timestamps of the midi events
  141. as they are read from the other sequence
  142. */
  143. void addSequence (const MidiMessageSequence& other,
  144. double timeAdjustmentDelta);
  145. //==============================================================================
  146. /** Makes sure all the note-on and note-off pairs are up-to-date.
  147. Call this after re-ordering messages or deleting/adding messages, and it
  148. will scan the list and make sure all the note-offs in the MidiEventHolder
  149. structures are pointing at the correct ones.
  150. */
  151. void updateMatchedPairs() noexcept;
  152. /** Forces a sort of the sequence.
  153. You may need to call this if you've manually modified the timestamps of some
  154. events such that the overall order now needs updating.
  155. */
  156. void sort() noexcept;
  157. //==============================================================================
  158. /** Copies all the messages for a particular midi channel to another sequence.
  159. @param channelNumberToExtract the midi channel to look for, in the range 1 to 16
  160. @param destSequence the sequence that the chosen events should be copied to
  161. @param alsoIncludeMetaEvents if true, any meta-events (which don't apply to a specific
  162. channel) will also be copied across.
  163. @see extractSysExMessages
  164. */
  165. void extractMidiChannelMessages (int channelNumberToExtract,
  166. MidiMessageSequence& destSequence,
  167. bool alsoIncludeMetaEvents) const;
  168. /** Copies all midi sys-ex messages to another sequence.
  169. @param destSequence this is the sequence to which any sys-exes in this sequence
  170. will be added
  171. @see extractMidiChannelMessages
  172. */
  173. void extractSysExMessages (MidiMessageSequence& destSequence) const;
  174. /** Removes any messages in this sequence that have a specific midi channel.
  175. @param channelNumberToRemove the midi channel to look for, in the range 1 to 16
  176. */
  177. void deleteMidiChannelMessages (int channelNumberToRemove);
  178. /** Removes any sys-ex messages from this sequence. */
  179. void deleteSysExMessages();
  180. /** Adds an offset to the timestamps of all events in the sequence.
  181. @param deltaTime the amount to add to each timestamp.
  182. */
  183. void addTimeToMessages (double deltaTime) noexcept;
  184. //==============================================================================
  185. /** Scans through the sequence to determine the state of any midi controllers at
  186. a given time.
  187. This will create a sequence of midi controller changes that can be
  188. used to set all midi controllers to the state they would be in at the
  189. specified time within this sequence.
  190. As well as controllers, it will also recreate the midi program number
  191. and pitch bend position.
  192. @param channelNumber the midi channel to look for, in the range 1 to 16. Controllers
  193. for other channels will be ignored.
  194. @param time the time at which you want to find out the state - there are
  195. no explicit units for this time measurement, it's the same units
  196. as used for the timestamps of the messages
  197. @param resultMessages an array to which midi controller-change messages will be added. This
  198. will be the minimum number of controller changes to recreate the
  199. state at the required time.
  200. */
  201. void createControllerUpdatesForTime (int channelNumber, double time,
  202. Array<MidiMessage>& resultMessages);
  203. //==============================================================================
  204. /** Swaps this sequence with another one. */
  205. void swapWith (MidiMessageSequence&) noexcept;
  206. private:
  207. //==============================================================================
  208. friend class MidiFile;
  209. OwnedArray<MidiEventHolder> list;
  210. JUCE_LEAK_DETECTOR (MidiMessageSequence)
  211. };
  212. #endif // JUCE_MIDIMESSAGESEQUENCE_H_INCLUDED