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.

381 lines
14KB

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