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.

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