tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

gain_applier_unittest.cc (3595B)


      1 /*
      2 *  Copyright (c) 2018 The WebRTC project authors. All Rights Reserved.
      3 *
      4 *  Use of this source code is governed by a BSD-style license
      5 *  that can be found in the LICENSE file in the root of the source
      6 *  tree. An additional intellectual property rights grant can be found
      7 *  in the file PATENTS.  All contributing project authors may
      8 *  be found in the AUTHORS file in the root of the source tree.
      9 */
     10 
     11 #include "modules/audio_processing/agc2/gain_applier.h"
     12 
     13 #include <algorithm>
     14 #include <cmath>
     15 #include <cstddef>
     16 #include <cstdint>
     17 #include <limits>
     18 
     19 #include "api/audio/audio_view.h"
     20 #include "modules/audio_processing/agc2/vector_float_frame.h"
     21 #include "test/gtest.h"
     22 
     23 namespace webrtc {
     24 TEST(AutomaticGainController2GainApplier, InitialGainIsRespected) {
     25  constexpr float initial_signal_level = 123.f;
     26  constexpr float gain_factor = 10.f;
     27  VectorFloatFrame fake_audio(1, 1, initial_signal_level);
     28  GainApplier gain_applier(true, gain_factor);
     29 
     30  auto fake_view = fake_audio.view();
     31  gain_applier.ApplyGain(fake_audio.view());
     32  EXPECT_NEAR(fake_view[0][0], initial_signal_level * gain_factor, 0.1f);
     33 }
     34 
     35 TEST(AutomaticGainController2GainApplier, ClippingIsDone) {
     36  constexpr float initial_signal_level = 30000.f;
     37  constexpr float gain_factor = 10.f;
     38  VectorFloatFrame fake_audio(1, 1, initial_signal_level);
     39  GainApplier gain_applier(true, gain_factor);
     40 
     41  gain_applier.ApplyGain(fake_audio.view());
     42  EXPECT_NEAR(fake_audio.view()[0][0], std::numeric_limits<int16_t>::max(),
     43              0.1f);
     44 }
     45 
     46 TEST(AutomaticGainController2GainApplier, ClippingIsNotDone) {
     47  constexpr float initial_signal_level = 30000.f;
     48  constexpr float gain_factor = 10.f;
     49  VectorFloatFrame fake_audio(1, 1, initial_signal_level);
     50  GainApplier gain_applier(false, gain_factor);
     51 
     52  gain_applier.ApplyGain(fake_audio.view());
     53 
     54  EXPECT_NEAR(fake_audio.view()[0][0], initial_signal_level * gain_factor,
     55              0.1f);
     56 }
     57 
     58 TEST(AutomaticGainController2GainApplier, RampingIsDone) {
     59  constexpr float initial_signal_level = 30000.f;
     60  constexpr float initial_gain_factor = 1.f;
     61  constexpr float target_gain_factor = 0.5f;
     62  constexpr int num_channels = 3;
     63  constexpr int samples_per_channel = 4;
     64  VectorFloatFrame fake_audio(num_channels, samples_per_channel,
     65                              initial_signal_level);
     66  GainApplier gain_applier(false, initial_gain_factor);
     67 
     68  gain_applier.SetGainFactor(target_gain_factor);
     69  gain_applier.ApplyGain(fake_audio.view());
     70 
     71  // The maximal gain change should be close to that in linear interpolation.
     72  for (size_t channel = 0; channel < num_channels; ++channel) {
     73    float max_signal_change = 0.f;
     74    float last_signal_level = initial_signal_level;
     75    for (const auto sample : fake_audio.view()[channel]) {
     76      const float current_change = fabs(last_signal_level - sample);
     77      max_signal_change = std::max(max_signal_change, current_change);
     78      last_signal_level = sample;
     79    }
     80    const float total_gain_change =
     81        fabs((initial_gain_factor - target_gain_factor) * initial_signal_level);
     82    EXPECT_NEAR(max_signal_change, total_gain_change / samples_per_channel,
     83                0.1f);
     84  }
     85 
     86  // Next frame should have the desired level.
     87  VectorFloatFrame next_fake_audio_frame(num_channels, samples_per_channel,
     88                                         initial_signal_level);
     89  gain_applier.ApplyGain(next_fake_audio_frame.view());
     90 
     91  // The last sample should have the new gain.
     92  EXPECT_NEAR(next_fake_audio_frame.view()[0][0],
     93              initial_signal_level * target_gain_factor, 0.1f);
     94 }
     95 }  // namespace webrtc