Jamba C++ API  4.0.0
GUIRawVstParameter.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 #pragma once
19 
20 #include "IGUIParameter.h"
22 #include "VstParameters.h"
23 #include "GUIParamCx.h"
24 
25 #include <string>
26 
28 
29 template<typename T>
31 
38 class GUIRawVstParameter : public ITGUIParameter<ParamValue>
39 {
40 public:
41  using ParamType = ParamValue;
43 
44 public:
58  class Editor : public EditorType
59  {
60  public:
61  Editor(ParamID iParamID, VstParametersSPtr iVstParameters);
62 
63  ~Editor() override { rollback(); }
64 
65  // disabling copy
66  Editor(Editor const &) = delete;
67  Editor& operator=(Editor const &) = delete;
68 
72  tresult setValue(ParamValue const &iValue) override;
73 
78  bool updateValue(ParamValue const &iValue) override;
79 
80  /*
81  * Call when you are done with the modifications.
82  * This has no effect if rollback() has already been called
83  */
84  tresult commit() override;
85 
86  // importing superclass commit methods
87  using EditorType::commit;
88 
93  tresult rollback() override;
94 
95  private:
96  ParamID fParamID;
98 
99  ParamValue fInitialParamValue;
101  };
102 
103 public:
104  // Constructor
105  GUIRawVstParameter(ParamID iParamID,
106  VstParametersSPtr iVstParameters,
107  std::shared_ptr<RawVstParamDef> iParamDef);
108 
109  // Destructor
110  ~GUIRawVstParameter() = default;
111 // {
112 // DLOG_F(INFO, "RawParameter::~RawParameter(%d)", fParamID);
113 // }
114 
115  // getParamID
116  inline ParamID getParamID() const override
117  {
118  return fParamID;
119  }
120 
121  // accessValue
122  tresult accessValue(ValueAccessor const &iGetter) const override
123  {
124  iGetter(getValue());
125  return kResultOk;
126  }
127 
131  inline ParamValue getValue() const
132  {
133  return fVstParameters->getParamNormalized(fParamID);
134  }
135 
139  inline int32 getStepCount() const override
140  {
141  auto parameterInfo = fVstParameters->getParameterInfo(fParamID);
142  if(parameterInfo)
143  return parameterInfo->stepCount;
144  else
145  return 0;
146  }
147 
151  void toString(String128 oString)
152  {
153  fParamDef->toString(getValue(), oString);
154  }
155 
159  String toString()
160  {
161  String128 s;
162  toString(s);
163  return String(s);
164  }
165 
166  // toUTF8String
167  std::string toUTF8String(int32 iPrecision) const override
168  {
169  return fParamDef->toUTF8String(getValue(), iPrecision);
170  }
171 
177  bool update(ParamValue const &iValue) override
178  {
179  auto const previousValue = getValue();
180  if(previousValue != iValue)
181  {
182  setValue(iValue);
183  return true;
184  }
185  return false;
186  }
187 
192  inline tresult setValue(ParamValue const &iValue) override
193  {
194  Editor editor(fParamID, fVstParameters);
195  editor.setValue(iValue);
196  return editor.commit();
197  }
198 
202  std::unique_ptr<EditorType> edit() override
203  {
204  return std::make_unique<Editor>(fParamID, fVstParameters);
205  }
206 
211 
215  std::unique_ptr<FObjectCx> connect(Parameters::IChangeListener *iChangeListener) const override
216  {
217  return fVstParameters->connect(fParamID, iChangeListener);
218  }
219 
223  std::unique_ptr<FObjectCx> connect(Parameters::ChangeCallback iChangeCallback) const override
224  {
225  return fVstParameters->connect(fParamID, std::move(iChangeCallback));
226  }
227 
232  template<typename T>
233  std::shared_ptr<GUIVstParameter<T>> asVstParameter();
234 
242  std::shared_ptr<GUIDiscreteParameter> asDiscreteParameter(int32 iStepCount) override;
243 
244 private:
245  ParamID fParamID;
247  std::shared_ptr<RawVstParamDef> fParamDef;
248 };
249 
250 //-------------------------------------------------------------------------------
251 // GUIRawVstParam - wrapper to make writing the code much simpler and natural
252 //-------------------------------------------------------------------------------
258 {
259 public:
260  // Constructor
261  GUIRawVstParam(std::shared_ptr<GUIRawVstParameter> iPtr = nullptr) : // NOLINT (not marked explicit on purpose)
262  fPtr{std::move(iPtr)}
263  {}
264 
266  GUIRawVstParam &operator=(GUIRawVstParam const &iOther) = default;
267 
268  // exists
269  inline bool exists() const { return (bool) fPtr; }
270 
271  // getParamID
272  inline ParamID getParamID() const { DCHECK_F(exists()); return fPtr->getParamID(); }
273 
277  inline ParamValue getValue() const { DCHECK_F(exists()); return fPtr->getValue(); }
278 
283  tresult setValue(ParamValue const &iValue) { DCHECK_F(exists()); return fPtr->setValue(iValue); }
284 
288  tresult copyValueFrom(GUIRawVstParam const &iParam) { DCHECK_F(exists()); return setValue(iParam.getValue()); }
289 
293  inline int32 getStepCount() const { DCHECK_F(exists()); return fPtr->getStepCount(); }
294 
298  void toString(String128 oString) { fPtr->toString(oString); }
299 
303  String toString() { DCHECK_F(exists()); return fPtr->toString(); }
304 
308  std::unique_ptr<GUIRawVstParameter::EditorType> edit() { DCHECK_F(exists()); return fPtr->edit(); }
309 
315  std::unique_ptr<GUIRawVstParameter::EditorType> edit(ParamValue const &iValue) { DCHECK_F(exists()); return fPtr->edit(iValue); }
316 
318  inline operator ParamValue() const { DCHECK_F(exists()); return fPtr->getValue(); } // NOLINT
319 
321  inline GUIRawVstParam &operator=(ParamValue const &iValue) { DCHECK_F(exists()); fPtr->setValue(iValue); return *this; }
322 
324  inline bool operator==(const GUIRawVstParam &rhs) const { DCHECK_F(exists()); return fPtr->getValue() == rhs.fPtr->getValue(); }
325 
327  inline bool operator!=(const GUIRawVstParam &rhs) const { DCHECK_F(exists()); return fPtr->getValue() != rhs.fPtr->getValue(); }
328 
332  inline std::unique_ptr<FObjectCx> connect(Parameters::IChangeListener *iChangeListener) const { DCHECK_F(exists()); return fPtr->connect(iChangeListener); }
333 
337  inline std::unique_ptr<FObjectCx> connect(Parameters::ChangeCallback iChangeCallback) const { DCHECK_F(exists()); return fPtr->connect(std::move(iChangeCallback)); }
338 
339 private:
340  std::shared_ptr<GUIRawVstParameter> fPtr;
341 };
342 
343 //------------------------------------------------------------------------
344 // shortcut notations
345 //------------------------------------------------------------------------
346 using GUIRawVstParamEditor = std::unique_ptr<GUIRawVstParameter::EditorType>;
347 
348 }
std::shared_ptr< GUIDiscreteParameter > asDiscreteParameter(int32 iStepCount) override
This implementation will adapt this parameter to be interpreted as a discrete parameter.
Definition: GUIRawVstParameter.cpp:144
~Editor() override
Definition: GUIRawVstParameter.h:63
GUIRawVstParam & operator=(ParamValue const &iValue)
Allow to write param = 0.5.
Definition: GUIRawVstParameter.h:321
GUIRawVstParam(std::shared_ptr< GUIRawVstParameter > iPtr=nullptr)
Definition: GUIRawVstParameter.h:261
ParamID fParamID
Definition: GUIRawVstParameter.h:96
bool exists() const
Definition: GUIRawVstParameter.h:269
This is the main class that the plugin should use as it exposes only the necessary methods of the par...
Definition: GUIRawVstParameter.h:257
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
std::unique_ptr< GUIRawVstParameter::EditorType > edit(ParamValue const &iValue)
Shortcut to create an editor and set the value to it.
Definition: GUIRawVstParameter.h:315
std::unique_ptr< FObjectCx > connect(Parameters::ChangeCallback iChangeCallback) const
Definition: GUIRawVstParameter.h:337
bool updateValue(ParamValue const &iValue) override
Change the value of the parameter.
Definition: GUIRawVstParameter.cpp:54
ParamID getParamID() const override
Each parameter has a unique ID returned by this method.
Definition: GUIRawVstParameter.h:116
std::shared_ptr< RawVstParamDef > fParamDef
Definition: GUIRawVstParameter.h:247
GUIRawVstParameter(ParamID iParamID, VstParametersSPtr iVstParameters, std::shared_ptr< RawVstParamDef > iParamDef)
Definition: GUIRawVstParameter.cpp:97
ParamValue fInitialParamValue
Definition: GUIRawVstParameter.h:99
std::function< void(ParamValue const &)> ValueAccessor
API to access the value of the param.
Definition: IGUIParameter.h:178
ParamID fParamID
Definition: GUIRawVstParameter.h:245
tresult setValue(ParamValue const &iValue)
Sets the value of this parameter.
Definition: GUIRawVstParameter.h:283
Editor(ParamID iParamID, VstParametersSPtr iVstParameters)
Definition: GUIRawVstParameter.cpp:26
bool fIsEditing
Definition: GUIRawVstParameter.h:100
ParamValue getValue() const
Definition: GUIRawVstParameter.h:131
String toString()
Returns a string representation of this parameter.
Definition: GUIRawVstParameter.h:303
Definition: GUIState.h:36
tresult rollback() override
Call this if you want to revert to the original value of the parameter (when the editor is created).
Definition: GUIRawVstParameter.cpp:82
tresult setValue(ParamValue const &iValue) override
Sets the value of this parameter.
Definition: GUIRawVstParameter.h:192
bool update(ParamValue const &iValue) override
Update the parameter with a value.
Definition: GUIRawVstParameter.h:177
VstParametersSPtr fVstParameters
Definition: GUIRawVstParameter.h:97
VstParametersSPtr fVstParameters
Definition: GUIRawVstParameter.h:246
GUIRawVstParam & operator=(GUIRawVstParam const &iOther)=default
Assignment operator: fMyParam = registerParam(...);
ITGUIParameter< ParamValue >::ITEditor EditorType
Definition: GUIRawVstParameter.h:42
ParamValue getValue() const
Definition: GUIRawVstParameter.h:277
std::unique_ptr< GUIRawVstParameter::EditorType > edit()
Definition: GUIRawVstParameter.h:308
bool operator!=(const GUIRawVstParam &rhs) const
Allow to write param1 != param2.
Definition: GUIRawVstParameter.h:327
void toString(String128 oString)
Populates the oString with a string representation of this parameter.
Definition: GUIRawVstParameter.h:151
This class wraps a GUIRawVstParameter to deal with any type T.
Definition: GUIRawVstParameter.h:30
ParamID getParamID() const
Definition: GUIRawVstParameter.h:272
int32 getStepCount() const override
Definition: GUIRawVstParameter.h:139
String toString()
Returns a string representation of this parameter.
Definition: GUIRawVstParameter.h:159
std::unique_ptr< GUIRawVstParameter::EditorType > GUIRawVstParamEditor
Definition: GUIRawVstParameter.h:346
bool operator==(const GUIRawVstParam &rhs) const
Allow to write param1 == param2.
Definition: GUIRawVstParameter.h:324
tresult commit() override
Definition: GUIRawVstParameter.cpp:68
std::shared_ptr< VstParameters > VstParametersSPtr
Definition: VstParameters.h:95
ParamValue ParamType
Definition: GUIRawVstParameter.h:41
std::shared_ptr< GUIRawVstParameter > fPtr
Definition: GUIRawVstParameter.h:340
tresult accessValue(ValueAccessor const &iGetter) const override
API to access the underlying value.
Definition: GUIRawVstParameter.h:122
void toString(String128 oString)
Populates the oString with a string representation of this parameter.
Definition: GUIRawVstParameter.h:298
tresult setValue(ParamValue const &iValue) override
Change the value of the parameter.
Definition: GUIRawVstParameter.cpp:39
std::shared_ptr< GUIVstParameter< T > > asVstParameter()
Converts to a typed parameter.
Definition: GUIVstParameter.h:372
tresult copyValueFrom(GUIRawVstParam const &iParam)
Shortcut to copy the value from another param to this one.
Definition: GUIRawVstParameter.h:288
Wrapper to edit a single parameter.
Definition: GUIRawVstParameter.h:58
Encapsulates a vst parameter and how to access it (read/write) as well as how to "connect" to it in o...
Definition: GUIRawVstParameter.h:38
std::unique_ptr< FObjectCx > connect(Parameters::IChangeListener *iChangeListener) const
Definition: GUIRawVstParameter.h:332
std::string toUTF8String(int32 iPrecision) const override
Returns the current value of the parameter as a string (which is properly UTF-8 encoded).
Definition: GUIRawVstParameter.h:167
std::unique_ptr< FObjectCx > connect(Parameters::IChangeListener *iChangeListener) const override
Definition: GUIRawVstParameter.h:215
std::unique_ptr< EditorType > edit() override
Definition: GUIRawVstParameter.h:202
Interface to implement to receive parameter changes.
Definition: Parameters.h:43
std::unique_ptr< FObjectCx > connect(Parameters::ChangeCallback iChangeCallback) const override
Definition: GUIRawVstParameter.h:223
int32 getStepCount() const
Definition: GUIRawVstParameter.h:293