Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
AudioProcessorParameterSlider for JUCE
#include "AudioProcessorParameterSlider.h"
using namespace juce;
AudioProcessorParameterSlider::AudioProcessorParameterSlider()
{
initParameter(nullptr);
}
AudioProcessorParameterSlider::AudioProcessorParameterSlider (const AudioProcessorParameter* p)
{
initParameter(p);
}
AudioProcessorParameterSlider::AudioProcessorParameterSlider (const String &componentName):
Slider (componentName)
{
initParameter(nullptr);
}
void AudioProcessorParameterSlider::initParameter (const AudioProcessorParameter* p)
{
parameter = p;
textSuffixEnabled = true;
}
void AudioProcessorParameterSlider::setTextSuffixEnabled (bool v)
{
textSuffixEnabled = v;
}
const AudioProcessorParameter* AudioProcessorParameterSlider::getParameter()
{
jassert (parameter != nullptr);
return parameter;
}
void AudioProcessorParameterSlider::setParameter (const AudioProcessorParameter* p)
{
if (parameter == p)
return;
parameter = p;
// Repaint because text may change.
updateText();
repaint();
}
#if JUCE_COMPILER_SUPPORTS_LAMBDAS || defined (DOXYGEN)
// wraps SliderAttachment to link with the slider
AudioProcessorParameterSlider::SliderAttachment::SliderAttachment (AudioProcessorValueTreeState& stateToControl, const String& parameterID, AudioProcessorParameterSlider& sliderToControl)
: AudioProcessorValueTreeState::SliderAttachment (stateToControl, parameterID, sliderToControl)
{
sliderToControl.setParameter(stateToControl.getParameter(parameterID));
}
#endif
String AudioProcessorParameterSlider::getTextFromValue (double value)
{
if (parameter == nullptr)
return Slider::getTextFromValue (value);
// juce::AudioProcessorValueTreeState::SliderAttachment sets the slider minimum and maximum to custom values.
// We map the range to a 0 to 1 range.
const NormalisableRange<double> range (getMinimum(), getMaximum());
const float normalizedVal = (float) range.convertTo0to1 (value);
String result = parameter->getText (normalizedVal, getNumDecimalPlacesToDisplay());
if (! textSuffixEnabled)
{
String label = parameter->getLabel();
if (! label.isEmpty() && result.endsWithIgnoreCase (parameter->getLabel()))
{
result = result.dropLastCharacters (label.length());
result.trimEnd();
}
}
return result;
}
double AudioProcessorParameterSlider::getValueFromText (const String& text)
{
if (parameter == nullptr)
return Slider::getValueFromText (text);
return parameter->getValueForText (text);
}
#ifndef AUDIOPROCESSORPARAMETERSLIDER_H_INCLUDED
#define AUDIOPROCESSORPARAMETERSLIDER_H_INCLUDED
#include "JuceHeader.h"
#include <assert.h>
// A Slider for an audio plugin parameter that has
// * The same mapping of proportion-of-length to value as the parameter (same as automation).
// * The same text formatting as the parameter.
//
// Internally the value for the Slider is the parameter value in 0 to 1 range,
// but due to the text formatting it looks like user-readable values.
class AudioProcessorParameterSlider : public juce::Slider
{
public:
AudioProcessorParameterSlider();
AudioProcessorParameterSlider(const juce::String &componentName);
AudioProcessorParameterSlider(const juce::AudioProcessorParameter*);
const juce::AudioProcessorParameter* getParameter();
void setParameter (const juce::AudioProcessorParameter*);
void setTextSuffixEnabled (bool);
#if JUCE_COMPILER_SUPPORTS_LAMBDAS || defined (DOXYGEN)
// SliderAttachment
class SliderAttachment : public juce::AudioProcessorValueTreeState::SliderAttachment
{
public:
SliderAttachment (juce::AudioProcessorValueTreeState& stateToControl,
const juce::String& parameterID,
AudioProcessorParameterSlider& sliderToControl);
virtual ~SliderAttachment() = default;
};
#endif
juce::String getTextFromValue (double value) override;
double getValueFromText (const juce::String& text) override;
protected:
const juce::AudioProcessorParameter* parameter;
private:
void initParameter(const juce::AudioProcessorParameter*);
bool textSuffixEnabled;
};
#endif // AUDIOPROCESSORPARAMETERSLIDER_H_INCLUDED
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment