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.

347 lines
13KB

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