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 | |
solenberg | 5e465c3 | 2015-12-08 13:22:33 -0800 | [diff] [blame^] | 55 | void NoiseSuppressionImpl::Initialize(int channels, int sample_rate_hz) { |
| 56 | RTC_DCHECK_LE(0, channels); |
| 57 | std::vector<rtc::scoped_ptr<Suppressor>> new_suppressors(channels); |
| 58 | for (int i = 0; i < channels; i++) { |
| 59 | new_suppressors[i].reset(new Suppressor(sample_rate_hz)); |
| 60 | } |
| 61 | rtc::CritScope cs(crit_); |
| 62 | suppressors_.swap(new_suppressors); |
| 63 | set_level(level_); |
| 64 | } |
| 65 | |
aluebs@webrtc.org | fda2c2e | 2014-09-18 09:54:06 +0000 | [diff] [blame] | 66 | int NoiseSuppressionImpl::AnalyzeCaptureAudio(AudioBuffer* audio) { |
solenberg | 5e465c3 | 2015-12-08 13:22:33 -0800 | [diff] [blame^] | 67 | RTC_DCHECK(audio); |
aluebs@webrtc.org | fda2c2e | 2014-09-18 09:54:06 +0000 | [diff] [blame] | 68 | #if defined(WEBRTC_NS_FLOAT) |
solenberg | 5e465c3 | 2015-12-08 13:22:33 -0800 | [diff] [blame^] | 69 | rtc::CritScope cs(crit_); |
| 70 | if (!enabled_) { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 71 | return AudioProcessing::kNoError; |
aluebs@webrtc.org | fda2c2e | 2014-09-18 09:54:06 +0000 | [diff] [blame] | 72 | } |
aluebs@webrtc.org | fda2c2e | 2014-09-18 09:54:06 +0000 | [diff] [blame] | 73 | |
solenberg | 5e465c3 | 2015-12-08 13:22:33 -0800 | [diff] [blame^] | 74 | RTC_DCHECK_GE(160u, audio->num_frames_per_band()); |
| 75 | RTC_DCHECK_EQ(suppressors_.size(), |
| 76 | static_cast<size_t>(audio->num_channels())); |
| 77 | for (size_t i = 0; i < suppressors_.size(); i++) { |
| 78 | WebRtcNs_Analyze(suppressors_[i]->state(), |
| 79 | audio->split_bands_const_f(i)[kBand0To8kHz]); |
aluebs@webrtc.org | fda2c2e | 2014-09-18 09:54:06 +0000 | [diff] [blame] | 80 | } |
| 81 | #endif |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 82 | return AudioProcessing::kNoError; |
aluebs@webrtc.org | fda2c2e | 2014-09-18 09:54:06 +0000 | [diff] [blame] | 83 | } |
| 84 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 85 | int NoiseSuppressionImpl::ProcessCaptureAudio(AudioBuffer* audio) { |
solenberg | 5e465c3 | 2015-12-08 13:22:33 -0800 | [diff] [blame^] | 86 | RTC_DCHECK(audio); |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 87 | rtc::CritScope cs(crit_); |
solenberg | 5e465c3 | 2015-12-08 13:22:33 -0800 | [diff] [blame^] | 88 | if (!enabled_) { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 89 | return AudioProcessing::kNoError; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 90 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 91 | |
solenberg | 5e465c3 | 2015-12-08 13:22:33 -0800 | [diff] [blame^] | 92 | RTC_DCHECK_GE(160u, audio->num_frames_per_band()); |
| 93 | RTC_DCHECK_EQ(suppressors_.size(), |
| 94 | static_cast<size_t>(audio->num_channels())); |
| 95 | for (size_t i = 0; i < suppressors_.size(); i++) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 96 | #if defined(WEBRTC_NS_FLOAT) |
solenberg | 5e465c3 | 2015-12-08 13:22:33 -0800 | [diff] [blame^] | 97 | WebRtcNs_Process(suppressors_[i]->state(), |
aluebs@webrtc.org | c5ebbd9 | 2014-12-10 19:30:57 +0000 | [diff] [blame] | 98 | audio->split_bands_const_f(i), |
| 99 | audio->num_bands(), |
| 100 | audio->split_bands_f(i)); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 101 | #elif defined(WEBRTC_NS_FIXED) |
solenberg | 5e465c3 | 2015-12-08 13:22:33 -0800 | [diff] [blame^] | 102 | WebRtcNsx_Process(suppressors_[i]->state(), |
aluebs@webrtc.org | c5ebbd9 | 2014-12-10 19:30:57 +0000 | [diff] [blame] | 103 | audio->split_bands_const(i), |
| 104 | audio->num_bands(), |
| 105 | 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 | } |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 108 | return AudioProcessing::kNoError; |
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 | 5e465c3 | 2015-12-08 13:22:33 -0800 | [diff] [blame^] | 113 | enabled_ = enable; |
| 114 | return AudioProcessing::kNoError; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | bool NoiseSuppressionImpl::is_enabled() const { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 118 | rtc::CritScope cs(crit_); |
solenberg | 5e465c3 | 2015-12-08 13:22:33 -0800 | [diff] [blame^] | 119 | return enabled_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | int NoiseSuppressionImpl::set_level(Level level) { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 123 | rtc::CritScope cs(crit_); |
solenberg | 5e465c3 | 2015-12-08 13:22:33 -0800 | [diff] [blame^] | 124 | int policy = 1; |
| 125 | switch (level) { |
| 126 | case NoiseSuppression::kLow: |
| 127 | policy = 0; |
| 128 | break; |
| 129 | case NoiseSuppression::kModerate: |
| 130 | policy = 1; |
| 131 | break; |
| 132 | case NoiseSuppression::kHigh: |
| 133 | policy = 2; |
| 134 | break; |
| 135 | case NoiseSuppression::kVeryHigh: |
| 136 | policy = 3; |
| 137 | break; |
| 138 | default: |
| 139 | RTC_NOTREACHED(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 140 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 141 | level_ = level; |
solenberg | 5e465c3 | 2015-12-08 13:22:33 -0800 | [diff] [blame^] | 142 | for (auto& suppressor : suppressors_) { |
| 143 | int error = NS_SET_POLICY(suppressor->state(), policy); |
| 144 | RTC_DCHECK_EQ(0, error); |
| 145 | } |
| 146 | return AudioProcessing::kNoError; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | NoiseSuppression::Level NoiseSuppressionImpl::level() const { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 150 | rtc::CritScope cs(crit_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 151 | return level_; |
| 152 | } |
| 153 | |
bjornv@webrtc.org | 08329f4 | 2012-07-12 21:00:43 +0000 | [diff] [blame] | 154 | float NoiseSuppressionImpl::speech_probability() const { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 155 | rtc::CritScope cs(crit_); |
bjornv@webrtc.org | 08329f4 | 2012-07-12 21:00:43 +0000 | [diff] [blame] | 156 | #if defined(WEBRTC_NS_FLOAT) |
| 157 | float probability_average = 0.0f; |
solenberg | 5e465c3 | 2015-12-08 13:22:33 -0800 | [diff] [blame^] | 158 | for (auto& suppressor : suppressors_) { |
| 159 | probability_average += |
| 160 | WebRtcNs_prior_speech_probability(suppressor->state()); |
bjornv@webrtc.org | 08329f4 | 2012-07-12 21:00:43 +0000 | [diff] [blame] | 161 | } |
solenberg | 5e465c3 | 2015-12-08 13:22:33 -0800 | [diff] [blame^] | 162 | if (suppressors_.size() > 0) { |
| 163 | probability_average /= suppressors_.size(); |
| 164 | } |
| 165 | return probability_average; |
bjornv@webrtc.org | 08329f4 | 2012-07-12 21:00:43 +0000 | [diff] [blame] | 166 | #elif defined(WEBRTC_NS_FIXED) |
solenberg | 5e465c3 | 2015-12-08 13:22:33 -0800 | [diff] [blame^] | 167 | // TODO(peah): Returning error code as a float! Remove this. |
bjornv@webrtc.org | 08329f4 | 2012-07-12 21:00:43 +0000 | [diff] [blame] | 168 | // Currently not available for the fixed point implementation. |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 169 | return AudioProcessing::kUnsupportedFunctionError; |
bjornv@webrtc.org | 08329f4 | 2012-07-12 21:00:43 +0000 | [diff] [blame] | 170 | #endif |
| 171 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 172 | } // namespace webrtc |