blob: 3ce84fb0b454dfcf9eee45b4ae9333f88c321eb4 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
andrew@webrtc.org40654032012-01-30 20:51:15 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
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
andrew@webrtc.org78693fe2013-03-01 16:36:19 +000011#include "webrtc/modules/audio_processing/audio_processing_impl.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000012
ajm@google.com808e0e02011-08-03 21:08:51 +000013#include <assert.h>
niklase@google.com470e71d2011-07-07 08:21:25 +000014
xians@webrtc.orge46bc772014-10-10 08:36:56 +000015#include "webrtc/base/platform_file.h"
andrew@webrtc.org17e40642014-03-04 20:58:13 +000016#include "webrtc/common_audio/include/audio_util.h"
andrew@webrtc.org60730cf2014-01-07 17:45:09 +000017#include "webrtc/common_audio/signal_processing/include/signal_processing_library.h"
pbos@webrtc.org788acd12014-12-15 09:41:24 +000018#include "webrtc/modules/audio_processing/agc/agc_manager_direct.h"
19#include "webrtc/modules/audio_processing/transient/transient_suppressor.h"
andrew@webrtc.org78693fe2013-03-01 16:36:19 +000020#include "webrtc/modules/audio_processing/audio_buffer.h"
aluebs@webrtc.org87893762014-11-27 23:40:25 +000021#include "webrtc/modules/audio_processing/channel_buffer.h"
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000022#include "webrtc/modules/audio_processing/common.h"
andrew@webrtc.org56e4a052014-02-27 22:23:17 +000023#include "webrtc/modules/audio_processing/echo_cancellation_impl.h"
andrew@webrtc.org78693fe2013-03-01 16:36:19 +000024#include "webrtc/modules/audio_processing/echo_control_mobile_impl.h"
25#include "webrtc/modules/audio_processing/gain_control_impl.h"
26#include "webrtc/modules/audio_processing/high_pass_filter_impl.h"
27#include "webrtc/modules/audio_processing/level_estimator_impl.h"
28#include "webrtc/modules/audio_processing/noise_suppression_impl.h"
29#include "webrtc/modules/audio_processing/processing_component.h"
andrew@webrtc.org78693fe2013-03-01 16:36:19 +000030#include "webrtc/modules/audio_processing/voice_detection_impl.h"
31#include "webrtc/modules/interface/module_common_types.h"
andrew@webrtc.org60730cf2014-01-07 17:45:09 +000032#include "webrtc/system_wrappers/interface/compile_assert.h"
andrew@webrtc.org78693fe2013-03-01 16:36:19 +000033#include "webrtc/system_wrappers/interface/critical_section_wrapper.h"
34#include "webrtc/system_wrappers/interface/file_wrapper.h"
35#include "webrtc/system_wrappers/interface/logging.h"
andrew@webrtc.org7bf26462011-12-03 00:03:31 +000036
37#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
38// Files generated at build-time by the protobuf compiler.
leozwang@webrtc.orga3736342012-03-16 21:36:00 +000039#ifdef WEBRTC_ANDROID_PLATFORM_BUILD
leozwang@webrtc.org534e4952012-10-22 21:21:52 +000040#include "external/webrtc/webrtc/modules/audio_processing/debug.pb.h"
leozwang@google.comce9bfbb2011-08-03 23:34:31 +000041#else
ajm@google.com808e0e02011-08-03 21:08:51 +000042#include "webrtc/audio_processing/debug.pb.h"
leozwang@google.comce9bfbb2011-08-03 23:34:31 +000043#endif
andrew@webrtc.org7bf26462011-12-03 00:03:31 +000044#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
niklase@google.com470e71d2011-07-07 08:21:25 +000045
andrew@webrtc.org60730cf2014-01-07 17:45:09 +000046#define RETURN_ON_ERR(expr) \
47 do { \
48 int err = expr; \
49 if (err != kNoError) { \
50 return err; \
51 } \
52 } while (0)
53
niklase@google.com470e71d2011-07-07 08:21:25 +000054namespace webrtc {
andrew@webrtc.org60730cf2014-01-07 17:45:09 +000055
56// Throughout webrtc, it's assumed that success is represented by zero.
57COMPILE_ASSERT(AudioProcessing::kNoError == 0, no_error_must_be_zero);
58
pbos@webrtc.org788acd12014-12-15 09:41:24 +000059// This class has two main functionalities:
60//
61// 1) It is returned instead of the real GainControl after the new AGC has been
62// enabled in order to prevent an outside user from overriding compression
63// settings. It doesn't do anything in its implementation, except for
64// delegating the const methods and Enable calls to the real GainControl, so
65// AGC can still be disabled.
66//
67// 2) It is injected into AgcManagerDirect and implements volume callbacks for
68// getting and setting the volume level. It just caches this value to be used
69// in VoiceEngine later.
70class GainControlForNewAgc : public GainControl, public VolumeCallbacks {
71 public:
72 explicit GainControlForNewAgc(GainControlImpl* gain_control)
73 : real_gain_control_(gain_control),
74 volume_(0) {
75 }
76
77 // GainControl implementation.
78 virtual int Enable(bool enable) OVERRIDE {
79 return real_gain_control_->Enable(enable);
80 }
81 virtual bool is_enabled() const OVERRIDE {
82 return real_gain_control_->is_enabled();
83 }
84 virtual int set_stream_analog_level(int level) OVERRIDE {
85 volume_ = level;
86 return AudioProcessing::kNoError;
87 }
88 virtual int stream_analog_level() OVERRIDE {
89 return volume_;
90 }
91 virtual int set_mode(Mode mode) OVERRIDE { return AudioProcessing::kNoError; }
92 virtual Mode mode() const OVERRIDE { return GainControl::kAdaptiveAnalog; }
93 virtual int set_target_level_dbfs(int level) OVERRIDE {
94 return AudioProcessing::kNoError;
95 }
96 virtual int target_level_dbfs() const OVERRIDE {
97 return real_gain_control_->target_level_dbfs();
98 }
99 virtual int set_compression_gain_db(int gain) OVERRIDE {
100 return AudioProcessing::kNoError;
101 }
102 virtual int compression_gain_db() const OVERRIDE {
103 return real_gain_control_->compression_gain_db();
104 }
105 virtual int enable_limiter(bool enable) OVERRIDE {
106 return AudioProcessing::kNoError;
107 }
108 virtual bool is_limiter_enabled() const OVERRIDE {
109 return real_gain_control_->is_limiter_enabled();
110 }
111 virtual int set_analog_level_limits(int minimum,
112 int maximum) OVERRIDE {
113 return AudioProcessing::kNoError;
114 }
115 virtual int analog_level_minimum() const OVERRIDE {
116 return real_gain_control_->analog_level_minimum();
117 }
118 virtual int analog_level_maximum() const OVERRIDE {
119 return real_gain_control_->analog_level_maximum();
120 }
121 virtual bool stream_is_saturated() const OVERRIDE {
122 return real_gain_control_->stream_is_saturated();
123 }
124
125 // VolumeCallbacks implementation.
126 virtual void SetMicVolume(int volume) OVERRIDE {
127 volume_ = volume;
128 }
129 virtual int GetMicVolume() OVERRIDE {
130 return volume_;
131 }
132
133 private:
134 GainControl* real_gain_control_;
135 int volume_;
136};
137
niklase@google.com470e71d2011-07-07 08:21:25 +0000138AudioProcessing* AudioProcessing::Create(int id) {
andrew@webrtc.orge84978f2014-01-25 02:09:06 +0000139 return Create();
140}
141
142AudioProcessing* AudioProcessing::Create() {
143 Config config;
144 return Create(config);
145}
146
147AudioProcessing* AudioProcessing::Create(const Config& config) {
148 AudioProcessingImpl* apm = new AudioProcessingImpl(config);
niklase@google.com470e71d2011-07-07 08:21:25 +0000149 if (apm->Initialize() != kNoError) {
150 delete apm;
151 apm = NULL;
152 }
153
154 return apm;
155}
156
andrew@webrtc.orge84978f2014-01-25 02:09:06 +0000157AudioProcessingImpl::AudioProcessingImpl(const Config& config)
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000158 : echo_cancellation_(NULL),
niklase@google.com470e71d2011-07-07 08:21:25 +0000159 echo_control_mobile_(NULL),
160 gain_control_(NULL),
161 high_pass_filter_(NULL),
162 level_estimator_(NULL),
163 noise_suppression_(NULL),
164 voice_detection_(NULL),
niklase@google.com470e71d2011-07-07 08:21:25 +0000165 crit_(CriticalSectionWrapper::CreateCriticalSection()),
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000166#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
167 debug_file_(FileWrapper::Create()),
168 event_msg_(new audioproc::Event()),
169#endif
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000170 fwd_in_format_(kSampleRate16kHz, 1),
aluebs@webrtc.org27d106b2014-12-11 17:09:21 +0000171 fwd_proc_format_(kSampleRate16kHz),
172 fwd_out_format_(kSampleRate16kHz, 1),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000173 rev_in_format_(kSampleRate16kHz, 1),
174 rev_proc_format_(kSampleRate16kHz, 1),
175 split_rate_(kSampleRate16kHz),
niklase@google.com470e71d2011-07-07 08:21:25 +0000176 stream_delay_ms_(0),
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000177 delay_offset_ms_(0),
niklase@google.com470e71d2011-07-07 08:21:25 +0000178 was_stream_delay_set_(false),
andrew@webrtc.org38bf2492014-02-13 17:43:44 +0000179 output_will_be_muted_(false),
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000180 key_pressed_(false),
181#if defined(WEBRTC_ANDROID) || defined(WEBRTC_IOS)
182 use_new_agc_(false),
183#else
184 use_new_agc_(config.Get<ExperimentalAgc>().enabled),
185#endif
186 transient_suppressor_enabled_(config.Get<ExperimentalNs>().enabled) {
andrew@webrtc.org56e4a052014-02-27 22:23:17 +0000187 echo_cancellation_ = new EchoCancellationImpl(this, crit_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000188 component_list_.push_back(echo_cancellation_);
189
andrew@webrtc.org56e4a052014-02-27 22:23:17 +0000190 echo_control_mobile_ = new EchoControlMobileImpl(this, crit_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000191 component_list_.push_back(echo_control_mobile_);
192
andrew@webrtc.org56e4a052014-02-27 22:23:17 +0000193 gain_control_ = new GainControlImpl(this, crit_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000194 component_list_.push_back(gain_control_);
195
andrew@webrtc.org56e4a052014-02-27 22:23:17 +0000196 high_pass_filter_ = new HighPassFilterImpl(this, crit_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000197 component_list_.push_back(high_pass_filter_);
198
andrew@webrtc.org56e4a052014-02-27 22:23:17 +0000199 level_estimator_ = new LevelEstimatorImpl(this, crit_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000200 component_list_.push_back(level_estimator_);
201
andrew@webrtc.org56e4a052014-02-27 22:23:17 +0000202 noise_suppression_ = new NoiseSuppressionImpl(this, crit_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000203 component_list_.push_back(noise_suppression_);
204
andrew@webrtc.org56e4a052014-02-27 22:23:17 +0000205 voice_detection_ = new VoiceDetectionImpl(this, crit_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000206 component_list_.push_back(voice_detection_);
andrew@webrtc.orge84978f2014-01-25 02:09:06 +0000207
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000208 gain_control_for_new_agc_.reset(new GainControlForNewAgc(gain_control_));
209
andrew@webrtc.orge84978f2014-01-25 02:09:06 +0000210 SetExtraOptions(config);
niklase@google.com470e71d2011-07-07 08:21:25 +0000211}
212
213AudioProcessingImpl::~AudioProcessingImpl() {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000214 {
215 CriticalSectionScoped crit_scoped(crit_);
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000216 // Depends on gain_control_ and gain_control_for_new_agc_.
217 agc_manager_.reset();
218 // Depends on gain_control_.
219 gain_control_for_new_agc_.reset();
andrew@webrtc.org81865342012-10-27 00:28:27 +0000220 while (!component_list_.empty()) {
221 ProcessingComponent* component = component_list_.front();
222 component->Destroy();
223 delete component;
224 component_list_.pop_front();
225 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000226
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000227#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
andrew@webrtc.org81865342012-10-27 00:28:27 +0000228 if (debug_file_->Open()) {
229 debug_file_->CloseFile();
230 }
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000231#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000232 }
andrew@webrtc.org16cfbe22012-08-29 16:58:25 +0000233 delete crit_;
234 crit_ = NULL;
niklase@google.com470e71d2011-07-07 08:21:25 +0000235}
236
niklase@google.com470e71d2011-07-07 08:21:25 +0000237int AudioProcessingImpl::Initialize() {
andrew@webrtc.org40654032012-01-30 20:51:15 +0000238 CriticalSectionScoped crit_scoped(crit_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000239 return InitializeLocked();
240}
241
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000242int AudioProcessingImpl::set_sample_rate_hz(int rate) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000243 CriticalSectionScoped crit_scoped(crit_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000244 return InitializeLocked(rate,
245 rate,
246 rev_in_format_.rate(),
247 fwd_in_format_.num_channels(),
aluebs@webrtc.org27d106b2014-12-11 17:09:21 +0000248 fwd_out_format_.num_channels(),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000249 rev_in_format_.num_channels());
250}
251
252int AudioProcessingImpl::Initialize(int input_sample_rate_hz,
253 int output_sample_rate_hz,
254 int reverse_sample_rate_hz,
255 ChannelLayout input_layout,
256 ChannelLayout output_layout,
257 ChannelLayout reverse_layout) {
258 CriticalSectionScoped crit_scoped(crit_);
259 return InitializeLocked(input_sample_rate_hz,
260 output_sample_rate_hz,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000261 reverse_sample_rate_hz,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000262 ChannelsFromLayout(input_layout),
263 ChannelsFromLayout(output_layout),
264 ChannelsFromLayout(reverse_layout));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000265}
266
niklase@google.com470e71d2011-07-07 08:21:25 +0000267int AudioProcessingImpl::InitializeLocked() {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000268 render_audio_.reset(new AudioBuffer(rev_in_format_.samples_per_channel(),
269 rev_in_format_.num_channels(),
270 rev_proc_format_.samples_per_channel(),
271 rev_proc_format_.num_channels(),
272 rev_proc_format_.samples_per_channel()));
273 capture_audio_.reset(new AudioBuffer(fwd_in_format_.samples_per_channel(),
274 fwd_in_format_.num_channels(),
275 fwd_proc_format_.samples_per_channel(),
aluebs@webrtc.org27d106b2014-12-11 17:09:21 +0000276 fwd_out_format_.num_channels(),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000277 fwd_out_format_.samples_per_channel()));
niklase@google.com470e71d2011-07-07 08:21:25 +0000278
niklase@google.com470e71d2011-07-07 08:21:25 +0000279 // Initialize all components.
280 std::list<ProcessingComponent*>::iterator it;
andrew@webrtc.org81865342012-10-27 00:28:27 +0000281 for (it = component_list_.begin(); it != component_list_.end(); ++it) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000282 int err = (*it)->Initialize();
283 if (err != kNoError) {
284 return err;
285 }
286 }
287
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000288 int err = InitializeExperimentalAgc();
289 if (err != kNoError) {
290 return err;
291 }
292
293 err = InitializeTransient();
294 if (err != kNoError) {
295 return err;
296 }
297
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000298#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
ajm@google.com808e0e02011-08-03 21:08:51 +0000299 if (debug_file_->Open()) {
300 int err = WriteInitMessage();
301 if (err != kNoError) {
302 return err;
303 }
304 }
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000305#endif
ajm@google.com808e0e02011-08-03 21:08:51 +0000306
niklase@google.com470e71d2011-07-07 08:21:25 +0000307 return kNoError;
308}
309
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000310int AudioProcessingImpl::InitializeLocked(int input_sample_rate_hz,
311 int output_sample_rate_hz,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000312 int reverse_sample_rate_hz,
313 int num_input_channels,
314 int num_output_channels,
315 int num_reverse_channels) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000316 if (input_sample_rate_hz <= 0 ||
317 output_sample_rate_hz <= 0 ||
318 reverse_sample_rate_hz <= 0) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000319 return kBadSampleRateError;
320 }
321 if (num_output_channels > num_input_channels) {
322 return kBadNumberChannelsError;
323 }
324 // Only mono and stereo supported currently.
325 if (num_input_channels > 2 || num_input_channels < 1 ||
326 num_output_channels > 2 || num_output_channels < 1 ||
327 num_reverse_channels > 2 || num_reverse_channels < 1) {
328 return kBadNumberChannelsError;
329 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000330
331 fwd_in_format_.set(input_sample_rate_hz, num_input_channels);
aluebs@webrtc.org27d106b2014-12-11 17:09:21 +0000332 fwd_out_format_.set(output_sample_rate_hz, num_output_channels);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000333 rev_in_format_.set(reverse_sample_rate_hz, num_reverse_channels);
334
335 // We process at the closest native rate >= min(input rate, output rate)...
336 int min_proc_rate = std::min(fwd_in_format_.rate(), fwd_out_format_.rate());
337 int fwd_proc_rate;
338 if (min_proc_rate > kSampleRate16kHz) {
339 fwd_proc_rate = kSampleRate32kHz;
340 } else if (min_proc_rate > kSampleRate8kHz) {
341 fwd_proc_rate = kSampleRate16kHz;
342 } else {
343 fwd_proc_rate = kSampleRate8kHz;
344 }
345 // ...with one exception.
346 if (echo_control_mobile_->is_enabled() && min_proc_rate > kSampleRate16kHz) {
347 fwd_proc_rate = kSampleRate16kHz;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000348 }
349
aluebs@webrtc.org27d106b2014-12-11 17:09:21 +0000350 fwd_proc_format_.set(fwd_proc_rate);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000351
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000352 // We normally process the reverse stream at 16 kHz. Unless...
353 int rev_proc_rate = kSampleRate16kHz;
354 if (fwd_proc_format_.rate() == kSampleRate8kHz) {
355 // ...the forward stream is at 8 kHz.
356 rev_proc_rate = kSampleRate8kHz;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000357 } else {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000358 if (rev_in_format_.rate() == kSampleRate32kHz) {
359 // ...or the input is at 32 kHz, in which case we use the splitting
360 // filter rather than the resampler.
361 rev_proc_rate = kSampleRate32kHz;
362 }
363 }
364
andrew@webrtc.org30be8272014-09-24 20:06:23 +0000365 // Always downmix the reverse stream to mono for analysis. This has been
366 // demonstrated to work well for AEC in most practical scenarios.
367 rev_proc_format_.set(rev_proc_rate, 1);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000368
aluebs@webrtc.org087da132014-11-17 23:01:23 +0000369 if (fwd_proc_format_.rate() == kSampleRate32kHz ||
370 fwd_proc_format_.rate() == kSampleRate48kHz) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000371 split_rate_ = kSampleRate16kHz;
372 } else {
373 split_rate_ = fwd_proc_format_.rate();
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000374 }
375
376 return InitializeLocked();
377}
378
379// Calls InitializeLocked() if any of the audio parameters have changed from
380// their current values.
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000381int AudioProcessingImpl::MaybeInitializeLocked(int input_sample_rate_hz,
382 int output_sample_rate_hz,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000383 int reverse_sample_rate_hz,
384 int num_input_channels,
385 int num_output_channels,
386 int num_reverse_channels) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000387 if (input_sample_rate_hz == fwd_in_format_.rate() &&
388 output_sample_rate_hz == fwd_out_format_.rate() &&
389 reverse_sample_rate_hz == rev_in_format_.rate() &&
390 num_input_channels == fwd_in_format_.num_channels() &&
aluebs@webrtc.org27d106b2014-12-11 17:09:21 +0000391 num_output_channels == fwd_out_format_.num_channels() &&
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000392 num_reverse_channels == rev_in_format_.num_channels()) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000393 return kNoError;
394 }
395
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000396 return InitializeLocked(input_sample_rate_hz,
397 output_sample_rate_hz,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000398 reverse_sample_rate_hz,
399 num_input_channels,
400 num_output_channels,
401 num_reverse_channels);
402}
403
andrew@webrtc.org61e596f2013-07-25 18:28:29 +0000404void AudioProcessingImpl::SetExtraOptions(const Config& config) {
andrew@webrtc.orge84978f2014-01-25 02:09:06 +0000405 CriticalSectionScoped crit_scoped(crit_);
andrew@webrtc.org61e596f2013-07-25 18:28:29 +0000406 std::list<ProcessingComponent*>::iterator it;
407 for (it = component_list_.begin(); it != component_list_.end(); ++it)
408 (*it)->SetExtraOptions(config);
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000409
410 if (transient_suppressor_enabled_ != config.Get<ExperimentalNs>().enabled) {
411 transient_suppressor_enabled_ = config.Get<ExperimentalNs>().enabled;
412 InitializeTransient();
413 }
andrew@webrtc.org61e596f2013-07-25 18:28:29 +0000414}
415
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000416int AudioProcessingImpl::input_sample_rate_hz() const {
andrew@webrtc.org40654032012-01-30 20:51:15 +0000417 CriticalSectionScoped crit_scoped(crit_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000418 return fwd_in_format_.rate();
niklase@google.com470e71d2011-07-07 08:21:25 +0000419}
420
andrew@webrtc.org46b31b12014-04-23 03:33:54 +0000421int AudioProcessingImpl::sample_rate_hz() const {
422 CriticalSectionScoped crit_scoped(crit_);
423 return fwd_in_format_.rate();
424}
425
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000426int AudioProcessingImpl::proc_sample_rate_hz() const {
427 return fwd_proc_format_.rate();
niklase@google.com470e71d2011-07-07 08:21:25 +0000428}
429
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000430int AudioProcessingImpl::proc_split_sample_rate_hz() const {
431 return split_rate_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000432}
433
434int AudioProcessingImpl::num_reverse_channels() const {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000435 return rev_proc_format_.num_channels();
niklase@google.com470e71d2011-07-07 08:21:25 +0000436}
437
438int AudioProcessingImpl::num_input_channels() const {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000439 return fwd_in_format_.num_channels();
niklase@google.com470e71d2011-07-07 08:21:25 +0000440}
441
442int AudioProcessingImpl::num_output_channels() const {
aluebs@webrtc.org27d106b2014-12-11 17:09:21 +0000443 return fwd_out_format_.num_channels();
niklase@google.com470e71d2011-07-07 08:21:25 +0000444}
445
andrew@webrtc.org17342e52014-02-12 22:28:31 +0000446void AudioProcessingImpl::set_output_will_be_muted(bool muted) {
447 output_will_be_muted_ = muted;
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000448 CriticalSectionScoped lock(crit_);
449 if (agc_manager_.get()) {
450 agc_manager_->SetCaptureMuted(output_will_be_muted_);
451 }
andrew@webrtc.org17342e52014-02-12 22:28:31 +0000452}
453
454bool AudioProcessingImpl::output_will_be_muted() const {
455 return output_will_be_muted_;
456}
457
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000458int AudioProcessingImpl::ProcessStream(const float* const* src,
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000459 int samples_per_channel,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000460 int input_sample_rate_hz,
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000461 ChannelLayout input_layout,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000462 int output_sample_rate_hz,
463 ChannelLayout output_layout,
464 float* const* dest) {
andrew@webrtc.org40654032012-01-30 20:51:15 +0000465 CriticalSectionScoped crit_scoped(crit_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000466 if (!src || !dest) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000467 return kNullPointerError;
468 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000469
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000470 RETURN_ON_ERR(MaybeInitializeLocked(input_sample_rate_hz,
471 output_sample_rate_hz,
472 rev_in_format_.rate(),
473 ChannelsFromLayout(input_layout),
474 ChannelsFromLayout(output_layout),
475 rev_in_format_.num_channels()));
476 if (samples_per_channel != fwd_in_format_.samples_per_channel()) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000477 return kBadDataLengthError;
478 }
479
480#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
481 if (debug_file_->Open()) {
482 event_msg_->set_type(audioproc::Event::STREAM);
483 audioproc::Stream* msg = event_msg_->mutable_stream();
aluebs@webrtc.org59a1b1b2014-08-28 10:43:09 +0000484 const size_t channel_size =
485 sizeof(float) * fwd_in_format_.samples_per_channel();
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000486 for (int i = 0; i < fwd_in_format_.num_channels(); ++i)
487 msg->add_input_channel(src[i], channel_size);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000488 }
489#endif
490
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000491 capture_audio_->CopyFrom(src, samples_per_channel, input_layout);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000492 RETURN_ON_ERR(ProcessStreamLocked());
493 if (output_copy_needed(is_data_processed())) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000494 capture_audio_->CopyTo(fwd_out_format_.samples_per_channel(),
495 output_layout,
496 dest);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000497 }
498
499#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
500 if (debug_file_->Open()) {
501 audioproc::Stream* msg = event_msg_->mutable_stream();
aluebs@webrtc.org59a1b1b2014-08-28 10:43:09 +0000502 const size_t channel_size =
503 sizeof(float) * fwd_out_format_.samples_per_channel();
aluebs@webrtc.org27d106b2014-12-11 17:09:21 +0000504 for (int i = 0; i < fwd_out_format_.num_channels(); ++i)
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000505 msg->add_output_channel(dest[i], channel_size);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000506 RETURN_ON_ERR(WriteMessageToDebugFile());
507 }
508#endif
509
510 return kNoError;
511}
512
513int AudioProcessingImpl::ProcessStream(AudioFrame* frame) {
514 CriticalSectionScoped crit_scoped(crit_);
515 if (!frame) {
516 return kNullPointerError;
517 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000518 // Must be a native rate.
519 if (frame->sample_rate_hz_ != kSampleRate8kHz &&
520 frame->sample_rate_hz_ != kSampleRate16kHz &&
aluebs@webrtc.org087da132014-11-17 23:01:23 +0000521 frame->sample_rate_hz_ != kSampleRate32kHz &&
522 frame->sample_rate_hz_ != kSampleRate48kHz) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000523 return kBadSampleRateError;
524 }
525 if (echo_control_mobile_->is_enabled() &&
526 frame->sample_rate_hz_ > kSampleRate16kHz) {
527 LOG(LS_ERROR) << "AECM only supports 16 or 8 kHz sample rates";
528 return kUnsupportedComponentError;
529 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000530
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000531 // TODO(ajm): The input and output rates and channels are currently
532 // constrained to be identical in the int16 interface.
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000533 RETURN_ON_ERR(MaybeInitializeLocked(frame->sample_rate_hz_,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000534 frame->sample_rate_hz_,
535 rev_in_format_.rate(),
536 frame->num_channels_,
537 frame->num_channels_,
538 rev_in_format_.num_channels()));
539 if (frame->samples_per_channel_ != fwd_in_format_.samples_per_channel()) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000540 return kBadDataLengthError;
541 }
542
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000543#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
niklase@google.com470e71d2011-07-07 08:21:25 +0000544 if (debug_file_->Open()) {
ajm@google.com808e0e02011-08-03 21:08:51 +0000545 event_msg_->set_type(audioproc::Event::STREAM);
546 audioproc::Stream* msg = event_msg_->mutable_stream();
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000547 const size_t data_size = sizeof(int16_t) *
andrew@webrtc.org63a50982012-05-02 23:56:37 +0000548 frame->samples_per_channel_ *
549 frame->num_channels_;
550 msg->set_input_data(frame->data_, data_size);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000551 }
552#endif
553
554 capture_audio_->DeinterleaveFrom(frame);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000555 RETURN_ON_ERR(ProcessStreamLocked());
556 capture_audio_->InterleaveTo(frame, output_copy_needed(is_data_processed()));
557
558#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
559 if (debug_file_->Open()) {
560 audioproc::Stream* msg = event_msg_->mutable_stream();
561 const size_t data_size = sizeof(int16_t) *
562 frame->samples_per_channel_ *
563 frame->num_channels_;
564 msg->set_output_data(frame->data_, data_size);
565 RETURN_ON_ERR(WriteMessageToDebugFile());
566 }
567#endif
568
569 return kNoError;
570}
571
572
573int AudioProcessingImpl::ProcessStreamLocked() {
574#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
575 if (debug_file_->Open()) {
576 audioproc::Stream* msg = event_msg_->mutable_stream();
ajm@google.com808e0e02011-08-03 21:08:51 +0000577 msg->set_delay(stream_delay_ms_);
578 msg->set_drift(echo_cancellation_->stream_drift_samples());
579 msg->set_level(gain_control_->stream_analog_level());
andrew@webrtc.orgce8e0772014-02-12 15:28:30 +0000580 msg->set_keypress(key_pressed_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000581 }
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000582#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000583
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000584 AudioBuffer* ca = capture_audio_.get(); // For brevity.
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000585 if (use_new_agc_ && gain_control_->is_enabled()) {
586 agc_manager_->AnalyzePreProcess(ca->data(0),
587 ca->num_channels(),
588 fwd_proc_format_.samples_per_channel());
589 }
590
andrew@webrtc.org369166a2012-04-24 18:38:03 +0000591 bool data_processed = is_data_processed();
592 if (analysis_needed(data_processed)) {
aluebs@webrtc.orgbe05c742014-11-14 22:18:10 +0000593 ca->SplitIntoFrequencyBands();
niklase@google.com470e71d2011-07-07 08:21:25 +0000594 }
595
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000596 RETURN_ON_ERR(high_pass_filter_->ProcessCaptureAudio(ca));
597 RETURN_ON_ERR(gain_control_->AnalyzeCaptureAudio(ca));
aluebs@webrtc.orga0ce9fa2014-09-24 14:18:03 +0000598 RETURN_ON_ERR(noise_suppression_->AnalyzeCaptureAudio(ca));
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000599 RETURN_ON_ERR(echo_cancellation_->ProcessCaptureAudio(ca));
niklase@google.com470e71d2011-07-07 08:21:25 +0000600
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000601 if (echo_control_mobile_->is_enabled() && noise_suppression_->is_enabled()) {
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000602 ca->CopyLowPassToReference();
niklase@google.com470e71d2011-07-07 08:21:25 +0000603 }
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000604 RETURN_ON_ERR(noise_suppression_->ProcessCaptureAudio(ca));
605 RETURN_ON_ERR(echo_control_mobile_->ProcessCaptureAudio(ca));
606 RETURN_ON_ERR(voice_detection_->ProcessCaptureAudio(ca));
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000607
608 if (use_new_agc_ && gain_control_->is_enabled()) {
609 agc_manager_->Process(ca->split_bands_const(0)[kBand0To8kHz],
610 ca->samples_per_split_channel(),
611 split_rate_);
612 }
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000613 RETURN_ON_ERR(gain_control_->ProcessCaptureAudio(ca));
niklase@google.com470e71d2011-07-07 08:21:25 +0000614
andrew@webrtc.org369166a2012-04-24 18:38:03 +0000615 if (synthesis_needed(data_processed)) {
aluebs@webrtc.orgbe05c742014-11-14 22:18:10 +0000616 ca->MergeFrequencyBands();
niklase@google.com470e71d2011-07-07 08:21:25 +0000617 }
618
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000619 // TODO(aluebs): Investigate if the transient suppression placement should be
620 // before or after the AGC.
621 if (transient_suppressor_enabled_) {
622 float voice_probability =
623 agc_manager_.get() ? agc_manager_->voice_probability() : 1.f;
624
625 transient_suppressor_->Suppress(ca->data_f(0),
626 ca->samples_per_channel(),
627 ca->num_channels(),
628 ca->split_bands_const_f(0)[kBand0To8kHz],
629 ca->samples_per_split_channel(),
630 ca->keyboard_data(),
631 ca->samples_per_keyboard_channel(),
632 voice_probability,
633 key_pressed_);
634 }
635
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000636 // The level estimator operates on the recombined data.
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000637 RETURN_ON_ERR(level_estimator_->ProcessStream(ca));
ajm@google.com808e0e02011-08-03 21:08:51 +0000638
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000639 was_stream_delay_set_ = false;
niklase@google.com470e71d2011-07-07 08:21:25 +0000640 return kNoError;
641}
642
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000643int AudioProcessingImpl::AnalyzeReverseStream(const float* const* data,
644 int samples_per_channel,
645 int sample_rate_hz,
646 ChannelLayout layout) {
647 CriticalSectionScoped crit_scoped(crit_);
648 if (data == NULL) {
649 return kNullPointerError;
650 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000651
652 const int num_channels = ChannelsFromLayout(layout);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000653 RETURN_ON_ERR(MaybeInitializeLocked(fwd_in_format_.rate(),
654 fwd_out_format_.rate(),
655 sample_rate_hz,
656 fwd_in_format_.num_channels(),
aluebs@webrtc.org27d106b2014-12-11 17:09:21 +0000657 fwd_out_format_.num_channels(),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000658 num_channels));
659 if (samples_per_channel != rev_in_format_.samples_per_channel()) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000660 return kBadDataLengthError;
661 }
662
663#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
664 if (debug_file_->Open()) {
665 event_msg_->set_type(audioproc::Event::REVERSE_STREAM);
666 audioproc::ReverseStream* msg = event_msg_->mutable_reverse_stream();
aluebs@webrtc.org59a1b1b2014-08-28 10:43:09 +0000667 const size_t channel_size =
668 sizeof(float) * rev_in_format_.samples_per_channel();
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000669 for (int i = 0; i < num_channels; ++i)
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000670 msg->add_channel(data[i], channel_size);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000671 RETURN_ON_ERR(WriteMessageToDebugFile());
672 }
673#endif
674
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000675 render_audio_->CopyFrom(data, samples_per_channel, layout);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000676 return AnalyzeReverseStreamLocked();
677}
678
niklase@google.com470e71d2011-07-07 08:21:25 +0000679int AudioProcessingImpl::AnalyzeReverseStream(AudioFrame* frame) {
andrew@webrtc.org40654032012-01-30 20:51:15 +0000680 CriticalSectionScoped crit_scoped(crit_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000681 if (frame == NULL) {
682 return kNullPointerError;
683 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000684 // Must be a native rate.
685 if (frame->sample_rate_hz_ != kSampleRate8kHz &&
686 frame->sample_rate_hz_ != kSampleRate16kHz &&
aluebs@webrtc.org087da132014-11-17 23:01:23 +0000687 frame->sample_rate_hz_ != kSampleRate32kHz &&
688 frame->sample_rate_hz_ != kSampleRate48kHz) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000689 return kBadSampleRateError;
690 }
691 // This interface does not tolerate different forward and reverse rates.
692 if (frame->sample_rate_hz_ != fwd_in_format_.rate()) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000693 return kBadSampleRateError;
694 }
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000695
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000696 RETURN_ON_ERR(MaybeInitializeLocked(fwd_in_format_.rate(),
697 fwd_out_format_.rate(),
698 frame->sample_rate_hz_,
699 fwd_in_format_.num_channels(),
700 fwd_in_format_.num_channels(),
701 frame->num_channels_));
702 if (frame->samples_per_channel_ != rev_in_format_.samples_per_channel()) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000703 return kBadDataLengthError;
704 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000705
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000706#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
niklase@google.com470e71d2011-07-07 08:21:25 +0000707 if (debug_file_->Open()) {
ajm@google.com808e0e02011-08-03 21:08:51 +0000708 event_msg_->set_type(audioproc::Event::REVERSE_STREAM);
709 audioproc::ReverseStream* msg = event_msg_->mutable_reverse_stream();
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000710 const size_t data_size = sizeof(int16_t) *
andrew@webrtc.org63a50982012-05-02 23:56:37 +0000711 frame->samples_per_channel_ *
712 frame->num_channels_;
713 msg->set_data(frame->data_, data_size);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000714 RETURN_ON_ERR(WriteMessageToDebugFile());
niklase@google.com470e71d2011-07-07 08:21:25 +0000715 }
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000716#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000717
718 render_audio_->DeinterleaveFrom(frame);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000719 return AnalyzeReverseStreamLocked();
720}
niklase@google.com470e71d2011-07-07 08:21:25 +0000721
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000722int AudioProcessingImpl::AnalyzeReverseStreamLocked() {
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000723 AudioBuffer* ra = render_audio_.get(); // For brevity.
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000724 if (rev_proc_format_.rate() == kSampleRate32kHz) {
aluebs@webrtc.orgbe05c742014-11-14 22:18:10 +0000725 ra->SplitIntoFrequencyBands();
niklase@google.com470e71d2011-07-07 08:21:25 +0000726 }
727
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000728 RETURN_ON_ERR(echo_cancellation_->ProcessRenderAudio(ra));
729 RETURN_ON_ERR(echo_control_mobile_->ProcessRenderAudio(ra));
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000730 if (!use_new_agc_) {
731 RETURN_ON_ERR(gain_control_->ProcessRenderAudio(ra));
732 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000733
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000734 return kNoError;
niklase@google.com470e71d2011-07-07 08:21:25 +0000735}
736
737int AudioProcessingImpl::set_stream_delay_ms(int delay) {
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000738 Error retval = kNoError;
niklase@google.com470e71d2011-07-07 08:21:25 +0000739 was_stream_delay_set_ = true;
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000740 delay += delay_offset_ms_;
741
niklase@google.com470e71d2011-07-07 08:21:25 +0000742 if (delay < 0) {
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000743 delay = 0;
744 retval = kBadStreamParameterWarning;
niklase@google.com470e71d2011-07-07 08:21:25 +0000745 }
746
747 // TODO(ajm): the max is rather arbitrarily chosen; investigate.
748 if (delay > 500) {
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000749 delay = 500;
750 retval = kBadStreamParameterWarning;
niklase@google.com470e71d2011-07-07 08:21:25 +0000751 }
752
753 stream_delay_ms_ = delay;
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000754 return retval;
niklase@google.com470e71d2011-07-07 08:21:25 +0000755}
756
757int AudioProcessingImpl::stream_delay_ms() const {
758 return stream_delay_ms_;
759}
760
761bool AudioProcessingImpl::was_stream_delay_set() const {
762 return was_stream_delay_set_;
763}
764
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000765void AudioProcessingImpl::set_stream_key_pressed(bool key_pressed) {
766 key_pressed_ = key_pressed;
767}
768
769bool AudioProcessingImpl::stream_key_pressed() const {
770 return key_pressed_;
771}
772
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000773void AudioProcessingImpl::set_delay_offset_ms(int offset) {
774 CriticalSectionScoped crit_scoped(crit_);
775 delay_offset_ms_ = offset;
776}
777
778int AudioProcessingImpl::delay_offset_ms() const {
779 return delay_offset_ms_;
780}
781
niklase@google.com470e71d2011-07-07 08:21:25 +0000782int AudioProcessingImpl::StartDebugRecording(
783 const char filename[AudioProcessing::kMaxFilenameSize]) {
andrew@webrtc.org40654032012-01-30 20:51:15 +0000784 CriticalSectionScoped crit_scoped(crit_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000785 assert(kMaxFilenameSize == FileWrapper::kMaxFileNameSize);
786
787 if (filename == NULL) {
788 return kNullPointerError;
789 }
790
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000791#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
niklase@google.com470e71d2011-07-07 08:21:25 +0000792 // Stop any ongoing recording.
793 if (debug_file_->Open()) {
794 if (debug_file_->CloseFile() == -1) {
795 return kFileError;
796 }
797 }
798
799 if (debug_file_->OpenFile(filename, false) == -1) {
800 debug_file_->CloseFile();
801 return kFileError;
802 }
803
ajm@google.com808e0e02011-08-03 21:08:51 +0000804 int err = WriteInitMessage();
805 if (err != kNoError) {
806 return err;
niklase@google.com470e71d2011-07-07 08:21:25 +0000807 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000808 return kNoError;
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000809#else
810 return kUnsupportedFunctionError;
811#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
niklase@google.com470e71d2011-07-07 08:21:25 +0000812}
813
henrikg@webrtc.org863b5362013-12-06 16:05:17 +0000814int AudioProcessingImpl::StartDebugRecording(FILE* handle) {
815 CriticalSectionScoped crit_scoped(crit_);
816
817 if (handle == NULL) {
818 return kNullPointerError;
819 }
820
821#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
822 // Stop any ongoing recording.
823 if (debug_file_->Open()) {
824 if (debug_file_->CloseFile() == -1) {
825 return kFileError;
826 }
827 }
828
829 if (debug_file_->OpenFromFileHandle(handle, true, false) == -1) {
830 return kFileError;
831 }
832
833 int err = WriteInitMessage();
834 if (err != kNoError) {
835 return err;
836 }
837 return kNoError;
838#else
839 return kUnsupportedFunctionError;
840#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
841}
842
xians@webrtc.orge46bc772014-10-10 08:36:56 +0000843int AudioProcessingImpl::StartDebugRecordingForPlatformFile(
844 rtc::PlatformFile handle) {
845 FILE* stream = rtc::FdopenPlatformFileForWriting(handle);
846 return StartDebugRecording(stream);
847}
848
niklase@google.com470e71d2011-07-07 08:21:25 +0000849int AudioProcessingImpl::StopDebugRecording() {
andrew@webrtc.org40654032012-01-30 20:51:15 +0000850 CriticalSectionScoped crit_scoped(crit_);
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000851
852#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
niklase@google.com470e71d2011-07-07 08:21:25 +0000853 // We just return if recording hasn't started.
854 if (debug_file_->Open()) {
855 if (debug_file_->CloseFile() == -1) {
856 return kFileError;
857 }
858 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000859 return kNoError;
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000860#else
861 return kUnsupportedFunctionError;
862#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
niklase@google.com470e71d2011-07-07 08:21:25 +0000863}
864
865EchoCancellation* AudioProcessingImpl::echo_cancellation() const {
866 return echo_cancellation_;
867}
868
869EchoControlMobile* AudioProcessingImpl::echo_control_mobile() const {
870 return echo_control_mobile_;
871}
872
873GainControl* AudioProcessingImpl::gain_control() const {
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000874 if (use_new_agc_) {
875 return gain_control_for_new_agc_.get();
876 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000877 return gain_control_;
878}
879
880HighPassFilter* AudioProcessingImpl::high_pass_filter() const {
881 return high_pass_filter_;
882}
883
884LevelEstimator* AudioProcessingImpl::level_estimator() const {
885 return level_estimator_;
886}
887
888NoiseSuppression* AudioProcessingImpl::noise_suppression() const {
889 return noise_suppression_;
890}
891
892VoiceDetection* AudioProcessingImpl::voice_detection() const {
893 return voice_detection_;
894}
895
andrew@webrtc.org369166a2012-04-24 18:38:03 +0000896bool AudioProcessingImpl::is_data_processed() const {
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000897 int enabled_count = 0;
898 std::list<ProcessingComponent*>::const_iterator it;
899 for (it = component_list_.begin(); it != component_list_.end(); it++) {
900 if ((*it)->is_component_enabled()) {
901 enabled_count++;
902 }
903 }
904
905 // Data is unchanged if no components are enabled, or if only level_estimator_
906 // or voice_detection_ is enabled.
907 if (enabled_count == 0) {
908 return false;
909 } else if (enabled_count == 1) {
910 if (level_estimator_->is_enabled() || voice_detection_->is_enabled()) {
911 return false;
912 }
913 } else if (enabled_count == 2) {
914 if (level_estimator_->is_enabled() && voice_detection_->is_enabled()) {
915 return false;
916 }
917 }
918 return true;
919}
920
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000921bool AudioProcessingImpl::output_copy_needed(bool is_data_processed) const {
andrew@webrtc.org369166a2012-04-24 18:38:03 +0000922 // Check if we've upmixed or downmixed the audio.
aluebs@webrtc.org27d106b2014-12-11 17:09:21 +0000923 return ((fwd_out_format_.num_channels() != fwd_in_format_.num_channels()) ||
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000924 is_data_processed || transient_suppressor_enabled_);
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000925}
926
andrew@webrtc.org369166a2012-04-24 18:38:03 +0000927bool AudioProcessingImpl::synthesis_needed(bool is_data_processed) const {
aluebs@webrtc.org087da132014-11-17 23:01:23 +0000928 return (is_data_processed && (fwd_proc_format_.rate() == kSampleRate32kHz ||
929 fwd_proc_format_.rate() == kSampleRate48kHz));
andrew@webrtc.org369166a2012-04-24 18:38:03 +0000930}
931
932bool AudioProcessingImpl::analysis_needed(bool is_data_processed) const {
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000933 if (!is_data_processed && !voice_detection_->is_enabled() &&
934 !transient_suppressor_enabled_) {
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000935 // Only level_estimator_ is enabled.
936 return false;
aluebs@webrtc.org087da132014-11-17 23:01:23 +0000937 } else if (fwd_proc_format_.rate() == kSampleRate32kHz ||
938 fwd_proc_format_.rate() == kSampleRate48kHz) {
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000939 // Something besides level_estimator_ is enabled, and we have super-wb.
940 return true;
941 }
942 return false;
943}
944
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000945int AudioProcessingImpl::InitializeExperimentalAgc() {
946 if (use_new_agc_) {
947 if (!agc_manager_.get()) {
948 agc_manager_.reset(
949 new AgcManagerDirect(gain_control_, gain_control_for_new_agc_.get()));
950 }
951 agc_manager_->Initialize();
952 agc_manager_->SetCaptureMuted(output_will_be_muted_);
953 }
954 return kNoError;
955}
956
957int AudioProcessingImpl::InitializeTransient() {
958 if (transient_suppressor_enabled_) {
959 if (!transient_suppressor_.get()) {
960 transient_suppressor_.reset(new TransientSuppressor());
961 }
962 transient_suppressor_->Initialize(fwd_proc_format_.rate(),
963 split_rate_,
964 fwd_out_format_.num_channels());
965 }
966 return kNoError;
967}
968
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000969#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
ajm@google.com808e0e02011-08-03 21:08:51 +0000970int AudioProcessingImpl::WriteMessageToDebugFile() {
971 int32_t size = event_msg_->ByteSize();
972 if (size <= 0) {
973 return kUnspecifiedError;
974 }
andrew@webrtc.org621df672013-10-22 10:27:23 +0000975#if defined(WEBRTC_ARCH_BIG_ENDIAN)
ajm@google.com808e0e02011-08-03 21:08:51 +0000976 // TODO(ajm): Use little-endian "on the wire". For the moment, we can be
977 // pretty safe in assuming little-endian.
978#endif
979
980 if (!event_msg_->SerializeToString(&event_str_)) {
981 return kUnspecifiedError;
982 }
983
984 // Write message preceded by its size.
985 if (!debug_file_->Write(&size, sizeof(int32_t))) {
986 return kFileError;
987 }
988 if (!debug_file_->Write(event_str_.data(), event_str_.length())) {
989 return kFileError;
990 }
991
992 event_msg_->Clear();
993
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000994 return kNoError;
ajm@google.com808e0e02011-08-03 21:08:51 +0000995}
996
997int AudioProcessingImpl::WriteInitMessage() {
998 event_msg_->set_type(audioproc::Event::INIT);
999 audioproc::Init* msg = event_msg_->mutable_init();
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001000 msg->set_sample_rate(fwd_in_format_.rate());
1001 msg->set_num_input_channels(fwd_in_format_.num_channels());
aluebs@webrtc.org27d106b2014-12-11 17:09:21 +00001002 msg->set_num_output_channels(fwd_out_format_.num_channels());
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001003 msg->set_num_reverse_channels(rev_in_format_.num_channels());
1004 msg->set_reverse_sample_rate(rev_in_format_.rate());
1005 msg->set_output_sample_rate(fwd_out_format_.rate());
ajm@google.com808e0e02011-08-03 21:08:51 +00001006
1007 int err = WriteMessageToDebugFile();
1008 if (err != kNoError) {
1009 return err;
1010 }
1011
1012 return kNoError;
1013}
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001014#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001015
niklase@google.com470e71d2011-07-07 08:21:25 +00001016} // namespace webrtc