iPlug 2: IPlugParameter.h Source File

1

2

3

4

5

6

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

26

27BEGIN_IPLUG_NAMESPACE

28

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

45 {

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

76 {

78

81

85

88

94

100 };

101

104 {

109

110 double mShape;

111 };

112

115 {

121

122 double mShape;

123 };

124

127 {

128 void Init(const IParam& param) override;

133

134 double mMul = 1.0;

135 double mAdd = 1.0;

136 };

137

138#pragma mark -

139

141

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

275

280 {

281 return Clip((mFlags & kFlagStepped ? std::round(value / mStep) * mStep : value), mMin, mMax);

282 }

283

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

304 {

305 return Constrain(mShape->NormalizedToValue(normalizedValue, *this));

306 }

307

310 void Set(double value) { mValue.store(Constrain(value)); }

311

315

319

322

326

331

335

338 void SetLabel(const char* label) { strcpy(mLabel, label); }

339

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

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

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

411

416

421 const char* GetDisplayTextAtIdx(int idx, double* pValue = nullptr) const;

422

427 bool MapDisplayText(const char* str, double* pValue) const;

428

433

438

443

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

473

477

480

483

486

489

492

495

498

502 void GetJSON(WDL_String& json, int idx) const;

503

506private:

508 struct DisplayText

509 {

510 double mValue;

511 char mText[MAX_PARAM_DISPLAY_LEN];

512 };

513

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;

530

531 WDL_TypedBuf<DisplayText> mDisplayTexts;

532} WDL_FIXALIGN;

533

534END_IPLUG_NAMESPACE

Utility functions and macros.

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

@ 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()

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.

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.