OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #include "webrtc/modules/audio_processing/noise_suppression_impl.h" | 11 #include "webrtc/modules/audio_processing/noise_suppression_impl.h" |
12 | 12 |
13 #include <assert.h> | 13 #include <assert.h> |
14 | 14 |
15 #include "webrtc/modules/audio_processing/audio_buffer.h" | 15 #include "webrtc/modules/audio_processing/audio_buffer.h" |
16 #if defined(WEBRTC_NS_FLOAT) | 16 #if defined(WEBRTC_NS_FLOAT) |
17 #include "webrtc/modules/audio_processing/ns/noise_suppression.h" | 17 #include "webrtc/modules/audio_processing/ns/noise_suppression.h" |
18 #elif defined(WEBRTC_NS_FIXED) | 18 #elif defined(WEBRTC_NS_FIXED) |
19 #include "webrtc/modules/audio_processing/ns/noise_suppression_x.h" | 19 #include "webrtc/modules/audio_processing/ns/noise_suppression_x.h" |
20 #endif | 20 #endif |
21 #include "webrtc/system_wrappers/include/critical_section_wrapper.h" | |
22 | 21 |
23 | 22 |
24 namespace webrtc { | 23 namespace webrtc { |
25 | 24 |
26 #if defined(WEBRTC_NS_FLOAT) | 25 #if defined(WEBRTC_NS_FLOAT) |
27 typedef NsHandle Handle; | 26 typedef NsHandle Handle; |
28 #elif defined(WEBRTC_NS_FIXED) | 27 #elif defined(WEBRTC_NS_FIXED) |
29 typedef NsxHandle Handle; | 28 typedef NsxHandle Handle; |
30 #endif | 29 #endif |
31 | 30 |
32 namespace { | 31 namespace { |
33 int MapSetting(NoiseSuppression::Level level) { | 32 int MapSetting(NoiseSuppression::Level level) { |
34 switch (level) { | 33 switch (level) { |
35 case NoiseSuppression::kLow: | 34 case NoiseSuppression::kLow: |
36 return 0; | 35 return 0; |
37 case NoiseSuppression::kModerate: | 36 case NoiseSuppression::kModerate: |
38 return 1; | 37 return 1; |
39 case NoiseSuppression::kHigh: | 38 case NoiseSuppression::kHigh: |
40 return 2; | 39 return 2; |
41 case NoiseSuppression::kVeryHigh: | 40 case NoiseSuppression::kVeryHigh: |
42 return 3; | 41 return 3; |
43 } | 42 } |
44 assert(false); | 43 assert(false); |
45 return -1; | 44 return -1; |
46 } | 45 } |
47 } // namespace | 46 } // namespace |
48 | 47 |
49 NoiseSuppressionImpl::NoiseSuppressionImpl(const AudioProcessing* apm, | 48 NoiseSuppressionImpl::NoiseSuppressionImpl( |
50 CriticalSectionWrapper* crit) | 49 const AudioProcessing* apm, |
51 : ProcessingComponent(), | 50 rtc::CriticalSection* crit) |
52 apm_(apm), | 51 : ProcessingComponent(), |
53 crit_(crit), | 52 apm_(apm), |
54 level_(kModerate) {} | 53 crit_(crit), |
| 54 level_(kModerate) { |
| 55 RTC_DCHECK(apm); |
| 56 RTC_DCHECK(crit); |
| 57 } |
55 | 58 |
56 NoiseSuppressionImpl::~NoiseSuppressionImpl() {} | 59 NoiseSuppressionImpl::~NoiseSuppressionImpl() {} |
57 | 60 |
58 int NoiseSuppressionImpl::AnalyzeCaptureAudio(AudioBuffer* audio) { | 61 int NoiseSuppressionImpl::AnalyzeCaptureAudio(AudioBuffer* audio) { |
59 #if defined(WEBRTC_NS_FLOAT) | 62 #if defined(WEBRTC_NS_FLOAT) |
60 if (!is_component_enabled()) { | 63 if (!is_component_enabled()) { |
61 return apm_->kNoError; | 64 return AudioProcessing::kNoError; |
62 } | 65 } |
63 assert(audio->num_frames_per_band() <= 160); | 66 assert(audio->num_frames_per_band() <= 160); |
64 assert(audio->num_channels() == num_handles()); | 67 assert(audio->num_channels() == num_handles()); |
65 | 68 |
66 for (int i = 0; i < num_handles(); ++i) { | 69 for (int i = 0; i < num_handles(); ++i) { |
67 Handle* my_handle = static_cast<Handle*>(handle(i)); | 70 Handle* my_handle = static_cast<Handle*>(handle(i)); |
68 | 71 |
69 WebRtcNs_Analyze(my_handle, audio->split_bands_const_f(i)[kBand0To8kHz]); | 72 WebRtcNs_Analyze(my_handle, audio->split_bands_const_f(i)[kBand0To8kHz]); |
70 } | 73 } |
71 #endif | 74 #endif |
72 return apm_->kNoError; | 75 return AudioProcessing::kNoError; |
73 } | 76 } |
74 | 77 |
75 int NoiseSuppressionImpl::ProcessCaptureAudio(AudioBuffer* audio) { | 78 int NoiseSuppressionImpl::ProcessCaptureAudio(AudioBuffer* audio) { |
| 79 rtc::CritScope cs(crit_); |
76 if (!is_component_enabled()) { | 80 if (!is_component_enabled()) { |
77 return apm_->kNoError; | 81 return AudioProcessing::kNoError; |
78 } | 82 } |
79 assert(audio->num_frames_per_band() <= 160); | 83 assert(audio->num_frames_per_band() <= 160); |
80 assert(audio->num_channels() == num_handles()); | 84 assert(audio->num_channels() == num_handles()); |
81 | 85 |
82 for (int i = 0; i < num_handles(); ++i) { | 86 for (int i = 0; i < num_handles(); ++i) { |
83 Handle* my_handle = static_cast<Handle*>(handle(i)); | 87 Handle* my_handle = static_cast<Handle*>(handle(i)); |
84 #if defined(WEBRTC_NS_FLOAT) | 88 #if defined(WEBRTC_NS_FLOAT) |
85 WebRtcNs_Process(my_handle, | 89 WebRtcNs_Process(my_handle, |
86 audio->split_bands_const_f(i), | 90 audio->split_bands_const_f(i), |
87 audio->num_bands(), | 91 audio->num_bands(), |
88 audio->split_bands_f(i)); | 92 audio->split_bands_f(i)); |
89 #elif defined(WEBRTC_NS_FIXED) | 93 #elif defined(WEBRTC_NS_FIXED) |
90 WebRtcNsx_Process(my_handle, | 94 WebRtcNsx_Process(my_handle, |
91 audio->split_bands_const(i), | 95 audio->split_bands_const(i), |
92 audio->num_bands(), | 96 audio->num_bands(), |
93 audio->split_bands(i)); | 97 audio->split_bands(i)); |
94 #endif | 98 #endif |
95 } | 99 } |
96 return apm_->kNoError; | 100 return AudioProcessing::kNoError; |
97 } | 101 } |
98 | 102 |
99 int NoiseSuppressionImpl::Enable(bool enable) { | 103 int NoiseSuppressionImpl::Enable(bool enable) { |
100 CriticalSectionScoped crit_scoped(crit_); | 104 rtc::CritScope cs(crit_); |
101 return EnableComponent(enable); | 105 return EnableComponent(enable); |
102 } | 106 } |
103 | 107 |
104 bool NoiseSuppressionImpl::is_enabled() const { | 108 bool NoiseSuppressionImpl::is_enabled() const { |
| 109 rtc::CritScope cs(crit_); |
105 return is_component_enabled(); | 110 return is_component_enabled(); |
106 } | 111 } |
107 | 112 |
108 int NoiseSuppressionImpl::set_level(Level level) { | 113 int NoiseSuppressionImpl::set_level(Level level) { |
109 CriticalSectionScoped crit_scoped(crit_); | 114 rtc::CritScope cs(crit_); |
110 if (MapSetting(level) == -1) { | 115 if (MapSetting(level) == -1) { |
111 return apm_->kBadParameterError; | 116 return AudioProcessing::kBadParameterError; |
112 } | 117 } |
113 | 118 |
114 level_ = level; | 119 level_ = level; |
115 return Configure(); | 120 return Configure(); |
116 } | 121 } |
117 | 122 |
118 NoiseSuppression::Level NoiseSuppressionImpl::level() const { | 123 NoiseSuppression::Level NoiseSuppressionImpl::level() const { |
| 124 rtc::CritScope cs(crit_); |
119 return level_; | 125 return level_; |
120 } | 126 } |
121 | 127 |
122 float NoiseSuppressionImpl::speech_probability() const { | 128 float NoiseSuppressionImpl::speech_probability() const { |
| 129 rtc::CritScope cs(crit_); |
123 #if defined(WEBRTC_NS_FLOAT) | 130 #if defined(WEBRTC_NS_FLOAT) |
124 float probability_average = 0.0f; | 131 float probability_average = 0.0f; |
125 for (int i = 0; i < num_handles(); i++) { | 132 for (int i = 0; i < num_handles(); i++) { |
126 Handle* my_handle = static_cast<Handle*>(handle(i)); | 133 Handle* my_handle = static_cast<Handle*>(handle(i)); |
127 probability_average += WebRtcNs_prior_speech_probability(my_handle); | 134 probability_average += WebRtcNs_prior_speech_probability(my_handle); |
128 } | 135 } |
129 return probability_average / num_handles(); | 136 return probability_average / num_handles(); |
130 #elif defined(WEBRTC_NS_FIXED) | 137 #elif defined(WEBRTC_NS_FIXED) |
131 // Currently not available for the fixed point implementation. | 138 // Currently not available for the fixed point implementation. |
132 return apm_->kUnsupportedFunctionError; | 139 return AudioProcessing::kUnsupportedFunctionError; |
133 #endif | 140 #endif |
134 } | 141 } |
135 | 142 |
136 void* NoiseSuppressionImpl::CreateHandle() const { | 143 void* NoiseSuppressionImpl::CreateHandle() const { |
137 #if defined(WEBRTC_NS_FLOAT) | 144 #if defined(WEBRTC_NS_FLOAT) |
138 return WebRtcNs_Create(); | 145 return WebRtcNs_Create(); |
139 #elif defined(WEBRTC_NS_FIXED) | 146 #elif defined(WEBRTC_NS_FIXED) |
140 return WebRtcNsx_Create(); | 147 return WebRtcNsx_Create(); |
141 #endif | 148 #endif |
142 } | 149 } |
(...skipping 10 matching lines...) Expand all Loading... |
153 #if defined(WEBRTC_NS_FLOAT) | 160 #if defined(WEBRTC_NS_FLOAT) |
154 return WebRtcNs_Init(static_cast<Handle*>(handle), | 161 return WebRtcNs_Init(static_cast<Handle*>(handle), |
155 apm_->proc_sample_rate_hz()); | 162 apm_->proc_sample_rate_hz()); |
156 #elif defined(WEBRTC_NS_FIXED) | 163 #elif defined(WEBRTC_NS_FIXED) |
157 return WebRtcNsx_Init(static_cast<Handle*>(handle), | 164 return WebRtcNsx_Init(static_cast<Handle*>(handle), |
158 apm_->proc_sample_rate_hz()); | 165 apm_->proc_sample_rate_hz()); |
159 #endif | 166 #endif |
160 } | 167 } |
161 | 168 |
162 int NoiseSuppressionImpl::ConfigureHandle(void* handle) const { | 169 int NoiseSuppressionImpl::ConfigureHandle(void* handle) const { |
| 170 rtc::CritScope cs(crit_); |
163 #if defined(WEBRTC_NS_FLOAT) | 171 #if defined(WEBRTC_NS_FLOAT) |
164 return WebRtcNs_set_policy(static_cast<Handle*>(handle), | 172 return WebRtcNs_set_policy(static_cast<Handle*>(handle), |
165 MapSetting(level_)); | 173 MapSetting(level_)); |
166 #elif defined(WEBRTC_NS_FIXED) | 174 #elif defined(WEBRTC_NS_FIXED) |
167 return WebRtcNsx_set_policy(static_cast<Handle*>(handle), | 175 return WebRtcNsx_set_policy(static_cast<Handle*>(handle), |
168 MapSetting(level_)); | 176 MapSetting(level_)); |
169 #endif | 177 #endif |
170 } | 178 } |
171 | 179 |
172 int NoiseSuppressionImpl::num_handles_required() const { | 180 int NoiseSuppressionImpl::num_handles_required() const { |
173 return apm_->num_output_channels(); | 181 return apm_->num_output_channels(); |
174 } | 182 } |
175 | 183 |
176 int NoiseSuppressionImpl::GetHandleError(void* handle) const { | 184 int NoiseSuppressionImpl::GetHandleError(void* handle) const { |
177 // The NS has no get_error() function. | 185 // The NS has no get_error() function. |
178 assert(handle != NULL); | 186 assert(handle != NULL); |
179 return apm_->kUnspecifiedError; | 187 return AudioProcessing::kUnspecifiedError; |
180 } | 188 } |
181 } // namespace webrtc | 189 } // namespace webrtc |
OLD | NEW |