The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

388 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. 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. MidiBuffer::Iterator iter (buffer);
  143. MidiMessage message;
  144. int samplePosition; // not actually used, so no need to initialise.
  145. while (iter.getNextEvent (message, samplePosition))
  146. processNextMidiEvent (message);
  147. }
  148. //==============================================================================
  149. void MPEZoneLayout::addListener (Listener* const listenerToAdd) noexcept
  150. {
  151. listeners.add (listenerToAdd);
  152. }
  153. void MPEZoneLayout::removeListener (Listener* const listenerToRemove) noexcept
  154. {
  155. listeners.remove (listenerToRemove);
  156. }
  157. //==============================================================================
  158. void MPEZoneLayout::checkAndLimitZoneParameters (int minValue, int maxValue,
  159. int& valueToCheckAndLimit) noexcept
  160. {
  161. if (valueToCheckAndLimit < minValue || valueToCheckAndLimit > maxValue)
  162. {
  163. // if you hit this, one of the parameters you supplied for this zone
  164. // was not within the allowed range!
  165. // we fit this back into the allowed range here to maintain a valid
  166. // state for the zone, but probably the resulting zone is not what you
  167. // wanted it to be!
  168. jassertfalse;
  169. valueToCheckAndLimit = jlimit (minValue, maxValue, valueToCheckAndLimit);
  170. }
  171. }
  172. //==============================================================================
  173. //==============================================================================
  174. #if JUCE_UNIT_TESTS
  175. class MPEZoneLayoutTests : public UnitTest
  176. {
  177. public:
  178. MPEZoneLayoutTests() : UnitTest ("MPEZoneLayout class", "MIDI/MPE") {}
  179. void runTest() override
  180. {
  181. beginTest ("initialisation");
  182. {
  183. MPEZoneLayout layout;
  184. expect (! layout.getLowerZone().isActive());
  185. expect (! layout.getUpperZone().isActive());
  186. }
  187. beginTest ("adding zones");
  188. {
  189. MPEZoneLayout layout;
  190. layout.setLowerZone (7);
  191. expect (layout.getLowerZone().isActive());
  192. expect (! layout.getUpperZone().isActive());
  193. expectEquals (layout.getLowerZone().getMasterChannel(), 1);
  194. expectEquals (layout.getLowerZone().numMemberChannels, 7);
  195. layout.setUpperZone (7);
  196. expect (layout.getLowerZone().isActive());
  197. expect (layout.getUpperZone().isActive());
  198. expectEquals (layout.getLowerZone().getMasterChannel(), 1);
  199. expectEquals (layout.getLowerZone().numMemberChannels, 7);
  200. expectEquals (layout.getUpperZone().getMasterChannel(), 16);
  201. expectEquals (layout.getUpperZone().numMemberChannels, 7);
  202. layout.setLowerZone (3);
  203. expect (layout.getLowerZone().isActive());
  204. expect (layout.getUpperZone().isActive());
  205. expectEquals (layout.getLowerZone().getMasterChannel(), 1);
  206. expectEquals (layout.getLowerZone().numMemberChannels, 3);
  207. expectEquals (layout.getUpperZone().getMasterChannel(), 16);
  208. expectEquals (layout.getUpperZone().numMemberChannels, 7);
  209. layout.setUpperZone (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, 3);
  216. layout.setLowerZone (15);
  217. expect (layout.getLowerZone().isActive());
  218. expect (! layout.getUpperZone().isActive());
  219. expectEquals (layout.getLowerZone().getMasterChannel(), 1);
  220. expectEquals (layout.getLowerZone().numMemberChannels, 15);
  221. }
  222. beginTest ("clear all zones");
  223. {
  224. MPEZoneLayout layout;
  225. expect (! layout.getLowerZone().isActive());
  226. expect (! layout.getUpperZone().isActive());
  227. layout.setLowerZone (7);
  228. layout.setUpperZone (2);
  229. expect (layout.getLowerZone().isActive());
  230. expect (layout.getUpperZone().isActive());
  231. layout.clearAllZones();
  232. expect (! layout.getLowerZone().isActive());
  233. expect (! layout.getUpperZone().isActive());
  234. }
  235. beginTest ("process MIDI buffers");
  236. {
  237. MPEZoneLayout layout;
  238. MidiBuffer buffer;
  239. buffer = MPEMessages::setLowerZone (7);
  240. layout.processNextMidiBuffer (buffer);
  241. expect (layout.getLowerZone().isActive());
  242. expect (! layout.getUpperZone().isActive());
  243. expectEquals (layout.getLowerZone().getMasterChannel(), 1);
  244. expectEquals (layout.getLowerZone().numMemberChannels, 7);
  245. buffer = MPEMessages::setUpperZone (7);
  246. layout.processNextMidiBuffer (buffer);
  247. expect (layout.getLowerZone().isActive());
  248. expect (layout.getUpperZone().isActive());
  249. expectEquals (layout.getLowerZone().getMasterChannel(), 1);
  250. expectEquals (layout.getLowerZone().numMemberChannels, 7);
  251. expectEquals (layout.getUpperZone().getMasterChannel(), 16);
  252. expectEquals (layout.getUpperZone().numMemberChannels, 7);
  253. {
  254. buffer = MPEMessages::setLowerZone (10);
  255. layout.processNextMidiBuffer (buffer);
  256. expect (layout.getLowerZone().isActive());
  257. expect (layout.getUpperZone().isActive());
  258. expectEquals (layout.getLowerZone().getMasterChannel(), 1);
  259. expectEquals (layout.getLowerZone().numMemberChannels, 10);
  260. expectEquals (layout.getUpperZone().getMasterChannel(), 16);
  261. expectEquals (layout.getUpperZone().numMemberChannels, 4);
  262. buffer = MPEMessages::setLowerZone (10, 33, 44);
  263. layout.processNextMidiBuffer (buffer);
  264. expectEquals (layout.getLowerZone().numMemberChannels, 10);
  265. expectEquals (layout.getLowerZone().perNotePitchbendRange, 33);
  266. expectEquals (layout.getLowerZone().masterPitchbendRange, 44);
  267. }
  268. {
  269. buffer = MPEMessages::setUpperZone (10);
  270. layout.processNextMidiBuffer (buffer);
  271. expect (layout.getLowerZone().isActive());
  272. expect (layout.getUpperZone().isActive());
  273. expectEquals (layout.getLowerZone().getMasterChannel(), 1);
  274. expectEquals (layout.getLowerZone().numMemberChannels, 4);
  275. expectEquals (layout.getUpperZone().getMasterChannel(), 16);
  276. expectEquals (layout.getUpperZone().numMemberChannels, 10);
  277. buffer = MPEMessages::setUpperZone (10, 33, 44);
  278. layout.processNextMidiBuffer (buffer);
  279. expectEquals (layout.getUpperZone().numMemberChannels, 10);
  280. expectEquals (layout.getUpperZone().perNotePitchbendRange, 33);
  281. expectEquals (layout.getUpperZone().masterPitchbendRange, 44);
  282. }
  283. buffer = MPEMessages::clearAllZones();
  284. layout.processNextMidiBuffer (buffer);
  285. expect (! layout.getLowerZone().isActive());
  286. expect (! layout.getUpperZone().isActive());
  287. }
  288. beginTest ("process individual MIDI messages");
  289. {
  290. MPEZoneLayout layout;
  291. layout.processNextMidiEvent ({ 0x80, 0x59, 0xd0 }); // unrelated note-off msg
  292. layout.processNextMidiEvent ({ 0xb0, 0x64, 0x06 }); // RPN part 1
  293. layout.processNextMidiEvent ({ 0xb0, 0x65, 0x00 }); // RPN part 2
  294. layout.processNextMidiEvent ({ 0xb8, 0x0b, 0x66 }); // unrelated CC msg
  295. layout.processNextMidiEvent ({ 0xb0, 0x06, 0x03 }); // RPN part 3
  296. layout.processNextMidiEvent ({ 0x90, 0x60, 0x00 }); // unrelated note-on msg
  297. expect (layout.getLowerZone().isActive());
  298. expect (! layout.getUpperZone().isActive());
  299. expectEquals (layout.getLowerZone().getMasterChannel(), 1);
  300. expectEquals (layout.getLowerZone().numMemberChannels, 3);
  301. expectEquals (layout.getLowerZone().perNotePitchbendRange, 48);
  302. expectEquals (layout.getLowerZone().masterPitchbendRange, 2);
  303. }
  304. }
  305. };
  306. static MPEZoneLayoutTests MPEZoneLayoutUnitTests;
  307. #endif // JUCE_UNIT_TESTS
  308. } // namespace juce