niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1 | /* |
saza | 0bad15f | 2019-10-16 11:46:11 +0200 | [diff] [blame] | 2 | * Copyright (c) 2019 The WebRTC project authors. All Rights Reserved. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 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 | |
Per Åhgren | 0cbb58e | 2019-10-29 22:59:44 +0100 | [diff] [blame] | 11 | #include "modules/audio_processing/legacy_noise_suppression.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 12 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 13 | #include "modules/audio_processing/audio_buffer.h" |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 14 | #include "rtc_base/checks.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 15 | #if defined(WEBRTC_NS_FLOAT) |
Per Åhgren | 0cbb58e | 2019-10-29 22:59:44 +0100 | [diff] [blame] | 16 | #include "modules/audio_processing/legacy_ns/noise_suppression.h" |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 17 | |
solenberg | 5e465c3 | 2015-12-08 13:22:33 -0800 | [diff] [blame] | 18 | #define NS_CREATE WebRtcNs_Create |
| 19 | #define NS_FREE WebRtcNs_Free |
| 20 | #define NS_INIT WebRtcNs_Init |
| 21 | #define NS_SET_POLICY WebRtcNs_set_policy |
| 22 | typedef NsHandle NsState; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 23 | #elif defined(WEBRTC_NS_FIXED) |
Per Åhgren | 0cbb58e | 2019-10-29 22:59:44 +0100 | [diff] [blame] | 24 | #include "modules/audio_processing/legacy_ns/noise_suppression_x.h" |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 25 | |
solenberg | 5e465c3 | 2015-12-08 13:22:33 -0800 | [diff] [blame] | 26 | #define NS_CREATE WebRtcNsx_Create |
| 27 | #define NS_FREE WebRtcNsx_Free |
| 28 | #define NS_INIT WebRtcNsx_Init |
| 29 | #define NS_SET_POLICY WebRtcNsx_set_policy |
| 30 | typedef NsxHandle NsState; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 31 | #endif |
| 32 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 33 | namespace webrtc { |
saza | 0bad15f | 2019-10-16 11:46:11 +0200 | [diff] [blame] | 34 | namespace { |
| 35 | int NoiseSuppressionLevelToPolicy(NoiseSuppression::Level level) { |
| 36 | switch (level) { |
| 37 | case NoiseSuppression::Level::kLow: |
| 38 | return 0; |
| 39 | case NoiseSuppression::Level::kModerate: |
| 40 | return 1; |
| 41 | case NoiseSuppression::Level::kHigh: |
| 42 | return 2; |
| 43 | case NoiseSuppression::Level::kVeryHigh: |
| 44 | return 3; |
| 45 | default: |
| 46 | RTC_NOTREACHED(); |
| 47 | } |
| 48 | return 1; |
| 49 | } |
| 50 | } // namespace |
| 51 | |
| 52 | class NoiseSuppression::Suppressor { |
solenberg | 5e465c3 | 2015-12-08 13:22:33 -0800 | [diff] [blame] | 53 | public: |
| 54 | explicit Suppressor(int sample_rate_hz) { |
| 55 | state_ = NS_CREATE(); |
| 56 | RTC_CHECK(state_); |
| 57 | int error = NS_INIT(state_, sample_rate_hz); |
| 58 | RTC_DCHECK_EQ(0, error); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 59 | } |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 60 | ~Suppressor() { NS_FREE(state_); } |
saza | 0bad15f | 2019-10-16 11:46:11 +0200 | [diff] [blame] | 61 | |
| 62 | Suppressor(Suppressor&) = delete; |
| 63 | Suppressor& operator=(Suppressor&) = delete; |
| 64 | |
solenberg | 5e465c3 | 2015-12-08 13:22:33 -0800 | [diff] [blame] | 65 | NsState* state() { return state_; } |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 66 | |
solenberg | 5e465c3 | 2015-12-08 13:22:33 -0800 | [diff] [blame] | 67 | private: |
| 68 | NsState* state_ = nullptr; |
solenberg | 5e465c3 | 2015-12-08 13:22:33 -0800 | [diff] [blame] | 69 | }; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 70 | |
saza | 0bad15f | 2019-10-16 11:46:11 +0200 | [diff] [blame] | 71 | NoiseSuppression::NoiseSuppression(size_t channels, |
| 72 | int sample_rate_hz, |
| 73 | Level level) { |
| 74 | const int policy = NoiseSuppressionLevelToPolicy(level); |
| 75 | for (size_t i = 0; i < channels; ++i) { |
| 76 | suppressors_.push_back(std::make_unique<Suppressor>(sample_rate_hz)); |
| 77 | int error = NS_SET_POLICY(suppressors_[i]->state(), policy); |
| 78 | RTC_DCHECK_EQ(0, error); |
solenberg | 29e2f93 | 2015-12-16 01:18:15 -0800 | [diff] [blame] | 79 | } |
solenberg | 5e465c3 | 2015-12-08 13:22:33 -0800 | [diff] [blame] | 80 | } |
| 81 | |
saza | 0bad15f | 2019-10-16 11:46:11 +0200 | [diff] [blame] | 82 | NoiseSuppression::~NoiseSuppression() {} |
| 83 | |
| 84 | void NoiseSuppression::AnalyzeCaptureAudio(AudioBuffer* audio) { |
solenberg | 5e465c3 | 2015-12-08 13:22:33 -0800 | [diff] [blame] | 85 | RTC_DCHECK(audio); |
aluebs@webrtc.org | fda2c2e | 2014-09-18 09:54:06 +0000 | [diff] [blame] | 86 | #if defined(WEBRTC_NS_FLOAT) |
kwiberg | af476c7 | 2016-11-28 15:21:39 -0800 | [diff] [blame] | 87 | RTC_DCHECK_GE(160, audio->num_frames_per_band()); |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 88 | RTC_DCHECK_EQ(suppressors_.size(), audio->num_channels()); |
solenberg | 5e465c3 | 2015-12-08 13:22:33 -0800 | [diff] [blame] | 89 | for (size_t i = 0; i < suppressors_.size(); i++) { |
| 90 | WebRtcNs_Analyze(suppressors_[i]->state(), |
Per Åhgren | d47941e | 2019-08-22 11:51:13 +0200 | [diff] [blame] | 91 | audio->split_bands_const(i)[kBand0To8kHz]); |
aluebs@webrtc.org | fda2c2e | 2014-09-18 09:54:06 +0000 | [diff] [blame] | 92 | } |
| 93 | #endif |
aluebs@webrtc.org | fda2c2e | 2014-09-18 09:54:06 +0000 | [diff] [blame] | 94 | } |
| 95 | |
saza | 0bad15f | 2019-10-16 11:46:11 +0200 | [diff] [blame] | 96 | void NoiseSuppression::ProcessCaptureAudio(AudioBuffer* audio) { |
solenberg | 5e465c3 | 2015-12-08 13:22:33 -0800 | [diff] [blame] | 97 | RTC_DCHECK(audio); |
kwiberg | af476c7 | 2016-11-28 15:21:39 -0800 | [diff] [blame] | 98 | RTC_DCHECK_GE(160, audio->num_frames_per_band()); |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 99 | RTC_DCHECK_EQ(suppressors_.size(), audio->num_channels()); |
solenberg | 5e465c3 | 2015-12-08 13:22:33 -0800 | [diff] [blame] | 100 | for (size_t i = 0; i < suppressors_.size(); i++) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 101 | #if defined(WEBRTC_NS_FLOAT) |
Per Åhgren | d47941e | 2019-08-22 11:51:13 +0200 | [diff] [blame] | 102 | WebRtcNs_Process(suppressors_[i]->state(), audio->split_bands_const(i), |
| 103 | audio->num_bands(), audio->split_bands(i)); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 104 | #elif defined(WEBRTC_NS_FIXED) |
Per Åhgren | 928146f | 2019-08-20 09:19:21 +0200 | [diff] [blame] | 105 | int16_t split_band_data[AudioBuffer::kMaxNumBands] |
| 106 | [AudioBuffer::kMaxSplitFrameLength]; |
| 107 | int16_t* split_bands[AudioBuffer::kMaxNumBands] = { |
| 108 | split_band_data[0], split_band_data[1], split_band_data[2]}; |
Per Åhgren | d47941e | 2019-08-22 11:51:13 +0200 | [diff] [blame] | 109 | audio->ExportSplitChannelData(i, split_bands); |
Per Åhgren | 928146f | 2019-08-20 09:19:21 +0200 | [diff] [blame] | 110 | |
| 111 | WebRtcNsx_Process(suppressors_[i]->state(), split_bands, audio->num_bands(), |
| 112 | split_bands); |
| 113 | |
Per Åhgren | d47941e | 2019-08-22 11:51:13 +0200 | [diff] [blame] | 114 | audio->ImportSplitChannelData(i, split_bands); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 115 | #endif |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 116 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 117 | } |
| 118 | |
saza | 0bad15f | 2019-10-16 11:46:11 +0200 | [diff] [blame] | 119 | float NoiseSuppression::speech_probability() const { |
bjornv@webrtc.org | 08329f4 | 2012-07-12 21:00:43 +0000 | [diff] [blame] | 120 | #if defined(WEBRTC_NS_FLOAT) |
| 121 | float probability_average = 0.0f; |
solenberg | 5e465c3 | 2015-12-08 13:22:33 -0800 | [diff] [blame] | 122 | for (auto& suppressor : suppressors_) { |
| 123 | probability_average += |
| 124 | WebRtcNs_prior_speech_probability(suppressor->state()); |
bjornv@webrtc.org | 08329f4 | 2012-07-12 21:00:43 +0000 | [diff] [blame] | 125 | } |
pkasting | 25702cb | 2016-01-08 13:50:27 -0800 | [diff] [blame] | 126 | if (!suppressors_.empty()) { |
solenberg | 5e465c3 | 2015-12-08 13:22:33 -0800 | [diff] [blame] | 127 | probability_average /= suppressors_.size(); |
| 128 | } |
| 129 | return probability_average; |
bjornv@webrtc.org | 08329f4 | 2012-07-12 21:00:43 +0000 | [diff] [blame] | 130 | #elif defined(WEBRTC_NS_FIXED) |
solenberg | 5e465c3 | 2015-12-08 13:22:33 -0800 | [diff] [blame] | 131 | // TODO(peah): Returning error code as a float! Remove this. |
bjornv@webrtc.org | 08329f4 | 2012-07-12 21:00:43 +0000 | [diff] [blame] | 132 | // Currently not available for the fixed point implementation. |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 133 | return AudioProcessing::kUnsupportedFunctionError; |
bjornv@webrtc.org | 08329f4 | 2012-07-12 21:00:43 +0000 | [diff] [blame] | 134 | #endif |
| 135 | } |
Alejandro Luebs | fa639f0 | 2016-02-09 11:24:32 -0800 | [diff] [blame] | 136 | |
saza | 0bad15f | 2019-10-16 11:46:11 +0200 | [diff] [blame] | 137 | std::vector<float> NoiseSuppression::NoiseEstimate() { |
Alejandro Luebs | fa639f0 | 2016-02-09 11:24:32 -0800 | [diff] [blame] | 138 | std::vector<float> noise_estimate; |
| 139 | #if defined(WEBRTC_NS_FLOAT) |
Alejandro Luebs | 3b14996 | 2016-04-01 13:54:36 -0700 | [diff] [blame] | 140 | const float kNumChannelsFraction = 1.f / suppressors_.size(); |
Alejandro Luebs | fa639f0 | 2016-02-09 11:24:32 -0800 | [diff] [blame] | 141 | noise_estimate.assign(WebRtcNs_num_freq(), 0.f); |
| 142 | for (auto& suppressor : suppressors_) { |
| 143 | const float* noise = WebRtcNs_noise_estimate(suppressor->state()); |
| 144 | for (size_t i = 0; i < noise_estimate.size(); ++i) { |
Alejandro Luebs | 3b14996 | 2016-04-01 13:54:36 -0700 | [diff] [blame] | 145 | noise_estimate[i] += kNumChannelsFraction * noise[i]; |
Alejandro Luebs | fa639f0 | 2016-02-09 11:24:32 -0800 | [diff] [blame] | 146 | } |
| 147 | } |
| 148 | #elif defined(WEBRTC_NS_FIXED) |
Alejandro Luebs | fa639f0 | 2016-02-09 11:24:32 -0800 | [diff] [blame] | 149 | noise_estimate.assign(WebRtcNsx_num_freq(), 0.f); |
| 150 | for (auto& suppressor : suppressors_) { |
Alejandro Luebs | 3b14996 | 2016-04-01 13:54:36 -0700 | [diff] [blame] | 151 | int q_noise; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 152 | const uint32_t* noise = |
| 153 | WebRtcNsx_noise_estimate(suppressor->state(), &q_noise); |
Alejandro Luebs | 3b14996 | 2016-04-01 13:54:36 -0700 | [diff] [blame] | 154 | const float kNormalizationFactor = |
| 155 | 1.f / ((1 << q_noise) * suppressors_.size()); |
Alejandro Luebs | fa639f0 | 2016-02-09 11:24:32 -0800 | [diff] [blame] | 156 | for (size_t i = 0; i < noise_estimate.size(); ++i) { |
Alejandro Luebs | 3b14996 | 2016-04-01 13:54:36 -0700 | [diff] [blame] | 157 | noise_estimate[i] += kNormalizationFactor * noise[i]; |
Alejandro Luebs | fa639f0 | 2016-02-09 11:24:32 -0800 | [diff] [blame] | 158 | } |
| 159 | } |
| 160 | #endif |
| 161 | return noise_estimate; |
| 162 | } |
| 163 | |
saza | 0bad15f | 2019-10-16 11:46:11 +0200 | [diff] [blame] | 164 | size_t NoiseSuppression::num_noise_bins() { |
Alex Luebs | 57ae829 | 2016-03-09 16:24:34 +0100 | [diff] [blame] | 165 | #if defined(WEBRTC_NS_FLOAT) |
| 166 | return WebRtcNs_num_freq(); |
| 167 | #elif defined(WEBRTC_NS_FIXED) |
| 168 | return WebRtcNsx_num_freq(); |
| 169 | #endif |
| 170 | } |
| 171 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 172 | } // namespace webrtc |