OLD | NEW |
---|---|
(Empty) | |
1 /* | |
2 * Copyright (c) 2017 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 | |
11 #include "webrtc/modules/audio_processing/aec3/comfort_noise_generator.h" | |
12 | |
13 #include <math.h> | |
14 #include <algorithm> | |
15 #include <array> | |
16 #include <functional> | |
17 #include <numeric> | |
18 | |
19 #include "webrtc/common_audio/signal_processing/include/signal_processing_librar y.h" | |
20 | |
21 namespace webrtc { | |
22 | |
23 ComfortNoiseGenerator::ComfortNoiseGenerator() | |
24 : seed_(42), N2_initial_(new std::array<float, kFftLengthBy2Plus1>()) { | |
25 N2_initial_->fill(0.f); | |
26 Y2_smoothed_.fill(0.f); | |
27 N2_.fill(1.0e6f); | |
28 } | |
29 | |
30 ComfortNoiseGenerator::~ComfortNoiseGenerator() = default; | |
31 | |
32 void ComfortNoiseGenerator::Compute( | |
33 const std::array<float, kFftLengthBy2Plus1>& capture_spectrum, | |
34 FftData* lower_band_noise, | |
35 FftData* upper_band_noise) { | |
36 RTC_DCHECK(lower_band_noise); | |
37 RTC_DCHECK(upper_band_noise); | |
38 const std::array<float, kFftLengthBy2Plus1>& Y2 = capture_spectrum; | |
ivoc
2017/02/10 13:52:33
I would prefer const auto&
peah-webrtc
2017/02/20 07:37:15
Nice!
Done.
| |
39 FftData* N_low = lower_band_noise; | |
40 FftData* N_high = upper_band_noise; | |
41 | |
42 // Smooth Y2. | |
43 std::transform(Y2_smoothed_.begin(), Y2_smoothed_.end(), Y2.begin(), | |
44 Y2_smoothed_.begin(), | |
45 [](float a, float b) { return a + 0.1f * (b - a); }); | |
ivoc
2017/02/10 13:52:33
Out of curiousity, what kind of filter is this?
peah-webrtc
2017/02/20 07:37:15
This is a low-pass filter: Y2_smoothed(t)=0.9f*Y2_
ivoc
2017/02/21 17:26:00
ah, I should have seen that :)
peah-webrtc
2017/02/22 23:51:35
No worries! Good question!
| |
46 | |
47 if (N2_counter_ > 50) { | |
48 // Update N2 from Y2_smoothed. | |
49 std::transform(N2_.begin(), N2_.end(), Y2_smoothed_.begin(), N2_.begin(), | |
50 [](float a, float b) { | |
51 return b < a ? (0.9f * b + 0.1f * a) * 1.0002f | |
52 : a * 1.0002f; | |
53 }); | |
54 } | |
55 | |
56 if (N2_initial_) { | |
57 if (++N2_counter_ == 1000) { | |
58 N2_initial_.reset(); | |
59 } else { | |
60 // Compute initial N2 from N2. | |
ivoc
2017/02/10 13:52:33
This is a bit confusing. I would rephrase as "Comp
peah-webrtc
2017/02/20 07:37:15
Done.
| |
61 std::transform( | |
62 N2_.begin(), N2_.end(), N2_initial_->begin(), N2_initial_->begin(), | |
63 [](float a, float b) { return a > b ? b + 0.001f * (a - b) : a; }); | |
64 } | |
65 } | |
66 | |
67 // Choose N2 estimate to use. | |
68 const std::array<float, kFftLengthBy2Plus1>& N2 = | |
69 N2_initial_ ? *N2_initial_ : N2_; | |
70 | |
71 // Compute square root spectrum. | |
72 std::array<float, kFftLengthBy2Plus1> N; | |
73 std::transform(N2.begin(), N2.end(), N.begin(), | |
74 [](float a) { return sqrtf(a); }); | |
75 | |
76 // Compute the noise level for the upper bands. | |
77 const float high_band_noise_level = | |
78 std::accumulate(N.begin() + kFftLengthBy2Plus1 / 2, N.end(), 0.f, | |
79 [](float a, float b) { return a + b; }) / | |
ivoc
2017/02/10 13:52:33
The last argument (the lambda) is not needed, accu
peah-webrtc
2017/02/20 07:37:15
Done.
| |
80 (kFftLengthBy2Plus1 / 2 + 1); | |
81 | |
82 // Generate complex noise. | |
83 std::array<int16_t, kFftLengthBy2 - 1> random_values_int; | |
84 WebRtcSpl_RandUArray(random_values_int.data(), random_values_int.size(), | |
85 &seed_); | |
86 | |
87 std::array<float, kFftLengthBy2 - 1> sin; | |
88 std::array<float, kFftLengthBy2 - 1> cos; | |
89 const float kScale = 6.28318530717959f * 1.f / 32768.0f; | |
ivoc
2017/02/10 13:52:33
The "* 1.f" can be removed, right? Also, this can
peah-webrtc
2017/02/20 07:37:15
Done.
| |
90 std::transform(random_values_int.begin(), random_values_int.end(), | |
91 sin.begin(), [&](int16_t a) { return -sinf(kScale * a); }); | |
92 std::transform(random_values_int.begin(), random_values_int.end(), | |
93 cos.begin(), [&](int16_t a) { return cosf(kScale * a); }); | |
94 | |
95 // Form low-frequency noise via spectral shaping. | |
96 N_low->re[0] = N_low->re[kFftLengthBy2] = N_high->re[0] = | |
97 N_high->re[kFftLengthBy2] = 0.f; | |
98 std::transform(cos.begin(), cos.end(), N.begin() + 1, N_low->re.begin() + 1, | |
99 std::multiplies<float>()); | |
100 std::transform(sin.begin(), sin.end(), N.begin() + 1, N_low->im.begin() + 1, | |
101 std::multiplies<float>()); | |
102 | |
103 // Form the high-frequency noise via simple levelling. | |
104 std::transform(cos.begin(), cos.end(), N_high->re.begin() + 1, | |
105 [&](float a) { return high_band_noise_level * a; }); | |
106 std::transform(sin.begin(), sin.end(), N_high->im.begin() + 1, | |
107 [&](float a) { return high_band_noise_level * a; }); | |
108 | |
109 // TODO(peah): Remove when landing. | |
ivoc
2017/02/10 13:52:33
Is that now?
peah-webrtc
2017/02/20 07:37:15
Should be :-) Thanks!
Done-
| |
110 std::array<float, kFftLength> noiseP; | |
111 std::array<float, kFftLength> noise_high_bandP; | |
112 N_low->CopyToPackedArray(&noiseP); | |
113 N_high->CopyToPackedArray(&noise_high_bandP); | |
114 } | |
115 | |
116 } // namespace webrtc | |
OLD | NEW |