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.

188 lines
5.6KB

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