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 "webrtc/typedefs.h" |
| 14 #if defined(WEBRTC_ARCH_X86_FAMILY) |
| 15 #include <emmintrin.h> |
| 16 #endif |
| 17 #include <math.h> |
| 18 #include <algorithm> |
| 19 #include <array> |
| 20 #include <functional> |
| 21 #include <numeric> |
| 22 |
| 23 #include "webrtc/common_audio/signal_processing/include/signal_processing_librar
y.h" |
| 24 |
| 25 namespace webrtc { |
| 26 |
| 27 namespace { |
| 28 |
| 29 // Creates an array of uniformly distributed variables. |
| 30 void TableRandomValue(int16_t* vector, int16_t vector_length, uint32_t* seed) { |
| 31 for (int i = 0; i < vector_length; i++) { |
| 32 seed[0] = (seed[0] * ((int32_t)69069) + 1) & (0x80000000 - 1); |
| 33 vector[i] = (int16_t)(seed[0] >> 16); |
| 34 } |
| 35 } |
| 36 |
| 37 } // namespace |
| 38 |
| 39 #if defined(WEBRTC_ARCH_X86_FAMILY) |
| 40 |
| 41 void EstimateComfortNoise_SSE2(const std::array<float, kFftLengthBy2Plus1>& N2, |
| 42 uint32_t* seed, |
| 43 FftData* lower_band_noise, |
| 44 FftData* upper_band_noise) { |
| 45 FftData* N_low = lower_band_noise; |
| 46 FftData* N_high = upper_band_noise; |
| 47 |
| 48 // Compute square root spectrum. |
| 49 std::array<float, kFftLengthBy2Plus1> N; |
| 50 for (size_t k = 0; k < kFftLengthBy2; k += 4) { |
| 51 __m128 v = _mm_loadu_ps(&N2[k]); |
| 52 v = _mm_sqrt_ps(v); |
| 53 _mm_storeu_ps(&N[k], v); |
| 54 } |
| 55 |
| 56 N[kFftLengthBy2] = sqrtf(N2[kFftLengthBy2]); |
| 57 |
| 58 // Compute the noise level for the upper bands. |
| 59 constexpr float kOneByNumBands = 1.f / (kFftLengthBy2Plus1 / 2 + 1); |
| 60 constexpr int kFftLengthBy2Plus1By2 = kFftLengthBy2Plus1 / 2; |
| 61 const float high_band_noise_level = |
| 62 std::accumulate(N.begin() + kFftLengthBy2Plus1By2, N.end(), 0.f) * |
| 63 kOneByNumBands; |
| 64 |
| 65 // Generate complex noise. |
| 66 std::array<int16_t, kFftLengthBy2 - 1> random_values_int; |
| 67 TableRandomValue(random_values_int.data(), random_values_int.size(), seed); |
| 68 |
| 69 std::array<float, kFftLengthBy2 - 1> sin; |
| 70 std::array<float, kFftLengthBy2 - 1> cos; |
| 71 constexpr float kScale = 6.28318530717959f / 32768.0f; |
| 72 std::transform(random_values_int.begin(), random_values_int.end(), |
| 73 sin.begin(), [&](int16_t a) { return -sinf(kScale * a); }); |
| 74 std::transform(random_values_int.begin(), random_values_int.end(), |
| 75 cos.begin(), [&](int16_t a) { return cosf(kScale * a); }); |
| 76 |
| 77 // Form low-frequency noise via spectral shaping. |
| 78 N_low->re[0] = N_low->re[kFftLengthBy2] = N_high->re[0] = |
| 79 N_high->re[kFftLengthBy2] = 0.f; |
| 80 std::transform(cos.begin(), cos.end(), N.begin() + 1, N_low->re.begin() + 1, |
| 81 std::multiplies<float>()); |
| 82 std::transform(sin.begin(), sin.end(), N.begin() + 1, N_low->im.begin() + 1, |
| 83 std::multiplies<float>()); |
| 84 |
| 85 // Form the high-frequency noise via simple levelling. |
| 86 std::transform(cos.begin(), cos.end(), N_high->re.begin() + 1, |
| 87 [&](float a) { return high_band_noise_level * a; }); |
| 88 std::transform(sin.begin(), sin.end(), N_high->im.begin() + 1, |
| 89 [&](float a) { return high_band_noise_level * a; }); |
| 90 } |
| 91 |
| 92 #endif |
| 93 |
| 94 void EstimateComfortNoise(const std::array<float, kFftLengthBy2Plus1>& N2, |
| 95 uint32_t* seed, |
| 96 FftData* lower_band_noise, |
| 97 FftData* upper_band_noise) { |
| 98 FftData* N_low = lower_band_noise; |
| 99 FftData* N_high = upper_band_noise; |
| 100 |
| 101 // Compute square root spectrum. |
| 102 std::array<float, kFftLengthBy2Plus1> N; |
| 103 std::transform(N2.begin(), N2.end(), N.begin(), |
| 104 [](float a) { return sqrtf(a); }); |
| 105 |
| 106 // Compute the noise level for the upper bands. |
| 107 constexpr float kOneByNumBands = 1.f / (kFftLengthBy2Plus1 / 2 + 1); |
| 108 constexpr int kFftLengthBy2Plus1By2 = kFftLengthBy2Plus1 / 2; |
| 109 const float high_band_noise_level = |
| 110 std::accumulate(N.begin() + kFftLengthBy2Plus1By2, N.end(), 0.f) * |
| 111 kOneByNumBands; |
| 112 |
| 113 // Generate complex noise. |
| 114 std::array<int16_t, kFftLengthBy2 - 1> random_values_int; |
| 115 TableRandomValue(random_values_int.data(), random_values_int.size(), seed); |
| 116 |
| 117 std::array<float, kFftLengthBy2 - 1> sin; |
| 118 std::array<float, kFftLengthBy2 - 1> cos; |
| 119 constexpr float kScale = 6.28318530717959f / 32768.0f; |
| 120 std::transform(random_values_int.begin(), random_values_int.end(), |
| 121 sin.begin(), [&](int16_t a) { return -sinf(kScale * a); }); |
| 122 std::transform(random_values_int.begin(), random_values_int.end(), |
| 123 cos.begin(), [&](int16_t a) { return cosf(kScale * a); }); |
| 124 |
| 125 // Form low-frequency noise via spectral shaping. |
| 126 N_low->re[0] = N_low->re[kFftLengthBy2] = N_high->re[0] = |
| 127 N_high->re[kFftLengthBy2] = 0.f; |
| 128 std::transform(cos.begin(), cos.end(), N.begin() + 1, N_low->re.begin() + 1, |
| 129 std::multiplies<float>()); |
| 130 std::transform(sin.begin(), sin.end(), N.begin() + 1, N_low->im.begin() + 1, |
| 131 std::multiplies<float>()); |
| 132 |
| 133 // Form the high-frequency noise via simple levelling. |
| 134 std::transform(cos.begin(), cos.end(), N_high->re.begin() + 1, |
| 135 [&](float a) { return high_band_noise_level * a; }); |
| 136 std::transform(sin.begin(), sin.end(), N_high->im.begin() + 1, |
| 137 [&](float a) { return high_band_noise_level * a; }); |
| 138 } |
| 139 |
| 140 ComfortNoiseGenerator::ComfortNoiseGenerator(Aec3Optimization optimization) |
| 141 : optimization_(optimization), |
| 142 seed_(42), |
| 143 N2_initial_(new std::array<float, kFftLengthBy2Plus1>()) { |
| 144 N2_initial_->fill(0.f); |
| 145 Y2_smoothed_.fill(0.f); |
| 146 N2_.fill(1.0e6f); |
| 147 } |
| 148 |
| 149 ComfortNoiseGenerator::~ComfortNoiseGenerator() = default; |
| 150 |
| 151 void ComfortNoiseGenerator::Compute( |
| 152 const AecState& aec_state, |
| 153 const std::array<float, kFftLengthBy2Plus1>& capture_spectrum, |
| 154 FftData* lower_band_noise, |
| 155 FftData* upper_band_noise) { |
| 156 RTC_DCHECK(lower_band_noise); |
| 157 RTC_DCHECK(upper_band_noise); |
| 158 const auto& Y2 = capture_spectrum; |
| 159 |
| 160 if (!aec_state.SaturatedCapture()) { |
| 161 // Smooth Y2. |
| 162 std::transform(Y2_smoothed_.begin(), Y2_smoothed_.end(), Y2.begin(), |
| 163 Y2_smoothed_.begin(), |
| 164 [](float a, float b) { return a + 0.1f * (b - a); }); |
| 165 |
| 166 if (N2_counter_ > 50) { |
| 167 // Update N2 from Y2_smoothed. |
| 168 std::transform(N2_.begin(), N2_.end(), Y2_smoothed_.begin(), N2_.begin(), |
| 169 [](float a, float b) { |
| 170 return b < a ? (0.9f * b + 0.1f * a) * 1.0002f |
| 171 : a * 1.0002f; |
| 172 }); |
| 173 } |
| 174 |
| 175 if (N2_initial_) { |
| 176 if (++N2_counter_ == 1000) { |
| 177 N2_initial_.reset(); |
| 178 } else { |
| 179 // Compute the N2_initial from N2. |
| 180 std::transform( |
| 181 N2_.begin(), N2_.end(), N2_initial_->begin(), N2_initial_->begin(), |
| 182 [](float a, float b) { return a > b ? b + 0.001f * (a - b) : a; }); |
| 183 } |
| 184 } |
| 185 } |
| 186 |
| 187 // Choose N2 estimate to use. |
| 188 const std::array<float, kFftLengthBy2Plus1>& N2 = |
| 189 N2_initial_ ? *N2_initial_ : N2_; |
| 190 |
| 191 if (optimization_ == Aec3Optimization::kNone) { |
| 192 EstimateComfortNoise(N2, &seed_, lower_band_noise, upper_band_noise); |
| 193 } |
| 194 #if defined(WEBRTC_ARCH_X86_FAMILY) |
| 195 if (optimization_ == Aec3Optimization::kSse2) { |
| 196 EstimateComfortNoise_SSE2(N2, &seed_, lower_band_noise, upper_band_noise); |
| 197 } |
| 198 #endif |
| 199 } |
| 200 |
| 201 } // namespace webrtc |
OLD | NEW |