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 | |
| 58 | int NoiseSuppressionImpl::ProcessCaptureAudio(AudioBuffer* audio) { |
| 59 | int err = apm_->kNoError; |
| 60 | |
| 61 | if (!is_component_enabled()) { |
| 62 | return apm_->kNoError; |
| 63 | } |
| 64 | assert(audio->samples_per_split_channel() <= 160); |
| 65 | assert(audio->num_channels() == num_handles()); |
| 66 | |
| 67 | for (int i = 0; i < num_handles(); i++) { |
| 68 | Handle* my_handle = static_cast<Handle*>(handle(i)); |
| 69 | #if defined(WEBRTC_NS_FLOAT) |
| 70 | err = WebRtcNs_Process(static_cast<Handle*>(handle(i)), |
| 71 | audio->low_pass_split_data(i), |
| 72 | audio->high_pass_split_data(i), |
| 73 | audio->low_pass_split_data(i), |
| 74 | audio->high_pass_split_data(i)); |
| 75 | #elif defined(WEBRTC_NS_FIXED) |
| 76 | err = WebRtcNsx_Process(static_cast<Handle*>(handle(i)), |
| 77 | audio->low_pass_split_data(i), |
| 78 | audio->high_pass_split_data(i), |
| 79 | audio->low_pass_split_data(i), |
| 80 | audio->high_pass_split_data(i)); |
| 81 | #endif |
| 82 | |
| 83 | if (err != apm_->kNoError) { |
| 84 | return GetHandleError(my_handle); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | return apm_->kNoError; |
| 89 | } |
| 90 | |
| 91 | int NoiseSuppressionImpl::Enable(bool enable) { |
andrew@webrtc.org | 56e4a05 | 2014-02-27 22:23:17 +0000 | [diff] [blame] | 92 | CriticalSectionScoped crit_scoped(crit_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 93 | return EnableComponent(enable); |
| 94 | } |
| 95 | |
| 96 | bool NoiseSuppressionImpl::is_enabled() const { |
| 97 | return is_component_enabled(); |
| 98 | } |
| 99 | |
| 100 | int NoiseSuppressionImpl::set_level(Level level) { |
andrew@webrtc.org | 56e4a05 | 2014-02-27 22:23:17 +0000 | [diff] [blame] | 101 | CriticalSectionScoped crit_scoped(crit_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 102 | if (MapSetting(level) == -1) { |
| 103 | return apm_->kBadParameterError; |
| 104 | } |
| 105 | |
| 106 | level_ = level; |
| 107 | return Configure(); |
| 108 | } |
| 109 | |
| 110 | NoiseSuppression::Level NoiseSuppressionImpl::level() const { |
| 111 | return level_; |
| 112 | } |
| 113 | |
bjornv@webrtc.org | 08329f4 | 2012-07-12 21:00:43 +0000 | [diff] [blame] | 114 | float NoiseSuppressionImpl::speech_probability() const { |
| 115 | #if defined(WEBRTC_NS_FLOAT) |
| 116 | float probability_average = 0.0f; |
| 117 | for (int i = 0; i < num_handles(); i++) { |
| 118 | Handle* my_handle = static_cast<Handle*>(handle(i)); |
| 119 | probability_average += WebRtcNs_prior_speech_probability(my_handle); |
| 120 | } |
| 121 | return probability_average / num_handles(); |
| 122 | #elif defined(WEBRTC_NS_FIXED) |
| 123 | // Currently not available for the fixed point implementation. |
| 124 | return apm_->kUnsupportedFunctionError; |
| 125 | #endif |
| 126 | } |
| 127 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 128 | void* NoiseSuppressionImpl::CreateHandle() const { |
| 129 | Handle* handle = NULL; |
| 130 | #if defined(WEBRTC_NS_FLOAT) |
| 131 | if (WebRtcNs_Create(&handle) != apm_->kNoError) |
| 132 | #elif defined(WEBRTC_NS_FIXED) |
| 133 | if (WebRtcNsx_Create(&handle) != apm_->kNoError) |
| 134 | #endif |
| 135 | { |
| 136 | handle = NULL; |
| 137 | } else { |
| 138 | assert(handle != NULL); |
| 139 | } |
| 140 | |
| 141 | return handle; |
| 142 | } |
| 143 | |
bjornv@webrtc.org | 5964fe0 | 2014-04-22 06:52:28 +0000 | [diff] [blame^] | 144 | void NoiseSuppressionImpl::DestroyHandle(void* handle) const { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 145 | #if defined(WEBRTC_NS_FLOAT) |
bjornv@webrtc.org | 5964fe0 | 2014-04-22 06:52:28 +0000 | [diff] [blame^] | 146 | WebRtcNs_Free(static_cast<Handle*>(handle)); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 147 | #elif defined(WEBRTC_NS_FIXED) |
bjornv@webrtc.org | 5964fe0 | 2014-04-22 06:52:28 +0000 | [diff] [blame^] | 148 | WebRtcNsx_Free(static_cast<Handle*>(handle)); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 149 | #endif |
| 150 | } |
| 151 | |
| 152 | int NoiseSuppressionImpl::InitializeHandle(void* handle) const { |
| 153 | #if defined(WEBRTC_NS_FLOAT) |
| 154 | return WebRtcNs_Init(static_cast<Handle*>(handle), apm_->sample_rate_hz()); |
| 155 | #elif defined(WEBRTC_NS_FIXED) |
| 156 | return WebRtcNsx_Init(static_cast<Handle*>(handle), apm_->sample_rate_hz()); |
| 157 | #endif |
| 158 | } |
| 159 | |
| 160 | int NoiseSuppressionImpl::ConfigureHandle(void* handle) const { |
| 161 | #if defined(WEBRTC_NS_FLOAT) |
| 162 | return WebRtcNs_set_policy(static_cast<Handle*>(handle), |
| 163 | MapSetting(level_)); |
| 164 | #elif defined(WEBRTC_NS_FIXED) |
| 165 | return WebRtcNsx_set_policy(static_cast<Handle*>(handle), |
| 166 | MapSetting(level_)); |
| 167 | #endif |
| 168 | } |
| 169 | |
| 170 | int NoiseSuppressionImpl::num_handles_required() const { |
| 171 | return apm_->num_output_channels(); |
| 172 | } |
| 173 | |
| 174 | int NoiseSuppressionImpl::GetHandleError(void* handle) const { |
| 175 | // The NS has no get_error() function. |
| 176 | assert(handle != NULL); |
| 177 | return apm_->kUnspecifiedError; |
| 178 | } |
| 179 | } // namespace webrtc |