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.

564 lines
25KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - Raw Material Software Limited
  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 6 End-User License
  8. Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).
  9. End User License Agreement: www.juce.com/juce-6-licence
  10. Privacy Policy: www.juce.com/juce-privacy-policy
  11. Or: You may also use this code under the terms of the GPL v3 (see
  12. www.gnu.org/licenses).
  13. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  14. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  15. DISCLAIMED.
  16. ==============================================================================
  17. */
  18. namespace juce
  19. {
  20. /**
  21. This class contains a ValueTree that is used to manage an AudioProcessor's entire state.
  22. It has its own internal class of parameter object that is linked to values
  23. within its ValueTree, and which are each identified by a string ID.
  24. You can get access to the underlying ValueTree object via the state member variable,
  25. so you can add extra properties to it as necessary.
  26. It also provides some utility child classes for connecting parameters directly to
  27. GUI controls like sliders.
  28. The favoured constructor of this class takes a collection of RangedAudioParameters or
  29. AudioProcessorParameterGroups of RangedAudioParameters and adds them to the attached
  30. AudioProcessor directly.
  31. The deprecated way of using this class is as follows:
  32. 1) Create an AudioProcessorValueTreeState, and give it some parameters using createAndAddParameter().
  33. 2) Initialise the state member variable with a type name.
  34. The deprecated constructor will be removed from the API in a future version of JUCE!
  35. @tags{Audio}
  36. */
  37. class JUCE_API AudioProcessorValueTreeState : private Timer,
  38. private ValueTree::Listener
  39. {
  40. public:
  41. //==============================================================================
  42. /** A class to contain a set of RangedAudioParameters and AudioProcessorParameterGroups
  43. containing RangedAudioParameters.
  44. This class is used in the AudioProcessorValueTreeState constructor to allow
  45. arbitrarily grouped RangedAudioParameters to be passed to an AudioProcessor.
  46. */
  47. class JUCE_API ParameterLayout final
  48. {
  49. private:
  50. //==============================================================================
  51. template <typename It>
  52. using ValidIfIterator = decltype (std::next (std::declval<It>()));
  53. public:
  54. //==============================================================================
  55. template <typename... Items>
  56. ParameterLayout (std::unique_ptr<Items>... items) { add (std::move (items)...); }
  57. template <typename It, typename = ValidIfIterator<It>>
  58. ParameterLayout (It begin, It end) { add (begin, end); }
  59. template <typename... Items>
  60. void add (std::unique_ptr<Items>... items)
  61. {
  62. parameters.reserve (parameters.size() + sizeof... (items));
  63. // We can replace this with some nicer code once generic lambdas become available. A
  64. // sequential context like an array initialiser is required to ensure we get the correct
  65. // order from the parameter pack.
  66. int unused[] { (parameters.emplace_back (MakeContents() (std::move (items))), 0)... };
  67. ignoreUnused (unused);
  68. }
  69. template <typename It, typename = ValidIfIterator<It>>
  70. void add (It begin, It end)
  71. {
  72. parameters.reserve (parameters.size() + std::size_t (std::distance (begin, end)));
  73. std::transform (std::make_move_iterator (begin),
  74. std::make_move_iterator (end),
  75. std::back_inserter (parameters),
  76. MakeContents());
  77. }
  78. ParameterLayout (const ParameterLayout& other) = delete;
  79. ParameterLayout (ParameterLayout&& other) noexcept { swap (other); }
  80. ParameterLayout& operator= (const ParameterLayout& other) = delete;
  81. ParameterLayout& operator= (ParameterLayout&& other) noexcept { swap (other); return *this; }
  82. void swap (ParameterLayout& other) noexcept { std::swap (other.parameters, parameters); }
  83. private:
  84. //==============================================================================
  85. struct Visitor
  86. {
  87. virtual ~Visitor() = default;
  88. // If you have a compiler error telling you that there is no matching
  89. // member function to call for 'visit', then you are probably attempting
  90. // to add a parameter that is not derived from RangedAudioParameter to
  91. // the AudioProcessorValueTreeState.
  92. virtual void visit (std::unique_ptr<RangedAudioParameter>) const = 0;
  93. virtual void visit (std::unique_ptr<AudioProcessorParameterGroup>) const = 0;
  94. };
  95. struct ParameterStorageBase
  96. {
  97. virtual ~ParameterStorageBase() = default;
  98. virtual void accept (const Visitor& visitor) = 0;
  99. };
  100. template <typename Contents>
  101. struct ParameterStorage : ParameterStorageBase
  102. {
  103. explicit ParameterStorage (std::unique_ptr<Contents> input) : contents (std::move (input)) {}
  104. void accept (const Visitor& visitor) override { visitor.visit (std::move (contents)); }
  105. std::unique_ptr<Contents> contents;
  106. };
  107. struct MakeContents final
  108. {
  109. template <typename Item>
  110. std::unique_ptr<ParameterStorageBase> operator() (std::unique_ptr<Item> item) const
  111. {
  112. return std::unique_ptr<ParameterStorageBase> (new ParameterStorage<Item> (std::move (item)));
  113. }
  114. };
  115. void add() {}
  116. friend class AudioProcessorValueTreeState;
  117. std::vector<std::unique_ptr<ParameterStorageBase>> parameters;
  118. };
  119. //==============================================================================
  120. /** Creates a state object for a given processor, and sets up all the parameters
  121. that will control that processor.
  122. You should *not* assign a new ValueTree to the state, or call
  123. createAndAddParameter, after using this constructor.
  124. Note that each AudioProcessorValueTreeState should be attached to only one
  125. processor, and must have the same lifetime as the processor, as they will
  126. have dependencies on each other.
  127. The ParameterLayout parameter has a set of constructors that allow you to
  128. add multiple RangedAudioParameters and AudioProcessorParameterGroups containing
  129. RangedAudioParameters to the AudioProcessorValueTreeState inside this constructor.
  130. @code
  131. YourAudioProcessor()
  132. : apvts (*this, &undoManager, "PARAMETERS",
  133. { std::make_unique<AudioParameterFloat> ("a", "Parameter A", NormalisableRange<float> (-100.0f, 100.0f), 0),
  134. std::make_unique<AudioParameterInt> ("b", "Parameter B", 0, 5, 2) })
  135. @endcode
  136. To add parameters programatically you can call `add` repeatedly on a
  137. ParameterLayout instance:
  138. @code
  139. AudioProcessorValueTreeState::ParameterLayout createParameterLayout()
  140. {
  141. AudioProcessorValueTreeState::ParameterLayout layout;
  142. for (int i = 1; i < 9; ++i)
  143. layout.add (std::make_unique<AudioParameterInt> (String (i), String (i), 0, i, 0));
  144. return layout;
  145. }
  146. YourAudioProcessor()
  147. : apvts (*this, &undoManager, "PARAMETERS", createParameterLayout())
  148. {
  149. }
  150. @endcode
  151. @param processorToConnectTo The Processor that will be managed by this object
  152. @param undoManagerToUse An optional UndoManager to use; pass nullptr if no UndoManager is required
  153. @param valueTreeType The identifier used to initialise the internal ValueTree
  154. @param parameterLayout An object that holds all parameters and parameter groups that the
  155. AudioProcessor should use.
  156. */
  157. AudioProcessorValueTreeState (AudioProcessor& processorToConnectTo,
  158. UndoManager* undoManagerToUse,
  159. const Identifier& valueTreeType,
  160. ParameterLayout parameterLayout);
  161. /** This constructor is discouraged and will be deprecated in a future version of JUCE!
  162. Use the other constructor instead.
  163. Creates a state object for a given processor.
  164. The UndoManager is optional and can be a nullptr. After creating your state object,
  165. you should add parameters with the createAndAddParameter() method. Note that each
  166. AudioProcessorValueTreeState should be attached to only one processor, and must have
  167. the same lifetime as the processor, as they will have dependencies on each other.
  168. */
  169. AudioProcessorValueTreeState (AudioProcessor& processorToConnectTo, UndoManager* undoManagerToUse);
  170. /** Destructor. */
  171. ~AudioProcessorValueTreeState() override;
  172. //==============================================================================
  173. #ifndef DOXYGEN
  174. /** Previous calls to
  175. @code
  176. createAndAddParameter (paramID1, paramName1, ...);
  177. @endcode
  178. can be replaced with
  179. @code
  180. using Parameter = AudioProcessorValueTreeState::Parameter;
  181. createAndAddParameter (std::make_unique<Parameter> (paramID1, paramName1, ...));
  182. @endcode
  183. However, a much better approach is to use the AudioProcessorValueTreeState
  184. constructor directly
  185. @code
  186. using Parameter = AudioProcessorValueTreeState::Parameter;
  187. YourAudioProcessor()
  188. : apvts (*this, &undoManager, "PARAMETERS", { std::make_unique<Parameter> (paramID1, paramName1, ...),
  189. std::make_unique<Parameter> (paramID2, paramName2, ...),
  190. ... })
  191. @endcode
  192. @see AudioProcessorValueTreeState::AudioProcessorValueTreeState
  193. This function creates and returns a new parameter object for controlling a
  194. parameter with the given ID.
  195. Calling this will create and add a special type of AudioProcessorParameter to the
  196. AudioProcessor to which this state is attached.
  197. */
  198. [[deprecated ("This function is deprecated and will be removed in a future version of JUCE! "
  199. "See the method docs for a code example of the replacement methods.")]]
  200. RangedAudioParameter* createAndAddParameter (const String& parameterID,
  201. const String& parameterName,
  202. const String& labelText,
  203. NormalisableRange<float> valueRange,
  204. float defaultValue,
  205. std::function<String (float)> valueToTextFunction,
  206. std::function<float (const String&)> textToValueFunction,
  207. bool isMetaParameter = false,
  208. bool isAutomatableParameter = true,
  209. bool isDiscrete = false,
  210. AudioProcessorParameter::Category parameterCategory = AudioProcessorParameter::genericParameter,
  211. bool isBoolean = false);
  212. #endif
  213. /** This function adds a parameter to the attached AudioProcessor and that parameter will
  214. be managed by this AudioProcessorValueTreeState object.
  215. */
  216. RangedAudioParameter* createAndAddParameter (std::unique_ptr<RangedAudioParameter> parameter);
  217. //==============================================================================
  218. /** Returns a parameter by its ID string. */
  219. RangedAudioParameter* getParameter (StringRef parameterID) const noexcept;
  220. /** Returns a pointer to a floating point representation of a particular parameter which a realtime
  221. process can read to find out its current value.
  222. Note that calling this method from within AudioProcessorValueTreeState::Listener::parameterChanged()
  223. is not guaranteed to return an up-to-date value for the parameter.
  224. */
  225. std::atomic<float>* getRawParameterValue (StringRef parameterID) const noexcept;
  226. //==============================================================================
  227. /** A listener class that can be attached to an AudioProcessorValueTreeState.
  228. Use AudioProcessorValueTreeState::addParameterListener() to register a callback.
  229. */
  230. struct JUCE_API Listener
  231. {
  232. virtual ~Listener() = default;
  233. /** This callback method is called by the AudioProcessorValueTreeState when a parameter changes.
  234. Within this call, retrieving the value of the parameter that has changed via the getRawParameterValue()
  235. or getParameter() methods is not guaranteed to return the up-to-date value. If you need this you should
  236. instead use the newValue parameter.
  237. */
  238. virtual void parameterChanged (const String& parameterID, float newValue) = 0;
  239. };
  240. /** Attaches a callback to one of the parameters, which will be called when the parameter changes. */
  241. void addParameterListener (StringRef parameterID, Listener* listener);
  242. /** Removes a callback that was previously added with addParameterCallback(). */
  243. void removeParameterListener (StringRef parameterID, Listener* listener);
  244. //==============================================================================
  245. /** Returns a Value object that can be used to control a particular parameter. */
  246. Value getParameterAsValue (StringRef parameterID) const;
  247. /** Returns the range that was set when the given parameter was created. */
  248. NormalisableRange<float> getParameterRange (StringRef parameterID) const noexcept;
  249. //==============================================================================
  250. /** Returns a copy of the state value tree.
  251. The AudioProcessorValueTreeState's ValueTree is updated internally on the
  252. message thread, but there may be cases when you may want to access the state
  253. from a different thread (getStateInformation is a good example). This method
  254. flushes all pending audio parameter value updates and returns a copy of the
  255. state in a thread safe way.
  256. Note: This method uses locks to synchronise thread access, so whilst it is
  257. thread-safe, it is not realtime-safe. Do not call this method from within
  258. your audio processing code!
  259. */
  260. ValueTree copyState();
  261. /** Replaces the state value tree.
  262. The AudioProcessorValueTreeState's ValueTree is updated internally on the
  263. message thread, but there may be cases when you may want to modify the state
  264. from a different thread (setStateInformation is a good example). This method
  265. allows you to replace the state in a thread safe way.
  266. Note: This method uses locks to synchronise thread access, so whilst it is
  267. thread-safe, it is not realtime-safe. Do not call this method from within
  268. your audio processing code!
  269. */
  270. void replaceState (const ValueTree& newState);
  271. //==============================================================================
  272. /** A reference to the processor with which this state is associated. */
  273. AudioProcessor& processor;
  274. /** The state of the whole processor.
  275. This must be initialised after all calls to createAndAddParameter().
  276. You can replace this with your own ValueTree object, and can add properties and
  277. children to the tree. This class will automatically add children for each of the
  278. parameter objects that are created by createAndAddParameter().
  279. */
  280. ValueTree state;
  281. /** Provides access to the undo manager that this object is using. */
  282. UndoManager* const undoManager;
  283. private:
  284. //==============================================================================
  285. class ParameterAdapter;
  286. public:
  287. //==============================================================================
  288. /** A parameter class that maintains backwards compatibility with deprecated
  289. AudioProcessorValueTreeState functionality.
  290. Previous calls to
  291. @code
  292. createAndAddParameter (paramID1, paramName1, ...);
  293. @endcode
  294. can be replaced with
  295. @code
  296. using Parameter = AudioProcessorValueTreeState::Parameter;
  297. createAndAddParameter (std::make_unique<Parameter> (paramID1, paramName1, ...));
  298. @endcode
  299. However, a much better approach is to use the AudioProcessorValueTreeState
  300. constructor directly
  301. @code
  302. using Parameter = AudioProcessorValueTreeState::Parameter;
  303. YourAudioProcessor()
  304. : apvts (*this, &undoManager, "PARAMETERS", { std::make_unique<Parameter> (paramID1, paramName1, ...),
  305. std::make_unique<Parameter> (paramID2, paramName2, ...),
  306. ... })
  307. @endcode
  308. */
  309. class Parameter final : public AudioParameterFloat
  310. {
  311. public:
  312. Parameter (const String& parameterID,
  313. const String& parameterName,
  314. const String& labelText,
  315. NormalisableRange<float> valueRange,
  316. float defaultValue,
  317. std::function<String (float)> valueToTextFunction,
  318. std::function<float (const String&)> textToValueFunction,
  319. bool isMetaParameter = false,
  320. bool isAutomatableParameter = true,
  321. bool isDiscrete = false,
  322. AudioProcessorParameter::Category parameterCategory = AudioProcessorParameter::genericParameter,
  323. bool isBoolean = false);
  324. float getDefaultValue() const override;
  325. int getNumSteps() const override;
  326. bool isMetaParameter() const override;
  327. bool isAutomatable() const override;
  328. bool isDiscrete() const override;
  329. bool isBoolean() const override;
  330. private:
  331. void valueChanged (float) override;
  332. std::function<void()> onValueChanged;
  333. const float unsnappedDefault;
  334. const bool metaParameter, automatable, discrete, boolean;
  335. std::atomic<float> lastValue { -1.0f };
  336. friend class AudioProcessorValueTreeState::ParameterAdapter;
  337. };
  338. //==============================================================================
  339. /** An object of this class maintains a connection between a Slider and a parameter
  340. in an AudioProcessorValueTreeState.
  341. During the lifetime of this SliderAttachment object, it keeps the two things in
  342. sync, making it easy to connect a slider to a parameter. When this object is
  343. deleted, the connection is broken. Make sure that your AudioProcessorValueTreeState
  344. and Slider aren't deleted before this object!
  345. */
  346. class JUCE_API SliderAttachment
  347. {
  348. public:
  349. SliderAttachment (AudioProcessorValueTreeState& stateToUse,
  350. const String& parameterID,
  351. Slider& slider);
  352. private:
  353. std::unique_ptr<SliderParameterAttachment> attachment;
  354. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (SliderAttachment)
  355. };
  356. //==============================================================================
  357. /** An object of this class maintains a connection between a ComboBox and a parameter
  358. in an AudioProcessorValueTreeState.
  359. Combobox items will be spaced linearly across the range of the parameter. For
  360. example if the range is specified by NormalisableRange<float> (-0.5f, 0.5f, 0.5f)
  361. and you add three items then the first will be mapped to a value of -0.5, the
  362. second to 0, and the third to 0.5.
  363. During the lifetime of this ComboBoxAttachment object, it keeps the two things in
  364. sync, making it easy to connect a combo box to a parameter. When this object is
  365. deleted, the connection is broken. Make sure that your AudioProcessorValueTreeState
  366. and ComboBox aren't deleted before this object!
  367. */
  368. class JUCE_API ComboBoxAttachment
  369. {
  370. public:
  371. ComboBoxAttachment (AudioProcessorValueTreeState& stateToUse,
  372. const String& parameterID,
  373. ComboBox& combo);
  374. private:
  375. std::unique_ptr<ComboBoxParameterAttachment> attachment;
  376. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ComboBoxAttachment)
  377. };
  378. //==============================================================================
  379. /** An object of this class maintains a connection between a Button and a parameter
  380. in an AudioProcessorValueTreeState.
  381. During the lifetime of this ButtonAttachment object, it keeps the two things in
  382. sync, making it easy to connect a button to a parameter. When this object is
  383. deleted, the connection is broken. Make sure that your AudioProcessorValueTreeState
  384. and Button aren't deleted before this object!
  385. */
  386. class JUCE_API ButtonAttachment
  387. {
  388. public:
  389. ButtonAttachment (AudioProcessorValueTreeState& stateToUse,
  390. const String& parameterID,
  391. Button& button);
  392. private:
  393. std::unique_ptr<ButtonParameterAttachment> attachment;
  394. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ButtonAttachment)
  395. };
  396. private:
  397. //==============================================================================
  398. /** Code that looks like this:
  399. @code
  400. auto paramA = apvts.createParameter ("a", "Parameter A", {}, { -100, 100 }, ...);
  401. auto paramB = apvts.createParameter ("b", "Parameter B", {}, { 0, 5 }, ...);
  402. addParameterGroup (std::make_unique<AudioProcessorParameterGroup> ("g1", "Group 1", " | ", std::move (paramA), std::move (paramB)));
  403. apvts.state = ValueTree (Identifier ("PARAMETERS"));
  404. @endcode
  405. can instead create the APVTS like this, avoiding the two-step initialization process and leveraging one of JUCE's
  406. pre-built parameter types (or your own custom type derived from RangedAudioParameter):
  407. @code
  408. using Parameter = AudioProcessorValueTreeState::Parameter;
  409. YourAudioProcessor()
  410. : apvts (*this, &undoManager, "PARAMETERS",
  411. { std::make_unique<AudioProcessorParameterGroup> ("g1", "Group 1", " | ",
  412. std::make_unique<Parameter> ("a", "Parameter A", "", NormalisableRange<float> (-100, 100), ...),
  413. std::make_unique<Parameter> ("b", "Parameter B", "", NormalisableRange<float> (0, 5), ...)) })
  414. @endcode
  415. */
  416. [[deprecated ("This method was introduced to allow you to use AudioProcessorValueTreeState parameters in "
  417. "an AudioProcessorParameterGroup, but there is now a much nicer way to achieve this. See the "
  418. "method docs for a code example.")]]
  419. std::unique_ptr<RangedAudioParameter> createParameter (const String&, const String&, const String&, NormalisableRange<float>,
  420. float, std::function<String (float)>, std::function<float (const String&)>,
  421. bool, bool, bool, AudioProcessorParameter::Category, bool);
  422. //==============================================================================
  423. #if JUCE_UNIT_TESTS
  424. friend struct ParameterAdapterTests;
  425. #endif
  426. void addParameterAdapter (RangedAudioParameter&);
  427. ParameterAdapter* getParameterAdapter (StringRef) const;
  428. bool flushParameterValuesToValueTree();
  429. void setNewState (ValueTree);
  430. void timerCallback() override;
  431. void valueTreePropertyChanged (ValueTree&, const Identifier&) override;
  432. void valueTreeChildAdded (ValueTree&, ValueTree&) override;
  433. void valueTreeRedirected (ValueTree&) override;
  434. void updateParameterConnectionsToChildTrees();
  435. const Identifier valueType { "PARAM" }, valuePropertyID { "value" }, idPropertyID { "id" };
  436. struct StringRefLessThan final
  437. {
  438. bool operator() (StringRef a, StringRef b) const noexcept { return a.text.compare (b.text) < 0; }
  439. };
  440. std::map<StringRef, std::unique_ptr<ParameterAdapter>, StringRefLessThan> adapterTable;
  441. CriticalSection valueTreeChanging;
  442. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (AudioProcessorValueTreeState)
  443. };
  444. } // namespace juce