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 |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
47 | 47 |
48 NoiseSuppressionImpl::NoiseSuppressionImpl(rtc::CriticalSection* crit) | 48 NoiseSuppressionImpl::NoiseSuppressionImpl(rtc::CriticalSection* crit) |
49 : crit_(crit) { | 49 : crit_(crit) { |
50 RTC_DCHECK(crit); | 50 RTC_DCHECK(crit); |
51 } | 51 } |
52 | 52 |
53 NoiseSuppressionImpl::~NoiseSuppressionImpl() {} | 53 NoiseSuppressionImpl::~NoiseSuppressionImpl() {} |
54 | 54 |
55 void NoiseSuppressionImpl::Initialize(int channels, int sample_rate_hz) { | 55 void NoiseSuppressionImpl::Initialize(int channels, int sample_rate_hz) { |
56 RTC_DCHECK_LE(0, channels); | 56 RTC_DCHECK_LE(0, channels); |
57 std::vector<rtc::scoped_ptr<Suppressor>> new_suppressors(channels); | 57 rtc::CritScope cs(crit_); |
58 for (int i = 0; i < channels; i++) { | 58 channels_ = channels; |
59 new_suppressors[i].reset(new Suppressor(sample_rate_hz)); | 59 sample_rate_hz_ = sample_rate_hz; |
| 60 std::vector<rtc::scoped_ptr<Suppressor>> new_suppressors; |
| 61 if (enabled_) { |
| 62 new_suppressors.resize(channels); |
| 63 for (int i = 0; i < channels; i++) { |
| 64 new_suppressors[i].reset(new Suppressor(sample_rate_hz)); |
| 65 } |
60 } | 66 } |
61 rtc::CritScope cs(crit_); | |
62 suppressors_.swap(new_suppressors); | 67 suppressors_.swap(new_suppressors); |
63 set_level(level_); | 68 set_level(level_); |
64 } | 69 } |
65 | 70 |
66 int NoiseSuppressionImpl::AnalyzeCaptureAudio(AudioBuffer* audio) { | 71 void NoiseSuppressionImpl::AnalyzeCaptureAudio(AudioBuffer* audio) { |
67 RTC_DCHECK(audio); | 72 RTC_DCHECK(audio); |
68 #if defined(WEBRTC_NS_FLOAT) | 73 #if defined(WEBRTC_NS_FLOAT) |
69 rtc::CritScope cs(crit_); | 74 rtc::CritScope cs(crit_); |
70 if (!enabled_) { | 75 if (!enabled_) { |
71 return AudioProcessing::kNoError; | 76 return; |
72 } | 77 } |
73 | 78 |
74 RTC_DCHECK_GE(160u, audio->num_frames_per_band()); | 79 RTC_DCHECK_GE(160u, audio->num_frames_per_band()); |
75 RTC_DCHECK_EQ(suppressors_.size(), | 80 RTC_DCHECK_EQ(suppressors_.size(), |
76 static_cast<size_t>(audio->num_channels())); | 81 static_cast<size_t>(audio->num_channels())); |
77 for (size_t i = 0; i < suppressors_.size(); i++) { | 82 for (size_t i = 0; i < suppressors_.size(); i++) { |
78 WebRtcNs_Analyze(suppressors_[i]->state(), | 83 WebRtcNs_Analyze(suppressors_[i]->state(), |
79 audio->split_bands_const_f(i)[kBand0To8kHz]); | 84 audio->split_bands_const_f(i)[kBand0To8kHz]); |
80 } | 85 } |
81 #endif | 86 #endif |
82 return AudioProcessing::kNoError; | |
83 } | 87 } |
84 | 88 |
85 int NoiseSuppressionImpl::ProcessCaptureAudio(AudioBuffer* audio) { | 89 void NoiseSuppressionImpl::ProcessCaptureAudio(AudioBuffer* audio) { |
86 RTC_DCHECK(audio); | 90 RTC_DCHECK(audio); |
87 rtc::CritScope cs(crit_); | 91 rtc::CritScope cs(crit_); |
88 if (!enabled_) { | 92 if (!enabled_) { |
89 return AudioProcessing::kNoError; | 93 return; |
90 } | 94 } |
91 | 95 |
92 RTC_DCHECK_GE(160u, audio->num_frames_per_band()); | 96 RTC_DCHECK_GE(160u, audio->num_frames_per_band()); |
93 RTC_DCHECK_EQ(suppressors_.size(), | 97 RTC_DCHECK_EQ(suppressors_.size(), |
94 static_cast<size_t>(audio->num_channels())); | 98 static_cast<size_t>(audio->num_channels())); |
95 for (size_t i = 0; i < suppressors_.size(); i++) { | 99 for (size_t i = 0; i < suppressors_.size(); i++) { |
96 #if defined(WEBRTC_NS_FLOAT) | 100 #if defined(WEBRTC_NS_FLOAT) |
97 WebRtcNs_Process(suppressors_[i]->state(), | 101 WebRtcNs_Process(suppressors_[i]->state(), |
98 audio->split_bands_const_f(i), | 102 audio->split_bands_const_f(i), |
99 audio->num_bands(), | 103 audio->num_bands(), |
100 audio->split_bands_f(i)); | 104 audio->split_bands_f(i)); |
101 #elif defined(WEBRTC_NS_FIXED) | 105 #elif defined(WEBRTC_NS_FIXED) |
102 WebRtcNsx_Process(suppressors_[i]->state(), | 106 WebRtcNsx_Process(suppressors_[i]->state(), |
103 audio->split_bands_const(i), | 107 audio->split_bands_const(i), |
104 audio->num_bands(), | 108 audio->num_bands(), |
105 audio->split_bands(i)); | 109 audio->split_bands(i)); |
106 #endif | 110 #endif |
107 } | 111 } |
| 112 } |
| 113 |
| 114 int NoiseSuppressionImpl::Enable(bool enable) { |
| 115 rtc::CritScope cs(crit_); |
| 116 if (enabled_ != enable) { |
| 117 enabled_ = enable; |
| 118 Initialize(channels_, sample_rate_hz_); |
| 119 } |
108 return AudioProcessing::kNoError; | 120 return AudioProcessing::kNoError; |
109 } | 121 } |
110 | 122 |
111 int NoiseSuppressionImpl::Enable(bool enable) { | |
112 rtc::CritScope cs(crit_); | |
113 enabled_ = enable; | |
114 return AudioProcessing::kNoError; | |
115 } | |
116 | |
117 bool NoiseSuppressionImpl::is_enabled() const { | 123 bool NoiseSuppressionImpl::is_enabled() const { |
118 rtc::CritScope cs(crit_); | 124 rtc::CritScope cs(crit_); |
119 return enabled_; | 125 return enabled_; |
120 } | 126 } |
121 | 127 |
122 int NoiseSuppressionImpl::set_level(Level level) { | 128 int NoiseSuppressionImpl::set_level(Level level) { |
123 rtc::CritScope cs(crit_); | |
124 int policy = 1; | 129 int policy = 1; |
125 switch (level) { | 130 switch (level) { |
126 case NoiseSuppression::kLow: | 131 case NoiseSuppression::kLow: |
127 policy = 0; | 132 policy = 0; |
128 break; | 133 break; |
129 case NoiseSuppression::kModerate: | 134 case NoiseSuppression::kModerate: |
130 policy = 1; | 135 policy = 1; |
131 break; | 136 break; |
132 case NoiseSuppression::kHigh: | 137 case NoiseSuppression::kHigh: |
133 policy = 2; | 138 policy = 2; |
134 break; | 139 break; |
135 case NoiseSuppression::kVeryHigh: | 140 case NoiseSuppression::kVeryHigh: |
136 policy = 3; | 141 policy = 3; |
137 break; | 142 break; |
138 default: | 143 default: |
139 RTC_NOTREACHED(); | 144 RTC_NOTREACHED(); |
140 } | 145 } |
| 146 rtc::CritScope cs(crit_); |
141 level_ = level; | 147 level_ = level; |
142 for (auto& suppressor : suppressors_) { | 148 for (auto& suppressor : suppressors_) { |
143 int error = NS_SET_POLICY(suppressor->state(), policy); | 149 int error = NS_SET_POLICY(suppressor->state(), policy); |
144 RTC_DCHECK_EQ(0, error); | 150 RTC_DCHECK_EQ(0, error); |
145 } | 151 } |
146 return AudioProcessing::kNoError; | 152 return AudioProcessing::kNoError; |
147 } | 153 } |
148 | 154 |
149 NoiseSuppression::Level NoiseSuppressionImpl::level() const { | 155 NoiseSuppression::Level NoiseSuppressionImpl::level() const { |
150 rtc::CritScope cs(crit_); | 156 rtc::CritScope cs(crit_); |
(...skipping 12 matching lines...) Expand all Loading... |
163 probability_average /= suppressors_.size(); | 169 probability_average /= suppressors_.size(); |
164 } | 170 } |
165 return probability_average; | 171 return probability_average; |
166 #elif defined(WEBRTC_NS_FIXED) | 172 #elif defined(WEBRTC_NS_FIXED) |
167 // TODO(peah): Returning error code as a float! Remove this. | 173 // TODO(peah): Returning error code as a float! Remove this. |
168 // Currently not available for the fixed point implementation. | 174 // Currently not available for the fixed point implementation. |
169 return AudioProcessing::kUnsupportedFunctionError; | 175 return AudioProcessing::kUnsupportedFunctionError; |
170 #endif | 176 #endif |
171 } | 177 } |
172 } // namespace webrtc | 178 } // namespace webrtc |
OLD | NEW |