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.

356 lines
14KB

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