Jamba  3.0.2
ParamDef.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018 pongasoft
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  *
16  * @author Yan Pujante
17  */
18 #ifndef __PONGASOFT_VST_PARAM_DEF_H__
19 #define __PONGASOFT_VST_PARAM_DEF_H__
20 
21 #include "ParamConverters.h"
22 #include "ParamSerializers.h"
23 #include "Messaging.h"
24 
25 #include <base/source/fstreamer.h>
26 #include <pluginterfaces/vst/vsttypes.h>
27 #include <pluginterfaces/vst/ivsteditcontroller.h>
28 #include <pluginterfaces/vst/ivstunits.h>
29 
30 #include <string>
31 #include <memory>
32 
33 namespace pongasoft {
34 namespace VST {
35 
36 using namespace Steinberg;
37 using namespace Steinberg::Vst;
38 
42 class IParamDef
43 {
44 public:
45  enum class Owner
46  {
47  kRT,
48  kGUI
49  };
50 public:
51  IParamDef(ParamID const iParamID,
52  TChar const *const iTitle,
53  Owner const iOwner,
54  bool const iTransient) :
55  fParamID{iParamID},
56  fTitle{iTitle},
57  fOwner{iOwner},
58  fTransient{iTransient}
59  {}
60 
61 public:
62  const ParamID fParamID;
63  const TChar *const fTitle;
64  const Owner fOwner; // who owns the parameter (and which stream will it be saved if non transient)
65  const bool fTransient; // not saved in the stream
66 };
67 
68 
72 class RawVstParamDef : public IParamDef
73 {
74 public:
75  RawVstParamDef(ParamID const iParamID,
76  TChar const *const iTitle,
77  TChar const *const iUnits,
78  ParamValue const iDefaultNormalizedValue,
79  int32 const iStepCount,
80  int32 const iFlags,
81  UnitID const iUnitID,
82  TChar const *const iShortTitle,
83  int32 const iPrecision,
84  Owner const iOwner,
85  bool const iTransient) :
86  IParamDef(iParamID, iTitle, iOwner, iTransient),
87  fUnits{iUnits},
88  fDefaultValue{Utils::clampE(iDefaultNormalizedValue, 0.0, 1.0)},
89  fStepCount{iStepCount},
90  fFlags{iFlags},
91  fUnitID{iUnitID},
92  fShortTitle{iShortTitle},
93  fPrecision{iPrecision}
94  {}
95 
96 public:
97  // readFromStream
98  ParamValue readFromStream(IBStreamer &iStreamer) const
99  {
100  ParamValue res = fDefaultValue;
101 
102  ParamValue value;
103  if(IBStreamHelper::readDouble(iStreamer, value) == kResultOk)
104  res = value;
105 
106  return res;
107  }
108 
109  // toString
110  virtual void toString(ParamValue iNormalizedValue, String128 iString) const
111  {
112  RawParamConverter::staticToString(iNormalizedValue, iString, fPrecision);
113  }
114 
115 public:
116  const TChar *const fUnits;
117  const ParamValue fDefaultValue;
118  const int32 fStepCount;
119  const int32 fFlags;
120  const UnitID fUnitID;
121  const TChar *const fShortTitle;
122  const int32 fPrecision;
123 };
124 
128 template<typename T>
130 {
131 public:
132  using ParamType = T;
133 
134  VstParamDef(ParamID const iParamID,
135  TChar const *const iTitle,
136  TChar const *const iUnits,
137  ParamType const iDefaultValue,
138  int32 const iFlags,
139  UnitID const iUnitID,
140  TChar const *const iShortTitle,
141  int32 const iPrecision,
142  Owner const iOwner,
143  bool const iTransient,
144  std::shared_ptr<IParamConverter<ParamType>> iConverter) :
145  RawVstParamDef(iParamID,
146  iTitle,
147  iUnits,
148  iConverter ? iConverter->normalize(iDefaultValue) : 0,
149  iConverter ? iConverter->getStepCount() : 0,
150  iFlags,
151  iUnitID,
152  iShortTitle,
153  iPrecision,
154  iOwner,
155  iTransient),
156  fDefaultValue{iDefaultValue},
157  fConverter{std::move(iConverter)}
158  {
159  }
160 
161  // getDefaultValue
162  ParamType getDefaultValue() const { return fDefaultValue; }
163 
164  // shortcut to normalize
165  inline ParamValue normalize(ParamType const &iValue) const
166  {
167  if(fConverter)
168  return fConverter->normalize(iValue);
169  return 0;
170  }
171 
172  // shortcut to denormalize
173  inline ParamType denormalize(ParamValue iNormalizedValue) const
174  {
175  if(fConverter)
176  return fConverter->denormalize(iNormalizedValue);
177  return fDefaultValue;
178  }
179 
183  void toString(ParamValue iNormalizedValue, String128 iString) const override
184  {
185  if(fConverter)
186  fConverter->toString(fConverter->denormalize(iNormalizedValue), iString, fPrecision);
187  else
188  RawVstParamDef::toString(iNormalizedValue, iString);
189  }
190 
191 public:
193  const std::shared_ptr<IParamConverter<ParamType>> fConverter;
194 };
195 
199 class IJmbParamDef : public IParamDef
200 {
201 public:
202  IJmbParamDef(const ParamID iParamID,
203  const TChar *const iTitle,
204  Owner const iOwner,
205  bool const iTransient,
206  bool const iShared)
207  : IParamDef(iParamID, iTitle, iOwner, iTransient),
208  fShared{iShared}
209  {}
210 
211  virtual ~IJmbParamDef() = default;
212 
213  // writeDefaultValue
214  virtual void writeDefaultValue(std::ostream &oStreamer) const = 0;
215 
216 public:
217  bool const fShared;
218 };
219 
224 template<typename T>
226 {
227 public:
228  using ParamType = T;
229 
230  JmbParamDef(ParamID const iParamID,
231  TChar const *const iTitle,
232  Owner const iOwner,
233  bool const iTransient,
234  bool const iShared,
235  ParamType const &iDefaultValue,
236  std::shared_ptr<IParamSerializer<ParamType>> iSerializer) :
237  IJmbParamDef(iParamID, iTitle, iOwner, iTransient, iShared),
238  fDefaultValue{iDefaultValue},
239  fSerializer{std::move(iSerializer)}
240  {}
241 
242  // readFromStream
243  tresult readFromStream(IBStreamer &iStreamer, ParamType &oValue) const override;
244  ParamType readFromStream(IBStreamer &iStreamer) const;
245 
246  // writeToStream
247  tresult writeToStream(ParamType const &iValue, IBStreamer &oStreamer) const override;
248 
249  // writeToStream
250  void writeToStream(ParamType const &iValue, std::ostream &oStreamer) const override;
251 
252  // writeDefaultValue
253  void writeDefaultValue(std::ostream &oStreamer) const override;
254 
255  // readFromMessage
256  tresult readFromMessage(Message const &iMessage, ParamType &oValue) const;
257 
258  // writeToMessage
259  tresult writeToMessage(ParamType const &iValue, Message &oMessage) const;
260 
261  // computeMessageAttrID
262  std::string computeMessageAttrID() const
263  {
264  return "__param__" + std::to_string(fParamID);
265  }
266 
267 public:
269  const std::shared_ptr<IParamSerializer<ParamType>> fSerializer;
270 };
271 
272 //------------------------------------------------------------------------
273 // JmbParamDef::readFromStream
274 //------------------------------------------------------------------------
275 template<typename T>
276 tresult JmbParamDef<T>::readFromStream(IBStreamer &iStreamer, T &oValue) const
277 {
278  if(fSerializer)
279  {
280  return fSerializer->readFromStream(iStreamer, oValue);
281  }
282  else
283  return kResultFalse;
284 }
285 
286 //------------------------------------------------------------------------
287 // JmbParamDef::readFromStream
288 //------------------------------------------------------------------------
289 template<typename T>
290 T JmbParamDef<T>::readFromStream(IBStreamer &iStreamer) const
291 {
292  T value;
293  if(readFromStream(iStreamer, value) != kResultOk)
294  value = fDefaultValue;
295  return value;
296 }
297 
298 
299 //------------------------------------------------------------------------
300 // JmbParamDef::writeToStream
301 //------------------------------------------------------------------------
302 template<typename T>
303 tresult JmbParamDef<T>::writeToStream(const T &iValue, IBStreamer &oStreamer) const
304 {
305  if(fSerializer)
306  return fSerializer->writeToStream(iValue, oStreamer);
307  else
308  return kResultFalse;
309 }
310 
311 //------------------------------------------------------------------------
312 // JmbParamDef::writeToStream
313 //------------------------------------------------------------------------
314 template<typename T>
315 void JmbParamDef<T>::writeToStream(const ParamType &iValue, std::ostream &oStreamer) const
316 {
317  if(fSerializer)
318  fSerializer->writeToStream(iValue, oStreamer);
319 }
320 
321 //------------------------------------------------------------------------
322 // JmbParamDef::writeDefaultValue
323 //------------------------------------------------------------------------
324 template<typename T>
325 void JmbParamDef<T>::writeDefaultValue(std::ostream &oStreamer) const
326 {
327  writeToStream(fDefaultValue, oStreamer);
328 }
329 
330 //------------------------------------------------------------------------
331 // JmbParamDef::readFromMessage
332 //------------------------------------------------------------------------
333 template<typename T>
334 tresult JmbParamDef<T>::readFromMessage(Message const &iMessage, ParamType &oValue) const
335 {
336  if(fSerializer)
337  return iMessage.getSerializableValue(computeMessageAttrID().c_str(), *this, oValue);
338  else
339  return kResultFalse;
340 }
341 
342 //------------------------------------------------------------------------
343 // JmbParamDef::writeToMessage
344 //------------------------------------------------------------------------
345 template<typename T>
346 tresult JmbParamDef<T>::writeToMessage(const ParamType &iValue, Message &oMessage) const
347 {
348  if(fSerializer)
349  return oMessage.setSerializableValue(computeMessageAttrID().c_str(), *this, iValue);
350  else
351  return kResultFalse;
352 }
353 
354 //------------------------------------------------------------------------
355 // VstParam - define shortcut notation
356 //------------------------------------------------------------------------
357 template<typename T>
358 using VstParam = std::shared_ptr<VstParamDef<T>>;
359 using RawVstParam = std::shared_ptr<RawVstParamDef>;
360 
361 //------------------------------------------------------------------------
362 // JmbParam - define shortcut notation
363 //------------------------------------------------------------------------
364 template<typename T>
365 using JmbParam = std::shared_ptr<JmbParamDef<T>>;
366 
367 }
368 }
369 
370 #endif // __PONGASOFT_VST_PARAM_DEF_H__
const UnitID fUnitID
Definition: ParamDef.h:120
const ParamType fDefaultValue
Definition: ParamDef.h:192
tresult readDouble(IBStreamer &iStreamer, double &oValue)
Definition: ParamSerializers.h:67
const Owner fOwner
Definition: ParamDef.h:64
const int32 fFlags
Definition: ParamDef.h:119
const TChar *const fUnits
Definition: ParamDef.h:116
std::shared_ptr< VstParamDef< T > > VstParam
Definition: ParamDef.h:358
tresult readFromMessage(Message const &iMessage, ParamType &oValue) const
Definition: ParamDef.h:334
const ParamID fParamID
Definition: ParamDef.h:62
Definition: ParamDef.h:199
T ParamType
Definition: ParamDef.h:228
void toString(ParamValue iNormalizedValue, String128 iString) const override
Definition: ParamDef.h:183
tresult readFromStream(IBStreamer &iStreamer, ParamType &oValue) const override
Definition: ParamDef.h:276
Definition: Messaging.h:43
static T clampE(const U &value, const T &lower, const T &upper)
Definition: Misc.h:58
JmbParamDef(ParamID const iParamID, TChar const *const iTitle, Owner const iOwner, bool const iTransient, bool const iShared, ParamType const &iDefaultValue, std::shared_ptr< IParamSerializer< ParamType >> iSerializer)
Definition: ParamDef.h:230
const int32 fStepCount
Definition: ParamDef.h:118
Definition: Clock.h:22
IJmbParamDef(const ParamID iParamID, const TChar *const iTitle, Owner const iOwner, bool const iTransient, bool const iShared)
Definition: ParamDef.h:202
tresult getSerializableValue(IAttributeList::AttrID id, IParamSerializer< T > const &iSerializer, T &oValue) const
Definition: Messaging.h:178
ParamValue normalize(ParamType const &iValue) const
Definition: ParamDef.h:165
const bool fTransient
Definition: ParamDef.h:65
const int32 fPrecision
Definition: ParamDef.h:122
ParamType denormalize(ParamValue iNormalizedValue) const
Definition: ParamDef.h:173
const std::shared_ptr< IParamSerializer< ParamType > > fSerializer
Definition: ParamDef.h:269
Definition: ParamDef.h:42
virtual void toString(ParamValue iNormalizedValue, String128 iString) const
Definition: ParamDef.h:110
tresult writeToMessage(ParamType const &iValue, Message &oMessage) const
Definition: ParamDef.h:346
ParamType getDefaultValue() const
Definition: ParamDef.h:162
std::shared_ptr< JmbParamDef< T > > JmbParam
Definition: ParamDef.h:365
const TChar *const fTitle
Definition: ParamDef.h:63
std::shared_ptr< RawVstParamDef > RawVstParam
Definition: ParamDef.h:359
void writeDefaultValue(std::ostream &oStreamer) const override
Definition: ParamDef.h:325
Definition: ParamDef.h:72
tresult writeToStream(ParamType const &iValue, IBStreamer &oStreamer) const override
Definition: ParamDef.h:303
bool const fShared
Definition: ParamDef.h:217
Definition: ParamSerializers.h:43
Owner
Definition: ParamDef.h:45
tresult setSerializableValue(IAttributeList::AttrID id, IParamSerializer< T > const &iSerializer, T const &iValue)
Definition: Messaging.h:159
const std::shared_ptr< IParamConverter< ParamType > > fConverter
Definition: ParamDef.h:193
T ParamType
Definition: ParamDef.h:132
static void staticToString(ParamValue const &iValue, String128 oString, int32 iPrecision)
Definition: ParamConverters.h:90
const TChar *const fShortTitle
Definition: ParamDef.h:121
RawVstParamDef(ParamID const iParamID, TChar const *const iTitle, TChar const *const iUnits, ParamValue const iDefaultNormalizedValue, int32 const iStepCount, int32 const iFlags, UnitID const iUnitID, TChar const *const iShortTitle, int32 const iPrecision, Owner const iOwner, bool const iTransient)
Definition: ParamDef.h:75
Definition: ParamDef.h:129
VstParamDef(ParamID const iParamID, TChar const *const iTitle, TChar const *const iUnits, ParamType const iDefaultValue, int32 const iFlags, UnitID const iUnitID, TChar const *const iShortTitle, int32 const iPrecision, Owner const iOwner, bool const iTransient, std::shared_ptr< IParamConverter< ParamType >> iConverter)
Definition: ParamDef.h:134
const ParamType fDefaultValue
Definition: ParamDef.h:268
Definition: ParamDef.h:225
std::string computeMessageAttrID() const
Definition: ParamDef.h:262
IParamDef(ParamID const iParamID, TChar const *const iTitle, Owner const iOwner, bool const iTransient)
Definition: ParamDef.h:51
ParamValue readFromStream(IBStreamer &iStreamer) const
Definition: ParamDef.h:98
const ParamValue fDefaultValue
Definition: ParamDef.h:117
Definition: ParamConverters.h:50