diff --git a/modules/juce_audio_processors/utilities/juce_AudioParameterChoice.h b/modules/juce_audio_processors/utilities/juce_AudioParameterChoice.h index dc711d5830..6f909ccde1 100644 --- a/modules/juce_audio_processors/utilities/juce_AudioParameterChoice.h +++ b/modules/juce_audio_processors/utilities/juce_AudioParameterChoice.h @@ -67,6 +67,7 @@ public: private: //============================================================================== float value, defaultValue; + const int maxIndex; float getValue() const override; void setValue (float newValue) override; diff --git a/modules/juce_audio_processors/utilities/juce_AudioParameterInt.h b/modules/juce_audio_processors/utilities/juce_AudioParameterInt.h index 4c2f02c33b..fbc6f7bbf3 100644 --- a/modules/juce_audio_processors/utilities/juce_AudioParameterInt.h +++ b/modules/juce_audio_processors/utilities/juce_AudioParameterInt.h @@ -64,7 +64,7 @@ public: private: //============================================================================== - int minValue, maxValue; + const int minValue, maxValue, rangeOfValues; float value, defaultValue; float getValue() const override; diff --git a/modules/juce_audio_processors/utilities/juce_AudioProcessorParameters.cpp b/modules/juce_audio_processors/utilities/juce_AudioProcessorParameters.cpp index 88da76bc00..6924d0218d 100644 --- a/modules/juce_audio_processors/utilities/juce_AudioProcessorParameters.cpp +++ b/modules/juce_audio_processors/utilities/juce_AudioProcessorParameters.cpp @@ -83,7 +83,7 @@ AudioParameterInt::AudioParameterInt (const String& idToUse, const String& nameT int mn, int mx, int def, const String& labelToUse) : AudioProcessorParameterWithID (idToUse, nameToUse, labelToUse), - minValue (mn), maxValue (mx), + minValue (mn), maxValue (mx), rangeOfValues (maxValue - minValue), value ((float) def), defaultValue (convertTo0to1 (def)) { @@ -93,13 +93,13 @@ AudioParameterInt::AudioParameterInt (const String& idToUse, const String& nameT AudioParameterInt::~AudioParameterInt() {} int AudioParameterInt::limitRange (int v) const noexcept { return jlimit (minValue, maxValue, v); } -float AudioParameterInt::convertTo0to1 (int v) const noexcept { return (limitRange (v) - minValue) / (float) (maxValue - minValue); } -int AudioParameterInt::convertFrom0to1 (float v) const noexcept { return limitRange (roundToInt ((v * (float) (maxValue - minValue)) + minValue)); } +float AudioParameterInt::convertTo0to1 (int v) const noexcept { return (limitRange (v) - minValue) / (float) rangeOfValues; } +int AudioParameterInt::convertFrom0to1 (float v) const noexcept { return limitRange (roundToInt ((v * (float) rangeOfValues) + minValue)); } float AudioParameterInt::getValue() const { return convertTo0to1 (roundToInt (value)); } void AudioParameterInt::setValue (float newValue) { value = (float) convertFrom0to1 (newValue); } float AudioParameterInt::getDefaultValue() const { return defaultValue; } -int AudioParameterInt::getNumSteps() const { return AudioProcessorParameterWithID::getNumSteps(); } +int AudioParameterInt::getNumSteps() const { return rangeOfValues + 1; } float AudioParameterInt::getValueForText (const String& text) const { return convertTo0to1 (text.getIntValue()); } String AudioParameterInt::getText (float v, int /*length*/) const { return String (convertFrom0to1 (v)); } @@ -145,16 +145,17 @@ AudioParameterChoice::AudioParameterChoice (const String& idToUse, const String& const StringArray& c, int def, const String& labelToUse) : AudioProcessorParameterWithID (idToUse, nameToUse, labelToUse), choices (c), value ((float) def), - defaultValue (convertTo0to1 (def)) + defaultValue (convertTo0to1 (def)), + maxIndex (choices.size() - 1) { jassert (choices.size() > 0); // you must supply an actual set of items to choose from! } AudioParameterChoice::~AudioParameterChoice() {} -int AudioParameterChoice::limitRange (int v) const noexcept { return jlimit (0, choices.size() - 1, v); } -float AudioParameterChoice::convertTo0to1 (int v) const noexcept { return jlimit (0.0f, 1.0f, (v + 0.5f) / (float) choices.size()); } -int AudioParameterChoice::convertFrom0to1 (float v) const noexcept { return limitRange ((int) (v * (float) choices.size())); } +int AudioParameterChoice::limitRange (int v) const noexcept { return jlimit (0, maxIndex, v); } +float AudioParameterChoice::convertTo0to1 (int v) const noexcept { return jlimit (0.0f, 1.0f, v / (float) maxIndex); } +int AudioParameterChoice::convertFrom0to1 (float v) const noexcept { return limitRange (roundToInt (v * (float) maxIndex)); } float AudioParameterChoice::getValue() const { return convertTo0to1 (roundToInt (value)); } void AudioParameterChoice::setValue (float newValue) { value = (float) convertFrom0to1 (newValue); }