OLD | NEW |
(Empty) | |
| 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 "webrtc/modules/audio_processing/aec3/comfort_noise_generator.h" |
| 12 |
| 13 #include <algorithm> |
| 14 #include <numeric> |
| 15 |
| 16 #include "webrtc/typedefs.h" |
| 17 #include "webrtc/base/random.h" |
| 18 #include "webrtc/system_wrappers/include/cpu_features_wrapper.h" |
| 19 #include "webrtc/test/gtest.h" |
| 20 |
| 21 namespace webrtc { |
| 22 namespace { |
| 23 |
| 24 float Power(const FftData& N) { |
| 25 std::array<float, kFftLengthBy2Plus1> N2; |
| 26 N.Spectrum(Aec3Optimization::kNone, &N2); |
| 27 return std::accumulate(N2.begin(), N2.end(), 0.f) / N2.size(); |
| 28 } |
| 29 |
| 30 } // namespace |
| 31 |
| 32 #if RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID) |
| 33 |
| 34 TEST(ComfortNoiseGenerator, NullLowerBandNoise) { |
| 35 std::array<float, kFftLengthBy2Plus1> N2; |
| 36 FftData noise; |
| 37 EXPECT_DEATH(ComfortNoiseGenerator(DetectOptimization()) |
| 38 .Compute(AecState(), N2, nullptr, &noise), |
| 39 ""); |
| 40 } |
| 41 |
| 42 TEST(ComfortNoiseGenerator, NullUpperBandNoise) { |
| 43 std::array<float, kFftLengthBy2Plus1> N2; |
| 44 FftData noise; |
| 45 EXPECT_DEATH(ComfortNoiseGenerator(DetectOptimization()) |
| 46 .Compute(AecState(), N2, &noise, nullptr), |
| 47 ""); |
| 48 } |
| 49 |
| 50 #endif |
| 51 |
| 52 #if defined(WEBRTC_ARCH_X86_FAMILY) |
| 53 // Verifies that the optimized methods are bitexact to their reference |
| 54 // counterparts. |
| 55 TEST(ComfortNoiseGenerator, TestOptimizations) { |
| 56 if (WebRtc_GetCPUInfo(kSSE2) != 0) { |
| 57 Random random_generator(42U); |
| 58 uint32_t seed = 42; |
| 59 uint32_t seed_SSE2 = 42; |
| 60 std::array<float, kFftLengthBy2Plus1> N2; |
| 61 FftData lower_band_noise; |
| 62 FftData upper_band_noise; |
| 63 FftData lower_band_noise_SSE2; |
| 64 FftData upper_band_noise_SSE2; |
| 65 for (int k = 0; k < 10; ++k) { |
| 66 for (size_t j = 0; j < N2.size(); ++j) { |
| 67 N2[j] = random_generator.Rand<float>() * 1000.f; |
| 68 } |
| 69 |
| 70 EstimateComfortNoise(N2, &seed, &lower_band_noise, &upper_band_noise); |
| 71 EstimateComfortNoise_SSE2(N2, &seed_SSE2, &lower_band_noise_SSE2, |
| 72 &upper_band_noise_SSE2); |
| 73 for (size_t j = 0; j < lower_band_noise.re.size(); ++j) { |
| 74 EXPECT_NEAR(lower_band_noise.re[j], lower_band_noise_SSE2.re[j], |
| 75 0.00001f); |
| 76 EXPECT_NEAR(upper_band_noise.re[j], upper_band_noise_SSE2.re[j], |
| 77 0.00001f); |
| 78 } |
| 79 for (size_t j = 1; j < lower_band_noise.re.size() - 1; ++j) { |
| 80 EXPECT_NEAR(lower_band_noise.im[j], lower_band_noise_SSE2.im[j], |
| 81 0.00001f); |
| 82 EXPECT_NEAR(upper_band_noise.im[j], upper_band_noise_SSE2.im[j], |
| 83 0.00001f); |
| 84 } |
| 85 } |
| 86 } |
| 87 } |
| 88 |
| 89 #endif |
| 90 |
| 91 TEST(ComfortNoiseGenerator, CorrectLevel) { |
| 92 ComfortNoiseGenerator cng(DetectOptimization()); |
| 93 AecState aec_state; |
| 94 |
| 95 std::array<float, kFftLengthBy2Plus1> N2; |
| 96 N2.fill(1000.f * 1000.f); |
| 97 |
| 98 FftData n_lower; |
| 99 FftData n_upper; |
| 100 n_lower.re.fill(0.f); |
| 101 n_lower.im.fill(0.f); |
| 102 n_upper.re.fill(0.f); |
| 103 n_upper.im.fill(0.f); |
| 104 |
| 105 // Ensure instantaneous updata to nonzero noise. |
| 106 cng.Compute(aec_state, N2, &n_lower, &n_upper); |
| 107 EXPECT_LT(0.f, Power(n_lower)); |
| 108 EXPECT_LT(0.f, Power(n_upper)); |
| 109 |
| 110 for (int k = 0; k < 10000; ++k) { |
| 111 cng.Compute(aec_state, N2, &n_lower, &n_upper); |
| 112 } |
| 113 EXPECT_NEAR(N2[0], Power(n_lower), N2[0] / 10.f); |
| 114 EXPECT_NEAR(N2[0], Power(n_upper), N2[0] / 10.f); |
| 115 } |
| 116 |
| 117 } // namespace webrtc |
OLD | NEW |