Audio plugin host https://kx.studio/carla
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.

153 lines
5.7KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. #ifndef JUCE_MIDIRPNDETECTOR_H_INCLUDED
  18. #define JUCE_MIDIRPNDETECTOR_H_INCLUDED
  19. //==============================================================================
  20. /** Represents a MIDI RPN (registered parameter number) or NRPN (non-registered
  21. parameter number) message.
  22. */
  23. struct MidiRPNMessage
  24. {
  25. /** Midi channel of the message, in the range 1 to 16. */
  26. int channel;
  27. /** The 14-bit parameter index, in the range 0 to 16383 (0x3fff). */
  28. int parameterNumber;
  29. /** The parameter value, in the range 0 to 16383 (0x3fff).
  30. If the message contains no value LSB, the value will be in the range
  31. 0 to 127 (0x7f).
  32. */
  33. int value;
  34. /** True if this message is an NRPN; false if it is an RPN. */
  35. bool isNRPN;
  36. /** True if the value uses 14-bit resolution (LSB + MSB); false if
  37. the value is 7-bit (MSB only).
  38. */
  39. bool is14BitValue;
  40. };
  41. //==============================================================================
  42. /**
  43. Parses a stream of MIDI data to assemble RPN and NRPN messages from their
  44. constituent MIDI CC messages.
  45. The detector uses the following parsing rules: the parameter number
  46. LSB/MSB can be sent/received in either order and must both come before the
  47. parameter value; for the parameter value, LSB always has to be sent/received
  48. before the value MSB, otherwise it will be treated as 7-bit (MSB only).
  49. */
  50. class JUCE_API MidiRPNDetector
  51. {
  52. public:
  53. /** Constructor. */
  54. MidiRPNDetector() noexcept;
  55. /** Destructor. */
  56. ~MidiRPNDetector() noexcept;
  57. /** Resets the RPN detector's internal state, so that it forgets about
  58. previously received MIDI CC messages.
  59. */
  60. void reset() noexcept;
  61. //==============================================================================
  62. /** Takes the next in a stream of incoming MIDI CC messages and returns true
  63. if it forms the last of a sequence that makes an RPN or NPRN.
  64. If this returns true, then the RPNMessage object supplied will be
  65. filled-out with the message's details.
  66. (If it returns false then the RPNMessage object will be unchanged).
  67. */
  68. bool parseControllerMessage (int midiChannel,
  69. int controllerNumber,
  70. int controllerValue,
  71. MidiRPNMessage& result) noexcept;
  72. private:
  73. //==============================================================================
  74. struct ChannelState
  75. {
  76. ChannelState() noexcept;
  77. bool handleController (int channel, int controllerNumber,
  78. int value, MidiRPNMessage&) noexcept;
  79. void resetValue() noexcept;
  80. bool sendIfReady (int channel, MidiRPNMessage&) noexcept;
  81. uint8 parameterMSB, parameterLSB, valueMSB, valueLSB;
  82. bool isNRPN;
  83. };
  84. //==============================================================================
  85. ChannelState states[16];
  86. JUCE_LEAK_DETECTOR (MidiRPNDetector)
  87. };
  88. //==============================================================================
  89. /**
  90. Generates an appropriate sequence of MIDI CC messages to represent an RPN
  91. or NRPN message.
  92. This sequence (as a MidiBuffer) can then be directly sent to a MidiOutput.
  93. */
  94. class JUCE_API MidiRPNGenerator
  95. {
  96. public:
  97. //==============================================================================
  98. /** Generates a MIDI sequence representing the given RPN or NRPN message. */
  99. static MidiBuffer generate (MidiRPNMessage message);
  100. //==============================================================================
  101. /** Generates a MIDI sequence representing an RPN or NRPN message with the
  102. given parameters.
  103. @param channel The MIDI channel of the RPN/NRPN message.
  104. @param parameterNumber The parameter number, in the range 0 to 16383.
  105. @param value The parameter value, in the range 0 to 16383, or
  106. in the range 0 to 127 if sendAs14BitValue is false.
  107. @param isNRPN Whether you need a MIDI RPN or NRPN sequence (RPN is default).
  108. @param use14BitValue If true (default), the value will have 14-bit precision
  109. (two MIDI bytes). If false, instead the value will have
  110. 7-bit presision (a single MIDI byte).
  111. */
  112. static MidiBuffer generate (int channel,
  113. int parameterNumber,
  114. int value,
  115. bool isNRPN = false,
  116. bool use14BitValue = true);
  117. };
  118. #endif // JUCE_MIDIRPNDETECTOR_H_INCLUDED