Jamba C++ API  6.0.0
RTParameter.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018-2020 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 #include <pluginterfaces/vst/ivstaudioprocessor.h>
19 
20 #ifndef __PONGASOFT_VST_RT_PARAMETER_H__
21 #define __PONGASOFT_VST_RT_PARAMETER_H__
22 
23 #include <pongasoft/VST/ParamDef.h>
24 #include <pongasoft/logging/logging.h>
26 
27 namespace pongasoft::VST::RT {
28 
35 {
36 public:
37  // Constructor
38  explicit RTRawVstParameter(std::shared_ptr<RawVstParamDef> iParamDef) :
39  fParamDef{std::move(iParamDef)},
40  fNormalizedValue{fParamDef->fDefaultValue},
42  {}
43 
44  // getParamID
45  ParamID getParamID() const { return fParamDef->fParamID; }
46 
47  // getParamDef
48  inline RawVstParamDef const *getParamDef() const { return fParamDef.get(); }
49 
56  virtual bool updateNormalizedValue(ParamValue iNormalizedValue);
57 
58  // getNormalizedValue
59  inline ParamValue const &getNormalizedValue() const { return fNormalizedValue; }
60 
61  // getPreviousNormalizedValue
62  inline ParamValue const &getPreviousNormalizedValue() const { return fPreviousNormalizedValue; }
63 
67  tresult addToOutput(ProcessData &oData);
68 
72  inline bool hasChanged() const { return fNormalizedValue != fPreviousNormalizedValue; }
73 
78  virtual bool resetPreviousValue();
79 
80 protected:
81  std::shared_ptr<RawVstParamDef> fParamDef;
82  ParamValue fNormalizedValue;
84 };
85 
90 template<typename T>
92 {
93 public:
94  using ParamType = T;
95 
96  // Constructor
97  explicit RTVstParameter(VstParam<T> iParamDef) :
98  RTRawVstParameter(iParamDef),
101  {
102  }
103 
104  // getParamDef
105  inline VstParamDef<T> const *getParamDefT() const
106  {
107  return static_cast<VstParamDef<T> const *>(getParamDef());
108  }
109 
110  // shortcut to normalize
111  inline ParamValue normalize(ParamType const &iValue) const { return getParamDefT()->normalize(iValue); }
112 
113  // shortcut to denormalize
114  inline ParamType denormalize(ParamValue iNormalizedValue) const { return getParamDefT()->denormalize(iNormalizedValue); }
115 
120  void update(ParamType const &iNewValue);
121 
122  // getValue
123  inline ParamType const &getValue() const { return fValue; }
124 
125  // getPreviousValue
126  inline ParamType const &getPreviousValue() const { return fPreviousValue; }
127 
128 protected:
129  // Override the base class to update the denormalized value as well
130  bool updateNormalizedValue(ParamValue iNormalizedValue) override;
131 
132  // Override the base class to update the denormalized value as well
133  bool resetPreviousValue() override;
134 
135 protected:
138 };
139 
140 //------------------------------------------------------------------------
141 // RTParameter::updateNormalizedValue - update fValue to the new value and return true if it changed
142 //------------------------------------------------------------------------
143 template<typename T>
144 bool RTVstParameter<T>::updateNormalizedValue(ParamValue iNormalizedValue)
145 {
146  if(RTRawVstParameter::updateNormalizedValue(iNormalizedValue))
147  {
148  fValue = denormalize(iNormalizedValue);
149  return true;
150  }
151 
152  return false;
153 }
154 
155 //------------------------------------------------------------------------
156 // RTParameter::resetPreviousValue
157 //------------------------------------------------------------------------
158 template<typename T>
160 {
162  {
163  fPreviousValue = fValue;
164  return true;
165  }
166 
167  return false;
168 }
169 
170 //------------------------------------------------------------------------
171 // RTParameter::update
172 //------------------------------------------------------------------------
173 template<typename T>
174 void RTVstParameter<T>::update(const ParamType &iNewValue)
175 {
176  fValue = iNewValue;
177  fNormalizedValue = normalize(fValue);
178 }
179 
180 //------------------------------------------------------------------------
181 // RTVstParam - wrapper to make writing the code much simpler and natural
182 //------------------------------------------------------------------------
189 template<typename T>
190 class RTVstParam : public Utils::Operators::Dereferenceable<RTVstParam<T>>
191 {
192  using ParamType = T;
193 
194 public:
195  RTVstParam(RTVstParameter<T> *iPtr) : fPtr{iPtr} // NOLINT (not marked explicit on purpose)
196  {
197  DCHECK_F(fPtr != nullptr);
198  }
199 
200  // getParamID
201  inline ParamID getParamID() const { return fPtr->getParamID(); }
202 
203  // shortcut to normalize
204  inline ParamValue normalize(ParamType const &iValue) const { return fPtr->normalize(iValue); }
205 
206  // shortcut to denormalize
207  inline ParamType denormalize(ParamValue iNormalizedValue) const { return fPtr->denormalize(iNormalizedValue); }
208 
209  // getValue
210  inline T const &getValue() const { return fPtr->getValue(); }
211 
212  // value -- synonym
213  inline T const &value() const { return fPtr->getValue(); }
214 
216  inline ParamValue const &normalizedValue() const { return fPtr->getNormalizedValue(); }
217 
223  inline void update(ParamType const &iNewValue) { fPtr->update(iNewValue); }
224 
229  inline void update(ParamType const &iNewValue, ProcessData &oData)
230  {
231  update(iNewValue);
232  addToOutput(oData);
233  }
234 
238  inline bool hasChanged() const { return fPtr->hasChanged(); }
239 
243  inline tresult addToOutput(ProcessData &oData) { return fPtr->addToOutput(oData); }
244 
245  // allow to use the param as the underlying ParamType (ex: "if(param)" in the case ParamType is bool))
246  [[deprecated("Since 4.1.0 - use operator* or value() instead (ex: *fState.fBypass or fState.fBypass.value())")]]
247  inline operator ParamType const &() const { return fPtr->getValue(); }
248 
250  constexpr ParamType const &operator *() const { return fPtr->getValue(); }
251 
252  // allow writing param->xxx to access the underlying type directly (if not a primitive)
253  constexpr ParamType const *operator->() const { return &fPtr->getValue(); }
254 
256  inline RTVstParam<T> &operator=(ParamType const &iValue) { update(iValue); return *this; }
257 
258  // previous
259  inline ParamType const &previous() const { return fPtr->getPreviousValue(); }
260 
261 private:
263 };
264 
265 //------------------------------------------------------------------------
266 // RTRawVstParam - wrapper to make writing the code much simpler and natural
267 //------------------------------------------------------------------------
273 {
274 public:
275  RTRawVstParam(RTRawVstParameter *iPtr) : fPtr{iPtr} // NOLINT (not marked explicit on purpose)
276  {
277  DCHECK_F(fPtr != nullptr);
278  }
279 
280  // getParamID
281  inline ParamID getParamID() const { return fPtr->getParamID(); }
282 
283  // getValue
284  inline ParamValue const &getValue() const { return fPtr->getNormalizedValue(); }
285 
286  // value - synonym
287  inline ParamValue const &value() const { return fPtr->getNormalizedValue(); }
288 
294  inline bool update(ParamValue const &iNewValue) { return fPtr->updateNormalizedValue(iNewValue); }
295 
300  inline bool update(ParamValue const &iNewValue, ProcessData &oData)
301  {
302  if(update(iNewValue))
303  {
304  addToOutput(oData);
305  return true;
306  }
307  else
308  return false;
309  }
310 
314  inline bool hasChanged() const { return fPtr->hasChanged(); }
315 
319  inline tresult addToOutput(ProcessData &oData) { return fPtr->addToOutput(oData); }
320 
321  // allow to use the param as the ParamValue
322  [[deprecated("Since 4.1.0 - use operator* instead")]]
323  inline operator ParamValue const &() const { return fPtr->getNormalizedValue(); }
324 
326  inline ParamValue operator *() const { return fPtr->getNormalizedValue(); }
327 
329  inline RTRawVstParam &operator=(ParamValue const &iValue) { update(iValue); return *this; }
330 
331  // previous
332  inline ParamValue const &previous() const { return fPtr->getPreviousNormalizedValue(); }
333 
334 private:
336 };
337 
338 }
339 
340 #endif // __PONGASOFT_VST_RT_PARAMETER_H__
RTVstParameter(VstParam< T > iParamDef)
Definition: RTParameter.h:97
void update(ParamType const &iNewValue)
This method is typically called during the processing method when the plugin needs to update the valu...
Definition: RTParameter.h:174
ParamType denormalize(ParamValue iNormalizedValue) const
Definition: RTParameter.h:114
This is the main class that the plugin should use as it exposes only the necessary methods of the par...
Definition: RTParameter.h:190
ParamValue fNormalizedValue
Definition: RTParameter.h:82
void update(ParamType const &iNewValue)
This method is typically called during the processing method when the plugin needs to update the valu...
Definition: RTParameter.h:223
T const & getValue() const
Definition: RTParameter.h:210
RTVstParam< T > & operator=(ParamType const &iValue)
Allow to write param = 3.0.
Definition: RTParameter.h:256
constexpr ParamType const & operator *() const
allow writing *param to access the underlying value (or in other words, *param is the same param....
Definition: RTParameter.h:250
RTRawVstParam & operator=(ParamValue const &iValue)
Allow to write param = 0.5.
Definition: RTParameter.h:329
ParamValue const & value() const
Definition: RTParameter.h:287
ParamType denormalize(ParamValue iNormalizedValue) const
Definition: RTParameter.h:207
RawVstParamDef const * getParamDef() const
Definition: RTParameter.h:48
ParamValue operator *() const
allow writing *param to access the underlying value (or in other words, *param is the same param....
Definition: RTParameter.h:326
ParamType const & previous() const
Definition: RTParameter.h:259
Implements all the various equality and relational operators for the type T which is assumed to encap...
Definition: Operators.h:54
VstParamDef< T > const * getParamDefT() const
Definition: RTParameter.h:105
T ParamType
Definition: RTParameter.h:94
Definition: RTJmbInParameter.h:25
bool resetPreviousValue() override
Called at the end of the frame so that previous value is set to current value for the next frame.
Definition: RTParameter.h:159
ParamType const & getValue() const
Definition: RTParameter.h:123
RTRawVstParam(RTRawVstParameter *iPtr)
Definition: RTParameter.h:275
void update(ParamType const &iNewValue, ProcessData &oData)
This method is typically called during the processing method when the plugin needs to update the valu...
Definition: RTParameter.h:229
T const & value() const
Definition: RTParameter.h:213
tresult addToOutput(ProcessData &oData)
Add the current normalized value as an output parameter changes which propagates the change to the vs...
Definition: RTParameter.h:243
bool hasChanged() const
Definition: RTParameter.h:238
RTVstParam(RTVstParameter< T > *iPtr)
Definition: RTParameter.h:195
bool updateNormalizedValue(ParamValue iNormalizedValue) override
Update the parameter with a new normalized value.
Definition: RTParameter.h:144
tresult addToOutput(ProcessData &oData)
Add the current normalized value as an output parameter changes.
Definition: RTParameter.cpp:29
ParamID getParamID() const
Definition: RTParameter.h:281
bool hasChanged() const
Definition: RTParameter.h:72
ParamType const & getPreviousValue() const
Definition: RTParameter.h:126
ParamValue const & getPreviousNormalizedValue() const
Definition: RTParameter.h:62
bool update(ParamValue const &iNewValue)
This method is typically called during the processing method when the plugin needs to update the valu...
Definition: RTParameter.h:294
RTRawVstParameter * fPtr
Definition: RTParameter.h:335
ParamValue const & normalizedValue() const
Return the normalized value.
Definition: RTParameter.h:216
constexpr ParamType const * operator->() const
Definition: RTParameter.h:253
ParamType fPreviousValue
Definition: RTParameter.h:137
ParamValue fPreviousNormalizedValue
Definition: RTParameter.h:83
ParamID getParamID() const
Definition: RTParameter.h:201
This is the main class that the plugin should use as it exposes only the necessary methods of the par...
Definition: RTParameter.h:272
RTRawVstParameter(std::shared_ptr< RawVstParamDef > iParamDef)
Definition: RTParameter.h:38
bool update(ParamValue const &iNewValue, ProcessData &oData)
This method is typically called during the processing method when the plugin needs to update the valu...
Definition: RTParameter.h:300
virtual bool updateNormalizedValue(ParamValue iNormalizedValue)
Update the parameter with a new normalized value.
Definition: RTParameter.cpp:49
virtual bool resetPreviousValue()
Called at the end of the frame so that previous value is set to current value for the next frame.
Definition: RTParameter.cpp:65
ParamValue normalize(ParamType const &iValue) const
Definition: RTParameter.h:204
Base class which deals with the "raw"/untyped parameter and keep the normalized value (ParamValue in ...
Definition: RTParameter.h:34
ParamValue const & getNormalizedValue() const
Definition: RTParameter.h:59
The typed version.
Definition: RTParameter.h:91
ParamValue const & previous() const
Definition: RTParameter.h:332
bool hasChanged() const
Definition: RTParameter.h:314
tresult addToOutput(ProcessData &oData)
Add the current normalized value as an output parameter changes which propagates the change to the vs...
Definition: RTParameter.h:319
RTVstParameter< T > * fPtr
Definition: RTParameter.h:262
T ParamType
Definition: RTParameter.h:192
std::shared_ptr< RawVstParamDef > fParamDef
Definition: RTParameter.h:81
ParamID getParamID() const
Definition: RTParameter.h:45
ParamValue const & getValue() const
Definition: RTParameter.h:284
ParamType fValue
Definition: RTParameter.h:136
ParamValue normalize(ParamType const &iValue) const
Definition: RTParameter.h:111