File: noise_suppressor_unittest.cc

package info (click to toggle)
chromium 140.0.7339.127-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 6,192,880 kB
  • sloc: cpp: 35,093,808; ansic: 7,161,670; javascript: 4,199,694; python: 1,441,797; asm: 949,904; xml: 747,503; pascal: 187,748; perl: 88,691; sh: 88,248; objc: 79,953; sql: 52,714; cs: 44,599; fortran: 24,137; makefile: 22,114; tcl: 15,277; php: 13,980; yacc: 9,000; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (100 lines) | stat: -rw-r--r-- 3,495 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*
 *  Copyright (c) 2016 The WebRTC project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */

#include "modules/audio_processing/ns/noise_suppressor.h"

#include <cstddef>
#include <string>

#include "modules/audio_processing/audio_buffer.h"
#include "modules/audio_processing/ns/ns_config.h"
#include "rtc_base/strings/string_builder.h"
#include "test/gtest.h"

namespace webrtc {
namespace {

std::string ProduceDebugText(int sample_rate_hz,
                             size_t num_channels,
                             NsConfig::SuppressionLevel level) {
  StringBuilder ss;
  ss << "Sample rate: " << sample_rate_hz << ", num_channels: " << num_channels
     << ", level: " << static_cast<int>(level);
  return ss.Release();
}

void PopulateInputFrameWithIdenticalChannels(size_t num_channels,
                                             size_t num_bands,
                                             size_t frame_index,
                                             AudioBuffer* audio) {
  for (size_t ch = 0; ch < num_channels; ++ch) {
    for (size_t b = 0; b < num_bands; ++b) {
      for (size_t i = 0; i < 160; ++i) {
        float value = static_cast<int>(frame_index * 160 + i);
        audio->split_bands(ch)[b][i] = (value > 0 ? 5000 * b + value : 0);
      }
    }
  }
}

void VerifyIdenticalChannels(size_t num_channels,
                             size_t num_bands,
                             size_t /* frame_index */,
                             const AudioBuffer& audio) {
  EXPECT_GT(num_channels, 1u);
  for (size_t ch = 1; ch < num_channels; ++ch) {
    for (size_t b = 0; b < num_bands; ++b) {
      for (size_t i = 0; i < 160; ++i) {
        EXPECT_EQ(audio.split_bands_const(ch)[b][i],
                  audio.split_bands_const(0)[b][i]);
      }
    }
  }
}

}  // namespace

// Verifies that the same noise reduction effect is applied to all channels.
TEST(NoiseSuppressor, IdenticalChannelEffects) {
  for (auto rate : {16000, 32000, 48000}) {
    for (auto num_channels : {1, 4, 8}) {
      for (auto level :
           {NsConfig::SuppressionLevel::k6dB, NsConfig::SuppressionLevel::k12dB,
            NsConfig::SuppressionLevel::k18dB,
            NsConfig::SuppressionLevel::k21dB}) {
        SCOPED_TRACE(ProduceDebugText(rate, num_channels, level));

        const size_t num_bands = rate / 16000;
        // const int frame_length = CheckedDivExact(rate, 100);
        AudioBuffer audio(rate, num_channels, rate, num_channels, rate,
                          num_channels);
        NsConfig cfg;
        NoiseSuppressor ns(cfg, rate, num_channels);
        for (size_t frame_index = 0; frame_index < 1000; ++frame_index) {
          if (rate > 16000) {
            audio.SplitIntoFrequencyBands();
          }

          PopulateInputFrameWithIdenticalChannels(num_channels, num_bands,
                                                  frame_index, &audio);

          ns.Analyze(audio);
          ns.Process(&audio);
          if (num_channels > 1) {
            VerifyIdenticalChannels(num_channels, num_bands, frame_index,
                                    audio);
          }
        }
      }
    }
  }
}

}  // namespace webrtc