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.

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