blob: aed63412dfeed8c5a4c01c5177583c40ceddbc44 [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"
Michael Graczykdfa36052015-03-25 16:37:27 -070017#include "webrtc/common_audio/channel_buffer.h"
andrew@webrtc.org60730cf2014-01-07 17:45:09 +000018#include "webrtc/common_audio/signal_processing/include/signal_processing_library.h"
pbos@webrtc.org788acd12014-12-15 09:41:24 +000019#include "webrtc/modules/audio_processing/agc/agc_manager_direct.h"
andrew@webrtc.org78693fe2013-03-01 16:36:19 +000020#include "webrtc/modules/audio_processing/audio_buffer.h"
mgraczyk@chromium.org0f663de2015-03-13 00:13:32 +000021#include "webrtc/modules/audio_processing/beamformer/nonlinear_beamformer.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 { \
mgraczyk@chromium.org0f663de2015-03-13 00:13:32 +000048 int err = (expr); \
andrew@webrtc.org60730cf2014-01-07 17:45:09 +000049 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.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000078 int Enable(bool enable) override {
pbos@webrtc.org788acd12014-12-15 09:41:24 +000079 return real_gain_control_->Enable(enable);
80 }
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000081 bool is_enabled() const override { return real_gain_control_->is_enabled(); }
82 int set_stream_analog_level(int level) override {
pbos@webrtc.org788acd12014-12-15 09:41:24 +000083 volume_ = level;
84 return AudioProcessing::kNoError;
85 }
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000086 int stream_analog_level() override { return volume_; }
87 int set_mode(Mode mode) override { return AudioProcessing::kNoError; }
88 Mode mode() const override { return GainControl::kAdaptiveAnalog; }
89 int set_target_level_dbfs(int level) override {
pbos@webrtc.org788acd12014-12-15 09:41:24 +000090 return AudioProcessing::kNoError;
91 }
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000092 int target_level_dbfs() const override {
pbos@webrtc.org788acd12014-12-15 09:41:24 +000093 return real_gain_control_->target_level_dbfs();
94 }
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000095 int set_compression_gain_db(int gain) override {
pbos@webrtc.org788acd12014-12-15 09:41:24 +000096 return AudioProcessing::kNoError;
97 }
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000098 int compression_gain_db() const override {
pbos@webrtc.org788acd12014-12-15 09:41:24 +000099 return real_gain_control_->compression_gain_db();
100 }
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000101 int enable_limiter(bool enable) override { return AudioProcessing::kNoError; }
102 bool is_limiter_enabled() const override {
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000103 return real_gain_control_->is_limiter_enabled();
104 }
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000105 int set_analog_level_limits(int minimum, int maximum) override {
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000106 return AudioProcessing::kNoError;
107 }
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000108 int analog_level_minimum() const override {
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000109 return real_gain_control_->analog_level_minimum();
110 }
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000111 int analog_level_maximum() const override {
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000112 return real_gain_control_->analog_level_maximum();
113 }
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000114 bool stream_is_saturated() const override {
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000115 return real_gain_control_->stream_is_saturated();
116 }
117
118 // VolumeCallbacks implementation.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000119 void SetMicVolume(int volume) override { volume_ = volume; }
120 int GetMicVolume() override { return volume_; }
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000121
122 private:
123 GainControl* real_gain_control_;
124 int volume_;
125};
126
andrew@webrtc.orge84978f2014-01-25 02:09:06 +0000127AudioProcessing* AudioProcessing::Create() {
128 Config config;
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000129 return Create(config, nullptr);
andrew@webrtc.orge84978f2014-01-25 02:09:06 +0000130}
131
132AudioProcessing* AudioProcessing::Create(const Config& config) {
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000133 return Create(config, nullptr);
134}
135
136AudioProcessing* AudioProcessing::Create(const Config& config,
Michael Graczykdfa36052015-03-25 16:37:27 -0700137 Beamformer<float>* beamformer) {
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000138 AudioProcessingImpl* apm = new AudioProcessingImpl(config, beamformer);
niklase@google.com470e71d2011-07-07 08:21:25 +0000139 if (apm->Initialize() != kNoError) {
140 delete apm;
141 apm = NULL;
142 }
143
144 return apm;
145}
146
andrew@webrtc.orge84978f2014-01-25 02:09:06 +0000147AudioProcessingImpl::AudioProcessingImpl(const Config& config)
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000148 : AudioProcessingImpl(config, nullptr) {}
149
150AudioProcessingImpl::AudioProcessingImpl(const Config& config,
Michael Graczykdfa36052015-03-25 16:37:27 -0700151 Beamformer<float>* beamformer)
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000152 : echo_cancellation_(NULL),
niklase@google.com470e71d2011-07-07 08:21:25 +0000153 echo_control_mobile_(NULL),
154 gain_control_(NULL),
155 high_pass_filter_(NULL),
156 level_estimator_(NULL),
157 noise_suppression_(NULL),
158 voice_detection_(NULL),
niklase@google.com470e71d2011-07-07 08:21:25 +0000159 crit_(CriticalSectionWrapper::CreateCriticalSection()),
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000160#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
161 debug_file_(FileWrapper::Create()),
162 event_msg_(new audioproc::Event()),
163#endif
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000164 fwd_in_format_(kSampleRate16kHz, 1),
aluebs@webrtc.org27d106b2014-12-11 17:09:21 +0000165 fwd_proc_format_(kSampleRate16kHz),
166 fwd_out_format_(kSampleRate16kHz, 1),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000167 rev_in_format_(kSampleRate16kHz, 1),
168 rev_proc_format_(kSampleRate16kHz, 1),
169 split_rate_(kSampleRate16kHz),
niklase@google.com470e71d2011-07-07 08:21:25 +0000170 stream_delay_ms_(0),
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000171 delay_offset_ms_(0),
niklase@google.com470e71d2011-07-07 08:21:25 +0000172 was_stream_delay_set_(false),
andrew@webrtc.org38bf2492014-02-13 17:43:44 +0000173 output_will_be_muted_(false),
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000174 key_pressed_(false),
175#if defined(WEBRTC_ANDROID) || defined(WEBRTC_IOS)
176 use_new_agc_(false),
177#else
178 use_new_agc_(config.Get<ExperimentalAgc>().enabled),
179#endif
Bjorn Volckeradc46c42015-04-15 11:42:40 +0200180 agc_startup_min_volume_(config.Get<ExperimentalAgc>().startup_min_volume),
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +0000181 transient_suppressor_enabled_(config.Get<ExperimentalNs>().enabled),
aluebs@webrtc.orgfb7a0392015-01-05 21:58:58 +0000182 beamformer_enabled_(config.Get<Beamforming>().enabled),
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000183 beamformer_(beamformer),
aluebs@webrtc.orgc9ce07e2015-03-02 20:07:31 +0000184 array_geometry_(config.Get<Beamforming>().array_geometry),
185 supports_48kHz_(config.Get<AudioProcessing48kHzSupport>().enabled) {
andrew@webrtc.org56e4a052014-02-27 22:23:17 +0000186 echo_cancellation_ = new EchoCancellationImpl(this, crit_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000187 component_list_.push_back(echo_cancellation_);
188
andrew@webrtc.org56e4a052014-02-27 22:23:17 +0000189 echo_control_mobile_ = new EchoControlMobileImpl(this, crit_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000190 component_list_.push_back(echo_control_mobile_);
191
andrew@webrtc.org56e4a052014-02-27 22:23:17 +0000192 gain_control_ = new GainControlImpl(this, crit_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000193 component_list_.push_back(gain_control_);
194
andrew@webrtc.org56e4a052014-02-27 22:23:17 +0000195 high_pass_filter_ = new HighPassFilterImpl(this, crit_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000196 component_list_.push_back(high_pass_filter_);
197
andrew@webrtc.org56e4a052014-02-27 22:23:17 +0000198 level_estimator_ = new LevelEstimatorImpl(this, crit_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000199 component_list_.push_back(level_estimator_);
200
andrew@webrtc.org56e4a052014-02-27 22:23:17 +0000201 noise_suppression_ = new NoiseSuppressionImpl(this, crit_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000202 component_list_.push_back(noise_suppression_);
203
andrew@webrtc.org56e4a052014-02-27 22:23:17 +0000204 voice_detection_ = new VoiceDetectionImpl(this, crit_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000205 component_list_.push_back(voice_detection_);
andrew@webrtc.orge84978f2014-01-25 02:09:06 +0000206
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000207 gain_control_for_new_agc_.reset(new GainControlForNewAgc(gain_control_));
208
andrew@webrtc.orge84978f2014-01-25 02:09:06 +0000209 SetExtraOptions(config);
niklase@google.com470e71d2011-07-07 08:21:25 +0000210}
211
212AudioProcessingImpl::~AudioProcessingImpl() {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000213 {
214 CriticalSectionScoped crit_scoped(crit_);
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000215 // Depends on gain_control_ and gain_control_for_new_agc_.
216 agc_manager_.reset();
217 // Depends on gain_control_.
218 gain_control_for_new_agc_.reset();
andrew@webrtc.org81865342012-10-27 00:28:27 +0000219 while (!component_list_.empty()) {
220 ProcessingComponent* component = component_list_.front();
221 component->Destroy();
222 delete component;
223 component_list_.pop_front();
224 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000225
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000226#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
andrew@webrtc.org81865342012-10-27 00:28:27 +0000227 if (debug_file_->Open()) {
228 debug_file_->CloseFile();
229 }
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000230#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000231 }
andrew@webrtc.org16cfbe22012-08-29 16:58:25 +0000232 delete crit_;
233 crit_ = NULL;
niklase@google.com470e71d2011-07-07 08:21:25 +0000234}
235
niklase@google.com470e71d2011-07-07 08:21:25 +0000236int AudioProcessingImpl::Initialize() {
andrew@webrtc.org40654032012-01-30 20:51:15 +0000237 CriticalSectionScoped crit_scoped(crit_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000238 return InitializeLocked();
239}
240
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000241int AudioProcessingImpl::set_sample_rate_hz(int rate) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000242 CriticalSectionScoped crit_scoped(crit_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000243 return InitializeLocked(rate,
244 rate,
245 rev_in_format_.rate(),
246 fwd_in_format_.num_channels(),
aluebs@webrtc.org27d106b2014-12-11 17:09:21 +0000247 fwd_out_format_.num_channels(),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000248 rev_in_format_.num_channels());
249}
250
251int AudioProcessingImpl::Initialize(int input_sample_rate_hz,
252 int output_sample_rate_hz,
253 int reverse_sample_rate_hz,
254 ChannelLayout input_layout,
255 ChannelLayout output_layout,
256 ChannelLayout reverse_layout) {
257 CriticalSectionScoped crit_scoped(crit_);
258 return InitializeLocked(input_sample_rate_hz,
259 output_sample_rate_hz,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000260 reverse_sample_rate_hz,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000261 ChannelsFromLayout(input_layout),
262 ChannelsFromLayout(output_layout),
263 ChannelsFromLayout(reverse_layout));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000264}
265
niklase@google.com470e71d2011-07-07 08:21:25 +0000266int AudioProcessingImpl::InitializeLocked() {
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +0000267 const int fwd_audio_buffer_channels = beamformer_enabled_ ?
268 fwd_in_format_.num_channels() :
269 fwd_out_format_.num_channels();
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000270 render_audio_.reset(new AudioBuffer(rev_in_format_.samples_per_channel(),
271 rev_in_format_.num_channels(),
272 rev_proc_format_.samples_per_channel(),
273 rev_proc_format_.num_channels(),
274 rev_proc_format_.samples_per_channel()));
275 capture_audio_.reset(new AudioBuffer(fwd_in_format_.samples_per_channel(),
276 fwd_in_format_.num_channels(),
277 fwd_proc_format_.samples_per_channel(),
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +0000278 fwd_audio_buffer_channels,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000279 fwd_out_format_.samples_per_channel()));
niklase@google.com470e71d2011-07-07 08:21:25 +0000280
niklase@google.com470e71d2011-07-07 08:21:25 +0000281 // Initialize all components.
mgraczyk@chromium.orge5340862015-03-12 23:23:38 +0000282 for (auto item : component_list_) {
283 int err = item->Initialize();
niklase@google.com470e71d2011-07-07 08:21:25 +0000284 if (err != kNoError) {
285 return err;
286 }
287 }
288
Bjorn Volckeradc46c42015-04-15 11:42:40 +0200289 InitializeExperimentalAgc();
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000290
Bjorn Volckeradc46c42015-04-15 11:42:40 +0200291 InitializeTransient();
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000292
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +0000293 InitializeBeamformer();
294
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000295#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
ajm@google.com808e0e02011-08-03 21:08:51 +0000296 if (debug_file_->Open()) {
297 int err = WriteInitMessage();
298 if (err != kNoError) {
299 return err;
300 }
301 }
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000302#endif
ajm@google.com808e0e02011-08-03 21:08:51 +0000303
niklase@google.com470e71d2011-07-07 08:21:25 +0000304 return kNoError;
305}
306
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000307int AudioProcessingImpl::InitializeLocked(int input_sample_rate_hz,
308 int output_sample_rate_hz,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000309 int reverse_sample_rate_hz,
310 int num_input_channels,
311 int num_output_channels,
312 int num_reverse_channels) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000313 if (input_sample_rate_hz <= 0 ||
314 output_sample_rate_hz <= 0 ||
315 reverse_sample_rate_hz <= 0) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000316 return kBadSampleRateError;
317 }
318 if (num_output_channels > num_input_channels) {
319 return kBadNumberChannelsError;
320 }
321 // Only mono and stereo supported currently.
322 if (num_input_channels > 2 || num_input_channels < 1 ||
323 num_output_channels > 2 || num_output_channels < 1 ||
324 num_reverse_channels > 2 || num_reverse_channels < 1) {
325 return kBadNumberChannelsError;
326 }
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000327 if (beamformer_enabled_ &&
328 (static_cast<size_t>(num_input_channels) != array_geometry_.size() ||
329 num_output_channels > 1)) {
330 return kBadNumberChannelsError;
331 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000332
333 fwd_in_format_.set(input_sample_rate_hz, num_input_channels);
aluebs@webrtc.org27d106b2014-12-11 17:09:21 +0000334 fwd_out_format_.set(output_sample_rate_hz, num_output_channels);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000335 rev_in_format_.set(reverse_sample_rate_hz, num_reverse_channels);
336
337 // We process at the closest native rate >= min(input rate, output rate)...
338 int min_proc_rate = std::min(fwd_in_format_.rate(), fwd_out_format_.rate());
339 int fwd_proc_rate;
aluebs@webrtc.orgc9ce07e2015-03-02 20:07:31 +0000340 if (supports_48kHz_ && min_proc_rate > kSampleRate32kHz) {
341 fwd_proc_rate = kSampleRate48kHz;
342 } else if (min_proc_rate > kSampleRate16kHz) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000343 fwd_proc_rate = kSampleRate32kHz;
344 } else if (min_proc_rate > kSampleRate8kHz) {
345 fwd_proc_rate = kSampleRate16kHz;
346 } else {
347 fwd_proc_rate = kSampleRate8kHz;
348 }
349 // ...with one exception.
350 if (echo_control_mobile_->is_enabled() && min_proc_rate > kSampleRate16kHz) {
351 fwd_proc_rate = kSampleRate16kHz;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000352 }
353
aluebs@webrtc.org27d106b2014-12-11 17:09:21 +0000354 fwd_proc_format_.set(fwd_proc_rate);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000355
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000356 // We normally process the reverse stream at 16 kHz. Unless...
357 int rev_proc_rate = kSampleRate16kHz;
358 if (fwd_proc_format_.rate() == kSampleRate8kHz) {
359 // ...the forward stream is at 8 kHz.
360 rev_proc_rate = kSampleRate8kHz;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000361 } else {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000362 if (rev_in_format_.rate() == kSampleRate32kHz) {
363 // ...or the input is at 32 kHz, in which case we use the splitting
364 // filter rather than the resampler.
365 rev_proc_rate = kSampleRate32kHz;
366 }
367 }
368
andrew@webrtc.org30be8272014-09-24 20:06:23 +0000369 // Always downmix the reverse stream to mono for analysis. This has been
370 // demonstrated to work well for AEC in most practical scenarios.
371 rev_proc_format_.set(rev_proc_rate, 1);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000372
aluebs@webrtc.org087da132014-11-17 23:01:23 +0000373 if (fwd_proc_format_.rate() == kSampleRate32kHz ||
374 fwd_proc_format_.rate() == kSampleRate48kHz) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000375 split_rate_ = kSampleRate16kHz;
376 } else {
377 split_rate_ = fwd_proc_format_.rate();
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000378 }
379
380 return InitializeLocked();
381}
382
383// Calls InitializeLocked() if any of the audio parameters have changed from
384// their current values.
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000385int AudioProcessingImpl::MaybeInitializeLocked(int input_sample_rate_hz,
386 int output_sample_rate_hz,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000387 int reverse_sample_rate_hz,
388 int num_input_channels,
389 int num_output_channels,
390 int num_reverse_channels) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000391 if (input_sample_rate_hz == fwd_in_format_.rate() &&
392 output_sample_rate_hz == fwd_out_format_.rate() &&
393 reverse_sample_rate_hz == rev_in_format_.rate() &&
394 num_input_channels == fwd_in_format_.num_channels() &&
aluebs@webrtc.org27d106b2014-12-11 17:09:21 +0000395 num_output_channels == fwd_out_format_.num_channels() &&
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000396 num_reverse_channels == rev_in_format_.num_channels()) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000397 return kNoError;
398 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000399 return InitializeLocked(input_sample_rate_hz,
400 output_sample_rate_hz,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000401 reverse_sample_rate_hz,
402 num_input_channels,
403 num_output_channels,
404 num_reverse_channels);
405}
406
andrew@webrtc.org61e596f2013-07-25 18:28:29 +0000407void AudioProcessingImpl::SetExtraOptions(const Config& config) {
andrew@webrtc.orge84978f2014-01-25 02:09:06 +0000408 CriticalSectionScoped crit_scoped(crit_);
mgraczyk@chromium.orge5340862015-03-12 23:23:38 +0000409 for (auto item : component_list_) {
410 item->SetExtraOptions(config);
411 }
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000412
413 if (transient_suppressor_enabled_ != config.Get<ExperimentalNs>().enabled) {
414 transient_suppressor_enabled_ = config.Get<ExperimentalNs>().enabled;
415 InitializeTransient();
416 }
andrew@webrtc.org61e596f2013-07-25 18:28:29 +0000417}
418
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000419int AudioProcessingImpl::input_sample_rate_hz() const {
andrew@webrtc.org40654032012-01-30 20:51:15 +0000420 CriticalSectionScoped crit_scoped(crit_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000421 return fwd_in_format_.rate();
niklase@google.com470e71d2011-07-07 08:21:25 +0000422}
423
andrew@webrtc.org46b31b12014-04-23 03:33:54 +0000424int AudioProcessingImpl::sample_rate_hz() const {
425 CriticalSectionScoped crit_scoped(crit_);
426 return fwd_in_format_.rate();
427}
428
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000429int AudioProcessingImpl::proc_sample_rate_hz() const {
430 return fwd_proc_format_.rate();
niklase@google.com470e71d2011-07-07 08:21:25 +0000431}
432
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000433int AudioProcessingImpl::proc_split_sample_rate_hz() const {
434 return split_rate_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000435}
436
437int AudioProcessingImpl::num_reverse_channels() const {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000438 return rev_proc_format_.num_channels();
niklase@google.com470e71d2011-07-07 08:21:25 +0000439}
440
441int AudioProcessingImpl::num_input_channels() const {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000442 return fwd_in_format_.num_channels();
niklase@google.com470e71d2011-07-07 08:21:25 +0000443}
444
445int AudioProcessingImpl::num_output_channels() const {
aluebs@webrtc.org27d106b2014-12-11 17:09:21 +0000446 return fwd_out_format_.num_channels();
niklase@google.com470e71d2011-07-07 08:21:25 +0000447}
448
andrew@webrtc.org17342e52014-02-12 22:28:31 +0000449void AudioProcessingImpl::set_output_will_be_muted(bool muted) {
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000450 CriticalSectionScoped lock(crit_);
Bjorn Volcker424694c2015-03-27 11:30:43 +0100451 output_will_be_muted_ = muted;
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000452 if (agc_manager_.get()) {
453 agc_manager_->SetCaptureMuted(output_will_be_muted_);
454 }
andrew@webrtc.org17342e52014-02-12 22:28:31 +0000455}
456
457bool AudioProcessingImpl::output_will_be_muted() const {
Bjorn Volcker424694c2015-03-27 11:30:43 +0100458 CriticalSectionScoped lock(crit_);
andrew@webrtc.org17342e52014-02-12 22:28:31 +0000459 return output_will_be_muted_;
460}
461
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000462int AudioProcessingImpl::ProcessStream(const float* const* src,
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000463 int samples_per_channel,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000464 int input_sample_rate_hz,
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000465 ChannelLayout input_layout,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000466 int output_sample_rate_hz,
467 ChannelLayout output_layout,
468 float* const* dest) {
andrew@webrtc.org40654032012-01-30 20:51:15 +0000469 CriticalSectionScoped crit_scoped(crit_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000470 if (!src || !dest) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000471 return kNullPointerError;
472 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000473
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000474 RETURN_ON_ERR(MaybeInitializeLocked(input_sample_rate_hz,
475 output_sample_rate_hz,
476 rev_in_format_.rate(),
477 ChannelsFromLayout(input_layout),
478 ChannelsFromLayout(output_layout),
479 rev_in_format_.num_channels()));
480 if (samples_per_channel != fwd_in_format_.samples_per_channel()) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000481 return kBadDataLengthError;
482 }
483
484#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
485 if (debug_file_->Open()) {
486 event_msg_->set_type(audioproc::Event::STREAM);
487 audioproc::Stream* msg = event_msg_->mutable_stream();
aluebs@webrtc.org59a1b1b2014-08-28 10:43:09 +0000488 const size_t channel_size =
489 sizeof(float) * fwd_in_format_.samples_per_channel();
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000490 for (int i = 0; i < fwd_in_format_.num_channels(); ++i)
491 msg->add_input_channel(src[i], channel_size);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000492 }
493#endif
494
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000495 capture_audio_->CopyFrom(src, samples_per_channel, input_layout);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000496 RETURN_ON_ERR(ProcessStreamLocked());
mgraczyk@chromium.orgd6e84d92015-01-14 01:33:54 +0000497 capture_audio_->CopyTo(fwd_out_format_.samples_per_channel(),
498 output_layout,
499 dest);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000500
501#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
502 if (debug_file_->Open()) {
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_out_format_.samples_per_channel();
aluebs@webrtc.org27d106b2014-12-11 17:09:21 +0000506 for (int i = 0; i < fwd_out_format_.num_channels(); ++i)
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000507 msg->add_output_channel(dest[i], channel_size);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000508 RETURN_ON_ERR(WriteMessageToDebugFile());
509 }
510#endif
511
512 return kNoError;
513}
514
515int AudioProcessingImpl::ProcessStream(AudioFrame* frame) {
516 CriticalSectionScoped crit_scoped(crit_);
517 if (!frame) {
518 return kNullPointerError;
519 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000520 // Must be a native rate.
521 if (frame->sample_rate_hz_ != kSampleRate8kHz &&
522 frame->sample_rate_hz_ != kSampleRate16kHz &&
aluebs@webrtc.org087da132014-11-17 23:01:23 +0000523 frame->sample_rate_hz_ != kSampleRate32kHz &&
524 frame->sample_rate_hz_ != kSampleRate48kHz) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000525 return kBadSampleRateError;
526 }
527 if (echo_control_mobile_->is_enabled() &&
528 frame->sample_rate_hz_ > kSampleRate16kHz) {
529 LOG(LS_ERROR) << "AECM only supports 16 or 8 kHz sample rates";
530 return kUnsupportedComponentError;
531 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000532
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000533 // TODO(ajm): The input and output rates and channels are currently
534 // constrained to be identical in the int16 interface.
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000535 RETURN_ON_ERR(MaybeInitializeLocked(frame->sample_rate_hz_,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000536 frame->sample_rate_hz_,
537 rev_in_format_.rate(),
538 frame->num_channels_,
539 frame->num_channels_,
540 rev_in_format_.num_channels()));
541 if (frame->samples_per_channel_ != fwd_in_format_.samples_per_channel()) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000542 return kBadDataLengthError;
543 }
544
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000545#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
niklase@google.com470e71d2011-07-07 08:21:25 +0000546 if (debug_file_->Open()) {
ajm@google.com808e0e02011-08-03 21:08:51 +0000547 event_msg_->set_type(audioproc::Event::STREAM);
548 audioproc::Stream* msg = event_msg_->mutable_stream();
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000549 const size_t data_size = sizeof(int16_t) *
andrew@webrtc.org63a50982012-05-02 23:56:37 +0000550 frame->samples_per_channel_ *
551 frame->num_channels_;
552 msg->set_input_data(frame->data_, data_size);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000553 }
554#endif
555
556 capture_audio_->DeinterleaveFrom(frame);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000557 RETURN_ON_ERR(ProcessStreamLocked());
558 capture_audio_->InterleaveTo(frame, output_copy_needed(is_data_processed()));
559
560#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
561 if (debug_file_->Open()) {
562 audioproc::Stream* msg = event_msg_->mutable_stream();
563 const size_t data_size = sizeof(int16_t) *
564 frame->samples_per_channel_ *
565 frame->num_channels_;
566 msg->set_output_data(frame->data_, data_size);
567 RETURN_ON_ERR(WriteMessageToDebugFile());
568 }
569#endif
570
571 return kNoError;
572}
573
574
575int AudioProcessingImpl::ProcessStreamLocked() {
576#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
577 if (debug_file_->Open()) {
578 audioproc::Stream* msg = event_msg_->mutable_stream();
ajm@google.com808e0e02011-08-03 21:08:51 +0000579 msg->set_delay(stream_delay_ms_);
580 msg->set_drift(echo_cancellation_->stream_drift_samples());
bjornv@webrtc.org63da1dd2015-02-06 19:44:21 +0000581 msg->set_level(gain_control()->stream_analog_level());
andrew@webrtc.orgce8e0772014-02-12 15:28:30 +0000582 msg->set_keypress(key_pressed_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000583 }
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000584#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000585
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000586 AudioBuffer* ca = capture_audio_.get(); // For brevity.
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000587 if (use_new_agc_ && gain_control_->is_enabled()) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000588 agc_manager_->AnalyzePreProcess(ca->channels()[0],
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000589 ca->num_channels(),
590 fwd_proc_format_.samples_per_channel());
591 }
592
andrew@webrtc.org369166a2012-04-24 18:38:03 +0000593 bool data_processed = is_data_processed();
594 if (analysis_needed(data_processed)) {
aluebs@webrtc.orgbe05c742014-11-14 22:18:10 +0000595 ca->SplitIntoFrequencyBands();
niklase@google.com470e71d2011-07-07 08:21:25 +0000596 }
597
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +0000598 if (beamformer_enabled_) {
Michael Graczykdfa36052015-03-25 16:37:27 -0700599 beamformer_->ProcessChunk(*ca->split_data_f(), ca->split_data_f());
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +0000600 ca->set_num_channels(1);
601 }
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +0000602
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000603 RETURN_ON_ERR(high_pass_filter_->ProcessCaptureAudio(ca));
604 RETURN_ON_ERR(gain_control_->AnalyzeCaptureAudio(ca));
aluebs@webrtc.orga0ce9fa2014-09-24 14:18:03 +0000605 RETURN_ON_ERR(noise_suppression_->AnalyzeCaptureAudio(ca));
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000606 RETURN_ON_ERR(echo_cancellation_->ProcessCaptureAudio(ca));
niklase@google.com470e71d2011-07-07 08:21:25 +0000607
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000608 if (echo_control_mobile_->is_enabled() && noise_suppression_->is_enabled()) {
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000609 ca->CopyLowPassToReference();
niklase@google.com470e71d2011-07-07 08:21:25 +0000610 }
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000611 RETURN_ON_ERR(noise_suppression_->ProcessCaptureAudio(ca));
612 RETURN_ON_ERR(echo_control_mobile_->ProcessCaptureAudio(ca));
613 RETURN_ON_ERR(voice_detection_->ProcessCaptureAudio(ca));
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000614
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000615 if (use_new_agc_ &&
616 gain_control_->is_enabled() &&
617 (!beamformer_enabled_ || beamformer_->is_target_present())) {
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000618 agc_manager_->Process(ca->split_bands_const(0)[kBand0To8kHz],
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000619 ca->num_frames_per_band(),
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000620 split_rate_);
621 }
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000622 RETURN_ON_ERR(gain_control_->ProcessCaptureAudio(ca));
niklase@google.com470e71d2011-07-07 08:21:25 +0000623
andrew@webrtc.org369166a2012-04-24 18:38:03 +0000624 if (synthesis_needed(data_processed)) {
aluebs@webrtc.orgbe05c742014-11-14 22:18:10 +0000625 ca->MergeFrequencyBands();
niklase@google.com470e71d2011-07-07 08:21:25 +0000626 }
627
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000628 // TODO(aluebs): Investigate if the transient suppression placement should be
629 // before or after the AGC.
630 if (transient_suppressor_enabled_) {
631 float voice_probability =
632 agc_manager_.get() ? agc_manager_->voice_probability() : 1.f;
633
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000634 transient_suppressor_->Suppress(ca->channels_f()[0],
635 ca->num_frames(),
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000636 ca->num_channels(),
637 ca->split_bands_const_f(0)[kBand0To8kHz],
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000638 ca->num_frames_per_band(),
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000639 ca->keyboard_data(),
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000640 ca->num_keyboard_frames(),
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000641 voice_probability,
642 key_pressed_);
643 }
644
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000645 // The level estimator operates on the recombined data.
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000646 RETURN_ON_ERR(level_estimator_->ProcessStream(ca));
ajm@google.com808e0e02011-08-03 21:08:51 +0000647
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000648 was_stream_delay_set_ = false;
niklase@google.com470e71d2011-07-07 08:21:25 +0000649 return kNoError;
650}
651
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000652int AudioProcessingImpl::AnalyzeReverseStream(const float* const* data,
653 int samples_per_channel,
654 int sample_rate_hz,
655 ChannelLayout layout) {
656 CriticalSectionScoped crit_scoped(crit_);
657 if (data == NULL) {
658 return kNullPointerError;
659 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000660
661 const int num_channels = ChannelsFromLayout(layout);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000662 RETURN_ON_ERR(MaybeInitializeLocked(fwd_in_format_.rate(),
663 fwd_out_format_.rate(),
664 sample_rate_hz,
665 fwd_in_format_.num_channels(),
aluebs@webrtc.org27d106b2014-12-11 17:09:21 +0000666 fwd_out_format_.num_channels(),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000667 num_channels));
668 if (samples_per_channel != rev_in_format_.samples_per_channel()) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000669 return kBadDataLengthError;
670 }
671
672#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
673 if (debug_file_->Open()) {
674 event_msg_->set_type(audioproc::Event::REVERSE_STREAM);
675 audioproc::ReverseStream* msg = event_msg_->mutable_reverse_stream();
aluebs@webrtc.org59a1b1b2014-08-28 10:43:09 +0000676 const size_t channel_size =
677 sizeof(float) * rev_in_format_.samples_per_channel();
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000678 for (int i = 0; i < num_channels; ++i)
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000679 msg->add_channel(data[i], channel_size);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000680 RETURN_ON_ERR(WriteMessageToDebugFile());
681 }
682#endif
683
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000684 render_audio_->CopyFrom(data, samples_per_channel, layout);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000685 return AnalyzeReverseStreamLocked();
686}
687
niklase@google.com470e71d2011-07-07 08:21:25 +0000688int AudioProcessingImpl::AnalyzeReverseStream(AudioFrame* frame) {
andrew@webrtc.org40654032012-01-30 20:51:15 +0000689 CriticalSectionScoped crit_scoped(crit_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000690 if (frame == NULL) {
691 return kNullPointerError;
692 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000693 // Must be a native rate.
694 if (frame->sample_rate_hz_ != kSampleRate8kHz &&
695 frame->sample_rate_hz_ != kSampleRate16kHz &&
aluebs@webrtc.org087da132014-11-17 23:01:23 +0000696 frame->sample_rate_hz_ != kSampleRate32kHz &&
697 frame->sample_rate_hz_ != kSampleRate48kHz) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000698 return kBadSampleRateError;
699 }
700 // This interface does not tolerate different forward and reverse rates.
701 if (frame->sample_rate_hz_ != fwd_in_format_.rate()) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000702 return kBadSampleRateError;
703 }
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000704
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000705 RETURN_ON_ERR(MaybeInitializeLocked(fwd_in_format_.rate(),
706 fwd_out_format_.rate(),
707 frame->sample_rate_hz_,
708 fwd_in_format_.num_channels(),
709 fwd_in_format_.num_channels(),
710 frame->num_channels_));
711 if (frame->samples_per_channel_ != rev_in_format_.samples_per_channel()) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000712 return kBadDataLengthError;
713 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000714
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000715#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
niklase@google.com470e71d2011-07-07 08:21:25 +0000716 if (debug_file_->Open()) {
ajm@google.com808e0e02011-08-03 21:08:51 +0000717 event_msg_->set_type(audioproc::Event::REVERSE_STREAM);
718 audioproc::ReverseStream* msg = event_msg_->mutable_reverse_stream();
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000719 const size_t data_size = sizeof(int16_t) *
andrew@webrtc.org63a50982012-05-02 23:56:37 +0000720 frame->samples_per_channel_ *
721 frame->num_channels_;
722 msg->set_data(frame->data_, data_size);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000723 RETURN_ON_ERR(WriteMessageToDebugFile());
niklase@google.com470e71d2011-07-07 08:21:25 +0000724 }
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000725#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000726
727 render_audio_->DeinterleaveFrom(frame);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000728 return AnalyzeReverseStreamLocked();
729}
niklase@google.com470e71d2011-07-07 08:21:25 +0000730
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000731int AudioProcessingImpl::AnalyzeReverseStreamLocked() {
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000732 AudioBuffer* ra = render_audio_.get(); // For brevity.
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000733 if (rev_proc_format_.rate() == kSampleRate32kHz) {
aluebs@webrtc.orgbe05c742014-11-14 22:18:10 +0000734 ra->SplitIntoFrequencyBands();
niklase@google.com470e71d2011-07-07 08:21:25 +0000735 }
736
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000737 RETURN_ON_ERR(echo_cancellation_->ProcessRenderAudio(ra));
738 RETURN_ON_ERR(echo_control_mobile_->ProcessRenderAudio(ra));
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000739 if (!use_new_agc_) {
740 RETURN_ON_ERR(gain_control_->ProcessRenderAudio(ra));
741 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000742
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000743 return kNoError;
niklase@google.com470e71d2011-07-07 08:21:25 +0000744}
745
746int AudioProcessingImpl::set_stream_delay_ms(int delay) {
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000747 Error retval = kNoError;
niklase@google.com470e71d2011-07-07 08:21:25 +0000748 was_stream_delay_set_ = true;
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000749 delay += delay_offset_ms_;
750
niklase@google.com470e71d2011-07-07 08:21:25 +0000751 if (delay < 0) {
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000752 delay = 0;
753 retval = kBadStreamParameterWarning;
niklase@google.com470e71d2011-07-07 08:21:25 +0000754 }
755
756 // TODO(ajm): the max is rather arbitrarily chosen; investigate.
757 if (delay > 500) {
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000758 delay = 500;
759 retval = kBadStreamParameterWarning;
niklase@google.com470e71d2011-07-07 08:21:25 +0000760 }
761
762 stream_delay_ms_ = delay;
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000763 return retval;
niklase@google.com470e71d2011-07-07 08:21:25 +0000764}
765
766int AudioProcessingImpl::stream_delay_ms() const {
767 return stream_delay_ms_;
768}
769
770bool AudioProcessingImpl::was_stream_delay_set() const {
771 return was_stream_delay_set_;
772}
773
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000774void AudioProcessingImpl::set_stream_key_pressed(bool key_pressed) {
775 key_pressed_ = key_pressed;
776}
777
778bool AudioProcessingImpl::stream_key_pressed() const {
779 return key_pressed_;
780}
781
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000782void AudioProcessingImpl::set_delay_offset_ms(int offset) {
783 CriticalSectionScoped crit_scoped(crit_);
784 delay_offset_ms_ = offset;
785}
786
787int AudioProcessingImpl::delay_offset_ms() const {
788 return delay_offset_ms_;
789}
790
niklase@google.com470e71d2011-07-07 08:21:25 +0000791int AudioProcessingImpl::StartDebugRecording(
792 const char filename[AudioProcessing::kMaxFilenameSize]) {
andrew@webrtc.org40654032012-01-30 20:51:15 +0000793 CriticalSectionScoped crit_scoped(crit_);
André Susano Pinto664cdaf2015-05-20 11:11:07 +0200794 static_assert(kMaxFilenameSize == FileWrapper::kMaxFileNameSize, "");
niklase@google.com470e71d2011-07-07 08:21:25 +0000795
796 if (filename == NULL) {
797 return kNullPointerError;
798 }
799
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000800#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
niklase@google.com470e71d2011-07-07 08:21:25 +0000801 // Stop any ongoing recording.
802 if (debug_file_->Open()) {
803 if (debug_file_->CloseFile() == -1) {
804 return kFileError;
805 }
806 }
807
808 if (debug_file_->OpenFile(filename, false) == -1) {
809 debug_file_->CloseFile();
810 return kFileError;
811 }
812
ajm@google.com808e0e02011-08-03 21:08:51 +0000813 int err = WriteInitMessage();
814 if (err != kNoError) {
815 return err;
niklase@google.com470e71d2011-07-07 08:21:25 +0000816 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000817 return kNoError;
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000818#else
819 return kUnsupportedFunctionError;
820#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
niklase@google.com470e71d2011-07-07 08:21:25 +0000821}
822
henrikg@webrtc.org863b5362013-12-06 16:05:17 +0000823int AudioProcessingImpl::StartDebugRecording(FILE* handle) {
824 CriticalSectionScoped crit_scoped(crit_);
825
826 if (handle == NULL) {
827 return kNullPointerError;
828 }
829
830#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
831 // Stop any ongoing recording.
832 if (debug_file_->Open()) {
833 if (debug_file_->CloseFile() == -1) {
834 return kFileError;
835 }
836 }
837
838 if (debug_file_->OpenFromFileHandle(handle, true, false) == -1) {
839 return kFileError;
840 }
841
842 int err = WriteInitMessage();
843 if (err != kNoError) {
844 return err;
845 }
846 return kNoError;
847#else
848 return kUnsupportedFunctionError;
849#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
850}
851
xians@webrtc.orge46bc772014-10-10 08:36:56 +0000852int AudioProcessingImpl::StartDebugRecordingForPlatformFile(
853 rtc::PlatformFile handle) {
854 FILE* stream = rtc::FdopenPlatformFileForWriting(handle);
855 return StartDebugRecording(stream);
856}
857
niklase@google.com470e71d2011-07-07 08:21:25 +0000858int AudioProcessingImpl::StopDebugRecording() {
andrew@webrtc.org40654032012-01-30 20:51:15 +0000859 CriticalSectionScoped crit_scoped(crit_);
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000860
861#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
niklase@google.com470e71d2011-07-07 08:21:25 +0000862 // We just return if recording hasn't started.
863 if (debug_file_->Open()) {
864 if (debug_file_->CloseFile() == -1) {
865 return kFileError;
866 }
867 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000868 return kNoError;
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000869#else
870 return kUnsupportedFunctionError;
871#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
niklase@google.com470e71d2011-07-07 08:21:25 +0000872}
873
874EchoCancellation* AudioProcessingImpl::echo_cancellation() const {
875 return echo_cancellation_;
876}
877
878EchoControlMobile* AudioProcessingImpl::echo_control_mobile() const {
879 return echo_control_mobile_;
880}
881
882GainControl* AudioProcessingImpl::gain_control() const {
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000883 if (use_new_agc_) {
884 return gain_control_for_new_agc_.get();
885 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000886 return gain_control_;
887}
888
889HighPassFilter* AudioProcessingImpl::high_pass_filter() const {
890 return high_pass_filter_;
891}
892
893LevelEstimator* AudioProcessingImpl::level_estimator() const {
894 return level_estimator_;
895}
896
897NoiseSuppression* AudioProcessingImpl::noise_suppression() const {
898 return noise_suppression_;
899}
900
901VoiceDetection* AudioProcessingImpl::voice_detection() const {
902 return voice_detection_;
903}
904
andrew@webrtc.org369166a2012-04-24 18:38:03 +0000905bool AudioProcessingImpl::is_data_processed() const {
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +0000906 if (beamformer_enabled_) {
907 return true;
908 }
909
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000910 int enabled_count = 0;
mgraczyk@chromium.orge5340862015-03-12 23:23:38 +0000911 for (auto item : component_list_) {
912 if (item->is_component_enabled()) {
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000913 enabled_count++;
914 }
915 }
916
917 // Data is unchanged if no components are enabled, or if only level_estimator_
918 // or voice_detection_ is enabled.
919 if (enabled_count == 0) {
920 return false;
921 } else if (enabled_count == 1) {
922 if (level_estimator_->is_enabled() || voice_detection_->is_enabled()) {
923 return false;
924 }
925 } else if (enabled_count == 2) {
926 if (level_estimator_->is_enabled() && voice_detection_->is_enabled()) {
927 return false;
928 }
929 }
930 return true;
931}
932
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000933bool AudioProcessingImpl::output_copy_needed(bool is_data_processed) const {
andrew@webrtc.org369166a2012-04-24 18:38:03 +0000934 // Check if we've upmixed or downmixed the audio.
aluebs@webrtc.org27d106b2014-12-11 17:09:21 +0000935 return ((fwd_out_format_.num_channels() != fwd_in_format_.num_channels()) ||
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000936 is_data_processed || transient_suppressor_enabled_);
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000937}
938
andrew@webrtc.org369166a2012-04-24 18:38:03 +0000939bool AudioProcessingImpl::synthesis_needed(bool is_data_processed) const {
aluebs@webrtc.org087da132014-11-17 23:01:23 +0000940 return (is_data_processed && (fwd_proc_format_.rate() == kSampleRate32kHz ||
941 fwd_proc_format_.rate() == kSampleRate48kHz));
andrew@webrtc.org369166a2012-04-24 18:38:03 +0000942}
943
944bool AudioProcessingImpl::analysis_needed(bool is_data_processed) const {
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000945 if (!is_data_processed && !voice_detection_->is_enabled() &&
946 !transient_suppressor_enabled_) {
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000947 // Only level_estimator_ is enabled.
948 return false;
aluebs@webrtc.org087da132014-11-17 23:01:23 +0000949 } else if (fwd_proc_format_.rate() == kSampleRate32kHz ||
950 fwd_proc_format_.rate() == kSampleRate48kHz) {
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000951 // Something besides level_estimator_ is enabled, and we have super-wb.
952 return true;
953 }
954 return false;
955}
956
Bjorn Volckeradc46c42015-04-15 11:42:40 +0200957void AudioProcessingImpl::InitializeExperimentalAgc() {
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000958 if (use_new_agc_) {
959 if (!agc_manager_.get()) {
Bjorn Volckeradc46c42015-04-15 11:42:40 +0200960 agc_manager_.reset(new AgcManagerDirect(gain_control_,
961 gain_control_for_new_agc_.get(),
962 agc_startup_min_volume_));
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000963 }
964 agc_manager_->Initialize();
965 agc_manager_->SetCaptureMuted(output_will_be_muted_);
966 }
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000967}
968
Bjorn Volckeradc46c42015-04-15 11:42:40 +0200969void AudioProcessingImpl::InitializeTransient() {
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000970 if (transient_suppressor_enabled_) {
971 if (!transient_suppressor_.get()) {
972 transient_suppressor_.reset(new TransientSuppressor());
973 }
974 transient_suppressor_->Initialize(fwd_proc_format_.rate(),
975 split_rate_,
976 fwd_out_format_.num_channels());
977 }
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000978}
979
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +0000980void AudioProcessingImpl::InitializeBeamformer() {
981 if (beamformer_enabled_) {
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000982 if (!beamformer_) {
mgraczyk@chromium.org0f663de2015-03-13 00:13:32 +0000983 beamformer_.reset(new NonlinearBeamformer(array_geometry_));
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000984 }
985 beamformer_->Initialize(kChunkSizeMs, split_rate_);
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +0000986 }
987}
988
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000989#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
ajm@google.com808e0e02011-08-03 21:08:51 +0000990int AudioProcessingImpl::WriteMessageToDebugFile() {
991 int32_t size = event_msg_->ByteSize();
992 if (size <= 0) {
993 return kUnspecifiedError;
994 }
andrew@webrtc.org621df672013-10-22 10:27:23 +0000995#if defined(WEBRTC_ARCH_BIG_ENDIAN)
ajm@google.com808e0e02011-08-03 21:08:51 +0000996 // TODO(ajm): Use little-endian "on the wire". For the moment, we can be
997 // pretty safe in assuming little-endian.
998#endif
999
1000 if (!event_msg_->SerializeToString(&event_str_)) {
1001 return kUnspecifiedError;
1002 }
1003
1004 // Write message preceded by its size.
1005 if (!debug_file_->Write(&size, sizeof(int32_t))) {
1006 return kFileError;
1007 }
1008 if (!debug_file_->Write(event_str_.data(), event_str_.length())) {
1009 return kFileError;
1010 }
1011
1012 event_msg_->Clear();
1013
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001014 return kNoError;
ajm@google.com808e0e02011-08-03 21:08:51 +00001015}
1016
1017int AudioProcessingImpl::WriteInitMessage() {
1018 event_msg_->set_type(audioproc::Event::INIT);
1019 audioproc::Init* msg = event_msg_->mutable_init();
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001020 msg->set_sample_rate(fwd_in_format_.rate());
1021 msg->set_num_input_channels(fwd_in_format_.num_channels());
aluebs@webrtc.org27d106b2014-12-11 17:09:21 +00001022 msg->set_num_output_channels(fwd_out_format_.num_channels());
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001023 msg->set_num_reverse_channels(rev_in_format_.num_channels());
1024 msg->set_reverse_sample_rate(rev_in_format_.rate());
1025 msg->set_output_sample_rate(fwd_out_format_.rate());
ajm@google.com808e0e02011-08-03 21:08:51 +00001026
1027 int err = WriteMessageToDebugFile();
1028 if (err != kNoError) {
1029 return err;
1030 }
1031
1032 return kNoError;
1033}
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001034#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001035
niklase@google.com470e71d2011-07-07 08:21:25 +00001036} // namespace webrtc