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

8 years ago
8 years ago
8 years ago
8 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2016 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of the ISC license
  6. http://www.isc.org/downloads/software-support-policy/isc-license/
  7. Permission to use, copy, modify, and/or distribute this software for any
  8. purpose with or without fee is hereby granted, provided that the above
  9. copyright notice and this permission notice appear in all copies.
  10. THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD
  11. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  12. FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
  13. OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  14. USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  15. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  16. OF THIS SOFTWARE.
  17. -----------------------------------------------------------------------------
  18. To release a closed-source product which uses other parts of JUCE not
  19. licensed under the ISC terms, commercial licenses are available: visit
  20. www.juce.com for more information.
  21. ==============================================================================
  22. */
  23. namespace
  24. {
  25. void checkAndLimitZoneParameters (int minValue,
  26. int maxValue,
  27. int& valueToCheckAndLimit) noexcept
  28. {
  29. if (valueToCheckAndLimit < minValue || valueToCheckAndLimit > maxValue)
  30. {
  31. // if you hit this, one of the parameters you supplied for MPEZone
  32. // was not within the allowed range!
  33. // we fit this back into the allowed range here to maintain a valid
  34. // state for the zone, but probably the resulting zone is not what you
  35. //wanted it to be!
  36. jassertfalse;
  37. valueToCheckAndLimit = jlimit (minValue, maxValue, valueToCheckAndLimit);
  38. }
  39. }
  40. }
  41. //==============================================================================
  42. MPEZone::MPEZone (int masterChannel_,
  43. int numNoteChannels_,
  44. int perNotePitchbendRange_,
  45. int masterPitchbendRange_) noexcept
  46. : masterChannel (masterChannel_),
  47. numNoteChannels (numNoteChannels_),
  48. perNotePitchbendRange (perNotePitchbendRange_),
  49. masterPitchbendRange (masterPitchbendRange_)
  50. {
  51. checkAndLimitZoneParameters (1, 15, masterChannel);
  52. checkAndLimitZoneParameters (1, 16 - masterChannel, numNoteChannels);
  53. checkAndLimitZoneParameters (0, 96, perNotePitchbendRange);
  54. checkAndLimitZoneParameters (0, 96, masterPitchbendRange);
  55. }
  56. //==============================================================================
  57. int MPEZone::getMasterChannel() const noexcept
  58. {
  59. return masterChannel;
  60. }
  61. int MPEZone::getNumNoteChannels() const noexcept
  62. {
  63. return numNoteChannels;
  64. }
  65. int MPEZone::getFirstNoteChannel() const noexcept
  66. {
  67. return masterChannel + 1;
  68. }
  69. int MPEZone::getLastNoteChannel() const noexcept
  70. {
  71. return masterChannel + numNoteChannels;
  72. }
  73. Range<int> MPEZone::getNoteChannelRange() const noexcept
  74. {
  75. return Range<int>::withStartAndLength (getFirstNoteChannel(), getNumNoteChannels());
  76. }
  77. bool MPEZone::isUsingChannel (int channel) const noexcept
  78. {
  79. jassert (channel > 0 && channel <= 16);
  80. return channel >= masterChannel && channel <= masterChannel + numNoteChannels;
  81. }
  82. bool MPEZone::isUsingChannelAsNoteChannel (int channel) const noexcept
  83. {
  84. jassert (channel > 0 && channel <= 16);
  85. return channel > masterChannel && channel <= masterChannel + numNoteChannels;
  86. }
  87. int MPEZone::getPerNotePitchbendRange() const noexcept
  88. {
  89. return perNotePitchbendRange;
  90. }
  91. int MPEZone::getMasterPitchbendRange() const noexcept
  92. {
  93. return masterPitchbendRange;
  94. }
  95. void MPEZone::setPerNotePitchbendRange (int rangeInSemitones) noexcept
  96. {
  97. checkAndLimitZoneParameters (0, 96, rangeInSemitones);
  98. perNotePitchbendRange = rangeInSemitones;
  99. }
  100. void MPEZone::setMasterPitchbendRange (int rangeInSemitones) noexcept
  101. {
  102. checkAndLimitZoneParameters (0, 96, rangeInSemitones);
  103. masterPitchbendRange = rangeInSemitones;
  104. }
  105. //==============================================================================
  106. bool MPEZone::overlapsWith (MPEZone other) const noexcept
  107. {
  108. if (masterChannel == other.masterChannel)
  109. return true;
  110. if (masterChannel > other.masterChannel)
  111. return other.overlapsWith (*this);
  112. return masterChannel + numNoteChannels >= other.masterChannel;
  113. }
  114. //==============================================================================
  115. bool MPEZone::truncateToFit (MPEZone other) noexcept
  116. {
  117. const int masterChannelDiff = other.masterChannel - masterChannel;
  118. // we need at least 2 channels to be left after truncation:
  119. // 1 master channel and 1 note channel. otherwise we can't truncate.
  120. if (masterChannelDiff < 2)
  121. return false;
  122. numNoteChannels = jmin (numNoteChannels, masterChannelDiff - 1);
  123. return true;
  124. }
  125. //==============================================================================
  126. bool MPEZone::operator== (const MPEZone& other) const noexcept
  127. {
  128. return masterChannel == other.masterChannel
  129. && numNoteChannels == other.numNoteChannels
  130. && perNotePitchbendRange == other.perNotePitchbendRange
  131. && masterPitchbendRange == other.masterPitchbendRange;
  132. }
  133. bool MPEZone::operator!= (const MPEZone& other) const noexcept
  134. {
  135. return ! operator== (other);
  136. }
  137. //==============================================================================
  138. //==============================================================================
  139. #if JUCE_UNIT_TESTS
  140. class MPEZoneTests : public UnitTest
  141. {
  142. public:
  143. MPEZoneTests() : UnitTest ("MPEZone class") {}
  144. void runTest() override
  145. {
  146. beginTest ("initialisation");
  147. {
  148. {
  149. MPEZone zone (1, 10);
  150. expectEquals (zone.getMasterChannel(), 1);
  151. expectEquals (zone.getNumNoteChannels(), 10);
  152. expectEquals (zone.getFirstNoteChannel(), 2);
  153. expectEquals (zone.getLastNoteChannel(), 11);
  154. expectEquals (zone.getPerNotePitchbendRange(), 48);
  155. expectEquals (zone.getMasterPitchbendRange(), 2);
  156. expect (zone.isUsingChannel (1));
  157. expect (zone.isUsingChannel (2));
  158. expect (zone.isUsingChannel (10));
  159. expect (zone.isUsingChannel (11));
  160. expect (! zone.isUsingChannel (12));
  161. expect (! zone.isUsingChannel (16));
  162. expect (! zone.isUsingChannelAsNoteChannel (1));
  163. expect (zone.isUsingChannelAsNoteChannel (2));
  164. expect (zone.isUsingChannelAsNoteChannel (10));
  165. expect (zone.isUsingChannelAsNoteChannel (11));
  166. expect (! zone.isUsingChannelAsNoteChannel (12));
  167. expect (! zone.isUsingChannelAsNoteChannel (16));
  168. }
  169. {
  170. MPEZone zone (5, 4);
  171. expectEquals (zone.getMasterChannel(), 5);
  172. expectEquals (zone.getNumNoteChannels(), 4);
  173. expectEquals (zone.getFirstNoteChannel(), 6);
  174. expectEquals (zone.getLastNoteChannel(), 9);
  175. expectEquals (zone.getPerNotePitchbendRange(), 48);
  176. expectEquals (zone.getMasterPitchbendRange(), 2);
  177. expect (! zone.isUsingChannel (1));
  178. expect (! zone.isUsingChannel (4));
  179. expect (zone.isUsingChannel (5));
  180. expect (zone.isUsingChannel (6));
  181. expect (zone.isUsingChannel (8));
  182. expect (zone.isUsingChannel (9));
  183. expect (! zone.isUsingChannel (10));
  184. expect (! zone.isUsingChannel (16));
  185. expect (! zone.isUsingChannelAsNoteChannel (5));
  186. expect (zone.isUsingChannelAsNoteChannel (6));
  187. expect (zone.isUsingChannelAsNoteChannel (8));
  188. expect (zone.isUsingChannelAsNoteChannel (9));
  189. expect (! zone.isUsingChannelAsNoteChannel (10));
  190. }
  191. }
  192. beginTest ("getNoteChannelRange");
  193. {
  194. MPEZone zone (2, 10);
  195. Range<int> noteChannelRange = zone.getNoteChannelRange();
  196. expectEquals (noteChannelRange.getStart(), 3);
  197. expectEquals (noteChannelRange.getEnd(), 13);
  198. }
  199. beginTest ("setting master pitchbend range");
  200. {
  201. MPEZone zone (1, 10);
  202. zone.setMasterPitchbendRange (96);
  203. expectEquals (zone.getMasterPitchbendRange(), 96);
  204. zone.setMasterPitchbendRange (0);
  205. expectEquals (zone.getMasterPitchbendRange(), 0);
  206. expectEquals (zone.getPerNotePitchbendRange(), 48);
  207. }
  208. beginTest ("setting per-note pitchbend range");
  209. {
  210. MPEZone zone (1, 10);
  211. zone.setPerNotePitchbendRange (96);
  212. expectEquals (zone.getPerNotePitchbendRange(), 96);
  213. zone.setPerNotePitchbendRange (0);
  214. expectEquals (zone.getPerNotePitchbendRange(), 0);
  215. expectEquals (zone.getMasterPitchbendRange(), 2);
  216. }
  217. beginTest ("checking overlap");
  218. {
  219. testOverlapsWith (1, 10, 1, 10, true);
  220. testOverlapsWith (1, 4, 6, 3, false);
  221. testOverlapsWith (1, 4, 8, 3, false);
  222. testOverlapsWith (2, 10, 2, 8, true);
  223. testOverlapsWith (1, 10, 3, 2, true);
  224. testOverlapsWith (3, 10, 5, 9, true);
  225. }
  226. beginTest ("truncating");
  227. {
  228. testTruncateToFit (1, 10, 3, 10, true, 1, 1);
  229. testTruncateToFit (3, 10, 1, 10, false, 3, 10);
  230. testTruncateToFit (1, 10, 5, 8, true, 1, 3);
  231. testTruncateToFit (5, 8, 1, 10, false, 5, 8);
  232. testTruncateToFit (1, 10, 4, 3, true, 1, 2);
  233. testTruncateToFit (4, 3, 1, 10, false, 4, 3);
  234. testTruncateToFit (1, 3, 5, 3, true, 1, 3);
  235. testTruncateToFit (5, 3, 1, 3, false, 5, 3);
  236. testTruncateToFit (1, 3, 7, 3, true, 1, 3);
  237. testTruncateToFit (7, 3, 1, 3, false, 7, 3);
  238. testTruncateToFit (1, 10, 2, 10, false, 1, 10);
  239. testTruncateToFit (2, 10, 1, 10, false, 2, 10);
  240. }
  241. }
  242. private:
  243. //==============================================================================
  244. void testOverlapsWith (int masterChannelFirst, int numNoteChannelsFirst,
  245. int masterChannelSecond, int numNoteChannelsSecond,
  246. bool expectedRetVal)
  247. {
  248. MPEZone first (masterChannelFirst, numNoteChannelsFirst);
  249. MPEZone second (masterChannelSecond, numNoteChannelsSecond);
  250. expect (first.overlapsWith (second) == expectedRetVal);
  251. expect (second.overlapsWith (first) == expectedRetVal);
  252. }
  253. //==============================================================================
  254. void testTruncateToFit (int masterChannelFirst, int numNoteChannelsFirst,
  255. int masterChannelSecond, int numNoteChannelsSecond,
  256. bool expectedRetVal,
  257. int masterChannelFirstAfter, int numNoteChannelsFirstAfter)
  258. {
  259. MPEZone first (masterChannelFirst, numNoteChannelsFirst);
  260. MPEZone second (masterChannelSecond, numNoteChannelsSecond);
  261. expect (first.truncateToFit (second) == expectedRetVal);
  262. expectEquals (first.getMasterChannel(), masterChannelFirstAfter);
  263. expectEquals (first.getNumNoteChannels(), numNoteChannelsFirstAfter);
  264. }
  265. };
  266. static MPEZoneTests MPEZoneUnitTests;
  267. #endif // JUCE_UNIT_TESTS