peah | 57d5a2e | 2016-03-29 09:48:36 -0700 | [diff] [blame] | 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/gain_control_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 | #if !(defined(WEBRTC_ARCH_ARM64) || defined(WEBRTC_ARCH_ARM) || \ |
| 20 | defined(WEBRTC_ANDROID)) |
| 21 | |
| 22 | namespace webrtc { |
| 23 | namespace { |
| 24 | |
| 25 | const int kNumFramesToProcess = 100; |
| 26 | |
| 27 | void ProcessOneFrame(int sample_rate_hz, |
| 28 | AudioBuffer* render_audio_buffer, |
| 29 | AudioBuffer* capture_audio_buffer, |
| 30 | GainControlImpl* gain_controller) { |
| 31 | if (sample_rate_hz > AudioProcessing::kSampleRate16kHz) { |
| 32 | render_audio_buffer->SplitIntoFrequencyBands(); |
| 33 | capture_audio_buffer->SplitIntoFrequencyBands(); |
| 34 | } |
| 35 | |
| 36 | gain_controller->ProcessRenderAudio(render_audio_buffer); |
| 37 | gain_controller->AnalyzeCaptureAudio(capture_audio_buffer); |
| 38 | gain_controller->ProcessCaptureAudio(capture_audio_buffer, false); |
| 39 | |
| 40 | if (sample_rate_hz > AudioProcessing::kSampleRate16kHz) { |
| 41 | capture_audio_buffer->MergeFrequencyBands(); |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | void SetupComponent(int sample_rate_hz, |
| 46 | GainControl::Mode mode, |
| 47 | int target_level_dbfs, |
| 48 | int stream_analog_level, |
| 49 | int compression_gain_db, |
| 50 | bool enable_limiter, |
| 51 | int analog_level_min, |
| 52 | int analog_level_max, |
| 53 | GainControlImpl* gain_controller) { |
| 54 | gain_controller->Initialize(1, sample_rate_hz); |
| 55 | GainControl* gc = static_cast<GainControl*>(gain_controller); |
| 56 | gc->Enable(true); |
| 57 | gc->set_mode(mode); |
| 58 | gc->set_stream_analog_level(stream_analog_level); |
| 59 | gc->set_target_level_dbfs(target_level_dbfs); |
| 60 | gc->set_compression_gain_db(compression_gain_db); |
| 61 | gc->enable_limiter(enable_limiter); |
| 62 | gc->set_analog_level_limits(analog_level_min, analog_level_max); |
| 63 | } |
| 64 | |
| 65 | void RunBitExactnessTest(int sample_rate_hz, |
| 66 | size_t num_channels, |
| 67 | GainControl::Mode mode, |
| 68 | int target_level_dbfs, |
| 69 | int stream_analog_level, |
| 70 | int compression_gain_db, |
| 71 | bool enable_limiter, |
| 72 | int analog_level_min, |
| 73 | int analog_level_max, |
| 74 | int achieved_stream_analog_level_reference, |
| 75 | rtc::ArrayView<const float> output_reference) { |
| 76 | rtc::CriticalSection crit_render; |
| 77 | rtc::CriticalSection crit_capture; |
| 78 | GainControlImpl gain_controller(&crit_render, &crit_capture); |
| 79 | SetupComponent(sample_rate_hz, mode, target_level_dbfs, stream_analog_level, |
| 80 | compression_gain_db, enable_limiter, analog_level_min, |
| 81 | analog_level_max, &gain_controller); |
| 82 | |
| 83 | const int samples_per_channel = rtc::CheckedDivExact(sample_rate_hz, 100); |
| 84 | const StreamConfig render_config(sample_rate_hz, num_channels, false); |
| 85 | AudioBuffer render_buffer( |
| 86 | render_config.num_frames(), render_config.num_channels(), |
| 87 | render_config.num_frames(), 1, render_config.num_frames()); |
| 88 | test::InputAudioFile render_file( |
| 89 | test::GetApmRenderTestVectorFileName(sample_rate_hz)); |
| 90 | std::vector<float> render_input(samples_per_channel * num_channels); |
| 91 | |
| 92 | const StreamConfig capture_config(sample_rate_hz, num_channels, false); |
| 93 | AudioBuffer capture_buffer( |
| 94 | capture_config.num_frames(), capture_config.num_channels(), |
| 95 | capture_config.num_frames(), 1, capture_config.num_frames()); |
| 96 | test::InputAudioFile capture_file( |
| 97 | test::GetApmCaptureTestVectorFileName(sample_rate_hz)); |
| 98 | std::vector<float> capture_input(samples_per_channel * num_channels); |
| 99 | |
| 100 | for (int frame_no = 0; frame_no < kNumFramesToProcess; ++frame_no) { |
| 101 | ReadFloatSamplesFromStereoFile(samples_per_channel, num_channels, |
| 102 | &render_file, render_input); |
| 103 | ReadFloatSamplesFromStereoFile(samples_per_channel, num_channels, |
| 104 | &capture_file, capture_input); |
| 105 | |
| 106 | test::CopyVectorToAudioBuffer(render_config, render_input, &render_buffer); |
| 107 | test::CopyVectorToAudioBuffer(capture_config, capture_input, |
| 108 | &capture_buffer); |
| 109 | |
| 110 | ProcessOneFrame(sample_rate_hz, &render_buffer, &capture_buffer, |
| 111 | &gain_controller); |
| 112 | } |
| 113 | |
| 114 | // Extract and verify the test results. |
| 115 | std::vector<float> capture_output; |
| 116 | test::ExtractVectorFromAudioBuffer(capture_config, &capture_buffer, |
| 117 | &capture_output); |
| 118 | |
| 119 | EXPECT_EQ(achieved_stream_analog_level_reference, |
| 120 | gain_controller.stream_analog_level()); |
| 121 | |
| 122 | // Compare the output with the reference. Only the first values of the output |
| 123 | // from last frame processed are compared in order not having to specify all |
| 124 | // preceeding frames as testvectors. As the algorithm being tested has a |
| 125 | // memory, testing only the last frame implicitly also tests the preceeding |
| 126 | // frames. |
| 127 | const float kTolerance = 1.0f / 32768.0f; |
| 128 | EXPECT_TRUE(test::BitExactFrame( |
| 129 | capture_config.num_frames(), capture_config.num_channels(), |
| 130 | output_reference, capture_output, kTolerance)); |
| 131 | } |
| 132 | |
| 133 | } // namespace |
| 134 | |
| 135 | TEST(GainControlBitExactnessTest, |
| 136 | Mono8kHz_AdaptiveAnalog_Tl10_SL50_CG5_Lim_AL0_100) { |
| 137 | const int kStreamAnalogLevelReference = 50; |
| 138 | const float kOutputReference[] = {-0.006622f, -0.002747f, 0.001587f}; |
| 139 | RunBitExactnessTest(8000, 1, GainControl::Mode::kAdaptiveAnalog, 10, 50, 5, |
| 140 | true, 0, 100, kStreamAnalogLevelReference, |
| 141 | kOutputReference); |
| 142 | } |
| 143 | |
| 144 | TEST(GainControlBitExactnessTest, |
| 145 | Mono16kHz_AdaptiveAnalog_Tl10_SL50_CG5_Lim_AL0_100) { |
| 146 | const int kStreamAnalogLevelReference = 50; |
| 147 | const float kOutputReference[] = {-0.006561f, -0.004608f, -0.002899f}; |
| 148 | RunBitExactnessTest(16000, 1, GainControl::Mode::kAdaptiveAnalog, 10, 50, 5, |
| 149 | true, 0, 100, kStreamAnalogLevelReference, |
| 150 | kOutputReference); |
| 151 | } |
| 152 | |
| 153 | TEST(GainControlBitExactnessTest, |
| 154 | Stereo16kHz_AdaptiveAnalog_Tl10_SL50_CG5_Lim_AL0_100) { |
| 155 | const int kStreamAnalogLevelReference = 50; |
| 156 | const float kOutputReference[] = {-0.027313f, -0.015900f, -0.028107f, |
| 157 | -0.027313f, -0.015900f, -0.028107f}; |
| 158 | RunBitExactnessTest(16000, 2, GainControl::Mode::kAdaptiveAnalog, 10, 50, 5, |
| 159 | true, 0, 100, kStreamAnalogLevelReference, |
| 160 | kOutputReference); |
| 161 | } |
| 162 | |
| 163 | TEST(GainControlBitExactnessTest, |
| 164 | Mono32kHz_AdaptiveAnalog_Tl10_SL50_CG5_Lim_AL0_100) { |
| 165 | const int kStreamAnalogLevelReference = 50; |
| 166 | const float kOutputReference[] = {-0.010162f, -0.009155f, -0.008301f}; |
| 167 | RunBitExactnessTest(32000, 1, GainControl::Mode::kAdaptiveAnalog, 10, 50, 5, |
| 168 | true, 0, 100, kStreamAnalogLevelReference, |
| 169 | kOutputReference); |
| 170 | } |
| 171 | |
| 172 | TEST(GainControlBitExactnessTest, |
| 173 | Mono48kHz_AdaptiveAnalog_Tl10_SL50_CG5_Lim_AL0_100) { |
| 174 | const int kStreamAnalogLevelReference = 50; |
| 175 | const float kOutputReference[] = {-0.010162f, -0.009155f, -0.008301f}; |
| 176 | RunBitExactnessTest(32000, 1, GainControl::Mode::kAdaptiveAnalog, 10, 50, 5, |
| 177 | true, 0, 100, kStreamAnalogLevelReference, |
| 178 | kOutputReference); |
| 179 | } |
| 180 | |
| 181 | TEST(GainControlBitExactnessTest, |
| 182 | Mono8kHz_AdaptiveDigital_Tl10_SL50_CG5_Lim_AL0_100) { |
| 183 | const int kStreamAnalogLevelReference = 50; |
| 184 | const float kOutputReference[] = {-0.006317f, -0.002625f, 0.001495f}; |
| 185 | RunBitExactnessTest(8000, 1, GainControl::Mode::kAdaptiveDigital, 10, 50, 5, |
| 186 | true, 0, 100, kStreamAnalogLevelReference, |
| 187 | kOutputReference); |
| 188 | } |
| 189 | |
| 190 | TEST(GainControlBitExactnessTest, |
| 191 | Mono16kHz_AdaptiveDigital_Tl10_SL50_CG5_Lim_AL0_100) { |
| 192 | const int kStreamAnalogLevelReference = 50; |
| 193 | const float kOutputReference[] = {-0.006256f, -0.004395f, -0.002777f}; |
| 194 | RunBitExactnessTest(16000, 1, GainControl::Mode::kAdaptiveDigital, 10, 50, 5, |
| 195 | true, 0, 100, kStreamAnalogLevelReference, |
| 196 | kOutputReference); |
| 197 | } |
| 198 | |
| 199 | TEST(GainControlBitExactnessTest, |
| 200 | Stereo16kHz_AdaptiveDigital_Tl10_SL50_CG5_Lim_AL0_100) { |
| 201 | const int kStreamAnalogLevelReference = 50; |
| 202 | const float kOutputReference[] = {-0.023956f, -0.013947f, -0.024597f, |
| 203 | -0.023956f, -0.013947f, -0.024597f}; |
| 204 | RunBitExactnessTest(16000, 2, GainControl::Mode::kAdaptiveDigital, 10, 50, 5, |
| 205 | true, 0, 100, kStreamAnalogLevelReference, |
| 206 | kOutputReference); |
| 207 | } |
| 208 | |
| 209 | TEST(GainControlBitExactnessTest, |
| 210 | Mono32kHz_AdaptiveDigital_Tl10_SL50_CG5_Lim_AL0_100) { |
| 211 | const int kStreamAnalogLevelReference = 50; |
| 212 | const float kOutputReference[] = {-0.009644f, -0.008728f, -0.007904f}; |
| 213 | RunBitExactnessTest(32000, 1, GainControl::Mode::kAdaptiveDigital, 10, 50, 5, |
| 214 | true, 0, 100, kStreamAnalogLevelReference, |
| 215 | kOutputReference); |
| 216 | } |
| 217 | |
| 218 | TEST(GainControlBitExactnessTest, |
| 219 | Mono48kHz_AdaptiveDigital_Tl10_SL50_CG5_Lim_AL0_100) { |
| 220 | const int kStreamAnalogLevelReference = 50; |
| 221 | const float kOutputReference[] = {-0.009644f, -0.008728f, -0.007904f}; |
| 222 | RunBitExactnessTest(32000, 1, GainControl::Mode::kAdaptiveDigital, 10, 50, 5, |
| 223 | true, 0, 100, kStreamAnalogLevelReference, |
| 224 | kOutputReference); |
| 225 | } |
| 226 | |
| 227 | TEST(GainControlBitExactnessTest, |
| 228 | Mono8kHz_FixedDigital_Tl10_SL50_CG5_Lim_AL0_100) { |
| 229 | const int kStreamAnalogLevelReference = 50; |
| 230 | const float kOutputReference[] = {-0.011871f, -0.004944f, 0.002838f}; |
| 231 | RunBitExactnessTest(8000, 1, GainControl::Mode::kFixedDigital, 10, 50, 5, |
| 232 | true, 0, 100, kStreamAnalogLevelReference, |
| 233 | kOutputReference); |
| 234 | } |
| 235 | |
| 236 | TEST(GainControlBitExactnessTest, |
| 237 | Mono16kHz_FixedDigital_Tl10_SL50_CG5_Lim_AL0_100) { |
| 238 | const int kStreamAnalogLevelReference = 50; |
| 239 | const float kOutputReference[] = {-0.011780f, -0.008270f, -0.005219f}; |
| 240 | RunBitExactnessTest(16000, 1, GainControl::Mode::kFixedDigital, 10, 50, 5, |
| 241 | true, 0, 100, kStreamAnalogLevelReference, |
| 242 | kOutputReference); |
| 243 | } |
| 244 | |
| 245 | TEST(GainControlBitExactnessTest, |
| 246 | Stereo16kHz_FixedDigital_Tl10_SL50_CG5_Lim_AL0_100) { |
| 247 | const int kStreamAnalogLevelReference = 50; |
| 248 | const float kOutputReference[] = {-0.048950f, -0.028503f, -0.050354f, |
| 249 | -0.048950f, -0.028503f, -0.050354f}; |
| 250 | RunBitExactnessTest(16000, 2, GainControl::Mode::kFixedDigital, 10, 50, 5, |
| 251 | true, 0, 100, kStreamAnalogLevelReference, |
| 252 | kOutputReference); |
| 253 | } |
| 254 | |
| 255 | TEST(GainControlBitExactnessTest, |
| 256 | Mono32kHz_FixedDigital_Tl10_SL50_CG5_Lim_AL0_100) { |
| 257 | const int kStreamAnalogLevelReference = 50; |
| 258 | const float kOutputReference[] = {-0.018188f, -0.016418f, -0.014862f}; |
| 259 | RunBitExactnessTest(32000, 1, GainControl::Mode::kFixedDigital, 10, 50, 5, |
| 260 | true, 0, 100, kStreamAnalogLevelReference, |
| 261 | kOutputReference); |
| 262 | } |
| 263 | |
| 264 | TEST(GainControlBitExactnessTest, |
| 265 | Mono48kHz_FixedDigital_Tl10_SL50_CG5_Lim_AL0_100) { |
| 266 | const int kStreamAnalogLevelReference = 50; |
| 267 | const float kOutputReference[] = {-0.018188f, -0.016418f, -0.014862f}; |
| 268 | RunBitExactnessTest(32000, 1, GainControl::Mode::kFixedDigital, 10, 50, 5, |
| 269 | true, 0, 100, kStreamAnalogLevelReference, |
| 270 | kOutputReference); |
| 271 | } |
| 272 | |
| 273 | TEST(GainControlBitExactnessTest, |
| 274 | Mono16kHz_AdaptiveAnalog_Tl10_SL10_CG5_Lim_AL0_100) { |
| 275 | const int kStreamAnalogLevelReference = 12; |
| 276 | const float kOutputReference[] = {-0.006561f, -0.004608f, -0.002899f}; |
| 277 | RunBitExactnessTest(16000, 1, GainControl::Mode::kAdaptiveAnalog, 10, 10, 5, |
| 278 | true, 0, 100, kStreamAnalogLevelReference, |
| 279 | kOutputReference); |
| 280 | } |
| 281 | |
| 282 | TEST(GainControlBitExactnessTest, |
| 283 | Mono16kHz_AdaptiveAnalog_Tl10_SL100_CG5_Lim_AL70_80) { |
| 284 | const int kStreamAnalogLevelReference = 100; |
| 285 | const float kOutputReference[] = {-0.006348f, -0.004456f, -0.002808f}; |
| 286 | RunBitExactnessTest(16000, 1, GainControl::Mode::kAdaptiveAnalog, 10, 100, 5, |
| 287 | true, 70, 80, kStreamAnalogLevelReference, |
| 288 | kOutputReference); |
| 289 | } |
| 290 | |
| 291 | TEST(GainControlBitExactnessTest, |
| 292 | Mono16kHz_AdaptiveDigital_Tl10_SL100_CG5_NoLim_AL0_100) { |
| 293 | const int kStreamAnalogLevelReference = 100; |
| 294 | const float kOutputReference[] = {-0.006592f, -0.004639f, -0.002930f}; |
| 295 | RunBitExactnessTest(16000, 1, GainControl::Mode::kAdaptiveDigital, 10, 100, 5, |
| 296 | false, 0, 100, kStreamAnalogLevelReference, |
| 297 | kOutputReference); |
| 298 | } |
| 299 | |
| 300 | TEST(GainControlBitExactnessTest, |
| 301 | Mono16kHz_AdaptiveDigital_Tl40_SL100_CG5_Lim_AL0_100) { |
| 302 | const int kStreamAnalogLevelReference = 100; |
| 303 | const float kOutputReference[] = {-0.008759f, -0.006134f, -0.003876f}; |
| 304 | RunBitExactnessTest(16000, 1, GainControl::Mode::kAdaptiveDigital, 40, 100, 5, |
| 305 | true, 0, 100, kStreamAnalogLevelReference, |
| 306 | kOutputReference); |
| 307 | } |
| 308 | |
| 309 | TEST(GainControlBitExactnessTest, |
| 310 | Mono16kHz_AdaptiveDigital_Tl10_SL100_CG30_Lim_AL0_100) { |
| 311 | const int kStreamAnalogLevelReference = 100; |
| 312 | const float kOutputReference[] = {-0.006134f, -0.004303f, -0.002716f}; |
| 313 | RunBitExactnessTest(16000, 1, GainControl::Mode::kAdaptiveDigital, 10, 100, |
| 314 | 30, true, 0, 100, kStreamAnalogLevelReference, |
| 315 | kOutputReference); |
| 316 | } |
| 317 | |
| 318 | } // namespace webrtc |
| 319 | |
| 320 | #endif |