Jamba  3.0.2
ParamConverters.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 #ifndef __PONGASOFT_VST_PARAM_CONVERTERS_H__
19 #define __PONGASOFT_VST_PARAM_CONVERTERS_H__
20 
21 #include <pongasoft/Utils/Misc.h>
22 
23 #include <pluginterfaces/vst/vsttypes.h>
24 #include <pluginterfaces/vst/ivstparameterchanges.h>
25 #include <pluginterfaces/base/ustring.h>
26 #include <base/source/fstring.h>
27 #include <pluginterfaces/base/ftypes.h>
28 
29 #include <cmath>
30 #include <algorithm>
31 #include <memory>
32 #include <string>
33 #include <array>
34 #include <vector>
35 
36 namespace pongasoft {
37 namespace VST {
38 
39 using namespace Steinberg;
40 using namespace Steinberg::Vst;
41 
49 template<typename T>
51 {
52 public:
53  using ParamType = T;
54  virtual int getStepCount() const { return 0; }
55  virtual ParamValue normalize(ParamType const &iValue) const = 0;
56  virtual ParamType denormalize(ParamValue iNormalizedValue) const = 0;
57  virtual void toString(ParamType const &iValue, String128 iString, int32 iPrecision) const { }
58  virtual std::string toString(ParamType const &iValue, int32 iPrecision) const
59  {
60  String128 s;
61  toString(iValue, s, iPrecision);
62  return String(s).text8();
63  }
64 };
65 
69 class RawParamConverter : public IParamConverter<ParamValue>
70 {
71 public:
72 
74 
75  inline ParamValue normalize(ParamValue const &iValue) const override
76  {
77  return Utils::clamp(iValue, 0.0, 1.0);
78  }
79 
80  inline ParamValue denormalize(ParamValue iNormalizedValue) const override
81  {
82  return Utils::clamp(iNormalizedValue, 0.0, 1.0);;
83  }
84 
85  inline void toString(ParamValue const &iValue, String128 oString, int32 iPrecision) const override
86  {
87  staticToString(iValue, oString, iPrecision);
88  }
89 
90  static inline void staticToString(ParamValue const &iValue, String128 oString, int32 iPrecision)
91  {
92  Steinberg::UString wrapper(oString, str16BufferSize(String128));
93  if(!wrapper.printFloat(iValue, iPrecision))
94  oString[0] = 0;
95  }
96 };
97 
104 {
105 public:
107 
108  explicit BooleanParamConverter(char16 const *iFalseString = STR16("Off"),
109  char16 const *iTrueString = STR16("On")) :
110  fFalseString{iFalseString},
111  fTrueString{iTrueString}
112  {}
113 
114  inline int getStepCount() const override { return 1; }
115 
116  inline ParamValue normalize(bool const &iValue) const override
117  {
118  return iValue ? 1.0 : 0;
119  }
120 
121  inline bool denormalize(ParamValue iNormalizedValue) const override
122  {
123  return iNormalizedValue >= 0.5;
124  }
125 
126  inline void toString(bool const &iValue, String128 oString, int32 /* iPrecision */) const override
127  {
128  Steinberg::UString wrapper(oString, str16BufferSize(String128));
129  if(iValue)
130  wrapper.assign(fTrueString);
131  else
132  wrapper.assign(fFalseString);
133  }
134 
135 protected:
136  char16 const *fFalseString;
137  char16 const *fTrueString;
138 };
139 
143 using Percent = double;
144 
149 class PercentParamConverter : public IParamConverter<Percent>
150 {
151 public:
152 
154 
155  inline ParamValue normalize(double const &iValue) const override
156  {
157  return Utils::clamp(iValue, 0.0, 1.0);
158  }
159 
160  inline double denormalize(ParamValue iNormalizedValue) const override
161  {
162  return Utils::clamp(iNormalizedValue, 0.0, 1.0);
163  }
164 
165  inline void toString(ParamType const &iValue, String128 oString, int32 iPrecision) const override
166  {
167  Steinberg::UString wrapper(oString, str16BufferSize (String128));
168  wrapper.printFloat(iValue * 100, iPrecision);
169  wrapper.append(STR16("%"));
170  }
171 };
172 
178 template<int StepCount>
180 {
181 public:
182  using ParamType = int;
183 
185 
186  // Constructor - you can provide an offset for the toString conversion (ex: counting from 1 instead of 0)
187  explicit DiscreteValueParamConverter(int iToStringOffset = 0) : fToStringOffset{iToStringOffset} {}
188 
189  // Constructor with printf style format where the parameter (%d) will be (value + offset)
190  explicit DiscreteValueParamConverter(char16 const *iFormat, int iToStringOffset = 0) :
191  fToStringOffset{iToStringOffset}, fFormat{iFormat} {}
192 
193  // Constructor with all values defined
194  explicit DiscreteValueParamConverter(std::array<ConstString, StepCount + 1> const &iToStringValues) :
195  fToStringValues(iToStringValues.cbegin(), iToStringValues.cend()) {}
196 
197  inline int getStepCount() const override { return StepCount; }
198 
199  inline ParamValue normalize(int const &iDiscreteValue) const override
200  {
201  auto value = Utils::clamp(iDiscreteValue, 0, StepCount);
202  return value / static_cast<double>(StepCount);
203  }
204 
205  inline int denormalize(ParamValue iNormalizedValue) const override
206  {
207  // ParamValue must remain within its bounds
208  auto value = Utils::clamp(iNormalizedValue, 0.0, 1.0);
209  return static_cast<int>(std::floor(std::min(static_cast<ParamValue>(StepCount),
210  value * (StepCount + 1))));
211  }
212 
213  // toString
214  void toString(ParamType const &iValue, String128 oString, int32 /* iPrecision */) const override
215  {
216  Steinberg::UString wrapper(oString, str16BufferSize (String128));
217  if(fFormat)
218  {
219  String s;
220  s.printf(fFormat, iValue + fToStringOffset);
221  wrapper.assign(s.text());
222  }
223  else
224  {
225  if(fToStringValues.empty())
226  {
227  if(!wrapper.printInt(iValue + fToStringOffset))
228  oString[0] = 0;
229  }
230  else
231  {
232  wrapper.assign(fToStringValues[iValue].text());
233  }
234  }
235  }
236 
237 private:
238  int fToStringOffset{};
239  char16 const *fFormat{};
240  std::vector<ConstString> fToStringValues{};
241 };
242 
247 template<typename Enum, Enum MaxValue>
249 {
250 public:
251  using ParamType = Enum;
252 
254 
255  // Constructor - you can provide an offset for the toString conversion (ex: counting from 1 instead of 0)
256  explicit EnumParamConverter(int iToStringOffset = 0) : fConverter{iToStringOffset} {}
257 
258  // Constructor with printf style format where the parameter (%d) will be (value + offset)
259  explicit EnumParamConverter(char16 const *iFormat, int iToStringOffset = 0) : fConverter{iFormat, iToStringOffset} {}
260 
261  // Constructor with all values defined
262  explicit EnumParamConverter(std::array<ConstString, MaxValue + 1> const &iToStringValues) : fConverter{iToStringValues} {}
263 
264  inline int getStepCount() const override { return fConverter.getStepCount(); }
265 
266  inline ParamValue normalize(ParamType const &iDiscreteValue) const override
267  {
268  return fConverter.normalize(iDiscreteValue);
269  }
270 
271  inline ParamType denormalize(ParamValue iNormalizedValue) const override
272  {
273  return static_cast<ParamType>(fConverter.denormalize(iNormalizedValue));
274  }
275 
276  void toString(ParamType const &iValue, String128 oString, int32 iPrecision) const override
277  {
278  fConverter.toString(iValue, oString, iPrecision);
279  }
280 
281 private:
283 };
284 
285 }
286 }
287 
288 #endif // __PONGASOFT_VST_PARAM_CONVERTERS_H__
Enum ParamType
Definition: ParamConverters.h:251
char16 const * fTrueString
Definition: ParamConverters.h:137
BooleanParamConverter(char16 const *iFalseString=STR16("Off"), char16 const *iTrueString=STR16("On"))
Definition: ParamConverters.h:108
EnumParamConverter(char16 const *iFormat, int iToStringOffset=0)
Definition: ParamConverters.h:259
static T clamp(const U &iValue, const T &iLower, const T &iUpper)
Definition: Misc.h:33
int getStepCount() const override
Definition: ParamConverters.h:114
EnumParamConverter(std::array< ConstString, MaxValue+1 > const &iToStringValues)
Definition: ParamConverters.h:262
Definition: Clock.h:22
virtual void toString(ParamType const &iValue, String128 iString, int32 iPrecision) const
Definition: ParamConverters.h:57
ParamValue normalize(int const &iDiscreteValue) const override
Definition: ParamConverters.h:199
int getStepCount() const override
Definition: ParamConverters.h:197
DiscreteValueParamConverter(int iToStringOffset=0)
Definition: ParamConverters.h:187
ParamValue denormalize(ParamValue iNormalizedValue) const override
Definition: ParamConverters.h:80
Definition: ParamConverters.h:69
double Percent
Definition: ParamConverters.h:143
Definition: ParamConverters.h:149
void toString(ParamType const &iValue, String128 oString, int32 iPrecision) const override
Definition: ParamConverters.h:276
void toString(ParamType const &iValue, String128 oString, int32 iPrecision) const override
Definition: ParamConverters.h:165
Enum ParamType
Definition: ParamConverters.h:53
ParamValue normalize(double const &iValue) const override
Definition: ParamConverters.h:155
void toString(ParamType const &iValue, String128 oString, int32) const override
Definition: ParamConverters.h:214
bool denormalize(ParamValue iNormalizedValue) const override
Definition: ParamConverters.h:121
ParamValue normalize(ParamType const &iDiscreteValue) const override
Definition: ParamConverters.h:266
EnumParamConverter(int iToStringOffset=0)
Definition: ParamConverters.h:256
int denormalize(ParamValue iNormalizedValue) const override
Definition: ParamConverters.h:205
DiscreteValueParamConverter< MaxValue > fConverter
Definition: ParamConverters.h:282
Definition: ParamConverters.h:103
Definition: ParamConverters.h:179
ParamValue normalize(bool const &iValue) const override
Definition: ParamConverters.h:116
virtual std::string toString(ParamType const &iValue, int32 iPrecision) const
Definition: ParamConverters.h:58
char16 const * fFalseString
Definition: ParamConverters.h:136
ParamType denormalize(ParamValue iNormalizedValue) const override
Definition: ParamConverters.h:271
static void staticToString(ParamValue const &iValue, String128 oString, int32 iPrecision)
Definition: ParamConverters.h:90
virtual int getStepCount() const
Definition: ParamConverters.h:54
int getStepCount() const override
Definition: ParamConverters.h:264
ParamValue normalize(ParamValue const &iValue) const override
Definition: ParamConverters.h:75
void toString(ParamValue const &iValue, String128 oString, int32 iPrecision) const override
Definition: ParamConverters.h:85
DiscreteValueParamConverter(std::array< ConstString, StepCount+1 > const &iToStringValues)
Definition: ParamConverters.h:194
double denormalize(ParamValue iNormalizedValue) const override
Definition: ParamConverters.h:160
void toString(bool const &iValue, String128 oString, int32) const override
Definition: ParamConverters.h:126
DiscreteValueParamConverter(char16 const *iFormat, int iToStringOffset=0)
Definition: ParamConverters.h:190
Definition: ParamConverters.h:248
Definition: ParamConverters.h:50