Jamba C++ API  4.1.0
RTParameter.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 #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 
198  // getParamID
199  inline ParamID getParamID() const { return fPtr->getParamID(); }
200 
201  // shortcut to normalize
202  inline ParamValue normalize(ParamType const &iValue) const { return fPtr->normalize(iValue); }
203 
204  // shortcut to denormalize
205  inline ParamType denormalize(ParamValue iNormalizedValue) const { return fPtr->denormalize(iNormalizedValue); }
206 
207  // getValue
208  inline T const &getValue() const { return fPtr->getValue(); }
209 
210  // value -- synonym
211  inline T const &value() const { return fPtr->getValue(); }
212 
214  inline ParamValue const &normalizedValue() const { return fPtr->getNormalizedValue(); }
215 
221  inline void update(ParamType const &iNewValue) { fPtr->update(iNewValue); }
222 
227  inline void update(ParamType const &iNewValue, ProcessData &oData)
228  {
229  update(iNewValue);
230  addToOutput(oData);
231  }
232 
236  inline bool hasChanged() const { return fPtr->hasChanged(); }
237 
241  inline tresult addToOutput(ProcessData &oData) { return fPtr->addToOutput(oData); }
242 
243  // allow to use the param as the underlying ParamType (ex: "if(param)" in the case ParamType is bool))
244  [[deprecated("Since 4.1.0 - use operator* or value() instead (ex: *fState.fBypass or fState.fBypass.value())")]]
245  inline operator ParamType const &() const { return fPtr->getValue(); }
246 
248  constexpr ParamType const &operator *() const { return fPtr->getValue(); }
249 
250  // allow writing param->xxx to access the underlying type directly (if not a primitive)
251  constexpr ParamType const *operator->() const { return &fPtr->getValue(); }
252 
254  inline RTVstParam<T> &operator=(ParamType const &iValue) { update(iValue); return *this; }
255 
256  // previous
257  inline ParamType const &previous() const { return fPtr->getPreviousValue(); }
258 
259 private:
261 };
262 
263 //------------------------------------------------------------------------
264 // RTRawVstParam - wrapper to make writing the code much simpler and natural
265 //------------------------------------------------------------------------
271 {
272 public:
274  {}
275 
276  // getParamID
277  inline ParamID getParamID() const { return fPtr->getParamID(); }
278 
279  // getValue
280  inline ParamValue const &getValue() const { return fPtr->getNormalizedValue(); }
281 
282  // value - synonym
283  inline ParamValue const &value() const { return fPtr->getNormalizedValue(); }
284 
290  inline bool update(ParamValue const &iNewValue) { return fPtr->updateNormalizedValue(iNewValue); }
291 
296  inline bool update(ParamValue const &iNewValue, ProcessData &oData)
297  {
298  if(update(iNewValue))
299  {
300  addToOutput(oData);
301  return true;
302  }
303  else
304  return false;
305  }
306 
310  inline bool hasChanged() const { return fPtr->hasChanged(); }
311 
315  inline tresult addToOutput(ProcessData &oData) { return fPtr->addToOutput(oData); }
316 
317  // allow to use the param as the ParamValue
318  [[deprecated("Since 4.1.0 - use operator* instead")]]
319  inline operator ParamValue const &() const { return fPtr->getNormalizedValue(); }
320 
322  inline ParamValue operator *() const { return fPtr->getNormalizedValue(); }
323 
325  inline RTRawVstParam &operator=(ParamValue const &iValue) { update(iValue); return *this; }
326 
327  // previous
328  inline ParamValue const &previous() const { return fPtr->getPreviousNormalizedValue(); }
329 
330 private:
332 };
333 
334 }
335 
336 #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:221
T const & getValue() const
Definition: RTParameter.h:208
RTVstParam< T > & operator=(ParamType const &iValue)
Allow to write param = 3.0.
Definition: RTParameter.h:254
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:248
RTRawVstParam & operator=(ParamValue const &iValue)
Allow to write param = 0.5.
Definition: RTParameter.h:325
ParamValue const & value() const
Definition: RTParameter.h:283
ParamType denormalize(ParamValue iNormalizedValue) const
Definition: RTParameter.h:205
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:322
ParamType const & previous() const
Definition: RTParameter.h:257
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:27
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:273
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:227
T const & value() const
Definition: RTParameter.h:211
tresult addToOutput(ProcessData &oData)
Add the current normalized value as an output parameter changes which propagates the change to the vs...
Definition: RTParameter.h:241
bool hasChanged() const
Definition: RTParameter.h:236
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:277
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:290
RTRawVstParameter * fPtr
Definition: RTParameter.h:331
ParamValue const & normalizedValue() const
Return the normalized value.
Definition: RTParameter.h:214
constexpr ParamType const * operator->() const
Definition: RTParameter.h:251
ParamType fPreviousValue
Definition: RTParameter.h:137
ParamValue fPreviousNormalizedValue
Definition: RTParameter.h:83
ParamID getParamID() const
Definition: RTParameter.h:199
This is the main class that the plugin should use as it exposes only the necessary methods of the par...
Definition: RTParameter.h:270
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:296
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:202
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:328
bool hasChanged() const
Definition: RTParameter.h:310
tresult addToOutput(ProcessData &oData)
Add the current normalized value as an output parameter changes which propagates the change to the vs...
Definition: RTParameter.h:315
RTVstParameter< T > * fPtr
Definition: RTParameter.h:260
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:280
ParamType fValue
Definition: RTParameter.h:136
ParamValue normalize(ParamType const &iValue) const
Definition: RTParameter.h:111