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.

245 lines
9.1KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 7 technical preview.
  4. Copyright (c) 2022 - Raw Material Software Limited
  5. You may use this code under the terms of the GPL v3
  6. (see www.gnu.org/licenses).
  7. For the technical preview this file cannot be licensed commercially.
  8. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  9. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  10. DISCLAIMED.
  11. ==============================================================================
  12. */
  13. namespace juce
  14. {
  15. /** Used to implement 'attachments' or 'controllers' that link a plug-in
  16. parameter to a UI element.
  17. To implement a new attachment type, create a new class which includes an
  18. instance of this class as a data member. Your class should pass a function
  19. to the constructor of the ParameterAttachment, which will then be called on
  20. the message thread when the parameter changes. You can use this function to
  21. update the state of the UI control. Your class should also register as a
  22. listener of the UI control and respond to respond to changes in the UI element
  23. by calling either setValueAsCompleteGesture or beginGesture,
  24. setValueAsPartOfGesture and endGesture.
  25. Make sure to call `sendInitialUpdate` at the end of your new attachment's
  26. constructor, so that the UI immediately reflects the state of the parameter.
  27. @tags{Audio}
  28. */
  29. class ParameterAttachment : private AudioProcessorParameter::Listener,
  30. private AsyncUpdater
  31. {
  32. public:
  33. /** Listens to a parameter and calls the the provided function in response to
  34. parameter changes. If an undoManager is supplied `beginNewTransaction` will
  35. be called on it whenever the UI requests a parameter change via this attachment.
  36. @param parameter The parameter to which this attachment will listen
  37. @param parameterChangedCallback The function that will be called on the message thread in response
  38. to parameter changes
  39. @param undoManager The UndoManager that will be used to begin transactions when the UI
  40. requests a parameter change.
  41. */
  42. ParameterAttachment (RangedAudioParameter& parameter,
  43. std::function<void (float)> parameterChangedCallback,
  44. UndoManager* undoManager = nullptr);
  45. /** Destructor. */
  46. ~ParameterAttachment() override;
  47. /** Calls the parameterChangedCallback function that was registered in
  48. the constructor, making the UI reflect the current parameter state.
  49. This function should be called after doing any necessary setup on
  50. the UI control that is being managed (e.g. adding ComboBox entries,
  51. making buttons toggle-able).
  52. */
  53. void sendInitialUpdate();
  54. /** Triggers a full gesture message on the managed parameter.
  55. Call this in the listener callback of the UI control in response
  56. to a one-off change in the UI like a button-press.
  57. */
  58. void setValueAsCompleteGesture (float newDenormalisedValue);
  59. /** Begins a gesture on the managed parameter.
  60. Call this when the UI is about to begin a continuous interaction,
  61. like when the mouse button is pressed on a slider.
  62. */
  63. void beginGesture();
  64. /** Updates the parameter value during a gesture.
  65. Call this during a continuous interaction, like a slider value
  66. changed callback.
  67. */
  68. void setValueAsPartOfGesture (float newDenormalisedValue);
  69. /** Ends a gesture on the managed parameter.
  70. Call this when the UI has finished a continuous interaction,
  71. like when the mouse button is released on a slider.
  72. */
  73. void endGesture();
  74. private:
  75. float normalise (float f) const { return parameter.convertTo0to1 (f); }
  76. template <typename Callback>
  77. void callIfParameterValueChanged (float newDenormalisedValue, Callback&& callback);
  78. void parameterValueChanged (int, float) override;
  79. void parameterGestureChanged (int, bool) override {}
  80. void handleAsyncUpdate() override;
  81. RangedAudioParameter& parameter;
  82. std::atomic<float> lastValue { 0.0f };
  83. UndoManager* undoManager = nullptr;
  84. std::function<void (float)> setValue;
  85. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ParameterAttachment)
  86. };
  87. //==============================================================================
  88. /** An object of this class maintains a connection between a Slider and a
  89. plug-in parameter.
  90. During the lifetime of this object it keeps the two things in sync, making
  91. it easy to connect a slider to a parameter. When this object is deleted, the
  92. connection is broken. Make sure that your parameter and Slider are not
  93. deleted before this object!
  94. @tags{Audio}
  95. */
  96. class SliderParameterAttachment : private Slider::Listener
  97. {
  98. public:
  99. /** Creates a connection between a plug-in parameter and a Slider.
  100. @param parameter The parameter to use
  101. @param slider The Slider to use
  102. @param undoManager An optional UndoManager
  103. */
  104. SliderParameterAttachment (RangedAudioParameter& parameter, Slider& slider,
  105. UndoManager* undoManager = nullptr);
  106. /** Destructor. */
  107. ~SliderParameterAttachment() override;
  108. /** Call this after setting up your slider in the case where you need to do
  109. extra setup after constructing this attachment.
  110. */
  111. void sendInitialUpdate();
  112. private:
  113. void setValue (float newValue);
  114. void sliderValueChanged (Slider*) override;
  115. void sliderDragStarted (Slider*) override { attachment.beginGesture(); }
  116. void sliderDragEnded (Slider*) override { attachment.endGesture(); }
  117. Slider& slider;
  118. ParameterAttachment attachment;
  119. bool ignoreCallbacks = false;
  120. };
  121. //==============================================================================
  122. /** An object of this class maintains a connection between a ComboBox and a
  123. plug-in parameter.
  124. ComboBox items will be spaced linearly across the range of the parameter. For
  125. example if the range is specified by NormalisableRange<float> (-0.5f, 0.5f, 0.5f)
  126. and you add three items then the first will be mapped to a value of -0.5, the
  127. second to 0, and the third to 0.5.
  128. During the lifetime of this object it keeps the two things in sync, making it
  129. easy to connect a combo box to a parameter. When this object is deleted, the
  130. connection is broken. Make sure that your parameter and ComboBox are not deleted
  131. before this object!
  132. @tags{Audio}
  133. */
  134. class ComboBoxParameterAttachment : private ComboBox::Listener
  135. {
  136. public:
  137. /** Creates a connection between a plug-in parameter and a ComboBox.
  138. @param parameter The parameter to use
  139. @param combo The ComboBox to use
  140. @param undoManager An optional UndoManager
  141. */
  142. ComboBoxParameterAttachment (RangedAudioParameter& parameter, ComboBox& combo,
  143. UndoManager* undoManager = nullptr);
  144. /** Destructor. */
  145. ~ComboBoxParameterAttachment() override;
  146. /** Call this after setting up your combo box in the case where you need to do
  147. extra setup after constructing this attachment.
  148. */
  149. void sendInitialUpdate();
  150. private:
  151. void setValue (float newValue);
  152. void comboBoxChanged (ComboBox*) override;
  153. ComboBox& comboBox;
  154. RangedAudioParameter& storedParameter;
  155. ParameterAttachment attachment;
  156. bool ignoreCallbacks = false;
  157. };
  158. //==============================================================================
  159. /** An object of this class maintains a connection between a Button and a
  160. plug-in parameter.
  161. During the lifetime of this object it keeps the two things in sync, making it
  162. easy to connect a button to a parameter. When this object is deleted, the
  163. connection is broken. Make sure that your parameter and Button are not deleted
  164. before this object!
  165. @tags{Audio}
  166. */
  167. class ButtonParameterAttachment : private Button::Listener
  168. {
  169. public:
  170. /** Creates a connection between a plug-in parameter and a Button.
  171. @param parameter The parameter to use
  172. @param button The Button to use
  173. @param undoManager An optional UndoManager
  174. */
  175. ButtonParameterAttachment (RangedAudioParameter& parameter, Button& button,
  176. UndoManager* undoManager = nullptr);
  177. /** Destructor. */
  178. ~ButtonParameterAttachment() override;
  179. /** Call this after setting up your button in the case where you need to do
  180. extra setup after constructing this attachment.
  181. */
  182. void sendInitialUpdate();
  183. private:
  184. void setValue (float newValue);
  185. void buttonClicked (Button*) override;
  186. Button& button;
  187. ParameterAttachment attachment;
  188. bool ignoreCallbacks = false;
  189. };
  190. } // namespace juce