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.

331 lines
13KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - 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. #ifndef DOXYGEN
  18. namespace juce
  19. {
  20. namespace universal_midi_packets
  21. {
  22. /**
  23. Functions to assist conversion of UMP messages to/from other formats,
  24. especially older 'bytestream' formatted MidiMessages.
  25. @tags{Audio}
  26. */
  27. struct Conversion
  28. {
  29. /** Converts from a MIDI 1 bytestream to MIDI 1 on Universal MIDI Packets.
  30. `callback` is a function which accepts a single View argument.
  31. */
  32. template <typename PacketCallbackFunction>
  33. static void toMidi1 (const MidiMessage& m, PacketCallbackFunction&& callback)
  34. {
  35. const auto* data = m.getRawData();
  36. const auto firstByte = data[0];
  37. const auto size = m.getRawDataSize();
  38. if (firstByte != 0xf0)
  39. {
  40. const auto mask = [size]() -> uint32_t
  41. {
  42. switch (size)
  43. {
  44. case 0: return 0xff000000;
  45. case 1: return 0xffff0000;
  46. case 2: return 0xffffff00;
  47. case 3: return 0xffffffff;
  48. }
  49. return 0x00000000;
  50. }();
  51. const auto extraByte = (uint8_t) ((((firstByte & 0xf0) == 0xf0) ? 0x1 : 0x2) << 0x4);
  52. const PacketX1 packet { mask & Utils::bytesToWord (extraByte, data[0], data[1], data[2]) };
  53. callback (View (packet.data()));
  54. return;
  55. }
  56. const auto numSysExBytes = m.getSysExDataSize();
  57. const auto numMessages = SysEx7::getNumPacketsRequiredForDataSize ((uint32_t) numSysExBytes);
  58. auto* dataOffset = m.getSysExData();
  59. if (numMessages <= 1)
  60. {
  61. const auto packet = Factory::makeSysExIn1Packet (0, (uint8_t) numSysExBytes, dataOffset);
  62. callback (View (packet.data()));
  63. return;
  64. }
  65. constexpr auto byteIncrement = 6;
  66. for (auto i = numSysExBytes; i > 0; i -= byteIncrement, dataOffset += byteIncrement)
  67. {
  68. const auto func = [&]
  69. {
  70. if (i == numSysExBytes)
  71. return Factory::makeSysExStart;
  72. if (i <= byteIncrement)
  73. return Factory::makeSysExEnd;
  74. return Factory::makeSysExContinue;
  75. }();
  76. const auto bytesNow = std::min (byteIncrement, i);
  77. const auto packet = func (0, (uint8_t) bytesNow, dataOffset);
  78. callback (View (packet.data()));
  79. }
  80. }
  81. /** Converts a MidiMessage to one or more messages in UMP format, using
  82. the MIDI 1.0 Protocol.
  83. `packets` is an out-param to allow the caller to control
  84. allocation/deallocation. Returning a new Packets object would
  85. require every call to toMidi1 to allocate. With this version, no
  86. allocations will occur, provided that `packets` has adequate reserved
  87. space.
  88. */
  89. static void toMidi1 (const MidiMessage& m, Packets& packets)
  90. {
  91. toMidi1 (m, [&] (const View& view) { packets.add (view); });
  92. }
  93. /** Widens a 7-bit MIDI 1.0 value to a 8-bit MIDI 2.0 value. */
  94. static uint8_t scaleTo8 (uint8_t word7Bit)
  95. {
  96. const auto shifted = (uint8_t) (word7Bit << 0x1);
  97. const auto repeat = (uint8_t) (word7Bit & 0x3f);
  98. const auto mask = (uint8_t) (word7Bit <= 0x40 ? 0x0 : 0xff);
  99. return (uint8_t) (shifted | ((repeat >> 5) & mask));
  100. }
  101. /** Widens a 7-bit MIDI 1.0 value to a 16-bit MIDI 2.0 value. */
  102. static uint16_t scaleTo16 (uint8_t word7Bit)
  103. {
  104. const auto shifted = (uint16_t) (word7Bit << 0x9);
  105. const auto repeat = (uint16_t) (word7Bit & 0x3f);
  106. const auto mask = (uint16_t) (word7Bit <= 0x40 ? 0x0 : 0xffff);
  107. return (uint16_t) (shifted | (((repeat << 3) | (repeat >> 3)) & mask));
  108. }
  109. /** Widens a 14-bit MIDI 1.0 value to a 16-bit MIDI 2.0 value. */
  110. static uint16_t scaleTo16 (uint16_t word14Bit)
  111. {
  112. const auto shifted = (uint16_t) (word14Bit << 0x2);
  113. const auto repeat = (uint16_t) (word14Bit & 0x1fff);
  114. const auto mask = (uint16_t) (word14Bit <= 0x2000 ? 0x0 : 0xffff);
  115. return (uint16_t) (shifted | ((repeat >> 11) & mask));
  116. }
  117. /** Widens a 7-bit MIDI 1.0 value to a 32-bit MIDI 2.0 value. */
  118. static uint32_t scaleTo32 (uint8_t word7Bit)
  119. {
  120. const auto shifted = (uint32_t) (word7Bit << 0x19);
  121. const auto repeat = (uint32_t) (word7Bit & 0x3f);
  122. const auto mask = (uint32_t) (word7Bit <= 0x40 ? 0x0 : 0xffffffff);
  123. return (uint32_t) (shifted | (((repeat << 19)
  124. | (repeat << 13)
  125. | (repeat << 7)
  126. | (repeat << 1)
  127. | (repeat >> 5)) & mask));
  128. }
  129. /** Widens a 14-bit MIDI 1.0 value to a 32-bit MIDI 2.0 value. */
  130. static uint32_t scaleTo32 (uint16_t word14Bit)
  131. {
  132. const auto shifted = (uint32_t) (word14Bit << 0x12);
  133. const auto repeat = (uint32_t) (word14Bit & 0x1fff);
  134. const auto mask = (uint32_t) (word14Bit <= 0x2000 ? 0x0 : 0xffffffff);
  135. return (uint32_t) (shifted | (((repeat << 5) | (repeat >> 8)) & mask));
  136. }
  137. /** Narrows a 16-bit MIDI 2.0 value to a 7-bit MIDI 1.0 value. */
  138. static uint8_t scaleTo7 (uint8_t word8Bit) { return (uint8_t) (word8Bit >> 1); }
  139. /** Narrows a 16-bit MIDI 2.0 value to a 7-bit MIDI 1.0 value. */
  140. static uint8_t scaleTo7 (uint16_t word16Bit) { return (uint8_t) (word16Bit >> 9); }
  141. /** Narrows a 32-bit MIDI 2.0 value to a 7-bit MIDI 1.0 value. */
  142. static uint8_t scaleTo7 (uint32_t word32Bit) { return (uint8_t) (word32Bit >> 25); }
  143. /** Narrows a 32-bit MIDI 2.0 value to a 14-bit MIDI 1.0 value. */
  144. static uint16_t scaleTo14 (uint16_t word16Bit) { return (uint16_t) (word16Bit >> 2); }
  145. /** Narrows a 32-bit MIDI 2.0 value to a 14-bit MIDI 1.0 value. */
  146. static uint16_t scaleTo14 (uint32_t word32Bit) { return (uint16_t) (word32Bit >> 18); }
  147. /** Converts UMP messages which may include MIDI 2.0 channel voice messages into
  148. equivalent MIDI 1.0 messages (still in UMP format).
  149. `callback` is a function that accepts a single View argument and will be
  150. called with each converted packet.
  151. Note that not all MIDI 2.0 messages have MIDI 1.0 equivalents, so such
  152. messages will be ignored.
  153. */
  154. template <typename Callback>
  155. static void midi2ToMidi1DefaultTranslation (const View& v, Callback&& callback)
  156. {
  157. const auto firstWord = v[0];
  158. if (Utils::getMessageType (firstWord) != 0x4)
  159. {
  160. callback (v);
  161. return;
  162. }
  163. const auto status = Utils::getStatus (firstWord);
  164. const auto typeAndGroup = (uint8_t) ((0x2 << 0x4) | Utils::getGroup (firstWord));
  165. switch (status)
  166. {
  167. case 0x8: // note off
  168. case 0x9: // note on
  169. case 0xa: // poly pressure
  170. case 0xb: // control change
  171. {
  172. const auto statusAndChannel = (uint8_t) ((firstWord >> 0x10) & 0xff);
  173. const auto byte2 = (uint8_t) ((firstWord >> 0x08) & 0xff);
  174. const auto byte3 = scaleTo7 (v[1]);
  175. // If this is a note-on, and the scaled byte is 0,
  176. // the scaled velocity should be 1 instead of 0
  177. const auto needsCorrection = status == 0x9 && byte3 == 0;
  178. const auto correctedByte = (uint8_t) (needsCorrection ? 1 : byte3);
  179. const auto shouldIgnore = status == 0xb && [&]
  180. {
  181. switch (byte2)
  182. {
  183. case 0:
  184. case 6:
  185. case 32:
  186. case 38:
  187. case 98:
  188. case 99:
  189. case 100:
  190. case 101:
  191. return true;
  192. }
  193. return false;
  194. }();
  195. if (shouldIgnore)
  196. return;
  197. const PacketX1 packet { Utils::bytesToWord (typeAndGroup,
  198. statusAndChannel,
  199. byte2,
  200. correctedByte) };
  201. callback (View (packet.data()));
  202. return;
  203. }
  204. case 0xd: // channel pressure
  205. {
  206. const auto statusAndChannel = (uint8_t) ((firstWord >> 0x10) & 0xff);
  207. const auto byte2 = scaleTo7 (v[1]);
  208. const PacketX1 packet { Utils::bytesToWord (typeAndGroup,
  209. statusAndChannel,
  210. byte2,
  211. 0) };
  212. callback (View (packet.data()));
  213. return;
  214. }
  215. case 0x2: // rpn
  216. case 0x3: // nrpn
  217. {
  218. const auto ccX = (uint8_t) (status == 0x2 ? 101 : 99);
  219. const auto ccY = (uint8_t) (status == 0x2 ? 100 : 98);
  220. const auto statusAndChannel = (uint8_t) ((0xb << 0x4) | Utils::getChannel (firstWord));
  221. const auto data = scaleTo14 (v[1]);
  222. const PacketX1 packets[]
  223. {
  224. PacketX1 { Utils::bytesToWord (typeAndGroup, statusAndChannel, ccX, (uint8_t) ((firstWord >> 0x8) & 0x7f)) },
  225. PacketX1 { Utils::bytesToWord (typeAndGroup, statusAndChannel, ccY, (uint8_t) ((firstWord >> 0x0) & 0x7f)) },
  226. PacketX1 { Utils::bytesToWord (typeAndGroup, statusAndChannel, 6, (uint8_t) ((data >> 0x7) & 0x7f)) },
  227. PacketX1 { Utils::bytesToWord (typeAndGroup, statusAndChannel, 38, (uint8_t) ((data >> 0x0) & 0x7f)) },
  228. };
  229. for (const auto& packet : packets)
  230. callback (View (packet.data()));
  231. return;
  232. }
  233. case 0xc: // program change / bank select
  234. {
  235. if (firstWord & 1)
  236. {
  237. const auto statusAndChannel = (uint8_t) ((0xb << 0x4) | Utils::getChannel (firstWord));
  238. const auto secondWord = v[1];
  239. const PacketX1 packets[]
  240. {
  241. PacketX1 { Utils::bytesToWord (typeAndGroup, statusAndChannel, 0, (uint8_t) ((secondWord >> 0x8) & 0x7f)) },
  242. PacketX1 { Utils::bytesToWord (typeAndGroup, statusAndChannel, 32, (uint8_t) ((secondWord >> 0x0) & 0x7f)) },
  243. };
  244. for (const auto& packet : packets)
  245. callback (View (packet.data()));
  246. }
  247. const auto statusAndChannel = (uint8_t) ((0xc << 0x4) | Utils::getChannel (firstWord));
  248. const PacketX1 packet { Utils::bytesToWord (typeAndGroup,
  249. statusAndChannel,
  250. (uint8_t) ((v[1] >> 0x18) & 0x7f),
  251. 0) };
  252. callback (View (packet.data()));
  253. return;
  254. }
  255. case 0xe: // pitch bend
  256. {
  257. const auto data = scaleTo14 (v[1]);
  258. const auto statusAndChannel = (uint8_t) ((firstWord >> 0x10) & 0xff);
  259. const PacketX1 packet { Utils::bytesToWord (typeAndGroup,
  260. statusAndChannel,
  261. (uint8_t) (data & 0x7f),
  262. (uint8_t) ((data >> 7) & 0x7f)) };
  263. callback (View (packet.data()));
  264. return;
  265. }
  266. default: // other message types do not translate
  267. return;
  268. }
  269. }
  270. };
  271. }
  272. }
  273. #endif