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.

294 lines
12KB

  1. /*
  2. ==============================================================================
  3. This file is part of the Water library.
  4. Copyright (c) 2016 ROLI Ltd.
  5. Copyright (C) 2017-2019 Filipe Coelho <falktx@falktx.com>
  6. Permission is granted to use this software under the terms of the ISC license
  7. http://www.isc.org/downloads/software-support-policy/isc-license/
  8. Permission to use, copy, modify, and/or distribute this software for any
  9. purpose with or without fee is hereby granted, provided that the above
  10. copyright notice and this permission notice appear in all copies.
  11. THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD
  12. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  13. FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
  14. OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  15. USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  16. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  17. OF THIS SOFTWARE.
  18. ==============================================================================
  19. */
  20. #ifndef WATER_MIDIMESSAGESEQUENCE_H_INCLUDED
  21. #define WATER_MIDIMESSAGESEQUENCE_H_INCLUDED
  22. #include "MidiMessage.h"
  23. #include "../containers/Array.h"
  24. #include "../containers/OwnedArray.h"
  25. #include "../text/String.h"
  26. namespace water {
  27. //==============================================================================
  28. /**
  29. A sequence of timestamped midi messages.
  30. This allows the sequence to be manipulated, and also to be read from and
  31. written to a standard midi file.
  32. @see MidiMessage, MidiFile
  33. */
  34. class MidiMessageSequence
  35. {
  36. public:
  37. //==============================================================================
  38. /** Creates an empty midi sequence object. */
  39. MidiMessageSequence();
  40. /** Creates a copy of another sequence. */
  41. MidiMessageSequence (const MidiMessageSequence&);
  42. /** Replaces this sequence with another one. */
  43. MidiMessageSequence& operator= (const MidiMessageSequence&);
  44. #if WATER_COMPILER_SUPPORTS_MOVE_SEMANTICS
  45. MidiMessageSequence (MidiMessageSequence&& other) noexcept
  46. : list (static_cast<OwnedArray<MidiEventHolder>&&> (other.list))
  47. {}
  48. MidiMessageSequence& operator= (MidiMessageSequence&& other) noexcept
  49. {
  50. list = static_cast<OwnedArray<MidiEventHolder>&&> (other.list);
  51. return *this;
  52. }
  53. #endif
  54. /** Destructor. */
  55. ~MidiMessageSequence();
  56. //==============================================================================
  57. /** Structure used to hold midi events in the sequence.
  58. These structures act as 'handles' on the events as they are moved about in
  59. the list, and make it quick to find the matching note-offs for note-on events.
  60. @see MidiMessageSequence::getEventPointer
  61. */
  62. class MidiEventHolder
  63. {
  64. public:
  65. //==============================================================================
  66. /** Destructor. */
  67. ~MidiEventHolder();
  68. /** The message itself, whose timestamp is used to specify the event's time. */
  69. MidiMessage message;
  70. /** The matching note-off event (if this is a note-on event).
  71. If this isn't a note-on, this pointer will be nullptr.
  72. Use the MidiMessageSequence::updateMatchedPairs() method to keep these
  73. note-offs up-to-date after events have been moved around in the sequence
  74. or deleted.
  75. */
  76. MidiEventHolder* noteOffObject;
  77. private:
  78. //==============================================================================
  79. friend class MidiMessageSequence;
  80. MidiEventHolder (const MidiMessage&);
  81. CARLA_DECLARE_NON_COPY_CLASS(MidiEventHolder);
  82. };
  83. //==============================================================================
  84. /** Clears the sequence. */
  85. void clear();
  86. /** Returns the number of events in the sequence. */
  87. int getNumEvents() const noexcept;
  88. /** Returns a pointer to one of the events. */
  89. MidiEventHolder* getEventPointer (int index) const noexcept;
  90. /** Returns the time of the note-up that matches the note-on at this index.
  91. If the event at this index isn't a note-on, it'll just return 0.
  92. @see MidiMessageSequence::MidiEventHolder::noteOffObject
  93. */
  94. double getTimeOfMatchingKeyUp (int index) const noexcept;
  95. /** Returns the index of the note-up that matches the note-on at this index.
  96. If the event at this index isn't a note-on, it'll just return -1.
  97. @see MidiMessageSequence::MidiEventHolder::noteOffObject
  98. */
  99. int getIndexOfMatchingKeyUp (int index) const noexcept;
  100. /** Returns the index of an event. */
  101. int getIndexOf (const MidiEventHolder* event) const noexcept;
  102. /** Returns the index of the first event on or after the given timestamp.
  103. If the time is beyond the end of the sequence, this will return the
  104. number of events.
  105. */
  106. int getNextIndexAtTime (double timeStamp) const noexcept;
  107. //==============================================================================
  108. /** Returns the timestamp of the first event in the sequence.
  109. @see getEndTime
  110. */
  111. double getStartTime() const noexcept;
  112. /** Returns the timestamp of the last event in the sequence.
  113. @see getStartTime
  114. */
  115. double getEndTime() const noexcept;
  116. /** Returns the timestamp of the event at a given index.
  117. If the index is out-of-range, this will return 0.0
  118. */
  119. double getEventTime (int index) const noexcept;
  120. //==============================================================================
  121. /** Inserts a midi message into the sequence.
  122. The index at which the new message gets inserted will depend on its timestamp,
  123. because the sequence is kept sorted.
  124. Remember to call updateMatchedPairs() after adding note-on events.
  125. @param newMessage the new message to add (an internal copy will be made)
  126. @param timeAdjustment an optional value to add to the timestamp of the message
  127. that will be inserted
  128. @see updateMatchedPairs
  129. */
  130. MidiEventHolder* addEvent (const MidiMessage& newMessage,
  131. double timeAdjustment = 0);
  132. /** Deletes one of the events in the sequence.
  133. Remember to call updateMatchedPairs() after removing events.
  134. @param index the index of the event to delete
  135. @param deleteMatchingNoteUp whether to also remove the matching note-off
  136. if the event you're removing is a note-on
  137. */
  138. void deleteEvent (int index, bool deleteMatchingNoteUp);
  139. /** Merges another sequence into this one.
  140. Remember to call updateMatchedPairs() after using this method.
  141. @param other the sequence to add from
  142. @param timeAdjustmentDelta an amount to add to the timestamps of the midi events
  143. as they are read from the other sequence
  144. @param firstAllowableDestTime events will not be added if their time is earlier
  145. than this time. (This is after their time has been adjusted
  146. by the timeAdjustmentDelta)
  147. @param endOfAllowableDestTimes events will not be added if their time is equal to
  148. or greater than this time. (This is after their time has
  149. been adjusted by the timeAdjustmentDelta)
  150. */
  151. void addSequence (const MidiMessageSequence& other,
  152. double timeAdjustmentDelta,
  153. double firstAllowableDestTime,
  154. double endOfAllowableDestTimes);
  155. /** Merges another sequence into this one.
  156. Remember to call updateMatchedPairs() after using this method.
  157. @param other the sequence to add from
  158. @param timeAdjustmentDelta an amount to add to the timestamps of the midi events
  159. as they are read from the other sequence
  160. */
  161. void addSequence (const MidiMessageSequence& other,
  162. double timeAdjustmentDelta);
  163. //==============================================================================
  164. /** Makes sure all the note-on and note-off pairs are up-to-date.
  165. Call this after re-ordering messages or deleting/adding messages, and it
  166. will scan the list and make sure all the note-offs in the MidiEventHolder
  167. structures are pointing at the correct ones.
  168. */
  169. void updateMatchedPairs() noexcept;
  170. /** Forces a sort of the sequence.
  171. You may need to call this if you've manually modified the timestamps of some
  172. events such that the overall order now needs updating.
  173. */
  174. void sort() noexcept;
  175. //==============================================================================
  176. /** Copies all the messages for a particular midi channel to another sequence.
  177. @param channelNumberToExtract the midi channel to look for, in the range 1 to 16
  178. @param destSequence the sequence that the chosen events should be copied to
  179. @param alsoIncludeMetaEvents if true, any meta-events (which don't apply to a specific
  180. channel) will also be copied across.
  181. @see extractSysExMessages
  182. */
  183. void extractMidiChannelMessages (int channelNumberToExtract,
  184. MidiMessageSequence& destSequence,
  185. bool alsoIncludeMetaEvents) const;
  186. /** Copies all midi sys-ex messages to another sequence.
  187. @param destSequence this is the sequence to which any sys-exes in this sequence
  188. will be added
  189. @see extractMidiChannelMessages
  190. */
  191. void extractSysExMessages (MidiMessageSequence& destSequence) const;
  192. /** Removes any messages in this sequence that have a specific midi channel.
  193. @param channelNumberToRemove the midi channel to look for, in the range 1 to 16
  194. */
  195. void deleteMidiChannelMessages (int channelNumberToRemove);
  196. /** Removes any sys-ex messages from this sequence. */
  197. void deleteSysExMessages();
  198. /** Adds an offset to the timestamps of all events in the sequence.
  199. @param deltaTime the amount to add to each timestamp.
  200. */
  201. void addTimeToMessages (double deltaTime) noexcept;
  202. //==============================================================================
  203. /** Scans through the sequence to determine the state of any midi controllers at
  204. a given time.
  205. This will create a sequence of midi controller changes that can be
  206. used to set all midi controllers to the state they would be in at the
  207. specified time within this sequence.
  208. As well as controllers, it will also recreate the midi program number
  209. and pitch bend position.
  210. @param channelNumber the midi channel to look for, in the range 1 to 16. Controllers
  211. for other channels will be ignored.
  212. @param time the time at which you want to find out the state - there are
  213. no explicit units for this time measurement, it's the same units
  214. as used for the timestamps of the messages
  215. @param resultMessages an array to which midi controller-change messages will be added. This
  216. will be the minimum number of controller changes to recreate the
  217. state at the required time.
  218. */
  219. void createControllerUpdatesForTime (int channelNumber, double time,
  220. Array<MidiMessage>& resultMessages);
  221. //==============================================================================
  222. /** Swaps this sequence with another one. */
  223. void swapWith (MidiMessageSequence&) noexcept;
  224. private:
  225. //==============================================================================
  226. friend class MidiFile;
  227. OwnedArray<MidiEventHolder> list;
  228. };
  229. }
  230. #endif // WATER_MIDIMESSAGESEQUENCE_H_INCLUDED