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.
This commit is contained in:
WarmUpTill 2024-05-09 22:08:29 +02:00 committed by WarmUpTill
parent f14e6bf252
commit 69b82ee2a3
2 changed files with 20 additions and 2 deletions

View File

@ -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<std::mutex> lock(_peakMutex);
peak = _peakUpdated ? _peak : _previousPeak;
_previousPeak = peak;
_peak = -std::numeric_limits<float>::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<float>::infinity();
if (_audioSource.GetType() == SourceSelection::Type::VARIABLE) {
ResetVolmeter();
}
@ -329,11 +342,13 @@ void MacroConditionAudio::SetVolumeLevel(void *data, const float *,
return;
}
std::lock_guard<std::mutex> 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()

View File

@ -73,7 +73,10 @@ private:
void SetupTempVars();
Type _checkType = Type::OUTPUT_VOLUME;
std::mutex _peakMutex;
float _peak = -std::numeric_limits<float>::infinity();
float _previousPeak = -std::numeric_limits<float>::infinity();
bool _peakUpdated = false;
static bool _registered;
static const std::string id;
};