LMMS
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) 2021, 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//------------------------------------------------------------------------
49{
50 info = {};
51}
52
53//------------------------------------------------------------------------
55: info (info), valueNormalized (info.defaultNormalizedValue), precision (4)
56{
57}
58
59//------------------------------------------------------------------------
61 ParamValue defaultValueNormalized, int32 stepCount, int32 flags,
62 UnitID unitID, const TChar* shortTitle)
63: precision (4)
64{
65 info = {};
66
68 if (units)
70 if (shortTitle)
71 UString (info.shortTitle, str16BufferSize (String128)).assign (shortTitle);
72
73 info.stepCount = stepCount;
74 info.defaultNormalizedValue = valueNormalized = defaultValueNormalized;
75 info.flags = flags;
76 info.id = tag;
77 info.unitId = unitID;
78}
79
80//------------------------------------------------------------------------
84
85//------------------------------------------------------------------------
87{
88 if (normValue > 1.0)
89 {
90 normValue = 1.0;
91 }
92 else if (normValue < 0.)
93 {
94 normValue = 0.;
95 }
96
97 if (normValue != valueNormalized)
98 {
99 valueNormalized = normValue;
100 changed ();
101 return true;
102 }
103 return false;
104}
105
106//------------------------------------------------------------------------
107void Parameter::toString (ParamValue normValue, String128 string) const
108{
109 UString wrapper (string, str16BufferSize (String128));
110 if (info.stepCount == 1)
111 {
112 if (normValue > 0.5)
113 {
114 wrapper.assign (STR16 ("On"));
115 }
116 else
117 {
118 wrapper.assign (STR16 ("Off"));
119 }
120 }
121 else
122 {
123 if (!wrapper.printFloat (normValue, precision))
124 string[0] = 0;
125 }
126}
127
128//------------------------------------------------------------------------
129bool Parameter::fromString (const TChar* string, ParamValue& normValue) const
130{
131 UString wrapper (const_cast<TChar*> (string), tstrlen (string));
132 return wrapper.scanFloat (normValue);
133}
134
135//------------------------------------------------------------------------
137{
138 return normValue;
139}
140
141//------------------------------------------------------------------------
143{
144 return plainValue;
145}
146
147//------------------------------------------------------------------------
148// RangeParameter Implementation
149//------------------------------------------------------------------------
153
154//------------------------------------------------------------------------
159
160//------------------------------------------------------------------------
163 ParamValue defaultValuePlain, int32 stepCount, int32 flags,
164 UnitID unitID, const TChar* shortTitle)
166{
168 if (units)
170 if (shortTitle)
171 UString (info.shortTitle, str16BufferSize (String128)).assign (shortTitle);
172
173 info.stepCount = stepCount;
174 info.defaultNormalizedValue = valueNormalized = toNormalized (defaultValuePlain);
175 info.flags = flags;
176 info.id = tag;
177 info.unitId = unitID;
178}
179
180//------------------------------------------------------------------------
181void RangeParameter::toString (ParamValue _valueNormalized, String128 string) const
182{
183 if (info.stepCount > 1)
184 {
185 UString wrapper (string, str16BufferSize (String128));
186 int64 plain = static_cast<int64> (toPlain (_valueNormalized));
187 if (!wrapper.printInt (plain))
188 string[0] = 0;
189 }
190 else
191 {
192 Parameter::toString (toPlain (_valueNormalized), string);
193 }
194}
195
196//------------------------------------------------------------------------
197bool RangeParameter::fromString (const TChar* string, ParamValue& _valueNormalized) const
198{
199 UString wrapper (const_cast<TChar*> (string), tstrlen (string));
200 if (info.stepCount > 1)
201 {
202 int64 plainValue;
203 if (wrapper.scanInt (plainValue))
204 {
205 _valueNormalized = toNormalized ((ParamValue)plainValue);
206 return true;
207 }
208 return false;
209 }
210 if (wrapper.scanFloat (_valueNormalized))
211 {
212 if (_valueNormalized < getMin ())
213 _valueNormalized = getMin ();
214 else if (_valueNormalized > getMax ())
215 _valueNormalized = getMax ();
216 _valueNormalized = toNormalized (_valueNormalized);
217 return true;
218 }
219 return false;
220}
221
222//------------------------------------------------------------------------
224{
225 if (info.stepCount > 1)
226 return FromNormalized<ParamValue> (_valueNormalized, info.stepCount) + getMin ();
227 return _valueNormalized * (getMax () - getMin ()) + getMin ();
228}
229
230//------------------------------------------------------------------------
232{
233 if (info.stepCount > 1)
234 return ToNormalized <ParamValue>(plainValue - getMin (), info.stepCount);
235 return (plainValue - getMin ()) / (getMax () - getMin ());
236}
237
238//------------------------------------------------------------------------
239// StringListParameter Implementation
240//------------------------------------------------------------------------
242{
243}
244
245//------------------------------------------------------------------------
247 int32 flags, UnitID unitID, const TChar* shortTitle)
248{
250 if (units)
252 if (shortTitle)
253 UString (info.shortTitle, str16BufferSize (String128)).assign (shortTitle);
254
255 info.stepCount = -1;
256 info.defaultNormalizedValue = 0;
257 info.flags = flags;
258 info.id = tag;
259 info.unitId = unitID;
260}
261
262//------------------------------------------------------------------------
264{
265 for (auto& string : strings)
266 std::free (string);
267}
268
269//------------------------------------------------------------------------
271{
272 int32 length = strlen16 (string);
273 TChar* buffer = (TChar*)std::malloc ((length + 1) * sizeof (TChar));
274 if (!buffer)
275 return;
276
277 memcpy (buffer, string, length * sizeof (TChar));
278 buffer[length] = 0;
279 strings.push_back (buffer);
280 info.stepCount++;
281}
282
283//------------------------------------------------------------------------
285{
286 TChar* str = strings.at (index);
287 if (!str)
288 return false;
289
290 int32 length = strlen16 (string);
291 TChar* buffer = (TChar*)malloc ((length + 1) * sizeof (TChar));
292 if (!buffer)
293 return false;
294
295 memcpy (buffer, string, length * sizeof (TChar));
296 buffer[length] = 0;
297 strings[index] = buffer;
298 std::free (str);
299 return true;
300}
301
302//------------------------------------------------------------------------
303void StringListParameter::toString (ParamValue _valueNormalized, String128 string) const
304{
305 int32 index = (int32)toPlain (_valueNormalized);
306 if (const TChar* valueString = strings.at (index))
307 {
308 UString (string, str16BufferSize (String128)).assign (valueString);
309 }
310 else
311 string[0] = 0;
312}
313
314//------------------------------------------------------------------------
315bool StringListParameter::fromString (const TChar* string, ParamValue& _valueNormalized) const
316{
317 int32 index = 0;
318 for (auto it = strings.begin (), end = strings.end (); it != end; ++it, ++index)
319 {
320 if (strcmp16 (*it, string) == 0)
321 {
322 _valueNormalized = toNormalized ((ParamValue)index);
323 return true;
324 }
325 }
326 return false;
327}
328
329//------------------------------------------------------------------------
331{
332 if (info.stepCount <= 0)
333 return 0;
334 return FromNormalized<ParamValue> (_valueNormalized, info.stepCount);
335}
336
337//------------------------------------------------------------------------
339{
340 if (info.stepCount <= 0)
341 return 0;
342 return ToNormalized<ParamValue> (plainValue, info.stepCount);
343}
344
345//------------------------------------------------------------------------
346// ParameterContainer Implementation
347//------------------------------------------------------------------------
351
352//------------------------------------------------------------------------
354{
355 if (params)
356 delete params;
357}
358
359//------------------------------------------------------------------------
360void ParameterContainer::init (int32 initialSize, int32 /*resizeDelta*/)
361{
362 if (!params)
363 {
365 if (initialSize > 0)
366 params->reserve (initialSize);
367 }
368}
369
370//------------------------------------------------------------------------
372{
373 if (!params)
374 init ();
375 id2index[p->getInfo ().id] = params->size ();
376 params->push_back (IPtr<Parameter> (p, false));
377 return p;
378}
379
380//------------------------------------------------------------------------
382{
383 if (!params)
384 init ();
385 auto* p = new Parameter (info);
386 if (addParameter (p))
387 return p;
388 p->release ();
389 return nullptr;
390}
391
392//------------------------------------------------------------------------
394{
395 if (params)
396 {
397 auto it = id2index.find (tag);
398 if (it != id2index.end ())
399 return params->at (it->second);
400 }
401 return nullptr;
402}
403
404//------------------------------------------------------------------------
406{
407 if (!params)
408 return false;
409
410 IndexMap::const_iterator it = id2index.find (tag);
411 if (it != id2index.end ())
412 {
413 params->erase (params->begin () + it->second);
414 id2index.erase (it);
415 }
416 return false;
417}
418
419//------------------------------------------------------------------------
421 int32 stepCount, ParamValue defaultNormalizedValue,
422 int32 flags, int32 tag, UnitID unitID, const TChar* shortTitle)
423{
424 if (!title)
425 {
426 return nullptr;
427 }
428
429 ParameterInfo info = {};
430
432 if (units)
434 if (shortTitle)
435 UString (info.shortTitle, str16BufferSize (String128)).assign (shortTitle);
436
437 info.stepCount = stepCount;
438 info.defaultNormalizedValue = defaultNormalizedValue;
439 info.flags = flags;
440 info.id = (tag >= 0) ? tag : getParameterCount ();
441 info.unitId = unitID;
442
443 return addParameter (info);
444}
445
446//------------------------------------------------------------------------
447} // namespace Vst
448} // namespace Steinberg
#define nullptr
Definition DistrhoDefines.h:75
virtual void changed(int32 msg=kChanged)
Inform all dependents, that the object has changed.
Definition fobject.cpp:106
Definition smartpointer.h:44
Definition ustring.h:29
bool printInt(int64 value)
Definition ustring.cpp:238
bool scanInt(int64 &value) const
Definition ustring.cpp:211
bool scanFloat(double &value) const
Definition ustring.cpp:146
UString & assign(const char16 *src, int32 srcSize=-1)
Definition ustring.cpp:110
bool printFloat(double value, int32 precision=4)
Definition ustring.cpp:173
std::vector< IPtr< Parameter > > ParameterPtrVector
Definition vstparameters.h:232
int32 getParameterCount() const
Definition vstparameters.h:212
void init(int32 initialSize=10, int32 resizeDelta=100)
Definition vstparameters.cpp:360
bool removeParameter(ParamID tag)
Definition vstparameters.cpp:405
Parameter * getParameter(ParamID tag) const
Definition vstparameters.cpp:393
~ParameterContainer()
Definition vstparameters.cpp:353
Parameter * addParameter(const ParameterInfo &info)
Definition vstparameters.cpp:381
IndexMap id2index
Definition vstparameters.h:235
ParameterPtrVector * params
Definition vstparameters.h:234
ParameterContainer()
Definition vstparameters.cpp:348
Definition vstparameters.h:55
virtual ParamValue toNormalized(ParamValue plainValue) const
Definition vstparameters.cpp:142
virtual bool setNormalized(ParamValue v)
Definition vstparameters.cpp:86
virtual ParamValue toPlain(ParamValue valueNormalized) const
Definition vstparameters.cpp:136
virtual bool fromString(const TChar *string, ParamValue &valueNormalized) const
Definition vstparameters.cpp:129
virtual void toString(ParamValue valueNormalized, String128 string) const
Definition vstparameters.cpp:107
ParamValue valueNormalized
Definition vstparameters.h:102
ParameterInfo info
Definition vstparameters.h:101
~Parameter() override
Definition vstparameters.cpp:81
int32 precision
Definition vstparameters.h:103
Parameter()
Definition vstparameters.cpp:48
ParamValue toPlain(ParamValue _valueNormalized) const SMTG_OVERRIDE
Definition vstparameters.cpp:223
virtual ParamValue getMin() const
Definition vstparameters.h:122
RangeParameter()
Definition vstparameters.cpp:150
ParamValue maxPlain
Definition vstparameters.h:146
void toString(ParamValue _valueNormalized, String128 string) const SMTG_OVERRIDE
Definition vstparameters.cpp:181
ParamValue toNormalized(ParamValue plainValue) const SMTG_OVERRIDE
Definition vstparameters.cpp:231
virtual ParamValue getMax() const
Definition vstparameters.h:126
bool fromString(const TChar *string, ParamValue &_valueNormalized) const SMTG_OVERRIDE
Definition vstparameters.cpp:197
ParamValue minPlain
Definition vstparameters.h:145
void toString(ParamValue _valueNormalized, String128 string) const SMTG_OVERRIDE
Definition vstparameters.cpp:303
virtual bool replaceString(int32 index, const String128 string)
Definition vstparameters.cpp:284
ParamValue toNormalized(ParamValue plainValue) const SMTG_OVERRIDE
Definition vstparameters.cpp:338
StringListParameter(const ParameterInfo &paramInfo)
Definition vstparameters.cpp:241
~StringListParameter() override
Definition vstparameters.cpp:263
virtual void appendString(const String128 string)
Definition vstparameters.cpp:270
bool fromString(const TChar *string, ParamValue &_valueNormalized) const SMTG_OVERRIDE
Definition vstparameters.cpp:315
ParamValue toPlain(ParamValue _valueNormalized) const SMTG_OVERRIDE
Definition vstparameters.cpp:330
StringVector strings
Definition vstparameters.h:182
#define STR16(x)
Definition fstrdefs.h:35
#define str16BufferSize(buffer)
Definition fstrdefs.h:48
static const char * title
Definition pugl.h:1747
struct backing_store_struct * info
Definition jmemsys.h:183
Definition ivstattributes.h:28
uint32 ParamID
parameter identifier
Definition vsttypes.h:75
TChar String128[128]
128 character UTF-16 string
Definition vsttypes.h:63
char16 TChar
UTF-16 character.
Definition vsttypes.h:62
double ParamValue
parameter value type
Definition vsttypes.h:74
int32 UnitID
unit identifier
Definition vsttypes.h:73
Definition baseiids.cpp:43
int32 FromNormalized(const T &norm, const int32 numSteps)
Definition futils.h:86
int int32
Definition ftypes.h:50
T ToNormalized(const T &value, const int32 numSteps)
Definition futils.h:80
long long int64
Definition ftypes.h:66
int32 strcmp16(const char16 *src, const char16 *dst)
Definition fstrdefs.h:150
int32 strlen16(const char16 *str)
Definition fstrdefs.h:126
int32 tstrlen(const tchar *str)
Definition fstrdefs.h:124
#define min(x, y)
Definition os.h:74
#define max(x, y)
Definition os.h:78
png_uint_32 length
Definition png.c:2247
static void units(std::ostream &o, const char *u)
Definition ports.cpp:1772
Definition ivsteditcontroller.h:47
uch * p
Definition crypt.c:594
memcpy(hh, h, RAND_HEAD_LEN)
ZCONST uch * init
Definition extract.c:2392
char * malloc()