niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1 | /* |
bjornv@webrtc.org | 0c6f931 | 2012-01-30 09:39:08 +0000 | [diff] [blame] | 2 | * Copyright (c) 2012 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "modules/audio_processing/noise_suppression_impl.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" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame^] | 15 | #include "rtc_base/constructor_magic.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 16 | #if defined(WEBRTC_NS_FLOAT) |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 17 | #include "modules/audio_processing/ns/noise_suppression.h" |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 18 | |
solenberg | 5e465c3 | 2015-12-08 13:22:33 -0800 | [diff] [blame] | 19 | #define NS_CREATE WebRtcNs_Create |
| 20 | #define NS_FREE WebRtcNs_Free |
| 21 | #define NS_INIT WebRtcNs_Init |
| 22 | #define NS_SET_POLICY WebRtcNs_set_policy |
| 23 | typedef NsHandle NsState; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 24 | #elif defined(WEBRTC_NS_FIXED) |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 25 | #include "modules/audio_processing/ns/noise_suppression_x.h" |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 26 | |
solenberg | 5e465c3 | 2015-12-08 13:22:33 -0800 | [diff] [blame] | 27 | #define NS_CREATE WebRtcNsx_Create |
| 28 | #define NS_FREE WebRtcNsx_Free |
| 29 | #define NS_INIT WebRtcNsx_Init |
| 30 | #define NS_SET_POLICY WebRtcNsx_set_policy |
| 31 | typedef NsxHandle NsState; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 32 | #endif |
| 33 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 34 | namespace webrtc { |
solenberg | 5e465c3 | 2015-12-08 13:22:33 -0800 | [diff] [blame] | 35 | class NoiseSuppressionImpl::Suppressor { |
| 36 | public: |
| 37 | explicit Suppressor(int sample_rate_hz) { |
| 38 | state_ = NS_CREATE(); |
| 39 | RTC_CHECK(state_); |
| 40 | int error = NS_INIT(state_, sample_rate_hz); |
| 41 | RTC_DCHECK_EQ(0, error); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 42 | } |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 43 | ~Suppressor() { NS_FREE(state_); } |
solenberg | 5e465c3 | 2015-12-08 13:22:33 -0800 | [diff] [blame] | 44 | NsState* state() { return state_; } |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 45 | |
solenberg | 5e465c3 | 2015-12-08 13:22:33 -0800 | [diff] [blame] | 46 | private: |
| 47 | NsState* state_ = nullptr; |
| 48 | RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(Suppressor); |
| 49 | }; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 50 | |
solenberg | 5e465c3 | 2015-12-08 13:22:33 -0800 | [diff] [blame] | 51 | NoiseSuppressionImpl::NoiseSuppressionImpl(rtc::CriticalSection* crit) |
| 52 | : crit_(crit) { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 53 | RTC_DCHECK(crit); |
| 54 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 55 | |
| 56 | NoiseSuppressionImpl::~NoiseSuppressionImpl() {} |
| 57 | |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 58 | void NoiseSuppressionImpl::Initialize(size_t channels, int sample_rate_hz) { |
solenberg | 5e465c3 | 2015-12-08 13:22:33 -0800 | [diff] [blame] | 59 | rtc::CritScope cs(crit_); |
solenberg | 29e2f93 | 2015-12-16 01:18:15 -0800 | [diff] [blame] | 60 | channels_ = channels; |
| 61 | sample_rate_hz_ = sample_rate_hz; |
kwiberg | 88788ad | 2016-02-19 07:04:49 -0800 | [diff] [blame] | 62 | std::vector<std::unique_ptr<Suppressor>> new_suppressors; |
solenberg | 29e2f93 | 2015-12-16 01:18:15 -0800 | [diff] [blame] | 63 | if (enabled_) { |
| 64 | new_suppressors.resize(channels); |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 65 | for (size_t i = 0; i < channels; i++) { |
solenberg | 29e2f93 | 2015-12-16 01:18:15 -0800 | [diff] [blame] | 66 | new_suppressors[i].reset(new Suppressor(sample_rate_hz)); |
| 67 | } |
| 68 | } |
solenberg | 5e465c3 | 2015-12-08 13:22:33 -0800 | [diff] [blame] | 69 | suppressors_.swap(new_suppressors); |
| 70 | set_level(level_); |
| 71 | } |
| 72 | |
solenberg | 29e2f93 | 2015-12-16 01:18:15 -0800 | [diff] [blame] | 73 | void NoiseSuppressionImpl::AnalyzeCaptureAudio(AudioBuffer* audio) { |
solenberg | 5e465c3 | 2015-12-08 13:22:33 -0800 | [diff] [blame] | 74 | RTC_DCHECK(audio); |
aluebs@webrtc.org | fda2c2e | 2014-09-18 09:54:06 +0000 | [diff] [blame] | 75 | #if defined(WEBRTC_NS_FLOAT) |
solenberg | 5e465c3 | 2015-12-08 13:22:33 -0800 | [diff] [blame] | 76 | rtc::CritScope cs(crit_); |
| 77 | if (!enabled_) { |
solenberg | 29e2f93 | 2015-12-16 01:18:15 -0800 | [diff] [blame] | 78 | return; |
aluebs@webrtc.org | fda2c2e | 2014-09-18 09:54:06 +0000 | [diff] [blame] | 79 | } |
aluebs@webrtc.org | fda2c2e | 2014-09-18 09:54:06 +0000 | [diff] [blame] | 80 | |
kwiberg | af476c7 | 2016-11-28 15:21:39 -0800 | [diff] [blame] | 81 | RTC_DCHECK_GE(160, audio->num_frames_per_band()); |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 82 | RTC_DCHECK_EQ(suppressors_.size(), audio->num_channels()); |
solenberg | 5e465c3 | 2015-12-08 13:22:33 -0800 | [diff] [blame] | 83 | for (size_t i = 0; i < suppressors_.size(); i++) { |
| 84 | WebRtcNs_Analyze(suppressors_[i]->state(), |
| 85 | audio->split_bands_const_f(i)[kBand0To8kHz]); |
aluebs@webrtc.org | fda2c2e | 2014-09-18 09:54:06 +0000 | [diff] [blame] | 86 | } |
| 87 | #endif |
aluebs@webrtc.org | fda2c2e | 2014-09-18 09:54:06 +0000 | [diff] [blame] | 88 | } |
| 89 | |
solenberg | 29e2f93 | 2015-12-16 01:18:15 -0800 | [diff] [blame] | 90 | void NoiseSuppressionImpl::ProcessCaptureAudio(AudioBuffer* audio) { |
solenberg | 5e465c3 | 2015-12-08 13:22:33 -0800 | [diff] [blame] | 91 | RTC_DCHECK(audio); |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 92 | rtc::CritScope cs(crit_); |
solenberg | 5e465c3 | 2015-12-08 13:22:33 -0800 | [diff] [blame] | 93 | if (!enabled_) { |
solenberg | 29e2f93 | 2015-12-16 01:18:15 -0800 | [diff] [blame] | 94 | return; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 95 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 96 | |
kwiberg | af476c7 | 2016-11-28 15:21:39 -0800 | [diff] [blame] | 97 | RTC_DCHECK_GE(160, audio->num_frames_per_band()); |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 98 | RTC_DCHECK_EQ(suppressors_.size(), audio->num_channels()); |
solenberg | 5e465c3 | 2015-12-08 13:22:33 -0800 | [diff] [blame] | 99 | for (size_t i = 0; i < suppressors_.size(); i++) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 100 | #if defined(WEBRTC_NS_FLOAT) |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 101 | WebRtcNs_Process(suppressors_[i]->state(), audio->split_bands_const_f(i), |
| 102 | audio->num_bands(), audio->split_bands_f(i)); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 103 | #elif defined(WEBRTC_NS_FIXED) |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 104 | WebRtcNsx_Process(suppressors_[i]->state(), audio->split_bands_const(i), |
| 105 | audio->num_bands(), audio->split_bands(i)); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 106 | #endif |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 107 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | int NoiseSuppressionImpl::Enable(bool enable) { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 111 | rtc::CritScope cs(crit_); |
solenberg | 29e2f93 | 2015-12-16 01:18:15 -0800 | [diff] [blame] | 112 | if (enabled_ != enable) { |
| 113 | enabled_ = enable; |
| 114 | Initialize(channels_, sample_rate_hz_); |
| 115 | } |
solenberg | 5e465c3 | 2015-12-08 13:22:33 -0800 | [diff] [blame] | 116 | return AudioProcessing::kNoError; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | bool NoiseSuppressionImpl::is_enabled() const { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 120 | rtc::CritScope cs(crit_); |
solenberg | 5e465c3 | 2015-12-08 13:22:33 -0800 | [diff] [blame] | 121 | return enabled_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | int NoiseSuppressionImpl::set_level(Level level) { |
solenberg | 5e465c3 | 2015-12-08 13:22:33 -0800 | [diff] [blame] | 125 | int policy = 1; |
| 126 | switch (level) { |
| 127 | case NoiseSuppression::kLow: |
| 128 | policy = 0; |
| 129 | break; |
| 130 | case NoiseSuppression::kModerate: |
| 131 | policy = 1; |
| 132 | break; |
| 133 | case NoiseSuppression::kHigh: |
| 134 | policy = 2; |
| 135 | break; |
| 136 | case NoiseSuppression::kVeryHigh: |
| 137 | policy = 3; |
| 138 | break; |
| 139 | default: |
| 140 | RTC_NOTREACHED(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 141 | } |
solenberg | 29e2f93 | 2015-12-16 01:18:15 -0800 | [diff] [blame] | 142 | rtc::CritScope cs(crit_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 143 | level_ = level; |
solenberg | 5e465c3 | 2015-12-08 13:22:33 -0800 | [diff] [blame] | 144 | for (auto& suppressor : suppressors_) { |
| 145 | int error = NS_SET_POLICY(suppressor->state(), policy); |
| 146 | RTC_DCHECK_EQ(0, error); |
| 147 | } |
| 148 | return AudioProcessing::kNoError; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | NoiseSuppression::Level NoiseSuppressionImpl::level() const { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 152 | rtc::CritScope cs(crit_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 153 | return level_; |
| 154 | } |
| 155 | |
bjornv@webrtc.org | 08329f4 | 2012-07-12 21:00:43 +0000 | [diff] [blame] | 156 | float NoiseSuppressionImpl::speech_probability() const { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 157 | rtc::CritScope cs(crit_); |
bjornv@webrtc.org | 08329f4 | 2012-07-12 21:00:43 +0000 | [diff] [blame] | 158 | #if defined(WEBRTC_NS_FLOAT) |
| 159 | float probability_average = 0.0f; |
solenberg | 5e465c3 | 2015-12-08 13:22:33 -0800 | [diff] [blame] | 160 | for (auto& suppressor : suppressors_) { |
| 161 | probability_average += |
| 162 | WebRtcNs_prior_speech_probability(suppressor->state()); |
bjornv@webrtc.org | 08329f4 | 2012-07-12 21:00:43 +0000 | [diff] [blame] | 163 | } |
pkasting | 25702cb | 2016-01-08 13:50:27 -0800 | [diff] [blame] | 164 | if (!suppressors_.empty()) { |
solenberg | 5e465c3 | 2015-12-08 13:22:33 -0800 | [diff] [blame] | 165 | probability_average /= suppressors_.size(); |
| 166 | } |
| 167 | return probability_average; |
bjornv@webrtc.org | 08329f4 | 2012-07-12 21:00:43 +0000 | [diff] [blame] | 168 | #elif defined(WEBRTC_NS_FIXED) |
solenberg | 5e465c3 | 2015-12-08 13:22:33 -0800 | [diff] [blame] | 169 | // TODO(peah): Returning error code as a float! Remove this. |
bjornv@webrtc.org | 08329f4 | 2012-07-12 21:00:43 +0000 | [diff] [blame] | 170 | // Currently not available for the fixed point implementation. |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 171 | return AudioProcessing::kUnsupportedFunctionError; |
bjornv@webrtc.org | 08329f4 | 2012-07-12 21:00:43 +0000 | [diff] [blame] | 172 | #endif |
| 173 | } |
Alejandro Luebs | fa639f0 | 2016-02-09 11:24:32 -0800 | [diff] [blame] | 174 | |
| 175 | std::vector<float> NoiseSuppressionImpl::NoiseEstimate() { |
| 176 | rtc::CritScope cs(crit_); |
| 177 | std::vector<float> noise_estimate; |
| 178 | #if defined(WEBRTC_NS_FLOAT) |
Alejandro Luebs | 3b14996 | 2016-04-01 13:54:36 -0700 | [diff] [blame] | 179 | const float kNumChannelsFraction = 1.f / suppressors_.size(); |
Alejandro Luebs | fa639f0 | 2016-02-09 11:24:32 -0800 | [diff] [blame] | 180 | noise_estimate.assign(WebRtcNs_num_freq(), 0.f); |
| 181 | for (auto& suppressor : suppressors_) { |
| 182 | const float* noise = WebRtcNs_noise_estimate(suppressor->state()); |
| 183 | for (size_t i = 0; i < noise_estimate.size(); ++i) { |
Alejandro Luebs | 3b14996 | 2016-04-01 13:54:36 -0700 | [diff] [blame] | 184 | noise_estimate[i] += kNumChannelsFraction * noise[i]; |
Alejandro Luebs | fa639f0 | 2016-02-09 11:24:32 -0800 | [diff] [blame] | 185 | } |
| 186 | } |
| 187 | #elif defined(WEBRTC_NS_FIXED) |
Alejandro Luebs | fa639f0 | 2016-02-09 11:24:32 -0800 | [diff] [blame] | 188 | noise_estimate.assign(WebRtcNsx_num_freq(), 0.f); |
| 189 | for (auto& suppressor : suppressors_) { |
Alejandro Luebs | 3b14996 | 2016-04-01 13:54:36 -0700 | [diff] [blame] | 190 | int q_noise; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 191 | const uint32_t* noise = |
| 192 | WebRtcNsx_noise_estimate(suppressor->state(), &q_noise); |
Alejandro Luebs | 3b14996 | 2016-04-01 13:54:36 -0700 | [diff] [blame] | 193 | const float kNormalizationFactor = |
| 194 | 1.f / ((1 << q_noise) * suppressors_.size()); |
Alejandro Luebs | fa639f0 | 2016-02-09 11:24:32 -0800 | [diff] [blame] | 195 | for (size_t i = 0; i < noise_estimate.size(); ++i) { |
Alejandro Luebs | 3b14996 | 2016-04-01 13:54:36 -0700 | [diff] [blame] | 196 | noise_estimate[i] += kNormalizationFactor * noise[i]; |
Alejandro Luebs | fa639f0 | 2016-02-09 11:24:32 -0800 | [diff] [blame] | 197 | } |
| 198 | } |
| 199 | #endif |
| 200 | return noise_estimate; |
| 201 | } |
| 202 | |
Alex Luebs | 57ae829 | 2016-03-09 16:24:34 +0100 | [diff] [blame] | 203 | size_t NoiseSuppressionImpl::num_noise_bins() { |
| 204 | #if defined(WEBRTC_NS_FLOAT) |
| 205 | return WebRtcNs_num_freq(); |
| 206 | #elif defined(WEBRTC_NS_FIXED) |
| 207 | return WebRtcNsx_num_freq(); |
| 208 | #endif |
| 209 | } |
| 210 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 211 | } // namespace webrtc |