The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

277 lines
11KB

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