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_ParameterAttachments.cpp
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
32 : parameter (param),
33 undoManager (um),
34 setValue (std::move (parameterChangedCallback))
35{
36 parameter.addListener (this);
37}
38
44
46{
47 parameterValueChanged ({}, parameter.getValue());
48}
49
51{
52 callIfParameterValueChanged (newDenormalisedValue, [this] (float f)
53 {
55 parameter.setValueNotifyingHost (f);
56 endGesture();
57 });
58}
59
61{
62 if (undoManager != nullptr)
63 undoManager->beginNewTransaction();
64
65 parameter.beginChangeGesture();
66}
67
69{
70 callIfParameterValueChanged (newDenormalisedValue, [this] (float f)
71 {
72 parameter.setValueNotifyingHost (f);
73 });
74}
75
80
81template <typename Callback>
82void ParameterAttachment::callIfParameterValueChanged (float newDenormalisedValue,
83 Callback&& callback)
84{
85 const auto newValue = normalise (newDenormalisedValue);
86
87 if (! approximatelyEqual (parameter.getValue(), newValue))
88 callback (newValue);
89}
90
91void ParameterAttachment::parameterValueChanged (int, float newValue)
92{
93 lastValue = newValue;
94
95 if (MessageManager::getInstance()->isThisTheMessageThread())
96 {
98 handleAsyncUpdate();
99 }
100 else
101 {
103 }
104}
105
106void ParameterAttachment::handleAsyncUpdate()
107{
108 NullCheckedInvocation::invoke (setValue, parameter.convertFrom0to1 (lastValue));
109}
110
111//==============================================================================
113 Slider& s,
115 : slider (s),
116 attachment (param, [this] (float f) { setValue (f); }, um)
117{
118 slider.valueFromTextFunction = [&param] (const String& text) { return (double) param.convertFrom0to1 (param.getValueForText (text)); };
119 slider.textFromValueFunction = [&param] (double value) { return param.getText (param.convertTo0to1 ((float) value), 0); };
120 slider.setDoubleClickReturnValue (true, param.convertFrom0to1 (param.getDefaultValue()));
121
122 auto range = param.getNormalisableRange();
123
124 auto convertFrom0To1Function = [range] (double currentRangeStart,
125 double currentRangeEnd,
126 double normalisedValue) mutable
127 {
128 range.start = (float) currentRangeStart;
129 range.end = (float) currentRangeEnd;
130 return (double) range.convertFrom0to1 ((float) normalisedValue);
131 };
132
133 auto convertTo0To1Function = [range] (double currentRangeStart,
134 double currentRangeEnd,
135 double mappedValue) mutable
136 {
137 range.start = (float) currentRangeStart;
138 range.end = (float) currentRangeEnd;
139 return (double) range.convertTo0to1 ((float) mappedValue);
140 };
141
142 auto snapToLegalValueFunction = [range] (double currentRangeStart,
143 double currentRangeEnd,
144 double mappedValue) mutable
145 {
146 range.start = (float) currentRangeStart;
147 range.end = (float) currentRangeEnd;
148 return (double) range.snapToLegalValue ((float) mappedValue);
149 };
150
151 NormalisableRange<double> newRange { (double) range.start,
152 (double) range.end,
153 std::move (convertFrom0To1Function),
154 std::move (convertTo0To1Function),
155 std::move (snapToLegalValueFunction) };
156 newRange.interval = range.interval;
157 newRange.skew = range.skew;
158 newRange.symmetricSkew = range.symmetricSkew;
159
160 slider.setNormalisableRange (newRange);
161
162 sendInitialUpdate();
163 slider.valueChanged();
164 slider.addListener (this);
165}
166
171
173
174void SliderParameterAttachment::setValue (float newValue)
175{
176 const ScopedValueSetter<bool> svs (ignoreCallbacks, true);
177 slider.setValue (newValue, sendNotificationSync);
178}
179
180void SliderParameterAttachment::sliderValueChanged (Slider*)
181{
182 if (! ignoreCallbacks)
183 attachment.setValueAsPartOfGesture ((float) slider.getValue());
184}
185
186//==============================================================================
188 ComboBox& c,
190 : comboBox (c),
191 storedParameter (param),
192 attachment (param, [this] (float f) { setValue (f); }, um)
193{
194 sendInitialUpdate();
195 comboBox.addListener (this);
196}
197
202
207
208void ComboBoxParameterAttachment::setValue (float newValue)
209{
210 const auto normValue = storedParameter.convertTo0to1 (newValue);
211 const auto index = roundToInt (normValue * (float) (comboBox.getNumItems() - 1));
212
213 if (index == comboBox.getSelectedItemIndex())
214 return;
215
216 const ScopedValueSetter<bool> svs (ignoreCallbacks, true);
218}
219
220void ComboBoxParameterAttachment::comboBoxChanged (ComboBox*)
221{
222 if (ignoreCallbacks)
223 return;
224
225 const auto numItems = comboBox.getNumItems();
226 const auto selected = (float) comboBox.getSelectedItemIndex();
227 const auto newValue = numItems > 1 ? selected / (float) (numItems - 1)
228 : 0.0f;
229
230 attachment.setValueAsCompleteGesture (storedParameter.convertFrom0to1 (newValue));
231}
232
233//==============================================================================
235 Button& b,
237 : button (b),
238 attachment (param, [this] (float f) { setValue (f); }, um)
239{
240 sendInitialUpdate();
241 button.addListener (this);
242}
243
248
253
254void ButtonParameterAttachment::setValue (float newValue)
255{
256 const ScopedValueSetter<bool> svs (ignoreCallbacks, true);
257 button.setToggleState (newValue >= 0.5f, sendNotificationSync);
258}
259
260void ButtonParameterAttachment::buttonClicked (Button*)
261{
262 if (ignoreCallbacks)
263 return;
264
265 attachment.setValueAsCompleteGesture (button.getToggleState() ? 1.0f : 0.0f);
266}
267
268} // namespace juce
void triggerAsyncUpdate()
Causes the callback to be triggered at a later time.
void cancelPendingUpdate() noexcept
This will stop any pending updates from happening.
virtual float getValue() const =0
Called by the host to find out the value of this parameter.
virtual float getDefaultValue() const =0
This should return the default value for this parameter.
virtual String getText(float normalisedValue, int) const
Returns a textual version of the supplied normalised parameter value.
void beginChangeGesture()
Sends a signal to the host to tell it that the user is about to start changing this parameter.
void removeListener(Listener *listener)
Removes a previously registered parameter listener.
void setValueNotifyingHost(float newValue)
A processor should call this when it needs to change one of its parameters.
void endChangeGesture()
Tells the host that the user has finished changing this parameter.
void addListener(Listener *newListener)
Registers a listener to receive events when the parameter's state changes.
virtual float getValueForText(const String &text) const =0
Should parse a string and return the appropriate value for it.
ButtonParameterAttachment(RangedAudioParameter &parameter, Button &button, UndoManager *undoManager=nullptr)
Creates a connection between a plug-in parameter and a Button.
void sendInitialUpdate()
Call this after setting up your button in the case where you need to do extra setup after constructin...
A base class for buttons.
Definition juce_Button.h:43
bool getToggleState() const noexcept
Returns true if the button is 'on'.
void setToggleState(bool shouldBeOn, NotificationType notification)
A button has an on/off state associated with it, and this changes that.
void removeListener(Listener *listener)
Removes a previously-registered button listener.
void sendInitialUpdate()
Call this after setting up your combo box in the case where you need to do extra setup after construc...
ComboBoxParameterAttachment(RangedAudioParameter &parameter, ComboBox &combo, UndoManager *undoManager=nullptr)
Creates a connection between a plug-in parameter and a ComboBox.
A component that lets the user choose from a drop-down list of choices.
void setSelectedItemIndex(int newItemIndex, NotificationType notification=sendNotificationAsync)
Sets one of the items to be the current selection.
int getNumItems() const noexcept
Returns the number of items that have been added to the list.
int getSelectedItemIndex() const
Returns the index of the item that's currently shown in the box.
void removeListener(Listener *listener)
Deregisters a previously-registered listener.
static MessageManager * getInstance()
Returns the global instance of the MessageManager.
ValueType start
The minimum value of the non-normalised range.
void endGesture()
Ends a gesture on the managed parameter.
void setValueAsCompleteGesture(float newDenormalisedValue)
Triggers a full gesture message on the managed parameter.
void sendInitialUpdate()
Calls the parameterChangedCallback function that was registered in the constructor,...
void beginGesture()
Begins a gesture on the managed parameter.
void setValueAsPartOfGesture(float newDenormalisedValue)
Updates the parameter value during a gesture.
~ParameterAttachment() override
Destructor.
ParameterAttachment(RangedAudioParameter &parameter, std::function< void(float)> parameterChangedCallback, UndoManager *undoManager=nullptr)
Listens to a parameter and calls the the provided function in response to parameter changes.
This abstract base class is used by some AudioProcessorParameter helper classes.
float convertTo0to1(float v) const noexcept
Normalises and snaps a value based on the normalisable range.
virtual const NormalisableRange< float > & getNormalisableRange() const =0
Returns the range of values that the parameter can take.
float convertFrom0to1(float v) const noexcept
Denormalises and snaps a value based on the normalisable range.
Helper class providing an RAII-based mechanism for temporarily setting and then re-setting a value.
void sendInitialUpdate()
Call this after setting up your slider in the case where you need to do extra setup after constructin...
SliderParameterAttachment(RangedAudioParameter &parameter, Slider &slider, UndoManager *undoManager=nullptr)
Creates a connection between a plug-in parameter and a Slider.
A slider control for changing a value.
Definition juce_Slider.h:54
double getValue() const
Returns the slider's current value.
void removeListener(Listener *listener)
Removes a previously-registered listener.
void setValue(double newValue, NotificationType notification=sendNotificationAsync)
Changes the slider's current value.
The JUCE String class!
Definition juce_String.h:53
Manages a list of undo/redo commands.
void beginNewTransaction()
Starts a new group of actions that together will be treated as a single transaction.
typedef float
JUCE Namespace.
constexpr bool approximatelyEqual(Type a, Type b, Tolerance< Type > tolerance=Tolerance< Type >{} .withAbsolute(std::numeric_limits< Type >::min()) .withRelative(std::numeric_limits< Type >::epsilon()))
Returns true if the two floating-point numbers are approximately equal.
@ sendNotificationSync
Requests a synchronous notification.
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
int roundToInt(const FloatType value) noexcept
Fast floating-point-to-integer conversion.