blob: 5029bc712f18b9668fbe184ad8d81189412afdce [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"
andrew@webrtc.org78693fe2013-03-01 16:36:19 +000019#include "webrtc/modules/audio_processing/audio_buffer.h"
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +000020#include "webrtc/modules/audio_processing/beamformer/beamformer.h"
kjellander@webrtc.org035e9122015-01-28 19:57:00 +000021#include "webrtc/common_audio/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"
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +000030#include "webrtc/modules/audio_processing/transient/transient_suppressor.h"
andrew@webrtc.org78693fe2013-03-01 16:36:19 +000031#include "webrtc/modules/audio_processing/voice_detection_impl.h"
32#include "webrtc/modules/interface/module_common_types.h"
33#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.
kwiberg@webrtc.org2ebfac52015-01-14 10:51:54 +000057static_assert(AudioProcessing::kNoError == 0, "kNoError must be zero");
andrew@webrtc.org60730cf2014-01-07 17:45:09 +000058
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
andrew@webrtc.orge84978f2014-01-25 02:09:06 +0000138AudioProcessing* AudioProcessing::Create() {
139 Config config;
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000140 return Create(config, nullptr);
andrew@webrtc.orge84978f2014-01-25 02:09:06 +0000141}
142
143AudioProcessing* AudioProcessing::Create(const Config& config) {
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000144 return Create(config, nullptr);
145}
146
147AudioProcessing* AudioProcessing::Create(const Config& config,
148 Beamformer* beamformer) {
149 AudioProcessingImpl* apm = new AudioProcessingImpl(config, beamformer);
niklase@google.com470e71d2011-07-07 08:21:25 +0000150 if (apm->Initialize() != kNoError) {
151 delete apm;
152 apm = NULL;
153 }
154
155 return apm;
156}
157
andrew@webrtc.orge84978f2014-01-25 02:09:06 +0000158AudioProcessingImpl::AudioProcessingImpl(const Config& config)
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000159 : AudioProcessingImpl(config, nullptr) {}
160
161AudioProcessingImpl::AudioProcessingImpl(const Config& config,
162 Beamformer* beamformer)
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000163 : echo_cancellation_(NULL),
niklase@google.com470e71d2011-07-07 08:21:25 +0000164 echo_control_mobile_(NULL),
165 gain_control_(NULL),
166 high_pass_filter_(NULL),
167 level_estimator_(NULL),
168 noise_suppression_(NULL),
169 voice_detection_(NULL),
niklase@google.com470e71d2011-07-07 08:21:25 +0000170 crit_(CriticalSectionWrapper::CreateCriticalSection()),
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000171#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
172 debug_file_(FileWrapper::Create()),
173 event_msg_(new audioproc::Event()),
174#endif
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000175 fwd_in_format_(kSampleRate16kHz, 1),
aluebs@webrtc.org27d106b2014-12-11 17:09:21 +0000176 fwd_proc_format_(kSampleRate16kHz),
177 fwd_out_format_(kSampleRate16kHz, 1),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000178 rev_in_format_(kSampleRate16kHz, 1),
179 rev_proc_format_(kSampleRate16kHz, 1),
180 split_rate_(kSampleRate16kHz),
niklase@google.com470e71d2011-07-07 08:21:25 +0000181 stream_delay_ms_(0),
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000182 delay_offset_ms_(0),
niklase@google.com470e71d2011-07-07 08:21:25 +0000183 was_stream_delay_set_(false),
andrew@webrtc.org38bf2492014-02-13 17:43:44 +0000184 output_will_be_muted_(false),
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000185 key_pressed_(false),
186#if defined(WEBRTC_ANDROID) || defined(WEBRTC_IOS)
187 use_new_agc_(false),
188#else
189 use_new_agc_(config.Get<ExperimentalAgc>().enabled),
190#endif
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +0000191 transient_suppressor_enabled_(config.Get<ExperimentalNs>().enabled),
aluebs@webrtc.orgfb7a0392015-01-05 21:58:58 +0000192 beamformer_enabled_(config.Get<Beamforming>().enabled),
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000193 beamformer_(beamformer),
aluebs@webrtc.orgc9ce07e2015-03-02 20:07:31 +0000194 array_geometry_(config.Get<Beamforming>().array_geometry),
195 supports_48kHz_(config.Get<AudioProcessing48kHzSupport>().enabled) {
andrew@webrtc.org56e4a052014-02-27 22:23:17 +0000196 echo_cancellation_ = new EchoCancellationImpl(this, crit_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000197 component_list_.push_back(echo_cancellation_);
198
andrew@webrtc.org56e4a052014-02-27 22:23:17 +0000199 echo_control_mobile_ = new EchoControlMobileImpl(this, crit_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000200 component_list_.push_back(echo_control_mobile_);
201
andrew@webrtc.org56e4a052014-02-27 22:23:17 +0000202 gain_control_ = new GainControlImpl(this, crit_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000203 component_list_.push_back(gain_control_);
204
andrew@webrtc.org56e4a052014-02-27 22:23:17 +0000205 high_pass_filter_ = new HighPassFilterImpl(this, crit_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000206 component_list_.push_back(high_pass_filter_);
207
andrew@webrtc.org56e4a052014-02-27 22:23:17 +0000208 level_estimator_ = new LevelEstimatorImpl(this, crit_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000209 component_list_.push_back(level_estimator_);
210
andrew@webrtc.org56e4a052014-02-27 22:23:17 +0000211 noise_suppression_ = new NoiseSuppressionImpl(this, crit_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000212 component_list_.push_back(noise_suppression_);
213
andrew@webrtc.org56e4a052014-02-27 22:23:17 +0000214 voice_detection_ = new VoiceDetectionImpl(this, crit_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000215 component_list_.push_back(voice_detection_);
andrew@webrtc.orge84978f2014-01-25 02:09:06 +0000216
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000217 gain_control_for_new_agc_.reset(new GainControlForNewAgc(gain_control_));
218
andrew@webrtc.orge84978f2014-01-25 02:09:06 +0000219 SetExtraOptions(config);
niklase@google.com470e71d2011-07-07 08:21:25 +0000220}
221
222AudioProcessingImpl::~AudioProcessingImpl() {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000223 {
224 CriticalSectionScoped crit_scoped(crit_);
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000225 // Depends on gain_control_ and gain_control_for_new_agc_.
226 agc_manager_.reset();
227 // Depends on gain_control_.
228 gain_control_for_new_agc_.reset();
andrew@webrtc.org81865342012-10-27 00:28:27 +0000229 while (!component_list_.empty()) {
230 ProcessingComponent* component = component_list_.front();
231 component->Destroy();
232 delete component;
233 component_list_.pop_front();
234 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000235
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000236#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
andrew@webrtc.org81865342012-10-27 00:28:27 +0000237 if (debug_file_->Open()) {
238 debug_file_->CloseFile();
239 }
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000240#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000241 }
andrew@webrtc.org16cfbe22012-08-29 16:58:25 +0000242 delete crit_;
243 crit_ = NULL;
niklase@google.com470e71d2011-07-07 08:21:25 +0000244}
245
niklase@google.com470e71d2011-07-07 08:21:25 +0000246int AudioProcessingImpl::Initialize() {
andrew@webrtc.org40654032012-01-30 20:51:15 +0000247 CriticalSectionScoped crit_scoped(crit_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000248 return InitializeLocked();
249}
250
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000251int AudioProcessingImpl::set_sample_rate_hz(int rate) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000252 CriticalSectionScoped crit_scoped(crit_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000253 return InitializeLocked(rate,
254 rate,
255 rev_in_format_.rate(),
256 fwd_in_format_.num_channels(),
aluebs@webrtc.org27d106b2014-12-11 17:09:21 +0000257 fwd_out_format_.num_channels(),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000258 rev_in_format_.num_channels());
259}
260
261int AudioProcessingImpl::Initialize(int input_sample_rate_hz,
262 int output_sample_rate_hz,
263 int reverse_sample_rate_hz,
264 ChannelLayout input_layout,
265 ChannelLayout output_layout,
266 ChannelLayout reverse_layout) {
267 CriticalSectionScoped crit_scoped(crit_);
268 return InitializeLocked(input_sample_rate_hz,
269 output_sample_rate_hz,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000270 reverse_sample_rate_hz,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000271 ChannelsFromLayout(input_layout),
272 ChannelsFromLayout(output_layout),
273 ChannelsFromLayout(reverse_layout));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000274}
275
niklase@google.com470e71d2011-07-07 08:21:25 +0000276int AudioProcessingImpl::InitializeLocked() {
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +0000277 const int fwd_audio_buffer_channels = beamformer_enabled_ ?
278 fwd_in_format_.num_channels() :
279 fwd_out_format_.num_channels();
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000280 render_audio_.reset(new AudioBuffer(rev_in_format_.samples_per_channel(),
281 rev_in_format_.num_channels(),
282 rev_proc_format_.samples_per_channel(),
283 rev_proc_format_.num_channels(),
284 rev_proc_format_.samples_per_channel()));
285 capture_audio_.reset(new AudioBuffer(fwd_in_format_.samples_per_channel(),
286 fwd_in_format_.num_channels(),
287 fwd_proc_format_.samples_per_channel(),
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +0000288 fwd_audio_buffer_channels,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000289 fwd_out_format_.samples_per_channel()));
niklase@google.com470e71d2011-07-07 08:21:25 +0000290
niklase@google.com470e71d2011-07-07 08:21:25 +0000291 // Initialize all components.
292 std::list<ProcessingComponent*>::iterator it;
andrew@webrtc.org81865342012-10-27 00:28:27 +0000293 for (it = component_list_.begin(); it != component_list_.end(); ++it) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000294 int err = (*it)->Initialize();
295 if (err != kNoError) {
296 return err;
297 }
298 }
299
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000300 int err = InitializeExperimentalAgc();
301 if (err != kNoError) {
302 return err;
303 }
304
305 err = InitializeTransient();
306 if (err != kNoError) {
307 return err;
308 }
309
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +0000310 InitializeBeamformer();
311
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000312#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
ajm@google.com808e0e02011-08-03 21:08:51 +0000313 if (debug_file_->Open()) {
314 int err = WriteInitMessage();
315 if (err != kNoError) {
316 return err;
317 }
318 }
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000319#endif
ajm@google.com808e0e02011-08-03 21:08:51 +0000320
niklase@google.com470e71d2011-07-07 08:21:25 +0000321 return kNoError;
322}
323
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000324int AudioProcessingImpl::InitializeLocked(int input_sample_rate_hz,
325 int output_sample_rate_hz,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000326 int reverse_sample_rate_hz,
327 int num_input_channels,
328 int num_output_channels,
329 int num_reverse_channels) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000330 if (input_sample_rate_hz <= 0 ||
331 output_sample_rate_hz <= 0 ||
332 reverse_sample_rate_hz <= 0) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000333 return kBadSampleRateError;
334 }
335 if (num_output_channels > num_input_channels) {
336 return kBadNumberChannelsError;
337 }
338 // Only mono and stereo supported currently.
339 if (num_input_channels > 2 || num_input_channels < 1 ||
340 num_output_channels > 2 || num_output_channels < 1 ||
341 num_reverse_channels > 2 || num_reverse_channels < 1) {
342 return kBadNumberChannelsError;
343 }
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000344 if (beamformer_enabled_ &&
345 (static_cast<size_t>(num_input_channels) != array_geometry_.size() ||
346 num_output_channels > 1)) {
347 return kBadNumberChannelsError;
348 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000349
350 fwd_in_format_.set(input_sample_rate_hz, num_input_channels);
aluebs@webrtc.org27d106b2014-12-11 17:09:21 +0000351 fwd_out_format_.set(output_sample_rate_hz, num_output_channels);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000352 rev_in_format_.set(reverse_sample_rate_hz, num_reverse_channels);
353
354 // We process at the closest native rate >= min(input rate, output rate)...
355 int min_proc_rate = std::min(fwd_in_format_.rate(), fwd_out_format_.rate());
356 int fwd_proc_rate;
aluebs@webrtc.orgc9ce07e2015-03-02 20:07:31 +0000357 if (supports_48kHz_ && min_proc_rate > kSampleRate32kHz) {
358 fwd_proc_rate = kSampleRate48kHz;
359 } else if (min_proc_rate > kSampleRate16kHz) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000360 fwd_proc_rate = kSampleRate32kHz;
361 } else if (min_proc_rate > kSampleRate8kHz) {
362 fwd_proc_rate = kSampleRate16kHz;
363 } else {
364 fwd_proc_rate = kSampleRate8kHz;
365 }
366 // ...with one exception.
367 if (echo_control_mobile_->is_enabled() && min_proc_rate > kSampleRate16kHz) {
368 fwd_proc_rate = kSampleRate16kHz;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000369 }
370
aluebs@webrtc.org27d106b2014-12-11 17:09:21 +0000371 fwd_proc_format_.set(fwd_proc_rate);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000372
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000373 // We normally process the reverse stream at 16 kHz. Unless...
374 int rev_proc_rate = kSampleRate16kHz;
375 if (fwd_proc_format_.rate() == kSampleRate8kHz) {
376 // ...the forward stream is at 8 kHz.
377 rev_proc_rate = kSampleRate8kHz;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000378 } else {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000379 if (rev_in_format_.rate() == kSampleRate32kHz) {
380 // ...or the input is at 32 kHz, in which case we use the splitting
381 // filter rather than the resampler.
382 rev_proc_rate = kSampleRate32kHz;
383 }
384 }
385
andrew@webrtc.org30be8272014-09-24 20:06:23 +0000386 // Always downmix the reverse stream to mono for analysis. This has been
387 // demonstrated to work well for AEC in most practical scenarios.
388 rev_proc_format_.set(rev_proc_rate, 1);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000389
aluebs@webrtc.org087da132014-11-17 23:01:23 +0000390 if (fwd_proc_format_.rate() == kSampleRate32kHz ||
391 fwd_proc_format_.rate() == kSampleRate48kHz) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000392 split_rate_ = kSampleRate16kHz;
393 } else {
394 split_rate_ = fwd_proc_format_.rate();
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000395 }
396
397 return InitializeLocked();
398}
399
400// Calls InitializeLocked() if any of the audio parameters have changed from
401// their current values.
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000402int AudioProcessingImpl::MaybeInitializeLocked(int input_sample_rate_hz,
403 int output_sample_rate_hz,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000404 int reverse_sample_rate_hz,
405 int num_input_channels,
406 int num_output_channels,
407 int num_reverse_channels) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000408 if (input_sample_rate_hz == fwd_in_format_.rate() &&
409 output_sample_rate_hz == fwd_out_format_.rate() &&
410 reverse_sample_rate_hz == rev_in_format_.rate() &&
411 num_input_channels == fwd_in_format_.num_channels() &&
aluebs@webrtc.org27d106b2014-12-11 17:09:21 +0000412 num_output_channels == fwd_out_format_.num_channels() &&
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000413 num_reverse_channels == rev_in_format_.num_channels()) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000414 return kNoError;
415 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000416 return InitializeLocked(input_sample_rate_hz,
417 output_sample_rate_hz,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000418 reverse_sample_rate_hz,
419 num_input_channels,
420 num_output_channels,
421 num_reverse_channels);
422}
423
andrew@webrtc.org61e596f2013-07-25 18:28:29 +0000424void AudioProcessingImpl::SetExtraOptions(const Config& config) {
andrew@webrtc.orge84978f2014-01-25 02:09:06 +0000425 CriticalSectionScoped crit_scoped(crit_);
andrew@webrtc.org61e596f2013-07-25 18:28:29 +0000426 std::list<ProcessingComponent*>::iterator it;
427 for (it = component_list_.begin(); it != component_list_.end(); ++it)
428 (*it)->SetExtraOptions(config);
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000429
430 if (transient_suppressor_enabled_ != config.Get<ExperimentalNs>().enabled) {
431 transient_suppressor_enabled_ = config.Get<ExperimentalNs>().enabled;
432 InitializeTransient();
433 }
andrew@webrtc.org61e596f2013-07-25 18:28:29 +0000434}
435
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000436int AudioProcessingImpl::input_sample_rate_hz() const {
andrew@webrtc.org40654032012-01-30 20:51:15 +0000437 CriticalSectionScoped crit_scoped(crit_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000438 return fwd_in_format_.rate();
niklase@google.com470e71d2011-07-07 08:21:25 +0000439}
440
andrew@webrtc.org46b31b12014-04-23 03:33:54 +0000441int AudioProcessingImpl::sample_rate_hz() const {
442 CriticalSectionScoped crit_scoped(crit_);
443 return fwd_in_format_.rate();
444}
445
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000446int AudioProcessingImpl::proc_sample_rate_hz() const {
447 return fwd_proc_format_.rate();
niklase@google.com470e71d2011-07-07 08:21:25 +0000448}
449
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000450int AudioProcessingImpl::proc_split_sample_rate_hz() const {
451 return split_rate_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000452}
453
454int AudioProcessingImpl::num_reverse_channels() const {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000455 return rev_proc_format_.num_channels();
niklase@google.com470e71d2011-07-07 08:21:25 +0000456}
457
458int AudioProcessingImpl::num_input_channels() const {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000459 return fwd_in_format_.num_channels();
niklase@google.com470e71d2011-07-07 08:21:25 +0000460}
461
462int AudioProcessingImpl::num_output_channels() const {
aluebs@webrtc.org27d106b2014-12-11 17:09:21 +0000463 return fwd_out_format_.num_channels();
niklase@google.com470e71d2011-07-07 08:21:25 +0000464}
465
andrew@webrtc.org17342e52014-02-12 22:28:31 +0000466void AudioProcessingImpl::set_output_will_be_muted(bool muted) {
467 output_will_be_muted_ = muted;
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000468 CriticalSectionScoped lock(crit_);
469 if (agc_manager_.get()) {
470 agc_manager_->SetCaptureMuted(output_will_be_muted_);
471 }
andrew@webrtc.org17342e52014-02-12 22:28:31 +0000472}
473
474bool AudioProcessingImpl::output_will_be_muted() const {
475 return output_will_be_muted_;
476}
477
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000478int AudioProcessingImpl::ProcessStream(const float* const* src,
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000479 int samples_per_channel,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000480 int input_sample_rate_hz,
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000481 ChannelLayout input_layout,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000482 int output_sample_rate_hz,
483 ChannelLayout output_layout,
484 float* const* dest) {
andrew@webrtc.org40654032012-01-30 20:51:15 +0000485 CriticalSectionScoped crit_scoped(crit_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000486 if (!src || !dest) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000487 return kNullPointerError;
488 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000489
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000490 RETURN_ON_ERR(MaybeInitializeLocked(input_sample_rate_hz,
491 output_sample_rate_hz,
492 rev_in_format_.rate(),
493 ChannelsFromLayout(input_layout),
494 ChannelsFromLayout(output_layout),
495 rev_in_format_.num_channels()));
496 if (samples_per_channel != fwd_in_format_.samples_per_channel()) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000497 return kBadDataLengthError;
498 }
499
500#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
501 if (debug_file_->Open()) {
502 event_msg_->set_type(audioproc::Event::STREAM);
503 audioproc::Stream* msg = event_msg_->mutable_stream();
aluebs@webrtc.org59a1b1b2014-08-28 10:43:09 +0000504 const size_t channel_size =
505 sizeof(float) * fwd_in_format_.samples_per_channel();
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000506 for (int i = 0; i < fwd_in_format_.num_channels(); ++i)
507 msg->add_input_channel(src[i], channel_size);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000508 }
509#endif
510
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000511 capture_audio_->CopyFrom(src, samples_per_channel, input_layout);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000512 RETURN_ON_ERR(ProcessStreamLocked());
mgraczyk@chromium.orgd6e84d92015-01-14 01:33:54 +0000513 capture_audio_->CopyTo(fwd_out_format_.samples_per_channel(),
514 output_layout,
515 dest);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000516
517#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
518 if (debug_file_->Open()) {
519 audioproc::Stream* msg = event_msg_->mutable_stream();
aluebs@webrtc.org59a1b1b2014-08-28 10:43:09 +0000520 const size_t channel_size =
521 sizeof(float) * fwd_out_format_.samples_per_channel();
aluebs@webrtc.org27d106b2014-12-11 17:09:21 +0000522 for (int i = 0; i < fwd_out_format_.num_channels(); ++i)
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000523 msg->add_output_channel(dest[i], channel_size);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000524 RETURN_ON_ERR(WriteMessageToDebugFile());
525 }
526#endif
527
528 return kNoError;
529}
530
531int AudioProcessingImpl::ProcessStream(AudioFrame* frame) {
532 CriticalSectionScoped crit_scoped(crit_);
533 if (!frame) {
534 return kNullPointerError;
535 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000536 // Must be a native rate.
537 if (frame->sample_rate_hz_ != kSampleRate8kHz &&
538 frame->sample_rate_hz_ != kSampleRate16kHz &&
aluebs@webrtc.org087da132014-11-17 23:01:23 +0000539 frame->sample_rate_hz_ != kSampleRate32kHz &&
540 frame->sample_rate_hz_ != kSampleRate48kHz) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000541 return kBadSampleRateError;
542 }
543 if (echo_control_mobile_->is_enabled() &&
544 frame->sample_rate_hz_ > kSampleRate16kHz) {
545 LOG(LS_ERROR) << "AECM only supports 16 or 8 kHz sample rates";
546 return kUnsupportedComponentError;
547 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000548
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000549 // TODO(ajm): The input and output rates and channels are currently
550 // constrained to be identical in the int16 interface.
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000551 RETURN_ON_ERR(MaybeInitializeLocked(frame->sample_rate_hz_,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000552 frame->sample_rate_hz_,
553 rev_in_format_.rate(),
554 frame->num_channels_,
555 frame->num_channels_,
556 rev_in_format_.num_channels()));
557 if (frame->samples_per_channel_ != fwd_in_format_.samples_per_channel()) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000558 return kBadDataLengthError;
559 }
560
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000561#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
niklase@google.com470e71d2011-07-07 08:21:25 +0000562 if (debug_file_->Open()) {
ajm@google.com808e0e02011-08-03 21:08:51 +0000563 event_msg_->set_type(audioproc::Event::STREAM);
564 audioproc::Stream* msg = event_msg_->mutable_stream();
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000565 const size_t data_size = sizeof(int16_t) *
andrew@webrtc.org63a50982012-05-02 23:56:37 +0000566 frame->samples_per_channel_ *
567 frame->num_channels_;
568 msg->set_input_data(frame->data_, data_size);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000569 }
570#endif
571
572 capture_audio_->DeinterleaveFrom(frame);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000573 RETURN_ON_ERR(ProcessStreamLocked());
574 capture_audio_->InterleaveTo(frame, output_copy_needed(is_data_processed()));
575
576#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
577 if (debug_file_->Open()) {
578 audioproc::Stream* msg = event_msg_->mutable_stream();
579 const size_t data_size = sizeof(int16_t) *
580 frame->samples_per_channel_ *
581 frame->num_channels_;
582 msg->set_output_data(frame->data_, data_size);
583 RETURN_ON_ERR(WriteMessageToDebugFile());
584 }
585#endif
586
587 return kNoError;
588}
589
590
591int AudioProcessingImpl::ProcessStreamLocked() {
592#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
593 if (debug_file_->Open()) {
594 audioproc::Stream* msg = event_msg_->mutable_stream();
ajm@google.com808e0e02011-08-03 21:08:51 +0000595 msg->set_delay(stream_delay_ms_);
596 msg->set_drift(echo_cancellation_->stream_drift_samples());
bjornv@webrtc.org63da1dd2015-02-06 19:44:21 +0000597 msg->set_level(gain_control()->stream_analog_level());
andrew@webrtc.orgce8e0772014-02-12 15:28:30 +0000598 msg->set_keypress(key_pressed_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000599 }
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000600#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000601
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000602 AudioBuffer* ca = capture_audio_.get(); // For brevity.
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000603 if (use_new_agc_ && gain_control_->is_enabled()) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000604 agc_manager_->AnalyzePreProcess(ca->channels()[0],
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000605 ca->num_channels(),
606 fwd_proc_format_.samples_per_channel());
607 }
608
andrew@webrtc.org369166a2012-04-24 18:38:03 +0000609 bool data_processed = is_data_processed();
610 if (analysis_needed(data_processed)) {
aluebs@webrtc.orgbe05c742014-11-14 22:18:10 +0000611 ca->SplitIntoFrequencyBands();
niklase@google.com470e71d2011-07-07 08:21:25 +0000612 }
613
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +0000614#ifdef WEBRTC_BEAMFORMER
615 if (beamformer_enabled_) {
aluebs@webrtc.org3aca0b02015-02-26 21:52:20 +0000616 beamformer_->ProcessChunk(ca->split_data_f(), ca->split_data_f());
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +0000617 ca->set_num_channels(1);
618 }
619#endif
620
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000621 RETURN_ON_ERR(high_pass_filter_->ProcessCaptureAudio(ca));
622 RETURN_ON_ERR(gain_control_->AnalyzeCaptureAudio(ca));
aluebs@webrtc.orga0ce9fa2014-09-24 14:18:03 +0000623 RETURN_ON_ERR(noise_suppression_->AnalyzeCaptureAudio(ca));
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000624 RETURN_ON_ERR(echo_cancellation_->ProcessCaptureAudio(ca));
niklase@google.com470e71d2011-07-07 08:21:25 +0000625
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000626 if (echo_control_mobile_->is_enabled() && noise_suppression_->is_enabled()) {
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000627 ca->CopyLowPassToReference();
niklase@google.com470e71d2011-07-07 08:21:25 +0000628 }
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000629 RETURN_ON_ERR(noise_suppression_->ProcessCaptureAudio(ca));
630 RETURN_ON_ERR(echo_control_mobile_->ProcessCaptureAudio(ca));
631 RETURN_ON_ERR(voice_detection_->ProcessCaptureAudio(ca));
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000632
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000633 if (use_new_agc_ &&
634 gain_control_->is_enabled() &&
635 (!beamformer_enabled_ || beamformer_->is_target_present())) {
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000636 agc_manager_->Process(ca->split_bands_const(0)[kBand0To8kHz],
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000637 ca->num_frames_per_band(),
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000638 split_rate_);
639 }
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000640 RETURN_ON_ERR(gain_control_->ProcessCaptureAudio(ca));
niklase@google.com470e71d2011-07-07 08:21:25 +0000641
andrew@webrtc.org369166a2012-04-24 18:38:03 +0000642 if (synthesis_needed(data_processed)) {
aluebs@webrtc.orgbe05c742014-11-14 22:18:10 +0000643 ca->MergeFrequencyBands();
niklase@google.com470e71d2011-07-07 08:21:25 +0000644 }
645
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000646 // TODO(aluebs): Investigate if the transient suppression placement should be
647 // before or after the AGC.
648 if (transient_suppressor_enabled_) {
649 float voice_probability =
650 agc_manager_.get() ? agc_manager_->voice_probability() : 1.f;
651
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000652 transient_suppressor_->Suppress(ca->channels_f()[0],
653 ca->num_frames(),
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000654 ca->num_channels(),
655 ca->split_bands_const_f(0)[kBand0To8kHz],
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000656 ca->num_frames_per_band(),
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000657 ca->keyboard_data(),
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000658 ca->num_keyboard_frames(),
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000659 voice_probability,
660 key_pressed_);
661 }
662
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000663 // The level estimator operates on the recombined data.
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000664 RETURN_ON_ERR(level_estimator_->ProcessStream(ca));
ajm@google.com808e0e02011-08-03 21:08:51 +0000665
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000666 was_stream_delay_set_ = false;
niklase@google.com470e71d2011-07-07 08:21:25 +0000667 return kNoError;
668}
669
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000670int AudioProcessingImpl::AnalyzeReverseStream(const float* const* data,
671 int samples_per_channel,
672 int sample_rate_hz,
673 ChannelLayout layout) {
674 CriticalSectionScoped crit_scoped(crit_);
675 if (data == NULL) {
676 return kNullPointerError;
677 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000678
679 const int num_channels = ChannelsFromLayout(layout);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000680 RETURN_ON_ERR(MaybeInitializeLocked(fwd_in_format_.rate(),
681 fwd_out_format_.rate(),
682 sample_rate_hz,
683 fwd_in_format_.num_channels(),
aluebs@webrtc.org27d106b2014-12-11 17:09:21 +0000684 fwd_out_format_.num_channels(),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000685 num_channels));
686 if (samples_per_channel != rev_in_format_.samples_per_channel()) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000687 return kBadDataLengthError;
688 }
689
690#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
691 if (debug_file_->Open()) {
692 event_msg_->set_type(audioproc::Event::REVERSE_STREAM);
693 audioproc::ReverseStream* msg = event_msg_->mutable_reverse_stream();
aluebs@webrtc.org59a1b1b2014-08-28 10:43:09 +0000694 const size_t channel_size =
695 sizeof(float) * rev_in_format_.samples_per_channel();
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000696 for (int i = 0; i < num_channels; ++i)
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000697 msg->add_channel(data[i], channel_size);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000698 RETURN_ON_ERR(WriteMessageToDebugFile());
699 }
700#endif
701
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000702 render_audio_->CopyFrom(data, samples_per_channel, layout);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000703 return AnalyzeReverseStreamLocked();
704}
705
niklase@google.com470e71d2011-07-07 08:21:25 +0000706int AudioProcessingImpl::AnalyzeReverseStream(AudioFrame* frame) {
andrew@webrtc.org40654032012-01-30 20:51:15 +0000707 CriticalSectionScoped crit_scoped(crit_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000708 if (frame == NULL) {
709 return kNullPointerError;
710 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000711 // Must be a native rate.
712 if (frame->sample_rate_hz_ != kSampleRate8kHz &&
713 frame->sample_rate_hz_ != kSampleRate16kHz &&
aluebs@webrtc.org087da132014-11-17 23:01:23 +0000714 frame->sample_rate_hz_ != kSampleRate32kHz &&
715 frame->sample_rate_hz_ != kSampleRate48kHz) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000716 return kBadSampleRateError;
717 }
718 // This interface does not tolerate different forward and reverse rates.
719 if (frame->sample_rate_hz_ != fwd_in_format_.rate()) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000720 return kBadSampleRateError;
721 }
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000722
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000723 RETURN_ON_ERR(MaybeInitializeLocked(fwd_in_format_.rate(),
724 fwd_out_format_.rate(),
725 frame->sample_rate_hz_,
726 fwd_in_format_.num_channels(),
727 fwd_in_format_.num_channels(),
728 frame->num_channels_));
729 if (frame->samples_per_channel_ != rev_in_format_.samples_per_channel()) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000730 return kBadDataLengthError;
731 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000732
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000733#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
niklase@google.com470e71d2011-07-07 08:21:25 +0000734 if (debug_file_->Open()) {
ajm@google.com808e0e02011-08-03 21:08:51 +0000735 event_msg_->set_type(audioproc::Event::REVERSE_STREAM);
736 audioproc::ReverseStream* msg = event_msg_->mutable_reverse_stream();
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000737 const size_t data_size = sizeof(int16_t) *
andrew@webrtc.org63a50982012-05-02 23:56:37 +0000738 frame->samples_per_channel_ *
739 frame->num_channels_;
740 msg->set_data(frame->data_, data_size);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000741 RETURN_ON_ERR(WriteMessageToDebugFile());
niklase@google.com470e71d2011-07-07 08:21:25 +0000742 }
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000743#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000744
745 render_audio_->DeinterleaveFrom(frame);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000746 return AnalyzeReverseStreamLocked();
747}
niklase@google.com470e71d2011-07-07 08:21:25 +0000748
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000749int AudioProcessingImpl::AnalyzeReverseStreamLocked() {
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000750 AudioBuffer* ra = render_audio_.get(); // For brevity.
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000751 if (rev_proc_format_.rate() == kSampleRate32kHz) {
aluebs@webrtc.orgbe05c742014-11-14 22:18:10 +0000752 ra->SplitIntoFrequencyBands();
niklase@google.com470e71d2011-07-07 08:21:25 +0000753 }
754
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000755 RETURN_ON_ERR(echo_cancellation_->ProcessRenderAudio(ra));
756 RETURN_ON_ERR(echo_control_mobile_->ProcessRenderAudio(ra));
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000757 if (!use_new_agc_) {
758 RETURN_ON_ERR(gain_control_->ProcessRenderAudio(ra));
759 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000760
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000761 return kNoError;
niklase@google.com470e71d2011-07-07 08:21:25 +0000762}
763
764int AudioProcessingImpl::set_stream_delay_ms(int delay) {
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000765 Error retval = kNoError;
niklase@google.com470e71d2011-07-07 08:21:25 +0000766 was_stream_delay_set_ = true;
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000767 delay += delay_offset_ms_;
768
niklase@google.com470e71d2011-07-07 08:21:25 +0000769 if (delay < 0) {
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000770 delay = 0;
771 retval = kBadStreamParameterWarning;
niklase@google.com470e71d2011-07-07 08:21:25 +0000772 }
773
774 // TODO(ajm): the max is rather arbitrarily chosen; investigate.
775 if (delay > 500) {
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000776 delay = 500;
777 retval = kBadStreamParameterWarning;
niklase@google.com470e71d2011-07-07 08:21:25 +0000778 }
779
780 stream_delay_ms_ = delay;
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000781 return retval;
niklase@google.com470e71d2011-07-07 08:21:25 +0000782}
783
784int AudioProcessingImpl::stream_delay_ms() const {
785 return stream_delay_ms_;
786}
787
788bool AudioProcessingImpl::was_stream_delay_set() const {
789 return was_stream_delay_set_;
790}
791
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000792void AudioProcessingImpl::set_stream_key_pressed(bool key_pressed) {
793 key_pressed_ = key_pressed;
794}
795
796bool AudioProcessingImpl::stream_key_pressed() const {
797 return key_pressed_;
798}
799
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000800void AudioProcessingImpl::set_delay_offset_ms(int offset) {
801 CriticalSectionScoped crit_scoped(crit_);
802 delay_offset_ms_ = offset;
803}
804
805int AudioProcessingImpl::delay_offset_ms() const {
806 return delay_offset_ms_;
807}
808
niklase@google.com470e71d2011-07-07 08:21:25 +0000809int AudioProcessingImpl::StartDebugRecording(
810 const char filename[AudioProcessing::kMaxFilenameSize]) {
andrew@webrtc.org40654032012-01-30 20:51:15 +0000811 CriticalSectionScoped crit_scoped(crit_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000812 assert(kMaxFilenameSize == FileWrapper::kMaxFileNameSize);
813
814 if (filename == NULL) {
815 return kNullPointerError;
816 }
817
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000818#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
niklase@google.com470e71d2011-07-07 08:21:25 +0000819 // Stop any ongoing recording.
820 if (debug_file_->Open()) {
821 if (debug_file_->CloseFile() == -1) {
822 return kFileError;
823 }
824 }
825
826 if (debug_file_->OpenFile(filename, false) == -1) {
827 debug_file_->CloseFile();
828 return kFileError;
829 }
830
ajm@google.com808e0e02011-08-03 21:08:51 +0000831 int err = WriteInitMessage();
832 if (err != kNoError) {
833 return err;
niklase@google.com470e71d2011-07-07 08:21:25 +0000834 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000835 return kNoError;
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000836#else
837 return kUnsupportedFunctionError;
838#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
niklase@google.com470e71d2011-07-07 08:21:25 +0000839}
840
henrikg@webrtc.org863b5362013-12-06 16:05:17 +0000841int AudioProcessingImpl::StartDebugRecording(FILE* handle) {
842 CriticalSectionScoped crit_scoped(crit_);
843
844 if (handle == NULL) {
845 return kNullPointerError;
846 }
847
848#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
849 // Stop any ongoing recording.
850 if (debug_file_->Open()) {
851 if (debug_file_->CloseFile() == -1) {
852 return kFileError;
853 }
854 }
855
856 if (debug_file_->OpenFromFileHandle(handle, true, false) == -1) {
857 return kFileError;
858 }
859
860 int err = WriteInitMessage();
861 if (err != kNoError) {
862 return err;
863 }
864 return kNoError;
865#else
866 return kUnsupportedFunctionError;
867#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
868}
869
xians@webrtc.orge46bc772014-10-10 08:36:56 +0000870int AudioProcessingImpl::StartDebugRecordingForPlatformFile(
871 rtc::PlatformFile handle) {
872 FILE* stream = rtc::FdopenPlatformFileForWriting(handle);
873 return StartDebugRecording(stream);
874}
875
niklase@google.com470e71d2011-07-07 08:21:25 +0000876int AudioProcessingImpl::StopDebugRecording() {
andrew@webrtc.org40654032012-01-30 20:51:15 +0000877 CriticalSectionScoped crit_scoped(crit_);
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000878
879#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
niklase@google.com470e71d2011-07-07 08:21:25 +0000880 // We just return if recording hasn't started.
881 if (debug_file_->Open()) {
882 if (debug_file_->CloseFile() == -1) {
883 return kFileError;
884 }
885 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000886 return kNoError;
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000887#else
888 return kUnsupportedFunctionError;
889#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
niklase@google.com470e71d2011-07-07 08:21:25 +0000890}
891
892EchoCancellation* AudioProcessingImpl::echo_cancellation() const {
893 return echo_cancellation_;
894}
895
896EchoControlMobile* AudioProcessingImpl::echo_control_mobile() const {
897 return echo_control_mobile_;
898}
899
900GainControl* AudioProcessingImpl::gain_control() const {
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000901 if (use_new_agc_) {
902 return gain_control_for_new_agc_.get();
903 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000904 return gain_control_;
905}
906
907HighPassFilter* AudioProcessingImpl::high_pass_filter() const {
908 return high_pass_filter_;
909}
910
911LevelEstimator* AudioProcessingImpl::level_estimator() const {
912 return level_estimator_;
913}
914
915NoiseSuppression* AudioProcessingImpl::noise_suppression() const {
916 return noise_suppression_;
917}
918
919VoiceDetection* AudioProcessingImpl::voice_detection() const {
920 return voice_detection_;
921}
922
andrew@webrtc.org369166a2012-04-24 18:38:03 +0000923bool AudioProcessingImpl::is_data_processed() const {
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +0000924 if (beamformer_enabled_) {
925 return true;
926 }
927
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000928 int enabled_count = 0;
929 std::list<ProcessingComponent*>::const_iterator it;
930 for (it = component_list_.begin(); it != component_list_.end(); it++) {
931 if ((*it)->is_component_enabled()) {
932 enabled_count++;
933 }
934 }
935
936 // Data is unchanged if no components are enabled, or if only level_estimator_
937 // or voice_detection_ is enabled.
938 if (enabled_count == 0) {
939 return false;
940 } else if (enabled_count == 1) {
941 if (level_estimator_->is_enabled() || voice_detection_->is_enabled()) {
942 return false;
943 }
944 } else if (enabled_count == 2) {
945 if (level_estimator_->is_enabled() && voice_detection_->is_enabled()) {
946 return false;
947 }
948 }
949 return true;
950}
951
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000952bool AudioProcessingImpl::output_copy_needed(bool is_data_processed) const {
andrew@webrtc.org369166a2012-04-24 18:38:03 +0000953 // Check if we've upmixed or downmixed the audio.
aluebs@webrtc.org27d106b2014-12-11 17:09:21 +0000954 return ((fwd_out_format_.num_channels() != fwd_in_format_.num_channels()) ||
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000955 is_data_processed || transient_suppressor_enabled_);
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000956}
957
andrew@webrtc.org369166a2012-04-24 18:38:03 +0000958bool AudioProcessingImpl::synthesis_needed(bool is_data_processed) const {
aluebs@webrtc.org087da132014-11-17 23:01:23 +0000959 return (is_data_processed && (fwd_proc_format_.rate() == kSampleRate32kHz ||
960 fwd_proc_format_.rate() == kSampleRate48kHz));
andrew@webrtc.org369166a2012-04-24 18:38:03 +0000961}
962
963bool AudioProcessingImpl::analysis_needed(bool is_data_processed) const {
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000964 if (!is_data_processed && !voice_detection_->is_enabled() &&
965 !transient_suppressor_enabled_) {
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000966 // Only level_estimator_ is enabled.
967 return false;
aluebs@webrtc.org087da132014-11-17 23:01:23 +0000968 } else if (fwd_proc_format_.rate() == kSampleRate32kHz ||
969 fwd_proc_format_.rate() == kSampleRate48kHz) {
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000970 // Something besides level_estimator_ is enabled, and we have super-wb.
971 return true;
972 }
973 return false;
974}
975
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000976int AudioProcessingImpl::InitializeExperimentalAgc() {
977 if (use_new_agc_) {
978 if (!agc_manager_.get()) {
979 agc_manager_.reset(
980 new AgcManagerDirect(gain_control_, gain_control_for_new_agc_.get()));
981 }
982 agc_manager_->Initialize();
983 agc_manager_->SetCaptureMuted(output_will_be_muted_);
984 }
985 return kNoError;
986}
987
988int AudioProcessingImpl::InitializeTransient() {
989 if (transient_suppressor_enabled_) {
990 if (!transient_suppressor_.get()) {
991 transient_suppressor_.reset(new TransientSuppressor());
992 }
993 transient_suppressor_->Initialize(fwd_proc_format_.rate(),
994 split_rate_,
995 fwd_out_format_.num_channels());
996 }
997 return kNoError;
998}
999
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +00001000void AudioProcessingImpl::InitializeBeamformer() {
1001 if (beamformer_enabled_) {
1002#ifdef WEBRTC_BEAMFORMER
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001003 if (!beamformer_) {
1004 beamformer_.reset(new Beamformer(array_geometry_));
1005 }
1006 beamformer_->Initialize(kChunkSizeMs, split_rate_);
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +00001007#else
1008 assert(false);
1009#endif
1010 }
1011}
1012
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001013#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
ajm@google.com808e0e02011-08-03 21:08:51 +00001014int AudioProcessingImpl::WriteMessageToDebugFile() {
1015 int32_t size = event_msg_->ByteSize();
1016 if (size <= 0) {
1017 return kUnspecifiedError;
1018 }
andrew@webrtc.org621df672013-10-22 10:27:23 +00001019#if defined(WEBRTC_ARCH_BIG_ENDIAN)
ajm@google.com808e0e02011-08-03 21:08:51 +00001020 // TODO(ajm): Use little-endian "on the wire". For the moment, we can be
1021 // pretty safe in assuming little-endian.
1022#endif
1023
1024 if (!event_msg_->SerializeToString(&event_str_)) {
1025 return kUnspecifiedError;
1026 }
1027
1028 // Write message preceded by its size.
1029 if (!debug_file_->Write(&size, sizeof(int32_t))) {
1030 return kFileError;
1031 }
1032 if (!debug_file_->Write(event_str_.data(), event_str_.length())) {
1033 return kFileError;
1034 }
1035
1036 event_msg_->Clear();
1037
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001038 return kNoError;
ajm@google.com808e0e02011-08-03 21:08:51 +00001039}
1040
1041int AudioProcessingImpl::WriteInitMessage() {
1042 event_msg_->set_type(audioproc::Event::INIT);
1043 audioproc::Init* msg = event_msg_->mutable_init();
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001044 msg->set_sample_rate(fwd_in_format_.rate());
1045 msg->set_num_input_channels(fwd_in_format_.num_channels());
aluebs@webrtc.org27d106b2014-12-11 17:09:21 +00001046 msg->set_num_output_channels(fwd_out_format_.num_channels());
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001047 msg->set_num_reverse_channels(rev_in_format_.num_channels());
1048 msg->set_reverse_sample_rate(rev_in_format_.rate());
1049 msg->set_output_sample_rate(fwd_out_format_.rate());
ajm@google.com808e0e02011-08-03 21:08:51 +00001050
1051 int err = WriteMessageToDebugFile();
1052 if (err != kNoError) {
1053 return err;
1054 }
1055
1056 return kNoError;
1057}
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001058#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001059
niklase@google.com470e71d2011-07-07 08:21:25 +00001060} // namespace webrtc