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_MPEZone.cpp 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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. namespace
  20. {
  21. void checkAndLimitZoneParameters (int minValue,
  22. int maxValue,
  23. int& valueToCheckAndLimit) noexcept
  24. {
  25. if (valueToCheckAndLimit < minValue || valueToCheckAndLimit > maxValue)
  26. {
  27. // if you hit this, one of the parameters you supplied for MPEZone
  28. // was not within the allowed range!
  29. // we fit this back into the allowed range here to maintain a valid
  30. // state for the zone, but probably the resulting zone is not what you
  31. //wanted it to be!
  32. jassertfalse;
  33. valueToCheckAndLimit = jlimit (minValue, maxValue, valueToCheckAndLimit);
  34. }
  35. }
  36. }
  37. //==============================================================================
  38. MPEZone::MPEZone (int masterChannel_,
  39. int numNoteChannels_,
  40. int perNotePitchbendRange_,
  41. int masterPitchbendRange_) noexcept
  42. : masterChannel (masterChannel_),
  43. numNoteChannels (numNoteChannels_),
  44. perNotePitchbendRange (perNotePitchbendRange_),
  45. masterPitchbendRange (masterPitchbendRange_)
  46. {
  47. checkAndLimitZoneParameters (1, 15, masterChannel);
  48. checkAndLimitZoneParameters (1, 16 - masterChannel, numNoteChannels);
  49. checkAndLimitZoneParameters (0, 96, perNotePitchbendRange);
  50. checkAndLimitZoneParameters (0, 96, masterPitchbendRange);
  51. }
  52. //==============================================================================
  53. int MPEZone::getMasterChannel() const noexcept
  54. {
  55. return masterChannel;
  56. }
  57. int MPEZone::getNumNoteChannels() const noexcept
  58. {
  59. return numNoteChannels;
  60. }
  61. int MPEZone::getFirstNoteChannel() const noexcept
  62. {
  63. return masterChannel + 1;
  64. }
  65. int MPEZone::getLastNoteChannel() const noexcept
  66. {
  67. return masterChannel + numNoteChannels;
  68. }
  69. Range<int> MPEZone::getNoteChannelRange() const noexcept
  70. {
  71. return Range<int>::withStartAndLength (getFirstNoteChannel(), getNumNoteChannels());
  72. }
  73. bool MPEZone::isUsingChannel (int channel) const noexcept
  74. {
  75. jassert (channel > 0 && channel <= 16);
  76. return channel >= masterChannel && channel <= masterChannel + numNoteChannels;
  77. }
  78. bool MPEZone::isUsingChannelAsNoteChannel (int channel) const noexcept
  79. {
  80. jassert (channel > 0 && channel <= 16);
  81. return channel > masterChannel && channel <= masterChannel + numNoteChannels;
  82. }
  83. int MPEZone::getPerNotePitchbendRange() const noexcept
  84. {
  85. return perNotePitchbendRange;
  86. }
  87. int MPEZone::getMasterPitchbendRange() const noexcept
  88. {
  89. return masterPitchbendRange;
  90. }
  91. void MPEZone::setPerNotePitchbendRange (int rangeInSemitones) noexcept
  92. {
  93. checkAndLimitZoneParameters (0, 96, rangeInSemitones);
  94. perNotePitchbendRange = rangeInSemitones;
  95. }
  96. void MPEZone::setMasterPitchbendRange (int rangeInSemitones) noexcept
  97. {
  98. checkAndLimitZoneParameters (0, 96, rangeInSemitones);
  99. masterPitchbendRange = rangeInSemitones;
  100. }
  101. //==============================================================================
  102. bool MPEZone::overlapsWith (MPEZone other) const noexcept
  103. {
  104. if (masterChannel == other.masterChannel)
  105. return true;
  106. if (masterChannel > other.masterChannel)
  107. return other.overlapsWith (*this);
  108. return masterChannel + numNoteChannels >= other.masterChannel;
  109. }
  110. //==============================================================================
  111. bool MPEZone::truncateToFit (MPEZone other) noexcept
  112. {
  113. const int masterChannelDiff = other.masterChannel - masterChannel;
  114. // we need at least 2 channels to be left after truncation:
  115. // 1 master channel and 1 note channel. otherwise we can't truncate.
  116. if (masterChannelDiff < 2)
  117. return false;
  118. numNoteChannels = jmin (numNoteChannels, masterChannelDiff - 1);
  119. return true;
  120. }
  121. //==============================================================================
  122. bool MPEZone::operator== (const MPEZone& other) const noexcept
  123. {
  124. return masterChannel == other.masterChannel
  125. && numNoteChannels == other.numNoteChannels
  126. && perNotePitchbendRange == other.perNotePitchbendRange
  127. && masterPitchbendRange == other.masterPitchbendRange;
  128. }
  129. bool MPEZone::operator!= (const MPEZone& other) const noexcept
  130. {
  131. return ! operator== (other);
  132. }
  133. //==============================================================================
  134. //==============================================================================
  135. #if JUCE_UNIT_TESTS
  136. class MPEZoneTests : public UnitTest
  137. {
  138. public:
  139. MPEZoneTests() : UnitTest ("MPEZone class", "MIDI/MPE") {}
  140. void runTest() override
  141. {
  142. beginTest ("initialisation");
  143. {
  144. {
  145. MPEZone zone (1, 10);
  146. expectEquals (zone.getMasterChannel(), 1);
  147. expectEquals (zone.getNumNoteChannels(), 10);
  148. expectEquals (zone.getFirstNoteChannel(), 2);
  149. expectEquals (zone.getLastNoteChannel(), 11);
  150. expectEquals (zone.getPerNotePitchbendRange(), 48);
  151. expectEquals (zone.getMasterPitchbendRange(), 2);
  152. expect (zone.isUsingChannel (1));
  153. expect (zone.isUsingChannel (2));
  154. expect (zone.isUsingChannel (10));
  155. expect (zone.isUsingChannel (11));
  156. expect (! zone.isUsingChannel (12));
  157. expect (! zone.isUsingChannel (16));
  158. expect (! zone.isUsingChannelAsNoteChannel (1));
  159. expect (zone.isUsingChannelAsNoteChannel (2));
  160. expect (zone.isUsingChannelAsNoteChannel (10));
  161. expect (zone.isUsingChannelAsNoteChannel (11));
  162. expect (! zone.isUsingChannelAsNoteChannel (12));
  163. expect (! zone.isUsingChannelAsNoteChannel (16));
  164. }
  165. {
  166. MPEZone zone (5, 4);
  167. expectEquals (zone.getMasterChannel(), 5);
  168. expectEquals (zone.getNumNoteChannels(), 4);
  169. expectEquals (zone.getFirstNoteChannel(), 6);
  170. expectEquals (zone.getLastNoteChannel(), 9);
  171. expectEquals (zone.getPerNotePitchbendRange(), 48);
  172. expectEquals (zone.getMasterPitchbendRange(), 2);
  173. expect (! zone.isUsingChannel (1));
  174. expect (! zone.isUsingChannel (4));
  175. expect (zone.isUsingChannel (5));
  176. expect (zone.isUsingChannel (6));
  177. expect (zone.isUsingChannel (8));
  178. expect (zone.isUsingChannel (9));
  179. expect (! zone.isUsingChannel (10));
  180. expect (! zone.isUsingChannel (16));
  181. expect (! zone.isUsingChannelAsNoteChannel (5));
  182. expect (zone.isUsingChannelAsNoteChannel (6));
  183. expect (zone.isUsingChannelAsNoteChannel (8));
  184. expect (zone.isUsingChannelAsNoteChannel (9));
  185. expect (! zone.isUsingChannelAsNoteChannel (10));
  186. }
  187. }
  188. beginTest ("getNoteChannelRange");
  189. {
  190. MPEZone zone (2, 10);
  191. Range<int> noteChannelRange = zone.getNoteChannelRange();
  192. expectEquals (noteChannelRange.getStart(), 3);
  193. expectEquals (noteChannelRange.getEnd(), 13);
  194. }
  195. beginTest ("setting master pitchbend range");
  196. {
  197. MPEZone zone (1, 10);
  198. zone.setMasterPitchbendRange (96);
  199. expectEquals (zone.getMasterPitchbendRange(), 96);
  200. zone.setMasterPitchbendRange (0);
  201. expectEquals (zone.getMasterPitchbendRange(), 0);
  202. expectEquals (zone.getPerNotePitchbendRange(), 48);
  203. }
  204. beginTest ("setting per-note pitchbend range");
  205. {
  206. MPEZone zone (1, 10);
  207. zone.setPerNotePitchbendRange (96);
  208. expectEquals (zone.getPerNotePitchbendRange(), 96);
  209. zone.setPerNotePitchbendRange (0);
  210. expectEquals (zone.getPerNotePitchbendRange(), 0);
  211. expectEquals (zone.getMasterPitchbendRange(), 2);
  212. }
  213. beginTest ("checking overlap");
  214. {
  215. testOverlapsWith (1, 10, 1, 10, true);
  216. testOverlapsWith (1, 4, 6, 3, false);
  217. testOverlapsWith (1, 4, 8, 3, false);
  218. testOverlapsWith (2, 10, 2, 8, true);
  219. testOverlapsWith (1, 10, 3, 2, true);
  220. testOverlapsWith (3, 10, 5, 9, true);
  221. }
  222. beginTest ("truncating");
  223. {
  224. testTruncateToFit (1, 10, 3, 10, true, 1, 1);
  225. testTruncateToFit (3, 10, 1, 10, false, 3, 10);
  226. testTruncateToFit (1, 10, 5, 8, true, 1, 3);
  227. testTruncateToFit (5, 8, 1, 10, false, 5, 8);
  228. testTruncateToFit (1, 10, 4, 3, true, 1, 2);
  229. testTruncateToFit (4, 3, 1, 10, false, 4, 3);
  230. testTruncateToFit (1, 3, 5, 3, true, 1, 3);
  231. testTruncateToFit (5, 3, 1, 3, false, 5, 3);
  232. testTruncateToFit (1, 3, 7, 3, true, 1, 3);
  233. testTruncateToFit (7, 3, 1, 3, false, 7, 3);
  234. testTruncateToFit (1, 10, 2, 10, false, 1, 10);
  235. testTruncateToFit (2, 10, 1, 10, false, 2, 10);
  236. }
  237. }
  238. private:
  239. //==============================================================================
  240. void testOverlapsWith (int masterChannelFirst, int numNoteChannelsFirst,
  241. int masterChannelSecond, int numNoteChannelsSecond,
  242. bool expectedRetVal)
  243. {
  244. MPEZone first (masterChannelFirst, numNoteChannelsFirst);
  245. MPEZone second (masterChannelSecond, numNoteChannelsSecond);
  246. expect (first.overlapsWith (second) == expectedRetVal);
  247. expect (second.overlapsWith (first) == expectedRetVal);
  248. }
  249. //==============================================================================
  250. void testTruncateToFit (int masterChannelFirst, int numNoteChannelsFirst,
  251. int masterChannelSecond, int numNoteChannelsSecond,
  252. bool expectedRetVal,
  253. int masterChannelFirstAfter, int numNoteChannelsFirstAfter)
  254. {
  255. MPEZone first (masterChannelFirst, numNoteChannelsFirst);
  256. MPEZone second (masterChannelSecond, numNoteChannelsSecond);
  257. expect (first.truncateToFit (second) == expectedRetVal);
  258. expectEquals (first.getMasterChannel(), masterChannelFirstAfter);
  259. expectEquals (first.getNumNoteChannels(), numNoteChannelsFirstAfter);
  260. }
  261. };
  262. static MPEZoneTests MPEZoneUnitTests;
  263. #endif // JUCE_UNIT_TESTS
  264. } // namespace juce