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_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
58 "AudioParameterInt requires a lock-free std::atomic<float>");
59 #endif
60}
61
62float AudioParameterInt::getValue() const { return convertTo0to1 (value); }
63void AudioParameterInt::setValue (float newValue) { value = convertFrom0to1 (newValue); valueChanged (get()); }
64float AudioParameterInt::getDefaultValue() const { return defaultValue; }
65int AudioParameterInt::getNumSteps() const { return ((int) getNormalisableRange().getRange().getLength()) + 1; }
66float AudioParameterInt::getValueForText (const String& text) const { return convertTo0to1 ((float) intFromStringFunction (text)); }
67String AudioParameterInt::getText (float v, int length) const { return stringFromIntFunction ((int) convertFrom0to1 (v), length); }
69
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
84{
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
134
135#endif
136
137} // namespace juce
Properties of an AudioParameterInt.
Provides a class of AudioProcessorParameter that can be used as an integer value with a given range.
AudioParameterInt(const ParameterID &parameterID, const String &parameterName, int minValue, int maxValue, int defaultValue, const AudioParameterIntAttributes &attributes={})
Creates a AudioParameterInt with the specified parameters.
AudioParameterInt & operator=(int newValue)
Changes the parameter's current value to a new integer.
int get() const noexcept
Returns the parameter's current value as an integer.
const NormalisableRange< float > & getNormalisableRange() const override
Returns the range of values that the parameter can take.
~AudioParameterInt() override
Destructor.
virtual void valueChanged(int newValue)
Override this method if you are interested in receiving callbacks when the parameter value changes.
Range< int > getRange() const noexcept
Returns the parameter's range.
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.
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.