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.

154 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. 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. bool handleController (int channel, int controllerNumber,
  79. int value, MidiRPNMessage&) noexcept;
  80. void resetValue() noexcept;
  81. bool sendIfReady (int channel, MidiRPNMessage&) noexcept;
  82. uint8 parameterMSB = 0xff, parameterLSB = 0xff, valueMSB = 0xff, valueLSB = 0xff;
  83. bool isNRPN = false;
  84. };
  85. //==============================================================================
  86. ChannelState states[16];
  87. JUCE_LEAK_DETECTOR (MidiRPNDetector)
  88. };
  89. //==============================================================================
  90. /**
  91. Generates an appropriate sequence of MIDI CC messages to represent an RPN
  92. or NRPN message.
  93. This sequence (as a MidiBuffer) can then be directly sent to a MidiOutput.
  94. @tags{Audio}
  95. */
  96. class JUCE_API MidiRPNGenerator
  97. {
  98. public:
  99. //==============================================================================
  100. /** Generates a MIDI sequence representing the given RPN or NRPN message. */
  101. static MidiBuffer generate (MidiRPNMessage message);
  102. //==============================================================================
  103. /** Generates a MIDI sequence representing an RPN or NRPN message with the
  104. given parameters.
  105. @param channel The MIDI channel of the RPN/NRPN message.
  106. @param parameterNumber The parameter number, in the range 0 to 16383.
  107. @param value The parameter value, in the range 0 to 16383, or
  108. in the range 0 to 127 if sendAs14BitValue is false.
  109. @param isNRPN Whether you need a MIDI RPN or NRPN sequence (RPN is default).
  110. @param use14BitValue If true (default), the value will have 14-bit precision
  111. (two MIDI bytes). If false, instead the value will have
  112. 7-bit precision (a single MIDI byte).
  113. */
  114. static MidiBuffer generate (int channel,
  115. int parameterNumber,
  116. int value,
  117. bool isNRPN = false,
  118. bool use14BitValue = true);
  119. };
  120. } // namespace juce