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

Side by Side Diff: webrtc/modules/audio_processing/aec3/suppression_filter.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/suppression_filter.h" 11 #include "webrtc/modules/audio_processing/aec3/suppression_filter.h"
12 12
13 #include <math.h> 13 #include <math.h>
14 #include <algorithm> 14 #include <algorithm>
15 #include <cstring> 15 #include <cstring>
16 #include <functional> 16 #include <functional>
17 #include <numeric> 17 #include <numeric>
18 18
19 #include "webrtc/base/safe_minmax.h"
19 #include "webrtc/modules/audio_processing/utility/ooura_fft.h" 20 #include "webrtc/modules/audio_processing/utility/ooura_fft.h"
20 21
21 namespace webrtc { 22 namespace webrtc {
22 namespace { 23 namespace {
23 24
24 // Hanning window from Matlab command win = sqrt(hanning(128)). 25 // Hanning window from Matlab command win = sqrt(hanning(128)).
25 const float kSqrtHanning[kFftLength] = { 26 const float kSqrtHanning[kFftLength] = {
26 0.00000000000000f, 0.02454122852291f, 0.04906767432742f, 0.07356456359967f, 27 0.00000000000000f, 0.02454122852291f, 0.04906767432742f, 0.07356456359967f,
27 0.09801714032956f, 0.12241067519922f, 0.14673047445536f, 0.17096188876030f, 28 0.09801714032956f, 0.12241067519922f, 0.14673047445536f, 0.17096188876030f,
28 0.19509032201613f, 0.21910124015687f, 0.24298017990326f, 0.26671275747490f, 29 0.19509032201613f, 0.21910124015687f, 0.24298017990326f, 0.26671275747490f,
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after
115 fft_.Ifft(E, &e_extended); 116 fft_.Ifft(E, &e_extended);
116 std::transform(e_output_old_[0].begin(), e_output_old_[0].end(), 117 std::transform(e_output_old_[0].begin(), e_output_old_[0].end(),
117 std::begin(kSqrtHanning) + kFftLengthBy2, (*e)[0].begin(), 118 std::begin(kSqrtHanning) + kFftLengthBy2, (*e)[0].begin(),
118 [&](float a, float b) { return kIfftNormalization * a * b; }); 119 [&](float a, float b) { return kIfftNormalization * a * b; });
119 std::transform(e_extended.begin(), e_extended.begin() + kFftLengthBy2, 120 std::transform(e_extended.begin(), e_extended.begin() + kFftLengthBy2,
120 std::begin(kSqrtHanning), e_extended.begin(), 121 std::begin(kSqrtHanning), e_extended.begin(),
121 [&](float a, float b) { return kIfftNormalization * a * b; }); 122 [&](float a, float b) { return kIfftNormalization * a * b; });
122 std::transform((*e)[0].begin(), (*e)[0].end(), e_extended.begin(), 123 std::transform((*e)[0].begin(), (*e)[0].end(), e_extended.begin(),
123 (*e)[0].begin(), std::plus<float>()); 124 (*e)[0].begin(), std::plus<float>());
124 std::for_each((*e)[0].begin(), (*e)[0].end(), [](float& x_k) { 125 std::for_each((*e)[0].begin(), (*e)[0].end(), [](float& x_k) {
125 x_k = std::max(std::min(x_k, 32767.0f), -32768.0f); 126 x_k = rtc::SafeClamp(x_k, -32768.f, 32767.f);
126 }); 127 });
127 std::copy(e_extended.begin() + kFftLengthBy2, e_extended.begin() + kFftLength, 128 std::copy(e_extended.begin() + kFftLengthBy2, e_extended.begin() + kFftLength,
128 std::begin(e_output_old_[0])); 129 std::begin(e_output_old_[0]));
129 130
130 if (e->size() > 1) { 131 if (e->size() > 1) {
131 // Form time-domain high-band noise. 132 // Form time-domain high-band noise.
132 std::array<float, kFftLength> time_domain_high_band_noise; 133 std::array<float, kFftLength> time_domain_high_band_noise;
133 std::transform(comfort_noise_high_band.re.begin(), 134 std::transform(comfort_noise_high_band.re.begin(),
134 comfort_noise_high_band.re.end(), E.re.begin(), 135 comfort_noise_high_band.re.end(), E.re.begin(),
135 [&](float a) { return kIfftNormalization * a; }); 136 [&](float a) { return kIfftNormalization * a; });
(...skipping 11 matching lines...) Expand all
147 (*e)[1].begin(), [&](float a, float b) { 148 (*e)[1].begin(), [&](float a, float b) {
148 return std::max( 149 return std::max(
149 std::min(b * high_bands_noise_scaling + high_bands_gain * a, 150 std::min(b * high_bands_noise_scaling + high_bands_gain * a,
150 32767.0f), 151 32767.0f),
151 -32768.0f); 152 -32768.0f);
152 }); 153 });
153 154
154 if (e->size() > 2) { 155 if (e->size() > 2) {
155 RTC_DCHECK_EQ(3, e->size()); 156 RTC_DCHECK_EQ(3, e->size());
156 std::for_each((*e)[2].begin(), (*e)[2].end(), [&](float& a) { 157 std::for_each((*e)[2].begin(), (*e)[2].end(), [&](float& a) {
157 a = std::max(std::min(a * high_bands_gain, 32767.0f), -32768.0f); 158 a = rtc::SafeClamp(a * high_bands_gain, -32768.f, 32767.f);
158 }); 159 });
159 } 160 }
160 161
161 std::array<float, kFftLengthBy2> tmp; 162 std::array<float, kFftLengthBy2> tmp;
162 for (size_t k = 1; k < e->size(); ++k) { 163 for (size_t k = 1; k < e->size(); ++k) {
163 std::copy((*e)[k].begin(), (*e)[k].end(), tmp.begin()); 164 std::copy((*e)[k].begin(), (*e)[k].end(), tmp.begin());
164 std::copy(e_output_old_[k].begin(), e_output_old_[k].end(), 165 std::copy(e_output_old_[k].begin(), e_output_old_[k].end(),
165 (*e)[k].begin()); 166 (*e)[k].begin());
166 std::copy(tmp.begin(), tmp.end(), e_output_old_[k].begin()); 167 std::copy(tmp.begin(), tmp.end(), e_output_old_[k].begin());
167 } 168 }
168 } 169 }
169 } 170 }
170 171
171 } // namespace webrtc 172 } // namespace webrtc
OLDNEW
« no previous file with comments | « webrtc/modules/audio_processing/aec3/subtractor.cc ('k') | webrtc/modules/audio_processing/agc/agc_manager_direct.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698