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/suppression_filter.h" |
| 12 |
| 13 #include <math.h> |
| 14 #include <algorithm> |
| 15 #include <cstring> |
| 16 #include <functional> |
| 17 #include <numeric> |
| 18 |
| 19 #include "webrtc/modules/audio_processing/utility/ooura_fft.h" |
| 20 |
| 21 namespace webrtc { |
| 22 namespace { |
| 23 |
| 24 // Hanning window from Matlab command win = sqrt(hanning(128)). |
| 25 const float kSqrtHanning[kFftLength] = { |
| 26 0.00000000000000f, 0.02454122852291f, 0.04906767432742f, 0.07356456359967f, |
| 27 0.09801714032956f, 0.12241067519922f, 0.14673047445536f, 0.17096188876030f, |
| 28 0.19509032201613f, 0.21910124015687f, 0.24298017990326f, 0.26671275747490f, |
| 29 0.29028467725446f, 0.31368174039889f, 0.33688985339222f, 0.35989503653499f, |
| 30 0.38268343236509f, 0.40524131400499f, 0.42755509343028f, 0.44961132965461f, |
| 31 0.47139673682600f, 0.49289819222978f, 0.51410274419322f, 0.53499761988710f, |
| 32 0.55557023301960f, 0.57580819141785f, 0.59569930449243f, 0.61523159058063f, |
| 33 0.63439328416365f, 0.65317284295378f, 0.67155895484702f, 0.68954054473707f, |
| 34 0.70710678118655f, 0.72424708295147f, 0.74095112535496f, 0.75720884650648f, |
| 35 0.77301045336274f, 0.78834642762661f, 0.80320753148064f, 0.81758481315158f, |
| 36 0.83146961230255f, 0.84485356524971f, 0.85772861000027f, 0.87008699110871f, |
| 37 0.88192126434835f, 0.89322430119552f, 0.90398929312344f, 0.91420975570353f, |
| 38 0.92387953251129f, 0.93299279883474f, 0.94154406518302f, 0.94952818059304f, |
| 39 0.95694033573221f, 0.96377606579544f, 0.97003125319454f, 0.97570213003853f, |
| 40 0.98078528040323f, 0.98527764238894f, 0.98917650996478f, 0.99247953459871f, |
| 41 0.99518472667220f, 0.99729045667869f, 0.99879545620517f, 0.99969881869620f, |
| 42 1.00000000000000f, 0.99969881869620f, 0.99879545620517f, 0.99729045667869f, |
| 43 0.99518472667220f, 0.99247953459871f, 0.98917650996478f, 0.98527764238894f, |
| 44 0.98078528040323f, 0.97570213003853f, 0.97003125319454f, 0.96377606579544f, |
| 45 0.95694033573221f, 0.94952818059304f, 0.94154406518302f, 0.93299279883474f, |
| 46 0.92387953251129f, 0.91420975570353f, 0.90398929312344f, 0.89322430119552f, |
| 47 0.88192126434835f, 0.87008699110871f, 0.85772861000027f, 0.84485356524971f, |
| 48 0.83146961230255f, 0.81758481315158f, 0.80320753148064f, 0.78834642762661f, |
| 49 0.77301045336274f, 0.75720884650648f, 0.74095112535496f, 0.72424708295147f, |
| 50 0.70710678118655f, 0.68954054473707f, 0.67155895484702f, 0.65317284295378f, |
| 51 0.63439328416365f, 0.61523159058063f, 0.59569930449243f, 0.57580819141785f, |
| 52 0.55557023301960f, 0.53499761988710f, 0.51410274419322f, 0.49289819222978f, |
| 53 0.47139673682600f, 0.44961132965461f, 0.42755509343028f, 0.40524131400499f, |
| 54 0.38268343236509f, 0.35989503653499f, 0.33688985339222f, 0.31368174039889f, |
| 55 0.29028467725446f, 0.26671275747490f, 0.24298017990326f, 0.21910124015687f, |
| 56 0.19509032201613f, 0.17096188876030f, 0.14673047445536f, 0.12241067519922f, |
| 57 0.09801714032956f, 0.07356456359967f, 0.04906767432742f, 0.02454122852291f}; |
| 58 |
| 59 } // namespace |
| 60 |
| 61 SuppressionFilter::SuppressionFilter(int sample_rate_hz) |
| 62 : sample_rate_hz_(sample_rate_hz), |
| 63 e_output_old_(NumBandsForRate(sample_rate_hz_)) { |
| 64 RTC_DCHECK(ValidFullBandRate(sample_rate_hz_)); |
| 65 e_input_old_.fill(0.f); |
| 66 std::for_each(e_output_old_.begin(), e_output_old_.end(), |
| 67 [](std::array<float, kFftLengthBy2>& a) { a.fill(0.f); }); |
| 68 } |
| 69 |
| 70 SuppressionFilter::~SuppressionFilter() = default; |
| 71 |
| 72 void SuppressionFilter::ApplyGain( |
| 73 const FftData& comfort_noise, |
| 74 const FftData& comfort_noise_high_band, |
| 75 const std::array<float, kFftLengthBy2Plus1>& suppression_gain, |
| 76 std::vector<std::vector<float>>* e) { |
| 77 RTC_DCHECK(e); |
| 78 RTC_DCHECK_EQ(e->size(), NumBandsForRate(sample_rate_hz_)); |
| 79 FftData E; |
| 80 std::array<float, kFftLength> e_extended; |
| 81 constexpr float kIfftNormalization = 2.f / kFftLength; |
| 82 |
| 83 // Analysis filterbank. |
| 84 std::transform(e_input_old_.begin(), e_input_old_.end(), |
| 85 std::begin(kSqrtHanning), e_extended.begin(), |
| 86 std::multiplies<float>()); |
| 87 std::transform((*e)[0].begin(), (*e)[0].end(), |
| 88 std::begin(kSqrtHanning) + kFftLengthBy2, |
| 89 e_extended.begin() + kFftLengthBy2, std::multiplies<float>()); |
| 90 std::copy((*e)[0].begin(), (*e)[0].end(), e_input_old_.begin()); |
| 91 fft_.Fft(&e_extended, &E); |
| 92 |
| 93 // Apply gain. |
| 94 std::transform(suppression_gain.begin(), suppression_gain.end(), E.re.begin(), |
| 95 E.re.begin(), std::multiplies<float>()); |
| 96 std::transform(suppression_gain.begin(), suppression_gain.end(), E.im.begin(), |
| 97 E.im.begin(), std::multiplies<float>()); |
| 98 |
| 99 // Compute and add the comfort noise. |
| 100 std::array<float, kFftLengthBy2Plus1> scaled_comfort_noise; |
| 101 std::transform(suppression_gain.begin(), suppression_gain.end(), |
| 102 comfort_noise.re.begin(), scaled_comfort_noise.begin(), |
| 103 [](float a, float b) { return std::max(1.f - a, 0.f) * b; }); |
| 104 std::transform(scaled_comfort_noise.begin(), scaled_comfort_noise.end(), |
| 105 E.re.begin(), E.re.begin(), std::plus<float>()); |
| 106 std::transform(suppression_gain.begin(), suppression_gain.end(), |
| 107 comfort_noise.im.begin(), scaled_comfort_noise.begin(), |
| 108 [](float a, float b) { return std::max(1.f - a, 0.f) * b; }); |
| 109 std::transform(scaled_comfort_noise.begin(), scaled_comfort_noise.end(), |
| 110 E.im.begin(), E.im.begin(), std::plus<float>()); |
| 111 |
| 112 // Synthesis filterbank. |
| 113 fft_.Ifft(E, &e_extended); |
| 114 std::transform(e_output_old_[0].begin(), e_output_old_[0].end(), |
| 115 std::begin(kSqrtHanning) + kFftLengthBy2, (*e)[0].begin(), |
| 116 [&](float a, float b) { return kIfftNormalization * a * b; }); |
| 117 std::transform(e_extended.begin(), e_extended.begin() + kFftLengthBy2, |
| 118 std::begin(kSqrtHanning), e_extended.begin(), |
| 119 [&](float a, float b) { return kIfftNormalization * a * b; }); |
| 120 std::transform((*e)[0].begin(), (*e)[0].end(), e_extended.begin(), |
| 121 (*e)[0].begin(), std::plus<float>()); |
| 122 std::for_each((*e)[0].begin(), (*e)[0].end(), [](float& x_k) { |
| 123 x_k = std::max(std::min(x_k, 32767.0f), -32768.0f); |
| 124 }); |
| 125 std::copy(e_extended.begin() + kFftLengthBy2, e_extended.begin() + kFftLength, |
| 126 std::begin(e_output_old_[0])); |
| 127 |
| 128 if (e->size() > 1) { |
| 129 // Form time-domain high-band noise. |
| 130 std::array<float, kFftLength> time_domain_high_band_noise; |
| 131 std::transform(comfort_noise_high_band.re.begin(), |
| 132 comfort_noise_high_band.re.end(), E.re.begin(), |
| 133 [&](float a) { return kIfftNormalization * a; }); |
| 134 std::transform(comfort_noise_high_band.im.begin(), |
| 135 comfort_noise_high_band.im.end(), E.im.begin(), |
| 136 [&](float a) { return kIfftNormalization * a; }); |
| 137 fft_.Ifft(E, &time_domain_high_band_noise); |
| 138 |
| 139 // Scale and apply the noise to the signals. |
| 140 // TODO(peah): Ensure that the high bands are properly delayed. |
| 141 constexpr int kNumBandsAveragingUpperGain = kFftLengthBy2 / 4; |
| 142 constexpr float kOneByNumBandsAveragingUpperGain = |
| 143 1.f / kNumBandsAveragingUpperGain; |
| 144 float high_bands_gain = |
| 145 std::accumulate(suppression_gain.end() - kNumBandsAveragingUpperGain, |
| 146 suppression_gain.end(), 0.f) * |
| 147 kOneByNumBandsAveragingUpperGain; |
| 148 |
| 149 float high_bands_noise_scaling = |
| 150 0.4f * std::max(1.f - high_bands_gain * high_bands_gain, 0.f); |
| 151 |
| 152 std::transform( |
| 153 (*e)[1].begin(), (*e)[1].end(), time_domain_high_band_noise.begin(), |
| 154 (*e)[1].begin(), [&](float a, float b) { |
| 155 return std::max( |
| 156 std::min(b * high_bands_noise_scaling + high_bands_gain * a, |
| 157 32767.0f), |
| 158 -32768.0f); |
| 159 }); |
| 160 |
| 161 if (e->size() > 2) { |
| 162 RTC_DCHECK_EQ(3, e->size()); |
| 163 std::for_each((*e)[2].begin(), (*e)[2].end(), [&](float& a) { |
| 164 a = std::max(std::min(a * high_bands_gain, 32767.0f), -32768.0f); |
| 165 }); |
| 166 } |
| 167 |
| 168 std::array<float, kFftLengthBy2> tmp; |
| 169 for (size_t k = 1; k < e->size(); ++k) { |
| 170 std::copy((*e)[k].begin(), (*e)[k].end(), tmp.begin()); |
| 171 std::copy(e_output_old_[k].begin(), e_output_old_[k].end(), |
| 172 (*e)[k].begin()); |
| 173 std::copy(tmp.begin(), tmp.end(), e_output_old_[k].begin()); |
| 174 } |
| 175 } |
| 176 } |
| 177 |
| 178 } // namespace webrtc |
OLD | NEW |