Jamba C++ API  4.0.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>
25 
27 
31 template<typename T>
32 class GUIVstParameter : public ITGUIParameter<T>
33 {
34 public:
35  using ParamType = T;
37 
38 public:
52  class Editor : public EditorType
53  {
54  public:
55  inline explicit Editor(GUIRawVstParamEditor iRawEditor,
56  std::shared_ptr<IParamConverter<T>> iConverter) :
57  fRawEditor{std::move(iRawEditor)},
58  fConverter{std::move(iConverter)}
59  {
60  }
61 
62  ~Editor() override { rollback(); }
63 
64  // disabling copy
65  Editor(Editor const &) = delete;
66  Editor& operator=(Editor const &) = delete;
67 
71  tresult setValue(ParamType const &iValue) override
72  {
73  return fRawEditor->setValue(fConverter->normalize(iValue));
74  }
75 
80  bool updateValue(ParamType const &iValue) override
81  {
82  return fRawEditor->updateValue(fConverter->normalize(iValue));
83  }
84 
85  /*
86  * Call when you are done with the modifications.
87  * This has no effect if rollback() has already been called
88  */
89  tresult commit() override
90  {
91  return fRawEditor->commit();
92  }
93 
94  // importing superclass commit methods
95  using EditorType::commit;
96 
101  tresult rollback() override
102  {
103  return fRawEditor->rollback();
104  }
105 
106  private:
108  std::shared_ptr<IParamConverter<T>> fConverter;
109  };
110 
111 public:
112  // Constructor
113  GUIVstParameter(std::shared_ptr<GUIRawVstParameter> iRawParameter,
114  std::shared_ptr<IParamConverter<T>> iConverter) :
115  fRawParameter{std::move(iRawParameter)},
116  fConverter{std::move(iConverter)}
117  {
118  DCHECK_NOTNULL_F(fRawParameter.get());
119  DCHECK_NOTNULL_F(fConverter.get());
120  // DLOG_F(INFO, "VSTParameter::VSTParameter(%d)", fRawParameter->getParamID());
121  }
122 
123  // Destructor
125  {
126  // DLOG_F(INFO, "VSTParameter::~VSTParameter(%d)", fRawParameter->getParamID());
127  }
128 
129  // getParamID
130  ParamID getParamID() const override
131  {
132  return fRawParameter->getParamID();
133  }
134 
135  // accessValue
136  tresult accessValue(typename ITGUIParameter<T>::ValueAccessor const &iGetter) const override
137  {
138  iGetter(getValue());
139  return kResultOk;
140  }
141 
146  {
147  return fConverter->denormalize(fRawParameter->getValue());
148  }
149 
153  ParamValue getNormalizedValue() const
154  {
155  return fRawParameter->getValue();
156  }
157 
163  bool update(ParamType const &iValue) override
164  {
165  // Implementation note: because ParamType may not define `operator!=` we use the normalized value instead
166  auto previousValue = getNormalizedValue();
167  if(previousValue != fConverter->normalize(iValue))
168  {
169  setValue(iValue);
170  return true;
171  }
172  return false;
173  }
174 
179  tresult setValue(ParamType const &iValue) override
180  {
181  return fRawParameter->setValue(fConverter->normalize(iValue));
182  }
183 
188  tresult setNormalizedValue(ParamValue const &iNormalizedValue)
189  {
190  return fRawParameter->setValue(iNormalizedValue);
191  }
192 
196  inline int32 getStepCount() const override { return fConverter->getStepCount(); }
197 
201  void toString(String128 oString)
202  {
203  fRawParameter->toString(oString);
204  }
205 
209  String toString()
210  {
211  return fRawParameter->toString();
212  }
213 
214  // toUTF8String
215  std::string toUTF8String(int32 iPrecision) const override
216  {
217  return fConverter->toString(getValue(), iPrecision);
218  }
219 
223  std::unique_ptr<EditorType> edit() override
224  {
225  return std::make_unique<Editor>(fRawParameter->edit(), fConverter);
226  }
227 
232 
236  std::unique_ptr<FObjectCx> connect(Parameters::IChangeListener *iChangeListener) const override
237  {
238  return fRawParameter->connect(iChangeListener);
239  }
240 
244  std::unique_ptr<FObjectCx> connect(Parameters::ChangeCallback iChangeCallback) const override
245  {
246  return fRawParameter->connect(std::move(iChangeCallback));
247  }
248 
249  // asDiscreteParameter
250  std::shared_ptr<GUIDiscreteParameter> asDiscreteParameter(int32 iStepCount) override
251  {
252  return fRawParameter->asDiscreteParameter(iStepCount);
253  }
254 
255 private:
256  std::shared_ptr<GUIRawVstParameter> fRawParameter;
257  std::shared_ptr<IParamConverter<T>> fConverter;
258 };
259 
260 //------------------------------------------------------------------------
261 // GUIVstParam - wrapper to make writing the code much simpler and natural
262 //------------------------------------------------------------------------
269 template<typename T>
271 {
272 public:
273  // Constructor
274  GUIVstParam(std::shared_ptr<GUIVstParameter<T>> iPtr = nullptr) : // NOLINT (not marked explicit on purpose)
275  fPtr{std::move(iPtr)}
276  {}
277 
279  GUIVstParam<T> &operator=(GUIVstParam<T> const &iOther) = default;
280 
281  // exists
282  inline bool exists() const { return (bool) fPtr; }
283 
284  // getParamID
285  inline ParamID getParamID() const { DCHECK_F(exists()); return fPtr->getParamID(); }
286 
290  inline T getValue() const { DCHECK_F(exists()); return fPtr->getValue(); }
291 
295  inline ParamValue getNormalizedValue() const { DCHECK_F(exists()); return fPtr->getNormalizedValue(); }
296 
301  tresult setValue(T const &iValue) { DCHECK_F(exists()); return fPtr->setValue(iValue); }
302 
307  tresult setNormalizedValue(ParamValue const &iNormalizedValue) { DCHECK_F(exists()); return fPtr->setNormalizedValue(iNormalizedValue); }
308 
313  tresult copyValueFrom(GUIVstParam<T> const &iParam) { DCHECK_F(exists()); return setNormalizedValue(iParam.getNormalizedValue()); }
314 
318  inline int32 getStepCount() const { DCHECK_F(exists()); return fPtr->getStepCount(); }
319 
323  void toString(String128 oString) { DCHECK_F(exists()); fPtr->toString(oString); }
324 
328  String toString() { DCHECK_F(exists()); return fPtr->toString(); }
329 
333  std::unique_ptr<typename GUIVstParameter<T>::EditorType> edit() { DCHECK_F(exists()); return fPtr->edit(); }
334 
340  std::unique_ptr<typename GUIVstParameter<T>::EditorType> edit(T const &iValue) { DCHECK_F(exists()); return fPtr->edit(iValue); }
341 
343  inline operator T() const { DCHECK_F(exists()); return fPtr->getValue(); } // NOLINT
344 
346  inline GUIVstParam<T> &operator=(T const &iValue) { DCHECK_F(exists()); fPtr->setValue(iValue); return *this; }
347 
349  inline bool operator==(const GUIVstParam<T> &rhs) const { DCHECK_F(exists()); return fPtr->getNormalizedValue() == rhs.fPtr->getNormalizedValue(); }
350 
352  inline bool operator!=(const GUIVstParam &rhs) const { DCHECK_F(exists()); return fPtr->getNormalizedValue() != rhs.fPtr->getNormalizedValue(); }
353 
357  inline std::unique_ptr<FObjectCx> connect(Parameters::IChangeListener *iChangeListener) const { DCHECK_F(exists()); return fPtr->connect(iChangeListener); }
358 
362  inline std::unique_ptr<FObjectCx> connect(Parameters::ChangeCallback iChangeCallback) const { DCHECK_F(exists()); return fPtr->connect(std::move(iChangeCallback)); }
363 
364 private:
365  std::shared_ptr<GUIVstParameter<T>> fPtr;
366 };
367 
368 //------------------------------------------------------------------------
369 // GUIRawVstParameter::asVstParameter
370 //------------------------------------------------------------------------
371 template<typename T>
372 std::shared_ptr<GUIVstParameter<T>> GUIRawVstParameter::asVstParameter()
373 {
374  auto vstParamDef = std::dynamic_pointer_cast<VstParamDef<T>>(fParamDef);
375  if(vstParamDef && vstParamDef->fConverter)
376  return std::make_shared<GUIVstParameter<T>>(std::dynamic_pointer_cast<GUIRawVstParameter>(shared_from_this()),
377  vstParamDef->fConverter);
378  else
379  return nullptr;
380 }
381 
382 //------------------------------------------------------------------------
383 // shortcut notations
384 //------------------------------------------------------------------------
385 template<typename T>
386 using GUIVstParamEditor = std::unique_ptr<typename GUIVstParameter<T>::EditorType>;
387 
390 
391 }
392 
393 #endif // __PONGASOFT_VST_GUI_PARAMETER_H__
typename ITGUIParameter< T >::ITEditor EditorType
Definition: GUIVstParameter.h:36
T getValue() const
Definition: GUIVstParameter.h:290
std::unique_ptr< typename GUIVstParameter< T >::EditorType > edit()
Definition: GUIVstParameter.h:333
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:101
tresult setValue(ParamType const &iValue) override
Sets the value of this parameter.
Definition: GUIVstParameter.h:179
ParamType getValue() const
Definition: GUIVstParameter.h:145
bool update(ParamType const &iValue) override
Update the parameter with a value.
Definition: GUIVstParameter.h:163
int32 getStepCount() const
Definition: GUIVstParameter.h:318
std::shared_ptr< IParamConverter< T > > fConverter
Definition: GUIVstParameter.h:257
bool operator!=(const GUIVstParam &rhs) const
Allow to write param1 != param2.
Definition: GUIVstParameter.h:352
tresult copyValueFrom(GUIVstParam< T > const &iParam)
Shortcut to copy the value from another param to this one.
Definition: GUIVstParameter.h:313
ParamValue getNormalizedValue() const
Definition: GUIVstParameter.h:153
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:307
~Editor() override
Definition: GUIVstParameter.h:62
bool exists() const
Definition: GUIVstParameter.h:282
std::unique_ptr< EditorType > edit() override
Definition: GUIVstParameter.h:223
void toString(String128 oString)
Populates the oString with a string representation of this parameter.
Definition: GUIVstParameter.h:323
std::shared_ptr< GUIVstParameter< T > > fPtr
Definition: GUIVstParameter.h:365
GUIVstParam< T > & operator=(T const &iValue)
Allow to write param = 3.0.
Definition: GUIVstParameter.h:346
std::shared_ptr< RawVstParamDef > fParamDef
Definition: GUIRawVstParameter.h:247
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:301
This is the main class that the plugin should use as it exposes only the necessary methods of the par...
Definition: GUIVstParameter.h:270
std::unique_ptr< typename GUIVstParameter< T >::EditorType > GUIVstParamEditor
Definition: GUIVstParameter.h:386
std::unique_ptr< FObjectCx > connect(Parameters::IChangeListener *iChangeListener) const
Definition: GUIVstParameter.h:357
std::shared_ptr< GUIDiscreteParameter > asDiscreteParameter(int32 iStepCount) override
Converts this parameter into a discrete parameter.
Definition: GUIVstParameter.h:250
Definition: GUIState.h:36
tresult setValue(ParamType const &iValue) override
Change the value of the parameter.
Definition: GUIVstParameter.h:71
bool updateValue(ParamType const &iValue) override
Change the value of the parameter.
Definition: GUIVstParameter.h:80
~GUIVstParameter()
Definition: GUIVstParameter.h:124
void toString(String128 oString)
Populates the oString with a string representation of this parameter.
Definition: GUIVstParameter.h:201
GUIVstParam(std::shared_ptr< GUIVstParameter< T >> iPtr=nullptr)
Definition: GUIVstParameter.h:274
Editor(GUIRawVstParamEditor iRawEditor, std::shared_ptr< IParamConverter< T >> iConverter)
Definition: GUIVstParameter.h:55
GUIRawVstParamEditor fRawEditor
Definition: GUIVstParameter.h:107
std::shared_ptr< GUIRawVstParameter > fRawParameter
Definition: GUIVstParameter.h:256
std::unique_ptr< FObjectCx > connect(Parameters::IChangeListener *iChangeListener) const override
Definition: GUIVstParameter.h:236
tresult accessValue(typename ITGUIParameter< T >::ValueAccessor const &iGetter) const override
Definition: GUIVstParameter.h:136
std::unique_ptr< FObjectCx > connect(Parameters::ChangeCallback iChangeCallback) const override
Definition: GUIVstParameter.h:244
This class wraps a GUIRawVstParameter to deal with any type T.
Definition: GUIRawVstParameter.h:30
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:215
tresult setNormalizedValue(ParamValue const &iNormalizedValue)
Sets the value of this parameter as a normalized value.
Definition: GUIVstParameter.h:188
GUIVstParameter(std::shared_ptr< GUIRawVstParameter > iRawParameter, std::shared_ptr< IParamConverter< T >> iConverter)
Definition: GUIVstParameter.h:113
std::unique_ptr< GUIRawVstParameter::EditorType > GUIRawVstParamEditor
Definition: GUIRawVstParameter.h:346
GUIVstParam< T > & operator=(GUIVstParam< T > const &iOther)=default
Assignment operator: fMyParam = registerParam(...);
String toString()
Returns a string representation of this parameter.
Definition: GUIVstParameter.h:209
ParamID getParamID() const
Definition: GUIVstParameter.h:285
Typed parameter definition.
Definition: ParamDef.h:148
T ParamType
Definition: GUIVstParameter.h:35
Defines the API for the editor which can be obtained by calling ITGUIParameter::edit().
Definition: IGUIParameter.h:232
ParamValue getNormalizedValue() const
Definition: GUIVstParameter.h:295
Wrapper to edit a single parameter.
Definition: GUIVstParameter.h:52
std::shared_ptr< GUIVstParameter< T > > asVstParameter()
Converts to a typed parameter.
Definition: GUIVstParameter.h:372
tresult commit() override
Definition: GUIVstParameter.h:89
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:340
std::shared_ptr< IParamConverter< T > > fConverter
Definition: GUIVstParameter.h:108
String toString()
Returns a string representation of this parameter.
Definition: GUIVstParameter.h:328
int32 getStepCount() const override
Definition: GUIVstParameter.h:196
ParamID getParamID() const override
Each parameter has a unique ID returned by this method.
Definition: GUIVstParameter.h:130
std::unique_ptr< FObjectCx > connect(Parameters::ChangeCallback iChangeCallback) const
Definition: GUIVstParameter.h:362
Interface to implement to receive parameter changes.
Definition: Parameters.h:43
bool operator==(const GUIVstParam< T > &rhs) const
Allow to write param1 == param2.
Definition: GUIVstParameter.h:349
A vst parameter is represented by a ParamValue type which is a double in the range [0,...
Definition: ParamConverters.h:53