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.

239 lines
11KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. namespace juce
  20. {
  21. /**
  22. This class contains a ValueTree which is used to manage an AudioProcessor's entire state.
  23. It has its own internal class of parameter object which are linked to values
  24. within its ValueTree, and which are each identified by a string ID.
  25. You can get access to the underlying ValueTree object via the state member variable,
  26. so you can add extra properties to it as necessary.
  27. It also provides some utility child classes for connecting parameters directly to
  28. GUI controls like sliders.
  29. To use:
  30. 1) Create an AudioProcessorValueTreeState, and give it some parameters using createAndAddParameter().
  31. 2) Initialise the state member variable with a type name.
  32. */
  33. class JUCE_API AudioProcessorValueTreeState : private Timer,
  34. private ValueTree::Listener
  35. {
  36. public:
  37. /** Creates a state object for a given processor.
  38. The UndoManager is optional and can be a nullptr.
  39. After creating your state object, you should add parameters with the
  40. createAndAddParameter() method. Note that each AudioProcessorValueTreeState
  41. should be attached to only one processor, and must have the same lifetime as the
  42. processor, as they will have dependencies on each other.
  43. */
  44. AudioProcessorValueTreeState (AudioProcessor& processorToConnectTo,
  45. UndoManager* undoManagerToUse);
  46. /** Destructor. */
  47. ~AudioProcessorValueTreeState();
  48. /** Creates and returns a new parameter object for controlling a parameter
  49. with the given ID.
  50. Calling this will create and add a special type of AudioProcessorParameter to the
  51. AudioProcessor to which this state is attached.
  52. @param parameterID A unique string ID for the new parameter
  53. @param parameterName The name that the parameter will return from AudioProcessorParameter::getName()
  54. @param labelText The label that the parameter will return from AudioProcessorParameter::getLabel()
  55. @param valueRange A mapping that will be used to determine the value range which this parameter uses
  56. @param defaultValue A default value for the parameter (in non-normalised units)
  57. @param valueToTextFunction A function that will convert a non-normalised value to a string for the
  58. AudioProcessorParameter::getText() method. This can be nullptr to use the
  59. default implementation
  60. @param textToValueFunction The inverse of valueToTextFunction
  61. @param isMetaParameter Set this value to true if this should be a meta parameter
  62. @param isAutomatableParameter Set this value to false if this parameter should not be automatable
  63. @param isDiscrete Set this value to true to make this parameter take discrete values in a host.
  64. @see AudioProcessorParameter::isDiscrete
  65. @returns the parameter object that was created
  66. */
  67. AudioProcessorParameterWithID* createAndAddParameter (const String& parameterID,
  68. const String& parameterName,
  69. const String& labelText,
  70. NormalisableRange<float> valueRange,
  71. float defaultValue,
  72. std::function<String (float)> valueToTextFunction,
  73. std::function<float (const String&)> textToValueFunction,
  74. bool isMetaParameter = false,
  75. bool isAutomatableParameter = true,
  76. bool isDiscrete = false);
  77. /** Returns a parameter by its ID string. */
  78. AudioProcessorParameterWithID* getParameter (StringRef parameterID) const noexcept;
  79. /** Returns a pointer to a floating point representation of a particular
  80. parameter which a realtime process can read to find out its current value.
  81. */
  82. float* getRawParameterValue (StringRef parameterID) const noexcept;
  83. /** A listener class that can be attached to an AudioProcessorValueTreeState.
  84. Use AudioProcessorValueTreeState::addParameterListener() to register a callback.
  85. */
  86. struct JUCE_API Listener
  87. {
  88. Listener();
  89. virtual ~Listener();
  90. /** This callback method is called by the AudioProcessorValueTreeState when a parameter changes. */
  91. virtual void parameterChanged (const String& parameterID, float newValue) = 0;
  92. };
  93. /** Attaches a callback to one of the parameters, which will be called when the parameter changes. */
  94. void addParameterListener (StringRef parameterID, Listener* listener);
  95. /** Removes a callback that was previously added with addParameterCallback(). */
  96. void removeParameterListener (StringRef parameterID, Listener* listener);
  97. /** Returns a Value object that can be used to control a particular parameter. */
  98. Value getParameterAsValue (StringRef parameterID) const;
  99. /** Returns the range that was set when the given parameter was created. */
  100. NormalisableRange<float> getParameterRange (StringRef parameterID) const noexcept;
  101. /** A reference to the processor with which this state is associated. */
  102. AudioProcessor& processor;
  103. /** The state of the whole processor.
  104. This must be initialised after all calls to createAndAddParameter().
  105. You can replace this with your own ValueTree object, and can add properties and
  106. children to the tree. This class will automatically add children for each of the
  107. parameter objects that are created by createAndAddParameter().
  108. */
  109. ValueTree state;
  110. /** Provides access to the undo manager that this object is using. */
  111. UndoManager* const undoManager;
  112. //==============================================================================
  113. /** An object of this class maintains a connection between a Slider and a parameter
  114. in an AudioProcessorValueTreeState.
  115. During the lifetime of this SliderAttachment object, it keeps the two things in
  116. sync, making it easy to connect a slider to a parameter. When this object is
  117. deleted, the connection is broken. Make sure that your AudioProcessorValueTreeState
  118. and Slider aren't deleted before this object!
  119. */
  120. class JUCE_API SliderAttachment
  121. {
  122. public:
  123. SliderAttachment (AudioProcessorValueTreeState& stateToControl,
  124. const String& parameterID,
  125. Slider& sliderToControl);
  126. ~SliderAttachment();
  127. private:
  128. struct Pimpl;
  129. friend struct ContainerDeletePolicy<Pimpl>;
  130. ScopedPointer<Pimpl> pimpl;
  131. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (SliderAttachment)
  132. };
  133. //==============================================================================
  134. /** An object of this class maintains a connection between a ComboBox and a parameter
  135. in an AudioProcessorValueTreeState.
  136. During the lifetime of this ComboBoxAttachment object, it keeps the two things in
  137. sync, making it easy to connect a combo box to a parameter. When this object is
  138. deleted, the connection is broken. Make sure that your AudioProcessorValueTreeState
  139. and ComboBox aren't deleted before this object!
  140. */
  141. class JUCE_API ComboBoxAttachment
  142. {
  143. public:
  144. ComboBoxAttachment (AudioProcessorValueTreeState& stateToControl,
  145. const String& parameterID,
  146. ComboBox& comboBoxToControl);
  147. ~ComboBoxAttachment();
  148. private:
  149. struct Pimpl;
  150. friend struct ContainerDeletePolicy<Pimpl>;
  151. ScopedPointer<Pimpl> pimpl;
  152. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ComboBoxAttachment)
  153. };
  154. //==============================================================================
  155. /** An object of this class maintains a connection between a Button and a parameter
  156. in an AudioProcessorValueTreeState.
  157. During the lifetime of this ButtonAttachment object, it keeps the two things in
  158. sync, making it easy to connect a button to a parameter. When this object is
  159. deleted, the connection is broken. Make sure that your AudioProcessorValueTreeState
  160. and Button aren't deleted before this object!
  161. */
  162. class JUCE_API ButtonAttachment
  163. {
  164. public:
  165. ButtonAttachment (AudioProcessorValueTreeState& stateToControl,
  166. const String& parameterID,
  167. Button& buttonToControl);
  168. ~ButtonAttachment();
  169. private:
  170. struct Pimpl;
  171. friend struct ContainerDeletePolicy<Pimpl>;
  172. ScopedPointer<Pimpl> pimpl;
  173. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ButtonAttachment)
  174. };
  175. private:
  176. //==============================================================================
  177. struct Parameter;
  178. friend struct Parameter;
  179. ValueTree getOrCreateChildValueTree (const String&);
  180. void timerCallback() override;
  181. void valueTreePropertyChanged (ValueTree&, const Identifier&) override;
  182. void valueTreeChildAdded (ValueTree&, ValueTree&) override;
  183. void valueTreeChildRemoved (ValueTree&, ValueTree&, int) override;
  184. void valueTreeChildOrderChanged (ValueTree&, int, int) override;
  185. void valueTreeParentChanged (ValueTree&) override;
  186. void valueTreeRedirected (ValueTree&) override;
  187. void updateParameterConnectionsToChildTrees();
  188. Identifier valueType, valuePropertyID, idPropertyID;
  189. bool updatingConnections;
  190. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (AudioProcessorValueTreeState)
  191. };
  192. } // namespace juce