]> git.sesse.net Git - nageru/commitdiff
Make some common decibel macros.
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Fri, 29 Jul 2016 17:25:35 +0000 (19:25 +0200)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Wed, 19 Oct 2016 22:55:44 +0000 (00:55 +0200)
audio_mixer.cpp
audio_mixer.h
db.h [new file with mode: 0644]
mixer.cpp

index 069a810e1ad8a705431017e02ebbe2847b23a2cd..764d7a2217fbbdfa6a7122960beb29668b5e207d 100644 (file)
@@ -6,6 +6,7 @@
 #include <stdio.h>
 #include <cmath>
 
+#include "db.h"
 #include "flags.h"
 #include "timebase.h"
 
@@ -167,12 +168,12 @@ vector<float> 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<float> 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<float> 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<float> 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<float> 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
index ef49120dc2b2047b99ed7ff045ec17ad226a137f..f0336d29d57368ae3dbbe247594c24b8817b4b24 100644 (file)
@@ -19,6 +19,7 @@
 #include <vector>
 
 #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<std::mutex> 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<std::mutex> 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 (file)
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 <math.h>
+
+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)
index 1b1e040a88c0142ead695b40468120347484bedb..b684b3d4d0e4dc404973ae4ef08c6c703599c2ac 100644 (file)
--- 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(),