blob: 147cb18232e1ea248279a919d7b9fbd32c0f4b1c [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
andrew@webrtc.org17e40642014-03-04 20:58:13 +000015#include "webrtc/common_audio/include/audio_util.h"
andrew@webrtc.org60730cf2014-01-07 17:45:09 +000016#include "webrtc/common_audio/signal_processing/include/signal_processing_library.h"
andrew@webrtc.org78693fe2013-03-01 16:36:19 +000017#include "webrtc/modules/audio_processing/audio_buffer.h"
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000018#include "webrtc/modules/audio_processing/common.h"
andrew@webrtc.org56e4a052014-02-27 22:23:17 +000019#include "webrtc/modules/audio_processing/echo_cancellation_impl.h"
andrew@webrtc.org78693fe2013-03-01 16:36:19 +000020#include "webrtc/modules/audio_processing/echo_control_mobile_impl.h"
21#include "webrtc/modules/audio_processing/gain_control_impl.h"
22#include "webrtc/modules/audio_processing/high_pass_filter_impl.h"
23#include "webrtc/modules/audio_processing/level_estimator_impl.h"
24#include "webrtc/modules/audio_processing/noise_suppression_impl.h"
25#include "webrtc/modules/audio_processing/processing_component.h"
andrew@webrtc.org78693fe2013-03-01 16:36:19 +000026#include "webrtc/modules/audio_processing/voice_detection_impl.h"
27#include "webrtc/modules/interface/module_common_types.h"
andrew@webrtc.org60730cf2014-01-07 17:45:09 +000028#include "webrtc/system_wrappers/interface/compile_assert.h"
andrew@webrtc.org78693fe2013-03-01 16:36:19 +000029#include "webrtc/system_wrappers/interface/critical_section_wrapper.h"
30#include "webrtc/system_wrappers/interface/file_wrapper.h"
31#include "webrtc/system_wrappers/interface/logging.h"
andrew@webrtc.org7bf26462011-12-03 00:03:31 +000032
33#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
34// Files generated at build-time by the protobuf compiler.
leozwang@webrtc.orga3736342012-03-16 21:36:00 +000035#ifdef WEBRTC_ANDROID_PLATFORM_BUILD
leozwang@webrtc.org534e4952012-10-22 21:21:52 +000036#include "external/webrtc/webrtc/modules/audio_processing/debug.pb.h"
leozwang@google.comce9bfbb2011-08-03 23:34:31 +000037#else
ajm@google.com808e0e02011-08-03 21:08:51 +000038#include "webrtc/audio_processing/debug.pb.h"
leozwang@google.comce9bfbb2011-08-03 23:34:31 +000039#endif
andrew@webrtc.org7bf26462011-12-03 00:03:31 +000040#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
niklase@google.com470e71d2011-07-07 08:21:25 +000041
andrew@webrtc.org60730cf2014-01-07 17:45:09 +000042#define RETURN_ON_ERR(expr) \
43 do { \
44 int err = expr; \
45 if (err != kNoError) { \
46 return err; \
47 } \
48 } while (0)
49
niklase@google.com470e71d2011-07-07 08:21:25 +000050namespace webrtc {
andrew@webrtc.org60730cf2014-01-07 17:45:09 +000051
52// Throughout webrtc, it's assumed that success is represented by zero.
53COMPILE_ASSERT(AudioProcessing::kNoError == 0, no_error_must_be_zero);
54
niklase@google.com470e71d2011-07-07 08:21:25 +000055AudioProcessing* AudioProcessing::Create(int id) {
andrew@webrtc.orge84978f2014-01-25 02:09:06 +000056 return Create();
57}
58
59AudioProcessing* AudioProcessing::Create() {
60 Config config;
61 return Create(config);
62}
63
64AudioProcessing* AudioProcessing::Create(const Config& config) {
65 AudioProcessingImpl* apm = new AudioProcessingImpl(config);
niklase@google.com470e71d2011-07-07 08:21:25 +000066 if (apm->Initialize() != kNoError) {
67 delete apm;
68 apm = NULL;
69 }
70
71 return apm;
72}
73
andrew@webrtc.orge84978f2014-01-25 02:09:06 +000074AudioProcessingImpl::AudioProcessingImpl(const Config& config)
andrew@webrtc.org60730cf2014-01-07 17:45:09 +000075 : echo_cancellation_(NULL),
niklase@google.com470e71d2011-07-07 08:21:25 +000076 echo_control_mobile_(NULL),
77 gain_control_(NULL),
78 high_pass_filter_(NULL),
79 level_estimator_(NULL),
80 noise_suppression_(NULL),
81 voice_detection_(NULL),
niklase@google.com470e71d2011-07-07 08:21:25 +000082 crit_(CriticalSectionWrapper::CreateCriticalSection()),
andrew@webrtc.org7bf26462011-12-03 00:03:31 +000083#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
84 debug_file_(FileWrapper::Create()),
85 event_msg_(new audioproc::Event()),
86#endif
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000087 fwd_in_format_(kSampleRate16kHz, 1),
88 fwd_proc_format_(kSampleRate16kHz, 1),
89 fwd_out_format_(kSampleRate16kHz),
90 rev_in_format_(kSampleRate16kHz, 1),
91 rev_proc_format_(kSampleRate16kHz, 1),
92 split_rate_(kSampleRate16kHz),
niklase@google.com470e71d2011-07-07 08:21:25 +000093 stream_delay_ms_(0),
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +000094 delay_offset_ms_(0),
niklase@google.com470e71d2011-07-07 08:21:25 +000095 was_stream_delay_set_(false),
andrew@webrtc.org38bf2492014-02-13 17:43:44 +000096 output_will_be_muted_(false),
andrew@webrtc.org07b59502014-02-12 16:41:13 +000097 key_pressed_(false) {
andrew@webrtc.org56e4a052014-02-27 22:23:17 +000098 echo_cancellation_ = new EchoCancellationImpl(this, crit_);
niklase@google.com470e71d2011-07-07 08:21:25 +000099 component_list_.push_back(echo_cancellation_);
100
andrew@webrtc.org56e4a052014-02-27 22:23:17 +0000101 echo_control_mobile_ = new EchoControlMobileImpl(this, crit_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000102 component_list_.push_back(echo_control_mobile_);
103
andrew@webrtc.org56e4a052014-02-27 22:23:17 +0000104 gain_control_ = new GainControlImpl(this, crit_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000105 component_list_.push_back(gain_control_);
106
andrew@webrtc.org56e4a052014-02-27 22:23:17 +0000107 high_pass_filter_ = new HighPassFilterImpl(this, crit_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000108 component_list_.push_back(high_pass_filter_);
109
andrew@webrtc.org56e4a052014-02-27 22:23:17 +0000110 level_estimator_ = new LevelEstimatorImpl(this, crit_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000111 component_list_.push_back(level_estimator_);
112
andrew@webrtc.org56e4a052014-02-27 22:23:17 +0000113 noise_suppression_ = new NoiseSuppressionImpl(this, crit_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000114 component_list_.push_back(noise_suppression_);
115
andrew@webrtc.org56e4a052014-02-27 22:23:17 +0000116 voice_detection_ = new VoiceDetectionImpl(this, crit_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000117 component_list_.push_back(voice_detection_);
andrew@webrtc.orge84978f2014-01-25 02:09:06 +0000118
119 SetExtraOptions(config);
niklase@google.com470e71d2011-07-07 08:21:25 +0000120}
121
122AudioProcessingImpl::~AudioProcessingImpl() {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000123 {
124 CriticalSectionScoped crit_scoped(crit_);
125 while (!component_list_.empty()) {
126 ProcessingComponent* component = component_list_.front();
127 component->Destroy();
128 delete component;
129 component_list_.pop_front();
130 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000131
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000132#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
andrew@webrtc.org81865342012-10-27 00:28:27 +0000133 if (debug_file_->Open()) {
134 debug_file_->CloseFile();
135 }
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000136#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000137 }
andrew@webrtc.org16cfbe22012-08-29 16:58:25 +0000138 delete crit_;
139 crit_ = NULL;
niklase@google.com470e71d2011-07-07 08:21:25 +0000140}
141
niklase@google.com470e71d2011-07-07 08:21:25 +0000142int AudioProcessingImpl::Initialize() {
andrew@webrtc.org40654032012-01-30 20:51:15 +0000143 CriticalSectionScoped crit_scoped(crit_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000144 return InitializeLocked();
145}
146
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000147int AudioProcessingImpl::set_sample_rate_hz(int rate) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000148 CriticalSectionScoped crit_scoped(crit_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000149 return InitializeLocked(rate,
150 rate,
151 rev_in_format_.rate(),
152 fwd_in_format_.num_channels(),
153 fwd_proc_format_.num_channels(),
154 rev_in_format_.num_channels());
155}
156
157int AudioProcessingImpl::Initialize(int input_sample_rate_hz,
158 int output_sample_rate_hz,
159 int reverse_sample_rate_hz,
160 ChannelLayout input_layout,
161 ChannelLayout output_layout,
162 ChannelLayout reverse_layout) {
163 CriticalSectionScoped crit_scoped(crit_);
164 return InitializeLocked(input_sample_rate_hz,
165 output_sample_rate_hz,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000166 reverse_sample_rate_hz,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000167 ChannelsFromLayout(input_layout),
168 ChannelsFromLayout(output_layout),
169 ChannelsFromLayout(reverse_layout));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000170}
171
niklase@google.com470e71d2011-07-07 08:21:25 +0000172int AudioProcessingImpl::InitializeLocked() {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000173 render_audio_.reset(new AudioBuffer(rev_in_format_.samples_per_channel(),
174 rev_in_format_.num_channels(),
175 rev_proc_format_.samples_per_channel(),
176 rev_proc_format_.num_channels(),
177 rev_proc_format_.samples_per_channel()));
178 capture_audio_.reset(new AudioBuffer(fwd_in_format_.samples_per_channel(),
179 fwd_in_format_.num_channels(),
180 fwd_proc_format_.samples_per_channel(),
181 fwd_proc_format_.num_channels(),
182 fwd_out_format_.samples_per_channel()));
niklase@google.com470e71d2011-07-07 08:21:25 +0000183
niklase@google.com470e71d2011-07-07 08:21:25 +0000184 // Initialize all components.
185 std::list<ProcessingComponent*>::iterator it;
andrew@webrtc.org81865342012-10-27 00:28:27 +0000186 for (it = component_list_.begin(); it != component_list_.end(); ++it) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000187 int err = (*it)->Initialize();
188 if (err != kNoError) {
189 return err;
190 }
191 }
192
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000193#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
ajm@google.com808e0e02011-08-03 21:08:51 +0000194 if (debug_file_->Open()) {
195 int err = WriteInitMessage();
196 if (err != kNoError) {
197 return err;
198 }
199 }
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000200#endif
ajm@google.com808e0e02011-08-03 21:08:51 +0000201
niklase@google.com470e71d2011-07-07 08:21:25 +0000202 return kNoError;
203}
204
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000205int AudioProcessingImpl::InitializeLocked(int input_sample_rate_hz,
206 int output_sample_rate_hz,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000207 int reverse_sample_rate_hz,
208 int num_input_channels,
209 int num_output_channels,
210 int num_reverse_channels) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000211 if (input_sample_rate_hz <= 0 ||
212 output_sample_rate_hz <= 0 ||
213 reverse_sample_rate_hz <= 0) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000214 return kBadSampleRateError;
215 }
216 if (num_output_channels > num_input_channels) {
217 return kBadNumberChannelsError;
218 }
219 // Only mono and stereo supported currently.
220 if (num_input_channels > 2 || num_input_channels < 1 ||
221 num_output_channels > 2 || num_output_channels < 1 ||
222 num_reverse_channels > 2 || num_reverse_channels < 1) {
223 return kBadNumberChannelsError;
224 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000225
226 fwd_in_format_.set(input_sample_rate_hz, num_input_channels);
227 fwd_out_format_.set(output_sample_rate_hz);
228 rev_in_format_.set(reverse_sample_rate_hz, num_reverse_channels);
229
230 // We process at the closest native rate >= min(input rate, output rate)...
231 int min_proc_rate = std::min(fwd_in_format_.rate(), fwd_out_format_.rate());
232 int fwd_proc_rate;
233 if (min_proc_rate > kSampleRate16kHz) {
234 fwd_proc_rate = kSampleRate32kHz;
235 } else if (min_proc_rate > kSampleRate8kHz) {
236 fwd_proc_rate = kSampleRate16kHz;
237 } else {
238 fwd_proc_rate = kSampleRate8kHz;
239 }
240 // ...with one exception.
241 if (echo_control_mobile_->is_enabled() && min_proc_rate > kSampleRate16kHz) {
242 fwd_proc_rate = kSampleRate16kHz;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000243 }
244
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000245 fwd_proc_format_.set(fwd_proc_rate, num_output_channels);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000246
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000247 // We normally process the reverse stream at 16 kHz. Unless...
248 int rev_proc_rate = kSampleRate16kHz;
249 if (fwd_proc_format_.rate() == kSampleRate8kHz) {
250 // ...the forward stream is at 8 kHz.
251 rev_proc_rate = kSampleRate8kHz;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000252 } else {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000253 if (rev_in_format_.rate() == kSampleRate32kHz) {
254 // ...or the input is at 32 kHz, in which case we use the splitting
255 // filter rather than the resampler.
256 rev_proc_rate = kSampleRate32kHz;
257 }
258 }
259
260 // TODO(ajm): Enable this.
261 // Always downmix the reverse stream to mono for analysis.
262 //rev_proc_format_.set(rev_proc_rate, 1);
263 rev_proc_format_.set(rev_proc_rate, rev_in_format_.num_channels());
264
265 if (fwd_proc_format_.rate() == kSampleRate32kHz) {
266 split_rate_ = kSampleRate16kHz;
267 } else {
268 split_rate_ = fwd_proc_format_.rate();
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000269 }
270
271 return InitializeLocked();
272}
273
274// Calls InitializeLocked() if any of the audio parameters have changed from
275// their current values.
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000276int AudioProcessingImpl::MaybeInitializeLocked(int input_sample_rate_hz,
277 int output_sample_rate_hz,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000278 int reverse_sample_rate_hz,
279 int num_input_channels,
280 int num_output_channels,
281 int num_reverse_channels) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000282 if (input_sample_rate_hz == fwd_in_format_.rate() &&
283 output_sample_rate_hz == fwd_out_format_.rate() &&
284 reverse_sample_rate_hz == rev_in_format_.rate() &&
285 num_input_channels == fwd_in_format_.num_channels() &&
286 num_output_channels == fwd_proc_format_.num_channels() &&
287 num_reverse_channels == rev_in_format_.num_channels()) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000288 return kNoError;
289 }
290
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000291 return InitializeLocked(input_sample_rate_hz,
292 output_sample_rate_hz,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000293 reverse_sample_rate_hz,
294 num_input_channels,
295 num_output_channels,
296 num_reverse_channels);
297}
298
andrew@webrtc.org61e596f2013-07-25 18:28:29 +0000299void AudioProcessingImpl::SetExtraOptions(const Config& config) {
andrew@webrtc.orge84978f2014-01-25 02:09:06 +0000300 CriticalSectionScoped crit_scoped(crit_);
andrew@webrtc.org61e596f2013-07-25 18:28:29 +0000301 std::list<ProcessingComponent*>::iterator it;
302 for (it = component_list_.begin(); it != component_list_.end(); ++it)
303 (*it)->SetExtraOptions(config);
304}
305
aluebs@webrtc.org0b72f582013-11-19 15:17:51 +0000306int AudioProcessingImpl::EnableExperimentalNs(bool enable) {
307 return kNoError;
308}
309
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000310int AudioProcessingImpl::input_sample_rate_hz() const {
andrew@webrtc.org40654032012-01-30 20:51:15 +0000311 CriticalSectionScoped crit_scoped(crit_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000312 return fwd_in_format_.rate();
niklase@google.com470e71d2011-07-07 08:21:25 +0000313}
314
andrew@webrtc.org46b31b12014-04-23 03:33:54 +0000315int AudioProcessingImpl::sample_rate_hz() const {
316 CriticalSectionScoped crit_scoped(crit_);
317 return fwd_in_format_.rate();
318}
319
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000320int AudioProcessingImpl::proc_sample_rate_hz() const {
321 return fwd_proc_format_.rate();
niklase@google.com470e71d2011-07-07 08:21:25 +0000322}
323
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000324int AudioProcessingImpl::proc_split_sample_rate_hz() const {
325 return split_rate_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000326}
327
328int AudioProcessingImpl::num_reverse_channels() const {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000329 return rev_proc_format_.num_channels();
niklase@google.com470e71d2011-07-07 08:21:25 +0000330}
331
332int AudioProcessingImpl::num_input_channels() const {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000333 return fwd_in_format_.num_channels();
niklase@google.com470e71d2011-07-07 08:21:25 +0000334}
335
336int AudioProcessingImpl::num_output_channels() const {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000337 return fwd_proc_format_.num_channels();
niklase@google.com470e71d2011-07-07 08:21:25 +0000338}
339
andrew@webrtc.org17342e52014-02-12 22:28:31 +0000340void AudioProcessingImpl::set_output_will_be_muted(bool muted) {
341 output_will_be_muted_ = muted;
342}
343
344bool AudioProcessingImpl::output_will_be_muted() const {
345 return output_will_be_muted_;
346}
347
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000348int AudioProcessingImpl::ProcessStream(const float* const* src,
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000349 int samples_per_channel,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000350 int input_sample_rate_hz,
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000351 ChannelLayout input_layout,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000352 int output_sample_rate_hz,
353 ChannelLayout output_layout,
354 float* const* dest) {
andrew@webrtc.org40654032012-01-30 20:51:15 +0000355 CriticalSectionScoped crit_scoped(crit_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000356 if (!src || !dest) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000357 return kNullPointerError;
358 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000359
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000360 RETURN_ON_ERR(MaybeInitializeLocked(input_sample_rate_hz,
361 output_sample_rate_hz,
362 rev_in_format_.rate(),
363 ChannelsFromLayout(input_layout),
364 ChannelsFromLayout(output_layout),
365 rev_in_format_.num_channels()));
366 if (samples_per_channel != fwd_in_format_.samples_per_channel()) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000367 return kBadDataLengthError;
368 }
369
370#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
371 if (debug_file_->Open()) {
372 event_msg_->set_type(audioproc::Event::STREAM);
373 audioproc::Stream* msg = event_msg_->mutable_stream();
374 const size_t channel_size = sizeof(float) * samples_per_channel;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000375 for (int i = 0; i < fwd_in_format_.num_channels(); ++i)
376 msg->add_input_channel(src[i], channel_size);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000377 }
378#endif
379
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000380 capture_audio_->CopyFrom(src, samples_per_channel, input_layout);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000381 RETURN_ON_ERR(ProcessStreamLocked());
382 if (output_copy_needed(is_data_processed())) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000383 capture_audio_->CopyTo(fwd_out_format_.samples_per_channel(),
384 output_layout,
385 dest);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000386 }
387
388#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
389 if (debug_file_->Open()) {
390 audioproc::Stream* msg = event_msg_->mutable_stream();
391 const size_t channel_size = sizeof(float) * samples_per_channel;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000392 for (int i = 0; i < fwd_proc_format_.num_channels(); ++i)
393 msg->add_output_channel(dest[i], channel_size);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000394 RETURN_ON_ERR(WriteMessageToDebugFile());
395 }
396#endif
397
398 return kNoError;
399}
400
401int AudioProcessingImpl::ProcessStream(AudioFrame* frame) {
402 CriticalSectionScoped crit_scoped(crit_);
403 if (!frame) {
404 return kNullPointerError;
405 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000406 // Must be a native rate.
407 if (frame->sample_rate_hz_ != kSampleRate8kHz &&
408 frame->sample_rate_hz_ != kSampleRate16kHz &&
409 frame->sample_rate_hz_ != kSampleRate32kHz) {
410 return kBadSampleRateError;
411 }
412 if (echo_control_mobile_->is_enabled() &&
413 frame->sample_rate_hz_ > kSampleRate16kHz) {
414 LOG(LS_ERROR) << "AECM only supports 16 or 8 kHz sample rates";
415 return kUnsupportedComponentError;
416 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000417
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000418 // TODO(ajm): The input and output rates and channels are currently
419 // constrained to be identical in the int16 interface.
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000420 RETURN_ON_ERR(MaybeInitializeLocked(frame->sample_rate_hz_,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000421 frame->sample_rate_hz_,
422 rev_in_format_.rate(),
423 frame->num_channels_,
424 frame->num_channels_,
425 rev_in_format_.num_channels()));
426 if (frame->samples_per_channel_ != fwd_in_format_.samples_per_channel()) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000427 return kBadDataLengthError;
428 }
429
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000430#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
niklase@google.com470e71d2011-07-07 08:21:25 +0000431 if (debug_file_->Open()) {
ajm@google.com808e0e02011-08-03 21:08:51 +0000432 event_msg_->set_type(audioproc::Event::STREAM);
433 audioproc::Stream* msg = event_msg_->mutable_stream();
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000434 const size_t data_size = sizeof(int16_t) *
andrew@webrtc.org63a50982012-05-02 23:56:37 +0000435 frame->samples_per_channel_ *
436 frame->num_channels_;
437 msg->set_input_data(frame->data_, data_size);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000438 }
439#endif
440
441 capture_audio_->DeinterleaveFrom(frame);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000442 RETURN_ON_ERR(ProcessStreamLocked());
443 capture_audio_->InterleaveTo(frame, output_copy_needed(is_data_processed()));
444
445#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
446 if (debug_file_->Open()) {
447 audioproc::Stream* msg = event_msg_->mutable_stream();
448 const size_t data_size = sizeof(int16_t) *
449 frame->samples_per_channel_ *
450 frame->num_channels_;
451 msg->set_output_data(frame->data_, data_size);
452 RETURN_ON_ERR(WriteMessageToDebugFile());
453 }
454#endif
455
456 return kNoError;
457}
458
459
460int AudioProcessingImpl::ProcessStreamLocked() {
461#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
462 if (debug_file_->Open()) {
463 audioproc::Stream* msg = event_msg_->mutable_stream();
ajm@google.com808e0e02011-08-03 21:08:51 +0000464 msg->set_delay(stream_delay_ms_);
465 msg->set_drift(echo_cancellation_->stream_drift_samples());
466 msg->set_level(gain_control_->stream_analog_level());
andrew@webrtc.orgce8e0772014-02-12 15:28:30 +0000467 msg->set_keypress(key_pressed_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000468 }
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000469#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000470
andrew@webrtc.org369166a2012-04-24 18:38:03 +0000471 bool data_processed = is_data_processed();
472 if (analysis_needed(data_processed)) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000473 for (int i = 0; i < fwd_proc_format_.num_channels(); i++) {
474 SplitFilterStates* filter_states = capture_audio_->filter_states(i);
niklase@google.com470e71d2011-07-07 08:21:25 +0000475 // Split into a low and high band.
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000476 WebRtcSpl_AnalysisQMF(capture_audio_->data(i),
477 capture_audio_->samples_per_channel(),
478 capture_audio_->low_pass_split_data(i),
479 capture_audio_->high_pass_split_data(i),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000480 filter_states->analysis_filter_state1,
481 filter_states->analysis_filter_state2);
niklase@google.com470e71d2011-07-07 08:21:25 +0000482 }
483 }
484
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000485 RETURN_ON_ERR(high_pass_filter_->ProcessCaptureAudio(capture_audio_.get()));
486 RETURN_ON_ERR(gain_control_->AnalyzeCaptureAudio(capture_audio_.get()));
487 RETURN_ON_ERR(echo_cancellation_->ProcessCaptureAudio(capture_audio_.get()));
niklase@google.com470e71d2011-07-07 08:21:25 +0000488
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000489 if (echo_control_mobile_->is_enabled() && noise_suppression_->is_enabled()) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000490 capture_audio_->CopyLowPassToReference();
491 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000492 RETURN_ON_ERR(noise_suppression_->ProcessCaptureAudio(capture_audio_.get()));
493 RETURN_ON_ERR(
494 echo_control_mobile_->ProcessCaptureAudio(capture_audio_.get()));
495 RETURN_ON_ERR(voice_detection_->ProcessCaptureAudio(capture_audio_.get()));
496 RETURN_ON_ERR(gain_control_->ProcessCaptureAudio(capture_audio_.get()));
niklase@google.com470e71d2011-07-07 08:21:25 +0000497
andrew@webrtc.org369166a2012-04-24 18:38:03 +0000498 if (synthesis_needed(data_processed)) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000499 for (int i = 0; i < fwd_proc_format_.num_channels(); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000500 // Recombine low and high bands.
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000501 SplitFilterStates* filter_states = capture_audio_->filter_states(i);
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000502 WebRtcSpl_SynthesisQMF(capture_audio_->low_pass_split_data(i),
503 capture_audio_->high_pass_split_data(i),
504 capture_audio_->samples_per_split_channel(),
505 capture_audio_->data(i),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000506 filter_states->synthesis_filter_state1,
507 filter_states->synthesis_filter_state2);
niklase@google.com470e71d2011-07-07 08:21:25 +0000508 }
509 }
510
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000511 // The level estimator operates on the recombined data.
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000512 RETURN_ON_ERR(level_estimator_->ProcessStream(capture_audio_.get()));
ajm@google.com808e0e02011-08-03 21:08:51 +0000513
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000514 was_stream_delay_set_ = false;
niklase@google.com470e71d2011-07-07 08:21:25 +0000515 return kNoError;
516}
517
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000518int AudioProcessingImpl::AnalyzeReverseStream(const float* const* data,
519 int samples_per_channel,
520 int sample_rate_hz,
521 ChannelLayout layout) {
522 CriticalSectionScoped crit_scoped(crit_);
523 if (data == NULL) {
524 return kNullPointerError;
525 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000526
527 const int num_channels = ChannelsFromLayout(layout);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000528 RETURN_ON_ERR(MaybeInitializeLocked(fwd_in_format_.rate(),
529 fwd_out_format_.rate(),
530 sample_rate_hz,
531 fwd_in_format_.num_channels(),
532 fwd_proc_format_.num_channels(),
533 num_channels));
534 if (samples_per_channel != rev_in_format_.samples_per_channel()) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000535 return kBadDataLengthError;
536 }
537
538#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
539 if (debug_file_->Open()) {
540 event_msg_->set_type(audioproc::Event::REVERSE_STREAM);
541 audioproc::ReverseStream* msg = event_msg_->mutable_reverse_stream();
542 const size_t channel_size = sizeof(float) * samples_per_channel;
543 for (int i = 0; i < num_channels; ++i)
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000544 msg->add_channel(data[i], channel_size);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000545 RETURN_ON_ERR(WriteMessageToDebugFile());
546 }
547#endif
548
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000549 render_audio_->CopyFrom(data, samples_per_channel, layout);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000550 return AnalyzeReverseStreamLocked();
551}
552
niklase@google.com470e71d2011-07-07 08:21:25 +0000553int AudioProcessingImpl::AnalyzeReverseStream(AudioFrame* frame) {
andrew@webrtc.org40654032012-01-30 20:51:15 +0000554 CriticalSectionScoped crit_scoped(crit_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000555 if (frame == NULL) {
556 return kNullPointerError;
557 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000558 // Must be a native rate.
559 if (frame->sample_rate_hz_ != kSampleRate8kHz &&
560 frame->sample_rate_hz_ != kSampleRate16kHz &&
561 frame->sample_rate_hz_ != kSampleRate32kHz) {
562 return kBadSampleRateError;
563 }
564 // This interface does not tolerate different forward and reverse rates.
565 if (frame->sample_rate_hz_ != fwd_in_format_.rate()) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000566 return kBadSampleRateError;
567 }
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000568
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000569 RETURN_ON_ERR(MaybeInitializeLocked(fwd_in_format_.rate(),
570 fwd_out_format_.rate(),
571 frame->sample_rate_hz_,
572 fwd_in_format_.num_channels(),
573 fwd_in_format_.num_channels(),
574 frame->num_channels_));
575 if (frame->samples_per_channel_ != rev_in_format_.samples_per_channel()) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000576 return kBadDataLengthError;
577 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000578
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000579#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
niklase@google.com470e71d2011-07-07 08:21:25 +0000580 if (debug_file_->Open()) {
ajm@google.com808e0e02011-08-03 21:08:51 +0000581 event_msg_->set_type(audioproc::Event::REVERSE_STREAM);
582 audioproc::ReverseStream* msg = event_msg_->mutable_reverse_stream();
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000583 const size_t data_size = sizeof(int16_t) *
andrew@webrtc.org63a50982012-05-02 23:56:37 +0000584 frame->samples_per_channel_ *
585 frame->num_channels_;
586 msg->set_data(frame->data_, data_size);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000587 RETURN_ON_ERR(WriteMessageToDebugFile());
niklase@google.com470e71d2011-07-07 08:21:25 +0000588 }
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000589#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000590
591 render_audio_->DeinterleaveFrom(frame);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000592 return AnalyzeReverseStreamLocked();
593}
niklase@google.com470e71d2011-07-07 08:21:25 +0000594
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000595// TODO(ajm): Have AnalyzeReverseStream accept sample rates not matching the
596// primary stream and convert ourselves rather than having the user manage it.
597// We can be smarter and use the splitting filter when appropriate. Similarly,
598// perform downmixing here.
599int AudioProcessingImpl::AnalyzeReverseStreamLocked() {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000600 if (rev_proc_format_.rate() == kSampleRate32kHz) {
601 for (int i = 0; i < rev_proc_format_.num_channels(); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000602 // Split into low and high band.
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000603 SplitFilterStates* filter_states = render_audio_->filter_states(i);
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000604 WebRtcSpl_AnalysisQMF(render_audio_->data(i),
605 render_audio_->samples_per_channel(),
606 render_audio_->low_pass_split_data(i),
607 render_audio_->high_pass_split_data(i),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000608 filter_states->analysis_filter_state1,
609 filter_states->analysis_filter_state2);
niklase@google.com470e71d2011-07-07 08:21:25 +0000610 }
611 }
612
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000613 RETURN_ON_ERR(echo_cancellation_->ProcessRenderAudio(render_audio_.get()));
614 RETURN_ON_ERR(echo_control_mobile_->ProcessRenderAudio(render_audio_.get()));
615 RETURN_ON_ERR(gain_control_->ProcessRenderAudio(render_audio_.get()));
niklase@google.com470e71d2011-07-07 08:21:25 +0000616
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000617 return kNoError;
niklase@google.com470e71d2011-07-07 08:21:25 +0000618}
619
620int AudioProcessingImpl::set_stream_delay_ms(int delay) {
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000621 Error retval = kNoError;
niklase@google.com470e71d2011-07-07 08:21:25 +0000622 was_stream_delay_set_ = true;
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000623 delay += delay_offset_ms_;
624
niklase@google.com470e71d2011-07-07 08:21:25 +0000625 if (delay < 0) {
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000626 delay = 0;
627 retval = kBadStreamParameterWarning;
niklase@google.com470e71d2011-07-07 08:21:25 +0000628 }
629
630 // TODO(ajm): the max is rather arbitrarily chosen; investigate.
631 if (delay > 500) {
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000632 delay = 500;
633 retval = kBadStreamParameterWarning;
niklase@google.com470e71d2011-07-07 08:21:25 +0000634 }
635
636 stream_delay_ms_ = delay;
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000637 return retval;
niklase@google.com470e71d2011-07-07 08:21:25 +0000638}
639
640int AudioProcessingImpl::stream_delay_ms() const {
641 return stream_delay_ms_;
642}
643
644bool AudioProcessingImpl::was_stream_delay_set() const {
645 return was_stream_delay_set_;
646}
647
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000648void AudioProcessingImpl::set_stream_key_pressed(bool key_pressed) {
649 key_pressed_ = key_pressed;
650}
651
652bool AudioProcessingImpl::stream_key_pressed() const {
653 return key_pressed_;
654}
655
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000656void AudioProcessingImpl::set_delay_offset_ms(int offset) {
657 CriticalSectionScoped crit_scoped(crit_);
658 delay_offset_ms_ = offset;
659}
660
661int AudioProcessingImpl::delay_offset_ms() const {
662 return delay_offset_ms_;
663}
664
niklase@google.com470e71d2011-07-07 08:21:25 +0000665int AudioProcessingImpl::StartDebugRecording(
666 const char filename[AudioProcessing::kMaxFilenameSize]) {
andrew@webrtc.org40654032012-01-30 20:51:15 +0000667 CriticalSectionScoped crit_scoped(crit_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000668 assert(kMaxFilenameSize == FileWrapper::kMaxFileNameSize);
669
670 if (filename == NULL) {
671 return kNullPointerError;
672 }
673
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000674#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
niklase@google.com470e71d2011-07-07 08:21:25 +0000675 // Stop any ongoing recording.
676 if (debug_file_->Open()) {
677 if (debug_file_->CloseFile() == -1) {
678 return kFileError;
679 }
680 }
681
682 if (debug_file_->OpenFile(filename, false) == -1) {
683 debug_file_->CloseFile();
684 return kFileError;
685 }
686
ajm@google.com808e0e02011-08-03 21:08:51 +0000687 int err = WriteInitMessage();
688 if (err != kNoError) {
689 return err;
niklase@google.com470e71d2011-07-07 08:21:25 +0000690 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000691 return kNoError;
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000692#else
693 return kUnsupportedFunctionError;
694#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
niklase@google.com470e71d2011-07-07 08:21:25 +0000695}
696
henrikg@webrtc.org863b5362013-12-06 16:05:17 +0000697int AudioProcessingImpl::StartDebugRecording(FILE* handle) {
698 CriticalSectionScoped crit_scoped(crit_);
699
700 if (handle == NULL) {
701 return kNullPointerError;
702 }
703
704#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
705 // Stop any ongoing recording.
706 if (debug_file_->Open()) {
707 if (debug_file_->CloseFile() == -1) {
708 return kFileError;
709 }
710 }
711
712 if (debug_file_->OpenFromFileHandle(handle, true, false) == -1) {
713 return kFileError;
714 }
715
716 int err = WriteInitMessage();
717 if (err != kNoError) {
718 return err;
719 }
720 return kNoError;
721#else
722 return kUnsupportedFunctionError;
723#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
724}
725
niklase@google.com470e71d2011-07-07 08:21:25 +0000726int AudioProcessingImpl::StopDebugRecording() {
andrew@webrtc.org40654032012-01-30 20:51:15 +0000727 CriticalSectionScoped crit_scoped(crit_);
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000728
729#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
niklase@google.com470e71d2011-07-07 08:21:25 +0000730 // We just return if recording hasn't started.
731 if (debug_file_->Open()) {
732 if (debug_file_->CloseFile() == -1) {
733 return kFileError;
734 }
735 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000736 return kNoError;
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000737#else
738 return kUnsupportedFunctionError;
739#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
niklase@google.com470e71d2011-07-07 08:21:25 +0000740}
741
742EchoCancellation* AudioProcessingImpl::echo_cancellation() const {
743 return echo_cancellation_;
744}
745
746EchoControlMobile* AudioProcessingImpl::echo_control_mobile() const {
747 return echo_control_mobile_;
748}
749
750GainControl* AudioProcessingImpl::gain_control() const {
751 return gain_control_;
752}
753
754HighPassFilter* AudioProcessingImpl::high_pass_filter() const {
755 return high_pass_filter_;
756}
757
758LevelEstimator* AudioProcessingImpl::level_estimator() const {
759 return level_estimator_;
760}
761
762NoiseSuppression* AudioProcessingImpl::noise_suppression() const {
763 return noise_suppression_;
764}
765
766VoiceDetection* AudioProcessingImpl::voice_detection() const {
767 return voice_detection_;
768}
769
andrew@webrtc.org369166a2012-04-24 18:38:03 +0000770bool AudioProcessingImpl::is_data_processed() const {
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000771 int enabled_count = 0;
772 std::list<ProcessingComponent*>::const_iterator it;
773 for (it = component_list_.begin(); it != component_list_.end(); it++) {
774 if ((*it)->is_component_enabled()) {
775 enabled_count++;
776 }
777 }
778
779 // Data is unchanged if no components are enabled, or if only level_estimator_
780 // or voice_detection_ is enabled.
781 if (enabled_count == 0) {
782 return false;
783 } else if (enabled_count == 1) {
784 if (level_estimator_->is_enabled() || voice_detection_->is_enabled()) {
785 return false;
786 }
787 } else if (enabled_count == 2) {
788 if (level_estimator_->is_enabled() && voice_detection_->is_enabled()) {
789 return false;
790 }
791 }
792 return true;
793}
794
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000795bool AudioProcessingImpl::output_copy_needed(bool is_data_processed) const {
andrew@webrtc.org369166a2012-04-24 18:38:03 +0000796 // Check if we've upmixed or downmixed the audio.
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000797 return ((fwd_proc_format_.num_channels() != fwd_in_format_.num_channels()) ||
798 is_data_processed);
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000799}
800
andrew@webrtc.org369166a2012-04-24 18:38:03 +0000801bool AudioProcessingImpl::synthesis_needed(bool is_data_processed) const {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000802 return (is_data_processed && fwd_proc_format_.rate() == kSampleRate32kHz);
andrew@webrtc.org369166a2012-04-24 18:38:03 +0000803}
804
805bool AudioProcessingImpl::analysis_needed(bool is_data_processed) const {
806 if (!is_data_processed && !voice_detection_->is_enabled()) {
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000807 // Only level_estimator_ is enabled.
808 return false;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000809 } else if (fwd_proc_format_.rate() == kSampleRate32kHz) {
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000810 // Something besides level_estimator_ is enabled, and we have super-wb.
811 return true;
812 }
813 return false;
814}
815
816#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
ajm@google.com808e0e02011-08-03 21:08:51 +0000817int AudioProcessingImpl::WriteMessageToDebugFile() {
818 int32_t size = event_msg_->ByteSize();
819 if (size <= 0) {
820 return kUnspecifiedError;
821 }
andrew@webrtc.org621df672013-10-22 10:27:23 +0000822#if defined(WEBRTC_ARCH_BIG_ENDIAN)
ajm@google.com808e0e02011-08-03 21:08:51 +0000823 // TODO(ajm): Use little-endian "on the wire". For the moment, we can be
824 // pretty safe in assuming little-endian.
825#endif
826
827 if (!event_msg_->SerializeToString(&event_str_)) {
828 return kUnspecifiedError;
829 }
830
831 // Write message preceded by its size.
832 if (!debug_file_->Write(&size, sizeof(int32_t))) {
833 return kFileError;
834 }
835 if (!debug_file_->Write(event_str_.data(), event_str_.length())) {
836 return kFileError;
837 }
838
839 event_msg_->Clear();
840
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000841 return kNoError;
ajm@google.com808e0e02011-08-03 21:08:51 +0000842}
843
844int AudioProcessingImpl::WriteInitMessage() {
845 event_msg_->set_type(audioproc::Event::INIT);
846 audioproc::Init* msg = event_msg_->mutable_init();
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000847 msg->set_sample_rate(fwd_in_format_.rate());
848 msg->set_num_input_channels(fwd_in_format_.num_channels());
849 msg->set_num_output_channels(fwd_proc_format_.num_channels());
850 msg->set_num_reverse_channels(rev_in_format_.num_channels());
851 msg->set_reverse_sample_rate(rev_in_format_.rate());
852 msg->set_output_sample_rate(fwd_out_format_.rate());
ajm@google.com808e0e02011-08-03 21:08:51 +0000853
854 int err = WriteMessageToDebugFile();
855 if (err != kNoError) {
856 return err;
857 }
858
859 return kNoError;
860}
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000861#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000862
niklase@google.com470e71d2011-07-07 08:21:25 +0000863} // namespace webrtc