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.

155 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. //==============================================================================
  20. /** Represents a MIDI RPN (registered parameter number) or NRPN (non-registered
  21. parameter number) message.
  22. @tags{Audio}
  23. */
  24. struct MidiRPNMessage
  25. {
  26. /** Midi channel of the message, in the range 1 to 16. */
  27. int channel;
  28. /** The 14-bit parameter index, in the range 0 to 16383 (0x3fff). */
  29. int parameterNumber;
  30. /** The parameter value, in the range 0 to 16383 (0x3fff).
  31. If the message contains no value LSB, the value will be in the range
  32. 0 to 127 (0x7f).
  33. */
  34. int value;
  35. /** True if this message is an NRPN; false if it is an RPN. */
  36. bool isNRPN;
  37. /** True if the value uses 14-bit resolution (LSB + MSB); false if
  38. the value is 7-bit (MSB only).
  39. */
  40. bool is14BitValue;
  41. };
  42. //==============================================================================
  43. /**
  44. Parses a stream of MIDI data to assemble RPN and NRPN messages from their
  45. constituent MIDI CC messages.
  46. The detector uses the following parsing rules: the parameter number
  47. LSB/MSB can be sent/received in either order and must both come before the
  48. parameter value; for the parameter value, LSB always has to be sent/received
  49. before the value MSB, otherwise it will be treated as 7-bit (MSB only).
  50. @tags{Audio}
  51. */
  52. class JUCE_API MidiRPNDetector
  53. {
  54. public:
  55. /** Constructor. */
  56. MidiRPNDetector() noexcept;
  57. /** Destructor. */
  58. ~MidiRPNDetector() noexcept;
  59. /** Resets the RPN detector's internal state, so that it forgets about
  60. previously received MIDI CC messages.
  61. */
  62. void reset() noexcept;
  63. //==============================================================================
  64. /** Takes the next in a stream of incoming MIDI CC messages and returns true
  65. if it forms the last of a sequence that makes an RPN or NPRN.
  66. If this returns true, then the RPNMessage object supplied will be
  67. filled-out with the message's details.
  68. (If it returns false then the RPNMessage object will be unchanged).
  69. */
  70. bool parseControllerMessage (int midiChannel,
  71. int controllerNumber,
  72. int controllerValue,
  73. MidiRPNMessage& result) noexcept;
  74. private:
  75. //==============================================================================
  76. struct ChannelState
  77. {
  78. ChannelState() noexcept;
  79. bool handleController (int channel, int controllerNumber,
  80. int value, MidiRPNMessage&) noexcept;
  81. void resetValue() noexcept;
  82. bool sendIfReady (int channel, MidiRPNMessage&) noexcept;
  83. uint8 parameterMSB, parameterLSB, valueMSB, valueLSB;
  84. bool isNRPN;
  85. };
  86. //==============================================================================
  87. ChannelState states[16];
  88. JUCE_LEAK_DETECTOR (MidiRPNDetector)
  89. };
  90. //==============================================================================
  91. /**
  92. Generates an appropriate sequence of MIDI CC messages to represent an RPN
  93. or NRPN message.
  94. This sequence (as a MidiBuffer) can then be directly sent to a MidiOutput.
  95. @tags{Audio}
  96. */
  97. class JUCE_API MidiRPNGenerator
  98. {
  99. public:
  100. //==============================================================================
  101. /** Generates a MIDI sequence representing the given RPN or NRPN message. */
  102. static MidiBuffer generate (MidiRPNMessage message);
  103. //==============================================================================
  104. /** Generates a MIDI sequence representing an RPN or NRPN message with the
  105. given parameters.
  106. @param channel The MIDI channel of the RPN/NRPN message.
  107. @param parameterNumber The parameter number, in the range 0 to 16383.
  108. @param value The parameter value, in the range 0 to 16383, or
  109. in the range 0 to 127 if sendAs14BitValue is false.
  110. @param isNRPN Whether you need a MIDI RPN or NRPN sequence (RPN is default).
  111. @param use14BitValue If true (default), the value will have 14-bit precision
  112. (two MIDI bytes). If false, instead the value will have
  113. 7-bit precision (a single MIDI byte).
  114. */
  115. static MidiBuffer generate (int channel,
  116. int parameterNumber,
  117. int value,
  118. bool isNRPN = false,
  119. bool use14BitValue = true);
  120. };
  121. } // namespace juce