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