JUCE-7.0.12-0-g4f43011b96 JUCE-7.0.12-0-g4f43011b96
JUCE — C++ application framework with suport for VST, VST3, LV2 audio plug-ins

« « « Anklang Documentation
Loading...
Searching...
No Matches
vstparameters.cpp
Go to the documentation of this file.
1 //-----------------------------------------------------------------------------
2// Project : VST SDK
3//
4// Category : Helpers
5// Filename : public.sdk/source/vst/vstparameters.cpp
6// Created by : Steinberg, 03/2008
7// Description : VST Parameter Implementation
8//
9//-----------------------------------------------------------------------------
10// LICENSE
11// (c) 2023, Steinberg Media Technologies GmbH, All Rights Reserved
12//-----------------------------------------------------------------------------
13// Redistribution and use in source and binary forms, with or without modification,
14// are permitted provided that the following conditions are met:
15//
16// * Redistributions of source code must retain the above copyright notice,
17// this list of conditions and the following disclaimer.
18// * Redistributions in binary form must reproduce the above copyright notice,
19// this list of conditions and the following disclaimer in the documentation
20// and/or other materials provided with the distribution.
21// * Neither the name of the Steinberg Media Technologies nor the names of its
22// contributors may be used to endorse or promote products derived from this
23// software without specific prior written permission.
24//
25// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
26// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
27// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
28// IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
29// INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
32// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
33// OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
34// OF THE POSSIBILITY OF SUCH DAMAGE.
35//-----------------------------------------------------------------------------
36
37#include "vstparameters.h"
40#include <cstdlib>
41
42namespace Steinberg {
43namespace Vst {
44
45//------------------------------------------------------------------------
46// Parameter Implementation
47//------------------------------------------------------------------------
48Parameter::Parameter ()
49{
50}
51
52//------------------------------------------------------------------------
53Parameter::Parameter (const ParameterInfo& info)
54: info (info), valueNormalized (info.defaultNormalizedValue)
55{
56}
57
58//------------------------------------------------------------------------
59Parameter::Parameter (const TChar* title, ParamID tag, const TChar* units,
60 ParamValue defaultValueNormalized, int32 stepCount, int32 flags,
61 UnitID unitID, const TChar* shortTitle)
62{
63 UString (info.title, str16BufferSize (String128)).assign (title);
64 if (units)
65 UString (info.units, str16BufferSize (String128)).assign (units);
66 if (shortTitle)
67 UString (info.shortTitle, str16BufferSize (String128)).assign (shortTitle);
68
69 info.stepCount = stepCount;
70 info.defaultNormalizedValue = valueNormalized = defaultValueNormalized;
71 info.flags = flags;
72 info.id = tag;
73 info.unitId = unitID;
74}
75
76//------------------------------------------------------------------------
77Parameter::~Parameter ()
78{
79}
80
81//------------------------------------------------------------------------
82bool Parameter::setNormalized (ParamValue normValue)
83{
84 if (normValue > 1.0)
85 {
86 normValue = 1.0;
87 }
88 else if (normValue < 0.)
89 {
90 normValue = 0.;
91 }
92
93 if (normValue != valueNormalized)
94 {
95 valueNormalized = normValue;
96 changed ();
97 return true;
98 }
99 return false;
100}
101
102//------------------------------------------------------------------------
103void Parameter::toString (ParamValue normValue, String128 string) const
104{
105 UString wrapper (string, str16BufferSize (String128));
106 if (info.stepCount == 1)
107 {
108 if (normValue > 0.5)
109 {
110 wrapper.assign (STR16 ("On"));
111 }
112 else
113 {
114 wrapper.assign (STR16 ("Off"));
115 }
116 }
117 else
118 {
119 if (!wrapper.printFloat (normValue, precision))
120 string[0] = 0;
121 }
122}
123
124//------------------------------------------------------------------------
125bool Parameter::fromString (const TChar* string, ParamValue& normValue) const
126{
127 UString wrapper (const_cast<TChar*> (string), tstrlen (string));
128 return wrapper.scanFloat (normValue);
129}
130
131//------------------------------------------------------------------------
132ParamValue Parameter::toPlain (ParamValue normValue) const
133{
134 return normValue;
135}
136
137//------------------------------------------------------------------------
138ParamValue Parameter::toNormalized (ParamValue plainValue) const
139{
140 return plainValue;
141}
142
143//------------------------------------------------------------------------
144// RangeParameter Implementation
145//------------------------------------------------------------------------
146RangeParameter::RangeParameter () : minPlain (0), maxPlain (1)
147{
148}
149
150//------------------------------------------------------------------------
151RangeParameter::RangeParameter (const ParameterInfo& paramInfo, ParamValue min, ParamValue max)
152: Parameter (paramInfo), minPlain (min), maxPlain (max)
153{
154}
155
156//------------------------------------------------------------------------
157RangeParameter::RangeParameter (const TChar* title, ParamID tag, const TChar* units,
158 ParamValue minPlain, ParamValue maxPlain,
159 ParamValue defaultValuePlain, int32 stepCount, int32 flags,
160 UnitID unitID, const TChar* shortTitle)
161: minPlain (minPlain), maxPlain (maxPlain)
162{
163 UString (info.title, str16BufferSize (String128)).assign (title);
164 if (units)
165 UString (info.units, str16BufferSize (String128)).assign (units);
166 if (shortTitle)
167 UString (info.shortTitle, str16BufferSize (String128)).assign (shortTitle);
168
169 info.stepCount = stepCount;
170 info.defaultNormalizedValue = valueNormalized = toNormalized (defaultValuePlain);
171 info.flags = flags;
172 info.id = tag;
173 info.unitId = unitID;
174}
175
176//------------------------------------------------------------------------
177void RangeParameter::toString (ParamValue _valueNormalized, String128 string) const
178{
179 if (info.stepCount > 1)
180 {
181 UString wrapper (string, str16BufferSize (String128));
182 int64 plain = static_cast<int64> (toPlain (_valueNormalized));
183 if (!wrapper.printInt (plain))
184 string[0] = 0;
185 }
186 else
187 {
188 Parameter::toString (toPlain (_valueNormalized), string);
189 }
190}
191
192//------------------------------------------------------------------------
193bool RangeParameter::fromString (const TChar* string, ParamValue& _valueNormalized) const
194{
195 UString wrapper (const_cast<TChar*> (string), tstrlen (string));
196 if (info.stepCount > 1)
197 {
198 int64 plainValue;
199 if (wrapper.scanInt (plainValue))
200 {
201 _valueNormalized = toNormalized ((ParamValue)plainValue);
202 return true;
203 }
204 return false;
205 }
206 if (wrapper.scanFloat (_valueNormalized))
207 {
208 if (_valueNormalized < getMin ())
209 _valueNormalized = getMin ();
210 else if (_valueNormalized > getMax ())
211 _valueNormalized = getMax ();
212 _valueNormalized = toNormalized (_valueNormalized);
213 return true;
214 }
215 return false;
216}
217
218//------------------------------------------------------------------------
220{
221 if (info.stepCount > 1)
222 return FromNormalized<ParamValue> (_valueNormalized, info.stepCount) + getMin ();
223 return _valueNormalized * (getMax () - getMin ()) + getMin ();
224}
225
226//------------------------------------------------------------------------
228{
229 if (info.stepCount > 1)
230 return ToNormalized <ParamValue>(plainValue - getMin (), info.stepCount);
231 return (plainValue - getMin ()) / (getMax () - getMin ());
232}
233
234//------------------------------------------------------------------------
235// StringListParameter Implementation
236//------------------------------------------------------------------------
237StringListParameter::StringListParameter (const ParameterInfo& paramInfo) : Parameter (paramInfo)
238{
239}
240
241//------------------------------------------------------------------------
242StringListParameter::StringListParameter (const TChar* title, ParamID tag, const TChar* units,
243 int32 flags, UnitID unitID, const TChar* shortTitle)
244{
245 UString (info.title, str16BufferSize (String128)).assign (title);
246 if (units)
247 UString (info.units, str16BufferSize (String128)).assign (units);
248 if (shortTitle)
249 UString (info.shortTitle, str16BufferSize (String128)).assign (shortTitle);
250
251 info.stepCount = -1;
252 info.defaultNormalizedValue = 0;
253 info.flags = flags;
254 info.id = tag;
255 info.unitId = unitID;
256}
257
258//------------------------------------------------------------------------
259StringListParameter::~StringListParameter ()
260{
261 for (auto& string : strings)
262 std::free (string);
263}
264
265//------------------------------------------------------------------------
267{
268 int32 length = strlen16 (string);
269 TChar* buffer = (TChar*)std::malloc ((length + 1) * sizeof (TChar));
270 if (!buffer)
271 return;
272
273 memcpy (buffer, string, length * sizeof (TChar));
274 buffer[length] = 0;
275 strings.push_back (buffer);
276 info.stepCount++;
277}
278
279//------------------------------------------------------------------------
280bool StringListParameter::replaceString (int32 index, const String128 string)
281{
282 TChar* str = strings.at (index);
283 if (!str)
284 return false;
285
286 int32 length = strlen16 (string);
287 TChar* buffer = (TChar*)malloc ((length + 1) * sizeof (TChar));
288 if (!buffer)
289 return false;
290
291 memcpy (buffer, string, length * sizeof (TChar));
292 buffer[length] = 0;
293 strings[index] = buffer;
294 std::free (str);
295 return true;
296}
297
298//------------------------------------------------------------------------
299void StringListParameter::toString (ParamValue _valueNormalized, String128 string) const
300{
301 int32 index = static_cast<int32> (toPlain (_valueNormalized));
302 if (const TChar* valueString = strings.at (index))
303 {
304 UString (string, str16BufferSize (String128)).assign (valueString);
305 }
306 else
307 string[0] = 0;
308}
309
310//------------------------------------------------------------------------
311bool StringListParameter::fromString (const TChar* string, ParamValue& _valueNormalized) const
312{
313 int32 index = 0;
314 for (auto it = strings.begin (), end = strings.end (); it != end; ++it, ++index)
315 {
316 if (strcmp16 (*it, string) == 0)
317 {
318 _valueNormalized = toNormalized ((ParamValue)index);
319 return true;
320 }
321 }
322 return false;
323}
324
325//------------------------------------------------------------------------
327{
328 if (info.stepCount <= 0)
329 return 0;
330 return FromNormalized<ParamValue> (_valueNormalized, info.stepCount);
331}
332
333//------------------------------------------------------------------------
335{
336 if (info.stepCount <= 0)
337 return 0;
338 return ToNormalized<ParamValue> (plainValue, info.stepCount);
339}
340
341//------------------------------------------------------------------------
342// ParameterContainer Implementation
343//------------------------------------------------------------------------
344ParameterContainer::ParameterContainer ()
345{
346}
347
348//------------------------------------------------------------------------
349ParameterContainer::~ParameterContainer ()
350{
351 if (params)
352 delete params;
353}
354
355//------------------------------------------------------------------------
356void ParameterContainer::init (int32 initialSize, int32 /*resizeDelta*/)
357{
358 if (!params)
359 {
360 params = new ParameterPtrVector;
361 if (initialSize > 0)
362 params->reserve (initialSize);
363 }
364}
365
366//------------------------------------------------------------------------
368{
369 if (!params)
370 init ();
371 id2index[p->getInfo ().id] = params->size ();
372 params->push_back (IPtr<Parameter> (p, false));
373 return p;
374}
375
376//------------------------------------------------------------------------
378{
379 if (!params)
380 init ();
381 auto* p = new Parameter (info);
382 if (addParameter (p))
383 return p;
384 p->release ();
385 return nullptr;
386}
387
388//------------------------------------------------------------------------
390{
391 if (!params || index < 0 || index >= static_cast<int32> (params->size ()))
392 return nullptr;
393 return params->at (index);
394}
395
396//------------------------------------------------------------------------
398{
399 if (params)
400 {
401 auto it = id2index.find (tag);
402 if (it != id2index.end ())
403 return params->at (it->second);
404 }
405 return nullptr;
406}
407
408//------------------------------------------------------------------------
410{
411 if (!params)
412 return false;
413
414 IndexMap::const_iterator it = id2index.find (tag);
415 if (it != id2index.end ())
416 {
417 params->erase (params->begin () + it->second);
418 id2index.erase (it);
419 }
420 return false;
421}
422
423//------------------------------------------------------------------------
425 int32 stepCount, ParamValue defaultNormalizedValue,
426 int32 flags, int32 tag, UnitID unitID, const TChar* shortTitle)
427{
428 if (!title)
429 {
430 return nullptr;
431 }
432
433 ParameterInfo info = {};
434
435 UString (info.title, str16BufferSize (String128)).assign (title);
436 if (units)
437 UString (info.units, str16BufferSize (String128)).assign (units);
438 if (shortTitle)
439 UString (info.shortTitle, str16BufferSize (String128)).assign (shortTitle);
440
441 info.stepCount = stepCount;
442 info.defaultNormalizedValue = defaultNormalizedValue;
443 info.flags = flags;
444 info.id = (tag >= 0) ? tag : getParameterCount ();
445 info.unitId = unitID;
446
447 return addParameter (info);
448}
449
450//------------------------------------------------------------------------
451} // namespace Vst
452} // namespace Steinberg
T at(T... args)
T begin(T... args)
IPtr - Smart pointer template class.
UTF-16 string class without buffer management.
Definition ustring.h:29
bool printInt(int64 value)
Print integer to string.
Definition ustring.cpp:238
bool scanInt(int64 &value) const
Scan integer from string.
Definition ustring.cpp:211
bool scanFloat(double &value) const
Scan float from string.
Definition ustring.cpp:146
UString & assign(const char16 *src, int32 srcSize=-1)
Copy from UTF-16 buffer (srcSize is in code unit (count of char16)).
Definition ustring.cpp:110
bool printFloat(double value, int32 precision=4)
Print float to string.
Definition ustring.cpp:173
int32 getParameterCount() const
Returns the count of parameters.
void init(int32 initialSize=10, int32 resizeDelta=100)
Init param array.
bool removeParameter(ParamID tag)
Remove a specific parameter by ID.
Parameter * getParameter(ParamID tag) const
Gets parameter by ID.
Parameter * addParameter(const ParameterInfo &info)
Creates and adds a new parameter from a ParameterInfo.
Parameter * getParameterByIndex(int32 index) const
Gets parameter by index.
Description of a Parameter.
virtual ParamValue toNormalized(ParamValue plainValue) const
Converts a plain value to a normalized value (e.g.
virtual void toString(ParamValue valueNormalized, String128 string) const
Converts a normalized value to a string.
virtual const ParameterInfo & getInfo() const
Returns its read only info.
ParamValue toPlain(ParamValue _valueNormalized) const SMTG_OVERRIDE
Converts a normalized value to plain value (e.g.
virtual ParamValue getMin() const
Gets the minimum plain value, same as toPlain (0).
void toString(ParamValue _valueNormalized, String128 string) const SMTG_OVERRIDE
Converts a normalized value to a string.
ParamValue toNormalized(ParamValue plainValue) const SMTG_OVERRIDE
Converts a plain value to a normalized value (e.g.
virtual ParamValue getMax() const
Gets the maximum plain value, same as toPlain (1).
bool fromString(const TChar *string, ParamValue &_valueNormalized) const SMTG_OVERRIDE
Converts a string to a normalized value.
void toString(ParamValue _valueNormalized, String128 string) const SMTG_OVERRIDE
Converts a normalized value to a string.
virtual bool replaceString(int32 index, const String128 string)
Replaces the string at index.
ParamValue toNormalized(ParamValue plainValue) const SMTG_OVERRIDE
Converts a plain value to a normalized value (e.g.
virtual void appendString(const String128 string)
Appends a string and increases the stepCount.
bool fromString(const TChar *string, ParamValue &_valueNormalized) const SMTG_OVERRIDE
Converts a string to a normalized value.
ParamValue toPlain(ParamValue _valueNormalized) const SMTG_OVERRIDE
Converts a normalized value to plain value (e.g.
T end(T... args)
T erase(T... args)
T find(T... args)
free
#define STR16(x)
string methods defines unicode / ASCII
Definition fstrdefs.h:31
T malloc(T... args)
memcpy
uint32 ParamID
parameter identifier
Definition vsttypes.h:81
TChar String128[128]
128 character UTF-16 string
Definition vsttypes.h:69
char16 TChar
UTF-16 character.
Definition vsttypes.h:68
double ParamValue
parameter value type
Definition vsttypes.h:80
int32 UnitID
unit identifier
Definition vsttypes.h:79
T push_back(T... args)
T reserve(T... args)
T size(T... args)
Controller Parameter Info.
int32 flags
ParameterFlags (see below)
ParamID id
unique identifier of this parameter (named tag too)
String128 title
parameter title (e.g. "Volume")
String128 units
parameter unit (e.g. "dB")
String128 shortTitle
parameter shortTitle (e.g. "Vol")
ParamValue defaultNormalizedValue
default normalized value [0,1] (in case of discrete value: defaultNormalizedValue = defDiscreteValue ...
int32 stepCount
number of discrete steps (0: continuous, 1: toggle, discrete value otherwise (corresponding to max - ...
UnitID unitId
id of unit this parameter belongs to (see vst3Units)