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.

871 lines
34KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - 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. //==============================================================================
  22. MidiMessageSequence::MidiMessageSequence()
  23. {
  24. }
  25. MidiMessageSequence::MidiMessageSequence (const MidiMessageSequence& other)
  26. {
  27. list.addCopiesOf (other.list);
  28. for (int i = 0; i < list.size(); ++i)
  29. {
  30. auto noteOffIndex = other.getIndexOfMatchingKeyUp (i);
  31. if (noteOffIndex >= 0)
  32. list.getUnchecked(i)->noteOffObject = list.getUnchecked (noteOffIndex);
  33. }
  34. }
  35. MidiMessageSequence& MidiMessageSequence::operator= (const MidiMessageSequence& other)
  36. {
  37. MidiMessageSequence otherCopy (other);
  38. swapWith (otherCopy);
  39. return *this;
  40. }
  41. MidiMessageSequence::MidiMessageSequence (MidiMessageSequence&& other) noexcept
  42. : list (std::move (other.list))
  43. {
  44. }
  45. MidiMessageSequence& MidiMessageSequence::operator= (MidiMessageSequence&& other) noexcept
  46. {
  47. list = std::move (other.list);
  48. return *this;
  49. }
  50. void MidiMessageSequence::swapWith (MidiMessageSequence& other) noexcept
  51. {
  52. list.swapWith (other.list);
  53. }
  54. void MidiMessageSequence::clear()
  55. {
  56. list.clear();
  57. }
  58. int MidiMessageSequence::getNumEvents() const noexcept
  59. {
  60. return list.size();
  61. }
  62. MidiMessageSequence::MidiEventHolder* MidiMessageSequence::getEventPointer (int index) const noexcept
  63. {
  64. return list[index];
  65. }
  66. MidiMessageSequence::MidiEventHolder** MidiMessageSequence::begin() noexcept { return list.begin(); }
  67. MidiMessageSequence::MidiEventHolder* const* MidiMessageSequence::begin() const noexcept { return list.begin(); }
  68. MidiMessageSequence::MidiEventHolder** MidiMessageSequence::end() noexcept { return list.end(); }
  69. MidiMessageSequence::MidiEventHolder* const* MidiMessageSequence::end() const noexcept { return list.end(); }
  70. double MidiMessageSequence::getTimeOfMatchingKeyUp (int index) const noexcept
  71. {
  72. if (auto* meh = list[index])
  73. if (auto* noteOff = meh->noteOffObject)
  74. return noteOff->message.getTimeStamp();
  75. return 0;
  76. }
  77. int MidiMessageSequence::getIndexOfMatchingKeyUp (int index) const noexcept
  78. {
  79. if (auto* meh = list[index])
  80. {
  81. if (auto* noteOff = meh->noteOffObject)
  82. {
  83. for (int i = index; i < list.size(); ++i)
  84. if (list.getUnchecked(i) == noteOff)
  85. return i;
  86. jassertfalse; // we've somehow got a pointer to a note-off object that isn't in the sequence
  87. }
  88. }
  89. return -1;
  90. }
  91. int MidiMessageSequence::getIndexOf (const MidiEventHolder* event) const noexcept
  92. {
  93. return list.indexOf (event);
  94. }
  95. int MidiMessageSequence::getNextIndexAtTime (double timeStamp) const noexcept
  96. {
  97. auto numEvents = list.size();
  98. int i;
  99. for (i = 0; i < numEvents; ++i)
  100. if (list.getUnchecked(i)->message.getTimeStamp() >= timeStamp)
  101. break;
  102. return i;
  103. }
  104. //==============================================================================
  105. double MidiMessageSequence::getStartTime() const noexcept
  106. {
  107. return getEventTime (0);
  108. }
  109. double MidiMessageSequence::getEndTime() const noexcept
  110. {
  111. return getEventTime (list.size() - 1);
  112. }
  113. double MidiMessageSequence::getEventTime (const int index) const noexcept
  114. {
  115. if (auto* meh = list[index])
  116. return meh->message.getTimeStamp();
  117. return 0;
  118. }
  119. //==============================================================================
  120. MidiMessageSequence::MidiEventHolder* MidiMessageSequence::addEvent (MidiEventHolder* newEvent, double timeAdjustment)
  121. {
  122. newEvent->message.addToTimeStamp (timeAdjustment);
  123. auto time = newEvent->message.getTimeStamp();
  124. int i;
  125. for (i = list.size(); --i >= 0;)
  126. if (list.getUnchecked(i)->message.getTimeStamp() <= time)
  127. break;
  128. list.insert (i + 1, newEvent);
  129. return newEvent;
  130. }
  131. MidiMessageSequence::MidiEventHolder* MidiMessageSequence::addEvent (const MidiMessage& newMessage, double timeAdjustment)
  132. {
  133. return addEvent (new MidiEventHolder (newMessage), timeAdjustment);
  134. }
  135. MidiMessageSequence::MidiEventHolder* MidiMessageSequence::addEvent (MidiMessage&& newMessage, double timeAdjustment)
  136. {
  137. return addEvent (new MidiEventHolder (std::move (newMessage)), timeAdjustment);
  138. }
  139. void MidiMessageSequence::deleteEvent (int index, bool deleteMatchingNoteUp)
  140. {
  141. if (isPositiveAndBelow (index, list.size()))
  142. {
  143. if (deleteMatchingNoteUp)
  144. deleteEvent (getIndexOfMatchingKeyUp (index), false);
  145. list.remove (index);
  146. }
  147. }
  148. void MidiMessageSequence::addSequence (const MidiMessageSequence& other, double timeAdjustment)
  149. {
  150. for (auto* m : other)
  151. {
  152. auto newOne = new MidiEventHolder (m->message);
  153. newOne->message.addToTimeStamp (timeAdjustment);
  154. list.add (newOne);
  155. }
  156. sort();
  157. }
  158. void MidiMessageSequence::addSequence (const MidiMessageSequence& other,
  159. double timeAdjustment,
  160. double firstAllowableTime,
  161. double endOfAllowableDestTimes)
  162. {
  163. for (auto* m : other)
  164. {
  165. auto t = m->message.getTimeStamp() + timeAdjustment;
  166. if (t >= firstAllowableTime && t < endOfAllowableDestTimes)
  167. {
  168. auto newOne = new MidiEventHolder (m->message);
  169. newOne->message.setTimeStamp (t);
  170. list.add (newOne);
  171. }
  172. }
  173. sort();
  174. }
  175. void MidiMessageSequence::sort() noexcept
  176. {
  177. std::stable_sort (list.begin(), list.end(),
  178. [] (const MidiEventHolder* a, const MidiEventHolder* b) { return a->message.getTimeStamp() < b->message.getTimeStamp(); });
  179. }
  180. void MidiMessageSequence::updateMatchedPairs() noexcept
  181. {
  182. for (int i = 0; i < list.size(); ++i)
  183. {
  184. auto* meh = list.getUnchecked(i);
  185. auto& m1 = meh->message;
  186. if (m1.isNoteOn())
  187. {
  188. meh->noteOffObject = nullptr;
  189. auto note = m1.getNoteNumber();
  190. auto chan = m1.getChannel();
  191. auto len = list.size();
  192. for (int j = i + 1; j < len; ++j)
  193. {
  194. auto* meh2 = list.getUnchecked(j);
  195. auto& m = meh2->message;
  196. if (m.getNoteNumber() == note && m.getChannel() == chan)
  197. {
  198. if (m.isNoteOff())
  199. {
  200. meh->noteOffObject = meh2;
  201. break;
  202. }
  203. if (m.isNoteOn())
  204. {
  205. auto newEvent = new MidiEventHolder (MidiMessage::noteOff (chan, note));
  206. list.insert (j, newEvent);
  207. newEvent->message.setTimeStamp (m.getTimeStamp());
  208. meh->noteOffObject = newEvent;
  209. break;
  210. }
  211. }
  212. }
  213. }
  214. }
  215. }
  216. void MidiMessageSequence::addTimeToMessages (double delta) noexcept
  217. {
  218. if (delta != 0)
  219. for (auto* m : list)
  220. m->message.addToTimeStamp (delta);
  221. }
  222. //==============================================================================
  223. void MidiMessageSequence::extractMidiChannelMessages (const int channelNumberToExtract,
  224. MidiMessageSequence& destSequence,
  225. const bool alsoIncludeMetaEvents) const
  226. {
  227. for (auto* meh : list)
  228. if (meh->message.isForChannel (channelNumberToExtract)
  229. || (alsoIncludeMetaEvents && meh->message.isMetaEvent()))
  230. destSequence.addEvent (meh->message);
  231. }
  232. void MidiMessageSequence::extractSysExMessages (MidiMessageSequence& destSequence) const
  233. {
  234. for (auto* meh : list)
  235. if (meh->message.isSysEx())
  236. destSequence.addEvent (meh->message);
  237. }
  238. void MidiMessageSequence::deleteMidiChannelMessages (const int channelNumberToRemove)
  239. {
  240. for (int i = list.size(); --i >= 0;)
  241. if (list.getUnchecked(i)->message.isForChannel (channelNumberToRemove))
  242. list.remove(i);
  243. }
  244. void MidiMessageSequence::deleteSysExMessages()
  245. {
  246. for (int i = list.size(); --i >= 0;)
  247. if (list.getUnchecked(i)->message.isSysEx())
  248. list.remove(i);
  249. }
  250. //==============================================================================
  251. class OptionalPitchWheel
  252. {
  253. Optional<int> value;
  254. public:
  255. void emit (int channel, Array<MidiMessage>& out) const
  256. {
  257. if (value.hasValue())
  258. out.add (MidiMessage::pitchWheel (channel, *value));
  259. }
  260. void set (int v)
  261. {
  262. value = v;
  263. }
  264. };
  265. class OptionalControllerValues
  266. {
  267. Optional<char> values[128];
  268. public:
  269. void emit (int channel, Array<MidiMessage>& out) const
  270. {
  271. for (auto it = std::begin (values); it != std::end (values); ++it)
  272. if (it->hasValue())
  273. out.add (MidiMessage::controllerEvent (channel, (int) std::distance (std::begin (values), it), **it));
  274. }
  275. void set (int controller, int value)
  276. {
  277. values[controller] = (char) value;
  278. }
  279. };
  280. class OptionalProgramChange
  281. {
  282. Optional<char> value, bankLSB, bankMSB;
  283. public:
  284. void emit (int channel, double time, Array<MidiMessage>& out) const
  285. {
  286. if (! value.hasValue())
  287. return;
  288. if (bankLSB.hasValue() && bankMSB.hasValue())
  289. {
  290. out.add (MidiMessage::controllerEvent (channel, 0x00, *bankMSB).withTimeStamp (time));
  291. out.add (MidiMessage::controllerEvent (channel, 0x20, *bankLSB).withTimeStamp (time));
  292. }
  293. out.add (MidiMessage::programChange (channel, *value).withTimeStamp (time));
  294. }
  295. // Returns true if this is a bank number change, and false otherwise.
  296. bool trySetBank (int controller, int v)
  297. {
  298. switch (controller)
  299. {
  300. case 0x00: bankMSB = (char) v; return true;
  301. case 0x20: bankLSB = (char) v; return true;
  302. }
  303. return false;
  304. }
  305. void setProgram (int v) { value = (char) v; }
  306. };
  307. class ParameterNumberState
  308. {
  309. enum class Kind { rpn, nrpn };
  310. Optional<char> newestRpnLsb, newestRpnMsb, newestNrpnLsb, newestNrpnMsb, lastSentLsb, lastSentMsb;
  311. Kind lastSentKind = Kind::rpn, newestKind = Kind::rpn;
  312. public:
  313. // If the effective parameter number has changed since the last time this function was called,
  314. // this will emit the current parameter in full (MSB and LSB).
  315. // This should be called before each data message (entry, increment, decrement: 0x06, 0x26, 0x60, 0x61)
  316. // to ensure that the data message operates on the correct parameter number.
  317. void sendIfNecessary (int channel, double time, Array<MidiMessage>& out)
  318. {
  319. const auto newestMsb = newestKind == Kind::rpn ? newestRpnMsb : newestNrpnMsb;
  320. const auto newestLsb = newestKind == Kind::rpn ? newestRpnLsb : newestNrpnLsb;
  321. auto lastSent = std::tie (lastSentKind, lastSentMsb, lastSentLsb);
  322. const auto newest = std::tie (newestKind, newestMsb, newestLsb);
  323. if (lastSent == newest || ! newestMsb.hasValue() || ! newestLsb.hasValue())
  324. return;
  325. out.add (MidiMessage::controllerEvent (channel, newestKind == Kind::rpn ? 0x65 : 0x63, *newestMsb).withTimeStamp (time));
  326. out.add (MidiMessage::controllerEvent (channel, newestKind == Kind::rpn ? 0x64 : 0x62, *newestLsb).withTimeStamp (time));
  327. lastSent = newest;
  328. }
  329. // Returns true if this is a parameter number change, and false otherwise.
  330. bool trySetProgramNumber (int controller, int value)
  331. {
  332. switch (controller)
  333. {
  334. case 0x65: newestRpnMsb = (char) value; newestKind = Kind::rpn; return true;
  335. case 0x64: newestRpnLsb = (char) value; newestKind = Kind::rpn; return true;
  336. case 0x63: newestNrpnMsb = (char) value; newestKind = Kind::nrpn; return true;
  337. case 0x62: newestNrpnLsb = (char) value; newestKind = Kind::nrpn; return true;
  338. }
  339. return false;
  340. }
  341. };
  342. void MidiMessageSequence::createControllerUpdatesForTime (int channel, double time, Array<MidiMessage>& dest)
  343. {
  344. OptionalProgramChange programChange;
  345. OptionalControllerValues controllers;
  346. OptionalPitchWheel pitchWheel;
  347. ParameterNumberState parameterNumberState;
  348. for (const auto& item : list)
  349. {
  350. const auto& mm = item->message;
  351. if (! (mm.isForChannel (channel) && mm.getTimeStamp() <= time))
  352. continue;
  353. if (mm.isController())
  354. {
  355. const auto num = mm.getControllerNumber();
  356. if (parameterNumberState.trySetProgramNumber (num, mm.getControllerValue()))
  357. continue;
  358. if (programChange.trySetBank (num, mm.getControllerValue()))
  359. continue;
  360. constexpr int passthroughs[] { 0x06, 0x26, 0x60, 0x61 };
  361. if (std::find (std::begin (passthroughs), std::end (passthroughs), num) != std::end (passthroughs))
  362. {
  363. parameterNumberState.sendIfNecessary (channel, mm.getTimeStamp(), dest);
  364. dest.add (mm);
  365. }
  366. else
  367. {
  368. controllers.set (num, mm.getControllerValue());
  369. }
  370. }
  371. else if (mm.isProgramChange())
  372. {
  373. programChange.setProgram (mm.getProgramChangeNumber());
  374. }
  375. else if (mm.isPitchWheel())
  376. {
  377. pitchWheel.set (mm.getPitchWheelValue());
  378. }
  379. }
  380. pitchWheel.emit (channel, dest);
  381. controllers.emit (channel, dest);
  382. // Also emits bank change messages if necessary.
  383. programChange.emit (channel, time, dest);
  384. // Set the parameter number to its final state.
  385. parameterNumberState.sendIfNecessary (channel, time, dest);
  386. }
  387. //==============================================================================
  388. //==============================================================================
  389. #if JUCE_UNIT_TESTS
  390. struct MidiMessageSequenceTest : public UnitTest
  391. {
  392. MidiMessageSequenceTest()
  393. : UnitTest ("MidiMessageSequence", UnitTestCategories::midi)
  394. {}
  395. void runTest() override
  396. {
  397. MidiMessageSequence s;
  398. s.addEvent (MidiMessage::noteOn (1, 60, 0.5f).withTimeStamp (0.0));
  399. s.addEvent (MidiMessage::noteOff (1, 60, 0.5f).withTimeStamp (4.0));
  400. s.addEvent (MidiMessage::noteOn (1, 30, 0.5f).withTimeStamp (2.0));
  401. s.addEvent (MidiMessage::noteOff (1, 30, 0.5f).withTimeStamp (8.0));
  402. beginTest ("Start & end time");
  403. expectEquals (s.getStartTime(), 0.0);
  404. expectEquals (s.getEndTime(), 8.0);
  405. expectEquals (s.getEventTime (1), 2.0);
  406. beginTest ("Matching note off & ons");
  407. s.updateMatchedPairs();
  408. expectEquals (s.getTimeOfMatchingKeyUp (0), 4.0);
  409. expectEquals (s.getTimeOfMatchingKeyUp (1), 8.0);
  410. expectEquals (s.getIndexOfMatchingKeyUp (0), 2);
  411. expectEquals (s.getIndexOfMatchingKeyUp (1), 3);
  412. beginTest ("Time & indices");
  413. expectEquals (s.getNextIndexAtTime (0.5), 1);
  414. expectEquals (s.getNextIndexAtTime (2.5), 2);
  415. expectEquals (s.getNextIndexAtTime (9.0), 4);
  416. beginTest ("Deleting events");
  417. s.deleteEvent (0, true);
  418. expectEquals (s.getNumEvents(), 2);
  419. beginTest ("Merging sequences");
  420. MidiMessageSequence s2;
  421. s2.addEvent (MidiMessage::noteOn (2, 25, 0.5f).withTimeStamp (0.0));
  422. s2.addEvent (MidiMessage::noteOn (2, 40, 0.5f).withTimeStamp (1.0));
  423. s2.addEvent (MidiMessage::noteOff (2, 40, 0.5f).withTimeStamp (5.0));
  424. s2.addEvent (MidiMessage::noteOn (2, 80, 0.5f).withTimeStamp (3.0));
  425. s2.addEvent (MidiMessage::noteOff (2, 80, 0.5f).withTimeStamp (7.0));
  426. s2.addEvent (MidiMessage::noteOff (2, 25, 0.5f).withTimeStamp (9.0));
  427. s.addSequence (s2, 0.0, 0.0, 8.0); // Intentionally cut off the last note off
  428. s.updateMatchedPairs();
  429. expectEquals (s.getNumEvents(), 7);
  430. expectEquals (s.getIndexOfMatchingKeyUp (0), -1); // Truncated note, should be no note off
  431. expectEquals (s.getTimeOfMatchingKeyUp (1), 5.0);
  432. struct ControlValue { int control, value; };
  433. struct DataEntry
  434. {
  435. int controllerBase, channel, parameter, value;
  436. double time;
  437. std::array<ControlValue, 4> getControlValues() const
  438. {
  439. return { { { controllerBase + 1, (parameter >> 7) & 0x7f },
  440. { controllerBase + 0, (parameter >> 0) & 0x7f },
  441. { 0x06, (value >> 7) & 0x7f },
  442. { 0x26, (value >> 0) & 0x7f } } };
  443. }
  444. void addToSequence (MidiMessageSequence& s) const
  445. {
  446. for (const auto& pair : getControlValues())
  447. s.addEvent (MidiMessage::controllerEvent (channel, pair.control, pair.value), time);
  448. }
  449. bool matches (const MidiMessage* begin, const MidiMessage* end) const
  450. {
  451. const auto isEqual = [this] (const ControlValue& cv, const MidiMessage& msg)
  452. {
  453. return msg.getTimeStamp() == time
  454. && msg.isController()
  455. && msg.getChannel() == channel
  456. && msg.getControllerNumber() == cv.control
  457. && msg.getControllerValue() == cv.value;
  458. };
  459. const auto pairs = getControlValues();
  460. return std::equal (pairs.begin(), pairs.end(), begin, end, isEqual);
  461. }
  462. };
  463. const auto addNrpn = [&] (MidiMessageSequence& seq, int channel, int parameter, int value, double time = 0.0)
  464. {
  465. DataEntry { 0x62, channel, parameter, value, time }.addToSequence (seq);
  466. };
  467. const auto addRpn = [&] (MidiMessageSequence& seq, int channel, int parameter, int value, double time = 0.0)
  468. {
  469. DataEntry { 0x64, channel, parameter, value, time }.addToSequence (seq);
  470. };
  471. const auto checkNrpn = [&] (const MidiMessage* begin, const MidiMessage* end, int channel, int parameter, int value, double time = 0.0)
  472. {
  473. expect (DataEntry { 0x62, channel, parameter, value, time }.matches (begin, end));
  474. };
  475. const auto checkRpn = [&] (const MidiMessage* begin, const MidiMessage* end, int channel, int parameter, int value, double time = 0.0)
  476. {
  477. expect (DataEntry { 0x64, channel, parameter, value, time }.matches (begin, end));
  478. };
  479. beginTest ("createControllerUpdatesForTime should emit (N)RPN components in the correct order");
  480. {
  481. const auto channel = 1;
  482. const auto number = 200;
  483. const auto value = 300;
  484. MidiMessageSequence sequence;
  485. addNrpn (sequence, channel, number, value);
  486. Array<MidiMessage> m;
  487. sequence.createControllerUpdatesForTime (channel, 1.0, m);
  488. checkNrpn (m.begin(), m.end(), channel, number, value);
  489. }
  490. beginTest ("createControllerUpdatesForTime ignores (N)RPNs after the final requested time");
  491. {
  492. const auto channel = 2;
  493. const auto number = 123;
  494. const auto value = 456;
  495. MidiMessageSequence sequence;
  496. addRpn (sequence, channel, number, value, 0.5);
  497. addRpn (sequence, channel, 111, 222, 1.5);
  498. addRpn (sequence, channel, 333, 444, 2.5);
  499. Array<MidiMessage> m;
  500. sequence.createControllerUpdatesForTime (channel, 1.0, m);
  501. checkRpn (m.begin(), std::next (m.begin(), 4), channel, number, value, 0.5);
  502. }
  503. beginTest ("createControllerUpdatesForTime should emit separate (N)RPN messages when appropriate");
  504. {
  505. const auto channel = 2;
  506. const auto numberA = 1111;
  507. const auto valueA = 9999;
  508. const auto numberB = 8888;
  509. const auto valueB = 2222;
  510. const auto numberC = 7777;
  511. const auto valueC = 3333;
  512. const auto numberD = 6666;
  513. const auto valueD = 4444;
  514. const auto time = 0.5;
  515. MidiMessageSequence sequence;
  516. addRpn (sequence, channel, numberA, valueA, time);
  517. addRpn (sequence, channel, numberB, valueB, time);
  518. addNrpn (sequence, channel, numberC, valueC, time);
  519. addNrpn (sequence, channel, numberD, valueD, time);
  520. Array<MidiMessage> m;
  521. sequence.createControllerUpdatesForTime (channel, time * 2, m);
  522. checkRpn (std::next (m.begin(), 0), std::next (m.begin(), 4), channel, numberA, valueA, time);
  523. checkRpn (std::next (m.begin(), 4), std::next (m.begin(), 8), channel, numberB, valueB, time);
  524. checkNrpn (std::next (m.begin(), 8), std::next (m.begin(), 12), channel, numberC, valueC, time);
  525. checkNrpn (std::next (m.begin(), 12), std::next (m.begin(), 16), channel, numberD, valueD, time);
  526. }
  527. beginTest ("createControllerUpdatesForTime correctly emits (N)RPN messages on multiple channels");
  528. {
  529. struct Info { int channel, number, value; };
  530. const Info infos[] { { 2, 1111, 9999 },
  531. { 8, 8888, 2222 },
  532. { 5, 7777, 3333 },
  533. { 1, 6666, 4444 } };
  534. const auto time = 0.5;
  535. MidiMessageSequence sequence;
  536. for (const auto& info : infos)
  537. addRpn (sequence, info.channel, info.number, info.value, time);
  538. for (const auto& info : infos)
  539. {
  540. Array<MidiMessage> m;
  541. sequence.createControllerUpdatesForTime (info.channel, time * 2, m);
  542. checkRpn (std::next (m.begin(), 0), std::next (m.begin(), 4), info.channel, info.number, info.value, time);
  543. }
  544. }
  545. const auto messagesAreEqual = [] (const MidiMessage& a, const MidiMessage& b)
  546. {
  547. return std::equal (a.getRawData(), a.getRawData() + a.getRawDataSize(),
  548. b.getRawData(), b.getRawData() + b.getRawDataSize());
  549. };
  550. beginTest ("createControllerUpdatesForTime sends bank select messages when the next program is in a new bank");
  551. {
  552. MidiMessageSequence sequence;
  553. const auto time = 0.0;
  554. const auto channel = 1;
  555. sequence.addEvent (MidiMessage::programChange (channel, 5), time);
  556. sequence.addEvent (MidiMessage::controllerEvent (channel, 0x00, 128), time);
  557. sequence.addEvent (MidiMessage::controllerEvent (channel, 0x20, 64), time);
  558. sequence.addEvent (MidiMessage::programChange (channel, 63), time);
  559. const Array<MidiMessage> finalEvents { MidiMessage::controllerEvent (channel, 0x00, 50),
  560. MidiMessage::controllerEvent (channel, 0x20, 40),
  561. MidiMessage::programChange (channel, 30) };
  562. for (const auto& e : finalEvents)
  563. sequence.addEvent (e);
  564. Array<MidiMessage> m;
  565. sequence.createControllerUpdatesForTime (channel, 1.0, m);
  566. expect (std::equal (m.begin(), m.end(), finalEvents.begin(), finalEvents.end(), messagesAreEqual));
  567. }
  568. beginTest ("createControllerUpdatesForTime preserves all Data Increment and Data Decrement messages");
  569. {
  570. MidiMessageSequence sequence;
  571. const auto time = 0.0;
  572. const auto channel = 1;
  573. const Array<MidiMessage> messages { MidiMessage::controllerEvent (channel, 0x60, 0),
  574. MidiMessage::controllerEvent (channel, 0x06, 100),
  575. MidiMessage::controllerEvent (channel, 0x26, 50),
  576. MidiMessage::controllerEvent (channel, 0x60, 10),
  577. MidiMessage::controllerEvent (channel, 0x61, 10),
  578. MidiMessage::controllerEvent (channel, 0x06, 20),
  579. MidiMessage::controllerEvent (channel, 0x26, 30),
  580. MidiMessage::controllerEvent (channel, 0x61, 10),
  581. MidiMessage::controllerEvent (channel, 0x61, 20) };
  582. for (const auto& m : messages)
  583. sequence.addEvent (m, time);
  584. Array<MidiMessage> m;
  585. sequence.createControllerUpdatesForTime (channel, 1.0, m);
  586. expect (std::equal (m.begin(), m.end(), messages.begin(), messages.end(), messagesAreEqual));
  587. }
  588. beginTest ("createControllerUpdatesForTime does not emit redundant parameter number changes");
  589. {
  590. MidiMessageSequence sequence;
  591. const auto time = 0.0;
  592. const auto channel = 1;
  593. const Array<MidiMessage> messages { MidiMessage::controllerEvent (channel, 0x65, 0),
  594. MidiMessage::controllerEvent (channel, 0x64, 100),
  595. MidiMessage::controllerEvent (channel, 0x63, 50),
  596. MidiMessage::controllerEvent (channel, 0x62, 10),
  597. MidiMessage::controllerEvent (channel, 0x06, 10) };
  598. for (const auto& m : messages)
  599. sequence.addEvent (m, time);
  600. Array<MidiMessage> m;
  601. sequence.createControllerUpdatesForTime (channel, 1.0, m);
  602. const Array<MidiMessage> expected { MidiMessage::controllerEvent (channel, 0x63, 50),
  603. MidiMessage::controllerEvent (channel, 0x62, 10),
  604. MidiMessage::controllerEvent (channel, 0x06, 10) };
  605. expect (std::equal (m.begin(), m.end(), expected.begin(), expected.end(), messagesAreEqual));
  606. }
  607. beginTest ("createControllerUpdatesForTime sets parameter number correctly at end of sequence");
  608. {
  609. MidiMessageSequence sequence;
  610. const auto time = 0.0;
  611. const auto channel = 1;
  612. const Array<MidiMessage> messages { MidiMessage::controllerEvent (channel, 0x65, 0),
  613. MidiMessage::controllerEvent (channel, 0x64, 100),
  614. MidiMessage::controllerEvent (channel, 0x63, 50),
  615. MidiMessage::controllerEvent (channel, 0x62, 10),
  616. MidiMessage::controllerEvent (channel, 0x06, 10),
  617. MidiMessage::controllerEvent (channel, 0x64, 5) };
  618. for (const auto& m : messages)
  619. sequence.addEvent (m, time);
  620. const auto finalTime = 1.0;
  621. Array<MidiMessage> m;
  622. sequence.createControllerUpdatesForTime (channel, finalTime, m);
  623. const Array<MidiMessage> expected { MidiMessage::controllerEvent (channel, 0x63, 50),
  624. MidiMessage::controllerEvent (channel, 0x62, 10),
  625. MidiMessage::controllerEvent (channel, 0x06, 10),
  626. // Note: we should send both the MSB and LSB!
  627. MidiMessage::controllerEvent (channel, 0x65, 0).withTimeStamp (finalTime),
  628. MidiMessage::controllerEvent (channel, 0x64, 5).withTimeStamp (finalTime) };
  629. expect (std::equal (m.begin(), m.end(), expected.begin(), expected.end(), messagesAreEqual));
  630. }
  631. beginTest ("createControllerUpdatesForTime does not emit duplicate parameter number change messages");
  632. {
  633. MidiMessageSequence sequence;
  634. const auto time = 0.0;
  635. const auto channel = 1;
  636. const Array<MidiMessage> messages { MidiMessage::controllerEvent (channel, 0x65, 1),
  637. MidiMessage::controllerEvent (channel, 0x64, 2),
  638. MidiMessage::controllerEvent (channel, 0x63, 3),
  639. MidiMessage::controllerEvent (channel, 0x62, 4),
  640. MidiMessage::controllerEvent (channel, 0x06, 10),
  641. MidiMessage::controllerEvent (channel, 0x63, 30),
  642. MidiMessage::controllerEvent (channel, 0x62, 40),
  643. MidiMessage::controllerEvent (channel, 0x63, 3),
  644. MidiMessage::controllerEvent (channel, 0x62, 4),
  645. MidiMessage::controllerEvent (channel, 0x60, 5),
  646. MidiMessage::controllerEvent (channel, 0x65, 10) };
  647. for (const auto& m : messages)
  648. sequence.addEvent (m, time);
  649. const auto finalTime = 1.0;
  650. Array<MidiMessage> m;
  651. sequence.createControllerUpdatesForTime (channel, finalTime, m);
  652. const Array<MidiMessage> expected { MidiMessage::controllerEvent (channel, 0x63, 3),
  653. MidiMessage::controllerEvent (channel, 0x62, 4),
  654. MidiMessage::controllerEvent (channel, 0x06, 10),
  655. // Parameter number is set to (30, 40) then back to (3, 4),
  656. // so there is no need to resend it
  657. MidiMessage::controllerEvent (channel, 0x60, 5),
  658. // Set parameter number to final value
  659. MidiMessage::controllerEvent (channel, 0x65, 10).withTimeStamp (finalTime),
  660. MidiMessage::controllerEvent (channel, 0x64, 2) .withTimeStamp (finalTime) };
  661. expect (std::equal (m.begin(), m.end(), expected.begin(), expected.end(), messagesAreEqual));
  662. }
  663. beginTest ("createControllerUpdatesForTime emits bank change messages immediately before program change");
  664. {
  665. MidiMessageSequence sequence;
  666. const auto time = 0.0;
  667. const auto channel = 1;
  668. const Array<MidiMessage> messages { MidiMessage::controllerEvent (channel, 0x00, 1),
  669. MidiMessage::controllerEvent (channel, 0x20, 2),
  670. MidiMessage::controllerEvent (channel, 0x65, 0),
  671. MidiMessage::controllerEvent (channel, 0x64, 0),
  672. MidiMessage::programChange (channel, 5) };
  673. for (const auto& m : messages)
  674. sequence.addEvent (m, time);
  675. const auto finalTime = 1.0;
  676. Array<MidiMessage> m;
  677. sequence.createControllerUpdatesForTime (channel, finalTime, m);
  678. const Array<MidiMessage> expected { MidiMessage::controllerEvent (channel, 0x00, 1),
  679. MidiMessage::controllerEvent (channel, 0x20, 2),
  680. MidiMessage::programChange (channel, 5),
  681. MidiMessage::controllerEvent (channel, 0x65, 0).withTimeStamp (finalTime),
  682. MidiMessage::controllerEvent (channel, 0x64, 0).withTimeStamp (finalTime) };
  683. expect (std::equal (m.begin(), m.end(), expected.begin(), expected.end(), messagesAreEqual));
  684. }
  685. }
  686. };
  687. static MidiMessageSequenceTest midiMessageSequenceTests;
  688. #endif
  689. } // namespace juce