From 544d3284e3b637f79e3cfcc7fd875256281848e5 Mon Sep 17 00:00:00 2001 From: hogliux Date: Thu, 26 Oct 2017 16:19:03 +0100 Subject: [PATCH] Added a callback to AudioParameterBool, Choice, Float and Int which is called when the parameter value changes --- .../utilities/juce_AudioParameterBool.h | 5 +++++ .../utilities/juce_AudioParameterChoice.h | 5 +++++ .../utilities/juce_AudioParameterFloat.h | 5 +++++ .../utilities/juce_AudioParameterInt.h | 5 +++++ .../utilities/juce_AudioProcessorParameters.cpp | 12 ++++++++---- 5 files changed, 28 insertions(+), 4 deletions(-) diff --git a/modules/juce_audio_processors/utilities/juce_AudioParameterBool.h b/modules/juce_audio_processors/utilities/juce_AudioParameterBool.h index c664ce8014..63d5750a50 100644 --- a/modules/juce_audio_processors/utilities/juce_AudioParameterBool.h +++ b/modules/juce_audio_processors/utilities/juce_AudioParameterBool.h @@ -52,6 +52,11 @@ public: /** Changes the parameter's current value to a new boolean. */ AudioParameterBool& operator= (bool newValue); +protected: + /** Override this method if you are interested in receiving callbacks + when the parameter value changes. + */ + virtual void valueChanged (bool newValue); private: //============================================================================== diff --git a/modules/juce_audio_processors/utilities/juce_AudioParameterChoice.h b/modules/juce_audio_processors/utilities/juce_AudioParameterChoice.h index 6f909ccde1..15f2a58fb9 100644 --- a/modules/juce_audio_processors/utilities/juce_AudioParameterChoice.h +++ b/modules/juce_audio_processors/utilities/juce_AudioParameterChoice.h @@ -63,6 +63,11 @@ public: /** Provides access to the list of choices that this parameter is working with. */ const StringArray choices; +protected: + /** Override this method if you are interested in receiving callbacks + when the parameter value changes. + */ + virtual void valueChanged (int newValue); private: //============================================================================== diff --git a/modules/juce_audio_processors/utilities/juce_AudioParameterFloat.h b/modules/juce_audio_processors/utilities/juce_AudioParameterFloat.h index c422749edf..4d94f80371 100644 --- a/modules/juce_audio_processors/utilities/juce_AudioParameterFloat.h +++ b/modules/juce_audio_processors/utilities/juce_AudioParameterFloat.h @@ -69,6 +69,11 @@ public: /** Provides access to the parameter's range. */ NormalisableRange range; +protected: + /** Override this method if you are interested in receiving callbacks + when the parameter value changes. + */ + virtual void valueChanged (float newValue); private: //============================================================================== diff --git a/modules/juce_audio_processors/utilities/juce_AudioParameterInt.h b/modules/juce_audio_processors/utilities/juce_AudioParameterInt.h index fbc6f7bbf3..ffc616f411 100644 --- a/modules/juce_audio_processors/utilities/juce_AudioParameterInt.h +++ b/modules/juce_audio_processors/utilities/juce_AudioParameterInt.h @@ -61,6 +61,11 @@ public: /** Returns the parameter's range. */ Range getRange() const noexcept { return Range (minValue, maxValue); } +protected: + /** Override this method if you are interested in receiving callbacks + when the parameter value changes. + */ + virtual void valueChanged (int newValue); private: //============================================================================== diff --git a/modules/juce_audio_processors/utilities/juce_AudioProcessorParameters.cpp b/modules/juce_audio_processors/utilities/juce_AudioProcessorParameters.cpp index 6924d0218d..26228dbc45 100644 --- a/modules/juce_audio_processors/utilities/juce_AudioProcessorParameters.cpp +++ b/modules/juce_audio_processors/utilities/juce_AudioProcessorParameters.cpp @@ -59,10 +59,11 @@ AudioParameterFloat::AudioParameterFloat (String pid, String nm, float minValue, AudioParameterFloat::~AudioParameterFloat() {} float AudioParameterFloat::getValue() const { return range.convertTo0to1 (value); } -void AudioParameterFloat::setValue (float newValue) { value = range.convertFrom0to1 (newValue); } +void AudioParameterFloat::setValue (float newValue) { value = range.convertFrom0to1 (newValue); valueChanged (get()); } float AudioParameterFloat::getDefaultValue() const { return range.convertTo0to1 (defaultValue); } int AudioParameterFloat::getNumSteps() const { return AudioProcessorParameterWithID::getNumSteps(); } float AudioParameterFloat::getValueForText (const String& text) const { return range.convertTo0to1 (text.getFloatValue()); } +void AudioParameterFloat::valueChanged (float) {} String AudioParameterFloat::getText (float v, int length) const { @@ -97,11 +98,12 @@ float AudioParameterInt::convertTo0to1 (int v) const noexcept { retur 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); } +void AudioParameterInt::setValue (float newValue) { value = (float) convertFrom0to1 (newValue); valueChanged (get()); } float AudioParameterInt::getDefaultValue() const { return defaultValue; } 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)); } +void AudioParameterInt::valueChanged (int) {} AudioParameterInt& AudioParameterInt::operator= (int newValue) { @@ -124,12 +126,13 @@ AudioParameterBool::AudioParameterBool (const String& idToUse, const String& nam AudioParameterBool::~AudioParameterBool() {} float AudioParameterBool::getValue() const { return value; } -void AudioParameterBool::setValue (float newValue) { value = newValue; } +void AudioParameterBool::setValue (float newValue) { value = newValue; valueChanged (get()); } float AudioParameterBool::getDefaultValue() const { return defaultValue; } int AudioParameterBool::getNumSteps() const { return 2; } bool AudioParameterBool::isDiscrete() const { return true; } float AudioParameterBool::getValueForText (const String& text) const { return text.getIntValue() != 0 ? 1.0f : 0.0f; } String AudioParameterBool::getText (float v, int /*length*/) const { return String ((int) (v > 0.5f ? 1 : 0)); } +void AudioParameterBool::valueChanged (bool) {} AudioParameterBool& AudioParameterBool::operator= (bool newValue) { @@ -158,12 +161,13 @@ float AudioParameterChoice::convertTo0to1 (int v) const noexcept { retur 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); } +void AudioParameterChoice::setValue (float newValue) { value = (float) convertFrom0to1 (newValue); valueChanged (getIndex()); } float AudioParameterChoice::getDefaultValue() const { return defaultValue; } int AudioParameterChoice::getNumSteps() const { return choices.size(); } bool AudioParameterChoice::isDiscrete() const { return true; } float AudioParameterChoice::getValueForText (const String& text) const { return convertTo0to1 (choices.indexOf (text)); } String AudioParameterChoice::getText (float v, int /*length*/) const { return choices [convertFrom0to1 (v)]; } +void AudioParameterChoice::valueChanged (int) {} AudioParameterChoice& AudioParameterChoice::operator= (int newValue) {