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.

387 lines
14KB

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