Jamba C++ API 7.5.0
Loading...
Searching...
No Matches
ParamDef.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2018-2023 pongasoft
3 *
4 * Licensed under the Apache License, Version 2.0 or the MIT license,
5 * at your option. You may not use this file except in compliance with
6 * one of these licenses. You may obtain copies of the licenses at:
7 *
8 * https://www.apache.org/licenses/LICENSE-2.0
9 * https://opensource.org/licenses/MIT
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14 * License for the specific language governing permissions and limitations under
15 * the License.
16 *
17 * @author Yan Pujante
18 */
19#pragma once
20
21#include "ParamConverters.h"
22#include "ParamSerializers.h"
23#include "Messaging.h"
24#include "NormalizedState.h"
25
26#include <base/source/fstreamer.h>
27#include <pluginterfaces/vst/vsttypes.h>
28#include <pluginterfaces/vst/ivsteditcontroller.h>
29#include <pluginterfaces/vst/ivstunits.h>
30
31#include <string>
32#include <memory>
33
34namespace pongasoft::VST {
35
36using namespace Steinberg;
37using namespace Steinberg::Vst;
38
39// forward declaration required for API
40namespace GUI::Params {
42}
43
47class IParamDef : public std::enable_shared_from_this<IParamDef>
48{
49public:
52 enum class Owner
53 {
56 };
57
59 static constexpr int16 kVersionNotDeprecated = -1;
60
61public:
62 IParamDef(ParamID const iParamID,
63 VstString16 iTitle,
64 Owner const iOwner,
65 bool const iTransient,
66 int16 const iDeprecatedSince) :
67 fParamID{iParamID},
68 fTitle{std::move(iTitle)},
69 fOwner{iOwner},
70 fTransient{iTransient},
71 fDeprecatedSince{iDeprecatedSince}
72 {}
73
74 virtual ~IParamDef() = default;
75
78
79public:
80 const ParamID fParamID;
82 const Owner fOwner; // who owns the parameter (and which stream will it be saved if non transient)
83 const bool fTransient; // not saved in the stream
84 const int16 fDeprecatedSince; // at which version the parameter got deprecated
85};
86
87
92{
93public:
94 RawVstParamDef(ParamID const iParamID,
95 VstString16 iTitle,
96 VstString16 iUnits,
97 ParamValue const iDefaultNormalizedValue,
98 int32 const iStepCount,
99 int32 const iFlags,
100 UnitID const iUnitID,
101 VstString16 iShortTitle,
102 int32 const iPrecision,
103 Owner const iOwner,
104 bool const iTransient,
105 int16 const iDeprecatedSince) :
106 IParamDef(iParamID, std::move(iTitle), iOwner, iTransient, iDeprecatedSince),
107 fUnits{std::move(iUnits)},
108 fDefaultValue{Utils::clampE(iDefaultNormalizedValue, 0.0, 1.0)},
109 fStepCount{iStepCount},
110 fFlags{iFlags},
111 fUnitID{iUnitID},
112 fShortTitle{std::move(iShortTitle)},
113 fPrecision{iPrecision}
114 {}
115
116public:
117 // readFromStream
118 ParamValue readFromStream(IBStreamer &iStreamer) const
119 {
120 ParamValue res = fDefaultValue;
121
122 ParamValue value;
123 if(IBStreamHelper::readDouble(iStreamer, value) == kResultOk)
124 res = value;
125
126 return res;
127 }
128
130 ParamValue readFromState(NormalizedState const &iState) const
131 {
132 ParamValue res = fDefaultValue;
133 iState.getNormalizedValue(fParamID, res);
134 return res;
135 }
136
138 tresult writeToState(ParamValue iValue, NormalizedState &oState) const
139 {
140 return oState.setNormalizedValue(fParamID, iValue);
141 }
142
143 // toString
144 virtual void toString(ParamValue iNormalizedValue, String128 iString) const
145 {
146 RawParamConverter::staticToString(iNormalizedValue, iString, fPrecision);
147 }
148
155 virtual std::string toUTF8String(ParamValue iNormalizedValue, int32 iPrecision) const
156 {
157 String128 s;
158 s[0] = 0;
159 RawParamConverter::staticToString(iNormalizedValue, s, iPrecision >= 0 ? iPrecision : fPrecision);
160 return VstUtils::toUT8String(s);
161 }
162
163public:
165 const ParamValue fDefaultValue;
166 const int32 fStepCount;
167 const int32 fFlags;
168 const UnitID fUnitID;
170 const int32 fPrecision;
171};
172
176template<typename T>
178{
179public:
180 using ParamType = T;
181
182 VstParamDef(ParamID const iParamID,
183 VstString16 iTitle,
184 VstString16 iUnits,
185 ParamType const iDefaultValue,
186 int32 const iFlags,
187 UnitID const iUnitID,
188 VstString16 iShortTitle,
189 int32 const iPrecision,
190 Owner const iOwner,
191 bool const iTransient,
192 int16 const iDeprecatedSince,
193 std::shared_ptr<IParamConverter<ParamType>> iConverter) :
194 RawVstParamDef(iParamID,
195 std::move(iTitle),
196 std::move(iUnits),
197 iConverter ? iConverter->normalize(iDefaultValue) : 0,
198 iConverter ? iConverter->getStepCount() : 0,
199 iFlags,
200 iUnitID,
201 std::move(iShortTitle),
202 iPrecision,
203 iOwner,
204 iTransient,
205 iDeprecatedSince),
206 fDefaultValue{iDefaultValue},
207 fConverter{std::move(iConverter)}
208 {
209 }
210
213 {
215 }
216
218 tresult writeToState(ParamType const &iValue, NormalizedState &oState) const
219 {
220 return RawVstParamDef::writeToState(normalize(iValue), oState);
221 }
222
223 // getDefaultValue
225
226 // shortcut to normalize
227 inline ParamValue normalize(ParamType const &iValue) const
228 {
229 if(fConverter)
230 return fConverter->normalize(iValue);
231 return 0;
232 }
233
234 // shortcut to denormalize
235 inline ParamType denormalize(ParamValue iNormalizedValue) const
236 {
237 if(fConverter)
238 return fConverter->denormalize(iNormalizedValue);
239 return fDefaultValue;
240 }
241
245 void toString(ParamValue iNormalizedValue, String128 iString) const override
246 {
247 if(fConverter)
248 fConverter->toString(fConverter->denormalize(iNormalizedValue), iString, fPrecision);
249 else
250 RawVstParamDef::toString(iNormalizedValue, iString);
251 }
252
259 std::string toUTF8String(ParamValue iNormalizedValue, int32 iPrecision) const override
260 {
261 if(fConverter)
262 {
263 String128 s;
264 s[0] = 0;
265 fConverter->toString(fConverter->denormalize(iNormalizedValue), s, iPrecision >= 0 ? iPrecision : fPrecision);
266 return VstUtils::toUT8String(s);
267 }
268 else
269 return RawVstParamDef::toUTF8String(iNormalizedValue, iPrecision);
270 }
271
272public:
274 const std::shared_ptr<IParamConverter<ParamType>> fConverter;
275};
276
281{
282public:
283 IJmbParamDef(const ParamID iParamID,
284 VstString16 iTitle,
285 Owner const iOwner,
286 bool const iTransient,
287 int16 const iDeprecatedSince,
288 bool const iShared)
289 : IParamDef(iParamID, std::move(iTitle), iOwner, iTransient, iDeprecatedSince),
290 fShared{iShared}
291 {}
292
293 ~IJmbParamDef() override = default;
294
295 // writeDefaultValue
296 virtual void writeDefaultValue(std::ostream &oStreamer) const = 0;
297
301 virtual std::shared_ptr<GUI::Params::IGUIJmbParameter> newGUIParam() = 0;
302
306 virtual bool isSerializable() const = 0;
307
308public:
309 bool const fShared;
310};
311
316template<typename T>
318{
319public:
320 using ParamType = T;
321
322 JmbParamDef(ParamID const iParamID,
323 VstString16 iTitle,
324 Owner const iOwner,
325 bool const iTransient,
326 int16 const iDeprecatedSince,
327 bool const iShared,
328 ParamType const &iDefaultValue,
329 std::shared_ptr<IParamSerializer<ParamType>> iSerializer) :
330 IJmbParamDef(iParamID, std::move(iTitle), iOwner, iTransient, iDeprecatedSince, iShared),
331 fDefaultValue{iDefaultValue},
332 fSerializer{std::move(iSerializer)}
333 {}
334
335 // readFromStream
336 tresult readFromStream(IBStreamer &iStreamer, ParamType &oValue) const override;
337 ParamType readFromStream(IBStreamer &iStreamer) const;
338
339 // writeToStream
340 tresult writeToStream(ParamType const &iValue, IBStreamer &oStreamer) const override;
341
342 // writeToStream
343 void writeToStream(ParamType const &iValue, std::ostream &oStreamer) const override;
344
345 // writeDefaultValue
346 void writeDefaultValue(std::ostream &oStreamer) const override;
347
348 // readFromMessage
349 tresult readFromMessage(Message const &iMessage, ParamType &oValue) const;
350
351 // writeToMessage
352 tresult writeToMessage(ParamType const &iValue, Message &oMessage) const;
353
360 std::string toUTF8String(ParamType const &iValue, int32 iPrecision) const
361 {
362 if(fSerializer)
363 return fSerializer->toString(iValue, iPrecision);
364 else
365 {
367 {
368 std::ostringstream s;
369 if(iPrecision >= 0)
370 {
371 s.precision(iPrecision);
372 s.setf(std::ios::fixed);
373 }
374 s << iValue;
375 return s.str();
376 }
377 else
378 return "";
379 }
380 }
381
385 std::shared_ptr<IDiscreteConverter<T>> getDiscreteConverter() const
386 {
387 // Implementation note: at this moment, only checks if the serializer also implements the API.
388 // But possible to add an additional separate field to set it explicitly if there is a need
389 return std::dynamic_pointer_cast<IDiscreteConverter<T>>(fSerializer);
390 }
391
392 // computeMessageAttrID
393 std::string computeMessageAttrID() const
394 {
395 return "__param__" + std::to_string(fParamID);
396 }
397
404 std::shared_ptr<GUI::Params::IGUIJmbParameter> newGUIParam() override;
405
409 bool isSerializable() const override { return fSerializer != nullptr; }
410
411public:
413 const std::shared_ptr<IParamSerializer<ParamType>> fSerializer;
414};
415
416//------------------------------------------------------------------------
417// JmbParamDef::readFromStream
418//------------------------------------------------------------------------
419template<typename T>
420tresult JmbParamDef<T>::readFromStream(IBStreamer &iStreamer, T &oValue) const
421{
422 if(fSerializer)
423 {
424 return fSerializer->readFromStream(iStreamer, oValue);
425 }
426 else
427 return kResultFalse;
428}
429
430//------------------------------------------------------------------------
431// JmbParamDef::readFromStream
432//------------------------------------------------------------------------
433template<typename T>
434T JmbParamDef<T>::readFromStream(IBStreamer &iStreamer) const
435{
436 T value;
437 if(readFromStream(iStreamer, value) != kResultOk)
438 value = fDefaultValue;
439 return value;
440}
441
442
443//------------------------------------------------------------------------
444// JmbParamDef::writeToStream
445//------------------------------------------------------------------------
446template<typename T>
447tresult JmbParamDef<T>::writeToStream(const T &iValue, IBStreamer &oStreamer) const
448{
449 if(fSerializer)
450 return fSerializer->writeToStream(iValue, oStreamer);
451 else
452 return kResultFalse;
453}
454
455//------------------------------------------------------------------------
456// JmbParamDef::writeToStream
457//------------------------------------------------------------------------
458template<typename T>
459void JmbParamDef<T>::writeToStream(const ParamType &iValue, std::ostream &oStream) const
460{
461 if(fSerializer)
462 fSerializer->writeToStream(iValue, oStream);
463 else
464 {
466 {
467 oStream << iValue;
468 }
469 }
470}
471
472//------------------------------------------------------------------------
473// JmbParamDef::writeDefaultValue
474//------------------------------------------------------------------------
475template<typename T>
476void JmbParamDef<T>::writeDefaultValue(std::ostream &oStreamer) const
477{
478 writeToStream(fDefaultValue, oStreamer);
479}
480
481//------------------------------------------------------------------------
482// JmbParamDef::readFromMessage
483//------------------------------------------------------------------------
484template<typename T>
485tresult JmbParamDef<T>::readFromMessage(Message const &iMessage, ParamType &oValue) const
486{
487 if(fSerializer)
488 return iMessage.getSerializableValue(computeMessageAttrID().c_str(), *this, oValue);
489 else
490 return kResultFalse;
491}
492
493//------------------------------------------------------------------------
494// JmbParamDef::writeToMessage
495//------------------------------------------------------------------------
496template<typename T>
497tresult JmbParamDef<T>::writeToMessage(const ParamType &iValue, Message &oMessage) const
498{
499 if(fSerializer)
500 return oMessage.setSerializableValue(computeMessageAttrID().c_str(), *this, iValue);
501 else
502 return kResultFalse;
503}
504
505//------------------------------------------------------------------------
506// VstParam - define shortcut notation
507//------------------------------------------------------------------------
508template<typename T>
509using VstParam = std::shared_ptr<VstParamDef<T>>;
510using RawVstParam = std::shared_ptr<RawVstParamDef>;
511
512template<typename T, size_t N>
513using VstParams = std::array<VstParam<T>, N>;
514
515template<size_t N>
516using RawVstParams = std::array<RawVstParam, N>;
517
518//------------------------------------------------------------------------
519// JmbParam - define shortcut notation
520//------------------------------------------------------------------------
521template<typename T>
522using JmbParam = std::shared_ptr<JmbParamDef<T>>;
523
524template<typename T, size_t N>
525using JmbParams = std::array<JmbParam<T>, N>;
526
527}
Base class for a Jamba (Jmb) GUI parameter.
Definition GUIJmbParameter.h:42
virtual void writeDefaultValue(std::ostream &oStreamer) const =0
~IJmbParamDef() override=default
bool const fShared
Definition ParamDef.h:309
virtual bool isSerializable() const =0
IJmbParamDef(const ParamID iParamID, VstString16 iTitle, Owner const iOwner, bool const iTransient, int16 const iDeprecatedSince, bool const iShared)
Definition ParamDef.h:283
virtual std::shared_ptr< GUI::Params::IGUIJmbParameter > newGUIParam()=0
Create a new IGUIJmbParameter of the proper subtype.
A vst parameter is represented by a ParamValue type which is a double in the range [0,...
Definition ParamConverters.h:55
IParamDef(ParamID const iParamID, VstString16 iTitle, Owner const iOwner, bool const iTransient, int16 const iDeprecatedSince)
Definition ParamDef.h:62
bool isDeprecated() const
Returns true if the parameter is deprecated (meaning it can only be used to upgrade to latest version...
Definition ParamDef.h:77
const bool fTransient
Definition ParamDef.h:83
const int16 fDeprecatedSince
Definition ParamDef.h:84
Owner
Who owns the parameter (mostly for state saving purposes).
Definition ParamDef.h:53
@ kRT
Definition ParamDef.h:54
@ kGUI
Definition ParamDef.h:55
static constexpr int16 kVersionNotDeprecated
Special version indicating the parameter is not deprecated.
Definition ParamDef.h:59
const VstString16 fTitle
Definition ParamDef.h:81
const ParamID fParamID
Definition ParamDef.h:80
virtual ~IParamDef()=default
const Owner fOwner
Definition ParamDef.h:82
A vst parameter is represented by a ParamValue type which is a double in the range [0,...
Definition ParamSerializers.h:109
tresult writeToMessage(ParamType const &iValue, Message &oMessage) const
Definition ParamDef.h:497
bool isSerializable() const override
Definition ParamDef.h:409
tresult readFromMessage(Message const &iMessage, ParamType &oValue) const
Definition ParamDef.h:485
std::shared_ptr< IDiscreteConverter< T > > getDiscreteConverter() const
Definition ParamDef.h:385
std::string toUTF8String(ParamType const &iValue, int32 iPrecision) const
Return the value as a utf-8 string.
Definition ParamDef.h:360
std::string computeMessageAttrID() const
Definition ParamDef.h:393
const std::shared_ptr< IParamSerializer< ParamType > > fSerializer
Definition ParamDef.h:413
void writeDefaultValue(std::ostream &oStreamer) const override
Definition ParamDef.h:476
const ParamType fDefaultValue
Definition ParamDef.h:412
T ParamType
Definition ParamDef.h:320
JmbParamDef(ParamID const iParamID, VstString16 iTitle, Owner const iOwner, bool const iTransient, int16 const iDeprecatedSince, bool const iShared, ParamType const &iDefaultValue, std::shared_ptr< IParamSerializer< ParamType > > iSerializer)
Definition ParamDef.h:322
tresult writeToStream(ParamType const &iValue, IBStreamer &oStreamer) const override
This method should write iValue to the stream (aka serialization).
Definition ParamDef.h:447
tresult readFromStream(IBStreamer &iStreamer, ParamType &oValue) const override
This method should read from the stream and populate oValue accordingly (aka deserialization).
Definition ParamDef.h:420
std::shared_ptr< GUI::Params::IGUIJmbParameter > newGUIParam() override
Create a new IGUIJmbParameter of the proper subtype.
Definition GUIState.h:393
Simple wrapper class with better api.
Definition Messaging.h:46
tresult setSerializableValue(IAttributeList::AttrID id, IParamSerializer< T > const &iSerializer, T const &iValue)
Serializes the parameter value as an entry in the message.
Definition Messaging.h:152
tresult getSerializableValue(IAttributeList::AttrID id, IParamSerializer< T > const &iSerializer, T &oValue) const
Deserializes the parameter value from an entry in the message.
Definition Messaging.h:170
Used to communicate the state between the UI and the RT and read/write to stream.
Definition NormalizedState.h:39
tresult getNormalizedValue(ParamID iParamID, ParamValue &oValue) const
Returns the normalized value for the given param id if it exists.
Definition NormalizedState.cpp:132
tresult setNormalizedValue(ParamID iParamID, ParamValue iValue)
Sets the normalized value for the given param id if it exists.
Definition NormalizedState.cpp:147
static void staticToString(ParamValue const &iValue, String128 oString, int32 iPrecision)
Definition ParamConverters.h:95
tresult writeToState(ParamValue iValue, NormalizedState &oState) const
Writes the provided value to the (normalized) state.
Definition ParamDef.h:138
RawVstParamDef(ParamID const iParamID, VstString16 iTitle, VstString16 iUnits, ParamValue const iDefaultNormalizedValue, int32 const iStepCount, int32 const iFlags, UnitID const iUnitID, VstString16 iShortTitle, int32 const iPrecision, Owner const iOwner, bool const iTransient, int16 const iDeprecatedSince)
Definition ParamDef.h:94
virtual void toString(ParamValue iNormalizedValue, String128 iString) const
Definition ParamDef.h:144
const int32 fPrecision
Definition ParamDef.h:170
const int32 fStepCount
Definition ParamDef.h:166
const VstString16 fShortTitle
Definition ParamDef.h:169
const ParamValue fDefaultValue
Definition ParamDef.h:165
const UnitID fUnitID
Definition ParamDef.h:168
ParamValue readFromStream(IBStreamer &iStreamer) const
Definition ParamDef.h:118
virtual std::string toUTF8String(ParamValue iNormalizedValue, int32 iPrecision) const
Return the value as a utf-8 string.
Definition ParamDef.h:155
const int32 fFlags
Definition ParamDef.h:167
ParamValue readFromState(NormalizedState const &iState) const
Read the value from the (normalized) state.
Definition ParamDef.h:130
const VstString16 fUnits
Definition ParamDef.h:164
ParamType denormalize(ParamValue iNormalizedValue) const
Definition ParamDef.h:235
ParamValue normalize(ParamType const &iValue) const
Definition ParamDef.h:227
const std::shared_ptr< IParamConverter< ParamType > > fConverter
Definition ParamDef.h:274
VstParamDef(ParamID const iParamID, VstString16 iTitle, VstString16 iUnits, ParamType const iDefaultValue, int32 const iFlags, UnitID const iUnitID, VstString16 iShortTitle, int32 const iPrecision, Owner const iOwner, bool const iTransient, int16 const iDeprecatedSince, std::shared_ptr< IParamConverter< ParamType > > iConverter)
Definition ParamDef.h:182
const ParamType fDefaultValue
Definition ParamDef.h:273
std::string toUTF8String(ParamValue iNormalizedValue, int32 iPrecision) const override
Return the value as a utf-8 string.
Definition ParamDef.h:259
ParamType readFromState(NormalizedState const &iState) const
Read the value from the (normalized) state.
Definition ParamDef.h:212
ParamType getDefaultValue() const
Definition ParamDef.h:224
T ParamType
Definition ParamDef.h:180
tresult writeToState(ParamType const &iValue, NormalizedState &oState) const
Writes the provided value to the (normalized) state.
Definition ParamDef.h:218
void toString(ParamValue iNormalizedValue, String128 iString) const override
Using fConverter::toString.
Definition ParamDef.h:245
Definition CircularBuffer.h:26
constexpr auto is_operator_write_to_ostream_defined
Allows to detect whether a type defines ostream << x at compile time.
Definition Metaprogramming.h:115
Definition GUIState.h:38
tresult readDouble(IBStreamer &iStreamer, double &oValue)
Definition ParamSerializers.h:157
std::string toUT8String(VstString16 const &iString)
Converts a VstString16 to a regular std::string that is properly utf-8 encoded.
Definition Utils.h:35
Definition Clock.h:24
std::array< VstParam< T >, N > VstParams
Definition ParamDef.h:513
std::array< JmbParam< T >, N > JmbParams
Definition ParamDef.h:525
std::array< RawVstParam, N > RawVstParams
Definition ParamDef.h:516
std::shared_ptr< JmbParamDef< T > > JmbParam
Definition ParamDef.h:522
std::shared_ptr< VstParamDef< T > > VstParam
Definition ParamDef.h:509
std::shared_ptr< RawVstParamDef > RawVstParam
Definition ParamDef.h:510
std::basic_string< Steinberg::char16 > VstString16
Strings made of char16 characters are represented by the native C++11 type std::basic_string<Steinber...
Definition Types.h:44