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 | |
pbos@webrtc.org | 12dc1a3 | 2013-08-05 16:22:53 +0000 | [diff] [blame] | 13 | #include <assert.h> |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 14 | |
andrew@webrtc.org | 56e4a05 | 2014-02-27 22:23:17 +0000 | [diff] [blame] | 15 | #include "webrtc/modules/audio_processing/audio_buffer.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 16 | #if defined(WEBRTC_NS_FLOAT) |
Henrik Kjellander | 9b72af9 | 2015-11-11 20:16:11 +0100 | [diff] [blame] | 17 | #include "webrtc/modules/audio_processing/ns/noise_suppression.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 18 | #elif defined(WEBRTC_NS_FIXED) |
Henrik Kjellander | 9b72af9 | 2015-11-11 20:16:11 +0100 | [diff] [blame] | 19 | #include "webrtc/modules/audio_processing/ns/noise_suppression_x.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 20 | #endif |
| 21 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 22 | |
| 23 | namespace webrtc { |
| 24 | |
| 25 | #if defined(WEBRTC_NS_FLOAT) |
| 26 | typedef NsHandle Handle; |
| 27 | #elif defined(WEBRTC_NS_FIXED) |
| 28 | typedef NsxHandle Handle; |
| 29 | #endif |
| 30 | |
| 31 | namespace { |
| 32 | int MapSetting(NoiseSuppression::Level level) { |
| 33 | switch (level) { |
| 34 | case NoiseSuppression::kLow: |
| 35 | return 0; |
| 36 | case NoiseSuppression::kModerate: |
| 37 | return 1; |
| 38 | case NoiseSuppression::kHigh: |
| 39 | return 2; |
| 40 | case NoiseSuppression::kVeryHigh: |
| 41 | return 3; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 42 | } |
andrew@webrtc.org | 648af74 | 2012-02-08 01:57:29 +0000 | [diff] [blame] | 43 | assert(false); |
mflodman@webrtc.org | ec31bc1 | 2012-02-06 12:42:45 +0000 | [diff] [blame] | 44 | return -1; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 45 | } |
| 46 | } // namespace |
| 47 | |
andrew@webrtc.org | 56e4a05 | 2014-02-27 22:23:17 +0000 | [diff] [blame] | 48 | NoiseSuppressionImpl::NoiseSuppressionImpl(const AudioProcessing* apm, |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame^] | 49 | rtc::CriticalSection* crit) |
| 50 | : ProcessingComponent(), apm_(apm), crit_(crit), level_(kModerate) { |
| 51 | RTC_DCHECK(apm); |
| 52 | RTC_DCHECK(crit); |
| 53 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 54 | |
| 55 | NoiseSuppressionImpl::~NoiseSuppressionImpl() {} |
| 56 | |
aluebs@webrtc.org | fda2c2e | 2014-09-18 09:54:06 +0000 | [diff] [blame] | 57 | int NoiseSuppressionImpl::AnalyzeCaptureAudio(AudioBuffer* audio) { |
| 58 | #if defined(WEBRTC_NS_FLOAT) |
| 59 | if (!is_component_enabled()) { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame^] | 60 | return AudioProcessing::kNoError; |
aluebs@webrtc.org | fda2c2e | 2014-09-18 09:54:06 +0000 | [diff] [blame] | 61 | } |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 62 | assert(audio->num_frames_per_band() <= 160); |
aluebs@webrtc.org | fda2c2e | 2014-09-18 09:54:06 +0000 | [diff] [blame] | 63 | assert(audio->num_channels() == num_handles()); |
| 64 | |
| 65 | for (int i = 0; i < num_handles(); ++i) { |
| 66 | Handle* my_handle = static_cast<Handle*>(handle(i)); |
| 67 | |
aluebs@webrtc.org | c5ebbd9 | 2014-12-10 19:30:57 +0000 | [diff] [blame] | 68 | WebRtcNs_Analyze(my_handle, audio->split_bands_const_f(i)[kBand0To8kHz]); |
aluebs@webrtc.org | fda2c2e | 2014-09-18 09:54:06 +0000 | [diff] [blame] | 69 | } |
| 70 | #endif |
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 | } |
| 73 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 74 | int NoiseSuppressionImpl::ProcessCaptureAudio(AudioBuffer* audio) { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame^] | 75 | rtc::CritScope cs(crit_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 76 | if (!is_component_enabled()) { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame^] | 77 | return AudioProcessing::kNoError; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 78 | } |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 79 | assert(audio->num_frames_per_band() <= 160); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 80 | assert(audio->num_channels() == num_handles()); |
| 81 | |
aluebs@webrtc.org | fda2c2e | 2014-09-18 09:54:06 +0000 | [diff] [blame] | 82 | for (int i = 0; i < num_handles(); ++i) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 83 | Handle* my_handle = static_cast<Handle*>(handle(i)); |
| 84 | #if defined(WEBRTC_NS_FLOAT) |
aluebs@webrtc.org | c5ebbd9 | 2014-12-10 19:30:57 +0000 | [diff] [blame] | 85 | WebRtcNs_Process(my_handle, |
| 86 | audio->split_bands_const_f(i), |
| 87 | audio->num_bands(), |
| 88 | audio->split_bands_f(i)); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 89 | #elif defined(WEBRTC_NS_FIXED) |
aluebs@webrtc.org | c5ebbd9 | 2014-12-10 19:30:57 +0000 | [diff] [blame] | 90 | WebRtcNsx_Process(my_handle, |
| 91 | audio->split_bands_const(i), |
| 92 | audio->num_bands(), |
| 93 | audio->split_bands(i)); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 94 | #endif |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 95 | } |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame^] | 96 | return AudioProcessing::kNoError; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | int NoiseSuppressionImpl::Enable(bool enable) { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame^] | 100 | rtc::CritScope cs(crit_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 101 | return EnableComponent(enable); |
| 102 | } |
| 103 | |
| 104 | bool NoiseSuppressionImpl::is_enabled() const { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame^] | 105 | rtc::CritScope cs(crit_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 106 | return is_component_enabled(); |
| 107 | } |
| 108 | |
| 109 | int NoiseSuppressionImpl::set_level(Level level) { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame^] | 110 | rtc::CritScope cs(crit_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 111 | if (MapSetting(level) == -1) { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame^] | 112 | return AudioProcessing::kBadParameterError; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | level_ = level; |
| 116 | return Configure(); |
| 117 | } |
| 118 | |
| 119 | NoiseSuppression::Level NoiseSuppressionImpl::level() const { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame^] | 120 | rtc::CritScope cs(crit_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 121 | return level_; |
| 122 | } |
| 123 | |
bjornv@webrtc.org | 08329f4 | 2012-07-12 21:00:43 +0000 | [diff] [blame] | 124 | float NoiseSuppressionImpl::speech_probability() const { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame^] | 125 | rtc::CritScope cs(crit_); |
bjornv@webrtc.org | 08329f4 | 2012-07-12 21:00:43 +0000 | [diff] [blame] | 126 | #if defined(WEBRTC_NS_FLOAT) |
| 127 | float probability_average = 0.0f; |
| 128 | for (int i = 0; i < num_handles(); i++) { |
| 129 | Handle* my_handle = static_cast<Handle*>(handle(i)); |
| 130 | probability_average += WebRtcNs_prior_speech_probability(my_handle); |
| 131 | } |
| 132 | return probability_average / num_handles(); |
| 133 | #elif defined(WEBRTC_NS_FIXED) |
| 134 | // Currently not available for the fixed point implementation. |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame^] | 135 | return AudioProcessing::kUnsupportedFunctionError; |
bjornv@webrtc.org | 08329f4 | 2012-07-12 21:00:43 +0000 | [diff] [blame] | 136 | #endif |
| 137 | } |
| 138 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 139 | void* NoiseSuppressionImpl::CreateHandle() const { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 140 | #if defined(WEBRTC_NS_FLOAT) |
Bjorn Volcker | 9345e86 | 2015-06-10 21:43:36 +0200 | [diff] [blame] | 141 | return WebRtcNs_Create(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 142 | #elif defined(WEBRTC_NS_FIXED) |
Bjorn Volcker | 9345e86 | 2015-06-10 21:43:36 +0200 | [diff] [blame] | 143 | return WebRtcNsx_Create(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 144 | #endif |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 145 | } |
| 146 | |
bjornv@webrtc.org | 5964fe0 | 2014-04-22 06:52:28 +0000 | [diff] [blame] | 147 | void NoiseSuppressionImpl::DestroyHandle(void* handle) const { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 148 | #if defined(WEBRTC_NS_FLOAT) |
bjornv@webrtc.org | 5964fe0 | 2014-04-22 06:52:28 +0000 | [diff] [blame] | 149 | WebRtcNs_Free(static_cast<Handle*>(handle)); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 150 | #elif defined(WEBRTC_NS_FIXED) |
bjornv@webrtc.org | 5964fe0 | 2014-04-22 06:52:28 +0000 | [diff] [blame] | 151 | WebRtcNsx_Free(static_cast<Handle*>(handle)); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 152 | #endif |
| 153 | } |
| 154 | |
| 155 | int NoiseSuppressionImpl::InitializeHandle(void* handle) const { |
| 156 | #if defined(WEBRTC_NS_FLOAT) |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 157 | return WebRtcNs_Init(static_cast<Handle*>(handle), |
aluebs@webrtc.org | 5088377 | 2014-09-26 14:33:08 +0000 | [diff] [blame] | 158 | apm_->proc_sample_rate_hz()); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 159 | #elif defined(WEBRTC_NS_FIXED) |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 160 | return WebRtcNsx_Init(static_cast<Handle*>(handle), |
| 161 | apm_->proc_sample_rate_hz()); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 162 | #endif |
| 163 | } |
| 164 | |
| 165 | int NoiseSuppressionImpl::ConfigureHandle(void* handle) const { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame^] | 166 | rtc::CritScope cs(crit_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 167 | #if defined(WEBRTC_NS_FLOAT) |
| 168 | return WebRtcNs_set_policy(static_cast<Handle*>(handle), |
| 169 | MapSetting(level_)); |
| 170 | #elif defined(WEBRTC_NS_FIXED) |
| 171 | return WebRtcNsx_set_policy(static_cast<Handle*>(handle), |
| 172 | MapSetting(level_)); |
| 173 | #endif |
| 174 | } |
| 175 | |
| 176 | int NoiseSuppressionImpl::num_handles_required() const { |
| 177 | return apm_->num_output_channels(); |
| 178 | } |
| 179 | |
| 180 | int NoiseSuppressionImpl::GetHandleError(void* handle) const { |
| 181 | // The NS has no get_error() function. |
| 182 | assert(handle != NULL); |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame^] | 183 | return AudioProcessing::kUnspecifiedError; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 184 | } |
| 185 | } // namespace webrtc |