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.

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