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.

377 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. 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() : UnitTest ("MidiRPNDetector class", "MIDI/MPE") {}
  136. void runTest() override
  137. {
  138. beginTest ("7-bit RPN");
  139. {
  140. MidiRPNDetector detector;
  141. MidiRPNMessage rpn;
  142. expect (! detector.parseControllerMessage (2, 101, 0, rpn));
  143. expect (! detector.parseControllerMessage (2, 100, 7, rpn));
  144. expect (detector.parseControllerMessage (2, 6, 42, rpn));
  145. expectEquals (rpn.channel, 2);
  146. expectEquals (rpn.parameterNumber, 7);
  147. expectEquals (rpn.value, 42);
  148. expect (! rpn.isNRPN);
  149. expect (! rpn.is14BitValue);
  150. }
  151. beginTest ("14-bit RPN");
  152. {
  153. MidiRPNDetector detector;
  154. MidiRPNMessage rpn;
  155. expect (! detector.parseControllerMessage (1, 100, 44, rpn));
  156. expect (! detector.parseControllerMessage (1, 101, 2, rpn));
  157. expect (! detector.parseControllerMessage (1, 38, 94, rpn));
  158. expect (detector.parseControllerMessage (1, 6, 1, rpn));
  159. expectEquals (rpn.channel, 1);
  160. expectEquals (rpn.parameterNumber, 300);
  161. expectEquals (rpn.value, 222);
  162. expect (! rpn.isNRPN);
  163. expect (rpn.is14BitValue);
  164. }
  165. beginTest ("RPNs on multiple channels simultaneously");
  166. {
  167. MidiRPNDetector detector;
  168. MidiRPNMessage rpn;
  169. expect (! detector.parseControllerMessage (1, 100, 44, rpn));
  170. expect (! detector.parseControllerMessage (2, 101, 0, rpn));
  171. expect (! detector.parseControllerMessage (1, 101, 2, rpn));
  172. expect (! detector.parseControllerMessage (2, 100, 7, rpn));
  173. expect (! detector.parseControllerMessage (1, 38, 94, rpn));
  174. expect (detector.parseControllerMessage (2, 6, 42, rpn));
  175. expectEquals (rpn.channel, 2);
  176. expectEquals (rpn.parameterNumber, 7);
  177. expectEquals (rpn.value, 42);
  178. expect (! rpn.isNRPN);
  179. expect (! rpn.is14BitValue);
  180. expect (detector.parseControllerMessage (1, 6, 1, rpn));
  181. expectEquals (rpn.channel, 1);
  182. expectEquals (rpn.parameterNumber, 300);
  183. expectEquals (rpn.value, 222);
  184. expect (! rpn.isNRPN);
  185. expect (rpn.is14BitValue);
  186. }
  187. beginTest ("14-bit RPN with value within 7-bit range");
  188. {
  189. MidiRPNDetector detector;
  190. MidiRPNMessage rpn;
  191. expect (! detector.parseControllerMessage (16, 100, 0 , rpn));
  192. expect (! detector.parseControllerMessage (16, 101, 0, rpn));
  193. expect (! detector.parseControllerMessage (16, 38, 3, rpn));
  194. expect (detector.parseControllerMessage (16, 6, 0, rpn));
  195. expectEquals (rpn.channel, 16);
  196. expectEquals (rpn.parameterNumber, 0);
  197. expectEquals (rpn.value, 3);
  198. expect (! rpn.isNRPN);
  199. expect (rpn.is14BitValue);
  200. }
  201. beginTest ("invalid RPN (wrong order)");
  202. {
  203. MidiRPNDetector detector;
  204. MidiRPNMessage rpn;
  205. expect (! detector.parseControllerMessage (2, 6, 42, rpn));
  206. expect (! detector.parseControllerMessage (2, 101, 0, rpn));
  207. expect (! detector.parseControllerMessage (2, 100, 7, rpn));
  208. }
  209. beginTest ("14-bit RPN interspersed with unrelated CC messages");
  210. {
  211. MidiRPNDetector detector;
  212. MidiRPNMessage rpn;
  213. expect (! detector.parseControllerMessage (16, 3, 80, rpn));
  214. expect (! detector.parseControllerMessage (16, 100, 0 , rpn));
  215. expect (! detector.parseControllerMessage (16, 4, 81, rpn));
  216. expect (! detector.parseControllerMessage (16, 101, 0, rpn));
  217. expect (! detector.parseControllerMessage (16, 5, 82, rpn));
  218. expect (! detector.parseControllerMessage (16, 5, 83, rpn));
  219. expect (! detector.parseControllerMessage (16, 38, 3, rpn));
  220. expect (! detector.parseControllerMessage (16, 4, 84, rpn));
  221. expect (! detector.parseControllerMessage (16, 3, 85, rpn));
  222. expect (detector.parseControllerMessage (16, 6, 0, rpn));
  223. expectEquals (rpn.channel, 16);
  224. expectEquals (rpn.parameterNumber, 0);
  225. expectEquals (rpn.value, 3);
  226. expect (! rpn.isNRPN);
  227. expect (rpn.is14BitValue);
  228. }
  229. beginTest ("14-bit NRPN");
  230. {
  231. MidiRPNDetector detector;
  232. MidiRPNMessage rpn;
  233. expect (! detector.parseControllerMessage (1, 98, 44, rpn));
  234. expect (! detector.parseControllerMessage (1, 99 , 2, rpn));
  235. expect (! detector.parseControllerMessage (1, 38, 94, rpn));
  236. expect (detector.parseControllerMessage (1, 6, 1, rpn));
  237. expectEquals (rpn.channel, 1);
  238. expectEquals (rpn.parameterNumber, 300);
  239. expectEquals (rpn.value, 222);
  240. expect (rpn.isNRPN);
  241. expect (rpn.is14BitValue);
  242. }
  243. beginTest ("reset");
  244. {
  245. MidiRPNDetector detector;
  246. MidiRPNMessage rpn;
  247. expect (! detector.parseControllerMessage (2, 101, 0, rpn));
  248. detector.reset();
  249. expect (! detector.parseControllerMessage (2, 100, 7, rpn));
  250. expect (! detector.parseControllerMessage (2, 6, 42, rpn));
  251. }
  252. }
  253. };
  254. static MidiRPNDetectorTests MidiRPNDetectorUnitTests;
  255. //==============================================================================
  256. class MidiRPNGeneratorTests : public UnitTest
  257. {
  258. public:
  259. MidiRPNGeneratorTests() : UnitTest ("MidiRPNGenerator class", "MIDI/MPE") {}
  260. void runTest() override
  261. {
  262. beginTest ("generating RPN/NRPN");
  263. {
  264. {
  265. MidiBuffer buffer = MidiRPNGenerator::generate (1, 23, 1337, true, true);
  266. expectContainsRPN (buffer, 1, 23, 1337, true, true);
  267. }
  268. {
  269. MidiBuffer buffer = MidiRPNGenerator::generate (16, 101, 34, false, false);
  270. expectContainsRPN (buffer, 16, 101, 34, false, false);
  271. }
  272. {
  273. MidiRPNMessage message = { 16, 101, 34, false, false };
  274. MidiBuffer buffer = MidiRPNGenerator::generate (message);
  275. expectContainsRPN (buffer, message);
  276. }
  277. }
  278. }
  279. private:
  280. //==============================================================================
  281. void expectContainsRPN (const MidiBuffer& midiBuffer,
  282. int channel,
  283. int parameterNumber,
  284. int value,
  285. bool isNRPN,
  286. bool is14BitValue)
  287. {
  288. MidiRPNMessage expected = { channel, parameterNumber, value, isNRPN, is14BitValue };
  289. expectContainsRPN (midiBuffer, expected);
  290. }
  291. //==============================================================================
  292. void expectContainsRPN (const MidiBuffer& midiBuffer, MidiRPNMessage expected)
  293. {
  294. MidiBuffer::Iterator iter (midiBuffer);
  295. MidiMessage midiMessage;
  296. MidiRPNMessage result = MidiRPNMessage();
  297. MidiRPNDetector detector;
  298. int samplePosition; // not actually used, so no need to initialise.
  299. while (iter.getNextEvent (midiMessage, samplePosition))
  300. {
  301. if (detector.parseControllerMessage (midiMessage.getChannel(),
  302. midiMessage.getControllerNumber(),
  303. midiMessage.getControllerValue(),
  304. result))
  305. break;
  306. }
  307. expectEquals (result.channel, expected.channel);
  308. expectEquals (result.parameterNumber, expected.parameterNumber);
  309. expectEquals (result.value, expected.value);
  310. expect (result.isNRPN == expected.isNRPN);
  311. expect (result.is14BitValue == expected.is14BitValue);
  312. }
  313. };
  314. static MidiRPNGeneratorTests MidiRPNGeneratorUnitTests;
  315. #endif // JUCE_UNIT_TESTS
  316. } // namespace juce