From 69b82ee2a34a7d72c7c5a98eb87deed3b7af28a8 Mon Sep 17 00:00:00 2001 From: WarmUpTill Date: Thu, 9 May 2024 22:08:29 +0200 Subject: [PATCH] Fix audio condition incorrectly reporting very low volume levels OBS might very rarely not update _peak quickly enough when very low intervals are configured on the General tab. In that case _peak might be set to negative infinity still, which will result in unexpected behavior, so we use the previously valid peak value instead. --- plugins/base/macro-condition-audio.cpp | 19 +++++++++++++++++-- plugins/base/macro-condition-audio.hpp | 3 +++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/plugins/base/macro-condition-audio.cpp b/plugins/base/macro-condition-audio.cpp index 3e71c6b7..7c00aeea 100644 --- a/plugins/base/macro-condition-audio.cpp +++ b/plugins/base/macro-condition-audio.cpp @@ -67,7 +67,21 @@ bool MacroConditionAudio::CheckOutputCondition() OBSSourceAutoRelease source = obs_weak_source_get_source(_audioSource.GetSource()); - double curVolume = _useDb ? _peak : DecibelToPercent(_peak) * 100; + // OBS might very rarely not update _peak quickly enough when very low + // intervals are configured on the General tab. + // In that case _peak might be set to negative infinity still, which + // will result in unexpected behavior, so we use the previously valid + // peak value instead. + float peak; + { + std::lock_guard lock(_peakMutex); + peak = _peakUpdated ? _peak : _previousPeak; + _previousPeak = peak; + _peak = -std::numeric_limits::infinity(); + _peakUpdated = false; + } + + double curVolume = _useDb ? peak : DecibelToPercent(peak) * 100; switch (_outputCondition) { case OutputCondition::ABOVE: @@ -92,7 +106,6 @@ bool MacroConditionAudio::CheckOutputCondition() SetTempVarValue("output_volume", std::to_string(curVolume)); // Reset for next check - _peak = -std::numeric_limits::infinity(); if (_audioSource.GetType() == SourceSelection::Type::VARIABLE) { ResetVolmeter(); } @@ -329,11 +342,13 @@ void MacroConditionAudio::SetVolumeLevel(void *data, const float *, return; } + std::lock_guard lock(c->_peakMutex); for (int i = 0; i < MAX_AUDIO_CHANNELS; i++) { if (peak[i] > c->_peak) { c->_peak = peak[i]; } } + c->_peakUpdated = true; } void MacroConditionAudio::ResetVolmeter() diff --git a/plugins/base/macro-condition-audio.hpp b/plugins/base/macro-condition-audio.hpp index 217089a5..dbff654e 100644 --- a/plugins/base/macro-condition-audio.hpp +++ b/plugins/base/macro-condition-audio.hpp @@ -73,7 +73,10 @@ private: void SetupTempVars(); Type _checkType = Type::OUTPUT_VOLUME; + std::mutex _peakMutex; float _peak = -std::numeric_limits::infinity(); + float _previousPeak = -std::numeric_limits::infinity(); + bool _peakUpdated = false; static bool _registered; static const std::string id; };