2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
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.
11 #include "webrtc/modules/audio_processing/noise_suppression_impl.h"
15 #include "webrtc/modules/audio_processing/audio_buffer.h"
16 #if defined(WEBRTC_NS_FLOAT)
17 #include "webrtc/modules/audio_processing/ns/include/noise_suppression.h"
18 #elif defined(WEBRTC_NS_FIXED)
19 #include "webrtc/modules/audio_processing/ns/include/noise_suppression_x.h"
21 #include "webrtc/system_wrappers/interface/critical_section_wrapper.h"
26 #if defined(WEBRTC_NS_FLOAT)
27 typedef NsHandle Handle;
28 #elif defined(WEBRTC_NS_FIXED)
29 typedef NsxHandle Handle;
33 int MapSetting(NoiseSuppression::Level level) {
35 case NoiseSuppression::kLow:
37 case NoiseSuppression::kModerate:
39 case NoiseSuppression::kHigh:
41 case NoiseSuppression::kVeryHigh:
49 NoiseSuppressionImpl::NoiseSuppressionImpl(const AudioProcessing* apm,
50 CriticalSectionWrapper* crit)
51 : ProcessingComponent(),
56 NoiseSuppressionImpl::~NoiseSuppressionImpl() {}
58 int NoiseSuppressionImpl::ProcessCaptureAudio(AudioBuffer* audio) {
59 int err = apm_->kNoError;
61 if (!is_component_enabled()) {
62 return apm_->kNoError;
64 assert(audio->samples_per_split_channel() <= 160);
65 assert(audio->num_channels() == num_handles());
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_f(i),
72 audio->high_pass_split_data_f(i),
73 audio->low_pass_split_data_f(i),
74 audio->high_pass_split_data_f(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));
83 if (err != apm_->kNoError) {
84 return GetHandleError(my_handle);
88 return apm_->kNoError;
91 int NoiseSuppressionImpl::Enable(bool enable) {
92 CriticalSectionScoped crit_scoped(crit_);
93 return EnableComponent(enable);
96 bool NoiseSuppressionImpl::is_enabled() const {
97 return is_component_enabled();
100 int NoiseSuppressionImpl::set_level(Level level) {
101 CriticalSectionScoped crit_scoped(crit_);
102 if (MapSetting(level) == -1) {
103 return apm_->kBadParameterError;
110 NoiseSuppression::Level NoiseSuppressionImpl::level() const {
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);
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;
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)
138 assert(handle != NULL);
144 void NoiseSuppressionImpl::DestroyHandle(void* handle) const {
145 #if defined(WEBRTC_NS_FLOAT)
146 WebRtcNs_Free(static_cast<Handle*>(handle));
147 #elif defined(WEBRTC_NS_FIXED)
148 WebRtcNsx_Free(static_cast<Handle*>(handle));
152 int NoiseSuppressionImpl::InitializeHandle(void* handle) const {
153 #if defined(WEBRTC_NS_FLOAT)
154 return WebRtcNs_Init(static_cast<Handle*>(handle),
155 apm_->proc_sample_rate_hz());
156 #elif defined(WEBRTC_NS_FIXED)
157 return WebRtcNsx_Init(static_cast<Handle*>(handle),
158 apm_->proc_sample_rate_hz());
162 int NoiseSuppressionImpl::ConfigureHandle(void* handle) const {
163 #if defined(WEBRTC_NS_FLOAT)
164 return WebRtcNs_set_policy(static_cast<Handle*>(handle),
166 #elif defined(WEBRTC_NS_FIXED)
167 return WebRtcNsx_set_policy(static_cast<Handle*>(handle),
172 int NoiseSuppressionImpl::num_handles_required() const {
173 return apm_->num_output_channels();
176 int NoiseSuppressionImpl::GetHandleError(void* handle) const {
177 // The NS has no get_error() function.
178 assert(handle != NULL);
179 return apm_->kUnspecifiedError;
181 } // namespace webrtc