| OLD | NEW | 
| (Empty) |  | 
 |    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 #include <vector> | 
 |   11  | 
 |   12 #include "testing/gtest/include/gtest/gtest.h" | 
 |   13 #include "webrtc/base/array_view.h" | 
 |   14 #include "webrtc/modules/audio_processing/audio_buffer.h" | 
 |   15 #include "webrtc/modules/audio_processing/noise_suppression_impl.h" | 
 |   16 #include "webrtc/modules/audio_processing/test/audio_buffer_tools.h" | 
 |   17 #include "webrtc/modules/audio_processing/test/bitexactness_tools.h" | 
 |   18  | 
 |   19 namespace webrtc { | 
 |   20 namespace { | 
 |   21  | 
 |   22 const int kNumFramesToProcess = 1000; | 
 |   23  | 
 |   24 // Process one frame of data and produce the output. | 
 |   25 void 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. | 
 |   42 void 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  | 
 |   80   const float kTolerance = 1.0f / 32768.0f; | 
 |   81   EXPECT_FLOAT_EQ(speech_probability_reference, speech_probability); | 
 |   82   EXPECT_TRUE(test::BitExactVector(noise_estimate_reference, noise_estimate, | 
 |   83                                    kTolerance)); | 
 |   84  | 
 |   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. | 
 |   90   EXPECT_TRUE(test::BitExactFrame( | 
 |   91       capture_config.num_frames(), capture_config.num_channels(), | 
 |   92       output_reference, capture_output, kTolerance)); | 
 |   93 } | 
 |   94  | 
 |   95 }  // namespace | 
 |   96  | 
 |   97 TEST(NoiseSuppresionBitExactnessTest, Mono8kHzLow) { | 
 |   98 #if !defined(WEBRTC_ANDROID) | 
 |   99   const float kSpeechProbabilityReference = 0.73421317; | 
 |  100   const float kNoiseEstimateReference[] = {0.035866f, 0.100382f, 0.229889f}; | 
 |  101   const float kOutputReference[] = {0.003263f, 0.004402f, 0.004537f}; | 
 |  102 #else | 
 |  103   const float kSpeechProbabilityReference = 0.73421317; | 
 |  104   const float kNoiseEstimateReference[] = {0.035866f, 0.100382f, 0.229889f}; | 
 |  105   const float kOutputReference[] = {0.003263f, 0.004402f, 0.004537f}; | 
 |  106 #endif | 
 |  107  | 
 |  108   RunBitexactnessTest(8000, 1, NoiseSuppression::Level::kLow, | 
 |  109                       kSpeechProbabilityReference, kNoiseEstimateReference, | 
 |  110                       kOutputReference); | 
 |  111 } | 
 |  112  | 
 |  113 TEST(NoiseSuppresionBitExactnessTest, Mono16kHzLow) { | 
 |  114 #if !defined(WEBRTC_ANDROID) | 
 |  115   const float kSpeechProbabilityReference = 0.71672988; | 
 |  116   const float kNoiseEstimateReference[] = {0.065653f, 0.198662f, 0.477870f}; | 
 |  117   const float kOutputReference[] = {0.003574f, 0.004494f, 0.004499f}; | 
 |  118 #else | 
 |  119   const float kSpeechProbabilityReference = 0.71672988; | 
 |  120   const float kNoiseEstimateReference[] = {0.065653f, 0.198662f, 0.477870f}; | 
 |  121   const float kOutputReference[] = {0.003574f, 0.004494f, 0.004499f}; | 
 |  122 #endif | 
 |  123  | 
 |  124   RunBitexactnessTest(16000, 1, NoiseSuppression::Level::kLow, | 
 |  125                       kSpeechProbabilityReference, kNoiseEstimateReference, | 
 |  126                       kOutputReference); | 
 |  127 } | 
 |  128  | 
 |  129 TEST(NoiseSuppresionBitExactnessTest, Mono32kHzLow) { | 
 |  130 #if !defined(WEBRTC_ANDROID) | 
 |  131   const float kSpeechProbabilityReference = 0.67999554; | 
 |  132   const float kNoiseEstimateReference[] = {0.065606f, 0.215971f, 0.455931f}; | 
 |  133   const float kOutputReference[] = {0.001221f, 0.001984f, 0.002228f}; | 
 |  134 #else | 
 |  135   const float kSpeechProbabilityReference = 0.67999554; | 
 |  136   const float kNoiseEstimateReference[] = {0.065606f, 0.215971f, 0.455931f}; | 
 |  137   const float kOutputReference[] = {0.001221f, 0.001984f, 0.002228f}; | 
 |  138 #endif | 
 |  139  | 
 |  140   RunBitexactnessTest(32000, 1, NoiseSuppression::Level::kLow, | 
 |  141                       kSpeechProbabilityReference, kNoiseEstimateReference, | 
 |  142                       kOutputReference); | 
 |  143 } | 
 |  144  | 
 |  145 TEST(NoiseSuppresionBitExactnessTest, Mono48kHzLow) { | 
 |  146 #if !defined(WEBRTC_ANDROID) | 
 |  147   const float kSpeechProbabilityReference = 0.70645678; | 
 |  148   const float kNoiseEstimateReference[] = {0.066186f, 0.210660f, 0.402548f}; | 
 |  149   const float kOutputReference[] = {-0.013062f, -0.012657f, -0.011934f}; | 
 |  150 #else | 
 |  151   const float kSpeechProbabilityReference = 0.70645678; | 
 |  152   const float kNoiseEstimateReference[] = {0.066186f, 0.210660f, 0.402548f}; | 
 |  153   const float kOutputReference[] = {-0.013062f, -0.012657f, -0.011934f}; | 
 |  154 #endif | 
 |  155  | 
 |  156   RunBitexactnessTest(48000, 1, NoiseSuppression::Level::kLow, | 
 |  157                       kSpeechProbabilityReference, kNoiseEstimateReference, | 
 |  158                       kOutputReference); | 
 |  159 } | 
 |  160  | 
 |  161 TEST(NoiseSuppresionBitExactnessTest, Stereo16kHzLow) { | 
 |  162 #if !defined(WEBRTC_ANDROID) | 
 |  163   const float kSpeechProbabilityReference = 0.67230678; | 
 |  164   const float kNoiseEstimateReference[] = {0.298195f, 0.345745f, 0.320528f}; | 
 |  165   const float kOutputReference[] = {-0.011459f, -0.008110f, -0.012728f, | 
 |  166                                     -0.002399f, 0.001018f,  -0.003189f}; | 
 |  167 #else | 
 |  168   const float kSpeechProbabilityReference = 0.67230678; | 
 |  169   const float kNoiseEstimateReference[] = {0.298195f, 0.345745f, 0.320528f}; | 
 |  170   const float kOutputReference[] = {-0.011459f, -0.008110f, -0.012728f, | 
 |  171                                     -0.002399f, 0.001018f,  -0.003189f}; | 
 |  172 #endif | 
 |  173  | 
 |  174   RunBitexactnessTest(16000, 2, NoiseSuppression::Level::kLow, | 
 |  175                       kSpeechProbabilityReference, kNoiseEstimateReference, | 
 |  176                       kOutputReference); | 
 |  177 } | 
 |  178  | 
 |  179 TEST(NoiseSuppresionBitExactnessTest, Mono16kHzModerate) { | 
 |  180 #if !defined(WEBRTC_ANDROID) | 
 |  181   const float kSpeechProbabilityReference = 0.70897013; | 
 |  182   const float kNoiseEstimateReference[] = {0.066269f, 0.199999f, 0.476885f}; | 
 |  183   const float kOutputReference[] = {0.004513f, 0.005590f, 0.005614f}; | 
 |  184 #else | 
 |  185   const float kSpeechProbabilityReference = 0.70897013; | 
 |  186   const float kNoiseEstimateReference[] = {0.066269f, 0.199999f, 0.476885f}; | 
 |  187   const float kOutputReference[] = {0.004513f, 0.005590f, 0.005614f}; | 
 |  188 #endif | 
 |  189  | 
 |  190   RunBitexactnessTest(16000, 1, NoiseSuppression::Level::kModerate, | 
 |  191                       kSpeechProbabilityReference, kNoiseEstimateReference, | 
 |  192                       kOutputReference); | 
 |  193 } | 
 |  194  | 
 |  195 TEST(NoiseSuppresionBitExactnessTest, Mono16kHzHigh) { | 
 |  196 #if !defined(WEBRTC_ANDROID) | 
 |  197   const float kSpeechProbabilityReference = 0.70106733; | 
 |  198   const float kNoiseEstimateReference[] = {0.067901f, 0.204835f, 0.481723f}; | 
 |  199   const float kOutputReference[] = {0.004394f, 0.005406f, 0.005416f}; | 
 |  200 #else | 
 |  201   const float kSpeechProbabilityReference = 0.70106733; | 
 |  202   const float kNoiseEstimateReference[] = {0.067901f, 0.204835f, 0.481723f}; | 
 |  203   const float kOutputReference[] = {0.004394f, 0.005406f, 0.005416f}; | 
 |  204 #endif | 
 |  205  | 
 |  206   RunBitexactnessTest(16000, 1, NoiseSuppression::Level::kHigh, | 
 |  207                       kSpeechProbabilityReference, kNoiseEstimateReference, | 
 |  208                       kOutputReference); | 
 |  209 } | 
 |  210  | 
 |  211 TEST(NoiseSuppresionBitExactnessTest, Mono16kHzVeryHigh) { | 
 |  212 #if !defined(WEBRTC_ANDROID) | 
 |  213   const float kSpeechProbabilityReference = 0.70281971; | 
 |  214   const float kNoiseEstimateReference[] = {0.068797f, 0.205191f, 0.481312f}; | 
 |  215   const float kOutputReference[] = {0.004321f, 0.005247f, 0.005263f}; | 
 |  216 #else | 
 |  217   const float kSpeechProbabilityReference = 0.70281971; | 
 |  218   const float kNoiseEstimateReference[] = {0.068797f, 0.205191f, 0.481312f}; | 
 |  219   const float kOutputReference[] = {0.004321f, 0.005247f, 0.005263f}; | 
 |  220 #endif | 
 |  221  | 
 |  222   RunBitexactnessTest(16000, 1, NoiseSuppression::Level::kVeryHigh, | 
 |  223                       kSpeechProbabilityReference, kNoiseEstimateReference, | 
 |  224                       kOutputReference); | 
 |  225 } | 
 |  226  | 
 |  227 }  // namespace webrtc | 
| OLD | NEW |