Browse Source

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).
Pedro J. Estébanez 4 years ago
parent
commit
a2b3a73e2d
3 changed files with 6 additions and 2 deletions
  1. 3 0
      core/math/audio_frame.h
  2. 2 1
      servers/audio_server.cpp
  3. 1 1
      servers/audio_server.h

+ 3 - 0
core/math/audio_frame.h

@@ -47,6 +47,9 @@ static inline float undenormalise(volatile float f) {
 	return (v.i & 0x7f800000) < 0x08000000 ? 0.0f : 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 {
 struct AudioFrame {
 	//left and right samples
 	//left and right samples
 	float l, r;
 	float l, r;

+ 2 - 1
servers/audio_server.cpp

@@ -401,6 +401,7 @@ void AudioServer::_mix_step() {
 
 
 		for (int k = 0; k < bus->channels.size(); k++) {
 		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;
 				continue;
 			}
 			}
 
 
@@ -434,7 +435,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) {
 			if (!bus->channels[k].used) {
 				//see if any audio is contained, because channel was not used
 				//see if any audio is contained, because channel was not used

+ 1 - 1
servers/audio_server.h

@@ -199,7 +199,7 @@ private:
 				last_mix_with_audio = 0;
 				last_mix_with_audio = 0;
 				used = false;
 				used = false;
 				active = false;
 				active = false;
-				peak_volume = AudioFrame(0, 0);
+				peak_volume = AudioFrame(AUDIO_MIN_PEAK_DB, AUDIO_MIN_PEAK_DB);
 			}
 			}
 		};
 		};