JUCE-7.0.12-0-g4f43011b96 JUCE-7.0.12-0-g4f43011b96
JUCE — C++ application framework with suport for VST, VST3, LV2 audio plug-ins

« « « Anklang Documentation
Loading...
Searching...
No Matches
juce_AudioProcessorValueTreeState.h
Go to the documentation of this file.
1 /*
2 ==============================================================================
3
4 This file is part of the JUCE library.
5 Copyright (c) 2022 - Raw Material Software Limited
6
7 JUCE is an open source library subject to commercial or open-source
8 licensing.
9
10 By using JUCE, you agree to the terms of both the JUCE 7 End-User License
11 Agreement and JUCE Privacy Policy.
12
13 End User License Agreement: www.juce.com/juce-7-licence
14 Privacy Policy: www.juce.com/juce-privacy-policy
15
16 Or: You may also use this code under the terms of the GPL v3 (see
17 www.gnu.org/licenses).
18
19 JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
20 EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
21 DISCLAIMED.
22
23 ==============================================================================
24*/
25
26namespace juce
27{
28
39{
44
45public:
47 [[nodiscard]] auto withStringFromValueFunction (StringFromValue x) const { return withMember (*this, &This::attributes, attributes.withStringFromValueFunction (std::move (x))); }
49 [[nodiscard]] auto withValueFromStringFunction (ValueFromString x) const { return withMember (*this, &This::attributes, attributes.withValueFromStringFunction (std::move (x))); }
51 [[nodiscard]] auto withLabel (String x) const { return withMember (*this, &This::attributes, attributes.withLabel (std::move (x))); }
53 [[nodiscard]] auto withCategory (Category x) const { return withMember (*this, &This::attributes, attributes.withCategory (std::move (x))); }
55 [[nodiscard]] auto withMeta (bool x) const { return withMember (*this, &This::attributes, attributes.withMeta (std::move (x))); }
57 [[nodiscard]] auto withAutomatable (bool x) const { return withMember (*this, &This::attributes, attributes.withAutomatable (std::move (x))); }
59 [[nodiscard]] auto withInverted (bool x) const { return withMember (*this, &This::attributes, attributes.withInverted (std::move (x))); }
60
65 [[nodiscard]] auto withDiscrete (bool x) const { return withMember (*this, &This::discrete, std::move (x)); }
66
71 [[nodiscard]] auto withBoolean (bool x) const { return withMember (*this, &This::boolean, std::move (x)); }
72
74 [[nodiscard]] const auto& getAudioParameterFloatAttributes() const { return attributes; }
76 [[nodiscard]] const auto& getDiscrete() const { return discrete; }
78 [[nodiscard]] const auto& getBoolean() const { return boolean; }
79
80private:
82 bool discrete = false, boolean = false;
83};
84
110class JUCE_API AudioProcessorValueTreeState : private Timer,
111 private ValueTree::Listener
112{
113public:
114 //==============================================================================
121 class JUCE_API ParameterLayout final
122 {
123 private:
124 //==============================================================================
125 template <typename It>
126 using ValidIfIterator = decltype (std::next (std::declval<It>()));
127
128 public:
129 //==============================================================================
130 template <typename... Items>
131 ParameterLayout (std::unique_ptr<Items>... items) { add (std::move (items)...); }
132
133 template <typename It, typename = ValidIfIterator<It>>
134 ParameterLayout (It begin, It end) { add (begin, end); }
135
136 template <typename... Items>
137 void add (std::unique_ptr<Items>... items)
138 {
139 parameters.reserve (parameters.size() + sizeof... (items));
140 (parameters.push_back (makeParameterStorage (std::move (items))), ...);
141 }
142
143 template <typename It, typename = ValidIfIterator<It>>
144 void add (It begin, It end)
145 {
146 parameters.reserve (parameters.size() + std::size_t (std::distance (begin, end)));
149 std::back_inserter (parameters),
150 [] (auto item) { return makeParameterStorage (std::move (item)); });
151 }
152
153 ParameterLayout (const ParameterLayout& other) = delete;
154 ParameterLayout (ParameterLayout&& other) noexcept { swap (other); }
155
156 ParameterLayout& operator= (const ParameterLayout& other) = delete;
157 ParameterLayout& operator= (ParameterLayout&& other) noexcept { swap (other); return *this; }
158
159 void swap (ParameterLayout& other) noexcept { std::swap (other.parameters, parameters); }
160
161 private:
162 //==============================================================================
163 struct Visitor
164 {
165 virtual ~Visitor() = default;
166
167 // If you have a compiler error telling you that there is no matching
168 // member function to call for 'visit', then you are probably attempting
169 // to add a parameter that is not derived from RangedAudioParameter to
170 // the AudioProcessorValueTreeState.
171 virtual void visit (std::unique_ptr<RangedAudioParameter>) const = 0;
172 virtual void visit (std::unique_ptr<AudioProcessorParameterGroup>) const = 0;
173 };
174
175 struct ParameterStorageBase
176 {
177 virtual ~ParameterStorageBase() = default;
178 virtual void accept (const Visitor& visitor) = 0;
179 };
180
181 template <typename Contents>
182 struct ParameterStorage : ParameterStorageBase
183 {
184 explicit ParameterStorage (std::unique_ptr<Contents> input) : contents (std::move (input)) {}
185
186 void accept (const Visitor& visitor) override { visitor.visit (std::move (contents)); }
187
189 };
190
191 template <typename Contents>
192 static std::unique_ptr<ParameterStorage<Contents>> makeParameterStorage (std::unique_ptr<Contents> contents)
193 {
194 return std::make_unique<ParameterStorage<Contents>> (std::move (contents));
195 }
196
197 void add() {}
198
199 friend class AudioProcessorValueTreeState;
200
202 };
203
204 //==============================================================================
252 AudioProcessorValueTreeState (AudioProcessor& processorToConnectTo,
253 UndoManager* undoManagerToUse,
254 const Identifier& valueTreeType,
255 ParameterLayout parameterLayout);
256
267 AudioProcessorValueTreeState (AudioProcessor& processorToConnectTo, UndoManager* undoManagerToUse);
268
271
272 //==============================================================================
273 #ifndef DOXYGEN
306 [[deprecated ("This function is deprecated and will be removed in a future version of JUCE! "
307 "See the method docs for a code example of the replacement methods.")]]
308 RangedAudioParameter* createAndAddParameter (const String& parameterID,
309 const String& parameterName,
310 const String& labelText,
311 NormalisableRange<float> valueRange,
312 float defaultValue,
313 std::function<String (float)> valueToTextFunction,
314 std::function<float (const String&)> textToValueFunction,
315 bool isMetaParameter = false,
316 bool isAutomatableParameter = true,
317 bool isDiscrete = false,
318 AudioProcessorParameter::Category parameterCategory = AudioProcessorParameter::genericParameter,
319 bool isBoolean = false);
320 #endif
321
325 RangedAudioParameter* createAndAddParameter (std::unique_ptr<RangedAudioParameter> parameter);
326
327 //==============================================================================
329 RangedAudioParameter* getParameter (StringRef parameterID) const noexcept;
330
337 std::atomic<float>* getRawParameterValue (StringRef parameterID) const noexcept;
338
339 //==============================================================================
343 struct JUCE_API Listener
344 {
345 virtual ~Listener() = default;
346
353 virtual void parameterChanged (const String& parameterID, float newValue) = 0;
354 };
355
357 void addParameterListener (StringRef parameterID, Listener* listener);
358
360 void removeParameterListener (StringRef parameterID, Listener* listener);
361
362 //==============================================================================
364 Value getParameterAsValue (StringRef parameterID) const;
365
367 NormalisableRange<float> getParameterRange (StringRef parameterID) const noexcept;
368
369 //==============================================================================
382 ValueTree copyState();
383
395 void replaceState (const ValueTree& newState);
396
397 //==============================================================================
400
409
412
413private:
414 //==============================================================================
415 class ParameterAdapter;
416
417public:
418 //==============================================================================
446 class Parameter final : public AudioParameterFloat
447 {
448 public:
471 Parameter (const ParameterID& parameterID,
472 const String& parameterName,
473 NormalisableRange<float> valueRange,
474 float defaultValue,
476
477 [[deprecated ("Prefer the signature taking an Attributes argument")]]
478 Parameter (const ParameterID& parameterID,
479 const String& parameterName,
480 const String& labelText,
481 NormalisableRange<float> valueRange,
482 float defaultParameterValue,
483 std::function<String (float)> valueToTextFunction,
484 std::function<float (const String&)> textToValueFunction,
485 bool isMetaParameter = false,
486 bool isAutomatableParameter = true,
487 bool isDiscrete = false,
488 AudioProcessorParameter::Category parameterCategory = AudioProcessorParameter::genericParameter,
489 bool isBoolean = false)
490 : Parameter (parameterID,
491 parameterName,
492 valueRange,
493 defaultParameterValue,
495 .withStringFromValueFunction (adaptSignature (std::move (valueToTextFunction)))
496 .withValueFromStringFunction (std::move (textToValueFunction))
497 .withMeta (isMetaParameter)
498 .withAutomatable (isAutomatableParameter)
499 .withDiscrete (isDiscrete)
500 .withCategory (parameterCategory)
501 .withBoolean (isBoolean))
502 {
503 }
504
505 float getDefaultValue() const override;
506 int getNumSteps() const override;
507
508 bool isDiscrete() const override;
509 bool isBoolean() const override;
510
511 private:
512 static std::function<String (float, int)> adaptSignature (std::function<String (float)> func)
513 {
514 if (func == nullptr)
515 return nullptr;
516
517 return [f = std::move (func)] (float v, int) { return f (v); };
518 }
519
520 void valueChanged (float) override;
521
522 std::function<void()> onValueChanged;
523
524 const float unsnappedDefault;
525 const bool discrete, boolean;
526 std::atomic<float> lastValue { -1.0f };
527
529 };
530
531 //==============================================================================
540 class JUCE_API SliderAttachment
541 {
542 public:
544 const String& parameterID,
545 Slider& slider);
546
547 private:
550 };
551
552 //==============================================================================
566 class JUCE_API ComboBoxAttachment
567 {
568 public:
570 const String& parameterID,
571 ComboBox& combo);
572
573 private:
576 };
577
578 //==============================================================================
587 class JUCE_API ButtonAttachment
588 {
589 public:
591 const String& parameterID,
592 Button& button);
593
594 private:
597 };
598
599private:
600 //==============================================================================
622 [[deprecated ("This method was introduced to allow you to use AudioProcessorValueTreeState parameters in "
623 "an AudioProcessorParameterGroup, but there is now a much nicer way to achieve this. See the "
624 "method docs for a code example.")]]
626 float, std::function<String (float)>, std::function<float (const String&)>,
627 bool, bool, bool, AudioProcessorParameter::Category, bool);
628
629 //==============================================================================
630 #if JUCE_UNIT_TESTS
631 friend struct ParameterAdapterTests;
632 #endif
633
634 void addParameterAdapter (RangedAudioParameter&);
635 ParameterAdapter* getParameterAdapter (StringRef) const;
636
637 bool flushParameterValuesToValueTree();
638 void setNewState (ValueTree);
639 void timerCallback() override;
640
641 void valueTreePropertyChanged (ValueTree&, const Identifier&) override;
642 void valueTreeChildAdded (ValueTree&, ValueTree&) override;
643 void valueTreeRedirected (ValueTree&) override;
644 void updateParameterConnectionsToChildTrees();
645
646 const Identifier valueType { "PARAM" }, valuePropertyID { "value" }, idPropertyID { "id" };
647
648 struct StringRefLessThan final
649 {
650 bool operator() (StringRef a, StringRef b) const noexcept { return a.text.compare (b.text) < 0; }
651 };
652
653 std::map<StringRef, std::unique_ptr<ParameterAdapter>, StringRefLessThan> adapterTable;
654
655 CriticalSection valueTreeChanging;
656
657 JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (AudioProcessorValueTreeState)
658};
659
660} // namespace juce
accept
T back_inserter(T... args)
Properties of an AudioParameterFloat.
A subclass of AudioProcessorParameter that provides an easy way to create a parameter which maps onto...
Advanced properties of an AudioProcessorValueTreeState::Parameter.
auto withDiscrete(bool x) const
Pass 'true' if this parameter has discrete steps, or 'false' if the parameter is continuous.
auto withBoolean(bool x) const
Pass 'true' if this parameter only has two valid states.
An object of this class maintains a connection between a Button and a parameter in an AudioProcessorV...
An object of this class maintains a connection between a ComboBox and a parameter in an AudioProcesso...
A class to contain a set of RangedAudioParameters and AudioProcessorParameterGroups containing Ranged...
A parameter class that maintains backwards compatibility with deprecated AudioProcessorValueTreeState...
An object of this class maintains a connection between a Slider and a parameter in an AudioProcessorV...
This class contains a ValueTree that is used to manage an AudioProcessor's entire state.
UndoManager *const undoManager
Provides access to the undo manager that this object is using.
AudioProcessor & processor
A reference to the processor with which this state is associated.
ValueTree state
The state of the whole processor.
Base class for audio processing classes or plugins.
A base class for buttons.
Definition juce_Button.h:43
A component that lets the user choose from a drop-down list of choices.
Represents a string identifier, designed for accessing properties by name.
Represents a mapping between an arbitrary range of values and a normalised 0->1 range.
Combines a parameter ID and a version hint.
auto withValueFromStringFunction(ValueFromString x) const
An optional lambda function that parses a string and converts it into a non-normalised value.
auto withMeta(bool x) const
See AudioProcessorParameter::isMetaParameter()
auto withCategory(Category x) const
See AudioProcessorParameterWithIDAttributes::withCategory()
auto withAutomatable(bool x) const
See AudioProcessorParameter::isAutomatable()
auto withStringFromValueFunction(StringFromValue x) const
An optional lambda function that converts a non-normalised value to a string with a maximum length.
auto withInverted(bool x) const
See AudioProcessorParameter::isOrientationInverted()
auto withLabel(String x) const
See AudioProcessorParameterWithIDAttributes::withLabel()
This abstract base class is used by some AudioProcessorParameter helper classes.
A slider control for changing a value.
Definition juce_Slider.h:54
A simple class for holding temporary references to a string literal or String.
The JUCE String class!
Definition juce_String.h:53
Makes repeated callbacks to a virtual method at a specified time interval.
Definition juce_Timer.h:52
Manages a list of undo/redo commands.
Listener class for events that happen to a ValueTree.
A powerful tree structure that can be used to hold free-form data, and which can handle its own undo ...
Represents a shared variant value.
Definition juce_Value.h:51
T distance(T... args)
#define JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(className)
This is a shorthand way of writing both a JUCE_DECLARE_NON_COPYABLE and JUCE_LEAK_DETECTOR macro for ...
typedef int
T make_move_iterator(T... args)
JUCE Namespace.
@ valueChanged
Indicates that the UI element's value has changed.
Object withMember(Object copy, Member OtherObject::*member, Other &&value)
Copies an object, sets one of the copy's members to the specified value, and then returns the copy.
Type unalignedPointerCast(void *ptr) noexcept
Casts a pointer to another type via void*, which suppresses the cast-align warning which sometimes ar...
Definition juce_Memory.h:88
T next(T... args)
A listener class that can be attached to an AudioProcessorValueTreeState.
virtual void parameterChanged(const String &parameterID, float newValue)=0
This callback method is called by the AudioProcessorValueTreeState when a parameter changes.
T swap(T... args)
T transform(T... args)