The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

406 lines
13KB

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