Vital
Loading...
Searching...
No Matches
value_bridge.h
Go to the documentation of this file.
1#pragma once
2
3#include "JuceHeader.h"
4#include "value.h"
5#include "synth_parameters.h"
6
17class ValueBridge : public AudioProcessorParameter {
18 public:
27 class Listener {
28 public:
32 virtual ~Listener() { }
33
40 virtual void parameterChanged(std::string name, vital::mono_float value) = 0;
41 };
42
46 ValueBridge() = delete;
47
54 ValueBridge(std::string name, vital::Value* value) :
55 AudioProcessorParameter(), name_(name), value_(value), listener_(nullptr),
56 source_changed_(false) {
57 details_ = vital::Parameters::getDetails(name);
58 span_ = details_.max - details_.min;
60 span_ = std::round(span_);
61 }
62
68 float getValue() const override {
69 return convertToPluginValue(value_->value());
70 }
71
80 void setValue(float value) override {
81 if (listener_ && !source_changed_) {
82 source_changed_ = true;
83 vital::mono_float synth_value = convertToEngineValue(value);
84 listener_->parameterChanged(name_.toStdString(), synth_value);
85 source_changed_ = false;
86 }
87 }
88
94 void setListener(Listener* listener) {
95 listener_ = listener;
96 }
97
103 float getDefaultValue() const override {
104 return convertToPluginValue(details_.default_value);
105 }
106
113 String getName(int maximumStringLength) const override {
114 return String(details_.display_name).substring(0, maximumStringLength);
115 }
116
124 String getLabel() const override {
125 return "";
126 }
127
139 String getText(float value, int maximumStringLength) const override {
140 float adjusted = convertToEngineValue(value);
141 String result = "";
142 if (details_.string_lookup)
143 result = details_.string_lookup[std::max<int>(0, std::min(adjusted, details_.max))];
144 else {
145 float display_value = details_.display_multiply * skewValue(adjusted) + details_.post_offset;
146 result = String(display_value) + details_.display_units;
147 }
148 return result.substring(0, maximumStringLength).trim();
149 }
150
159 float getValueForText(const String &text) const override {
160 return unskewValue(text.getFloatValue() / details_.display_multiply);
161 }
162
168 bool isAutomatable() const override {
169 return true;
170 }
171
180 int getNumSteps() const override {
181 if (isDiscrete())
182 return 1 + (int)span_;
183 return AudioProcessorParameter::getNumSteps();
184 }
185
191 bool isDiscrete() const override {
192 static constexpr int kMaxIndexedSteps = 300;
193 return details_.value_scale == vital::ValueDetails::kIndexed && span_ < kMaxIndexedSteps;
194 }
195
201 bool isBoolean() const override {
202 return isDiscrete() && span_ == 1.0f;
203 }
204
211 float convertToPluginValue(vital::mono_float synth_value) const {
212 return (synth_value - details_.min) / span_;
213 }
214
223 float convertToEngineValue(vital::mono_float plugin_value) const {
224 float value = plugin_value * span_ + details_.min;
225
227 return std::round(value);
228
229 return value;
230 }
231
237 void setValueNotifyHost(float new_value) {
238 if (!source_changed_) {
239 source_changed_ = true;
240 setValueNotifyingHost(new_value);
241 source_changed_ = false;
242 }
243 }
244
245 private:
251 float getSkewedValue() const {
252 return skewValue(value_->value());
253 }
254
265 float skewValue(float value) const {
266 switch (details_.value_scale) {
268 return value * value;
270 return value * value * value;
272 value *= value;
273 return value * value;
275 if (details_.display_invert)
276 return 1.0f / powf(2.0f, value);
277 return powf(2.0f, value);
279 return sqrtf(value);
280 default:
281 return value;
282 }
283 }
284
291 float unskewValue(float value) const {
292 switch (details_.value_scale) {
294 return sqrtf(value);
296 return powf(value, 1.0f / 3.0f);
298 return powf(value, 1.0f / 4.0f);
300 if (details_.display_invert)
301 return log2(1.0f / value);
302 return log2(value);
303 default:
304 return value;
305 }
306 }
307
308 String name_;
309 vital::ValueDetails details_;
310 vital::mono_float span_;
311 vital::Value* value_;
312 Listener* listener_;
313 bool source_changed_;
314
315 JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(ValueBridge)
316};
317
An interface for receiving parameter change notifications from the ValueBridge.
Definition value_bridge.h:27
virtual void parameterChanged(std::string name, vital::mono_float value)=0
Called when the parameter value changes.
virtual ~Listener()
Virtual destructor.
Definition value_bridge.h:32
A parameter bridge that connects a vital::Value to an AudioProcessorParameter, allowing the host to m...
Definition value_bridge.h:17
bool isDiscrete() const override
Checks if this parameter is discrete (indexed steps) or continuous.
Definition value_bridge.h:191
float convertToEngineValue(vital::mono_float plugin_value) const
Converts a normalized (0.0 to 1.0) parameter value back into the engine's range.
Definition value_bridge.h:223
String getText(float value, int maximumStringLength) const override
Converts a normalized value into a user-facing text string.
Definition value_bridge.h:139
float convertToPluginValue(vital::mono_float synth_value) const
Converts the internal engine value to a normalized value from 0.0 to 1.0.
Definition value_bridge.h:211
int getNumSteps() const override
Returns the number of discrete steps this parameter has, if any.
Definition value_bridge.h:180
void setValueNotifyHost(float new_value)
Sets the parameter value and notifies the host, preventing recursive updates.
Definition value_bridge.h:237
float getValue() const override
Gets the current normalized (0.0 to 1.0) value of the parameter.
Definition value_bridge.h:68
void setValue(float value) override
Sets the parameter value from a normalized (0.0 to 1.0) float.
Definition value_bridge.h:80
ValueBridge(std::string name, vital::Value *value)
Constructs a ValueBridge to expose a Vital parameter as a host-automatable parameter.
Definition value_bridge.h:54
float getValueForText(const String &text) const override
Converts a user-facing string back into a normalized parameter value.
Definition value_bridge.h:159
ValueBridge()=delete
Deleted default constructor.
void setListener(Listener *listener)
Sets a listener to receive parameter change callbacks.
Definition value_bridge.h:94
bool isAutomatable() const override
Indicates whether this parameter can be automated by the host.
Definition value_bridge.h:168
String getName(int maximumStringLength) const override
Returns the display name of this parameter, truncated to a given length.
Definition value_bridge.h:113
float getDefaultValue() const override
Returns the default normalized value of this parameter.
Definition value_bridge.h:103
String getLabel() const override
Returns the label (unit) associated with this parameter.
Definition value_bridge.h:124
bool isBoolean() const override
Checks if this parameter is essentially a boolean (on/off) parameter.
Definition value_bridge.h:201
static const ValueDetails & getDetails(const std::string &name)
Definition synth_parameters.h:200
A Processor that maintains and outputs a constant poly_float value.
Definition value.h:24
force_inline mono_float value() const
Returns the current mono_float value of the first lane.
Definition value.h:60
force_inline poly_float log2(poly_float value)
Approximates log2(value) for poly_float values using a polynomial approximation.
Definition futils.h:68
float mono_float
Definition common.h:33
Holds metadata about a single parameter (control) in the Vital synthesizer.
Definition synth_parameters.h:23
mono_float max
Maximum parameter value.
Definition synth_parameters.h:41
mono_float display_multiply
Multiplier for converting internal values to display units.
Definition synth_parameters.h:44
bool display_invert
If true, invert the displayed value range.
Definition synth_parameters.h:46
mono_float post_offset
Offset applied after scaling (for certain scale types).
Definition synth_parameters.h:43
@ kQuartic
Parameter value transformed by a quartic curve.
Definition synth_parameters.h:33
@ kSquareRoot
Parameter value transformed by a square root function.
Definition synth_parameters.h:34
@ kCubic
Parameter value transformed by a cubic curve.
Definition synth_parameters.h:32
@ kExponential
Parameter value transformed by an exponential function.
Definition synth_parameters.h:35
@ kIndexed
Parameter steps through discrete indexed values.
Definition synth_parameters.h:29
@ kQuadratic
Parameter value transformed by a quadratic curve.
Definition synth_parameters.h:31
mono_float min
Minimum parameter value.
Definition synth_parameters.h:40
std::string display_name
Human-readable name for display in UI.
Definition synth_parameters.h:48
const std::string * string_lookup
Optional lookup table for indexed parameter names.
Definition synth_parameters.h:49
mono_float default_value
Default value for the parameter.
Definition synth_parameters.h:42
ValueScale value_scale
The scaling mode of the parameter value.
Definition synth_parameters.h:45
std::string display_units
Units to display next to the parameter (e.g., "Hz", "dB").
Definition synth_parameters.h:47
Declares Value processors that output a constant value and can be dynamically set.