commit e35376a2268bb7f1e08b141b5420f48b2ded6d51
parent 1504e7d327bfe0eac6a889cecd199c963d35532f
Author: falkTX <falktx@falktx.com>
Date: Fri, 12 Jan 2024 16:17:13 +0100
Add float round utilities, use it in vst3
Signed-off-by: falkTX <falktx@falktx.com>
Diffstat:
2 files changed, 42 insertions(+), 10 deletions(-)
diff --git a/distrho/DistrhoUtils.hpp b/distrho/DistrhoUtils.hpp
@@ -309,6 +309,39 @@ uint32_t d_nextPowerOf2(uint32_t size) noexcept
return ++size;
}
+/**
+ Round a floating point number to integer.
+ Fast operation for values known to be 0 or positive.
+ */
+template<typename T>
+static inline constexpr
+int32_t d_roundToIntPositive(const T& value)
+{
+ return static_cast<int32_t>(value + static_cast<T>(0.5));
+}
+
+/**
+ Round a floating point number to integer.
+ Fast operation for values known to be 0 or negative.
+ */
+template<typename T>
+static inline constexpr
+int32_t d_roundToIntNegative(const T& value)
+{
+ return static_cast<int32_t>(value - static_cast<T>(0.5));
+}
+
+/**
+ Round a floating point number to integer.
+ */
+template<typename T>
+static inline constexpr
+int32_t d_roundToInt(const T& value)
+{
+ return value >= 0 ? static_cast<int32_t>(value + static_cast<T>(0.5))
+ : static_cast<int32_t>(value - static_cast<T>(0.5));
+}
+
/** @} */
/* --------------------------------------------------------------------------------------------------------------------
diff --git a/distrho/src/DistrhoPluginVST3.cpp b/distrho/src/DistrhoPluginVST3.cpp
@@ -21,7 +21,6 @@
* - parameter groups via unit ids
* - test parameter changes from DSP (aka requestParameterValueChange)
* - implement getParameterNormalized/setParameterNormalized for MIDI CC params ?
- * - float to int safe casting
* - verify that latency changes works (with and without DPF_VST3_USES_SEPARATE_CONTROLLER)
* == MIDI
* - MIDI CC changes (need to store value to give to the host?)
@@ -721,9 +720,9 @@ public:
}
else if (hints & kParameterIsInteger)
{
- const int ivalue = static_cast<int>(std::round(value));
+ const int ivalue = d_roundToInt(value);
- if (static_cast<int>(fCachedParameterValues[kVst3InternalParameterBaseCount + index]) == ivalue)
+ if (d_roundToInt(fCachedParameterValues[kVst3InternalParameterBaseCount + index]) == ivalue)
return;
value = ivalue;
@@ -1236,7 +1235,7 @@ public:
tmpStr = fPlugin.getParameterSymbol(i);
tmpStr += "\xff";
if (fPlugin.getParameterHints(i) & kParameterIsInteger)
- tmpStr += String(static_cast<int>(std::round(fPlugin.getParameterValue(i))));
+ tmpStr += String(d_roundToInt(fPlugin.getParameterValue(i)));
else
tmpStr += String(fPlugin.getParameterValue(i));
tmpStr += "\xff";
@@ -1420,7 +1419,7 @@ public:
fTimePosition.bbt.valid = true;
fTimePosition.bbt.bar = static_cast<int32_t>(ppqPos) / ppqPerBar + 1;
- fTimePosition.bbt.beat = static_cast<int32_t>(barBeats - rest + 0.5) + 1;
+ fTimePosition.bbt.beat = d_roundToIntPositive<int32_t>(barBeats - rest) + 1;
fTimePosition.bbt.tick = rest * fTimePosition.bbt.ticksPerBeat;
fTimePosition.bbt.beatsPerBar = ctx->time_sig_numerator;
fTimePosition.bbt.beatType = ctx->time_sig_denom;
@@ -1782,20 +1781,20 @@ public:
{
#if DPF_VST3_USES_SEPARATE_CONTROLLER
case kVst3InternalParameterBufferSize:
- snprintf_i32_utf16(output, static_cast<int>(normalized * DPF_VST3_MAX_BUFFER_SIZE + 0.5), 128);
+ snprintf_i32_utf16(output, d_roundToIntPositive(normalized * DPF_VST3_MAX_BUFFER_SIZE), 128);
return V3_OK;
case kVst3InternalParameterSampleRate:
- snprintf_f32_utf16(output, std::round(normalized * DPF_VST3_MAX_SAMPLE_RATE), 128);
+ snprintf_i32_utf16(output, d_roundToIntPositive(normalized * DPF_VST3_MAX_SAMPLE_RATE), 128);
return V3_OK;
#endif
#if DISTRHO_PLUGIN_WANT_LATENCY
case kVst3InternalParameterLatency:
- snprintf_f32_utf16(output, std::round(normalized * DPF_VST3_MAX_LATENCY), 128);
+ snprintf_i32_utf16(output, d_roundToIntPositive(normalized * DPF_VST3_MAX_LATENCY), 128);
return V3_OK;
#endif
#if DISTRHO_PLUGIN_WANT_PROGRAMS
case kVst3InternalParameterProgram:
- const uint32_t program = std::round(normalized * fProgramCountMinusOne);
+ const uint32_t program = d_roundToIntPositive(normalized * fProgramCountMinusOne);
strncpy_utf16(output, fPlugin.getProgramName(program), 128);
return V3_OK;
#endif
@@ -1805,7 +1804,7 @@ public:
#if DISTRHO_PLUGIN_WANT_MIDI_INPUT
if (rindex < kVst3InternalParameterCount)
{
- snprintf_f32_utf16(output, std::round(normalized * 127), 128);
+ snprintf_i32_utf16(output, d_roundToIntPositive(normalized * 127), 128);
return V3_OK;
}
#endif