eea0a04a2a624a4469687ddd9b5d9c97d90b00ee
[platform/framework/web/crosswalk.git] / src / third_party / webrtc / modules / audio_processing / noise_suppression_impl.cc
1 /*
2  *  Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
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
11 #include "webrtc/modules/audio_processing/noise_suppression_impl.h"
12
13 #include <assert.h>
14
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"
20 #endif
21 #include "webrtc/system_wrappers/interface/critical_section_wrapper.h"
22
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;
43   }
44   assert(false);
45   return -1;
46 }
47 }  // namespace
48
49 NoiseSuppressionImpl::NoiseSuppressionImpl(const AudioProcessing* apm,
50                                            CriticalSectionWrapper* crit)
51   : ProcessingComponent(),
52     apm_(apm),
53     crit_(crit),
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_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));
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) {
92   CriticalSectionScoped crit_scoped(crit_);
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) {
101   CriticalSectionScoped crit_scoped(crit_);
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
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
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
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));
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),
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());
159 #endif
160 }
161
162 int NoiseSuppressionImpl::ConfigureHandle(void* handle) const {
163 #if defined(WEBRTC_NS_FLOAT)
164   return WebRtcNs_set_policy(static_cast<Handle*>(handle),
165                              MapSetting(level_));
166 #elif defined(WEBRTC_NS_FIXED)
167   return WebRtcNsx_set_policy(static_cast<Handle*>(handle),
168                               MapSetting(level_));
169 #endif
170 }
171
172 int NoiseSuppressionImpl::num_handles_required() const {
173   return apm_->num_output_channels();
174 }
175
176 int NoiseSuppressionImpl::GetHandleError(void* handle) const {
177   // The NS has no get_error() function.
178   assert(handle != NULL);
179   return apm_->kUnspecifiedError;
180 }
181 }  // namespace webrtc