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.

juce_MPEZoneLayout.cpp 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - Raw Material Software Limited
  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 (MPEZone lower, MPEZone upper)
  20. : lowerZone (lower), upperZone (upper)
  21. {
  22. }
  23. MPEZoneLayout::MPEZoneLayout (MPEZone zone)
  24. : lowerZone (zone.isLowerZone() ? zone : MPEZone()),
  25. upperZone (! zone.isLowerZone() ? zone : MPEZone())
  26. {
  27. }
  28. MPEZoneLayout::MPEZoneLayout (const MPEZoneLayout& other)
  29. : lowerZone (other.lowerZone),
  30. upperZone (other.upperZone)
  31. {
  32. }
  33. MPEZoneLayout& MPEZoneLayout::operator= (const MPEZoneLayout& other)
  34. {
  35. lowerZone = other.lowerZone;
  36. upperZone = other.upperZone;
  37. sendLayoutChangeMessage();
  38. return *this;
  39. }
  40. void MPEZoneLayout::sendLayoutChangeMessage()
  41. {
  42. listeners.call ([this] (Listener& l) { l.zoneLayoutChanged (*this); });
  43. }
  44. //==============================================================================
  45. void MPEZoneLayout::setZone (bool isLower, int numMemberChannels, int perNotePitchbendRange, int masterPitchbendRange) noexcept
  46. {
  47. checkAndLimitZoneParameters (0, 15, numMemberChannels);
  48. checkAndLimitZoneParameters (0, 96, perNotePitchbendRange);
  49. checkAndLimitZoneParameters (0, 96, masterPitchbendRange);
  50. if (isLower)
  51. lowerZone = { MPEZone::Type::lower, numMemberChannels, perNotePitchbendRange, masterPitchbendRange };
  52. else
  53. upperZone = { MPEZone::Type::upper, numMemberChannels, perNotePitchbendRange, masterPitchbendRange };
  54. if (numMemberChannels > 0)
  55. {
  56. auto totalChannels = lowerZone.numMemberChannels + upperZone.numMemberChannels;
  57. if (totalChannels >= 15)
  58. {
  59. if (isLower)
  60. upperZone.numMemberChannels = 14 - numMemberChannels;
  61. else
  62. lowerZone.numMemberChannels = 14 - numMemberChannels;
  63. }
  64. }
  65. sendLayoutChangeMessage();
  66. }
  67. void MPEZoneLayout::setLowerZone (int numMemberChannels, int perNotePitchbendRange, int masterPitchbendRange) noexcept
  68. {
  69. setZone (true, numMemberChannels, perNotePitchbendRange, masterPitchbendRange);
  70. }
  71. void MPEZoneLayout::setUpperZone (int numMemberChannels, int perNotePitchbendRange, int masterPitchbendRange) noexcept
  72. {
  73. setZone (false, numMemberChannels, perNotePitchbendRange, masterPitchbendRange);
  74. }
  75. void MPEZoneLayout::clearAllZones()
  76. {
  77. lowerZone = { MPEZone::Type::lower, 0 };
  78. upperZone = { MPEZone::Type::upper, 0 };
  79. sendLayoutChangeMessage();
  80. }
  81. //==============================================================================
  82. void MPEZoneLayout::processNextMidiEvent (const MidiMessage& message)
  83. {
  84. if (! message.isController())
  85. return;
  86. MidiRPNMessage rpn;
  87. if (rpnDetector.parseControllerMessage (message.getChannel(),
  88. message.getControllerNumber(),
  89. message.getControllerValue(),
  90. rpn))
  91. {
  92. processRpnMessage (rpn);
  93. }
  94. }
  95. void MPEZoneLayout::processRpnMessage (MidiRPNMessage rpn)
  96. {
  97. if (rpn.parameterNumber == MPEMessages::zoneLayoutMessagesRpnNumber)
  98. processZoneLayoutRpnMessage (rpn);
  99. else if (rpn.parameterNumber == 0)
  100. processPitchbendRangeRpnMessage (rpn);
  101. }
  102. void MPEZoneLayout::processZoneLayoutRpnMessage (MidiRPNMessage rpn)
  103. {
  104. if (rpn.value < 16)
  105. {
  106. if (rpn.channel == 1)
  107. setLowerZone (rpn.value);
  108. else if (rpn.channel == 16)
  109. setUpperZone (rpn.value);
  110. }
  111. }
  112. void MPEZoneLayout::updateMasterPitchbend (MPEZone& zone, int value)
  113. {
  114. if (zone.masterPitchbendRange != value)
  115. {
  116. checkAndLimitZoneParameters (0, 96, zone.masterPitchbendRange);
  117. zone.masterPitchbendRange = value;
  118. sendLayoutChangeMessage();
  119. }
  120. }
  121. void MPEZoneLayout::updatePerNotePitchbendRange (MPEZone& zone, int value)
  122. {
  123. if (zone.perNotePitchbendRange != value)
  124. {
  125. checkAndLimitZoneParameters (0, 96, zone.perNotePitchbendRange);
  126. zone.perNotePitchbendRange = value;
  127. sendLayoutChangeMessage();
  128. }
  129. }
  130. void MPEZoneLayout::processPitchbendRangeRpnMessage (MidiRPNMessage rpn)
  131. {
  132. if (rpn.channel == 1)
  133. {
  134. updateMasterPitchbend (lowerZone, rpn.value);
  135. }
  136. else if (rpn.channel == 16)
  137. {
  138. updateMasterPitchbend (upperZone, rpn.value);
  139. }
  140. else
  141. {
  142. if (lowerZone.isUsingChannelAsMemberChannel (rpn.channel))
  143. updatePerNotePitchbendRange (lowerZone, rpn.value);
  144. else if (upperZone.isUsingChannelAsMemberChannel (rpn.channel))
  145. updatePerNotePitchbendRange (upperZone, rpn.value);
  146. }
  147. }
  148. void MPEZoneLayout::processNextMidiBuffer (const MidiBuffer& buffer)
  149. {
  150. for (const auto metadata : buffer)
  151. processNextMidiEvent (metadata.getMessage());
  152. }
  153. //==============================================================================
  154. void MPEZoneLayout::addListener (Listener* const listenerToAdd) noexcept
  155. {
  156. listeners.add (listenerToAdd);
  157. }
  158. void MPEZoneLayout::removeListener (Listener* const listenerToRemove) noexcept
  159. {
  160. listeners.remove (listenerToRemove);
  161. }
  162. //==============================================================================
  163. void MPEZoneLayout::checkAndLimitZoneParameters (int minValue, int maxValue,
  164. int& valueToCheckAndLimit) noexcept
  165. {
  166. if (valueToCheckAndLimit < minValue || valueToCheckAndLimit > maxValue)
  167. {
  168. // if you hit this, one of the parameters you supplied for this zone
  169. // was not within the allowed range!
  170. // we fit this back into the allowed range here to maintain a valid
  171. // state for the zone, but probably the resulting zone is not what you
  172. // wanted it to be!
  173. jassertfalse;
  174. valueToCheckAndLimit = jlimit (minValue, maxValue, valueToCheckAndLimit);
  175. }
  176. }
  177. //==============================================================================
  178. //==============================================================================
  179. #if JUCE_UNIT_TESTS
  180. class MPEZoneLayoutTests : public UnitTest
  181. {
  182. public:
  183. MPEZoneLayoutTests()
  184. : UnitTest ("MPEZoneLayout class", UnitTestCategories::midi)
  185. {}
  186. void runTest() override
  187. {
  188. beginTest ("initialisation");
  189. {
  190. MPEZoneLayout layout;
  191. expect (! layout.getLowerZone().isActive());
  192. expect (! layout.getUpperZone().isActive());
  193. }
  194. beginTest ("adding zones");
  195. {
  196. MPEZoneLayout layout;
  197. layout.setLowerZone (7);
  198. expect (layout.getLowerZone().isActive());
  199. expect (! layout.getUpperZone().isActive());
  200. expectEquals (layout.getLowerZone().getMasterChannel(), 1);
  201. expectEquals (layout.getLowerZone().numMemberChannels, 7);
  202. layout.setUpperZone (7);
  203. expect (layout.getLowerZone().isActive());
  204. expect (layout.getUpperZone().isActive());
  205. expectEquals (layout.getLowerZone().getMasterChannel(), 1);
  206. expectEquals (layout.getLowerZone().numMemberChannels, 7);
  207. expectEquals (layout.getUpperZone().getMasterChannel(), 16);
  208. expectEquals (layout.getUpperZone().numMemberChannels, 7);
  209. layout.setLowerZone (3);
  210. expect (layout.getLowerZone().isActive());
  211. expect (layout.getUpperZone().isActive());
  212. expectEquals (layout.getLowerZone().getMasterChannel(), 1);
  213. expectEquals (layout.getLowerZone().numMemberChannels, 3);
  214. expectEquals (layout.getUpperZone().getMasterChannel(), 16);
  215. expectEquals (layout.getUpperZone().numMemberChannels, 7);
  216. layout.setUpperZone (3);
  217. expect (layout.getLowerZone().isActive());
  218. expect (layout.getUpperZone().isActive());
  219. expectEquals (layout.getLowerZone().getMasterChannel(), 1);
  220. expectEquals (layout.getLowerZone().numMemberChannels, 3);
  221. expectEquals (layout.getUpperZone().getMasterChannel(), 16);
  222. expectEquals (layout.getUpperZone().numMemberChannels, 3);
  223. layout.setLowerZone (15);
  224. expect (layout.getLowerZone().isActive());
  225. expect (! layout.getUpperZone().isActive());
  226. expectEquals (layout.getLowerZone().getMasterChannel(), 1);
  227. expectEquals (layout.getLowerZone().numMemberChannels, 15);
  228. }
  229. beginTest ("clear all zones");
  230. {
  231. MPEZoneLayout layout;
  232. expect (! layout.getLowerZone().isActive());
  233. expect (! layout.getUpperZone().isActive());
  234. layout.setLowerZone (7);
  235. layout.setUpperZone (2);
  236. expect (layout.getLowerZone().isActive());
  237. expect (layout.getUpperZone().isActive());
  238. layout.clearAllZones();
  239. expect (! layout.getLowerZone().isActive());
  240. expect (! layout.getUpperZone().isActive());
  241. }
  242. beginTest ("process MIDI buffers");
  243. {
  244. MPEZoneLayout layout;
  245. MidiBuffer buffer;
  246. buffer = MPEMessages::setLowerZone (7);
  247. layout.processNextMidiBuffer (buffer);
  248. expect (layout.getLowerZone().isActive());
  249. expect (! layout.getUpperZone().isActive());
  250. expectEquals (layout.getLowerZone().getMasterChannel(), 1);
  251. expectEquals (layout.getLowerZone().numMemberChannels, 7);
  252. buffer = MPEMessages::setUpperZone (7);
  253. layout.processNextMidiBuffer (buffer);
  254. expect (layout.getLowerZone().isActive());
  255. expect (layout.getUpperZone().isActive());
  256. expectEquals (layout.getLowerZone().getMasterChannel(), 1);
  257. expectEquals (layout.getLowerZone().numMemberChannels, 7);
  258. expectEquals (layout.getUpperZone().getMasterChannel(), 16);
  259. expectEquals (layout.getUpperZone().numMemberChannels, 7);
  260. {
  261. buffer = MPEMessages::setLowerZone (10);
  262. layout.processNextMidiBuffer (buffer);
  263. expect (layout.getLowerZone().isActive());
  264. expect (layout.getUpperZone().isActive());
  265. expectEquals (layout.getLowerZone().getMasterChannel(), 1);
  266. expectEquals (layout.getLowerZone().numMemberChannels, 10);
  267. expectEquals (layout.getUpperZone().getMasterChannel(), 16);
  268. expectEquals (layout.getUpperZone().numMemberChannels, 4);
  269. buffer = MPEMessages::setLowerZone (10, 33, 44);
  270. layout.processNextMidiBuffer (buffer);
  271. expectEquals (layout.getLowerZone().numMemberChannels, 10);
  272. expectEquals (layout.getLowerZone().perNotePitchbendRange, 33);
  273. expectEquals (layout.getLowerZone().masterPitchbendRange, 44);
  274. }
  275. {
  276. buffer = MPEMessages::setUpperZone (10);
  277. layout.processNextMidiBuffer (buffer);
  278. expect (layout.getLowerZone().isActive());
  279. expect (layout.getUpperZone().isActive());
  280. expectEquals (layout.getLowerZone().getMasterChannel(), 1);
  281. expectEquals (layout.getLowerZone().numMemberChannels, 4);
  282. expectEquals (layout.getUpperZone().getMasterChannel(), 16);
  283. expectEquals (layout.getUpperZone().numMemberChannels, 10);
  284. buffer = MPEMessages::setUpperZone (10, 33, 44);
  285. layout.processNextMidiBuffer (buffer);
  286. expectEquals (layout.getUpperZone().numMemberChannels, 10);
  287. expectEquals (layout.getUpperZone().perNotePitchbendRange, 33);
  288. expectEquals (layout.getUpperZone().masterPitchbendRange, 44);
  289. }
  290. buffer = MPEMessages::clearAllZones();
  291. layout.processNextMidiBuffer (buffer);
  292. expect (! layout.getLowerZone().isActive());
  293. expect (! layout.getUpperZone().isActive());
  294. }
  295. beginTest ("process individual MIDI messages");
  296. {
  297. MPEZoneLayout layout;
  298. layout.processNextMidiEvent ({ 0x80, 0x59, 0xd0 }); // unrelated note-off msg
  299. layout.processNextMidiEvent ({ 0xb0, 0x64, 0x06 }); // RPN part 1
  300. layout.processNextMidiEvent ({ 0xb0, 0x65, 0x00 }); // RPN part 2
  301. layout.processNextMidiEvent ({ 0xb8, 0x0b, 0x66 }); // unrelated CC msg
  302. layout.processNextMidiEvent ({ 0xb0, 0x06, 0x03 }); // RPN part 3
  303. layout.processNextMidiEvent ({ 0x90, 0x60, 0x00 }); // unrelated note-on msg
  304. expect (layout.getLowerZone().isActive());
  305. expect (! layout.getUpperZone().isActive());
  306. expectEquals (layout.getLowerZone().getMasterChannel(), 1);
  307. expectEquals (layout.getLowerZone().numMemberChannels, 3);
  308. expectEquals (layout.getLowerZone().perNotePitchbendRange, 48);
  309. expectEquals (layout.getLowerZone().masterPitchbendRange, 2);
  310. }
  311. }
  312. };
  313. static MPEZoneLayoutTests MPEZoneLayoutUnitTests;
  314. #endif
  315. } // namespace juce