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.

juce_ParameterAttachments.h 9.0KB

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