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.

416 lines
15KB

  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. MPEZoneLayout::MPEZoneLayout() noexcept
  20. {
  21. }
  22. MPEZoneLayout::MPEZoneLayout (const MPEZoneLayout& other)
  23. : zones (other.zones)
  24. {
  25. }
  26. MPEZoneLayout& MPEZoneLayout::operator= (const MPEZoneLayout& other)
  27. {
  28. zones = other.zones;
  29. sendLayoutChangeMessage();
  30. return *this;
  31. }
  32. void MPEZoneLayout::sendLayoutChangeMessage()
  33. {
  34. listeners.call ([this] (Listener& l) { l.zoneLayoutChanged (*this); });
  35. }
  36. //==============================================================================
  37. bool MPEZoneLayout::addZone (MPEZone newZone)
  38. {
  39. bool noOtherZonesModified = true;
  40. for (int i = zones.size(); --i >= 0;)
  41. {
  42. auto& zone = zones.getReference (i);
  43. if (zone.overlapsWith (newZone))
  44. {
  45. if (! zone.truncateToFit (newZone))
  46. zones.removeRange (i, 1);
  47. // can't use zones.remove (i) because that requires a default c'tor :-(
  48. noOtherZonesModified = false;
  49. }
  50. }
  51. zones.add (newZone);
  52. sendLayoutChangeMessage();
  53. return noOtherZonesModified;
  54. }
  55. //==============================================================================
  56. int MPEZoneLayout::getNumZones() const noexcept
  57. {
  58. return zones.size();
  59. }
  60. const MPEZone* MPEZoneLayout::getZoneByIndex (int index) const noexcept
  61. {
  62. if (zones.size() < index)
  63. return nullptr;
  64. return &(zones.getReference (index));
  65. }
  66. MPEZone* MPEZoneLayout::getZoneByIndex (int index) noexcept
  67. {
  68. return const_cast<MPEZone*> (static_cast<const MPEZoneLayout&> (*this).getZoneByIndex (index));
  69. }
  70. void MPEZoneLayout::clearAllZones()
  71. {
  72. zones.clear();
  73. sendLayoutChangeMessage();
  74. }
  75. //==============================================================================
  76. void MPEZoneLayout::processNextMidiEvent (const MidiMessage& message)
  77. {
  78. if (! message.isController())
  79. return;
  80. MidiRPNMessage rpn;
  81. if (rpnDetector.parseControllerMessage (message.getChannel(),
  82. message.getControllerNumber(),
  83. message.getControllerValue(),
  84. rpn))
  85. {
  86. processRpnMessage (rpn);
  87. }
  88. }
  89. void MPEZoneLayout::processRpnMessage (MidiRPNMessage rpn)
  90. {
  91. if (rpn.parameterNumber == MPEMessages::zoneLayoutMessagesRpnNumber)
  92. processZoneLayoutRpnMessage (rpn);
  93. else if (rpn.parameterNumber == 0)
  94. processPitchbendRangeRpnMessage (rpn);
  95. }
  96. void MPEZoneLayout::processZoneLayoutRpnMessage (MidiRPNMessage rpn)
  97. {
  98. if (rpn.value < 16)
  99. addZone (MPEZone (rpn.channel - 1, rpn.value));
  100. else
  101. clearAllZones();
  102. }
  103. //==============================================================================
  104. void MPEZoneLayout::processPitchbendRangeRpnMessage (MidiRPNMessage rpn)
  105. {
  106. if (auto* zone = getZoneByFirstNoteChannel (rpn.channel))
  107. {
  108. if (zone->getPerNotePitchbendRange() != rpn.value)
  109. {
  110. zone->setPerNotePitchbendRange (rpn.value);
  111. sendLayoutChangeMessage();
  112. return;
  113. }
  114. }
  115. if (auto* zone = getZoneByMasterChannel (rpn.channel))
  116. {
  117. if (zone->getMasterPitchbendRange() != rpn.value)
  118. {
  119. zone->setMasterPitchbendRange (rpn.value);
  120. sendLayoutChangeMessage();
  121. return;
  122. }
  123. }
  124. }
  125. //==============================================================================
  126. void MPEZoneLayout::processNextMidiBuffer (const MidiBuffer& buffer)
  127. {
  128. MidiBuffer::Iterator iter (buffer);
  129. MidiMessage message;
  130. int samplePosition; // not actually used, so no need to initialise.
  131. while (iter.getNextEvent (message, samplePosition))
  132. processNextMidiEvent (message);
  133. }
  134. //==============================================================================
  135. const MPEZone* MPEZoneLayout::getZoneByChannel (int channel) const noexcept
  136. {
  137. for (auto& zone : zones)
  138. if (zone.isUsingChannel (channel))
  139. return &zone;
  140. return nullptr;
  141. }
  142. MPEZone* MPEZoneLayout::getZoneByChannel (int channel) noexcept
  143. {
  144. return const_cast<MPEZone*> (static_cast<const MPEZoneLayout&> (*this).getZoneByChannel (channel));
  145. }
  146. const MPEZone* MPEZoneLayout::getZoneByMasterChannel (int channel) const noexcept
  147. {
  148. for (auto& zone : zones)
  149. if (zone.getMasterChannel() == channel)
  150. return &zone;
  151. return nullptr;
  152. }
  153. MPEZone* MPEZoneLayout::getZoneByMasterChannel (int channel) noexcept
  154. {
  155. return const_cast<MPEZone*> (static_cast<const MPEZoneLayout&> (*this).getZoneByMasterChannel (channel));
  156. }
  157. const MPEZone* MPEZoneLayout::getZoneByFirstNoteChannel (int channel) const noexcept
  158. {
  159. for (auto& zone : zones)
  160. if (zone.getFirstNoteChannel() == channel)
  161. return &zone;
  162. return nullptr;
  163. }
  164. MPEZone* MPEZoneLayout::getZoneByFirstNoteChannel (int channel) noexcept
  165. {
  166. return const_cast<MPEZone*> (static_cast<const MPEZoneLayout&> (*this).getZoneByFirstNoteChannel (channel));
  167. }
  168. const MPEZone* MPEZoneLayout::getZoneByNoteChannel (int channel) const noexcept
  169. {
  170. for (auto& zone : zones)
  171. if (zone.isUsingChannelAsNoteChannel (channel))
  172. return &zone;
  173. return nullptr;
  174. }
  175. MPEZone* MPEZoneLayout::getZoneByNoteChannel (int channel) noexcept
  176. {
  177. return const_cast<MPEZone*> (static_cast<const MPEZoneLayout&> (*this).getZoneByNoteChannel (channel));
  178. }
  179. //==============================================================================
  180. void MPEZoneLayout::addListener (Listener* const listenerToAdd) noexcept
  181. {
  182. listeners.add (listenerToAdd);
  183. }
  184. void MPEZoneLayout::removeListener (Listener* const listenerToRemove) noexcept
  185. {
  186. listeners.remove (listenerToRemove);
  187. }
  188. //==============================================================================
  189. //==============================================================================
  190. #if JUCE_UNIT_TESTS
  191. class MPEZoneLayoutTests : public UnitTest
  192. {
  193. public:
  194. MPEZoneLayoutTests() : UnitTest ("MPEZoneLayout class", "MIDI/MPE") {}
  195. void runTest() override
  196. {
  197. beginTest ("initialisation");
  198. {
  199. MPEZoneLayout layout;
  200. expectEquals (layout.getNumZones(), 0);
  201. }
  202. beginTest ("adding zones");
  203. {
  204. MPEZoneLayout layout;
  205. expect (layout.addZone (MPEZone (1, 7)));
  206. expectEquals (layout.getNumZones(), 1);
  207. expectEquals (layout.getZoneByIndex (0)->getMasterChannel(), 1);
  208. expectEquals (layout.getZoneByIndex (0)->getNumNoteChannels(), 7);
  209. expect (layout.addZone (MPEZone (9, 7)));
  210. expectEquals (layout.getNumZones(), 2);
  211. expectEquals (layout.getZoneByIndex (0)->getMasterChannel(), 1);
  212. expectEquals (layout.getZoneByIndex (0)->getNumNoteChannels(), 7);
  213. expectEquals (layout.getZoneByIndex (1)->getMasterChannel(), 9);
  214. expectEquals (layout.getZoneByIndex (1)->getNumNoteChannels(), 7);
  215. expect (! layout.addZone (MPEZone (5, 3)));
  216. expectEquals (layout.getNumZones(), 3);
  217. expectEquals (layout.getZoneByIndex (0)->getMasterChannel(), 1);
  218. expectEquals (layout.getZoneByIndex (0)->getNumNoteChannels(), 3);
  219. expectEquals (layout.getZoneByIndex (1)->getMasterChannel(), 9);
  220. expectEquals (layout.getZoneByIndex (1)->getNumNoteChannels(), 7);
  221. expectEquals (layout.getZoneByIndex (2)->getMasterChannel(), 5);
  222. expectEquals (layout.getZoneByIndex (2)->getNumNoteChannels(), 3);
  223. expect (! layout.addZone (MPEZone (5, 4)));
  224. expectEquals (layout.getNumZones(), 2);
  225. expectEquals (layout.getZoneByIndex (0)->getMasterChannel(), 1);
  226. expectEquals (layout.getZoneByIndex (0)->getNumNoteChannels(), 3);
  227. expectEquals (layout.getZoneByIndex (1)->getMasterChannel(), 5);
  228. expectEquals (layout.getZoneByIndex (1)->getNumNoteChannels(), 4);
  229. expect (! layout.addZone (MPEZone (6, 4)));
  230. expectEquals (layout.getNumZones(), 2);
  231. expectEquals (layout.getZoneByIndex (0)->getMasterChannel(), 1);
  232. expectEquals (layout.getZoneByIndex (0)->getNumNoteChannels(), 3);
  233. expectEquals (layout.getZoneByIndex (1)->getMasterChannel(), 6);
  234. expectEquals (layout.getZoneByIndex (1)->getNumNoteChannels(), 4);
  235. }
  236. beginTest ("querying zones");
  237. {
  238. MPEZoneLayout layout;
  239. layout.addZone (MPEZone (2, 5));
  240. layout.addZone (MPEZone (9, 4));
  241. expect (layout.getZoneByMasterChannel (1) == nullptr);
  242. expect (layout.getZoneByMasterChannel (2) != nullptr);
  243. expect (layout.getZoneByMasterChannel (3) == nullptr);
  244. expect (layout.getZoneByMasterChannel (8) == nullptr);
  245. expect (layout.getZoneByMasterChannel (9) != nullptr);
  246. expect (layout.getZoneByMasterChannel (10) == nullptr);
  247. expectEquals (layout.getZoneByMasterChannel (2)->getNumNoteChannels(), 5);
  248. expectEquals (layout.getZoneByMasterChannel (9)->getNumNoteChannels(), 4);
  249. expect (layout.getZoneByFirstNoteChannel (2) == nullptr);
  250. expect (layout.getZoneByFirstNoteChannel (3) != nullptr);
  251. expect (layout.getZoneByFirstNoteChannel (4) == nullptr);
  252. expect (layout.getZoneByFirstNoteChannel (9) == nullptr);
  253. expect (layout.getZoneByFirstNoteChannel (10) != nullptr);
  254. expect (layout.getZoneByFirstNoteChannel (11) == nullptr);
  255. expectEquals (layout.getZoneByFirstNoteChannel (3)->getNumNoteChannels(), 5);
  256. expectEquals (layout.getZoneByFirstNoteChannel (10)->getNumNoteChannels(), 4);
  257. expect (layout.getZoneByNoteChannel (2) == nullptr);
  258. expect (layout.getZoneByNoteChannel (3) != nullptr);
  259. expect (layout.getZoneByNoteChannel (4) != nullptr);
  260. expect (layout.getZoneByNoteChannel (6) != nullptr);
  261. expect (layout.getZoneByNoteChannel (7) != nullptr);
  262. expect (layout.getZoneByNoteChannel (8) == nullptr);
  263. expect (layout.getZoneByNoteChannel (9) == nullptr);
  264. expect (layout.getZoneByNoteChannel (10) != nullptr);
  265. expect (layout.getZoneByNoteChannel (11) != nullptr);
  266. expect (layout.getZoneByNoteChannel (12) != nullptr);
  267. expect (layout.getZoneByNoteChannel (13) != nullptr);
  268. expect (layout.getZoneByNoteChannel (14) == nullptr);
  269. expectEquals (layout.getZoneByNoteChannel (5)->getNumNoteChannels(), 5);
  270. expectEquals (layout.getZoneByNoteChannel (13)->getNumNoteChannels(), 4);
  271. }
  272. beginTest ("clear all zones");
  273. {
  274. MPEZoneLayout layout;
  275. expect (layout.addZone (MPEZone (1, 7)));
  276. expect (layout.addZone (MPEZone (10, 2)));
  277. layout.clearAllZones();
  278. expectEquals (layout.getNumZones(), 0);
  279. }
  280. beginTest ("process MIDI buffers");
  281. {
  282. MPEZoneLayout layout;
  283. MidiBuffer buffer;
  284. buffer = MPEMessages::addZone (MPEZone (1, 7));
  285. layout.processNextMidiBuffer (buffer);
  286. expectEquals (layout.getNumZones(), 1);
  287. expectEquals (layout.getZoneByIndex (0)->getMasterChannel(), 1);
  288. expectEquals (layout.getZoneByIndex (0)->getNumNoteChannels(), 7);
  289. buffer = MPEMessages::addZone (MPEZone (9, 7));
  290. layout.processNextMidiBuffer (buffer);
  291. expectEquals (layout.getNumZones(), 2);
  292. expectEquals (layout.getZoneByIndex (0)->getMasterChannel(), 1);
  293. expectEquals (layout.getZoneByIndex (0)->getNumNoteChannels(), 7);
  294. expectEquals (layout.getZoneByIndex (1)->getMasterChannel(), 9);
  295. expectEquals (layout.getZoneByIndex (1)->getNumNoteChannels(), 7);
  296. MPEZone zone (1, 10);
  297. buffer = MPEMessages::addZone (zone);
  298. layout.processNextMidiBuffer (buffer);
  299. expectEquals (layout.getNumZones(), 1);
  300. expectEquals (layout.getZoneByIndex (0)->getMasterChannel(), 1);
  301. expectEquals (layout.getZoneByIndex (0)->getNumNoteChannels(), 10);
  302. zone.setPerNotePitchbendRange (33);
  303. zone.setMasterPitchbendRange (44);
  304. buffer = MPEMessages::masterPitchbendRange (zone);
  305. buffer.addEvents (MPEMessages::perNotePitchbendRange (zone), 0, -1, 0);
  306. layout.processNextMidiBuffer (buffer);
  307. expectEquals (layout.getZoneByIndex (0)->getPerNotePitchbendRange(), 33);
  308. expectEquals (layout.getZoneByIndex (0)->getMasterPitchbendRange(), 44);
  309. }
  310. beginTest ("process individual MIDI messages");
  311. {
  312. MPEZoneLayout layout;
  313. layout.processNextMidiEvent (MidiMessage (0x80, 0x59, 0xd0)); // unrelated note-off msg
  314. layout.processNextMidiEvent (MidiMessage (0xb1, 0x64, 0x06)); // RPN part 1
  315. layout.processNextMidiEvent (MidiMessage (0xb1, 0x65, 0x00)); // RPN part 2
  316. layout.processNextMidiEvent (MidiMessage (0xb8, 0x0b, 0x66)); // unrelated CC msg
  317. layout.processNextMidiEvent (MidiMessage (0xb1, 0x06, 0x03)); // RPN part 3
  318. layout.processNextMidiEvent (MidiMessage (0x90, 0x60, 0x00)); // unrelated note-on msg
  319. expectEquals (layout.getNumZones(), 1);
  320. expectEquals (layout.getZoneByIndex (0)->getMasterChannel(), 1);
  321. expectEquals (layout.getZoneByIndex (0)->getNumNoteChannels(), 3);
  322. }
  323. }
  324. };
  325. static MPEZoneLayoutTests MPEZoneLayoutUnitTests;
  326. #endif // JUCE_UNIT_TESTS
  327. } // namespace juce