LMMS
Loading...
Searching...
No Matches
juce_AudioParameterInt.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 int minValue, int maxValue, int def,
31 const AudioParameterIntAttributes& attributes)
32 : RangedAudioParameter (idToUse, nameToUse, attributes.getAudioProcessorParameterWithIDAttributes()),
33 range ([minValue, maxValue]
34 {
35 NormalisableRange<float> rangeWithInterval { (float) minValue, (float) maxValue,
36 [] (float start, float end, float v) { return jlimit (start, end, v * (end - start) + start); },
37 [] (float start, float end, float v) { return jlimit (0.0f, 1.0f, (v - start) / (end - start)); },
38 [] (float start, float end, float v) { return (float) roundToInt (juce::jlimit (start, end, v)); } };
39 rangeWithInterval.interval = 1.0f;
40 return rangeWithInterval;
41 }()),
42 value ((float) def),
43 defaultValue (convertTo0to1 ((float) def)),
44 stringFromIntFunction (attributes.getStringFromValueFunction() != nullptr
45 ? attributes.getStringFromValueFunction()
46 : [] (int v, int) { return String (v); }),
47 intFromStringFunction (attributes.getValueFromStringFunction() != nullptr
48 ? attributes.getValueFromStringFunction()
49 : [] (const String& text) { return text.getIntValue(); })
50{
51 jassert (minValue < maxValue); // must have a non-zero range of values!
52}
53
55{
56 #if __cpp_lib_atomic_is_always_lock_free
57 static_assert (std::atomic<float>::is_always_lock_free,
58 "AudioParameterInt requires a lock-free std::atomic<float>");
59 #endif
60}
61
63void AudioParameterInt::setValue (float newValue) { value = convertFrom0to1 (newValue); valueChanged (get()); }
65int AudioParameterInt::getNumSteps() const { return ((int) getNormalisableRange().getRange().getLength()) + 1; }
69
70AudioParameterInt& AudioParameterInt::operator= (int newValue)
71{
72 if (get() != newValue)
73 setValueNotifyingHost (convertTo0to1 ((float) newValue));
74
75 return *this;
76}
77
78
79//==============================================================================
80//==============================================================================
81#if JUCE_UNIT_TESTS
82
83struct AudioParameterIntTests : public UnitTest
84{
85 AudioParameterIntTests()
86 : UnitTest ("AudioParameterInt", UnitTestCategories::audioProcessorParameters)
87 {}
88
89 void runTest() override
90 {
91 beginTest ("Three options switches at the correct points");
92 {
93 AudioParameterInt intParam ({}, {}, 1, 3, 1);
94
95 intParam.setValueNotifyingHost (0.0f);
96 expectEquals (intParam.get(), 1);
97
98 intParam.setValueNotifyingHost (0.2f);
99 expectEquals (intParam.get(), 1);
100
101 intParam.setValueNotifyingHost (0.3f);
102 expectEquals (intParam.get(), 2);
103
104 intParam.setValueNotifyingHost (0.7f);
105 expectEquals (intParam.get(), 2);
106
107 intParam.setValueNotifyingHost (0.8f);
108 expectEquals (intParam.get(), 3);
109
110 intParam.setValueNotifyingHost (1.0f);
111 expectEquals (intParam.get(), 3);
112 }
113
114 beginTest ("Out-of-bounds input");
115 {
116 AudioParameterInt intParam ({}, {}, -1, 2, 0);
117
118 intParam.setValueNotifyingHost (-0.5f);
119 expectEquals (intParam.get(), -1);
120
121 intParam.setValueNotifyingHost (1.5f);
122 expectEquals (intParam.get(), 2);
123
124 intParam = -5;
125 expectEquals (intParam.get(), -1);
126
127 intParam = 5;
128 expectEquals (intParam.get(), 2);
129 }
130 }
131};
132
133static AudioParameterIntTests audioParameterIntTests;
134
135#endif
136
137} // namespace juce
Definition juce_AudioParameterInt.h:33
AudioParameterInt(const ParameterID &parameterID, const String &parameterName, int minValue, int maxValue, int defaultValue, const AudioParameterIntAttributes &attributes={})
Definition juce_AudioParameterInt.cpp:29
float getValueForText(const String &) const override
Definition juce_AudioParameterInt.cpp:66
float getDefaultValue() const override
Definition juce_AudioParameterInt.cpp:64
int getNumSteps() const override
Definition juce_AudioParameterInt.cpp:65
int get() const noexcept
Definition juce_AudioParameterInt.h:112
std::function< String(int, int)> stringFromIntFunction
Definition juce_AudioParameterInt.h:146
std::function< int(const String &)> intFromStringFunction
Definition juce_AudioParameterInt.h:147
float getValue() const override
Definition juce_AudioParameterInt.cpp:62
const NormalisableRange< float > & getNormalisableRange() const override
Definition juce_AudioParameterInt.h:126
void setValue(float newValue) override
Definition juce_AudioParameterInt.cpp:63
~AudioParameterInt() override
Definition juce_AudioParameterInt.cpp:54
virtual void valueChanged(int newValue)
Definition juce_AudioParameterInt.cpp:68
String getText(float, int) const override
Definition juce_AudioParameterInt.cpp:67
std::atomic< float > value
Definition juce_AudioParameterInt.h:144
const float defaultValue
Definition juce_AudioParameterInt.h:145
Range< int > getRange() const noexcept
Definition juce_AudioParameterInt.h:123
const NormalisableRange< float > range
Definition juce_AudioParameterInt.h:143
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_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