Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(169)

Side by Side Diff: webrtc/modules/audio_processing/noise_suppression_impl.cc

Issue 1507683006: Make NoiseSuppression not a processing component (bit exact). (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: missing explicit Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « webrtc/modules/audio_processing/noise_suppression_impl.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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>
14
15 #include "webrtc/modules/audio_processing/audio_buffer.h" 13 #include "webrtc/modules/audio_processing/audio_buffer.h"
16 #if defined(WEBRTC_NS_FLOAT) 14 #if defined(WEBRTC_NS_FLOAT)
17 #include "webrtc/modules/audio_processing/ns/noise_suppression.h" 15 #include "webrtc/modules/audio_processing/ns/noise_suppression.h"
16 #define NS_CREATE WebRtcNs_Create
17 #define NS_FREE WebRtcNs_Free
18 #define NS_INIT WebRtcNs_Init
19 #define NS_SET_POLICY WebRtcNs_set_policy
20 typedef NsHandle NsState;
18 #elif defined(WEBRTC_NS_FIXED) 21 #elif defined(WEBRTC_NS_FIXED)
19 #include "webrtc/modules/audio_processing/ns/noise_suppression_x.h" 22 #include "webrtc/modules/audio_processing/ns/noise_suppression_x.h"
23 #define NS_CREATE WebRtcNsx_Create
24 #define NS_FREE WebRtcNsx_Free
25 #define NS_INIT WebRtcNsx_Init
26 #define NS_SET_POLICY WebRtcNsx_set_policy
27 typedef NsxHandle NsState;
20 #endif 28 #endif
21 29
30 namespace webrtc {
31 class NoiseSuppressionImpl::Suppressor {
32 public:
33 explicit Suppressor(int sample_rate_hz) {
34 state_ = NS_CREATE();
35 RTC_CHECK(state_);
36 int error = NS_INIT(state_, sample_rate_hz);
37 RTC_DCHECK_EQ(0, error);
38 }
39 ~Suppressor() {
40 NS_FREE(state_);
41 }
42 NsState* state() { return state_; }
43 private:
44 NsState* state_ = nullptr;
45 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(Suppressor);
46 };
22 47
23 namespace webrtc { 48 NoiseSuppressionImpl::NoiseSuppressionImpl(rtc::CriticalSection* crit)
24 49 : crit_(crit) {
25 #if defined(WEBRTC_NS_FLOAT)
26 typedef NsHandle Handle;
27 #elif defined(WEBRTC_NS_FIXED)
28 typedef NsxHandle Handle;
29 #endif
30
31 namespace {
32 int MapSetting(NoiseSuppression::Level level) {
33 switch (level) {
34 case NoiseSuppression::kLow:
35 return 0;
36 case NoiseSuppression::kModerate:
37 return 1;
38 case NoiseSuppression::kHigh:
39 return 2;
40 case NoiseSuppression::kVeryHigh:
41 return 3;
42 }
43 assert(false);
44 return -1;
45 }
46 } // namespace
47
48 NoiseSuppressionImpl::NoiseSuppressionImpl(const AudioProcessing* apm,
49 rtc::CriticalSection* crit)
50 : ProcessingComponent(), apm_(apm), crit_(crit), level_(kModerate) {
51 RTC_DCHECK(apm);
52 RTC_DCHECK(crit); 50 RTC_DCHECK(crit);
53 } 51 }
54 52
55 NoiseSuppressionImpl::~NoiseSuppressionImpl() {} 53 NoiseSuppressionImpl::~NoiseSuppressionImpl() {}
56 54
55 void NoiseSuppressionImpl::Initialize(int channels, int sample_rate_hz) {
56 RTC_DCHECK_LE(0, channels);
57 std::vector<rtc::scoped_ptr<Suppressor>> new_suppressors(channels);
58 for (int i = 0; i < channels; i++) {
59 new_suppressors[i].reset(new Suppressor(sample_rate_hz));
60 }
61 rtc::CritScope cs(crit_);
62 suppressors_.swap(new_suppressors);
63 set_level(level_);
64 }
65
57 int NoiseSuppressionImpl::AnalyzeCaptureAudio(AudioBuffer* audio) { 66 int NoiseSuppressionImpl::AnalyzeCaptureAudio(AudioBuffer* audio) {
67 RTC_DCHECK(audio);
58 #if defined(WEBRTC_NS_FLOAT) 68 #if defined(WEBRTC_NS_FLOAT)
59 if (!is_component_enabled()) { 69 rtc::CritScope cs(crit_);
70 if (!enabled_) {
60 return AudioProcessing::kNoError; 71 return AudioProcessing::kNoError;
61 } 72 }
62 assert(audio->num_frames_per_band() <= 160);
63 assert(audio->num_channels() == num_handles());
64 73
65 for (int i = 0; i < num_handles(); ++i) { 74 RTC_DCHECK_GE(160u, audio->num_frames_per_band());
66 Handle* my_handle = static_cast<Handle*>(handle(i)); 75 RTC_DCHECK_EQ(suppressors_.size(),
67 76 static_cast<size_t>(audio->num_channels()));
68 WebRtcNs_Analyze(my_handle, audio->split_bands_const_f(i)[kBand0To8kHz]); 77 for (size_t i = 0; i < suppressors_.size(); i++) {
78 WebRtcNs_Analyze(suppressors_[i]->state(),
79 audio->split_bands_const_f(i)[kBand0To8kHz]);
69 } 80 }
70 #endif 81 #endif
71 return AudioProcessing::kNoError; 82 return AudioProcessing::kNoError;
72 } 83 }
73 84
74 int NoiseSuppressionImpl::ProcessCaptureAudio(AudioBuffer* audio) { 85 int NoiseSuppressionImpl::ProcessCaptureAudio(AudioBuffer* audio) {
86 RTC_DCHECK(audio);
75 rtc::CritScope cs(crit_); 87 rtc::CritScope cs(crit_);
76 if (!is_component_enabled()) { 88 if (!enabled_) {
77 return AudioProcessing::kNoError; 89 return AudioProcessing::kNoError;
78 } 90 }
79 assert(audio->num_frames_per_band() <= 160);
80 assert(audio->num_channels() == num_handles());
81 91
82 for (int i = 0; i < num_handles(); ++i) { 92 RTC_DCHECK_GE(160u, audio->num_frames_per_band());
83 Handle* my_handle = static_cast<Handle*>(handle(i)); 93 RTC_DCHECK_EQ(suppressors_.size(),
94 static_cast<size_t>(audio->num_channels()));
95 for (size_t i = 0; i < suppressors_.size(); i++) {
84 #if defined(WEBRTC_NS_FLOAT) 96 #if defined(WEBRTC_NS_FLOAT)
85 WebRtcNs_Process(my_handle, 97 WebRtcNs_Process(suppressors_[i]->state(),
86 audio->split_bands_const_f(i), 98 audio->split_bands_const_f(i),
87 audio->num_bands(), 99 audio->num_bands(),
88 audio->split_bands_f(i)); 100 audio->split_bands_f(i));
89 #elif defined(WEBRTC_NS_FIXED) 101 #elif defined(WEBRTC_NS_FIXED)
90 WebRtcNsx_Process(my_handle, 102 WebRtcNsx_Process(suppressors_[i]->state(),
91 audio->split_bands_const(i), 103 audio->split_bands_const(i),
92 audio->num_bands(), 104 audio->num_bands(),
93 audio->split_bands(i)); 105 audio->split_bands(i));
94 #endif 106 #endif
95 } 107 }
96 return AudioProcessing::kNoError; 108 return AudioProcessing::kNoError;
97 } 109 }
98 110
99 int NoiseSuppressionImpl::Enable(bool enable) { 111 int NoiseSuppressionImpl::Enable(bool enable) {
100 rtc::CritScope cs(crit_); 112 rtc::CritScope cs(crit_);
101 return EnableComponent(enable); 113 enabled_ = enable;
114 return AudioProcessing::kNoError;
102 } 115 }
103 116
104 bool NoiseSuppressionImpl::is_enabled() const { 117 bool NoiseSuppressionImpl::is_enabled() const {
105 rtc::CritScope cs(crit_); 118 rtc::CritScope cs(crit_);
106 return is_component_enabled(); 119 return enabled_;
107 } 120 }
108 121
109 int NoiseSuppressionImpl::set_level(Level level) { 122 int NoiseSuppressionImpl::set_level(Level level) {
110 rtc::CritScope cs(crit_); 123 rtc::CritScope cs(crit_);
111 if (MapSetting(level) == -1) { 124 int policy = 1;
112 return AudioProcessing::kBadParameterError; 125 switch (level) {
126 case NoiseSuppression::kLow:
127 policy = 0;
128 break;
129 case NoiseSuppression::kModerate:
130 policy = 1;
131 break;
132 case NoiseSuppression::kHigh:
133 policy = 2;
134 break;
135 case NoiseSuppression::kVeryHigh:
136 policy = 3;
137 break;
138 default:
139 RTC_NOTREACHED();
113 } 140 }
114
115 level_ = level; 141 level_ = level;
116 return Configure(); 142 for (auto& suppressor : suppressors_) {
143 int error = NS_SET_POLICY(suppressor->state(), policy);
144 RTC_DCHECK_EQ(0, error);
145 }
146 return AudioProcessing::kNoError;
117 } 147 }
118 148
119 NoiseSuppression::Level NoiseSuppressionImpl::level() const { 149 NoiseSuppression::Level NoiseSuppressionImpl::level() const {
120 rtc::CritScope cs(crit_); 150 rtc::CritScope cs(crit_);
121 return level_; 151 return level_;
122 } 152 }
123 153
124 float NoiseSuppressionImpl::speech_probability() const { 154 float NoiseSuppressionImpl::speech_probability() const {
125 rtc::CritScope cs(crit_); 155 rtc::CritScope cs(crit_);
126 #if defined(WEBRTC_NS_FLOAT) 156 #if defined(WEBRTC_NS_FLOAT)
127 float probability_average = 0.0f; 157 float probability_average = 0.0f;
128 for (int i = 0; i < num_handles(); i++) { 158 for (auto& suppressor : suppressors_) {
129 Handle* my_handle = static_cast<Handle*>(handle(i)); 159 probability_average +=
130 probability_average += WebRtcNs_prior_speech_probability(my_handle); 160 WebRtcNs_prior_speech_probability(suppressor->state());
131 } 161 }
132 return probability_average / num_handles(); 162 if (suppressors_.size() > 0) {
163 probability_average /= suppressors_.size();
164 }
165 return probability_average;
133 #elif defined(WEBRTC_NS_FIXED) 166 #elif defined(WEBRTC_NS_FIXED)
167 // TODO(peah): Returning error code as a float! Remove this.
134 // Currently not available for the fixed point implementation. 168 // Currently not available for the fixed point implementation.
135 return AudioProcessing::kUnsupportedFunctionError; 169 return AudioProcessing::kUnsupportedFunctionError;
136 #endif 170 #endif
137 } 171 }
138
139 void* NoiseSuppressionImpl::CreateHandle() const {
140 #if defined(WEBRTC_NS_FLOAT)
141 return WebRtcNs_Create();
142 #elif defined(WEBRTC_NS_FIXED)
143 return WebRtcNsx_Create();
144 #endif
145 }
146
147 void NoiseSuppressionImpl::DestroyHandle(void* handle) const {
148 #if defined(WEBRTC_NS_FLOAT)
149 WebRtcNs_Free(static_cast<Handle*>(handle));
150 #elif defined(WEBRTC_NS_FIXED)
151 WebRtcNsx_Free(static_cast<Handle*>(handle));
152 #endif
153 }
154
155 int NoiseSuppressionImpl::InitializeHandle(void* handle) const {
156 #if defined(WEBRTC_NS_FLOAT)
157 return WebRtcNs_Init(static_cast<Handle*>(handle),
158 apm_->proc_sample_rate_hz());
159 #elif defined(WEBRTC_NS_FIXED)
160 return WebRtcNsx_Init(static_cast<Handle*>(handle),
161 apm_->proc_sample_rate_hz());
162 #endif
163 }
164
165 int NoiseSuppressionImpl::ConfigureHandle(void* handle) const {
166 rtc::CritScope cs(crit_);
167 #if defined(WEBRTC_NS_FLOAT)
168 return WebRtcNs_set_policy(static_cast<Handle*>(handle),
169 MapSetting(level_));
170 #elif defined(WEBRTC_NS_FIXED)
171 return WebRtcNsx_set_policy(static_cast<Handle*>(handle),
172 MapSetting(level_));
173 #endif
174 }
175
176 int NoiseSuppressionImpl::num_handles_required() const {
177 return apm_->num_output_channels();
178 }
179
180 int NoiseSuppressionImpl::GetHandleError(void* handle) const {
181 // The NS has no get_error() function.
182 assert(handle != NULL);
183 return AudioProcessing::kUnspecifiedError;
184 }
185 } // namespace webrtc 172 } // namespace webrtc
OLDNEW
« no previous file with comments | « webrtc/modules/audio_processing/noise_suppression_impl.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698