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.

389 lines
14KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2016 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of the ISC license
  6. http://www.isc.org/downloads/software-support-policy/isc-license/
  7. Permission to use, copy, modify, and/or distribute this software for any
  8. purpose with or without fee is hereby granted, provided that the above
  9. copyright notice and this permission notice appear in all copies.
  10. THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD
  11. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  12. FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
  13. OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  14. USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  15. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  16. OF THIS SOFTWARE.
  17. -----------------------------------------------------------------------------
  18. To release a closed-source product which uses other parts of JUCE not
  19. licensed under the ISC terms, commercial licenses are available: visit
  20. www.juce.com for more information.
  21. ==============================================================================
  22. */
  23. MPEZoneLayout::MPEZoneLayout() noexcept
  24. {
  25. }
  26. MPEZoneLayout::MPEZoneLayout (const MPEZoneLayout& other)
  27. : zones (other.zones)
  28. {
  29. }
  30. MPEZoneLayout& MPEZoneLayout::operator= (const MPEZoneLayout& other)
  31. {
  32. zones = other.zones;
  33. listeners.call (&MPEZoneLayout::Listener::zoneLayoutChanged, *this);
  34. return *this;
  35. }
  36. //==============================================================================
  37. bool MPEZoneLayout::addZone (MPEZone newZone)
  38. {
  39. bool noOtherZonesModified = true;
  40. for (int i = zones.size(); --i >= 0;)
  41. {
  42. MPEZone& 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. listeners.call (&MPEZoneLayout::Listener::zoneLayoutChanged, *this);
  53. return noOtherZonesModified;
  54. }
  55. //==============================================================================
  56. int MPEZoneLayout::getNumZones() const noexcept
  57. {
  58. return zones.size();
  59. }
  60. MPEZone* MPEZoneLayout::getZoneByIndex (int index) const noexcept
  61. {
  62. if (zones.size() < index)
  63. return nullptr;
  64. return &(zones.getReference (index));
  65. }
  66. void MPEZoneLayout::clearAllZones()
  67. {
  68. zones.clear();
  69. listeners.call (&MPEZoneLayout::Listener::zoneLayoutChanged, *this);
  70. }
  71. //==============================================================================
  72. void MPEZoneLayout::processNextMidiEvent (const MidiMessage& message)
  73. {
  74. if (! message.isController())
  75. return;
  76. MidiRPNMessage rpn;
  77. if (rpnDetector.parseControllerMessage (message.getChannel(),
  78. message.getControllerNumber(),
  79. message.getControllerValue(),
  80. rpn))
  81. {
  82. processRpnMessage (rpn);
  83. }
  84. }
  85. void MPEZoneLayout::processRpnMessage (MidiRPNMessage rpn)
  86. {
  87. if (rpn.parameterNumber == MPEMessages::zoneLayoutMessagesRpnNumber)
  88. processZoneLayoutRpnMessage (rpn);
  89. else if (rpn.parameterNumber == 0)
  90. processPitchbendRangeRpnMessage (rpn);
  91. }
  92. void MPEZoneLayout::processZoneLayoutRpnMessage (MidiRPNMessage rpn)
  93. {
  94. if (rpn.value < 16)
  95. addZone (MPEZone (rpn.channel - 1, rpn.value));
  96. else
  97. clearAllZones();
  98. }
  99. //==============================================================================
  100. void MPEZoneLayout::processPitchbendRangeRpnMessage (MidiRPNMessage rpn)
  101. {
  102. if (MPEZone* zone = getZoneByFirstNoteChannel (rpn.channel))
  103. {
  104. if (zone->getPerNotePitchbendRange() != rpn.value)
  105. {
  106. zone->setPerNotePitchbendRange (rpn.value);
  107. listeners.call (&MPEZoneLayout::Listener::zoneLayoutChanged, *this);
  108. return;
  109. }
  110. }
  111. if (MPEZone* zone = getZoneByMasterChannel (rpn.channel))
  112. {
  113. if (zone->getMasterPitchbendRange() != rpn.value)
  114. {
  115. zone->setMasterPitchbendRange (rpn.value);
  116. listeners.call (&MPEZoneLayout::Listener::zoneLayoutChanged, *this);
  117. return;
  118. }
  119. }
  120. }
  121. //==============================================================================
  122. void MPEZoneLayout::processNextMidiBuffer (const MidiBuffer& buffer)
  123. {
  124. MidiBuffer::Iterator iter (buffer);
  125. MidiMessage message;
  126. int samplePosition; // not actually used, so no need to initialise.
  127. while (iter.getNextEvent (message, samplePosition))
  128. processNextMidiEvent (message);
  129. }
  130. //==============================================================================
  131. MPEZone* MPEZoneLayout::getZoneByChannel (int channel) const noexcept
  132. {
  133. for (MPEZone* zone = zones.begin(); zone != zones.end(); ++zone)
  134. if (zone->isUsingChannel (channel))
  135. return zone;
  136. return nullptr;
  137. }
  138. MPEZone* MPEZoneLayout::getZoneByMasterChannel (int channel) const noexcept
  139. {
  140. for (MPEZone* zone = zones.begin(); zone != zones.end(); ++zone)
  141. if (zone->getMasterChannel() == channel)
  142. return zone;
  143. return nullptr;
  144. }
  145. MPEZone* MPEZoneLayout::getZoneByFirstNoteChannel (int channel) const noexcept
  146. {
  147. for (MPEZone* zone = zones.begin(); zone != zones.end(); ++zone)
  148. if (zone->getFirstNoteChannel() == channel)
  149. return zone;
  150. return nullptr;
  151. }
  152. MPEZone* MPEZoneLayout::getZoneByNoteChannel (int channel) const noexcept
  153. {
  154. for (MPEZone* zone = zones.begin(); zone != zones.end(); ++zone)
  155. if (zone->isUsingChannelAsNoteChannel (channel))
  156. return zone;
  157. return nullptr;
  158. }
  159. //==============================================================================
  160. void MPEZoneLayout::addListener (Listener* const listenerToAdd) noexcept
  161. {
  162. listeners.add (listenerToAdd);
  163. }
  164. void MPEZoneLayout::removeListener (Listener* const listenerToRemove) noexcept
  165. {
  166. listeners.remove (listenerToRemove);
  167. }
  168. //==============================================================================
  169. //==============================================================================
  170. #if JUCE_UNIT_TESTS
  171. class MPEZoneLayoutTests : public UnitTest
  172. {
  173. public:
  174. MPEZoneLayoutTests() : UnitTest ("MPEZoneLayout class") {}
  175. void runTest() override
  176. {
  177. beginTest ("initialisation");
  178. {
  179. MPEZoneLayout layout;
  180. expectEquals (layout.getNumZones(), 0);
  181. }
  182. beginTest ("adding zones");
  183. {
  184. MPEZoneLayout layout;
  185. expect (layout.addZone (MPEZone (1, 7)));
  186. expectEquals (layout.getNumZones(), 1);
  187. expectEquals (layout.getZoneByIndex (0)->getMasterChannel(), 1);
  188. expectEquals (layout.getZoneByIndex (0)->getNumNoteChannels(), 7);
  189. expect (layout.addZone (MPEZone (9, 7)));
  190. expectEquals (layout.getNumZones(), 2);
  191. expectEquals (layout.getZoneByIndex (0)->getMasterChannel(), 1);
  192. expectEquals (layout.getZoneByIndex (0)->getNumNoteChannels(), 7);
  193. expectEquals (layout.getZoneByIndex (1)->getMasterChannel(), 9);
  194. expectEquals (layout.getZoneByIndex (1)->getNumNoteChannels(), 7);
  195. expect (! layout.addZone (MPEZone (5, 3)));
  196. expectEquals (layout.getNumZones(), 3);
  197. expectEquals (layout.getZoneByIndex (0)->getMasterChannel(), 1);
  198. expectEquals (layout.getZoneByIndex (0)->getNumNoteChannels(), 3);
  199. expectEquals (layout.getZoneByIndex (1)->getMasterChannel(), 9);
  200. expectEquals (layout.getZoneByIndex (1)->getNumNoteChannels(), 7);
  201. expectEquals (layout.getZoneByIndex (2)->getMasterChannel(), 5);
  202. expectEquals (layout.getZoneByIndex (2)->getNumNoteChannels(), 3);
  203. expect (! layout.addZone (MPEZone (5, 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(), 5);
  208. expectEquals (layout.getZoneByIndex (1)->getNumNoteChannels(), 4);
  209. expect (! layout.addZone (MPEZone (6, 4)));
  210. expectEquals (layout.getNumZones(), 2);
  211. expectEquals (layout.getZoneByIndex (0)->getMasterChannel(), 1);
  212. expectEquals (layout.getZoneByIndex (0)->getNumNoteChannels(), 3);
  213. expectEquals (layout.getZoneByIndex (1)->getMasterChannel(), 6);
  214. expectEquals (layout.getZoneByIndex (1)->getNumNoteChannels(), 4);
  215. }
  216. beginTest ("querying zones");
  217. {
  218. MPEZoneLayout layout;
  219. layout.addZone (MPEZone (2, 5));
  220. layout.addZone (MPEZone (9, 4));
  221. expect (layout.getZoneByMasterChannel (1) == nullptr);
  222. expect (layout.getZoneByMasterChannel (2) != nullptr);
  223. expect (layout.getZoneByMasterChannel (3) == nullptr);
  224. expect (layout.getZoneByMasterChannel (8) == nullptr);
  225. expect (layout.getZoneByMasterChannel (9) != nullptr);
  226. expect (layout.getZoneByMasterChannel (10) == nullptr);
  227. expectEquals (layout.getZoneByMasterChannel (2)->getNumNoteChannels(), 5);
  228. expectEquals (layout.getZoneByMasterChannel (9)->getNumNoteChannels(), 4);
  229. expect (layout.getZoneByFirstNoteChannel (2) == nullptr);
  230. expect (layout.getZoneByFirstNoteChannel (3) != nullptr);
  231. expect (layout.getZoneByFirstNoteChannel (4) == nullptr);
  232. expect (layout.getZoneByFirstNoteChannel (9) == nullptr);
  233. expect (layout.getZoneByFirstNoteChannel (10) != nullptr);
  234. expect (layout.getZoneByFirstNoteChannel (11) == nullptr);
  235. expectEquals (layout.getZoneByFirstNoteChannel (3)->getNumNoteChannels(), 5);
  236. expectEquals (layout.getZoneByFirstNoteChannel (10)->getNumNoteChannels(), 4);
  237. expect (layout.getZoneByNoteChannel (2) == nullptr);
  238. expect (layout.getZoneByNoteChannel (3) != nullptr);
  239. expect (layout.getZoneByNoteChannel (4) != nullptr);
  240. expect (layout.getZoneByNoteChannel (6) != nullptr);
  241. expect (layout.getZoneByNoteChannel (7) != nullptr);
  242. expect (layout.getZoneByNoteChannel (8) == nullptr);
  243. expect (layout.getZoneByNoteChannel (9) == nullptr);
  244. expect (layout.getZoneByNoteChannel (10) != nullptr);
  245. expect (layout.getZoneByNoteChannel (11) != nullptr);
  246. expect (layout.getZoneByNoteChannel (12) != nullptr);
  247. expect (layout.getZoneByNoteChannel (13) != nullptr);
  248. expect (layout.getZoneByNoteChannel (14) == nullptr);
  249. expectEquals (layout.getZoneByNoteChannel (5)->getNumNoteChannels(), 5);
  250. expectEquals (layout.getZoneByNoteChannel (13)->getNumNoteChannels(), 4);
  251. }
  252. beginTest ("clear all zones");
  253. {
  254. MPEZoneLayout layout;
  255. expect (layout.addZone (MPEZone (1, 7)));
  256. expect (layout.addZone (MPEZone (10, 2)));
  257. layout.clearAllZones();
  258. expectEquals (layout.getNumZones(), 0);
  259. }
  260. beginTest ("process MIDI buffers");
  261. {
  262. MPEZoneLayout layout;
  263. MidiBuffer buffer;
  264. buffer = MPEMessages::addZone (MPEZone (1, 7));
  265. layout.processNextMidiBuffer (buffer);
  266. expectEquals (layout.getNumZones(), 1);
  267. expectEquals (layout.getZoneByIndex (0)->getMasterChannel(), 1);
  268. expectEquals (layout.getZoneByIndex (0)->getNumNoteChannels(), 7);
  269. buffer = MPEMessages::addZone (MPEZone (9, 7));
  270. layout.processNextMidiBuffer (buffer);
  271. expectEquals (layout.getNumZones(), 2);
  272. expectEquals (layout.getZoneByIndex (0)->getMasterChannel(), 1);
  273. expectEquals (layout.getZoneByIndex (0)->getNumNoteChannels(), 7);
  274. expectEquals (layout.getZoneByIndex (1)->getMasterChannel(), 9);
  275. expectEquals (layout.getZoneByIndex (1)->getNumNoteChannels(), 7);
  276. MPEZone zone (1, 10);
  277. buffer = MPEMessages::addZone (zone);
  278. layout.processNextMidiBuffer (buffer);
  279. expectEquals (layout.getNumZones(), 1);
  280. expectEquals (layout.getZoneByIndex (0)->getMasterChannel(), 1);
  281. expectEquals (layout.getZoneByIndex (0)->getNumNoteChannels(), 10);
  282. zone.setPerNotePitchbendRange (33);
  283. zone.setMasterPitchbendRange (44);
  284. buffer = MPEMessages::masterPitchbendRange (zone);
  285. buffer.addEvents (MPEMessages::perNotePitchbendRange (zone), 0, -1, 0);
  286. layout.processNextMidiBuffer (buffer);
  287. expectEquals (layout.getZoneByIndex (0)->getPerNotePitchbendRange(), 33);
  288. expectEquals (layout.getZoneByIndex (0)->getMasterPitchbendRange(), 44);
  289. }
  290. beginTest ("process individual MIDI messages");
  291. {
  292. MPEZoneLayout layout;
  293. layout.processNextMidiEvent (MidiMessage (0x80, 0x59, 0xd0)); // unrelated note-off msg
  294. layout.processNextMidiEvent (MidiMessage (0xb1, 0x64, 0x06)); // RPN part 1
  295. layout.processNextMidiEvent (MidiMessage (0xb1, 0x65, 0x00)); // RPN part 2
  296. layout.processNextMidiEvent (MidiMessage (0xb8, 0x0b, 0x66)); // unrelated CC msg
  297. layout.processNextMidiEvent (MidiMessage (0xb1, 0x06, 0x03)); // RPN part 3
  298. layout.processNextMidiEvent (MidiMessage (0x90, 0x60, 0x00)); // unrelated note-on msg
  299. expectEquals (layout.getNumZones(), 1);
  300. expectEquals (layout.getZoneByIndex (0)->getMasterChannel(), 1);
  301. expectEquals (layout.getZoneByIndex (0)->getNumNoteChannels(), 3);
  302. }
  303. }
  304. };
  305. static MPEZoneLayoutTests MPEZoneLayoutUnitTests;
  306. #endif // JUCE_UNIT_TESTS