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.

juce_MidiMessageSequence.cpp 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. The code included in this file is provided under the terms of the ISC license
  8. http://www.isc.org/downloads/software-support-policy/isc-license. Permission
  9. To use, copy, modify, and/or distribute this software for any purpose with or
  10. without fee is hereby granted provided that the above copyright notice and
  11. this permission notice appear in all copies.
  12. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  13. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  14. DISCLAIMED.
  15. ==============================================================================
  16. */
  17. namespace juce
  18. {
  19. MidiMessageSequence::MidiEventHolder::MidiEventHolder (const MidiMessage& mm) : message (mm) {}
  20. MidiMessageSequence::MidiEventHolder::MidiEventHolder (MidiMessage&& mm) : message (static_cast<MidiMessage&&> (mm)) {}
  21. MidiMessageSequence::MidiEventHolder::~MidiEventHolder() {}
  22. //==============================================================================
  23. MidiMessageSequence::MidiMessageSequence()
  24. {
  25. }
  26. MidiMessageSequence::MidiMessageSequence (const MidiMessageSequence& other)
  27. {
  28. list.addCopiesOf (other.list);
  29. updateMatchedPairs();
  30. }
  31. MidiMessageSequence& MidiMessageSequence::operator= (const MidiMessageSequence& other)
  32. {
  33. MidiMessageSequence otherCopy (other);
  34. swapWith (otherCopy);
  35. return *this;
  36. }
  37. MidiMessageSequence::MidiMessageSequence (MidiMessageSequence&& other) noexcept
  38. : list (static_cast<OwnedArray<MidiEventHolder>&&> (other.list))
  39. {}
  40. MidiMessageSequence& MidiMessageSequence::operator= (MidiMessageSequence&& other) noexcept
  41. {
  42. list = static_cast<OwnedArray<MidiEventHolder>&&> (other.list);
  43. return *this;
  44. }
  45. MidiMessageSequence::~MidiMessageSequence()
  46. {
  47. }
  48. void MidiMessageSequence::swapWith (MidiMessageSequence& other) noexcept
  49. {
  50. list.swapWith (other.list);
  51. }
  52. void MidiMessageSequence::clear()
  53. {
  54. list.clear();
  55. }
  56. int MidiMessageSequence::getNumEvents() const noexcept
  57. {
  58. return list.size();
  59. }
  60. MidiMessageSequence::MidiEventHolder* MidiMessageSequence::getEventPointer (int index) const noexcept
  61. {
  62. return list[index];
  63. }
  64. MidiMessageSequence::MidiEventHolder** MidiMessageSequence::begin() const noexcept { return list.begin(); }
  65. MidiMessageSequence::MidiEventHolder** MidiMessageSequence::end() const noexcept { return list.end(); }
  66. double MidiMessageSequence::getTimeOfMatchingKeyUp (int index) const noexcept
  67. {
  68. if (auto* meh = list[index])
  69. if (meh->noteOffObject != nullptr)
  70. return meh->noteOffObject->message.getTimeStamp();
  71. return 0.0;
  72. }
  73. int MidiMessageSequence::getIndexOfMatchingKeyUp (int index) const noexcept
  74. {
  75. if (auto* meh = list [index])
  76. return list.indexOf (meh->noteOffObject);
  77. return -1;
  78. }
  79. int MidiMessageSequence::getIndexOf (const MidiEventHolder* event) const noexcept
  80. {
  81. return list.indexOf (event);
  82. }
  83. int MidiMessageSequence::getNextIndexAtTime (double timeStamp) const noexcept
  84. {
  85. const int numEvents = list.size();
  86. int i;
  87. for (i = 0; i < numEvents; ++i)
  88. if (list.getUnchecked(i)->message.getTimeStamp() >= timeStamp)
  89. break;
  90. return i;
  91. }
  92. //==============================================================================
  93. double MidiMessageSequence::getStartTime() const noexcept
  94. {
  95. return getEventTime (0);
  96. }
  97. double MidiMessageSequence::getEndTime() const noexcept
  98. {
  99. return getEventTime (list.size() - 1);
  100. }
  101. double MidiMessageSequence::getEventTime (const int index) const noexcept
  102. {
  103. if (auto* meh = list [index])
  104. return meh->message.getTimeStamp();
  105. return 0.0;
  106. }
  107. //==============================================================================
  108. MidiMessageSequence::MidiEventHolder* MidiMessageSequence::addEvent (MidiEventHolder* newEvent, double timeAdjustment)
  109. {
  110. newEvent->message.addToTimeStamp (timeAdjustment);
  111. auto time = newEvent->message.getTimeStamp();
  112. int i;
  113. for (i = list.size(); --i >= 0;)
  114. if (list.getUnchecked(i)->message.getTimeStamp() <= time)
  115. break;
  116. list.insert (i + 1, newEvent);
  117. return newEvent;
  118. }
  119. MidiMessageSequence::MidiEventHolder* MidiMessageSequence::addEvent (const MidiMessage& newMessage, double timeAdjustment)
  120. {
  121. return addEvent (new MidiEventHolder (newMessage), timeAdjustment);
  122. }
  123. MidiMessageSequence::MidiEventHolder* MidiMessageSequence::addEvent (MidiMessage&& newMessage, double timeAdjustment)
  124. {
  125. return addEvent (new MidiEventHolder (static_cast<MidiMessage&&> (newMessage)), timeAdjustment);
  126. }
  127. void MidiMessageSequence::deleteEvent (int index, bool deleteMatchingNoteUp)
  128. {
  129. if (isPositiveAndBelow (index, list.size()))
  130. {
  131. if (deleteMatchingNoteUp)
  132. deleteEvent (getIndexOfMatchingKeyUp (index), false);
  133. list.remove (index);
  134. }
  135. }
  136. void MidiMessageSequence::addSequence (const MidiMessageSequence& other, double timeAdjustment)
  137. {
  138. for (auto* m : other)
  139. {
  140. auto newOne = new MidiEventHolder (m->message);
  141. newOne->message.addToTimeStamp (timeAdjustment);
  142. list.add (newOne);
  143. }
  144. sort();
  145. }
  146. void MidiMessageSequence::addSequence (const MidiMessageSequence& other,
  147. double timeAdjustment,
  148. double firstAllowableTime,
  149. double endOfAllowableDestTimes)
  150. {
  151. for (auto* m : other)
  152. {
  153. auto t = m->message.getTimeStamp() + timeAdjustment;
  154. if (t >= firstAllowableTime && t < endOfAllowableDestTimes)
  155. {
  156. auto newOne = new MidiEventHolder (m->message);
  157. newOne->message.setTimeStamp (t);
  158. list.add (newOne);
  159. }
  160. }
  161. sort();
  162. }
  163. struct MidiMessageSequenceSorter
  164. {
  165. static int compareElements (const MidiMessageSequence::MidiEventHolder* first,
  166. const MidiMessageSequence::MidiEventHolder* second) noexcept
  167. {
  168. auto diff = first->message.getTimeStamp() - second->message.getTimeStamp();
  169. return (diff > 0) - (diff < 0);
  170. }
  171. };
  172. void MidiMessageSequence::sort() noexcept
  173. {
  174. MidiMessageSequenceSorter sorter;
  175. list.sort (sorter, true);
  176. }
  177. void MidiMessageSequence::updateMatchedPairs() noexcept
  178. {
  179. for (int i = 0; i < list.size(); ++i)
  180. {
  181. auto* meh = list.getUnchecked(i);
  182. auto& m1 = meh->message;
  183. if (m1.isNoteOn())
  184. {
  185. meh->noteOffObject = nullptr;
  186. auto note = m1.getNoteNumber();
  187. auto chan = m1.getChannel();
  188. auto len = list.size();
  189. for (int j = i + 1; j < len; ++j)
  190. {
  191. auto* meh2 = list.getUnchecked(j);
  192. auto& m = meh2->message;
  193. if (m.getNoteNumber() == note && m.getChannel() == chan)
  194. {
  195. if (m.isNoteOff())
  196. {
  197. meh->noteOffObject = meh2;
  198. break;
  199. }
  200. if (m.isNoteOn())
  201. {
  202. auto newEvent = new MidiEventHolder (MidiMessage::noteOff (chan, note));
  203. list.insert (j, newEvent);
  204. newEvent->message.setTimeStamp (m.getTimeStamp());
  205. meh->noteOffObject = newEvent;
  206. break;
  207. }
  208. }
  209. }
  210. }
  211. }
  212. }
  213. void MidiMessageSequence::addTimeToMessages (double delta) noexcept
  214. {
  215. if (delta != 0)
  216. for (auto* m : list)
  217. m->message.addToTimeStamp (delta);
  218. }
  219. //==============================================================================
  220. void MidiMessageSequence::extractMidiChannelMessages (const int channelNumberToExtract,
  221. MidiMessageSequence& destSequence,
  222. const bool alsoIncludeMetaEvents) const
  223. {
  224. for (auto* meh : list)
  225. if (meh->message.isForChannel (channelNumberToExtract)
  226. || (alsoIncludeMetaEvents && meh->message.isMetaEvent()))
  227. destSequence.addEvent (meh->message);
  228. }
  229. void MidiMessageSequence::extractSysExMessages (MidiMessageSequence& destSequence) const
  230. {
  231. for (auto* meh : list)
  232. if (meh->message.isSysEx())
  233. destSequence.addEvent (meh->message);
  234. }
  235. void MidiMessageSequence::deleteMidiChannelMessages (const int channelNumberToRemove)
  236. {
  237. for (int i = list.size(); --i >= 0;)
  238. if (list.getUnchecked(i)->message.isForChannel (channelNumberToRemove))
  239. list.remove(i);
  240. }
  241. void MidiMessageSequence::deleteSysExMessages()
  242. {
  243. for (int i = list.size(); --i >= 0;)
  244. if (list.getUnchecked(i)->message.isSysEx())
  245. list.remove(i);
  246. }
  247. //==============================================================================
  248. void MidiMessageSequence::createControllerUpdatesForTime (int channelNumber, double time, Array<MidiMessage>& dest)
  249. {
  250. bool doneProg = false;
  251. bool donePitchWheel = false;
  252. bool doneControllers[128] = {};
  253. for (int i = list.size(); --i >= 0;)
  254. {
  255. auto& mm = list.getUnchecked(i)->message;
  256. if (mm.isForChannel (channelNumber) && mm.getTimeStamp() <= time)
  257. {
  258. if (mm.isProgramChange() && ! doneProg)
  259. {
  260. doneProg = true;
  261. dest.add (MidiMessage (mm, 0.0));
  262. }
  263. else if (mm.isPitchWheel() && ! donePitchWheel)
  264. {
  265. donePitchWheel = true;
  266. dest.add (MidiMessage (mm, 0.0));
  267. }
  268. else if (mm.isController())
  269. {
  270. const int controllerNumber = mm.getControllerNumber();
  271. jassert (isPositiveAndBelow (controllerNumber, 128));
  272. if (! doneControllers[controllerNumber])
  273. {
  274. doneControllers[controllerNumber] = true;
  275. dest.add (MidiMessage (mm, 0.0));
  276. }
  277. }
  278. }
  279. }
  280. }
  281. } // namespace juce