Skip to content

Commit

Permalink
Add float round utilities, use it in vst3
Browse files Browse the repository at this point in the history
Signed-off-by: falkTX <falktx@falktx.com>
  • Loading branch information
falkTX committed Jan 12, 2024
1 parent 1504e7d commit 73ce285
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 10 deletions.
33 changes: 33 additions & 0 deletions distrho/DistrhoUtils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 F>
static inline constexpr
int32_t d_roundToIntPositive(const T& value)
{
return static_cast<int32_t>(value + static_cast<F>(0.5));
}

/**
Round a floating point number to integer.
Fast operation for values known to be 0 or negative.
*/
template<typename F>
static inline constexpr
int32_t d_roundToIntNegative(const T& value)
{
return static_cast<int32_t>(value - static_cast<F>(0.5));
}

/**
Round a floating point number to integer.
*/
template<typename F>
static inline constexpr
int32_t d_roundToInt(const T& value)
{
return value >= 0 ? static_cast<int32_t>(value + static_cast<F>(0.5))
: static_cast<int32_t>(value - static_cast<F>(0.5));
}

/** @} */

/* --------------------------------------------------------------------------------------------------------------------
Expand Down
19 changes: 9 additions & 10 deletions distrho/src/DistrhoPluginVST3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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?)
Expand Down Expand Up @@ -721,9 +720,9 @@ class PluginVst3
}
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;
Expand Down Expand Up @@ -1236,7 +1235,7 @@ class PluginVst3
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";
Expand Down Expand Up @@ -1420,7 +1419,7 @@ class PluginVst3

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;
Expand Down Expand Up @@ -1782,20 +1781,20 @@ class PluginVst3
{
#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
Expand All @@ -1805,7 +1804,7 @@ class PluginVst3
#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
Expand Down

0 comments on commit 73ce285

Please sign in to comment.