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