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

Side by Side Diff: webrtc/modules/audio_processing/intelligibility/intelligibility_enhancer.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) 2014 The WebRTC project authors. All Rights Reserved. 2 * Copyright (c) 2014 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/intelligibility/intelligibility_enhanc er.h" 11 #include "webrtc/modules/audio_processing/intelligibility/intelligibility_enhanc er.h"
12 12
13 #include <math.h> 13 #include <math.h>
14 #include <stdlib.h> 14 #include <stdlib.h>
15 #include <algorithm> 15 #include <algorithm>
16 #include <limits> 16 #include <limits>
17 #include <numeric> 17 #include <numeric>
18 18
19 #include "webrtc/base/checks.h" 19 #include "webrtc/base/checks.h"
20 #include "webrtc/base/logging.h" 20 #include "webrtc/base/logging.h"
21 #include "webrtc/base/safe_minmax.h"
21 #include "webrtc/common_audio/include/audio_util.h" 22 #include "webrtc/common_audio/include/audio_util.h"
22 #include "webrtc/common_audio/window_generator.h" 23 #include "webrtc/common_audio/window_generator.h"
23 24
24 namespace webrtc { 25 namespace webrtc {
25 26
26 namespace { 27 namespace {
27 28
28 const size_t kErbResolution = 2; 29 const size_t kErbResolution = 2;
29 const int kWindowSizeMs = 16; 30 const int kWindowSizeMs = 16;
30 const int kChunkSizeMs = 10; // Size provided by APM. 31 const int kChunkSizeMs = 10; // Size provided by APM.
(...skipping 249 matching lines...) Expand 10 before | Expand all | Expand 10 after
280 float last_center_freq = center_freqs_[bank_size_ - 1]; 281 float last_center_freq = center_freqs_[bank_size_ - 1];
281 for (size_t i = 0; i < bank_size_; ++i) { 282 for (size_t i = 0; i < bank_size_; ++i) {
282 center_freqs_[i] *= 0.5f * sample_rate_hz_ / last_center_freq; 283 center_freqs_[i] *= 0.5f * sample_rate_hz_ / last_center_freq;
283 } 284 }
284 285
285 for (size_t i = 0; i < bank_size_; ++i) { 286 for (size_t i = 0; i < bank_size_; ++i) {
286 filter_bank[i].resize(num_freqs); 287 filter_bank[i].resize(num_freqs);
287 } 288 }
288 289
289 for (size_t i = 1; i <= bank_size_; ++i) { 290 for (size_t i = 1; i <= bank_size_; ++i) {
290 static const size_t kOne = 1; // Avoids repeated static_cast<>s below. 291 size_t lll = static_cast<size_t>(
291 size_t lll = 292 round(center_freqs_[rtc::SafeMax<size_t>(1, i - lf) - 1] * num_freqs /
292 static_cast<size_t>(round(center_freqs_[std::max(kOne, i - lf) - 1] * 293 (0.5f * sample_rate_hz_)));
293 num_freqs / (0.5f * sample_rate_hz_))); 294 size_t ll = static_cast<size_t>(
294 size_t ll = static_cast<size_t>(round(center_freqs_[std::max(kOne, i) - 1] * 295 round(center_freqs_[rtc::SafeMax<size_t>(1, i) - 1] * num_freqs /
295 num_freqs / (0.5f * sample_rate_hz_))); 296 (0.5f * sample_rate_hz_)));
296 lll = std::min(num_freqs, std::max(lll, kOne)) - 1; 297 lll = rtc::SafeClamp<size_t>(lll, 1, num_freqs) - 1;
297 ll = std::min(num_freqs, std::max(ll, kOne)) - 1; 298 ll = rtc::SafeClamp<size_t>(ll, 1, num_freqs) - 1;
298 299
299 size_t rrr = static_cast<size_t>( 300 size_t rrr = static_cast<size_t>(
300 round(center_freqs_[std::min(bank_size_, i + rf) - 1] * num_freqs / 301 round(center_freqs_[rtc::SafeMin<size_t>(bank_size_, i + rf) - 1] *
301 (0.5f * sample_rate_hz_))); 302 num_freqs / (0.5f * sample_rate_hz_)));
302 size_t rr = static_cast<size_t>( 303 size_t rr = static_cast<size_t>(
303 round(center_freqs_[std::min(bank_size_, i + 1) - 1] * num_freqs / 304 round(center_freqs_[rtc::SafeMin<size_t>(bank_size_, i + 1) - 1] *
304 (0.5f * sample_rate_hz_))); 305 num_freqs / (0.5f * sample_rate_hz_)));
305 rrr = std::min(num_freqs, std::max(rrr, kOne)) - 1; 306 rrr = rtc::SafeClamp<size_t>(rrr, 1, num_freqs) - 1;
306 rr = std::min(num_freqs, std::max(rr, kOne)) - 1; 307 rr = rtc::SafeClamp<size_t>(rr, 1, num_freqs) - 1;
307 308
308 float step = ll == lll ? 0.f : 1.f / (ll - lll); 309 float step = ll == lll ? 0.f : 1.f / (ll - lll);
309 float element = 0.f; 310 float element = 0.f;
310 for (size_t j = lll; j <= ll; ++j) { 311 for (size_t j = lll; j <= ll; ++j) {
311 filter_bank[i - 1][j] = element; 312 filter_bank[i - 1][j] = element;
312 element += step; 313 element += step;
313 } 314 }
314 step = rr == rrr ? 0.f : 1.f / (rrr - rr); 315 step = rr == rrr ? 0.f : 1.f / (rrr - rr);
315 element = 1.f; 316 element = 1.f;
316 for (size_t j = rr; j <= rrr; ++j) { 317 for (size_t j = rr; j <= rrr; ++j) {
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
381 382
382 void IntelligibilityEnhancer::DelayHighBands(AudioBuffer* audio) { 383 void IntelligibilityEnhancer::DelayHighBands(AudioBuffer* audio) {
383 RTC_DCHECK_EQ(audio->num_bands(), high_bands_buffers_.size() + 1); 384 RTC_DCHECK_EQ(audio->num_bands(), high_bands_buffers_.size() + 1);
384 for (size_t i = 0u; i < high_bands_buffers_.size(); ++i) { 385 for (size_t i = 0u; i < high_bands_buffers_.size(); ++i) {
385 Band band = static_cast<Band>(i + 1); 386 Band band = static_cast<Band>(i + 1);
386 high_bands_buffers_[i]->Delay(audio->split_channels_f(band), chunk_length_); 387 high_bands_buffers_[i]->Delay(audio->split_channels_f(band), chunk_length_);
387 } 388 }
388 } 389 }
389 390
390 } // namespace webrtc 391 } // namespace webrtc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698