peah | d4f6ea7 | 2016-03-29 00:37:44 -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/echo_cancellation_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 SetupComponent(int sample_rate_hz, |
| 28 | EchoCancellation::SuppressionLevel suppression_level, |
| 29 | bool drift_compensation_enabled, |
| 30 | EchoCancellationImpl* echo_canceller) { |
| 31 | echo_canceller->Initialize(sample_rate_hz, 1, 1, 1); |
| 32 | EchoCancellation* ec = static_cast<EchoCancellation*>(echo_canceller); |
| 33 | ec->Enable(true); |
| 34 | ec->set_suppression_level(suppression_level); |
| 35 | ec->enable_drift_compensation(drift_compensation_enabled); |
| 36 | |
| 37 | Config config; |
| 38 | config.Set<DelayAgnostic>(new DelayAgnostic(true)); |
| 39 | config.Set<ExtendedFilter>(new ExtendedFilter(true)); |
| 40 | echo_canceller->SetExtraOptions(config); |
| 41 | } |
| 42 | |
| 43 | void ProcessOneFrame(int sample_rate_hz, |
| 44 | int stream_delay_ms, |
| 45 | bool drift_compensation_enabled, |
| 46 | int stream_drift_samples, |
| 47 | AudioBuffer* render_audio_buffer, |
| 48 | AudioBuffer* capture_audio_buffer, |
| 49 | EchoCancellationImpl* echo_canceller) { |
| 50 | if (sample_rate_hz > AudioProcessing::kSampleRate16kHz) { |
| 51 | render_audio_buffer->SplitIntoFrequencyBands(); |
| 52 | capture_audio_buffer->SplitIntoFrequencyBands(); |
| 53 | } |
| 54 | |
| 55 | echo_canceller->ProcessRenderAudio(render_audio_buffer); |
| 56 | |
| 57 | if (drift_compensation_enabled) { |
| 58 | static_cast<EchoCancellation*>(echo_canceller) |
| 59 | ->set_stream_drift_samples(stream_drift_samples); |
| 60 | } |
| 61 | |
| 62 | echo_canceller->ProcessCaptureAudio(capture_audio_buffer, stream_delay_ms); |
| 63 | |
| 64 | if (sample_rate_hz > AudioProcessing::kSampleRate16kHz) { |
| 65 | capture_audio_buffer->MergeFrequencyBands(); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | void RunBitexactnessTest(int sample_rate_hz, |
| 70 | size_t num_channels, |
| 71 | int stream_delay_ms, |
| 72 | bool drift_compensation_enabled, |
| 73 | int stream_drift_samples, |
| 74 | EchoCancellation::SuppressionLevel suppression_level, |
| 75 | bool stream_has_echo_reference, |
| 76 | const rtc::ArrayView<const float>& output_reference) { |
| 77 | rtc::CriticalSection crit_render; |
| 78 | rtc::CriticalSection crit_capture; |
| 79 | EchoCancellationImpl echo_canceller(&crit_render, &crit_capture); |
| 80 | SetupComponent(sample_rate_hz, suppression_level, drift_compensation_enabled, |
| 81 | &echo_canceller); |
| 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, stream_delay_ms, drift_compensation_enabled, |
| 111 | stream_drift_samples, &render_buffer, &capture_buffer, |
| 112 | &echo_canceller); |
| 113 | } |
| 114 | |
| 115 | // Extract and verify the test results. |
| 116 | std::vector<float> capture_output; |
| 117 | test::ExtractVectorFromAudioBuffer(capture_config, &capture_buffer, |
| 118 | &capture_output); |
| 119 | |
| 120 | EXPECT_EQ(stream_has_echo_reference, |
| 121 | static_cast<EchoCancellation*>(&echo_canceller)->stream_has_echo()); |
| 122 | |
| 123 | // Compare the output with the reference. Only the first values of the output |
| 124 | // from last frame processed are compared in order not having to specify all |
| 125 | // preceeding frames as testvectors. As the algorithm being tested has a |
| 126 | // memory, testing only the last frame implicitly also tests the preceeding |
| 127 | // frames. |
peah | 7ea928e | 2016-03-30 08:13:57 -0700 | [diff] [blame^] | 128 | const float kElementErrorBound = 1.0f / 32768.0f; |
| 129 | EXPECT_TRUE(test::VerifyDeinterleavedArray( |
peah | d4f6ea7 | 2016-03-29 00:37:44 -0700 | [diff] [blame] | 130 | capture_config.num_frames(), capture_config.num_channels(), |
peah | 7ea928e | 2016-03-30 08:13:57 -0700 | [diff] [blame^] | 131 | output_reference, capture_output, kElementErrorBound)); |
peah | d4f6ea7 | 2016-03-29 00:37:44 -0700 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | const bool kStreamHasEchoReference = false; |
| 135 | |
| 136 | } // namespace |
| 137 | |
| 138 | // TODO(peah): Activate all these tests for ARM and ARM64 once the issue on the |
| 139 | // Chromium ARM and ARM64 boths have been identified. |
| 140 | |
| 141 | TEST(EchoCancellationBitExactnessTest, |
| 142 | Mono8kHz_HighLevel_NoDrift_StreamDelay0) { |
| 143 | const float kOutputReference[] = {-0.006622f, -0.002747f, 0.001587f}; |
| 144 | RunBitexactnessTest(8000, 1, 0, false, 0, |
| 145 | EchoCancellation::SuppressionLevel::kHighSuppression, |
| 146 | kStreamHasEchoReference, kOutputReference); |
| 147 | } |
| 148 | |
| 149 | TEST(EchoCancellationBitExactnessTest, |
| 150 | Mono16kHz_HighLevel_NoDrift_StreamDelay0) { |
| 151 | const float kOutputReference[] = {-0.006561f, -0.004608f, -0.002899f}; |
| 152 | RunBitexactnessTest(16000, 1, 0, false, 0, |
| 153 | EchoCancellation::SuppressionLevel::kHighSuppression, |
| 154 | kStreamHasEchoReference, kOutputReference); |
| 155 | } |
| 156 | |
| 157 | TEST(EchoCancellationBitExactnessTest, |
| 158 | Mono32kHz_HighLevel_NoDrift_StreamDelay0) { |
| 159 | const float kOutputReference[] = {-0.010162f, -0.009155f, -0.008301f}; |
| 160 | RunBitexactnessTest(32000, 1, 0, false, 0, |
| 161 | EchoCancellation::SuppressionLevel::kHighSuppression, |
| 162 | kStreamHasEchoReference, kOutputReference); |
| 163 | } |
| 164 | |
| 165 | TEST(EchoCancellationBitExactnessTest, |
| 166 | Mono48kHz_HighLevel_NoDrift_StreamDelay0) { |
| 167 | const float kOutputReference[] = {-0.009554f, -0.009857f, -0.009868f}; |
| 168 | RunBitexactnessTest(48000, 1, 0, false, 0, |
| 169 | EchoCancellation::SuppressionLevel::kHighSuppression, |
| 170 | kStreamHasEchoReference, kOutputReference); |
| 171 | } |
| 172 | |
| 173 | TEST(EchoCancellationBitExactnessTest, |
| 174 | Mono16kHz_LowLevel_NoDrift_StreamDelay0) { |
| 175 | const float kOutputReference[] = {-0.006561f, -0.004608f, -0.002899f}; |
| 176 | RunBitexactnessTest(16000, 1, 0, false, 0, |
| 177 | EchoCancellation::SuppressionLevel::kLowSuppression, |
| 178 | kStreamHasEchoReference, kOutputReference); |
| 179 | } |
| 180 | |
| 181 | TEST(EchoCancellationBitExactnessTest, |
| 182 | Mono16kHz_ModerateLevel_NoDrift_StreamDelay0) { |
| 183 | const float kOutputReference[] = {-0.006561f, -0.004608f, -0.002899f}; |
| 184 | RunBitexactnessTest(16000, 1, 0, false, 0, |
| 185 | EchoCancellation::SuppressionLevel::kModerateSuppression, |
| 186 | kStreamHasEchoReference, kOutputReference); |
| 187 | } |
| 188 | |
| 189 | TEST(EchoCancellationBitExactnessTest, |
| 190 | Mono16kHz_HighLevel_NoDrift_StreamDelay10) { |
| 191 | const float kOutputReference[] = {-0.006561f, -0.004608f, -0.002899f}; |
| 192 | RunBitexactnessTest(16000, 1, 10, false, 0, |
| 193 | EchoCancellation::SuppressionLevel::kHighSuppression, |
| 194 | kStreamHasEchoReference, kOutputReference); |
| 195 | } |
| 196 | |
| 197 | TEST(EchoCancellationBitExactnessTest, |
| 198 | Mono16kHz_HighLevel_NoDrift_StreamDelay20) { |
| 199 | const float kOutputReference[] = {-0.006561f, -0.004608f, -0.002899f}; |
| 200 | RunBitexactnessTest(16000, 1, 20, false, 0, |
| 201 | EchoCancellation::SuppressionLevel::kHighSuppression, |
| 202 | kStreamHasEchoReference, kOutputReference); |
| 203 | } |
| 204 | |
| 205 | TEST(EchoCancellationBitExactnessTest, |
| 206 | Mono16kHz_HighLevel_Drift0_StreamDelay0) { |
| 207 | const float kOutputReference[] = {-0.006561f, -0.004608f, -0.002899f}; |
| 208 | RunBitexactnessTest(16000, 1, 0, true, 0, |
| 209 | EchoCancellation::SuppressionLevel::kHighSuppression, |
| 210 | kStreamHasEchoReference, kOutputReference); |
| 211 | } |
| 212 | |
| 213 | TEST(EchoCancellationBitExactnessTest, |
| 214 | Mono16kHz_HighLevel_Drift5_StreamDelay0) { |
| 215 | const float kOutputReference[] = {-0.006561f, -0.004608f, -0.002899f}; |
| 216 | RunBitexactnessTest(16000, 1, 0, true, 5, |
| 217 | EchoCancellation::SuppressionLevel::kHighSuppression, |
| 218 | kStreamHasEchoReference, kOutputReference); |
| 219 | } |
| 220 | |
| 221 | TEST(EchoCancellationBitExactnessTest, |
| 222 | Stereo8kHz_HighLevel_NoDrift_StreamDelay0) { |
| 223 | const float kOutputReference[] = {-0.027359f, -0.015823f, -0.028488f, |
| 224 | -0.027359f, -0.015823f, -0.028488f}; |
| 225 | RunBitexactnessTest(8000, 2, 0, false, 0, |
| 226 | EchoCancellation::SuppressionLevel::kHighSuppression, |
| 227 | kStreamHasEchoReference, kOutputReference); |
| 228 | } |
| 229 | |
| 230 | TEST(EchoCancellationBitExactnessTest, |
| 231 | Stereo16kHz_HighLevel_NoDrift_StreamDelay0) { |
| 232 | const float kOutputReference[] = {-0.027298f, -0.015900f, -0.028107f, |
| 233 | -0.027298f, -0.015900f, -0.028107f}; |
| 234 | RunBitexactnessTest(16000, 2, 0, false, 0, |
| 235 | EchoCancellation::SuppressionLevel::kHighSuppression, |
| 236 | kStreamHasEchoReference, kOutputReference); |
| 237 | } |
| 238 | |
| 239 | TEST(EchoCancellationBitExactnessTest, |
| 240 | Stereo32kHz_HighLevel_NoDrift_StreamDelay0) { |
| 241 | const float kOutputReference[] = {0.004547f, -0.004456f, -0.000946f, |
| 242 | 0.004547f, -0.004456f, -0.000946f}; |
| 243 | RunBitexactnessTest(32000, 2, 0, false, 0, |
| 244 | EchoCancellation::SuppressionLevel::kHighSuppression, |
| 245 | kStreamHasEchoReference, kOutputReference); |
| 246 | } |
| 247 | |
| 248 | TEST(EchoCancellationBitExactnessTest, |
| 249 | Stereo48kHz_HighLevel_NoDrift_StreamDelay0) { |
| 250 | const float kOutputReference[] = {-0.003500f, -0.001894f, -0.003176f, |
| 251 | -0.003500f, -0.001894f, -0.003176f}; |
| 252 | RunBitexactnessTest(48000, 2, 0, false, 0, |
| 253 | EchoCancellation::SuppressionLevel::kHighSuppression, |
| 254 | kStreamHasEchoReference, kOutputReference); |
| 255 | } |
| 256 | |
| 257 | } // namespace webrtc |
| 258 | |
| 259 | #endif |