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