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.

159 lines
6.1KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2016 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of the ISC license
  6. http://www.isc.org/downloads/software-support-policy/isc-license/
  7. Permission to use, copy, modify, and/or distribute this software for any
  8. purpose with or without fee is hereby granted, provided that the above
  9. copyright notice and this permission notice appear in all copies.
  10. THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD
  11. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  12. FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
  13. OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  14. USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  15. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  16. OF THIS SOFTWARE.
  17. -----------------------------------------------------------------------------
  18. To release a closed-source product which uses other parts of JUCE not
  19. licensed under the ISC terms, commercial licenses are available: visit
  20. www.juce.com for more information.
  21. ==============================================================================
  22. */
  23. #ifndef JUCE_MIDIRPNDETECTOR_H_INCLUDED
  24. #define JUCE_MIDIRPNDETECTOR_H_INCLUDED
  25. //==============================================================================
  26. /** Represents a MIDI RPN (registered parameter number) or NRPN (non-registered
  27. parameter number) message.
  28. */
  29. struct MidiRPNMessage
  30. {
  31. /** Midi channel of the message, in the range 1 to 16. */
  32. int channel;
  33. /** The 14-bit parameter index, in the range 0 to 16383 (0x3fff). */
  34. int parameterNumber;
  35. /** The parameter value, in the range 0 to 16383 (0x3fff).
  36. If the message contains no value LSB, the value will be in the range
  37. 0 to 127 (0x7f).
  38. */
  39. int value;
  40. /** True if this message is an NRPN; false if it is an RPN. */
  41. bool isNRPN;
  42. /** True if the value uses 14-bit resolution (LSB + MSB); false if
  43. the value is 7-bit (MSB only).
  44. */
  45. bool is14BitValue;
  46. };
  47. //==============================================================================
  48. /**
  49. Parses a stream of MIDI data to assemble RPN and NRPN messages from their
  50. constituent MIDI CC messages.
  51. The detector uses the following parsing rules: the parameter number
  52. LSB/MSB can be sent/received in either order and must both come before the
  53. parameter value; for the parameter value, LSB always has to be sent/received
  54. before the value MSB, otherwise it will be treated as 7-bit (MSB only).
  55. */
  56. class JUCE_API MidiRPNDetector
  57. {
  58. public:
  59. /** Constructor. */
  60. MidiRPNDetector() noexcept;
  61. /** Destructor. */
  62. ~MidiRPNDetector() noexcept;
  63. /** Resets the RPN detector's internal state, so that it forgets about
  64. previously received MIDI CC messages.
  65. */
  66. void reset() noexcept;
  67. //==============================================================================
  68. /** Takes the next in a stream of incoming MIDI CC messages and returns true
  69. if it forms the last of a sequence that makes an RPN or NPRN.
  70. If this returns true, then the RPNMessage object supplied will be
  71. filled-out with the message's details.
  72. (If it returns false then the RPNMessage object will be unchanged).
  73. */
  74. bool parseControllerMessage (int midiChannel,
  75. int controllerNumber,
  76. int controllerValue,
  77. MidiRPNMessage& result) noexcept;
  78. private:
  79. //==============================================================================
  80. struct ChannelState
  81. {
  82. ChannelState() noexcept;
  83. bool handleController (int channel, int controllerNumber,
  84. int value, MidiRPNMessage&) noexcept;
  85. void resetValue() noexcept;
  86. bool sendIfReady (int channel, MidiRPNMessage&) noexcept;
  87. uint8 parameterMSB, parameterLSB, valueMSB, valueLSB;
  88. bool isNRPN;
  89. };
  90. //==============================================================================
  91. ChannelState states[16];
  92. JUCE_LEAK_DETECTOR (MidiRPNDetector)
  93. };
  94. //==============================================================================
  95. /**
  96. Generates an appropriate sequence of MIDI CC messages to represent an RPN
  97. or NRPN message.
  98. This sequence (as a MidiBuffer) can then be directly sent to a MidiOutput.
  99. */
  100. class JUCE_API MidiRPNGenerator
  101. {
  102. public:
  103. //==============================================================================
  104. /** Generates a MIDI sequence representing the given RPN or NRPN message. */
  105. static MidiBuffer generate (MidiRPNMessage message);
  106. //==============================================================================
  107. /** Generates a MIDI sequence representing an RPN or NRPN message with the
  108. given parameters.
  109. @param channel The MIDI channel of the RPN/NRPN message.
  110. @param parameterNumber The parameter number, in the range 0 to 16383.
  111. @param value The parameter value, in the range 0 to 16383, or
  112. in the range 0 to 127 if sendAs14BitValue is false.
  113. @param isNRPN Whether you need a MIDI RPN or NRPN sequence (RPN is default).
  114. @param use14BitValue If true (default), the value will have 14-bit precision
  115. (two MIDI bytes). If false, instead the value will have
  116. 7-bit presision (a single MIDI byte).
  117. */
  118. static MidiBuffer generate (int channel,
  119. int parameterNumber,
  120. int value,
  121. bool isNRPN = false,
  122. bool use14BitValue = true);
  123. };
  124. #endif // JUCE_MIDIRPNDETECTOR_H_INCLUDED