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

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