noise_suppressor_unittest.cc (3495B)
1 /* 2 * Copyright (c) 2016 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/ns/noise_suppressor.h" 12 13 #include <cstddef> 14 #include <string> 15 16 #include "modules/audio_processing/audio_buffer.h" 17 #include "modules/audio_processing/ns/ns_config.h" 18 #include "rtc_base/strings/string_builder.h" 19 #include "test/gtest.h" 20 21 namespace webrtc { 22 namespace { 23 24 std::string ProduceDebugText(int sample_rate_hz, 25 size_t num_channels, 26 NsConfig::SuppressionLevel level) { 27 StringBuilder ss; 28 ss << "Sample rate: " << sample_rate_hz << ", num_channels: " << num_channels 29 << ", level: " << static_cast<int>(level); 30 return ss.Release(); 31 } 32 33 void PopulateInputFrameWithIdenticalChannels(size_t num_channels, 34 size_t num_bands, 35 size_t frame_index, 36 AudioBuffer* audio) { 37 for (size_t ch = 0; ch < num_channels; ++ch) { 38 for (size_t b = 0; b < num_bands; ++b) { 39 for (size_t i = 0; i < 160; ++i) { 40 float value = static_cast<int>(frame_index * 160 + i); 41 audio->split_bands(ch)[b][i] = (value > 0 ? 5000 * b + value : 0); 42 } 43 } 44 } 45 } 46 47 void VerifyIdenticalChannels(size_t num_channels, 48 size_t num_bands, 49 size_t /* frame_index */, 50 const AudioBuffer& audio) { 51 EXPECT_GT(num_channels, 1u); 52 for (size_t ch = 1; ch < num_channels; ++ch) { 53 for (size_t b = 0; b < num_bands; ++b) { 54 for (size_t i = 0; i < 160; ++i) { 55 EXPECT_EQ(audio.split_bands_const(ch)[b][i], 56 audio.split_bands_const(0)[b][i]); 57 } 58 } 59 } 60 } 61 62 } // namespace 63 64 // Verifies that the same noise reduction effect is applied to all channels. 65 TEST(NoiseSuppressor, IdenticalChannelEffects) { 66 for (auto rate : {16000, 32000, 48000}) { 67 for (auto num_channels : {1, 4, 8}) { 68 for (auto level : 69 {NsConfig::SuppressionLevel::k6dB, NsConfig::SuppressionLevel::k12dB, 70 NsConfig::SuppressionLevel::k18dB, 71 NsConfig::SuppressionLevel::k21dB}) { 72 SCOPED_TRACE(ProduceDebugText(rate, num_channels, level)); 73 74 const size_t num_bands = rate / 16000; 75 // const int frame_length = CheckedDivExact(rate, 100); 76 AudioBuffer audio(rate, num_channels, rate, num_channels, rate, 77 num_channels); 78 NsConfig cfg; 79 NoiseSuppressor ns(cfg, rate, num_channels); 80 for (size_t frame_index = 0; frame_index < 1000; ++frame_index) { 81 if (rate > 16000) { 82 audio.SplitIntoFrequencyBands(); 83 } 84 85 PopulateInputFrameWithIdenticalChannels(num_channels, num_bands, 86 frame_index, &audio); 87 88 ns.Analyze(audio); 89 ns.Process(&audio); 90 if (num_channels > 1) { 91 VerifyIdenticalChannels(num_channels, num_bands, frame_index, 92 audio); 93 } 94 } 95 } 96 } 97 } 98 } 99 100 } // namespace webrtc