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.

192 lines
5.6KB

  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. Translates a series of MIDI 1 Universal MIDI Packets to corresponding MIDI 2
  24. packets.
  25. @tags{Audio}
  26. */
  27. class Midi1ToMidi2DefaultTranslator
  28. {
  29. public:
  30. Midi1ToMidi2DefaultTranslator() = default;
  31. /** Converts MIDI 1 Universal MIDI Packets to corresponding MIDI 2 packets,
  32. calling `callback` with each converted packet.
  33. In some cases (such as RPN/NRPN messages) multiple MIDI 1 packets will
  34. convert to a single MIDI 2 packet. In these cases, the translator will
  35. accumulate the full message internally, and send a single callback with
  36. the completed message, once all the individual MIDI 1 packets have been
  37. processed.
  38. */
  39. template <typename PacketCallback>
  40. void dispatch (const View& v, PacketCallback&& callback)
  41. {
  42. const auto firstWord = v[0];
  43. const auto messageType = Utils::getMessageType (firstWord);
  44. if (messageType != 0x2)
  45. {
  46. callback (v);
  47. return;
  48. }
  49. const HelperValues helperValues
  50. {
  51. std::byte ((0x4 << 0x4) | Utils::getGroup (firstWord)),
  52. std::byte ((firstWord >> 0x10) & 0xff),
  53. std::byte ((firstWord >> 0x08) & 0x7f),
  54. std::byte ((firstWord >> 0x00) & 0x7f),
  55. };
  56. switch (Utils::getStatus (firstWord))
  57. {
  58. case 0x8:
  59. case 0x9:
  60. {
  61. const auto packet = processNoteOnOrOff (helperValues);
  62. callback (View (packet.data()));
  63. return;
  64. }
  65. case 0xa:
  66. {
  67. const auto packet = processPolyPressure (helperValues);
  68. callback (View (packet.data()));
  69. return;
  70. }
  71. case 0xb:
  72. {
  73. PacketX2 packet;
  74. if (processControlChange (helperValues, packet))
  75. callback (View (packet.data()));
  76. return;
  77. }
  78. case 0xc:
  79. {
  80. const auto packet = processProgramChange (helperValues);
  81. callback (View (packet.data()));
  82. return;
  83. }
  84. case 0xd:
  85. {
  86. const auto packet = processChannelPressure (helperValues);
  87. callback (View (packet.data()));
  88. return;
  89. }
  90. case 0xe:
  91. {
  92. const auto packet = processPitchBend (helperValues);
  93. callback (View (packet.data()));
  94. return;
  95. }
  96. }
  97. }
  98. void reset()
  99. {
  100. groupAccumulators = {};
  101. groupBanks = {};
  102. }
  103. private:
  104. enum class PnKind { nrpn, rpn };
  105. struct HelperValues
  106. {
  107. std::byte typeAndGroup;
  108. std::byte byte0;
  109. std::byte byte1;
  110. std::byte byte2;
  111. };
  112. static PacketX2 processNoteOnOrOff (const HelperValues helpers);
  113. static PacketX2 processPolyPressure (const HelperValues helpers);
  114. bool processControlChange (const HelperValues helpers, PacketX2& packet);
  115. PacketX2 processProgramChange (const HelperValues helpers) const;
  116. static PacketX2 processChannelPressure (const HelperValues helpers);
  117. static PacketX2 processPitchBend (const HelperValues helpers);
  118. class PnAccumulator
  119. {
  120. public:
  121. bool addByte (uint8_t cc, std::byte byte);
  122. const std::array<std::byte, 4>& getBytes() const noexcept { return bytes; }
  123. PnKind getKind() const noexcept { return kind; }
  124. private:
  125. std::array<std::byte, 4> bytes;
  126. uint8_t index = 0;
  127. PnKind kind = PnKind::nrpn;
  128. };
  129. class Bank
  130. {
  131. public:
  132. bool isValid() const noexcept { return ! (msb & 0x80); }
  133. uint8_t getMsb() const noexcept { return msb & 0x7f; }
  134. uint8_t getLsb() const noexcept { return lsb & 0x7f; }
  135. void setMsb (uint8_t i) noexcept { msb = i & 0x7f; }
  136. void setLsb (uint8_t i) noexcept { msb &= 0x7f; lsb = i & 0x7f; }
  137. private:
  138. // We use the top bit to indicate whether this bank is valid.
  139. // After reading the spec, it's not clear how we should determine whether
  140. // there are valid values, so we'll just assume that the bank is valid
  141. // once either the lsb or msb have been written.
  142. uint8_t msb = 0x80;
  143. uint8_t lsb = 0x00;
  144. };
  145. using ChannelAccumulators = std::array<PnAccumulator, 16>;
  146. std::array<ChannelAccumulators, 16> groupAccumulators;
  147. using ChannelBanks = std::array<Bank, 16>;
  148. std::array<ChannelBanks, 16> groupBanks;
  149. };
  150. }
  151. }
  152. #endif