]> git.sesse.net Git - nageru/blobdiff - audio_mixer.cpp
Added some mux metrics.
[nageru] / audio_mixer.cpp
index e4d95a49422250e44dced2a84f11dc866ac878f6..7da5b0bea3d84db6853ce5616b795382a5db12b9 100644 (file)
@@ -20,6 +20,7 @@
 
 #include "db.h"
 #include "flags.h"
+#include "metrics.h"
 #include "state.pb.h"
 #include "timebase.h"
 
@@ -184,7 +185,18 @@ AudioMixer::AudioMixer(unsigned num_cards)
        set_limiter_enabled(global_flags.limiter_enabled);
        set_final_makeup_gain_auto(global_flags.final_makeup_gain_auto);
 
+       r128.init(2, OUTPUT_FREQUENCY);
+       r128.integr_start();
+
+       // hlen=16 is pretty low quality, but we use quite a bit of CPU otherwise,
+       // and there's a limit to how important the peak meter is.
+       peak_resampler.setup(OUTPUT_FREQUENCY, OUTPUT_FREQUENCY * 4, /*num_channels=*/2, /*hlen=*/16, /*frel=*/1.0);
+
+       global_audio_mixer = this;
+       alsa_pool.init();
+
        if (!global_flags.input_mapping_filename.empty()) {
+               // Must happen after ALSAPool is initialized, as it needs to know the card list.
                current_mapping_mode = MappingMode::MULTICHANNEL;
                InputMapping new_input_mapping;
                if (!load_input_mapping_from_file(get_devices(),
@@ -202,15 +214,13 @@ AudioMixer::AudioMixer(unsigned num_cards)
                }
        }
 
-       r128.init(2, OUTPUT_FREQUENCY);
-       r128.integr_start();
-
-       // hlen=16 is pretty low quality, but we use quite a bit of CPU otherwise,
-       // and there's a limit to how important the peak meter is.
-       peak_resampler.setup(OUTPUT_FREQUENCY, OUTPUT_FREQUENCY * 4, /*num_channels=*/2, /*hlen=*/16, /*frel=*/1.0);
-
-       global_audio_mixer = this;
-       alsa_pool.init();
+       global_metrics.add("audio_loudness_short_lufs", &metric_audio_loudness_short_lufs, Metrics::TYPE_GAUGE);
+       global_metrics.add("audio_loudness_integrated_lufs", &metric_audio_loudness_integrated_lufs, Metrics::TYPE_GAUGE);
+       global_metrics.add("audio_loudness_range_low_lufs", &metric_audio_loudness_range_low_lufs, Metrics::TYPE_GAUGE);
+       global_metrics.add("audio_loudness_range_high_lufs", &metric_audio_loudness_range_high_lufs, Metrics::TYPE_GAUGE);
+       global_metrics.add("audio_peak_dbfs", &metric_audio_peak_dbfs, Metrics::TYPE_GAUGE);
+       global_metrics.add("audio_final_makeup_gain_db", &metric_audio_final_makeup_gain_db, Metrics::TYPE_GAUGE);
+       global_metrics.add("audio_correlation", &metric_audio_correlation, Metrics::TYPE_GAUGE);
 }
 
 void AudioMixer::reset_resampler(DeviceSpec device_spec)
@@ -273,6 +283,12 @@ bool AudioMixer::add_audio(DeviceSpec device_spec, const uint8_t *data, unsigned
                }
        }
 
+       // If we changed frequency since last frame, we'll need to reset the resampler.
+       if (audio_format.sample_rate != device->capture_frequency) {
+               device->capture_frequency = audio_format.sample_rate;
+               reset_resampler_mutex_held(device_spec);
+       }
+
        // Now add it.
        device->resampling_queue->add_input_samples(frame_time, audio.get(), num_samples, ResamplingQueue::ADJUST_RATE);
        return true;
@@ -820,6 +836,14 @@ void AudioMixer::send_audio_level_callback()
        double loudness_range_low = r128.range_min();
        double loudness_range_high = r128.range_max();
 
+       metric_audio_loudness_short_lufs = loudness_s;
+       metric_audio_loudness_integrated_lufs = loudness_i;
+       metric_audio_loudness_range_low_lufs = loudness_range_low;
+       metric_audio_loudness_range_high_lufs = loudness_range_high;
+       metric_audio_peak_dbfs = to_db(peak);
+       metric_audio_final_makeup_gain_db = to_db(final_makeup_gain);
+       metric_audio_correlation = correlation.get_correlation();
+
        vector<BusLevel> bus_levels;
        bus_levels.resize(input_mapping.buses.size());
        {