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.

199 lines
6.2KB

  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. namespace juce::universal_midi_packets
  18. {
  19. PacketX2 Midi1ToMidi2DefaultTranslator::processNoteOnOrOff (const HelperValues helpers)
  20. {
  21. const auto velocity = helpers.byte2;
  22. const auto needsConversion = (helpers.byte0 & std::byte { 0xf0 }) == std::byte { 0x90 } && velocity == std::byte { 0 };
  23. const auto firstByte = needsConversion ? (std::byte { 0x80 } | (helpers.byte0 & std::byte { 0xf }))
  24. : helpers.byte0;
  25. return PacketX2
  26. {
  27. Utils::bytesToWord (helpers.typeAndGroup, firstByte, helpers.byte1, std::byte { 0 }),
  28. (uint32_t) (Conversion::scaleTo16 (uint8_t (velocity)) << 0x10)
  29. };
  30. }
  31. PacketX2 Midi1ToMidi2DefaultTranslator::processPolyPressure (const HelperValues helpers)
  32. {
  33. return PacketX2
  34. {
  35. Utils::bytesToWord (helpers.typeAndGroup, helpers.byte0, helpers.byte1, std::byte { 0 }),
  36. Conversion::scaleTo32 (uint8_t (helpers.byte2))
  37. };
  38. }
  39. bool Midi1ToMidi2DefaultTranslator::processControlChange (const HelperValues helpers,
  40. PacketX2& packet)
  41. {
  42. const auto statusAndChannel = helpers.byte0;
  43. const auto cc = uint8_t (helpers.byte1);
  44. const auto shouldAccumulate = [&]
  45. {
  46. switch (cc)
  47. {
  48. case 6:
  49. case 38:
  50. case 98:
  51. case 99:
  52. case 100:
  53. case 101:
  54. return true;
  55. }
  56. return false;
  57. }();
  58. const auto group = (uint8_t) (helpers.typeAndGroup & std::byte { 0xf });
  59. const auto channel = (uint8_t) (statusAndChannel & std::byte { 0xf });
  60. const auto byte = helpers.byte2;
  61. if (shouldAccumulate)
  62. {
  63. auto& accumulator = groupAccumulators[group][channel];
  64. if (accumulator.addByte (cc, byte))
  65. {
  66. const auto& bytes = accumulator.getBytes();
  67. const auto bank = bytes[0];
  68. const auto index = bytes[1];
  69. const auto msb = bytes[2];
  70. const auto lsb = bytes[3];
  71. const auto value = uint16_t ((uint16_t (msb & std::byte { 0x7f }) << 7) | uint16_t (lsb & std::byte { 0x7f }));
  72. const auto newStatus = (uint8_t) (accumulator.getKind() == PnKind::nrpn ? 0x3 : 0x2);
  73. packet = PacketX2
  74. {
  75. Utils::bytesToWord (helpers.typeAndGroup, std::byte ((newStatus << 0x4) | channel), bank, index),
  76. Conversion::scaleTo32 (value)
  77. };
  78. return true;
  79. }
  80. return false;
  81. }
  82. if (cc == 0)
  83. {
  84. groupBanks[group][channel].setMsb (uint8_t (byte));
  85. return false;
  86. }
  87. if (cc == 32)
  88. {
  89. groupBanks[group][channel].setLsb (uint8_t (byte));
  90. return false;
  91. }
  92. packet = PacketX2
  93. {
  94. Utils::bytesToWord (helpers.typeAndGroup, statusAndChannel, std::byte { cc }, std::byte { 0 }),
  95. Conversion::scaleTo32 (uint8_t (helpers.byte2))
  96. };
  97. return true;
  98. }
  99. PacketX2 Midi1ToMidi2DefaultTranslator::processProgramChange (const HelperValues helpers) const
  100. {
  101. const auto group = (uint8_t) (helpers.typeAndGroup & std::byte { 0xf });
  102. const auto channel = (uint8_t) (helpers.byte0 & std::byte { 0xf });
  103. const auto bank = groupBanks[group][channel];
  104. const auto valid = bank.isValid();
  105. return PacketX2
  106. {
  107. Utils::bytesToWord (helpers.typeAndGroup,
  108. helpers.byte0,
  109. std::byte { 0 },
  110. valid ? std::byte { 1 } : std::byte { 0 }),
  111. Utils::bytesToWord (helpers.byte1,
  112. std::byte { 0 },
  113. valid ? std::byte { bank.getMsb() } : std::byte { 0 },
  114. valid ? std::byte { bank.getLsb() } : std::byte { 0 })
  115. };
  116. }
  117. PacketX2 Midi1ToMidi2DefaultTranslator::processChannelPressure (const HelperValues helpers)
  118. {
  119. return PacketX2
  120. {
  121. Utils::bytesToWord (helpers.typeAndGroup, helpers.byte0, std::byte { 0 }, std::byte { 0 }),
  122. Conversion::scaleTo32 (uint8_t (helpers.byte1))
  123. };
  124. }
  125. PacketX2 Midi1ToMidi2DefaultTranslator::processPitchBend (const HelperValues helpers)
  126. {
  127. const auto lsb = helpers.byte1;
  128. const auto msb = helpers.byte2;
  129. const auto value = uint16_t (uint16_t (msb) << 7 | uint16_t (lsb));
  130. return PacketX2
  131. {
  132. Utils::bytesToWord (helpers.typeAndGroup, helpers.byte0, std::byte { 0 }, std::byte { 0 }),
  133. Conversion::scaleTo32 (value)
  134. };
  135. }
  136. bool Midi1ToMidi2DefaultTranslator::PnAccumulator::addByte (uint8_t cc, std::byte byte)
  137. {
  138. const auto isStart = cc == 99 || cc == 101;
  139. if (isStart)
  140. {
  141. kind = cc == 99 ? PnKind::nrpn : PnKind::rpn;
  142. index = 0;
  143. }
  144. bytes[index] = byte;
  145. const auto shouldContinue = [&]
  146. {
  147. switch (index)
  148. {
  149. case 0: return isStart;
  150. case 1: return kind == PnKind::nrpn ? cc == 98 : cc == 100;
  151. case 2: return cc == 6;
  152. case 3: return cc == 38;
  153. }
  154. return false;
  155. }();
  156. index = shouldContinue ? index + 1 : 0;
  157. if (index != bytes.size())
  158. return false;
  159. index = 0;
  160. return true;
  161. }
  162. } // namespace juce::universal_midi_packets