LMMS
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 {
38 NormalisableRange<float> rangeWithInterval { 0.0f, (float) choices.size() - 1.0f,
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
60 static_assert (std::atomic<float>::is_always_lock_free,
61 "AudioParameterChoice requires a lock-free std::atomic<float>");
62 #endif
63}
64
66void AudioParameterChoice::setValue (float newValue) { value = convertFrom0to1 (newValue); valueChanged (getIndex()); }
68int AudioParameterChoice::getNumSteps() const { return choices.size(); }
69bool AudioParameterChoice::isDiscrete() const { return true; }
73
74AudioParameterChoice& AudioParameterChoice::operator= (int newValue)
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
87struct AudioParameterChoiceTests : public UnitTest
88{
89 AudioParameterChoiceTests()
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
132static AudioParameterChoiceTests audioParameterChoiceTests;
133
134#endif
135
136} // namespace juce
Definition juce_AudioParameterChoice.h:33
const StringArray choices
Definition juce_AudioParameterChoice.h:124
void setValue(float newValue) override
Definition juce_AudioParameterChoice.cpp:66
std::function< int(const String &)> indexFromStringFunction
Definition juce_AudioParameterChoice.h:146
float getValueForText(const String &) const override
Definition juce_AudioParameterChoice.cpp:70
std::function< String(int, int)> stringFromIndexFunction
Definition juce_AudioParameterChoice.h:145
float getValue() const override
Definition juce_AudioParameterChoice.cpp:65
bool isDiscrete() const override
Definition juce_AudioParameterChoice.cpp:69
const NormalisableRange< float > range
Definition juce_AudioParameterChoice.h:142
float getDefaultValue() const override
Definition juce_AudioParameterChoice.cpp:67
AudioParameterChoice(const ParameterID &parameterID, const String &parameterName, const StringArray &choices, int defaultItemIndex, const AudioParameterChoiceAttributes &attributes={})
Definition juce_AudioParameterChoice.cpp:29
std::atomic< float > value
Definition juce_AudioParameterChoice.h:143
~AudioParameterChoice() override
Definition juce_AudioParameterChoice.cpp:57
int getIndex() const noexcept
Definition juce_AudioParameterChoice.h:106
int getNumSteps() const override
Definition juce_AudioParameterChoice.cpp:68
String getText(float, int) const override
Definition juce_AudioParameterChoice.cpp:71
const float defaultValue
Definition juce_AudioParameterChoice.h:144
virtual void valueChanged(int newValue)
Definition juce_AudioParameterChoice.cpp:72
void setValueNotifyingHost(float newValue)
Definition juce_AudioProcessor.cpp:1517
Definition juce_NormalisableRange.h:40
ValueType interval
Definition juce_NormalisableRange.h:220
Definition juce_AudioProcessorParameterWithID.h:33
Definition juce_RangedAudioParameter.h:98
float convertTo0to1(float v) const noexcept
Definition juce_RangedAudioParameter.cpp:39
float convertFrom0to1(float v) const noexcept
Definition juce_RangedAudioParameter.cpp:45
Definition juce_StringArray.h:35
Definition juce_String.h:53
Definition juce_UnitTest.h:70
unsigned v[N_MAX]
Definition inflate.c:1584
static PuglViewHint int value
Definition pugl.h:1708
virtual ASIOError start()=0
#define jassert(expression)
Definition juce_UnitTestCategories.h:27
Definition carla_juce.cpp:31
RangedDirectoryIterator end(const RangedDirectoryIterator &)
Definition juce_RangedDirectoryIterator.h:184
Type jlimit(Type lowerLimit, Type upperLimit, Type valueToConstrain) noexcept
Definition juce_MathsFunctions.h:262
int roundToInt(const FloatType value) noexcept
Definition juce_MathsFunctions.h:465
png_uint_32 length
Definition png.c:2247
const char * text
Definition swell-functions.h:167
return c
Definition crypt.c:175