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.

176 lines
6.6KB

  1. /*
  2. ==============================================================================
  3. This file is part of the Water library.
  4. Copyright (c) 2016 ROLI Ltd.
  5. Copyright (C) 2017-2022 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. namespace water {
  26. //==============================================================================
  27. /**
  28. A sequence of timestamped midi messages.
  29. This allows the sequence to be manipulated, and also to be read from and
  30. written to a standard midi file.
  31. @see MidiMessage, MidiFile
  32. */
  33. class MidiMessageSequence
  34. {
  35. public:
  36. //==============================================================================
  37. /** Creates an empty midi sequence object. */
  38. MidiMessageSequence();
  39. /** Creates a copy of another sequence. */
  40. MidiMessageSequence (const MidiMessageSequence&);
  41. /** Replaces this sequence with another one. */
  42. MidiMessageSequence& operator= (const MidiMessageSequence&);
  43. /** Destructor. */
  44. ~MidiMessageSequence();
  45. //==============================================================================
  46. /** Structure used to hold midi events in the sequence.
  47. These structures act as 'handles' on the events as they are moved about in
  48. the list, and make it quick to find the matching note-offs for note-on events.
  49. @see MidiMessageSequence::getEventPointer
  50. */
  51. class MidiEventHolder
  52. {
  53. public:
  54. //==============================================================================
  55. /** Destructor. */
  56. ~MidiEventHolder();
  57. /** The message itself, whose timestamp is used to specify the event's time. */
  58. MidiMessage message;
  59. /** The matching note-off event (if this is a note-on event).
  60. If this isn't a note-on, this pointer will be nullptr.
  61. Use the MidiMessageSequence::updateMatchedPairs() method to keep these
  62. note-offs up-to-date after events have been moved around in the sequence
  63. or deleted.
  64. */
  65. MidiEventHolder* noteOffObject;
  66. private:
  67. //==============================================================================
  68. friend class MidiMessageSequence;
  69. MidiEventHolder (const MidiMessage&);
  70. CARLA_DECLARE_NON_COPYABLE(MidiEventHolder);
  71. };
  72. //==============================================================================
  73. /** Clears the sequence. */
  74. void clear();
  75. /** Returns the number of events in the sequence. */
  76. int getNumEvents() const noexcept;
  77. /** Returns a pointer to one of the events. */
  78. MidiEventHolder* getEventPointer (int index) const noexcept;
  79. //==============================================================================
  80. /** Returns the timestamp of the first event in the sequence.
  81. @see getEndTime
  82. */
  83. double getStartTime() const noexcept;
  84. /** Returns the timestamp of the last event in the sequence.
  85. @see getStartTime
  86. */
  87. double getEndTime() const noexcept;
  88. /** Returns the timestamp of the event at a given index.
  89. If the index is out-of-range, this will return 0.0
  90. */
  91. double getEventTime (int index) const noexcept;
  92. //==============================================================================
  93. /** Inserts a midi message into the sequence.
  94. The index at which the new message gets inserted will depend on its timestamp,
  95. because the sequence is kept sorted.
  96. Remember to call updateMatchedPairs() after adding note-on events.
  97. @param newMessage the new message to add (an internal copy will be made)
  98. @param timeAdjustment an optional value to add to the timestamp of the message
  99. that will be inserted
  100. @see updateMatchedPairs
  101. */
  102. MidiEventHolder* addEvent (const MidiMessage& newMessage,
  103. double timeAdjustment = 0);
  104. /** Merges another sequence into this one.
  105. Remember to call updateMatchedPairs() after using this method.
  106. @param other the sequence to add from
  107. @param timeAdjustmentDelta an amount to add to the timestamps of the midi events
  108. as they are read from the other sequence
  109. */
  110. void addSequence (const MidiMessageSequence& other,
  111. double timeAdjustmentDelta);
  112. //==============================================================================
  113. /** Makes sure all the note-on and note-off pairs are up-to-date.
  114. Call this after re-ordering messages or deleting/adding messages, and it
  115. will scan the list and make sure all the note-offs in the MidiEventHolder
  116. structures are pointing at the correct ones.
  117. */
  118. void updateMatchedPairs() noexcept;
  119. /** Forces a sort of the sequence.
  120. You may need to call this if you've manually modified the timestamps of some
  121. events such that the overall order now needs updating.
  122. */
  123. void sort() noexcept;
  124. //==============================================================================
  125. /** Swaps this sequence with another one. */
  126. void swapWith (MidiMessageSequence&) noexcept;
  127. private:
  128. //==============================================================================
  129. friend class MidiFile;
  130. OwnedArray<MidiEventHolder> list;
  131. };
  132. }
  133. #endif // WATER_MIDIMESSAGESEQUENCE_H_INCLUDED