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.

381 lines
14KB

  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. MidiRPNDetector::MidiRPNDetector() noexcept
  20. {
  21. }
  22. MidiRPNDetector::~MidiRPNDetector() noexcept
  23. {
  24. }
  25. bool MidiRPNDetector::parseControllerMessage (int midiChannel,
  26. int controllerNumber,
  27. int controllerValue,
  28. MidiRPNMessage& result) noexcept
  29. {
  30. jassert (midiChannel >= 1 && midiChannel <= 16);
  31. jassert (controllerNumber >= 0 && controllerNumber < 128);
  32. jassert (controllerValue >= 0 && controllerValue < 128);
  33. return states[midiChannel - 1].handleController (midiChannel, controllerNumber, controllerValue, result);
  34. }
  35. void MidiRPNDetector::reset() noexcept
  36. {
  37. for (int i = 0; i < 16; ++i)
  38. {
  39. states[i].parameterMSB = 0xff;
  40. states[i].parameterLSB = 0xff;
  41. states[i].resetValue();
  42. states[i].isNRPN = false;
  43. }
  44. }
  45. //==============================================================================
  46. MidiRPNDetector::ChannelState::ChannelState() noexcept
  47. : parameterMSB (0xff), parameterLSB (0xff), valueMSB (0xff), valueLSB (0xff), isNRPN (false)
  48. {
  49. }
  50. bool MidiRPNDetector::ChannelState::handleController (int channel,
  51. int controllerNumber,
  52. int value,
  53. MidiRPNMessage& result) noexcept
  54. {
  55. switch (controllerNumber)
  56. {
  57. case 0x62: parameterLSB = uint8 (value); resetValue(); isNRPN = true; break;
  58. case 0x63: parameterMSB = uint8 (value); resetValue(); isNRPN = true; break;
  59. case 0x64: parameterLSB = uint8 (value); resetValue(); isNRPN = false; break;
  60. case 0x65: parameterMSB = uint8 (value); resetValue(); isNRPN = false; break;
  61. case 0x06: valueMSB = uint8 (value); return sendIfReady (channel, result);
  62. case 0x26: valueLSB = uint8 (value); break;
  63. default: break;
  64. }
  65. return false;
  66. }
  67. void MidiRPNDetector::ChannelState::resetValue() noexcept
  68. {
  69. valueMSB = 0xff;
  70. valueLSB = 0xff;
  71. }
  72. //==============================================================================
  73. bool MidiRPNDetector::ChannelState::sendIfReady (int channel, MidiRPNMessage& result) noexcept
  74. {
  75. if (parameterMSB < 0x80 && parameterLSB < 0x80)
  76. {
  77. if (valueMSB < 0x80)
  78. {
  79. result.channel = channel;
  80. result.parameterNumber = (parameterMSB << 7) + parameterLSB;
  81. result.isNRPN = isNRPN;
  82. if (valueLSB < 0x80)
  83. {
  84. result.value = (valueMSB << 7) + valueLSB;
  85. result.is14BitValue = true;
  86. }
  87. else
  88. {
  89. result.value = valueMSB;
  90. result.is14BitValue = false;
  91. }
  92. return true;
  93. }
  94. }
  95. return false;
  96. }
  97. //==============================================================================
  98. MidiBuffer MidiRPNGenerator::generate (MidiRPNMessage message)
  99. {
  100. return generate (message.channel,
  101. message.parameterNumber,
  102. message.value,
  103. message.isNRPN,
  104. message.is14BitValue);
  105. }
  106. MidiBuffer MidiRPNGenerator::generate (int midiChannel,
  107. int parameterNumber,
  108. int value,
  109. bool isNRPN,
  110. bool use14BitValue)
  111. {
  112. jassert (midiChannel > 0 && midiChannel <= 16);
  113. jassert (parameterNumber >= 0 && parameterNumber < 16384);
  114. jassert (value >= 0 && value < (use14BitValue ? 16384 : 128));
  115. uint8 parameterLSB = uint8 (parameterNumber & 0x0000007f);
  116. uint8 parameterMSB = uint8 (parameterNumber >> 7);
  117. uint8 valueLSB = use14BitValue ? uint8 (value & 0x0000007f) : 0x00;
  118. uint8 valueMSB = use14BitValue ? uint8 (value >> 7) : uint8 (value);
  119. uint8 channelByte = uint8 (0xb0 + midiChannel - 1);
  120. MidiBuffer buffer;
  121. buffer.addEvent (MidiMessage (channelByte, isNRPN ? 0x62 : 0x64, parameterLSB), 0);
  122. buffer.addEvent (MidiMessage (channelByte, isNRPN ? 0x63 : 0x65, parameterMSB), 0);
  123. // sending the value LSB is optional, but must come before sending the value MSB:
  124. if (use14BitValue)
  125. buffer.addEvent (MidiMessage (channelByte, 0x26, valueLSB), 0);
  126. buffer.addEvent (MidiMessage (channelByte, 0x06, valueMSB), 0);
  127. return buffer;
  128. }
  129. //==============================================================================
  130. //==============================================================================
  131. #if JUCE_UNIT_TESTS
  132. class MidiRPNDetectorTests : public UnitTest
  133. {
  134. public:
  135. MidiRPNDetectorTests()
  136. : UnitTest ("MidiRPNDetector class", UnitTestCategories::midi)
  137. {}
  138. void runTest() override
  139. {
  140. beginTest ("7-bit RPN");
  141. {
  142. MidiRPNDetector detector;
  143. MidiRPNMessage rpn;
  144. expect (! detector.parseControllerMessage (2, 101, 0, rpn));
  145. expect (! detector.parseControllerMessage (2, 100, 7, rpn));
  146. expect (detector.parseControllerMessage (2, 6, 42, rpn));
  147. expectEquals (rpn.channel, 2);
  148. expectEquals (rpn.parameterNumber, 7);
  149. expectEquals (rpn.value, 42);
  150. expect (! rpn.isNRPN);
  151. expect (! rpn.is14BitValue);
  152. }
  153. beginTest ("14-bit RPN");
  154. {
  155. MidiRPNDetector detector;
  156. MidiRPNMessage rpn;
  157. expect (! detector.parseControllerMessage (1, 100, 44, rpn));
  158. expect (! detector.parseControllerMessage (1, 101, 2, rpn));
  159. expect (! detector.parseControllerMessage (1, 38, 94, rpn));
  160. expect (detector.parseControllerMessage (1, 6, 1, rpn));
  161. expectEquals (rpn.channel, 1);
  162. expectEquals (rpn.parameterNumber, 300);
  163. expectEquals (rpn.value, 222);
  164. expect (! rpn.isNRPN);
  165. expect (rpn.is14BitValue);
  166. }
  167. beginTest ("RPNs on multiple channels simultaneously");
  168. {
  169. MidiRPNDetector detector;
  170. MidiRPNMessage rpn;
  171. expect (! detector.parseControllerMessage (1, 100, 44, rpn));
  172. expect (! detector.parseControllerMessage (2, 101, 0, rpn));
  173. expect (! detector.parseControllerMessage (1, 101, 2, rpn));
  174. expect (! detector.parseControllerMessage (2, 100, 7, rpn));
  175. expect (! detector.parseControllerMessage (1, 38, 94, rpn));
  176. expect (detector.parseControllerMessage (2, 6, 42, rpn));
  177. expectEquals (rpn.channel, 2);
  178. expectEquals (rpn.parameterNumber, 7);
  179. expectEquals (rpn.value, 42);
  180. expect (! rpn.isNRPN);
  181. expect (! rpn.is14BitValue);
  182. expect (detector.parseControllerMessage (1, 6, 1, rpn));
  183. expectEquals (rpn.channel, 1);
  184. expectEquals (rpn.parameterNumber, 300);
  185. expectEquals (rpn.value, 222);
  186. expect (! rpn.isNRPN);
  187. expect (rpn.is14BitValue);
  188. }
  189. beginTest ("14-bit RPN with value within 7-bit range");
  190. {
  191. MidiRPNDetector detector;
  192. MidiRPNMessage rpn;
  193. expect (! detector.parseControllerMessage (16, 100, 0 , rpn));
  194. expect (! detector.parseControllerMessage (16, 101, 0, rpn));
  195. expect (! detector.parseControllerMessage (16, 38, 3, rpn));
  196. expect (detector.parseControllerMessage (16, 6, 0, rpn));
  197. expectEquals (rpn.channel, 16);
  198. expectEquals (rpn.parameterNumber, 0);
  199. expectEquals (rpn.value, 3);
  200. expect (! rpn.isNRPN);
  201. expect (rpn.is14BitValue);
  202. }
  203. beginTest ("invalid RPN (wrong order)");
  204. {
  205. MidiRPNDetector detector;
  206. MidiRPNMessage rpn;
  207. expect (! detector.parseControllerMessage (2, 6, 42, rpn));
  208. expect (! detector.parseControllerMessage (2, 101, 0, rpn));
  209. expect (! detector.parseControllerMessage (2, 100, 7, rpn));
  210. }
  211. beginTest ("14-bit RPN interspersed with unrelated CC messages");
  212. {
  213. MidiRPNDetector detector;
  214. MidiRPNMessage rpn;
  215. expect (! detector.parseControllerMessage (16, 3, 80, rpn));
  216. expect (! detector.parseControllerMessage (16, 100, 0 , rpn));
  217. expect (! detector.parseControllerMessage (16, 4, 81, rpn));
  218. expect (! detector.parseControllerMessage (16, 101, 0, rpn));
  219. expect (! detector.parseControllerMessage (16, 5, 82, rpn));
  220. expect (! detector.parseControllerMessage (16, 5, 83, rpn));
  221. expect (! detector.parseControllerMessage (16, 38, 3, rpn));
  222. expect (! detector.parseControllerMessage (16, 4, 84, rpn));
  223. expect (! detector.parseControllerMessage (16, 3, 85, rpn));
  224. expect (detector.parseControllerMessage (16, 6, 0, rpn));
  225. expectEquals (rpn.channel, 16);
  226. expectEquals (rpn.parameterNumber, 0);
  227. expectEquals (rpn.value, 3);
  228. expect (! rpn.isNRPN);
  229. expect (rpn.is14BitValue);
  230. }
  231. beginTest ("14-bit NRPN");
  232. {
  233. MidiRPNDetector detector;
  234. MidiRPNMessage rpn;
  235. expect (! detector.parseControllerMessage (1, 98, 44, rpn));
  236. expect (! detector.parseControllerMessage (1, 99 , 2, rpn));
  237. expect (! detector.parseControllerMessage (1, 38, 94, rpn));
  238. expect (detector.parseControllerMessage (1, 6, 1, rpn));
  239. expectEquals (rpn.channel, 1);
  240. expectEquals (rpn.parameterNumber, 300);
  241. expectEquals (rpn.value, 222);
  242. expect (rpn.isNRPN);
  243. expect (rpn.is14BitValue);
  244. }
  245. beginTest ("reset");
  246. {
  247. MidiRPNDetector detector;
  248. MidiRPNMessage rpn;
  249. expect (! detector.parseControllerMessage (2, 101, 0, rpn));
  250. detector.reset();
  251. expect (! detector.parseControllerMessage (2, 100, 7, rpn));
  252. expect (! detector.parseControllerMessage (2, 6, 42, rpn));
  253. }
  254. }
  255. };
  256. static MidiRPNDetectorTests MidiRPNDetectorUnitTests;
  257. //==============================================================================
  258. class MidiRPNGeneratorTests : public UnitTest
  259. {
  260. public:
  261. MidiRPNGeneratorTests()
  262. : UnitTest ("MidiRPNGenerator class", UnitTestCategories::midi)
  263. {}
  264. void runTest() override
  265. {
  266. beginTest ("generating RPN/NRPN");
  267. {
  268. {
  269. MidiBuffer buffer = MidiRPNGenerator::generate (1, 23, 1337, true, true);
  270. expectContainsRPN (buffer, 1, 23, 1337, true, true);
  271. }
  272. {
  273. MidiBuffer buffer = MidiRPNGenerator::generate (16, 101, 34, false, false);
  274. expectContainsRPN (buffer, 16, 101, 34, false, false);
  275. }
  276. {
  277. MidiRPNMessage message = { 16, 101, 34, false, false };
  278. MidiBuffer buffer = MidiRPNGenerator::generate (message);
  279. expectContainsRPN (buffer, message);
  280. }
  281. }
  282. }
  283. private:
  284. //==============================================================================
  285. void expectContainsRPN (const MidiBuffer& midiBuffer,
  286. int channel,
  287. int parameterNumber,
  288. int value,
  289. bool isNRPN,
  290. bool is14BitValue)
  291. {
  292. MidiRPNMessage expected = { channel, parameterNumber, value, isNRPN, is14BitValue };
  293. expectContainsRPN (midiBuffer, expected);
  294. }
  295. //==============================================================================
  296. void expectContainsRPN (const MidiBuffer& midiBuffer, MidiRPNMessage expected)
  297. {
  298. MidiRPNMessage result = MidiRPNMessage();
  299. MidiRPNDetector detector;
  300. for (const auto metadata : midiBuffer)
  301. {
  302. const auto midiMessage = metadata.getMessage();
  303. if (detector.parseControllerMessage (midiMessage.getChannel(),
  304. midiMessage.getControllerNumber(),
  305. midiMessage.getControllerValue(),
  306. result))
  307. break;
  308. }
  309. expectEquals (result.channel, expected.channel);
  310. expectEquals (result.parameterNumber, expected.parameterNumber);
  311. expectEquals (result.value, expected.value);
  312. expect (result.isNRPN == expected.isNRPN);
  313. expect (result.is14BitValue == expected.is14BitValue);
  314. }
  315. };
  316. static MidiRPNGeneratorTests MidiRPNGeneratorUnitTests;
  317. #endif
  318. } // namespace juce