From 149ecac9d3d8fc923ecb008b6749a82670907744 Mon Sep 17 00:00:00 2001 From: Andrew Belt Date: Thu, 26 Oct 2017 10:19:43 -0400 Subject: [PATCH] Added engineGetSampleTime() --- include/engine.hpp | 2 ++ src/engine.cpp | 13 +++++++++---- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/include/engine.hpp b/include/engine.hpp index c5a14354..1496f0d6 100644 --- a/include/engine.hpp +++ b/include/engine.hpp @@ -97,6 +97,8 @@ void engineSetParam(Module *module, int paramId, float value); void engineSetParamSmooth(Module *module, int paramId, float value); void engineSetSampleRate(float sampleRate); float engineGetSampleRate(); +/** Returns the inverse of the current sample rate */ +float engineGetSampleTime(); extern bool gPaused; diff --git a/src/engine.cpp b/src/engine.cpp index e064947b..bf903cda 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -14,7 +14,8 @@ namespace rack { -float sampleRate = 0.0; +float sampleRate; +float sampleTime; bool gPaused = false; @@ -38,7 +39,7 @@ float Light::getBrightness() { } void Light::setBrightnessSmooth(float brightness) { - value += (brightness * brightness - value) / sampleRate * 60.0; + value += (brightness * brightness - value) * sampleTime * 60.0; } @@ -70,7 +71,7 @@ static void engineStep() { smoothModule = NULL; } else { - value += delta * lambda / sampleRate; + value += delta * lambda * sampleTime; smoothModule->params[smoothParamId].value = value; } } @@ -107,7 +108,7 @@ static void engineRun() { } } - double stepTime = mutexSteps / sampleRate; + double stepTime = mutexSteps * sampleTime; ahead += stepTime; auto currTime = std::chrono::high_resolution_clock::now(); const double aheadFactor = 2.0; @@ -232,6 +233,7 @@ void engineSetSampleRate(float newSampleRate) { VIPLock vipLock(vipMutex); std::lock_guard lock(mutex); sampleRate = newSampleRate; + sampleTime = 1.0 / sampleRate; // onSampleRateChange for (Module *module : modules) { module->onSampleRateChange(); @@ -242,5 +244,8 @@ float engineGetSampleRate() { return sampleRate; } +float engineGetSampleTime() { + return sampleTime; +} } // namespace rack