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.

MidiMessageSequence.cpp 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. #include "MidiMessageSequence.h"
  21. namespace water {
  22. MidiMessageSequence::MidiMessageSequence()
  23. {
  24. }
  25. MidiMessageSequence::MidiMessageSequence (const MidiMessageSequence& other)
  26. {
  27. addSequence(other, 0.0);
  28. updateMatchedPairs();
  29. }
  30. MidiMessageSequence& MidiMessageSequence::operator= (const MidiMessageSequence& other)
  31. {
  32. MidiMessageSequence otherCopy (other);
  33. swapWith (otherCopy);
  34. return *this;
  35. }
  36. void MidiMessageSequence::swapWith (MidiMessageSequence& other) noexcept
  37. {
  38. list.swapWith (other.list);
  39. }
  40. MidiMessageSequence::~MidiMessageSequence()
  41. {
  42. }
  43. void MidiMessageSequence::clear()
  44. {
  45. list.clear();
  46. }
  47. int MidiMessageSequence::getNumEvents() const noexcept
  48. {
  49. return list.size();
  50. }
  51. MidiMessageSequence::MidiEventHolder* MidiMessageSequence::getEventPointer (const int index) const noexcept
  52. {
  53. return list [index];
  54. }
  55. //==============================================================================
  56. double MidiMessageSequence::getStartTime() const noexcept
  57. {
  58. return getEventTime (0);
  59. }
  60. double MidiMessageSequence::getEndTime() const noexcept
  61. {
  62. return getEventTime (list.size() - 1);
  63. }
  64. double MidiMessageSequence::getEventTime (const int index) const noexcept
  65. {
  66. if (const MidiEventHolder* const meh = list [index])
  67. return meh->message.getTimeStamp();
  68. return 0.0;
  69. }
  70. //==============================================================================
  71. MidiMessageSequence::MidiEventHolder* MidiMessageSequence::addEvent (const MidiMessage& newMessage,
  72. double timeAdjustment)
  73. {
  74. MidiEventHolder* const newOne = new MidiEventHolder (newMessage);
  75. timeAdjustment += newMessage.getTimeStamp();
  76. newOne->message.setTimeStamp (timeAdjustment);
  77. int i;
  78. for (i = list.size(); --i >= 0;)
  79. if (list.getUnchecked(i)->message.getTimeStamp() <= timeAdjustment)
  80. break;
  81. list.insert (i + 1, newOne);
  82. return newOne;
  83. }
  84. struct MidiMessageSequenceSorter
  85. {
  86. static int compareElements (const MidiMessageSequence::MidiEventHolder* const first,
  87. const MidiMessageSequence::MidiEventHolder* const second) noexcept
  88. {
  89. const double diff = first->message.getTimeStamp() - second->message.getTimeStamp();
  90. return (diff > 0) - (diff < 0);
  91. }
  92. };
  93. void MidiMessageSequence::addSequence (const MidiMessageSequence& other, double timeAdjustment)
  94. {
  95. for (int i = 0; i < static_cast<int>(other.list.size()); ++i)
  96. {
  97. const MidiMessage& m = other.list.getUnchecked(i)->message;
  98. MidiEventHolder* const newOne = new MidiEventHolder (m);
  99. newOne->message.addToTimeStamp (timeAdjustment);
  100. list.add (newOne);
  101. }
  102. sort();
  103. }
  104. //==============================================================================
  105. void MidiMessageSequence::sort() noexcept
  106. {
  107. MidiMessageSequenceSorter sorter;
  108. list.sort (sorter, true);
  109. }
  110. void MidiMessageSequence::updateMatchedPairs() noexcept
  111. {
  112. for (int i = 0; i < static_cast<int>(list.size()); ++i)
  113. {
  114. MidiEventHolder* const meh = list.getUnchecked(i);
  115. const MidiMessage& m1 = meh->message;
  116. if (m1.isNoteOn())
  117. {
  118. meh->noteOffObject = nullptr;
  119. const int note = m1.getNoteNumber();
  120. const int chan = m1.getChannel();
  121. const int len = list.size();
  122. for (int j = i + 1; j < len; ++j)
  123. {
  124. const MidiMessage& m = list.getUnchecked(j)->message;
  125. if (m.getNoteNumber() == note && m.getChannel() == chan)
  126. {
  127. if (m.isNoteOff())
  128. {
  129. meh->noteOffObject = list[j];
  130. break;
  131. }
  132. else if (m.isNoteOn())
  133. {
  134. MidiEventHolder* const newEvent = new MidiEventHolder (MidiMessage::noteOff (chan, note));
  135. list.insert (j, newEvent);
  136. newEvent->message.setTimeStamp (m.getTimeStamp());
  137. meh->noteOffObject = newEvent;
  138. break;
  139. }
  140. }
  141. }
  142. }
  143. }
  144. }
  145. //==============================================================================
  146. MidiMessageSequence::MidiEventHolder::MidiEventHolder (const MidiMessage& mm)
  147. : message (mm), noteOffObject (nullptr)
  148. {
  149. }
  150. MidiMessageSequence::MidiEventHolder::~MidiEventHolder()
  151. {
  152. }
  153. }