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.

382 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. return;
  102. }
  103. }
  104. if (MPEZone* zone = getZoneByMasterChannel (rpn.channel))
  105. {
  106. if (zone->getMasterPitchbendRange() != rpn.value)
  107. {
  108. zone->setMasterPitchbendRange (rpn.value);
  109. listeners.call (&MPEZoneLayout::Listener::zoneLayoutChanged, *this);
  110. return;
  111. }
  112. }
  113. }
  114. //==============================================================================
  115. void MPEZoneLayout::processNextMidiBuffer (const MidiBuffer& buffer)
  116. {
  117. MidiBuffer::Iterator iter (buffer);
  118. MidiMessage message;
  119. int samplePosition; // not actually used, so no need to initialise.
  120. while (iter.getNextEvent (message, samplePosition))
  121. processNextMidiEvent (message);
  122. }
  123. //==============================================================================
  124. MPEZone* MPEZoneLayout::getZoneByChannel (int channel) const noexcept
  125. {
  126. for (MPEZone* zone = zones.begin(); zone != zones.end(); ++zone)
  127. if (zone->isUsingChannel (channel))
  128. return zone;
  129. return nullptr;
  130. }
  131. MPEZone* MPEZoneLayout::getZoneByMasterChannel (int channel) const noexcept
  132. {
  133. for (MPEZone* zone = zones.begin(); zone != zones.end(); ++zone)
  134. if (zone->getMasterChannel() == channel)
  135. return zone;
  136. return nullptr;
  137. }
  138. MPEZone* MPEZoneLayout::getZoneByFirstNoteChannel (int channel) const noexcept
  139. {
  140. for (MPEZone* zone = zones.begin(); zone != zones.end(); ++zone)
  141. if (zone->getFirstNoteChannel() == channel)
  142. return zone;
  143. return nullptr;
  144. }
  145. MPEZone* MPEZoneLayout::getZoneByNoteChannel (int channel) const noexcept
  146. {
  147. for (MPEZone* zone = zones.begin(); zone != zones.end(); ++zone)
  148. if (zone->isUsingChannelAsNoteChannel (channel))
  149. return zone;
  150. return nullptr;
  151. }
  152. //==============================================================================
  153. void MPEZoneLayout::addListener (Listener* const listenerToAdd) noexcept
  154. {
  155. listeners.add (listenerToAdd);
  156. }
  157. void MPEZoneLayout::removeListener (Listener* const listenerToRemove) noexcept
  158. {
  159. listeners.remove (listenerToRemove);
  160. }
  161. //==============================================================================
  162. //==============================================================================
  163. #if JUCE_UNIT_TESTS
  164. class MPEZoneLayoutTests : public UnitTest
  165. {
  166. public:
  167. MPEZoneLayoutTests() : UnitTest ("MPEZoneLayout class") {}
  168. void runTest() override
  169. {
  170. beginTest ("initialisation");
  171. {
  172. MPEZoneLayout layout;
  173. expectEquals (layout.getNumZones(), 0);
  174. }
  175. beginTest ("adding zones");
  176. {
  177. MPEZoneLayout layout;
  178. expect (layout.addZone (MPEZone (1, 7)));
  179. expectEquals (layout.getNumZones(), 1);
  180. expectEquals (layout.getZoneByIndex (0)->getMasterChannel(), 1);
  181. expectEquals (layout.getZoneByIndex (0)->getNumNoteChannels(), 7);
  182. expect (layout.addZone (MPEZone (9, 7)));
  183. expectEquals (layout.getNumZones(), 2);
  184. expectEquals (layout.getZoneByIndex (0)->getMasterChannel(), 1);
  185. expectEquals (layout.getZoneByIndex (0)->getNumNoteChannels(), 7);
  186. expectEquals (layout.getZoneByIndex (1)->getMasterChannel(), 9);
  187. expectEquals (layout.getZoneByIndex (1)->getNumNoteChannels(), 7);
  188. expect (! layout.addZone (MPEZone (5, 3)));
  189. expectEquals (layout.getNumZones(), 3);
  190. expectEquals (layout.getZoneByIndex (0)->getMasterChannel(), 1);
  191. expectEquals (layout.getZoneByIndex (0)->getNumNoteChannels(), 3);
  192. expectEquals (layout.getZoneByIndex (1)->getMasterChannel(), 9);
  193. expectEquals (layout.getZoneByIndex (1)->getNumNoteChannels(), 7);
  194. expectEquals (layout.getZoneByIndex (2)->getMasterChannel(), 5);
  195. expectEquals (layout.getZoneByIndex (2)->getNumNoteChannels(), 3);
  196. expect (! layout.addZone (MPEZone (5, 4)));
  197. expectEquals (layout.getNumZones(), 2);
  198. expectEquals (layout.getZoneByIndex (0)->getMasterChannel(), 1);
  199. expectEquals (layout.getZoneByIndex (0)->getNumNoteChannels(), 3);
  200. expectEquals (layout.getZoneByIndex (1)->getMasterChannel(), 5);
  201. expectEquals (layout.getZoneByIndex (1)->getNumNoteChannels(), 4);
  202. expect (! layout.addZone (MPEZone (6, 4)));
  203. expectEquals (layout.getNumZones(), 2);
  204. expectEquals (layout.getZoneByIndex (0)->getMasterChannel(), 1);
  205. expectEquals (layout.getZoneByIndex (0)->getNumNoteChannels(), 3);
  206. expectEquals (layout.getZoneByIndex (1)->getMasterChannel(), 6);
  207. expectEquals (layout.getZoneByIndex (1)->getNumNoteChannels(), 4);
  208. }
  209. beginTest ("querying zones");
  210. {
  211. MPEZoneLayout layout;
  212. layout.addZone (MPEZone (2, 5));
  213. layout.addZone (MPEZone (9, 4));
  214. expect (layout.getZoneByMasterChannel (1) == nullptr);
  215. expect (layout.getZoneByMasterChannel (2) != nullptr);
  216. expect (layout.getZoneByMasterChannel (3) == nullptr);
  217. expect (layout.getZoneByMasterChannel (8) == nullptr);
  218. expect (layout.getZoneByMasterChannel (9) != nullptr);
  219. expect (layout.getZoneByMasterChannel (10) == nullptr);
  220. expectEquals (layout.getZoneByMasterChannel (2)->getNumNoteChannels(), 5);
  221. expectEquals (layout.getZoneByMasterChannel (9)->getNumNoteChannels(), 4);
  222. expect (layout.getZoneByFirstNoteChannel (2) == nullptr);
  223. expect (layout.getZoneByFirstNoteChannel (3) != nullptr);
  224. expect (layout.getZoneByFirstNoteChannel (4) == nullptr);
  225. expect (layout.getZoneByFirstNoteChannel (9) == nullptr);
  226. expect (layout.getZoneByFirstNoteChannel (10) != nullptr);
  227. expect (layout.getZoneByFirstNoteChannel (11) == nullptr);
  228. expectEquals (layout.getZoneByFirstNoteChannel (3)->getNumNoteChannels(), 5);
  229. expectEquals (layout.getZoneByFirstNoteChannel (10)->getNumNoteChannels(), 4);
  230. expect (layout.getZoneByNoteChannel (2) == nullptr);
  231. expect (layout.getZoneByNoteChannel (3) != nullptr);
  232. expect (layout.getZoneByNoteChannel (4) != nullptr);
  233. expect (layout.getZoneByNoteChannel (6) != nullptr);
  234. expect (layout.getZoneByNoteChannel (7) != nullptr);
  235. expect (layout.getZoneByNoteChannel (8) == nullptr);
  236. expect (layout.getZoneByNoteChannel (9) == nullptr);
  237. expect (layout.getZoneByNoteChannel (10) != nullptr);
  238. expect (layout.getZoneByNoteChannel (11) != nullptr);
  239. expect (layout.getZoneByNoteChannel (12) != nullptr);
  240. expect (layout.getZoneByNoteChannel (13) != nullptr);
  241. expect (layout.getZoneByNoteChannel (14) == nullptr);
  242. expectEquals (layout.getZoneByNoteChannel (5)->getNumNoteChannels(), 5);
  243. expectEquals (layout.getZoneByNoteChannel (13)->getNumNoteChannels(), 4);
  244. }
  245. beginTest ("clear all zones");
  246. {
  247. MPEZoneLayout layout;
  248. expect (layout.addZone (MPEZone (1, 7)));
  249. expect (layout.addZone (MPEZone (10, 2)));
  250. layout.clearAllZones();
  251. expectEquals (layout.getNumZones(), 0);
  252. }
  253. beginTest ("process MIDI buffers");
  254. {
  255. MPEZoneLayout layout;
  256. MidiBuffer buffer;
  257. buffer = MPEMessages::addZone (MPEZone (1, 7));
  258. layout.processNextMidiBuffer (buffer);
  259. expectEquals (layout.getNumZones(), 1);
  260. expectEquals (layout.getZoneByIndex (0)->getMasterChannel(), 1);
  261. expectEquals (layout.getZoneByIndex (0)->getNumNoteChannels(), 7);
  262. buffer = MPEMessages::addZone (MPEZone (9, 7));
  263. layout.processNextMidiBuffer (buffer);
  264. expectEquals (layout.getNumZones(), 2);
  265. expectEquals (layout.getZoneByIndex (0)->getMasterChannel(), 1);
  266. expectEquals (layout.getZoneByIndex (0)->getNumNoteChannels(), 7);
  267. expectEquals (layout.getZoneByIndex (1)->getMasterChannel(), 9);
  268. expectEquals (layout.getZoneByIndex (1)->getNumNoteChannels(), 7);
  269. MPEZone zone (1, 10);
  270. buffer = MPEMessages::addZone (zone);
  271. layout.processNextMidiBuffer (buffer);
  272. expectEquals (layout.getNumZones(), 1);
  273. expectEquals (layout.getZoneByIndex (0)->getMasterChannel(), 1);
  274. expectEquals (layout.getZoneByIndex (0)->getNumNoteChannels(), 10);
  275. zone.setPerNotePitchbendRange (33);
  276. zone.setMasterPitchbendRange (44);
  277. buffer = MPEMessages::masterPitchbendRange (zone);
  278. buffer.addEvents (MPEMessages::perNotePitchbendRange (zone), 0, -1, 0);
  279. layout.processNextMidiBuffer (buffer);
  280. expectEquals (layout.getZoneByIndex (0)->getPerNotePitchbendRange(), 33);
  281. expectEquals (layout.getZoneByIndex (0)->getMasterPitchbendRange(), 44);
  282. }
  283. beginTest ("process individual MIDI messages");
  284. {
  285. MPEZoneLayout layout;
  286. layout.processNextMidiEvent (MidiMessage (0x80, 0x59, 0xd0)); // unrelated note-off msg
  287. layout.processNextMidiEvent (MidiMessage (0xb1, 0x64, 0x06)); // RPN part 1
  288. layout.processNextMidiEvent (MidiMessage (0xb1, 0x65, 0x00)); // RPN part 2
  289. layout.processNextMidiEvent (MidiMessage (0xb8, 0x0b, 0x66)); // unrelated CC msg
  290. layout.processNextMidiEvent (MidiMessage (0xb1, 0x06, 0x03)); // RPN part 3
  291. layout.processNextMidiEvent (MidiMessage (0x90, 0x60, 0x00)); // unrelated note-on msg
  292. expectEquals (layout.getNumZones(), 1);
  293. expectEquals (layout.getZoneByIndex (0)->getMasterChannel(), 1);
  294. expectEquals (layout.getZoneByIndex (0)->getNumNoteChannels(), 3);
  295. }
  296. }
  297. };
  298. static MPEZoneLayoutTests MPEZoneLayoutUnitTests;
  299. #endif // JUCE_UNIT_TESTS