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