blob: 0b734fdd3f29d098fc5f7226a6e1f7519a9ba4bc [file] [log] [blame]
peah55850012016-03-20 01:01:091/*
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#include <vector>
11
Mirko Bonadei92ea95e2017-09-15 04:47:3112#include "api/array_view.h"
13#include "modules/audio_processing/audio_buffer.h"
14#include "modules/audio_processing/noise_suppression_impl.h"
15#include "modules/audio_processing/test/audio_buffer_tools.h"
16#include "modules/audio_processing/test/bitexactness_tools.h"
17#include "test/gtest.h"
peah55850012016-03-20 01:01:0918
19namespace webrtc {
20namespace {
21
22const int kNumFramesToProcess = 1000;
23
24// Process one frame of data and produce the output.
25void ProcessOneFrame(int sample_rate_hz,
26 AudioBuffer* capture_buffer,
27 NoiseSuppressionImpl* noise_suppressor) {
28 if (sample_rate_hz > AudioProcessing::kSampleRate16kHz) {
29 capture_buffer->SplitIntoFrequencyBands();
30 }
31
32 noise_suppressor->AnalyzeCaptureAudio(capture_buffer);
33 noise_suppressor->ProcessCaptureAudio(capture_buffer);
34
35 if (sample_rate_hz > AudioProcessing::kSampleRate16kHz) {
36 capture_buffer->MergeFrequencyBands();
37 }
38}
39
40// Processes a specified amount of frames, verifies the results and reports
41// any errors.
42void RunBitexactnessTest(int sample_rate_hz,
43 size_t num_channels,
44 NoiseSuppressionImpl::Level level,
45 float speech_probability_reference,
46 rtc::ArrayView<const float> noise_estimate_reference,
47 rtc::ArrayView<const float> output_reference) {
48 rtc::CriticalSection crit_capture;
49 NoiseSuppressionImpl noise_suppressor(&crit_capture);
50 noise_suppressor.Initialize(num_channels, sample_rate_hz);
51 noise_suppressor.Enable(true);
52 noise_suppressor.set_level(level);
53
54 int samples_per_channel = rtc::CheckedDivExact(sample_rate_hz, 100);
55 const StreamConfig capture_config(sample_rate_hz, num_channels, false);
56 AudioBuffer capture_buffer(
57 capture_config.num_frames(), capture_config.num_channels(),
58 capture_config.num_frames(), capture_config.num_channels(),
59 capture_config.num_frames());
60 test::InputAudioFile capture_file(
61 test::GetApmCaptureTestVectorFileName(sample_rate_hz));
62 std::vector<float> capture_input(samples_per_channel * num_channels);
63 for (size_t frame_no = 0; frame_no < kNumFramesToProcess; ++frame_no) {
64 ReadFloatSamplesFromStereoFile(samples_per_channel, num_channels,
65 &capture_file, capture_input);
66
67 test::CopyVectorToAudioBuffer(capture_config, capture_input,
68 &capture_buffer);
69
70 ProcessOneFrame(sample_rate_hz, &capture_buffer, &noise_suppressor);
71 }
72
73 // Extract test results.
74 std::vector<float> capture_output;
75 test::ExtractVectorFromAudioBuffer(capture_config, &capture_buffer,
76 &capture_output);
77 float speech_probability = noise_suppressor.speech_probability();
78 std::vector<float> noise_estimate = noise_suppressor.NoiseEstimate();
79
peah7ea928e2016-03-30 15:13:5780 const float kVectorElementErrorBound = 1.0f / 32768.0f;
peah55850012016-03-20 01:01:0981 EXPECT_FLOAT_EQ(speech_probability_reference, speech_probability);
peah7ea928e2016-03-30 15:13:5782 EXPECT_TRUE(test::VerifyArray(noise_estimate_reference, noise_estimate,
83 kVectorElementErrorBound));
peah55850012016-03-20 01:01:0984
85 // Compare the output with the reference. Only the first values of the output
86 // from last frame processed are compared in order not having to specify all
87 // preceeding frames as testvectors. As the algorithm being tested has a
88 // memory, testing only the last frame implicitly also tests the preceeding
89 // frames.
peah7ea928e2016-03-30 15:13:5790 EXPECT_TRUE(test::VerifyDeinterleavedArray(
peah55850012016-03-20 01:01:0991 capture_config.num_frames(), capture_config.num_channels(),
peah7ea928e2016-03-30 15:13:5792 output_reference, capture_output, kVectorElementErrorBound));
peah55850012016-03-20 01:01:0993}
94
95} // namespace
96
aluebs853c8402016-04-05 17:03:3497TEST(NoiseSuppresionBitExactnessTest, Mono8kHzLow) {
peah55850012016-03-20 01:01:0998#if defined(WEBRTC_ARCH_ARM64)
99 const float kSpeechProbabilityReference = -4.0f;
aluebs853c8402016-04-05 17:03:34100 const float kNoiseEstimateReference[] =
101 {1432.341431f, 3321.919922f, 7677.521973f};
peah55850012016-03-20 01:01:09102 const float kOutputReference[] = {0.003510f, 0.004517f, 0.004669f};
103#elif defined(WEBRTC_ARCH_ARM)
104 const float kSpeechProbabilityReference = -4.0f;
aluebs853c8402016-04-05 17:03:34105 const float kNoiseEstimateReference[] =
106 {1432.341431f, 3321.919922f, 7677.521973f};
peah55850012016-03-20 01:01:09107 const float kOutputReference[] = {0.003510f, 0.004517f, 0.004669f};
108#else
109 const float kSpeechProbabilityReference = 0.73421317f;
aluebs853c8402016-04-05 17:03:34110 const float kNoiseEstimateReference[] =
111 {1175.266113f, 3289.305908f, 7532.991211f};
peah55850012016-03-20 01:01:09112 const float kOutputReference[] = {0.003263f, 0.004402f, 0.004537f};
113#endif
114
115 RunBitexactnessTest(8000, 1, NoiseSuppression::Level::kLow,
116 kSpeechProbabilityReference, kNoiseEstimateReference,
117 kOutputReference);
118}
119
aluebs853c8402016-04-05 17:03:34120TEST(NoiseSuppresionBitExactnessTest, Mono16kHzLow) {
peah55850012016-03-20 01:01:09121#if defined(WEBRTC_ARCH_ARM64)
122 const float kSpeechProbabilityReference = -4.0f;
aluebs853c8402016-04-05 17:03:34123 const float kNoiseEstimateReference[] =
124 {2534.461914f, 6277.638672f, 14367.499023f};
peah55850012016-03-20 01:01:09125 const float kOutputReference[] = {0.003449f, 0.004334f, 0.004303f};
126#elif defined(WEBRTC_ARCH_ARM)
127 const float kSpeechProbabilityReference = -4.0f;
aluebs853c8402016-04-05 17:03:34128 const float kNoiseEstimateReference[] =
129 {2534.461914f, 6277.638672f, 14367.499023f};
peah55850012016-03-20 01:01:09130 const float kOutputReference[] = {0.003449f, 0.004334f, 0.004303f};
131#else
132 const float kSpeechProbabilityReference = 0.71672988f;
aluebs853c8402016-04-05 17:03:34133 const float kNoiseEstimateReference[] =
134 {2151.313965f, 6509.765137f, 15658.848633f};
peah55850012016-03-20 01:01:09135 const float kOutputReference[] = {0.003574f, 0.004494f, 0.004499f};
136#endif
137
138 RunBitexactnessTest(16000, 1, NoiseSuppression::Level::kLow,
139 kSpeechProbabilityReference, kNoiseEstimateReference,
140 kOutputReference);
141}
142
aluebs853c8402016-04-05 17:03:34143TEST(NoiseSuppresionBitExactnessTest, Mono32kHzLow) {
peah55850012016-03-20 01:01:09144#if defined(WEBRTC_ARCH_ARM64)
145 const float kSpeechProbabilityReference = -4.0f;
aluebs853c8402016-04-05 17:03:34146 const float kNoiseEstimateReference[] =
147 {2540.059082f, 6317.822754f, 14440.845703f};
peah55850012016-03-20 01:01:09148 const float kOutputReference[] = {0.001679f, 0.002411f, 0.002594f};
149#elif defined(WEBRTC_ARCH_ARM)
150 const float kSpeechProbabilityReference = -4.0f;
aluebs853c8402016-04-05 17:03:34151 const float kNoiseEstimateReference[] =
152 {2540.059082f, 6317.822754f, 14440.845703f};
peah55850012016-03-20 01:01:09153 const float kOutputReference[] = {0.001679f, 0.002411f, 0.002594f};
154#else
155 const float kSpeechProbabilityReference = 0.67999554f;
aluebs853c8402016-04-05 17:03:34156 const float kNoiseEstimateReference[] =
157 {2149.780518f, 7076.936035f, 14939.945312f};
peah55850012016-03-20 01:01:09158 const float kOutputReference[] = {0.001221f, 0.001984f, 0.002228f};
159#endif
160
161 RunBitexactnessTest(32000, 1, NoiseSuppression::Level::kLow,
162 kSpeechProbabilityReference, kNoiseEstimateReference,
163 kOutputReference);
164}
165
aluebs853c8402016-04-05 17:03:34166TEST(NoiseSuppresionBitExactnessTest, Mono48kHzLow) {
peah55850012016-03-20 01:01:09167#if defined(WEBRTC_ARCH_ARM64)
168 const float kSpeechProbabilityReference = -4.0f;
aluebs853c8402016-04-05 17:03:34169 const float kNoiseEstimateReference[] =
170 {2564.605713f, 6213.656250f, 13372.284180f};
peah55850012016-03-20 01:01:09171 const float kOutputReference[] = {-0.013185f, -0.012769f, -0.012023f};
172#elif defined(WEBRTC_ARCH_ARM)
173 const float kSpeechProbabilityReference = -4.0f;
aluebs853c8402016-04-05 17:03:34174 const float kNoiseEstimateReference[] =
175 {2564.605713f, 6213.656250f, 13372.284180f};
peah55850012016-03-20 01:01:09176 const float kOutputReference[] = {-0.013185f, -0.012769f, -0.012023f};
177#else
178 const float kSpeechProbabilityReference = 0.70645678f;
aluebs853c8402016-04-05 17:03:34179 const float kNoiseEstimateReference[] =
180 {2168.783203f, 6902.895508f, 13190.677734f};
peah55850012016-03-20 01:01:09181 const float kOutputReference[] = {-0.013062f, -0.012657f, -0.011934f};
182#endif
183
184 RunBitexactnessTest(48000, 1, NoiseSuppression::Level::kLow,
185 kSpeechProbabilityReference, kNoiseEstimateReference,
186 kOutputReference);
187}
188
aluebs853c8402016-04-05 17:03:34189TEST(NoiseSuppresionBitExactnessTest, Stereo16kHzLow) {
peah55850012016-03-20 01:01:09190#if defined(WEBRTC_ARCH_ARM64)
191 const float kSpeechProbabilityReference = -4.0f;
aluebs853c8402016-04-05 17:03:34192 const float kNoiseEstimateReference[] =
193 {9992.127930f, 12689.569336f, 11589.296875f};
peah55850012016-03-20 01:01:09194 const float kOutputReference[] = {-0.011108f, -0.007904f, -0.012390f,
195 -0.002441f, 0.000855f, -0.003204f};
196#elif defined(WEBRTC_ARCH_ARM)
197 const float kSpeechProbabilityReference = -4.0f;
aluebs853c8402016-04-05 17:03:34198 const float kNoiseEstimateReference[] =
199 {10321.353516f, 12133.852539f, 10923.060547f};
peah55850012016-03-20 01:01:09200 const float kOutputReference[] = {-0.011108f, -0.007904f, -0.012390f,
201 -0.002472f, 0.000916f, -0.003235f};
202#else
203 const float kSpeechProbabilityReference = 0.67230678f;
aluebs853c8402016-04-05 17:03:34204 const float kNoiseEstimateReference[] =
205 {9771.250000f, 11329.377930f, 10503.052734f};
peah55850012016-03-20 01:01:09206 const float kOutputReference[] = {-0.011459f, -0.008110f, -0.012728f,
207 -0.002399f, 0.001018f, -0.003189f};
208#endif
209
210 RunBitexactnessTest(16000, 2, NoiseSuppression::Level::kLow,
211 kSpeechProbabilityReference, kNoiseEstimateReference,
212 kOutputReference);
213}
214
aluebs853c8402016-04-05 17:03:34215TEST(NoiseSuppresionBitExactnessTest, Mono16kHzModerate) {
peah55850012016-03-20 01:01:09216#if defined(WEBRTC_ARCH_ARM64)
217 const float kSpeechProbabilityReference = -4.0f;
aluebs853c8402016-04-05 17:03:34218 const float kNoiseEstimateReference[] =
219 {2057.085938f, 7601.055176f, 19666.187500f};
peah55850012016-03-20 01:01:09220 const float kOutputReference[] = {0.004669f, 0.005524f, 0.005432f};
221#elif defined(WEBRTC_ARCH_ARM)
222 const float kSpeechProbabilityReference = -4.0f;
aluebs853c8402016-04-05 17:03:34223 const float kNoiseEstimateReference[] =
224 {2244.497803f, 6864.164062f, 16726.523438f};
peah55850012016-03-20 01:01:09225 const float kOutputReference[] = {0.004669f, 0.005615f, 0.005585f};
226#else
227 const float kSpeechProbabilityReference = 0.70897013f;
aluebs853c8402016-04-05 17:03:34228 const float kNoiseEstimateReference[] =
229 {2171.490723f, 6553.567871f, 15626.562500f};
peah55850012016-03-20 01:01:09230 const float kOutputReference[] = {0.004513f, 0.005590f, 0.005614f};
231#endif
232
233 RunBitexactnessTest(16000, 1, NoiseSuppression::Level::kModerate,
234 kSpeechProbabilityReference, kNoiseEstimateReference,
235 kOutputReference);
236}
237
aluebs853c8402016-04-05 17:03:34238TEST(NoiseSuppresionBitExactnessTest, Mono16kHzHigh) {
peah55850012016-03-20 01:01:09239#if defined(WEBRTC_ARCH_ARM64)
240 const float kSpeechProbabilityReference = -4.0f;
aluebs853c8402016-04-05 17:03:34241 const float kNoiseEstimateReference[] =
242 {2095.148193f, 7698.553711f, 19689.533203f};
peah55850012016-03-20 01:01:09243 const float kOutputReference[] = {0.004639f, 0.005402f, 0.005310f};
244#elif defined(WEBRTC_ARCH_ARM)
245 const float kSpeechProbabilityReference = -4.0f;
aluebs853c8402016-04-05 17:03:34246 const float kNoiseEstimateReference[] =
247 {2282.515625f, 6984.408203f, 16920.960938f};
peah55850012016-03-20 01:01:09248 const float kOutputReference[] = {0.004547f, 0.005432f, 0.005402f};
249#else
250 const float kSpeechProbabilityReference = 0.70106733f;
aluebs853c8402016-04-05 17:03:34251 const float kNoiseEstimateReference[] =
252 {2224.968506f, 6712.025879f, 15785.087891f};
peah55850012016-03-20 01:01:09253 const float kOutputReference[] = {0.004394f, 0.005406f, 0.005416f};
254#endif
255
256 RunBitexactnessTest(16000, 1, NoiseSuppression::Level::kHigh,
257 kSpeechProbabilityReference, kNoiseEstimateReference,
258 kOutputReference);
259}
260
aluebs853c8402016-04-05 17:03:34261TEST(NoiseSuppresionBitExactnessTest, Mono16kHzVeryHigh) {
peah55850012016-03-20 01:01:09262#if defined(WEBRTC_ARCH_ARM64)
263 const float kSpeechProbabilityReference = -4.0f;
aluebs853c8402016-04-05 17:03:34264 const float kNoiseEstimateReference[] =
265 {2677.733398f, 6186.987305f, 14365.744141f};
peah55850012016-03-20 01:01:09266 const float kOutputReference[] = {0.004273f, 0.005127f, 0.005188f};
267#elif defined(WEBRTC_ARCH_ARM)
268 const float kSpeechProbabilityReference = -4.0f;
aluebs853c8402016-04-05 17:03:34269 const float kNoiseEstimateReference[] =
270 {2677.733398f, 6186.987305f, 14365.744141f};
peah55850012016-03-20 01:01:09271 const float kOutputReference[] = {0.004273f, 0.005127f, 0.005188f};
272#else
273 const float kSpeechProbabilityReference = 0.70281971f;
aluebs853c8402016-04-05 17:03:34274 const float kNoiseEstimateReference[] =
275 {2254.347900f, 6723.699707f, 15771.625977f};
peah55850012016-03-20 01:01:09276 const float kOutputReference[] = {0.004321f, 0.005247f, 0.005263f};
277#endif
278
279 RunBitexactnessTest(16000, 1, NoiseSuppression::Level::kVeryHigh,
280 kSpeechProbabilityReference, kNoiseEstimateReference,
281 kOutputReference);
282}
283
284} // namespace webrtc