18 #ifndef __PONGASOFT_VST_PARAM_CONVERTERS_H__ 19 #define __PONGASOFT_VST_PARAM_CONVERTERS_H__ 24 #include <pluginterfaces/vst/vsttypes.h> 25 #include <pluginterfaces/vst/ivstparameterchanges.h> 26 #include <pluginterfaces/base/ustring.h> 27 #include <base/source/fstring.h> 28 #include <pluginterfaces/base/ftypes.h> 36 #include <type_traits> 41 using namespace Steinberg;
42 using namespace Steinberg::Vst;
57 virtual ParamValue normalize(ParamType
const &iValue)
const = 0;
58 virtual ParamType denormalize(ParamValue iNormalizedValue)
const = 0;
59 virtual void toString(
ParamType const &iValue, String128 iString, int32 iPrecision)
const { }
63 toString(iValue, s, iPrecision);
64 return String(s).text8();
77 inline ParamValue
normalize(ParamValue
const &iValue)
const override 82 inline ParamValue
denormalize(ParamValue iNormalizedValue)
const override 87 inline void toString(ParamValue
const &iValue, String128 oString, int32 iPrecision)
const override 89 staticToString(iValue, oString, iPrecision);
92 static inline void staticToString(ParamValue
const &iValue, String128 oString, int32 iPrecision)
94 Steinberg::UString wrapper(oString, str16BufferSize(String128));
95 if(!wrapper.printFloat(iValue, iPrecision))
112 fFalseString{std::move(iFalseString)},
113 fTrueString{std::move(iTrueString)}
118 inline ParamValue
normalize(
bool const &iValue)
const override 120 return iValue ? 1.0 : 0;
123 inline bool denormalize(ParamValue iNormalizedValue)
const override 125 return toBoolean(iNormalizedValue);
128 inline void toString(
bool const &iValue, String128 oString, int32 )
const override 130 Steinberg::UString wrapper(oString, str16BufferSize(String128));
132 wrapper.assign(fTrueString.c_str());
134 wrapper.assign(fFalseString.c_str());
140 inline static bool toBoolean(ParamValue iNormalizedValue) {
return iNormalizedValue >= 0.5; }
162 inline ParamValue
normalize(
double const &iValue)
const override 167 inline double denormalize(ParamValue iNormalizedValue)
const override 172 inline void toString(
ParamType const &iValue, String128 oString, int32 iPrecision)
const override 174 Steinberg::UString wrapper(oString, str16BufferSize (String128));
175 wrapper.printFloat(iValue * 100, iPrecision);
176 wrapper.append(STR16(
"%"));
184 template<
typename IntType =
int>
187 auto value = Utils::clamp<int32, int32>(iDiscreteValue, 0, iStepCount);
191 return value / static_cast<ParamValue>(iStepCount);
198 template<
typename IntType =
int>
203 auto discreteValue = std::floor(std::min(static_cast<ParamValue>(iStepCount), value * (iStepCount + 1)));
204 return static_cast<IntType>(discreteValue);
212 template<
int32 StepCount,
typename IntType =
int>
225 fToStringOffset{iToStringOffset}, fFormat{std::move(iFormat)} {}
229 fToStringValues(iToStringValues.cbegin(), iToStringValues.cend()) {}
235 return convertDiscreteValueToNormalizedValue<IntType>(StepCount, iDiscreteValue);
240 return convertNormalizedValueToDiscreteValue<IntType>(StepCount, iNormalizedValue);
246 Steinberg::UString wrapper(oString, str16BufferSize (String128));
250 s.printf(fFormat.c_str(), iValue + fToStringOffset);
251 wrapper.assign(s.text());
255 if(fToStringValues.empty())
257 if(!wrapper.printInt(iValue + fToStringOffset))
262 wrapper.assign(fToStringValues[iValue].c_str());
268 IntType fToStringOffset{};
270 std::vector<VstString16> fToStringValues{};
277 template<
typename Enum, Enum MaxValue>
294 explicit EnumParamConverter(std::array<VstString16, MaxValue + 1>
const &iToStringValues) : fConverter{iToStringValues} {}
300 return fConverter.normalize(static_cast<IntType>(iDiscreteValue));
305 return static_cast<Enum>(fConverter.denormalize(iNormalizedValue));
310 fConverter.toString(static_cast<IntType>(iValue), oString, iPrecision);
320 #endif // __PONGASOFT_VST_PARAM_CONVERTERS_H__ Enum ParamType
Definition: ParamConverters.h:281
DiscreteValueParamConverter(VstString16 iFormat, IntType iToStringOffset=0)
Definition: ParamConverters.h:224
static bool toBoolean(ParamValue iNormalizedValue)
Definition: ParamConverters.h:140
static T clamp(const U &iValue, const T &iLower, const T &iUpper)
Definition: Misc.h:33
EnumParamConverter(VstString16 iFormat, IntType iToStringOffset=0)
Definition: ParamConverters.h:291
static ParamValue convertDiscreteValueToNormalizedValue(int32 iStepCount, IntType iDiscreteValue)
Definition: ParamConverters.h:185
virtual void toString(ParamType const &iValue, String128 iString, int32 iPrecision) const
Definition: ParamConverters.h:59
VstString16 fTrueString
Definition: ParamConverters.h:144
ParamValue normalize(ParamType const &iDiscreteValue) const override
Definition: ParamConverters.h:233
std::basic_string< Steinberg::char16 > VstString16
Definition: Types.h:37
VstString16 fFalseString
Definition: ParamConverters.h:143
BooleanParamConverter(VstString16 iFalseString=STR16("Off"), VstString16 iTrueString=STR16("On"))
Definition: ParamConverters.h:110
ParamValue denormalize(ParamValue iNormalizedValue) const override
Definition: ParamConverters.h:82
int32 getStepCount() const override
Definition: ParamConverters.h:116
Definition: ParamConverters.h:71
double Percent
Definition: ParamConverters.h:150
Definition: ParamConverters.h:156
void toString(ParamType const &iValue, String128 oString, int32 iPrecision) const override
Definition: ParamConverters.h:308
DiscreteValueParamConverter< MaxValue, IntType > fConverter
Definition: ParamConverters.h:314
void toString(ParamType const &iValue, String128 oString, int32 iPrecision) const override
Definition: ParamConverters.h:172
Enum ParamType
Definition: ParamConverters.h:55
ParamType denormalize(ParamValue iNormalizedValue) const override
Definition: ParamConverters.h:238
void toString(ParamType const &iValue, String128 oString, int32) const override
Definition: ParamConverters.h:244
ParamValue normalize(double const &iValue) const override
Definition: ParamConverters.h:162
EnumParamConverter(std::array< VstString16, MaxValue+1 > const &iToStringValues)
Definition: ParamConverters.h:294
bool denormalize(ParamValue iNormalizedValue) const override
Definition: ParamConverters.h:123
ParamValue normalize(ParamType const &iDiscreteValue) const override
Definition: ParamConverters.h:298
int32 getStepCount() const override
Definition: ParamConverters.h:231
int32 getStepCount() const override
Definition: ParamConverters.h:296
DiscreteValueParamConverter(IntType iToStringOffset=0)
Definition: ParamConverters.h:221
DiscreteValueParamConverter(std::array< VstString16, StepCount+1 > const &iToStringValues)
Definition: ParamConverters.h:228
Definition: ParamConverters.h:105
Definition: ParamConverters.h:213
ParamValue normalize(bool const &iValue) const override
Definition: ParamConverters.h:118
virtual std::string toString(ParamType const &iValue, int32 iPrecision) const
Definition: ParamConverters.h:60
ParamType denormalize(ParamValue iNormalizedValue) const override
Definition: ParamConverters.h:303
EnumParamConverter(IntType iToStringOffset=0)
Definition: ParamConverters.h:288
static void staticToString(ParamValue const &iValue, String128 oString, int32 iPrecision)
Definition: ParamConverters.h:92
ParamValue normalize(ParamValue const &iValue) const override
Definition: ParamConverters.h:77
void toString(ParamValue const &iValue, String128 oString, int32 iPrecision) const override
Definition: ParamConverters.h:87
double denormalize(ParamValue iNormalizedValue) const override
Definition: ParamConverters.h:167
void toString(bool const &iValue, String128 oString, int32) const override
Definition: ParamConverters.h:128
virtual int32 getStepCount() const
Definition: ParamConverters.h:56
static IntType convertNormalizedValueToDiscreteValue(int32 iStepCount, ParamValue iNormalizedValue)
Definition: ParamConverters.h:199
std::underlying_type_t< Enum > IntType
Definition: ParamConverters.h:283
Definition: ParamConverters.h:278
Definition: ParamConverters.h:52