tor-browser

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

suppression_gain_unittest.cc (6628B)


      1 /*
      2 *  Copyright (c) 2017 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/aec3/suppression_gain.h"
     12 
     13 #include <algorithm>
     14 #include <array>
     15 #include <cstddef>
     16 #include <memory>
     17 #include <optional>
     18 #include <vector>
     19 
     20 #include "api/audio/echo_canceller3_config.h"
     21 #include "api/environment/environment.h"
     22 #include "api/environment/environment_factory.h"
     23 #include "modules/audio_processing/aec3/aec3_common.h"
     24 #include "modules/audio_processing/aec3/aec_state.h"
     25 #include "modules/audio_processing/aec3/block.h"
     26 #include "modules/audio_processing/aec3/delay_estimate.h"
     27 #include "modules/audio_processing/aec3/fft_data.h"
     28 #include "modules/audio_processing/aec3/render_delay_buffer.h"
     29 #include "modules/audio_processing/aec3/render_signal_analyzer.h"
     30 #include "modules/audio_processing/aec3/subtractor.h"
     31 #include "modules/audio_processing/aec3/subtractor_output.h"
     32 #include "modules/audio_processing/logging/apm_data_dumper.h"
     33 #include "rtc_base/checks.h"
     34 #include "test/gtest.h"
     35 
     36 namespace webrtc {
     37 namespace aec3 {
     38 
     39 #if RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
     40 
     41 // Verifies that the check for non-null output gains works.
     42 TEST(SuppressionGainDeathTest, NullOutputGains) {
     43  std::vector<std::array<float, kFftLengthBy2Plus1>> E2(1, {0.0f});
     44  std::vector<std::array<float, kFftLengthBy2Plus1>> R2(1, {0.0f});
     45  std::vector<std::array<float, kFftLengthBy2Plus1>> R2_unbounded(1, {0.0f});
     46  std::vector<std::array<float, kFftLengthBy2Plus1>> S2(1);
     47  std::vector<std::array<float, kFftLengthBy2Plus1>> N2(1, {0.0f});
     48  for (auto& S2_k : S2) {
     49    S2_k.fill(0.1f);
     50  }
     51  FftData E;
     52  FftData Y;
     53  E.re.fill(0.0f);
     54  E.im.fill(0.0f);
     55  Y.re.fill(0.0f);
     56  Y.im.fill(0.0f);
     57 
     58  float high_bands_gain;
     59  AecState aec_state(CreateEnvironment(), EchoCanceller3Config{}, 1);
     60  EXPECT_DEATH(
     61      SuppressionGain(EchoCanceller3Config{}, DetectOptimization(), 16000, 1)
     62          .GetGain(E2, S2, R2, R2_unbounded, N2,
     63                   RenderSignalAnalyzer((EchoCanceller3Config{})), aec_state,
     64                   Block(3, 1), false, &high_bands_gain, nullptr),
     65      "");
     66 }
     67 
     68 #endif
     69 
     70 // Does a sanity check that the gains are correctly computed.
     71 TEST(SuppressionGain, BasicGainComputation) {
     72  constexpr size_t kNumRenderChannels = 1;
     73  constexpr size_t kNumCaptureChannels = 2;
     74  constexpr int kSampleRateHz = 16000;
     75  constexpr size_t kNumBands = NumBandsForRate(kSampleRateHz);
     76  SuppressionGain suppression_gain(EchoCanceller3Config(), DetectOptimization(),
     77                                   kSampleRateHz, kNumCaptureChannels);
     78  RenderSignalAnalyzer analyzer(EchoCanceller3Config{});
     79  float high_bands_gain;
     80  std::vector<std::array<float, kFftLengthBy2Plus1>> E2(kNumCaptureChannels);
     81  std::vector<std::array<float, kFftLengthBy2Plus1>> S2(kNumCaptureChannels,
     82                                                        {0.0f});
     83  std::vector<std::array<float, kFftLengthBy2Plus1>> Y2(kNumCaptureChannels);
     84  std::vector<std::array<float, kFftLengthBy2Plus1>> R2(kNumCaptureChannels);
     85  std::vector<std::array<float, kFftLengthBy2Plus1>> R2_unbounded(
     86      kNumCaptureChannels);
     87  std::vector<std::array<float, kFftLengthBy2Plus1>> N2(kNumCaptureChannels);
     88  std::array<float, kFftLengthBy2Plus1> g;
     89  std::vector<SubtractorOutput> output(kNumCaptureChannels);
     90  Block x(kNumBands, kNumRenderChannels);
     91  const Environment env = CreateEnvironment();
     92  EchoCanceller3Config config;
     93  AecState aec_state(env, config, kNumCaptureChannels);
     94  ApmDataDumper data_dumper(42);
     95  Subtractor subtractor(env, config, kNumRenderChannels, kNumCaptureChannels,
     96                        &data_dumper, DetectOptimization());
     97  std::unique_ptr<RenderDelayBuffer> render_delay_buffer(
     98      RenderDelayBuffer::Create(config, kSampleRateHz, kNumRenderChannels));
     99  std::optional<DelayEstimate> delay_estimate;
    100 
    101  // Ensure that a strong noise is detected to mask any echoes.
    102  for (size_t ch = 0; ch < kNumCaptureChannels; ++ch) {
    103    E2[ch].fill(10.f);
    104    Y2[ch].fill(10.f);
    105    R2[ch].fill(0.1f);
    106    R2_unbounded[ch].fill(0.1f);
    107    N2[ch].fill(100.0f);
    108  }
    109  for (auto& subtractor_output : output) {
    110    subtractor_output.Reset();
    111  }
    112 
    113  // Ensure that the gain is no longer forced to zero.
    114  for (int k = 0; k <= kNumBlocksPerSecond / 5 + 1; ++k) {
    115    aec_state.Update(delay_estimate, subtractor.FilterFrequencyResponses(),
    116                     subtractor.FilterImpulseResponses(),
    117                     *render_delay_buffer->GetRenderBuffer(), E2, Y2, output);
    118  }
    119 
    120  for (int k = 0; k < 100; ++k) {
    121    aec_state.Update(delay_estimate, subtractor.FilterFrequencyResponses(),
    122                     subtractor.FilterImpulseResponses(),
    123                     *render_delay_buffer->GetRenderBuffer(), E2, Y2, output);
    124    suppression_gain.GetGain(E2, S2, R2, R2_unbounded, N2, analyzer, aec_state,
    125                             x, false, &high_bands_gain, &g);
    126  }
    127  std::for_each(g.begin(), g.end(),
    128                [](float a) { EXPECT_NEAR(1.0f, a, 0.001f); });
    129 
    130  // Ensure that a strong nearend is detected to mask any echoes.
    131  for (size_t ch = 0; ch < kNumCaptureChannels; ++ch) {
    132    E2[ch].fill(100.f);
    133    Y2[ch].fill(100.f);
    134    R2[ch].fill(0.1f);
    135    R2_unbounded[ch].fill(0.1f);
    136    S2[ch].fill(0.1f);
    137    N2[ch].fill(0.f);
    138  }
    139 
    140  for (int k = 0; k < 100; ++k) {
    141    aec_state.Update(delay_estimate, subtractor.FilterFrequencyResponses(),
    142                     subtractor.FilterImpulseResponses(),
    143                     *render_delay_buffer->GetRenderBuffer(), E2, Y2, output);
    144    suppression_gain.GetGain(E2, S2, R2, R2_unbounded, N2, analyzer, aec_state,
    145                             x, false, &high_bands_gain, &g);
    146  }
    147  std::for_each(g.begin(), g.end(),
    148                [](float a) { EXPECT_NEAR(1.0f, a, 0.001f); });
    149 
    150  // Add a strong echo to one of the channels and ensure that it is suppressed.
    151  E2[1].fill(1000000000.0f);
    152  R2[1].fill(10000000000000.0f);
    153  R2_unbounded[1].fill(10000000000000.0f);
    154 
    155  for (int k = 0; k < 10; ++k) {
    156    suppression_gain.GetGain(E2, S2, R2, R2_unbounded, N2, analyzer, aec_state,
    157                             x, false, &high_bands_gain, &g);
    158  }
    159  std::for_each(g.begin(), g.end(),
    160                [](float a) { EXPECT_NEAR(0.0f, a, 0.001f); });
    161 }
    162 
    163 }  // namespace aec3
    164 }  // namespace webrtc