iPlug2 - C++ Audio Plug-in Framework
Loading...
Searching...
No Matches
IPlugParameter.h
Go to the documentation of this file.
1/*
2 ==============================================================================
3
4 This file is part of the iPlug 2 library. Copyright (C) the iPlug 2 developers.
5
6 See LICENSE.txt for more info.
7
8 ==============================================================================
9*/
10
11#pragma once
12
18#include <atomic>
19#include <cstring>
20#include <functional>
21#include <memory>
22
23#include "wdlstring.h"
24
25#include "IPlugUtilities.h"
26
27BEGIN_IPLUG_NAMESPACE
28
30class IParam
31{
32public:
33
35 enum EParamType { kTypeNone, kTypeBool, kTypeInt, kTypeEnum, kTypeDouble };
36
38 enum EParamUnit { kUnitPercentage, kUnitSeconds, kUnitMilliseconds, kUnitSamples, kUnitDB, kUnitLinearGain, kUnitPan, kUnitPhase, kUnitDegrees, kUnitMeters, kUnitRate, kUnitRatio, kUnitFrequency, kUnitOctaves, kUnitCents, kUnitAbsCents, kUnitSemitones, kUnitMIDINote, kUnitMIDICtrlNum, kUnitBPM, kUnitBeats, kUnitCustom };
39
41 enum EDisplayType { kDisplayLinear, kDisplayLog, kDisplayExp, kDisplaySquared, kDisplaySquareRoot, kDisplayCubed, kDisplayCubeRoot };
42
44 enum EFlags
45 {
57 kFlagMeta = 0x10,
58 };
59
62 {
63 kShapeLinear = 0,
64 kShapePowCurve = 1,
65 kShapeExponential = 2,
66 kShapeUnknown,
67 };
68
70 using DisplayFunc = std::function<void(double, WDL_String&)>;
71
72#pragma mark - Shape
73
75 struct Shape
76 {
77 virtual ~Shape() {}
78
80 virtual Shape* Clone() const = 0;
81
84 virtual void Init(const IParam& param) {}
85
87 virtual EDisplayType GetDisplayType() const = 0;
88
93 virtual double NormalizedToValue(double value, const IParam& param) const = 0;
94
99 virtual double ValueToNormalized(double value, const IParam& param) const = 0;
100 };
101
103 struct ShapeLinear : public Shape
104 {
105 Shape* Clone() const override { return new ShapeLinear(*this); }
106 IParam::EDisplayType GetDisplayType() const override { return kDisplayLinear; }
107 double NormalizedToValue(double value, const IParam& param) const override;
108 double ValueToNormalized(double value, const IParam& param) const override;
109
110 double mShape;
111 };
112
114 struct ShapePowCurve : public Shape
115 {
116 ShapePowCurve(double shape);
117 Shape* Clone() const override { return new ShapePowCurve(*this); }
118 IParam::EDisplayType GetDisplayType() const override;
119 double NormalizedToValue(double value, const IParam& param) const override;
120 double ValueToNormalized(double value, const IParam& param) const override;
121
122 double mShape;
123 };
124
126 struct ShapeExp : public Shape
127 {
128 void Init(const IParam& param) override;
129 Shape* Clone() const override { return new ShapeExp(*this); }
130 IParam::EDisplayType GetDisplayType() const override { return kDisplayLog; }
131 double NormalizedToValue(double value, const IParam& param) const override;
132 double ValueToNormalized(double value, const IParam& param) const override;
133
134 double mMul = 1.0;
135 double mAdd = 1.0;
136 };
137
138#pragma mark -
139
140 IParam();
141
142 IParam(const IParam&) = delete;
143 IParam& operator=(const IParam&) = delete;
144
153 void InitBool(const char* name, bool defaultValue, const char* label = "", int flags = 0, const char* group = "", const char* offText = "off", const char* onText = "on");
154
163 void InitEnum(const char* name, int defaultValue, int nEnums, const char* label = "", int flags = 0, const char* group = "", const char* listItems = 0, ...);
164
171 void InitEnum(const char* name, int defaultValue, const std::initializer_list<const char*>& listItems, int flags = 0, const char* group = "");
172
181 void InitInt(const char* name, int defaultValue, int minVal, int maxVal, const char* label = "", int flags = 0, const char* group = "");
182
195 void InitDouble(const char* name, double defaultVal, double minVal, double maxVal, double step, const char* label = "", int flags = 0, const char* group = "", const Shape& shape = ShapeLinear(), EParamUnit unit = kUnitCustom, DisplayFunc displayFunc = nullptr);
196
205 void InitSeconds(const char* name, double defaultVal = 1., double minVal = 0., double maxVal = 10., double step = 0.1, int flags = 0, const char* group = "");
206
215 void InitMilliseconds(const char* name, double defaultVal = 1., double minVal = 0., double maxVal = 100., int flags = 0, const char* group = "");
216
225 void InitFrequency(const char* name, double defaultVal = 1000., double minVal = 0.1, double maxVal = 10000., double step = 0.1, int flags = 0, const char* group = "");
226
234 void InitPitch(const char* name, int defaultVal = 60, int minVal = 0, int maxVal = 128, int flags = 0, const char* group = "", bool middleCisC4 = false);
235
244 void InitGain(const char* name, double defaultVal = 0., double minVal = -70., double maxVal = 24., double step = 0.5, int flags = 0, const char* group = "");
245
253 void InitPercentage(const char* name, double defaultVal = 0., double minVal = 0., double maxVal = 100., int flags = 0, const char* group = "");
254
262 void InitAngleDegrees(const char* name, double defaultVal = 0., double minVal = 0., double maxVal = 360., int flags = 0, const char* group = "");
263
269 void Init(const IParam& p, const char* searchStr = "", const char* replaceStr = "", const char* newGroup = "");
270
274 double StringToValue(const char* str) const;
275
279 inline double Constrain(double value) const
280 {
281 return Clip((mFlags & kFlagStepped ? std::round(value / mStep) * mStep : value), mMin, mMax);
282 }
283
287 inline double ConstrainNormalized(double normalizedValue) const
288 {
289 return ToNormalized(mShape->NormalizedToValue(normalizedValue, *this));
290 }
291
295 inline double ToNormalized(double nonNormalizedValue) const
296 {
297 return Clip(mShape->ValueToNormalized(Constrain(nonNormalizedValue), *this), 0., 1.);
298 }
299
303 inline double FromNormalized(double normalizedValue) const
304 {
305 return Constrain(mShape->NormalizedToValue(normalizedValue, *this));
306 }
307
310 void Set(double value) { mValue.store(Constrain(value)); }
311
314 void SetNormalized(double normalizedValue) { Set(FromNormalized(normalizedValue)); }
315
318 void SetString(const char* str) { mValue.store(StringToValue(str)); }
319
321 void SetToDefault() { mValue.store(mDefault); }
322
325 void SetDefault(double value) { mDefault = value; SetToDefault(); }
326
330 void SetDisplayText(double value, const char* str);
331
334 void SetDisplayPrecision(int precision);
335
338 void SetLabel(const char* label) { strcpy(mLabel, label); }
339
342 void SetDisplayFunc(DisplayFunc func) { mDisplayFunction = func; }
343
346 double Value() const { return mValue.load(); }
347
350 bool Bool() const { return (mValue.load() >= 0.5); }
351
354 int Int() const { return static_cast<int>(mValue.load()); }
355
360 double DBToAmp() const { return iplug::DBToAmp(mValue.load()); }
361
364 double GetNormalized() const { return ToNormalized(mValue.load()); }
365
369 void GetDisplay(WDL_String& display, bool withDisplayText = true) const { GetDisplay(mValue.load(), false, display, withDisplayText); }
370
376 void GetDisplay(double value, bool normalized, WDL_String& display, bool withDisplayText = true) const;
377
381 void GetDisplayWithLabel(WDL_String& display, bool withDisplayText = true) const
382 {
383 GetDisplay(mValue.load(), false, display, withDisplayText);
384 const char* hostlabel = GetLabel();
385 if (CStringHasContents(hostlabel))
386 {
387 display.Append(" ");
388 display.Append(hostlabel);
389 }
390 }
391
394 const char* GetName() const;
395
398 const char* GetLabel() const;
399
402 const char* GetGroup() const;
403
406 const char* GetCustomUnit() const { return mUnit == kUnitCustom ? mLabel : nullptr; }
407
410 int NDisplayTexts() const;
411
415 const char* GetDisplayText(double value) const;
416
421 const char* GetDisplayTextAtIdx(int idx, double* pValue = nullptr) const;
422
427 bool MapDisplayText(const char* str, double* pValue) const;
428
432 EParamType Type() const { return mType; }
433
437 EParamUnit Unit() const { return mUnit; }
438
442 EDisplayType DisplayType() const { return mShape->GetDisplayType(); }
443
447 double GetDefault(bool normalized = false) const { return normalized ? ToNormalized(GetDefault()) : mDefault; }
448
451 double GetMin() const { return mMin; }
452
455 double GetMax() const { return mMax; }
456
460 void GetBounds(double& lo, double& hi) const;
461
464 double GetRange() const { return mMax - mMin; }
465
468 double GetStep() const { return mStep; }
469
472 int GetDisplayPrecision() const {return mDisplayPrecision;}
473
476 int GetFlags() const { return mFlags; }
477
479 bool GetCanAutomate() const { return !(mFlags & kFlagCannotAutomate); }
480
482 bool GetStepped() const { return mFlags & kFlagStepped; }
483
485 bool GetNegateDisplay() const { return mFlags & kFlagNegateDisplay; }
486
488 bool GetSignDisplay() const { return mFlags & kFlagSignDisplay; }
489
491 bool GetMeta() const { return mFlags & kFlagMeta; }
492
494 EShapeIDs GetShapeID() const;
495
497 double GetShapeValue() const;
498
502 void GetJSON(WDL_String& json, int idx) const;
503
505 void PrintDetails() const;
506private:
508 struct DisplayText
509 {
510 double mValue;
511 char mText[MAX_PARAM_DISPLAY_LEN];
512 };
513
514 EParamType mType = kTypeNone;
515 EParamUnit mUnit = kUnitCustom;
516 std::atomic<double> mValue{0.0};
517 double mMin = 0.0;
518 double mMax = 1.0;
519 double mStep = 1.0;
520 double mDefault = 0.0;
521 int mDisplayPrecision = 0;
522 int mFlags = 0;
523
524 char mName[MAX_PARAM_NAME_LEN];
525 char mLabel[MAX_PARAM_LABEL_LEN];
526 char mParamGroup[MAX_PARAM_GROUP_LEN];
527
528 std::unique_ptr<Shape> mShape;
529 DisplayFunc mDisplayFunction = nullptr;
530
531 WDL_TypedBuf<DisplayText> mDisplayTexts;
532} WDL_FIXALIGN;
533
534END_IPLUG_NAMESPACE
Utility functions and macros.
IPlug's parameter class.
double GetStep() const
Returns the parameter's step size.
double GetDefault(bool normalized=false) const
Returns the parameter's default value.
void InitGain(const char *name, double defaultVal=0., double minVal=-70., double maxVal=24., double step=0.5, int flags=0, const char *group="")
Initialize the parameter as gain (units in decibels)
void InitAngleDegrees(const char *name, double defaultVal=0., double minVal=0., double maxVal=360., int flags=0, const char *group="")
Initialize the parameter as angle in degrees.
void SetToDefault()
Replaces the parameter's current value with the default one
EParamType Type() const
Get the parameter's type.
int GetFlags() const
Returns the parameter's flags.
double ToNormalized(double nonNormalizedValue) const
Convert a real value to normalized value for this parameter.
void InitPercentage(const char *name, double defaultVal=0., double minVal=0., double maxVal=100., int flags=0, const char *group="")
Initialize the parameter as percentage.
bool GetSignDisplay() const
EFlags
Flags to determine characteristics of the parameter.
@ kFlagSignDisplay
Indicates that the parameter should be displayed as a signed value.
@ kFlagCannotAutomate
Indicates that the parameter is not automatable.
@ kFlagMeta
Indicates that the parameter may influence the state of other parameters.
@ kFlagStepped
Indicates that the parameter is stepped
@ kFlagsNone
No flags.
@ kFlagNegateDisplay
Indicates that the parameter should be displayed as a negative value.
void InitEnum(const char *name, int defaultValue, int nEnums, const char *label="", int flags=0, const char *group="", const char *listItems=0,...)
Initialize the parameter as an enumerated list.
int GetDisplayPrecision() const
Returns the parameter's precision.
bool GetCanAutomate() const
double ConstrainNormalized(double normalizedValue) const
Constrains a normalised input value similarly to Constrain()
bool GetMeta() const
void GetDisplay(WDL_String &display, bool withDisplayText=true) const
Get the current textual display for the current parameter value.
double GetMin() const
Returns the parameter's minimum value.
void InitDouble(const char *name, double defaultVal, double minVal, double maxVal, double step, const char *label="", int flags=0, const char *group="", const Shape &shape=ShapeLinear(), EParamUnit unit=kUnitCustom, DisplayFunc displayFunc=nullptr)
Initialize the parameter as double.
void Init(const IParam &p, const char *searchStr="", const char *replaceStr="", const char *newGroup="")
Initialize the parameter based on another parameter, replacing a CString in the name.
void SetDisplayFunc(DisplayFunc func)
Set the function to translate display values.
void GetBounds(double &lo, double &hi) const
Get the minimum and maximum real value of the parameter's range in one method call.
void Set(double value)
Sets the parameter value.
const char * GetLabel() const
Returns the parameter's label.
bool GetNegateDisplay() const
void SetDisplayText(double value, const char *str)
Set some text to display for a particular value, e.g.
std::function< void(double, WDL_String &)> DisplayFunc
DisplayFunc allows custom parameter display functions, defined by a lambda matching this signature.
EShapeIDs
IDs for the shapes.
double DBToAmp() const
Gain based on parameter's current value in dB.
double StringToValue(const char *str) const
Convert a textual representation of the parameter value to a double (real value)
EParamUnit Unit() const
Get the parameter's unit.
bool MapDisplayText(const char *str, double *pValue) const
Get the value of a particular display text.
void InitFrequency(const char *name, double defaultVal=1000., double minVal=0.1, double maxVal=10000., double step=0.1, int flags=0, const char *group="")
Initialize the parameter as frequency.
void SetNormalized(double normalizedValue)
Sets the parameter value from a normalized range (usually coming from the linked IControl)
EParamUnit
Used by AudioUnit plugins to determine the appearance of parameters, based on the kind of data they r...
void SetString(const char *str)
Set the parameter value using a textual representation.
const char * GetName() const
Returns the parameter's name.
bool GetStepped() const
EDisplayType DisplayType() const
Get the parameter's display type.
void InitSeconds(const char *name, double defaultVal=1., double minVal=0., double maxVal=10., double step=0.1, int flags=0, const char *group="")
Initialize the parameter as seconds.
double FromNormalized(double normalizedValue) const
Convert a normalized value to real value for this parameter.
bool Bool() const
Returns the parameter's value as a boolean.
void GetDisplayWithLabel(WDL_String &display, bool withDisplayText=true) const
Fills the WDL_String the value of the parameter along with the label, e.g.
void PrintDetails() const
Helper to print the parameter details to debug console in debug builds.
double GetNormalized() const
Returns the parameter's normalized value.
const char * GetDisplayTextAtIdx(int idx, double *pValue=nullptr) const
Get the display text at a particular index.
int Int() const
Returns the parameter's value as an integer.
double Constrain(double value) const
Constrains the input value between mMin and mMax and apply stepping if relevant.
void SetLabel(const char *label)
Set the parameters label after creation.
double GetRange() const
Returns the parameter's range.
const char * GetDisplayText(double value) const
Get the display text for a particular value.
const char * GetGroup() const
Returns the parameter's group.
void InitMilliseconds(const char *name, double defaultVal=1., double minVal=0., double maxVal=100., int flags=0, const char *group="")
Initialize the parameter as milliseconds.
void SetDisplayPrecision(int precision)
Set the parameters display precision.
EDisplayType
Used by AudioUnit plugins to determine the mapping of parameters.
double GetShapeValue() const
void InitPitch(const char *name, int defaultVal=60, int minVal=0, int maxVal=128, int flags=0, const char *group="", bool middleCisC4=false)
Initialize the parameter as pitch.
double Value() const
Gets a readable value of the parameter.
int NDisplayTexts() const
Get the number of display texts for the parameter.
EParamType
Defines types or parameter.
EShapeIDs GetShapeID() const
double GetMax() const
Returns the parameter's maximum value.
void InitBool(const char *name, bool defaultValue, const char *label="", int flags=0, const char *group="", const char *offText="off", const char *onText="on")
Initialize the parameter as boolean.
void SetDefault(double value)
Set the parameter's default value, and set the parameter to that default.
const char * GetCustomUnit() const
Get parameter's label (unit suffix)
void InitInt(const char *name, int defaultValue, int minVal, int maxVal, const char *label="", int flags=0, const char *group="")
Initialize the parameter as integer.
void GetJSON(WDL_String &json, int idx) const
Get a JSON description of the parameter.
BEGIN_IPLUG_NAMESPACE T Clip(T x, T lo, T hi)
Clips the value x between lo and hi.
Exponential parameter shaping.
void Init(const IParam &param) override
Initializes the shape instance.
double NormalizedToValue(double value, const IParam &param) const override
Returns the real value from a normalized input, based on an IParam's settings.
double ValueToNormalized(double value, const IParam &param) const override
Returns the normalized value from a real value, based on an IParam's settings.
IParam::EDisplayType GetDisplayType() const override
Shape * Clone() const override
Base struct for parameter shaping.
virtual double NormalizedToValue(double value, const IParam &param) const =0
Returns the real value from a normalized input, based on an IParam's settings.
virtual Shape * Clone() const =0
virtual double ValueToNormalized(double value, const IParam &param) const =0
Returns the normalized value from a real value, based on an IParam's settings.
virtual EDisplayType GetDisplayType() const =0
virtual void Init(const IParam &param)
Initializes the shape instance.
Linear parameter shaping.
double ValueToNormalized(double value, const IParam &param) const override
Returns the normalized value from a real value, based on an IParam's settings.
double NormalizedToValue(double value, const IParam &param) const override
Returns the real value from a normalized input, based on an IParam's settings.
IParam::EDisplayType GetDisplayType() const override
Shape * Clone() const override
PowCurve parameter shaping.
Shape * Clone() const override
IParam::EDisplayType GetDisplayType() const override
double ValueToNormalized(double value, const IParam &param) const override
Returns the normalized value from a real value, based on an IParam's settings.
double NormalizedToValue(double value, const IParam &param) const override
Returns the real value from a normalized input, based on an IParam's settings.