From 302511d3f5b600c45a437f29091357a08c151919 Mon Sep 17 00:00:00 2001 From: Daniel Walz Date: Thu, 6 Jun 2024 18:01:24 +0200 Subject: [PATCH] Snap values for discrete parameters --- Source/TestUtilities.h | 15 +++++++++++++++ Source/tests/BasicTests.cpp | 13 +++++++------ 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/Source/TestUtilities.h b/Source/TestUtilities.h index da3993b..eccd92f 100644 --- a/Source/TestUtilities.h +++ b/Source/TestUtilities.h @@ -53,6 +53,21 @@ static inline juce::Array getNonBypassAutomatabl return parameters; } +//============================================================================== +static inline void randomizeParameter (juce::Random& r, juce::AudioProcessorParameter* parameter) +{ + auto newValue = r.nextFloat(); + + // snap to legal value + if (parameter->isDiscrete()) + { + auto maxValue = static_cast(parameter->getNumSteps() - 1); + newValue = std::round(newValue * maxValue) / maxValue; + } + + parameter->setValue (newValue); +} + //============================================================================== template void iterateAudioBuffer (juce::AudioBuffer& ab, UnaryFunction fn) diff --git a/Source/tests/BasicTests.cpp b/Source/tests/BasicTests.cpp index 4366bd5..919ad9a 100644 --- a/Source/tests/BasicTests.cpp +++ b/Source/tests/BasicTests.cpp @@ -288,7 +288,7 @@ struct PluginStateTest : public PluginTest // Set random parameter values for (auto parameter : getNonBypassAutomatableParameters (instance)) - parameter->setValue (r.nextFloat()); + randomizeParameter (r, parameter); // Restore original state callSetStateInformationOnMessageThreadIfVST3 (instance, originalState); @@ -318,8 +318,9 @@ struct PluginStateTestRestoration : public PluginTest // Set random parameter values for (auto parameter : getNonBypassAutomatableParameters(instance)) { - const auto originalValue = parameter->getValue(); - parameter->setValue(r.nextFloat()); + const auto originalValue = parameter->getValue(); + + randomizeParameter (r, parameter); // Restore original state callSetStateInformationOnMessageThreadIfVST3(instance, originalState); @@ -398,7 +399,7 @@ struct AutomationTest : public PluginTest for (int i = 0; i < juce::jmin (10, parameters.size()); ++i) { const int paramIndex = r.nextInt (parameters.size()); - parameters[paramIndex]->setValue (r.nextFloat()); + randomizeParameter (r, parameters[paramIndex]); } } @@ -461,7 +462,7 @@ struct EditorAutomationTest : public PluginTest while (--numBlocks >= 0) { for (auto parameter : parameters) - parameter->setValue (r.nextFloat()); + randomizeParameter (r, parameter); ut.resetTimeout(); juce::Thread::sleep (10); @@ -589,7 +590,7 @@ struct BackgroundThreadStateTest : public PluginTest // Set random parameter values for (auto parameter : parameters) - parameter->setValue (r.nextFloat()); + randomizeParameter (r, parameter); // Restore original state callSetStateInformationOnMessageThreadIfVST3 (instance, originalState);