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_AudioParameterChoice.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
30 const String& nameToUse,
31 const StringArray& c,
32 int def,
33 const AudioParameterChoiceAttributes& attributes)
34 : RangedAudioParameter (idToUse, nameToUse, attributes.getAudioProcessorParameterWithIDAttributes()),
35 choices (c),
36 range ([this]
37 {
39 [] (float, float end, float v) { return jlimit (0.0f, end, v * end); },
40 [] (float, float end, float v) { return jlimit (0.0f, 1.0f, v / end); },
41 [] (float start, float end, float v) { return (float) roundToInt (juce::jlimit (start, end, v)); } };
42 rangeWithInterval.interval = 1.0f;
43 return rangeWithInterval;
44 }()),
45 value ((float) def),
46 defaultValue (convertTo0to1 ((float) def)),
47 stringFromIndexFunction (attributes.getStringFromValueFunction() != nullptr
48 ? attributes.getStringFromValueFunction()
49 : [this] (int index, int) { return choices [index]; }),
50 indexFromStringFunction (attributes.getValueFromStringFunction() != nullptr
51 ? attributes.getValueFromStringFunction()
52 : [this] (const String& text) { return choices.indexOf (text); })
53{
54 jassert (choices.size() > 1); // you must supply an actual set of items to choose from!
55}
56
58{
59 #if __cpp_lib_atomic_is_always_lock_free
61 "AudioParameterChoice requires a lock-free std::atomic<float>");
62 #endif
63}
64
65float AudioParameterChoice::getValue() const { return convertTo0to1 (value); }
66void AudioParameterChoice::setValue (float newValue) { value = convertFrom0to1 (newValue); valueChanged (getIndex()); }
67float AudioParameterChoice::getDefaultValue() const { return defaultValue; }
68int AudioParameterChoice::getNumSteps() const { return choices.size(); }
69bool AudioParameterChoice::isDiscrete() const { return true; }
70float AudioParameterChoice::getValueForText (const String& text) const { return convertTo0to1 ((float) indexFromStringFunction (text)); }
71String AudioParameterChoice::getText (float v, int length) const { return stringFromIndexFunction ((int) convertFrom0to1 (v), length); }
73
75{
76 if (getIndex() != newValue)
77 setValueNotifyingHost (convertTo0to1 ((float) newValue));
78
79 return *this;
80}
81
82
83//==============================================================================
84//==============================================================================
85#if JUCE_UNIT_TESTS
86
88{
90 : UnitTest ("AudioParameterChoice", UnitTestCategories::audioProcessorParameters)
91 {}
92
93 void runTest() override
94 {
95 beginTest ("Three options switches at the correct points");
96 {
97 AudioParameterChoice choice ({}, {}, { "a", "b", "c" }, {});
98
99 choice.setValueNotifyingHost (0.0f);
100 expectEquals (choice.getIndex(), 0);
101
102 choice.setValueNotifyingHost (0.2f);
103 expectEquals (choice.getIndex(), 0);
104
105 choice.setValueNotifyingHost (0.3f);
106 expectEquals (choice.getIndex(), 1);
107
108 choice.setValueNotifyingHost (0.7f);
109 expectEquals (choice.getIndex(), 1);
110
111 choice.setValueNotifyingHost (0.8f);
112 expectEquals (choice.getIndex(), 2);
113
114 choice.setValueNotifyingHost (1.0f);
115 expectEquals (choice.getIndex(), 2);
116 }
117
118 beginTest ("Out-of-bounds input");
119 {
120 AudioParameterChoice choiceParam ({}, {}, { "a", "b", "c" }, {});
121
122 choiceParam.setValueNotifyingHost (-0.5f);
123 expectEquals (choiceParam.getIndex(), 0);
124
125 choiceParam.setValueNotifyingHost (1.5f);
126 expectEquals (choiceParam.getIndex(), 2);
127 }
128 }
129
130};
131
133
134#endif
135
136} // namespace juce
Properties of an AudioParameterChoice.
Provides a class of AudioProcessorParameter that can be used to select an indexed,...
const StringArray choices
Provides access to the list of choices that this parameter is working with.
AudioParameterChoice(const ParameterID &parameterID, const String &parameterName, const StringArray &choices, int defaultItemIndex, const AudioParameterChoiceAttributes &attributes={})
Creates a AudioParameterChoice with the specified parameters.
~AudioParameterChoice() override
Destructor.
int getIndex() const noexcept
Returns the current index of the selected item.
virtual void valueChanged(int newValue)
Override this method if you are interested in receiving callbacks when the parameter value changes.
AudioParameterChoice & operator=(int newValue)
Changes the selected item to a new index.
void setValueNotifyingHost(float newValue)
A processor should call this when it needs to change one of its parameters.
Represents a mapping between an arbitrary range of values and a normalised 0->1 range.
Combines a parameter ID and a version hint.
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.
float convertFrom0to1(float v) const noexcept
Denormalises and snaps a value based on the normalisable range.
A special array for holding a list of strings.
int size() const noexcept
Returns the number of strings in the array.
The JUCE String class!
Definition juce_String.h:53
This is a base class for classes that perform a unit test.
#define jassert(expression)
Platform-independent assertion macro.
typedef int
typedef float
JUCE Namespace.
RangedDirectoryIterator end(const RangedDirectoryIterator &)
Returns a default-constructed sentinel value.
Type jlimit(Type lowerLimit, Type upperLimit, Type valueToConstrain) noexcept
Constrains a value to keep it within a given range.
@ valueChanged
Indicates that the UI element's value has changed.
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.