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.

155 lines
6.0KB

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