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.

326 lines
9.9KB

  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. MidiMessageSequence::MidiMessageSequence()
  18. {
  19. }
  20. MidiMessageSequence::MidiMessageSequence (const MidiMessageSequence& other)
  21. {
  22. list.addCopiesOf (other.list);
  23. updateMatchedPairs();
  24. }
  25. MidiMessageSequence& MidiMessageSequence::operator= (const MidiMessageSequence& other)
  26. {
  27. MidiMessageSequence otherCopy (other);
  28. swapWith (otherCopy);
  29. return *this;
  30. }
  31. void MidiMessageSequence::swapWith (MidiMessageSequence& other) noexcept
  32. {
  33. list.swapWith (other.list);
  34. }
  35. MidiMessageSequence::~MidiMessageSequence()
  36. {
  37. }
  38. void MidiMessageSequence::clear()
  39. {
  40. list.clear();
  41. }
  42. int MidiMessageSequence::getNumEvents() const noexcept
  43. {
  44. return list.size();
  45. }
  46. MidiMessageSequence::MidiEventHolder* MidiMessageSequence::getEventPointer (const int index) const noexcept
  47. {
  48. return list [index];
  49. }
  50. double MidiMessageSequence::getTimeOfMatchingKeyUp (const int index) const noexcept
  51. {
  52. if (const MidiEventHolder* const meh = list [index])
  53. if (meh->noteOffObject != nullptr)
  54. return meh->noteOffObject->message.getTimeStamp();
  55. return 0.0;
  56. }
  57. int MidiMessageSequence::getIndexOfMatchingKeyUp (const int index) const noexcept
  58. {
  59. if (const MidiEventHolder* const meh = list [index])
  60. return list.indexOf (meh->noteOffObject);
  61. return -1;
  62. }
  63. int MidiMessageSequence::getIndexOf (MidiEventHolder* const event) const noexcept
  64. {
  65. return list.indexOf (event);
  66. }
  67. int MidiMessageSequence::getNextIndexAtTime (const double timeStamp) const noexcept
  68. {
  69. const int numEvents = list.size();
  70. int i;
  71. for (i = 0; i < numEvents; ++i)
  72. if (list.getUnchecked(i)->message.getTimeStamp() >= timeStamp)
  73. break;
  74. return i;
  75. }
  76. //==============================================================================
  77. double MidiMessageSequence::getStartTime() const noexcept
  78. {
  79. return getEventTime (0);
  80. }
  81. double MidiMessageSequence::getEndTime() const noexcept
  82. {
  83. return getEventTime (list.size() - 1);
  84. }
  85. double MidiMessageSequence::getEventTime (const int index) const noexcept
  86. {
  87. if (const MidiEventHolder* const meh = list [index])
  88. return meh->message.getTimeStamp();
  89. return 0.0;
  90. }
  91. //==============================================================================
  92. MidiMessageSequence::MidiEventHolder* MidiMessageSequence::addEvent (const MidiMessage& newMessage,
  93. double timeAdjustment)
  94. {
  95. MidiEventHolder* const newOne = new MidiEventHolder (newMessage);
  96. timeAdjustment += newMessage.getTimeStamp();
  97. newOne->message.setTimeStamp (timeAdjustment);
  98. int i;
  99. for (i = list.size(); --i >= 0;)
  100. if (list.getUnchecked(i)->message.getTimeStamp() <= timeAdjustment)
  101. break;
  102. list.insert (i + 1, newOne);
  103. return newOne;
  104. }
  105. void MidiMessageSequence::deleteEvent (const int index,
  106. const bool deleteMatchingNoteUp)
  107. {
  108. if (isPositiveAndBelow (index, list.size()))
  109. {
  110. if (deleteMatchingNoteUp)
  111. deleteEvent (getIndexOfMatchingKeyUp (index), false);
  112. list.remove (index);
  113. }
  114. }
  115. struct MidiMessageSequenceSorter
  116. {
  117. static int compareElements (const MidiMessageSequence::MidiEventHolder* const first,
  118. const MidiMessageSequence::MidiEventHolder* const second) noexcept
  119. {
  120. const double diff = first->message.getTimeStamp() - second->message.getTimeStamp();
  121. return (diff > 0) - (diff < 0);
  122. }
  123. };
  124. void MidiMessageSequence::addSequence (const MidiMessageSequence& other,
  125. double timeAdjustment,
  126. double firstAllowableTime,
  127. double endOfAllowableDestTimes)
  128. {
  129. firstAllowableTime -= timeAdjustment;
  130. endOfAllowableDestTimes -= timeAdjustment;
  131. for (int i = 0; i < other.list.size(); ++i)
  132. {
  133. const MidiMessage& m = other.list.getUnchecked(i)->message;
  134. const double t = m.getTimeStamp();
  135. if (t >= firstAllowableTime && t < endOfAllowableDestTimes)
  136. {
  137. MidiEventHolder* const newOne = new MidiEventHolder (m);
  138. newOne->message.setTimeStamp (timeAdjustment + t);
  139. list.add (newOne);
  140. }
  141. }
  142. sort();
  143. }
  144. //==============================================================================
  145. void MidiMessageSequence::sort() noexcept
  146. {
  147. MidiMessageSequenceSorter sorter;
  148. list.sort (sorter, true);
  149. }
  150. void MidiMessageSequence::updateMatchedPairs() noexcept
  151. {
  152. for (int i = 0; i < list.size(); ++i)
  153. {
  154. MidiEventHolder* const meh = list.getUnchecked(i);
  155. const MidiMessage& m1 = meh->message;
  156. if (m1.isNoteOn())
  157. {
  158. meh->noteOffObject = nullptr;
  159. const int note = m1.getNoteNumber();
  160. const int chan = m1.getChannel();
  161. const int len = list.size();
  162. for (int j = i + 1; j < len; ++j)
  163. {
  164. const MidiMessage& m = list.getUnchecked(j)->message;
  165. if (m.getNoteNumber() == note && m.getChannel() == chan)
  166. {
  167. if (m.isNoteOff())
  168. {
  169. meh->noteOffObject = list[j];
  170. break;
  171. }
  172. else if (m.isNoteOn())
  173. {
  174. MidiEventHolder* const newEvent = new MidiEventHolder (MidiMessage::noteOff (chan, note));
  175. list.insert (j, newEvent);
  176. newEvent->message.setTimeStamp (m.getTimeStamp());
  177. meh->noteOffObject = newEvent;
  178. break;
  179. }
  180. }
  181. }
  182. }
  183. }
  184. }
  185. void MidiMessageSequence::addTimeToMessages (const double delta) noexcept
  186. {
  187. for (int i = list.size(); --i >= 0;)
  188. {
  189. MidiMessage& mm = list.getUnchecked(i)->message;
  190. mm.setTimeStamp (mm.getTimeStamp() + delta);
  191. }
  192. }
  193. //==============================================================================
  194. void MidiMessageSequence::extractMidiChannelMessages (const int channelNumberToExtract,
  195. MidiMessageSequence& destSequence,
  196. const bool alsoIncludeMetaEvents) const
  197. {
  198. for (int i = 0; i < list.size(); ++i)
  199. {
  200. const MidiMessage& mm = list.getUnchecked(i)->message;
  201. if (mm.isForChannel (channelNumberToExtract) || (alsoIncludeMetaEvents && mm.isMetaEvent()))
  202. destSequence.addEvent (mm);
  203. }
  204. }
  205. void MidiMessageSequence::extractSysExMessages (MidiMessageSequence& destSequence) const
  206. {
  207. for (int i = 0; i < list.size(); ++i)
  208. {
  209. const MidiMessage& mm = list.getUnchecked(i)->message;
  210. if (mm.isSysEx())
  211. destSequence.addEvent (mm);
  212. }
  213. }
  214. void MidiMessageSequence::deleteMidiChannelMessages (const int channelNumberToRemove)
  215. {
  216. for (int i = list.size(); --i >= 0;)
  217. if (list.getUnchecked(i)->message.isForChannel (channelNumberToRemove))
  218. list.remove(i);
  219. }
  220. void MidiMessageSequence::deleteSysExMessages()
  221. {
  222. for (int i = list.size(); --i >= 0;)
  223. if (list.getUnchecked(i)->message.isSysEx())
  224. list.remove(i);
  225. }
  226. //==============================================================================
  227. void MidiMessageSequence::createControllerUpdatesForTime (const int channelNumber, const double time, Array<MidiMessage>& dest)
  228. {
  229. bool doneProg = false;
  230. bool donePitchWheel = false;
  231. bool doneControllers[128] = { 0 };
  232. for (int i = list.size(); --i >= 0;)
  233. {
  234. const MidiMessage& mm = list.getUnchecked(i)->message;
  235. if (mm.isForChannel (channelNumber) && mm.getTimeStamp() <= time)
  236. {
  237. if (mm.isProgramChange() && ! doneProg)
  238. {
  239. doneProg = true;
  240. dest.add (MidiMessage (mm, 0.0));
  241. }
  242. else if (mm.isPitchWheel() && ! donePitchWheel)
  243. {
  244. donePitchWheel = true;
  245. dest.add (MidiMessage (mm, 0.0));
  246. }
  247. else if (mm.isController())
  248. {
  249. const int controllerNumber = mm.getControllerNumber();
  250. jassert (isPositiveAndBelow (controllerNumber, 128));
  251. if (! doneControllers[controllerNumber])
  252. {
  253. doneControllers[controllerNumber] = true;
  254. dest.add (MidiMessage (mm, 0.0));
  255. }
  256. }
  257. }
  258. }
  259. }
  260. //==============================================================================
  261. MidiMessageSequence::MidiEventHolder::MidiEventHolder (const MidiMessage& mm)
  262. : message (mm), noteOffObject (nullptr)
  263. {
  264. }
  265. MidiMessageSequence::MidiEventHolder::~MidiEventHolder()
  266. {
  267. }