Jamba C++ API  4.1.0
GUIVstParameter.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018-2019 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_GUI_PARAMETER_H__
19 #define __PONGASOFT_VST_GUI_PARAMETER_H__
20 
21 #include "IGUIParameter.h"
22 #include "GUIRawVstParameter.h"
24 #include <pongasoft/VST/ParamDef.h>
26 
28 
32 template<typename T>
33 class GUIVstParameter : public ITGUIParameter<T>
34 {
35 public:
36  using ParamType = T;
38 
39 public:
53  class Editor : public EditorType
54  {
55  public:
56  inline explicit Editor(GUIRawVstParamEditor iRawEditor,
57  std::shared_ptr<IParamConverter<T>> iConverter) :
58  fRawEditor{std::move(iRawEditor)},
59  fConverter{std::move(iConverter)}
60  {
61  }
62 
63  ~Editor() override { rollback(); }
64 
65  // disabling copy
66  Editor(Editor const &) = delete;
67  Editor& operator=(Editor const &) = delete;
68 
72  tresult setValue(ParamType const &iValue) override
73  {
74  return fRawEditor->setValue(fConverter->normalize(iValue));
75  }
76 
81  bool updateValue(ParamType const &iValue) override
82  {
83  return fRawEditor->updateValue(fConverter->normalize(iValue));
84  }
85 
86  /*
87  * Call when you are done with the modifications.
88  * This has no effect if rollback() has already been called
89  */
90  tresult commit() override
91  {
92  return fRawEditor->commit();
93  }
94 
95  // importing superclass commit methods
96  using EditorType::commit;
97 
102  tresult rollback() override
103  {
104  return fRawEditor->rollback();
105  }
106 
107  private:
109  std::shared_ptr<IParamConverter<T>> fConverter;
110  };
111 
112 public:
113  // Constructor
114  GUIVstParameter(std::shared_ptr<GUIRawVstParameter> iRawParameter,
115  std::shared_ptr<IParamConverter<T>> iConverter) :
116  fRawParameter{std::move(iRawParameter)},
117  fConverter{std::move(iConverter)}
118  {
119  DCHECK_NOTNULL_F(fRawParameter.get());
120  DCHECK_NOTNULL_F(fConverter.get());
121  // DLOG_F(INFO, "VSTParameter::VSTParameter(%d)", fRawParameter->getParamID());
122  }
123 
124  // Destructor
126  {
127  // DLOG_F(INFO, "VSTParameter::~VSTParameter(%d)", fRawParameter->getParamID());
128  }
129 
130  // getParamID
131  ParamID getParamID() const override
132  {
133  return fRawParameter->getParamID();
134  }
135 
136  // accessValue
137  tresult accessValue(typename ITGUIParameter<T>::ValueAccessor const &iGetter) const override
138  {
139  iGetter(getValue());
140  return kResultOk;
141  }
142 
147  {
148  return fConverter->denormalize(fRawParameter->getValue());
149  }
150 
154  ParamValue getNormalizedValue() const
155  {
156  return fRawParameter->getValue();
157  }
158 
164  bool update(ParamType const &iValue) override
165  {
166  // Implementation note: because ParamType may not define `operator!=` we use the normalized value instead
167  auto currentNormalizedValue = getNormalizedValue();
168  auto newNormalizedValued = fConverter->normalize(iValue);
169  if(currentNormalizedValue != newNormalizedValued)
170  {
171  if(setNormalizedValue(newNormalizedValued) == kResultOk)
172  return true;
173  }
174  return false;
175  }
176 
181  tresult setValue(ParamType const &iValue) override
182  {
183  return fRawParameter->setValue(fConverter->normalize(iValue));
184  }
185 
190  tresult setNormalizedValue(ParamValue const &iNormalizedValue)
191  {
192  return fRawParameter->setValue(iNormalizedValue);
193  }
194 
198  inline int32 getStepCount() const override { return fConverter->getStepCount(); }
199 
203  void toString(String128 oString)
204  {
205  fRawParameter->toString(oString);
206  }
207 
211  String toString()
212  {
213  return fRawParameter->toString();
214  }
215 
216  // toUTF8String
217  std::string toUTF8String(int32 iPrecision) const override
218  {
219  return fConverter->toString(getValue(), iPrecision);
220  }
221 
225  std::unique_ptr<EditorType> edit() override
226  {
227  return std::make_unique<Editor>(fRawParameter->edit(), fConverter);
228  }
229 
234 
238  std::unique_ptr<FObjectCx> connect(Parameters::IChangeListener *iChangeListener) const override
239  {
240  return fRawParameter->connect(iChangeListener);
241  }
242 
246  std::unique_ptr<FObjectCx> connect(Parameters::ChangeCallback iChangeCallback) const override
247  {
248  return fRawParameter->connect(std::move(iChangeCallback));
249  }
250 
251  // asDiscreteParameter
252  std::shared_ptr<GUIDiscreteParameter> asDiscreteParameter(int32 iStepCount) override
253  {
254  return fRawParameter->asDiscreteParameter(iStepCount);
255  }
256 
257 private:
258  std::shared_ptr<GUIRawVstParameter> fRawParameter;
259  std::shared_ptr<IParamConverter<T>> fConverter;
260 };
261 
262 //------------------------------------------------------------------------
263 // GUIVstParam - wrapper to make writing the code much simpler and natural
264 //------------------------------------------------------------------------
271 template<typename T>
272 class GUIVstParam: public Utils::Operators::Dereferenceable<GUIVstParam<T>>
273 {
274 public:
279  class Value {
280  public:
281  constexpr T const *operator ->() const { return &fValue; }
282  friend class GUIVstParam<T>;
283  private:
284  explicit Value(T const &iValue) : fValue{iValue} {}
286  };
287 
288 public:
289  // Constructor
290  GUIVstParam(std::shared_ptr<GUIVstParameter<T>> iPtr = nullptr) : // NOLINT (not marked explicit on purpose)
291  fPtr{std::move(iPtr)}
292  {}
293 
295  GUIVstParam<T> &operator=(GUIVstParam<T> const &iOther) = default;
296 
297  // exists
298  inline bool exists() const { return (bool) fPtr; }
299 
300  // getParamID
301  inline ParamID getParamID() const { DCHECK_F(exists()); return fPtr->getParamID(); }
302 
306  inline T getValue() const { DCHECK_F(exists()); return fPtr->getValue(); }
307 
309  inline T value() const { DCHECK_F(exists()); return fPtr->getValue(); }
310 
314  inline ParamValue getNormalizedValue() const { DCHECK_F(exists()); return fPtr->getNormalizedValue(); }
315 
321  bool update(T const &iValue) { DCHECK_F(exists()); return fPtr->update(iValue); }
322 
327  tresult setValue(T const &iValue) { DCHECK_F(exists()); return fPtr->setValue(iValue); }
328 
333  tresult setNormalizedValue(ParamValue const &iNormalizedValue) { DCHECK_F(exists()); return fPtr->setNormalizedValue(iNormalizedValue); }
334 
339  template<typename V>
340  tresult copyValueFrom(GUIVstParam<V> const &iParam) { DCHECK_F(exists()); return setNormalizedValue(iParam.getNormalizedValue()); }
341 
345  tresult copyValueFrom(GUIRawVstParam const &iParam) { DCHECK_F(exists()); return setNormalizedValue(iParam.getValue()); }
346 
350  inline int32 getStepCount() const { DCHECK_F(exists()); return fPtr->getStepCount(); }
351 
355  void toString(String128 oString) { DCHECK_F(exists()); fPtr->toString(oString); }
356 
360  String toString() { DCHECK_F(exists()); return fPtr->toString(); }
361 
365  std::unique_ptr<typename GUIVstParameter<T>::EditorType> edit() { DCHECK_F(exists()); return fPtr->edit(); }
366 
372  std::unique_ptr<typename GUIVstParameter<T>::EditorType> edit(T const &iValue) { DCHECK_F(exists()); return fPtr->edit(iValue); }
373 
375  constexpr T operator *() const { DCHECK_F(exists()); return fPtr->getValue(); }
376 
378  constexpr Value operator ->() const { DCHECK_F(exists()); return Value{fPtr->getValue()}; }
379 
381  [[deprecated("Since 4.1.0 - use operator* or .value() instead (ex: if(*param) {...} or if(param.value()) {...}")]]
382  inline operator T() const { DCHECK_F(exists()); return fPtr->getValue(); } // NOLINT
383 
385  inline GUIVstParam<T> &operator=(T const &iValue) { DCHECK_F(exists()); fPtr->setValue(iValue); return *this; }
386 
388 // friend constexpr bool operator==(GUIVstParam<T> const &lhs, GUIVstParam<T> const &rhs) { return lhs.fPtr->getNormalizedValue() == rhs.fPtr->getNormalizedValue(); }
389 
393  inline std::unique_ptr<FObjectCx> connect(Parameters::IChangeListener *iChangeListener) const { DCHECK_F(exists()); return fPtr->connect(iChangeListener); }
394 
398  inline std::unique_ptr<FObjectCx> connect(Parameters::ChangeCallback iChangeCallback) const { DCHECK_F(exists()); return fPtr->connect(std::move(iChangeCallback)); }
399 
400 private:
401  std::shared_ptr<GUIVstParameter<T>> fPtr;
402 };
403 
404 
405 //------------------------------------------------------------------------
406 // GUIRawVstParameter::asVstParameter
407 //------------------------------------------------------------------------
408 template<typename T>
409 std::shared_ptr<GUIVstParameter<T>> GUIRawVstParameter::asVstParameter()
410 {
411  auto vstParamDef = std::dynamic_pointer_cast<VstParamDef<T>>(fParamDef);
412  if(vstParamDef && vstParamDef->fConverter)
413  return std::make_shared<GUIVstParameter<T>>(std::dynamic_pointer_cast<GUIRawVstParameter>(shared_from_this()),
414  vstParamDef->fConverter);
415  else
416  return nullptr;
417 }
418 
419 //------------------------------------------------------------------------
420 // shortcut notations
421 //------------------------------------------------------------------------
422 template<typename T>
423 using GUIVstParamEditor = std::unique_ptr<typename GUIVstParameter<T>::EditorType>;
424 
427 
428 }
429 
430 #endif // __PONGASOFT_VST_GUI_PARAMETER_H__
typename ITGUIParameter< T >::ITEditor EditorType
Definition: GUIVstParameter.h:37
T getValue() const
Definition: GUIVstParameter.h:306
std::unique_ptr< typename GUIVstParameter< T >::EditorType > edit()
Definition: GUIVstParameter.h:365
T value() const
Synonym to getValue()
Definition: GUIVstParameter.h:309
tresult rollback() override
Call this if you want to revert to the original value of the parameter (when the editor is created).
Definition: GUIVstParameter.h:102
tresult setValue(ParamType const &iValue) override
Sets the value of this parameter.
Definition: GUIVstParameter.h:181
ParamType getValue() const
Definition: GUIVstParameter.h:146
bool update(ParamType const &iValue) override
Update the parameter with a value.
Definition: GUIVstParameter.h:164
int32 getStepCount() const
Definition: GUIVstParameter.h:350
std::shared_ptr< IParamConverter< T > > fConverter
Definition: GUIVstParameter.h:259
This is the main class that the plugin should use as it exposes only the necessary methods of the par...
Definition: GUIRawVstParameter.h:258
ParamValue getNormalizedValue() const
Definition: GUIVstParameter.h:154
std::function< void()> ChangeCallback
A callback that will be invoked for changes.
Definition: Parameters.h:55
Represents a gui parameter with its underlying backing type T (aka ParamType).
Definition: IGUIParameter.h:33
tresult setNormalizedValue(ParamValue const &iNormalizedValue)
Sets the value of this parameter as a normalized value.
Definition: GUIVstParameter.h:333
~Editor() override
Definition: GUIVstParameter.h:63
bool exists() const
Definition: GUIVstParameter.h:298
std::unique_ptr< EditorType > edit() override
Definition: GUIVstParameter.h:225
Value(T const &iValue)
Definition: GUIVstParameter.h:284
void toString(String128 oString)
Populates the oString with a string representation of this parameter.
Definition: GUIVstParameter.h:355
std::shared_ptr< GUIVstParameter< T > > fPtr
Definition: GUIVstParameter.h:401
GUIVstParam< T > & operator=(T const &iValue)
Allow to write param = 3.0.
Definition: GUIVstParameter.h:385
tresult copyValueFrom(GUIVstParam< V > const &iParam)
Shortcut to copy the value from another param to this one.
Definition: GUIVstParameter.h:340
std::shared_ptr< RawVstParamDef > fParamDef
Definition: GUIRawVstParameter.h:248
Implements all the various equality and relational operators for the type T which is assumed to encap...
Definition: Operators.h:54
std::function< void(T const &)> ValueAccessor
API to access the value of the param.
Definition: IGUIParameter.h:178
tresult setValue(T const &iValue)
Sets the value of this parameter.
Definition: GUIVstParameter.h:327
This is the main class that the plugin should use as it exposes only the necessary methods of the par...
Definition: GUIVstParameter.h:272
bool update(T const &iValue)
Update the parameter with a value.
Definition: GUIVstParameter.h:321
std::unique_ptr< typename GUIVstParameter< T >::EditorType > GUIVstParamEditor
Definition: GUIVstParameter.h:423
std::unique_ptr< FObjectCx > connect(Parameters::IChangeListener *iChangeListener) const
Allow to write param1 == param2.
Definition: GUIVstParameter.h:393
std::shared_ptr< GUIDiscreteParameter > asDiscreteParameter(int32 iStepCount) override
Converts this parameter into a discrete parameter.
Definition: GUIVstParameter.h:252
Definition: GUIState.h:36
tresult setValue(ParamType const &iValue) override
Change the value of the parameter.
Definition: GUIVstParameter.h:72
bool updateValue(ParamType const &iValue) override
Change the value of the parameter.
Definition: GUIVstParameter.h:81
~GUIVstParameter()
Definition: GUIVstParameter.h:125
void toString(String128 oString)
Populates the oString with a string representation of this parameter.
Definition: GUIVstParameter.h:203
The purpose of this class is to copy the value so that it can be accessed via -> thus allowing to wri...
Definition: GUIVstParameter.h:279
GUIVstParam(std::shared_ptr< GUIVstParameter< T >> iPtr=nullptr)
Definition: GUIVstParameter.h:290
Editor(GUIRawVstParamEditor iRawEditor, std::shared_ptr< IParamConverter< T >> iConverter)
Definition: GUIVstParameter.h:56
GUIRawVstParamEditor fRawEditor
Definition: GUIVstParameter.h:108
std::shared_ptr< GUIRawVstParameter > fRawParameter
Definition: GUIVstParameter.h:258
std::unique_ptr< FObjectCx > connect(Parameters::IChangeListener *iChangeListener) const override
Definition: GUIVstParameter.h:238
ParamValue getValue() const
Definition: GUIRawVstParameter.h:278
tresult accessValue(typename ITGUIParameter< T >::ValueAccessor const &iGetter) const override
Definition: GUIVstParameter.h:137
std::unique_ptr< FObjectCx > connect(Parameters::ChangeCallback iChangeCallback) const override
Definition: GUIVstParameter.h:246
constexpr Value operator ->() const
allow writing param->x to access the underlying value when T is a struct or class
Definition: GUIVstParameter.h:378
This class wraps a GUIRawVstParameter to deal with any type T.
Definition: GUIRawVstParameter.h:31
std::string toUTF8String(int32 iPrecision) const override
Returns the current value of the parameter as a string (which is properly UTF-8 encoded).
Definition: GUIVstParameter.h:217
tresult setNormalizedValue(ParamValue const &iNormalizedValue)
Sets the value of this parameter as a normalized value.
Definition: GUIVstParameter.h:190
GUIVstParameter(std::shared_ptr< GUIRawVstParameter > iRawParameter, std::shared_ptr< IParamConverter< T >> iConverter)
Definition: GUIVstParameter.h:114
std::unique_ptr< GUIRawVstParameter::EditorType > GUIRawVstParamEditor
Definition: GUIRawVstParameter.h:355
GUIVstParam< T > & operator=(GUIVstParam< T > const &iOther)=default
Assignment operator: fMyParam = registerParam(...);
String toString()
Returns a string representation of this parameter.
Definition: GUIVstParameter.h:211
ParamID getParamID() const
Definition: GUIVstParameter.h:301
Typed parameter definition.
Definition: ParamDef.h:148
tresult copyValueFrom(GUIRawVstParam const &iParam)
Shortcut to copy the value from another param to this one (raw value)
Definition: GUIVstParameter.h:345
T ParamType
Definition: GUIVstParameter.h:36
Defines the API for the editor which can be obtained by calling ITGUIParameter::edit().
Definition: IGUIParameter.h:232
ParamValue getNormalizedValue() const
Definition: GUIVstParameter.h:314
Wrapper to edit a single parameter.
Definition: GUIVstParameter.h:53
constexpr T const * operator ->() const
Definition: GUIVstParameter.h:281
std::shared_ptr< GUIVstParameter< T > > asVstParameter()
Converts to a typed parameter.
Definition: GUIVstParameter.h:409
T fValue
Definition: GUIVstParameter.h:285
tresult commit() override
Definition: GUIVstParameter.h:90
std::unique_ptr< typename GUIVstParameter< T >::EditorType > edit(T const &iValue)
Shortcut to create an editor and set the value to it.
Definition: GUIVstParameter.h:372
std::shared_ptr< IParamConverter< T > > fConverter
Definition: GUIVstParameter.h:109
String toString()
Returns a string representation of this parameter.
Definition: GUIVstParameter.h:360
int32 getStepCount() const override
Definition: GUIVstParameter.h:198
ParamID getParamID() const override
Each parameter has a unique ID returned by this method.
Definition: GUIVstParameter.h:131
constexpr T operator *() const
allow writing *param to access the underlying value (or in other words, *param is the same param....
Definition: GUIVstParameter.h:375
std::unique_ptr< FObjectCx > connect(Parameters::ChangeCallback iChangeCallback) const
Definition: GUIVstParameter.h:398
Interface to implement to receive parameter changes.
Definition: Parameters.h:43
A vst parameter is represented by a ParamValue type which is a double in the range [0,...
Definition: ParamConverters.h:53