From daecb8caa604264b015633485597408dcfdc3223 Mon Sep 17 00:00:00 2001 From: Andrew Belt Date: Thu, 28 Jun 2018 20:29:25 -0400 Subject: [PATCH] Make parameter smoothing thread-safe and lock-free --- src/engine.cpp | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/src/engine.cpp b/src/engine.cpp index f32ecca3..8a557256 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -92,18 +92,23 @@ static void engineStep() { } // Param smoothing - if (smoothModule) { - float value = smoothModule->params[smoothParamId].value; - const float lambda = 60.0; // decay rate is 1 graphics frame - float delta = smoothValue - value; - float newValue = value + delta * lambda * sampleTime; - if (value == newValue) { - // Snap to actual smooth value if the value doesn't change enough (due to the granularity of floats) - smoothModule->params[smoothParamId].value = smoothValue; - smoothModule = NULL; - } - else { - smoothModule->params[smoothParamId].value = newValue; + { + Module *localSmoothModule = smoothModule; + int localSmoothParamId = smoothParamId; + float localSmoothValue = smoothValue; + if (localSmoothModule) { + float value = localSmoothModule->params[localSmoothParamId].value; + const float lambda = 60.0; // decay rate is 1 graphics frame + float delta = localSmoothValue - value; + float newValue = value + delta * lambda * sampleTime; + if (value == newValue) { + // Snap to actual smooth value if the value doesn't change enough (due to the granularity of floats) + localSmoothModule->params[localSmoothParamId].value = localSmoothValue; + smoothModule = NULL; + } + else { + localSmoothModule->params[localSmoothParamId].value = newValue; + } } }