Skip to content

Instantly share code, notes, and snippets.

@talaviram
Created January 16, 2018 11:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save talaviram/9fc97fd6c9a200c6823df0b4e4e6b77d to your computer and use it in GitHub Desktop.
Save talaviram/9fc97fd6c9a200c6823df0b4e4e6b77d to your computer and use it in GitHub Desktop.
An elegant (IMHO) solution to link between parameters and components for JUCE (using AudioProcessorValueTree).
/*
==============================================================================
AudioParameterUtilities.cpp
Created: 16 Jan 2018 10:34:07am
Author: Tal Aviram
==============================================================================
*/
#include "AudioParameterUtilities.h"
const char* AudioParameterUtilities::ASSIGNED_PARAMETER_ID = "assigned_param_id";
AudioProcessorValueTreeState::ButtonAttachment* AudioParameterUtilities::createComponentAttachment(AudioProcessorValueTreeState& state, const juce::String& paramId, juce::Button& component) {
component.getProperties().set(ASSIGNED_PARAMETER_ID, paramId);
return new AudioProcessorValueTreeState::ButtonAttachment(state, paramId, component);
}
AudioProcessorValueTreeState::SliderAttachment* AudioParameterUtilities::createComponentAttachment(AudioProcessorValueTreeState& state, const juce::String& paramId, juce::Slider& component) {
component.getProperties().set(ASSIGNED_PARAMETER_ID, paramId);
return new AudioProcessorValueTreeState::SliderAttachment(state, paramId, component);
}
AudioProcessorValueTreeState::ComboBoxAttachment* AudioParameterUtilities::createComponentAttachment(juce::AudioProcessorValueTreeState& state, const juce::String& paramId, juce::ComboBox& component) {
component.getProperties().set(ASSIGNED_PARAMETER_ID, paramId);
return new AudioProcessorValueTreeState::ComboBoxAttachment(state, paramId, component);
}
int AudioParameterUtilities::getControlParameterIndexForComponent(juce::AudioProcessorValueTreeState &state, juce::Component *component) {
juce::String paramId = component->getProperties().getWithDefault(ASSIGNED_PARAMETER_ID, "");
// fallback for components that got components inside them (we walk only one parent back, seems sufficient).
if (paramId.isEmpty())
{
auto parent = component->getParentComponent();
if (parent != nullptr) paramId = parent->getProperties().getWithDefault(ASSIGNED_PARAMETER_ID, "");
}
auto param = state.getParameter(paramId);
return (param != nullptr && paramId.isNotEmpty()) ? state.getParameter(paramId)->getParameterIndex() : -1;
}
/*
==============================================================================
AudioParameterUtilities.h
Additional utilities useful for parameters.
Created: 16 Jan 2018 10:34:07am
Author: Tal Aviram
==============================================================================
*/
#pragma once
#include "JuceHeader.h"
class AudioParameterUtilities {
public:
static const char* ASSIGNED_PARAMETER_ID;
static juce::AudioProcessorValueTreeState::ButtonAttachment* createComponentAttachment(AudioProcessorValueTreeState& state, const juce::String& paramId, juce::Button& component);
static juce::AudioProcessorValueTreeState::SliderAttachment* createComponentAttachment(AudioProcessorValueTreeState& state, const juce::String& paramId, juce::Slider& component);
static juce::AudioProcessorValueTreeState::ComboBoxAttachment* createComponentAttachment(AudioProcessorValueTreeState& state, const juce::String& paramId, juce::ComboBox& component);
/**
Simplifies the use of AudioProcessorEditor::getControlParameterIndexForComponent when using the utility class.
*/
static int getControlParameterIndexForComponent(juce::AudioProcessorValueTreeState& state, juce::Component* component);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment