The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

327 lines
13KB

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