Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(14)

Side by Side Diff: webrtc/modules/audio_processing/aec3/main_filter_update_gain_unittest.cc

Issue 2808513003: Add SafeClamp(), which accepts args of different types (Closed)
Patch Set: rebase Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (c) 2017 The WebRTC project authors. All Rights Reserved. 2 * Copyright (c) 2017 The WebRTC project authors. All Rights Reserved.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license 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 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 6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may 7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree. 8 * be found in the AUTHORS file in the root of the source tree.
9 */ 9 */
10 10
11 #include "webrtc/modules/audio_processing/aec3/main_filter_update_gain.h" 11 #include "webrtc/modules/audio_processing/aec3/main_filter_update_gain.h"
12 12
13 #include <algorithm> 13 #include <algorithm>
14 #include <numeric> 14 #include <numeric>
15 #include <string> 15 #include <string>
16 16
17 #include "webrtc/base/random.h" 17 #include "webrtc/base/random.h"
18 #include "webrtc/base/safe_minmax.h"
18 #include "webrtc/modules/audio_processing/aec3/adaptive_fir_filter.h" 19 #include "webrtc/modules/audio_processing/aec3/adaptive_fir_filter.h"
19 #include "webrtc/modules/audio_processing/aec3/aec_state.h" 20 #include "webrtc/modules/audio_processing/aec3/aec_state.h"
20 #include "webrtc/modules/audio_processing/aec3/render_buffer.h" 21 #include "webrtc/modules/audio_processing/aec3/render_buffer.h"
21 #include "webrtc/modules/audio_processing/aec3/render_signal_analyzer.h" 22 #include "webrtc/modules/audio_processing/aec3/render_signal_analyzer.h"
22 #include "webrtc/modules/audio_processing/aec3/shadow_filter_update_gain.h" 23 #include "webrtc/modules/audio_processing/aec3/shadow_filter_update_gain.h"
23 #include "webrtc/modules/audio_processing/aec3/subtractor_output.h" 24 #include "webrtc/modules/audio_processing/aec3/subtractor_output.h"
24 #include "webrtc/modules/audio_processing/logging/apm_data_dumper.h" 25 #include "webrtc/modules/audio_processing/logging/apm_data_dumper.h"
25 #include "webrtc/modules/audio_processing/test/echo_canceller_test_tools.h" 26 #include "webrtc/modules/audio_processing/test/echo_canceller_test_tools.h"
26 #include "webrtc/test/gtest.h" 27 #include "webrtc/test/gtest.h"
27 28
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
92 delay_buffer.Delay(x[0], y); 93 delay_buffer.Delay(x[0], y);
93 render_buffer.Insert(x); 94 render_buffer.Insert(x);
94 render_signal_analyzer.Update(render_buffer, aec_state.FilterDelay()); 95 render_signal_analyzer.Update(render_buffer, aec_state.FilterDelay());
95 96
96 // Apply the main filter. 97 // Apply the main filter.
97 main_filter.Filter(render_buffer, &S); 98 main_filter.Filter(render_buffer, &S);
98 fft.Ifft(S, &s); 99 fft.Ifft(S, &s);
99 std::transform(y.begin(), y.end(), s.begin() + kFftLengthBy2, 100 std::transform(y.begin(), y.end(), s.begin() + kFftLengthBy2,
100 e_main.begin(), 101 e_main.begin(),
101 [&](float a, float b) { return a - b * kScale; }); 102 [&](float a, float b) { return a - b * kScale; });
102 std::for_each(e_main.begin(), e_main.end(), [](float& a) { 103 std::for_each(e_main.begin(), e_main.end(),
103 a = std::max(std::min(a, 32767.0f), -32768.0f); 104 [](float& a) { a = rtc::SafeClamp(a, -32768.f, 32767.f); });
104 });
105 fft.ZeroPaddedFft(e_main, &E_main); 105 fft.ZeroPaddedFft(e_main, &E_main);
106 106
107 // Apply the shadow filter. 107 // Apply the shadow filter.
108 shadow_filter.Filter(render_buffer, &S); 108 shadow_filter.Filter(render_buffer, &S);
109 fft.Ifft(S, &s); 109 fft.Ifft(S, &s);
110 std::transform(y.begin(), y.end(), s.begin() + kFftLengthBy2, 110 std::transform(y.begin(), y.end(), s.begin() + kFftLengthBy2,
111 e_shadow.begin(), 111 e_shadow.begin(),
112 [&](float a, float b) { return a - b * kScale; }); 112 [&](float a, float b) { return a - b * kScale; });
113 std::for_each(e_shadow.begin(), e_shadow.end(), [](float& a) { 113 std::for_each(e_shadow.begin(), e_shadow.end(),
114 a = std::max(std::min(a, 32767.0f), -32768.0f); 114 [](float& a) { a = rtc::SafeClamp(a, -32768.f, 32767.f); });
115 });
116 fft.ZeroPaddedFft(e_shadow, &E_shadow); 115 fft.ZeroPaddedFft(e_shadow, &E_shadow);
117 116
118 // Compute spectra for future use. 117 // Compute spectra for future use.
119 E_main.Spectrum(Aec3Optimization::kNone, &output.E2_main); 118 E_main.Spectrum(Aec3Optimization::kNone, &output.E2_main);
120 E_shadow.Spectrum(Aec3Optimization::kNone, &output.E2_shadow); 119 E_shadow.Spectrum(Aec3Optimization::kNone, &output.E2_shadow);
121 120
122 // Adapt the shadow filter. 121 // Adapt the shadow filter.
123 shadow_gain.Compute(render_buffer, render_signal_analyzer, E_shadow, 122 shadow_gain.Compute(render_buffer, render_signal_analyzer, E_shadow,
124 shadow_filter.SizePartitions(), saturation, &G); 123 shadow_filter.SizePartitions(), saturation, &G);
125 shadow_filter.Adapt(render_buffer, G); 124 shadow_filter.Adapt(render_buffer, G);
(...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after
279 blocks_with_saturation, false, &e, &y, &G_b); 278 blocks_with_saturation, false, &e, &y, &G_b);
280 279
281 G_a.Spectrum(Aec3Optimization::kNone, &G_a_power); 280 G_a.Spectrum(Aec3Optimization::kNone, &G_a_power);
282 G_b.Spectrum(Aec3Optimization::kNone, &G_b_power); 281 G_b.Spectrum(Aec3Optimization::kNone, &G_b_power);
283 282
284 EXPECT_LT(std::accumulate(G_a_power.begin(), G_a_power.end(), 0.), 283 EXPECT_LT(std::accumulate(G_a_power.begin(), G_a_power.end(), 0.),
285 std::accumulate(G_b_power.begin(), G_b_power.end(), 0.)); 284 std::accumulate(G_b_power.begin(), G_b_power.end(), 0.));
286 } 285 }
287 286
288 } // namespace webrtc 287 } // namespace webrtc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698