From ae215451fce2315a6288cf4de4ecab9ef32b5151 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pedro=20J=2E=20Est=C3=A9banez?= Date: Tue, 2 Feb 2021 11:25:40 +0100 Subject: [PATCH] Make audio bus channels' peak volume consistent Channels that are inactive -or when playback has not started yet- will report -200 dB as their peak value (which is also the lowest value possible during playback). (cherry picked from commit a2b3a73e2d0c2b9e9badeab28fef26565a9ec3f2) --- core/math/audio_frame.h | 3 +++ servers/audio_server.cpp | 7 ++++--- servers/audio_server.h | 2 +- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/core/math/audio_frame.h b/core/math/audio_frame.h index b1f6575f851..73f9bdf4cc7 100644 --- a/core/math/audio_frame.h +++ b/core/math/audio_frame.h @@ -47,6 +47,9 @@ static inline float undenormalise(volatile float f) { return (v.i & 0x7f800000) < 0x08000000 ? 0.0f : f; } +static const float AUDIO_PEAK_OFFSET = 0.0000000001f; +static const float AUDIO_MIN_PEAK_DB = -200.0f; // linear2db(AUDIO_PEAK_OFFSET) + struct AudioFrame { //left and right samples diff --git a/servers/audio_server.cpp b/servers/audio_server.cpp index 8247359c90d..53b67c2ca7f 100644 --- a/servers/audio_server.cpp +++ b/servers/audio_server.cpp @@ -411,9 +411,10 @@ void AudioServer::_mix_step() { } for (int k = 0; k < bus->channels.size(); k++) { - - if (!bus->channels[k].active) + if (!bus->channels[k].active) { + bus->channels.write[k].peak_volume = AudioFrame(AUDIO_MIN_PEAK_DB, AUDIO_MIN_PEAK_DB); continue; + } AudioFrame *buf = bus->channels.write[k].buffer.ptrw(); @@ -446,7 +447,7 @@ void AudioServer::_mix_step() { } } - bus->channels.write[k].peak_volume = AudioFrame(Math::linear2db(peak.l + 0.0000000001), Math::linear2db(peak.r + 0.0000000001)); + bus->channels.write[k].peak_volume = AudioFrame(Math::linear2db(peak.l + AUDIO_PEAK_OFFSET), Math::linear2db(peak.r + AUDIO_PEAK_OFFSET)); if (!bus->channels[k].used) { //see if any audio is contained, because channel was not used diff --git a/servers/audio_server.h b/servers/audio_server.h index 4e6e92d8396..3e9e71b52ad 100644 --- a/servers/audio_server.h +++ b/servers/audio_server.h @@ -204,7 +204,7 @@ private: last_mix_with_audio = 0; used = false; active = false; - peak_volume = AudioFrame(0, 0); + peak_volume = AudioFrame(AUDIO_MIN_PEAK_DB, AUDIO_MIN_PEAK_DB); } };