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 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - Raw Material Software Limited
  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 (std::move (mm)) {}
  21. MidiMessageSequence::MidiEventHolder::~MidiEventHolder() {}
  22. //==============================================================================
  23. MidiMessageSequence::MidiMessageSequence()
  24. {
  25. }
  26. MidiMessageSequence::MidiMessageSequence (const MidiMessageSequence& other)
  27. {
  28. list.addCopiesOf (other.list);
  29. for (int i = 0; i < list.size(); ++i)
  30. {
  31. auto noteOffIndex = other.getIndexOfMatchingKeyUp (i);
  32. if (noteOffIndex >= 0)
  33. list.getUnchecked(i)->noteOffObject = list.getUnchecked (noteOffIndex);
  34. }
  35. }
  36. MidiMessageSequence& MidiMessageSequence::operator= (const MidiMessageSequence& other)
  37. {
  38. MidiMessageSequence otherCopy (other);
  39. swapWith (otherCopy);
  40. return *this;
  41. }
  42. MidiMessageSequence::MidiMessageSequence (MidiMessageSequence&& other) noexcept
  43. : list (std::move (other.list))
  44. {
  45. }
  46. MidiMessageSequence& MidiMessageSequence::operator= (MidiMessageSequence&& other) noexcept
  47. {
  48. list = std::move (other.list);
  49. return *this;
  50. }
  51. MidiMessageSequence::~MidiMessageSequence()
  52. {
  53. }
  54. void MidiMessageSequence::swapWith (MidiMessageSequence& other) noexcept
  55. {
  56. list.swapWith (other.list);
  57. }
  58. void MidiMessageSequence::clear()
  59. {
  60. list.clear();
  61. }
  62. int MidiMessageSequence::getNumEvents() const noexcept
  63. {
  64. return list.size();
  65. }
  66. MidiMessageSequence::MidiEventHolder* MidiMessageSequence::getEventPointer (int index) const noexcept
  67. {
  68. return list[index];
  69. }
  70. MidiMessageSequence::MidiEventHolder** MidiMessageSequence::begin() noexcept { return list.begin(); }
  71. MidiMessageSequence::MidiEventHolder* const* MidiMessageSequence::begin() const noexcept { return list.begin(); }
  72. MidiMessageSequence::MidiEventHolder** MidiMessageSequence::end() noexcept { return list.end(); }
  73. MidiMessageSequence::MidiEventHolder* const* MidiMessageSequence::end() const noexcept { return list.end(); }
  74. double MidiMessageSequence::getTimeOfMatchingKeyUp (int index) const noexcept
  75. {
  76. if (auto* meh = list[index])
  77. if (auto* noteOff = meh->noteOffObject)
  78. return noteOff->message.getTimeStamp();
  79. return 0;
  80. }
  81. int MidiMessageSequence::getIndexOfMatchingKeyUp (int index) const noexcept
  82. {
  83. if (auto* meh = list[index])
  84. {
  85. if (auto* noteOff = meh->noteOffObject)
  86. {
  87. for (int i = index; i < list.size(); ++i)
  88. if (list.getUnchecked(i) == noteOff)
  89. return i;
  90. jassertfalse; // we've somehow got a pointer to a note-off object that isn't in the sequence
  91. }
  92. }
  93. return -1;
  94. }
  95. int MidiMessageSequence::getIndexOf (const MidiEventHolder* event) const noexcept
  96. {
  97. return list.indexOf (event);
  98. }
  99. int MidiMessageSequence::getNextIndexAtTime (double timeStamp) const noexcept
  100. {
  101. auto numEvents = list.size();
  102. int i;
  103. for (i = 0; i < numEvents; ++i)
  104. if (list.getUnchecked(i)->message.getTimeStamp() >= timeStamp)
  105. break;
  106. return i;
  107. }
  108. //==============================================================================
  109. double MidiMessageSequence::getStartTime() const noexcept
  110. {
  111. return getEventTime (0);
  112. }
  113. double MidiMessageSequence::getEndTime() const noexcept
  114. {
  115. return getEventTime (list.size() - 1);
  116. }
  117. double MidiMessageSequence::getEventTime (const int index) const noexcept
  118. {
  119. if (auto* meh = list[index])
  120. return meh->message.getTimeStamp();
  121. return 0;
  122. }
  123. //==============================================================================
  124. MidiMessageSequence::MidiEventHolder* MidiMessageSequence::addEvent (MidiEventHolder* newEvent, double timeAdjustment)
  125. {
  126. newEvent->message.addToTimeStamp (timeAdjustment);
  127. auto time = newEvent->message.getTimeStamp();
  128. int i;
  129. for (i = list.size(); --i >= 0;)
  130. if (list.getUnchecked(i)->message.getTimeStamp() <= time)
  131. break;
  132. list.insert (i + 1, newEvent);
  133. return newEvent;
  134. }
  135. MidiMessageSequence::MidiEventHolder* MidiMessageSequence::addEvent (const MidiMessage& newMessage, double timeAdjustment)
  136. {
  137. return addEvent (new MidiEventHolder (newMessage), timeAdjustment);
  138. }
  139. MidiMessageSequence::MidiEventHolder* MidiMessageSequence::addEvent (MidiMessage&& newMessage, double timeAdjustment)
  140. {
  141. return addEvent (new MidiEventHolder (std::move (newMessage)), timeAdjustment);
  142. }
  143. void MidiMessageSequence::deleteEvent (int index, bool deleteMatchingNoteUp)
  144. {
  145. if (isPositiveAndBelow (index, list.size()))
  146. {
  147. if (deleteMatchingNoteUp)
  148. deleteEvent (getIndexOfMatchingKeyUp (index), false);
  149. list.remove (index);
  150. }
  151. }
  152. void MidiMessageSequence::addSequence (const MidiMessageSequence& other, double timeAdjustment)
  153. {
  154. for (auto* m : other)
  155. {
  156. auto newOne = new MidiEventHolder (m->message);
  157. newOne->message.addToTimeStamp (timeAdjustment);
  158. list.add (newOne);
  159. }
  160. sort();
  161. }
  162. void MidiMessageSequence::addSequence (const MidiMessageSequence& other,
  163. double timeAdjustment,
  164. double firstAllowableTime,
  165. double endOfAllowableDestTimes)
  166. {
  167. for (auto* m : other)
  168. {
  169. auto t = m->message.getTimeStamp() + timeAdjustment;
  170. if (t >= firstAllowableTime && t < endOfAllowableDestTimes)
  171. {
  172. auto newOne = new MidiEventHolder (m->message);
  173. newOne->message.setTimeStamp (t);
  174. list.add (newOne);
  175. }
  176. }
  177. sort();
  178. }
  179. void MidiMessageSequence::sort() noexcept
  180. {
  181. std::stable_sort (list.begin(), list.end(),
  182. [] (const MidiEventHolder* a, const MidiEventHolder* b) { return a->message.getTimeStamp() < b->message.getTimeStamp(); });
  183. }
  184. void MidiMessageSequence::updateMatchedPairs() noexcept
  185. {
  186. for (int i = 0; i < list.size(); ++i)
  187. {
  188. auto* meh = list.getUnchecked(i);
  189. auto& m1 = meh->message;
  190. if (m1.isNoteOn())
  191. {
  192. meh->noteOffObject = nullptr;
  193. auto note = m1.getNoteNumber();
  194. auto chan = m1.getChannel();
  195. auto len = list.size();
  196. for (int j = i + 1; j < len; ++j)
  197. {
  198. auto* meh2 = list.getUnchecked(j);
  199. auto& m = meh2->message;
  200. if (m.getNoteNumber() == note && m.getChannel() == chan)
  201. {
  202. if (m.isNoteOff())
  203. {
  204. meh->noteOffObject = meh2;
  205. break;
  206. }
  207. if (m.isNoteOn())
  208. {
  209. auto newEvent = new MidiEventHolder (MidiMessage::noteOff (chan, note));
  210. list.insert (j, newEvent);
  211. newEvent->message.setTimeStamp (m.getTimeStamp());
  212. meh->noteOffObject = newEvent;
  213. break;
  214. }
  215. }
  216. }
  217. }
  218. }
  219. }
  220. void MidiMessageSequence::addTimeToMessages (double delta) noexcept
  221. {
  222. if (delta != 0)
  223. for (auto* m : list)
  224. m->message.addToTimeStamp (delta);
  225. }
  226. //==============================================================================
  227. void MidiMessageSequence::extractMidiChannelMessages (const int channelNumberToExtract,
  228. MidiMessageSequence& destSequence,
  229. const bool alsoIncludeMetaEvents) const
  230. {
  231. for (auto* meh : list)
  232. if (meh->message.isForChannel (channelNumberToExtract)
  233. || (alsoIncludeMetaEvents && meh->message.isMetaEvent()))
  234. destSequence.addEvent (meh->message);
  235. }
  236. void MidiMessageSequence::extractSysExMessages (MidiMessageSequence& destSequence) const
  237. {
  238. for (auto* meh : list)
  239. if (meh->message.isSysEx())
  240. destSequence.addEvent (meh->message);
  241. }
  242. void MidiMessageSequence::deleteMidiChannelMessages (const int channelNumberToRemove)
  243. {
  244. for (int i = list.size(); --i >= 0;)
  245. if (list.getUnchecked(i)->message.isForChannel (channelNumberToRemove))
  246. list.remove(i);
  247. }
  248. void MidiMessageSequence::deleteSysExMessages()
  249. {
  250. for (int i = list.size(); --i >= 0;)
  251. if (list.getUnchecked(i)->message.isSysEx())
  252. list.remove(i);
  253. }
  254. //==============================================================================
  255. void MidiMessageSequence::createControllerUpdatesForTime (int channelNumber, double time, Array<MidiMessage>& dest)
  256. {
  257. bool doneProg = false;
  258. bool donePitchWheel = false;
  259. bool doneControllers[128] = {};
  260. for (int i = list.size(); --i >= 0;)
  261. {
  262. auto& mm = list.getUnchecked(i)->message;
  263. if (mm.isForChannel (channelNumber) && mm.getTimeStamp() <= time)
  264. {
  265. if (mm.isProgramChange() && ! doneProg)
  266. {
  267. doneProg = true;
  268. dest.add (MidiMessage (mm, 0.0));
  269. }
  270. else if (mm.isPitchWheel() && ! donePitchWheel)
  271. {
  272. donePitchWheel = true;
  273. dest.add (MidiMessage (mm, 0.0));
  274. }
  275. else if (mm.isController())
  276. {
  277. auto controllerNumber = mm.getControllerNumber();
  278. jassert (isPositiveAndBelow (controllerNumber, 128));
  279. if (! doneControllers[controllerNumber])
  280. {
  281. doneControllers[controllerNumber] = true;
  282. dest.add (MidiMessage (mm, 0.0));
  283. }
  284. }
  285. }
  286. }
  287. }
  288. //==============================================================================
  289. //==============================================================================
  290. #if JUCE_UNIT_TESTS
  291. struct MidiMessageSequenceTest : public UnitTest
  292. {
  293. MidiMessageSequenceTest()
  294. : UnitTest ("MidiMessageSequence", UnitTestCategories::midi)
  295. {}
  296. void runTest() override
  297. {
  298. MidiMessageSequence s;
  299. s.addEvent (MidiMessage::noteOn (1, 60, 0.5f).withTimeStamp (0.0));
  300. s.addEvent (MidiMessage::noteOff (1, 60, 0.5f).withTimeStamp (4.0));
  301. s.addEvent (MidiMessage::noteOn (1, 30, 0.5f).withTimeStamp (2.0));
  302. s.addEvent (MidiMessage::noteOff (1, 30, 0.5f).withTimeStamp (8.0));
  303. beginTest ("Start & end time");
  304. expectEquals (s.getStartTime(), 0.0);
  305. expectEquals (s.getEndTime(), 8.0);
  306. expectEquals (s.getEventTime (1), 2.0);
  307. beginTest ("Matching note off & ons");
  308. s.updateMatchedPairs();
  309. expectEquals (s.getTimeOfMatchingKeyUp (0), 4.0);
  310. expectEquals (s.getTimeOfMatchingKeyUp (1), 8.0);
  311. expectEquals (s.getIndexOfMatchingKeyUp (0), 2);
  312. expectEquals (s.getIndexOfMatchingKeyUp (1), 3);
  313. beginTest ("Time & indices");
  314. expectEquals (s.getNextIndexAtTime (0.5), 1);
  315. expectEquals (s.getNextIndexAtTime (2.5), 2);
  316. expectEquals (s.getNextIndexAtTime (9.0), 4);
  317. beginTest ("Deleting events");
  318. s.deleteEvent (0, true);
  319. expectEquals (s.getNumEvents(), 2);
  320. beginTest ("Merging sequences");
  321. MidiMessageSequence s2;
  322. s2.addEvent (MidiMessage::noteOn (2, 25, 0.5f).withTimeStamp (0.0));
  323. s2.addEvent (MidiMessage::noteOn (2, 40, 0.5f).withTimeStamp (1.0));
  324. s2.addEvent (MidiMessage::noteOff (2, 40, 0.5f).withTimeStamp (5.0));
  325. s2.addEvent (MidiMessage::noteOn (2, 80, 0.5f).withTimeStamp (3.0));
  326. s2.addEvent (MidiMessage::noteOff (2, 80, 0.5f).withTimeStamp (7.0));
  327. s2.addEvent (MidiMessage::noteOff (2, 25, 0.5f).withTimeStamp (9.0));
  328. s.addSequence (s2, 0.0, 0.0, 8.0); // Intentionally cut off the last note off
  329. s.updateMatchedPairs();
  330. expectEquals (s.getNumEvents(), 7);
  331. expectEquals (s.getIndexOfMatchingKeyUp (0), -1); // Truncated note, should be no note off
  332. expectEquals (s.getTimeOfMatchingKeyUp (1), 5.0);
  333. }
  334. };
  335. static MidiMessageSequenceTest midiMessageSequenceTests;
  336. #endif
  337. } // namespace juce