Browse Source

Add float round utilities, use it in vst3

Signed-off-by: falkTX <falktx@falktx.com>
pull/448/head
falkTX 2 years ago
parent
commit
e35376a226
Signed by: falkTX <falktx@falktx.com> GPG Key ID: CDBAA37ABC74FBA0
2 changed files with 42 additions and 10 deletions
  1. +33
    -0
      distrho/DistrhoUtils.hpp
  2. +9
    -10
      distrho/src/DistrhoPluginVST3.cpp

+ 33
- 0
distrho/DistrhoUtils.hpp View File

@@ -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));
}

/** @} */

/* --------------------------------------------------------------------------------------------------------------------


+ 9
- 10
distrho/src/DistrhoPluginVST3.cpp View File

@@ -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


Loading…
Cancel
Save