]> git.sesse.net Git - nageru/blobdiff - audio_mixer.cpp
Remove a FIXME; practical loopback testing showed this was right.
[nageru] / audio_mixer.cpp
index 46ae44c6f74e73a1f432b712e76008aafdb92394..01fed687b47a8d2ae77a752896f925d11c176c37 100644 (file)
@@ -1,19 +1,25 @@
 #include "audio_mixer.h"
 
 #include <assert.h>
-#include <endian.h>
 #include <bmusb/bmusb.h>
-#include <stdio.h>
 #include <endian.h>
-#include <cmath>
-#include <limits>
-#ifdef __SSE__
+#include <math.h>
+#ifdef __SSE2__
 #include <immintrin.h>
 #endif
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <algorithm>
+#include <chrono>
+#include <cmath>
+#include <cstddef>
+#include <limits>
+#include <utility>
 
 #include "db.h"
 #include "flags.h"
-#include "mixer.h"
 #include "state.pb.h"
 #include "timebase.h"
 
@@ -164,8 +170,6 @@ AudioMixer::AudioMixer(unsigned num_cards)
          limiter(OUTPUT_FREQUENCY),
          correlation(OUTPUT_FREQUENCY)
 {
-       global_audio_mixer = this;
-
        for (unsigned bus_index = 0; bus_index < MAX_BUSES; ++bus_index) {
                locut[bus_index].init(FILTER_HPF, 2);
                eq[bus_index][EQ_BAND_BASS].init(FILTER_LOW_SHELF, 1);
@@ -178,7 +182,6 @@ AudioMixer::AudioMixer(unsigned num_cards)
        }
        set_limiter_enabled(global_flags.limiter_enabled);
        set_final_makeup_gain_auto(global_flags.final_makeup_gain_auto);
-       alsa_pool.init();
 
        if (!global_flags.input_mapping_filename.empty()) {
                current_mapping_mode = MappingMode::MULTICHANNEL;
@@ -204,6 +207,9 @@ AudioMixer::AudioMixer(unsigned num_cards)
        // 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();
 }
 
 void AudioMixer::reset_resampler(DeviceSpec device_spec)
@@ -221,7 +227,9 @@ void AudioMixer::reset_resampler_mutex_held(DeviceSpec device_spec)
        } else {
                // TODO: ResamplingQueue should probably take the full device spec.
                // (It's only used for console output, though.)
-               device->resampling_queue.reset(new ResamplingQueue(device_spec.index, device->capture_frequency, OUTPUT_FREQUENCY, device->interesting_channels.size()));
+               device->resampling_queue.reset(new ResamplingQueue(
+                       device_spec.index, device->capture_frequency, OUTPUT_FREQUENCY, device->interesting_channels.size(),
+                       global_flags.audio_queue_length_ms * 0.001));
        }
        device->next_local_pts = 0;
 }
@@ -577,13 +585,12 @@ vector<float> AudioMixer::get_output(double pts, unsigned num_samples, Resamplin
        // (half-time of 30 seconds).
        double target_loudness_factor, alpha;
        double loudness_lu = r128.loudness_M() - ref_level_lufs;
-       double current_makeup_lu = to_db(final_makeup_gain);
        target_loudness_factor = final_makeup_gain * from_db(-loudness_lu);
 
-       // If we're outside +/- 5 LU uncorrected, we don't count it as
+       // If we're outside +/- 5 LU (after correction), we don't count it as
        // a normal signal (probably silence) and don't change the
        // correction factor; just apply what we already have.
-       if (fabs(loudness_lu - current_makeup_lu) >= 5.0 || !final_makeup_gain_auto) {
+       if (fabs(loudness_lu) >= 5.0 || !final_makeup_gain_auto) {
                alpha = 0.0;
        } else {
                // Formula adapted from