From: Steinar H. Gunderson Date: Fri, 29 Jul 2016 17:25:35 +0000 (+0200) Subject: Make some common decibel macros. X-Git-Tag: 1.4.0~126 X-Git-Url: https://git.sesse.net/?a=commitdiff_plain;h=2a30d1f3a428511dab64587bd7615bc528a2573b;p=nageru Make some common decibel macros. --- diff --git a/audio_mixer.cpp b/audio_mixer.cpp index 069a810..764d7a2 100644 --- a/audio_mixer.cpp +++ b/audio_mixer.cpp @@ -6,6 +6,7 @@ #include #include +#include "db.h" #include "flags.h" #include "timebase.h" @@ -167,12 +168,12 @@ vector AudioMixer::get_output(double pts, unsigned num_samples, Resamplin float ratio = 20.0f; float attack_time = 0.5f; float release_time = 20.0f; - float makeup_gain = pow(10.0f, (ref_level_dbfs - (-40.0f)) / 20.0f); // +26 dB. + float makeup_gain = from_db(ref_level_dbfs - (-40.0f)); // +26 dB. level_compressor.process(samples_out.data(), samples_out.size() / 2, threshold, ratio, attack_time, release_time, makeup_gain); - gain_staging_db = 20.0 * log10(level_compressor.get_attenuation() * makeup_gain); + gain_staging_db = to_db(level_compressor.get_attenuation() * makeup_gain); } else { // Just apply the gain we already had. - float g = pow(10.0f, gain_staging_db / 20.0f); + float g = from_db(gain_staging_db); for (size_t i = 0; i < samples_out.size(); ++i) { samples_out[i] *= g; } @@ -181,16 +182,16 @@ vector AudioMixer::get_output(double pts, unsigned num_samples, Resamplin #if 0 printf("level=%f (%+5.2f dBFS) attenuation=%f (%+5.2f dB) end_result=%+5.2f dB\n", - level_compressor.get_level(), 20.0 * log10(level_compressor.get_level()), - level_compressor.get_attenuation(), 20.0 * log10(level_compressor.get_attenuation()), - 20.0 * log10(level_compressor.get_level() * level_compressor.get_attenuation() * makeup_gain)); + level_compressor.get_level(), to_db(level_compressor.get_level()), + level_compressor.get_attenuation(), to_db(level_compressor.get_attenuation()), + to_db(level_compressor.get_level() * level_compressor.get_attenuation() * makeup_gain)); #endif // float limiter_att, compressor_att; // The real compressor. if (compressor_enabled) { - float threshold = pow(10.0f, compressor_threshold_dbfs / 20.0f); + float threshold = from_db(compressor_threshold_dbfs); float ratio = 20.0f; float attack_time = 0.005f; float release_time = 0.040f; @@ -202,7 +203,7 @@ vector AudioMixer::get_output(double pts, unsigned num_samples, Resamplin // Finally a limiter at -4 dB (so, -10 dBFS) to take out the worst peaks only. // Note that since ratio is not infinite, we could go slightly higher than this. if (limiter_enabled) { - float threshold = pow(10.0f, limiter_threshold_dbfs / 20.0f); + float threshold = from_db(limiter_threshold_dbfs); float ratio = 30.0f; float attack_time = 0.0f; // Instant. float release_time = 0.020f; @@ -211,7 +212,7 @@ vector AudioMixer::get_output(double pts, unsigned num_samples, Resamplin // limiter_att = limiter.get_attenuation(); } - // printf("limiter=%+5.1f compressor=%+5.1f\n", 20.0*log10(limiter_att), 20.0*log10(compressor_att)); + // printf("limiter=%+5.1f compressor=%+5.1f\n", to_db(limiter_att), to_db(compressor_att)); } // At this point, we are most likely close to +0 LU, but all of our @@ -227,8 +228,8 @@ vector AudioMixer::get_output(double pts, unsigned num_samples, Resamplin // (half-time of 100 seconds). double target_loudness_factor, alpha; double loudness_lu = loudness_lufs - ref_level_lufs; - double current_makeup_lu = 20.0f * log10(final_makeup_gain); - target_loudness_factor = pow(10.0f, -loudness_lu / 20.0f); + double current_makeup_lu = to_db(final_makeup_gain); + target_loudness_factor = from_db(-loudness_lu); // If we're outside +/- 5 LU uncorrected, we don't count it as // a normal signal (probably silence) and don't change the diff --git a/audio_mixer.h b/audio_mixer.h index ef49120..f0336d2 100644 --- a/audio_mixer.h +++ b/audio_mixer.h @@ -19,6 +19,7 @@ #include #include "bmusb/bmusb.h" +#include "db.h" #include "defs.h" #include "filter.h" #include "resampling_queue.h" @@ -125,13 +126,13 @@ public: { std::unique_lock lock(compressor_mutex); final_makeup_gain_auto = false; - final_makeup_gain = pow(10.0f, gain_db / 20.0f); + final_makeup_gain = from_db(gain_db); } float get_final_makeup_gain_db() { std::unique_lock lock(compressor_mutex); - return 20.0 * log10(final_makeup_gain); + return to_db(final_makeup_gain); } void set_final_makeup_gain_auto(bool enabled) diff --git a/db.h b/db.h new file mode 100644 index 0000000..53261ab --- /dev/null +++ b/db.h @@ -0,0 +1,11 @@ +#ifndef _DB_H +#define _DB_H 1 + +// Utility routines for working with decibels. + +#include + +static inline double from_db(double db) { return pow(10.0, db / 20.0); } +static inline double to_db(double val) { return 20.0 * log10(val); } + +#endif // !defined(_DB_H) diff --git a/mixer.cpp b/mixer.cpp index 1b1e040..b684b3d 100644 --- a/mixer.cpp +++ b/mixer.cpp @@ -33,6 +33,7 @@ #include "bmusb/bmusb.h" #include "bmusb/fake_capture.h" #include "context.h" +#include "db.h" #include "decklink_capture.h" #include "defs.h" #include "disk_space_estimator.h" @@ -876,7 +877,7 @@ void Mixer::send_audio_level_callback() double loudness_range_low = r128.range_min(); double loudness_range_high = r128.range_max(); - audio_level_callback(loudness_s, 20.0 * log10(peak), + audio_level_callback(loudness_s, to_db(peak), loudness_i, loudness_range_low, loudness_range_high, audio_mixer.get_gain_staging_db(), audio_mixer.get_final_makeup_gain_db(),