blob: 005b3f76b02d013b32eed6fcb48c0834902f0979 [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.orgddbb8a22014-04-22 21:00:04 +0000315int AudioProcessingImpl::proc_sample_rate_hz() const {
316 return fwd_proc_format_.rate();
niklase@google.com470e71d2011-07-07 08:21:25 +0000317}
318
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000319int AudioProcessingImpl::proc_split_sample_rate_hz() const {
320 return split_rate_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000321}
322
323int AudioProcessingImpl::num_reverse_channels() const {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000324 return rev_proc_format_.num_channels();
niklase@google.com470e71d2011-07-07 08:21:25 +0000325}
326
327int AudioProcessingImpl::num_input_channels() const {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000328 return fwd_in_format_.num_channels();
niklase@google.com470e71d2011-07-07 08:21:25 +0000329}
330
331int AudioProcessingImpl::num_output_channels() const {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000332 return fwd_proc_format_.num_channels();
niklase@google.com470e71d2011-07-07 08:21:25 +0000333}
334
andrew@webrtc.org17342e52014-02-12 22:28:31 +0000335void AudioProcessingImpl::set_output_will_be_muted(bool muted) {
336 output_will_be_muted_ = muted;
337}
338
339bool AudioProcessingImpl::output_will_be_muted() const {
340 return output_will_be_muted_;
341}
342
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000343int AudioProcessingImpl::ProcessStream(const float* const* src,
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000344 int samples_per_channel,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000345 int input_sample_rate_hz,
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000346 ChannelLayout input_layout,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000347 int output_sample_rate_hz,
348 ChannelLayout output_layout,
349 float* const* dest) {
andrew@webrtc.org40654032012-01-30 20:51:15 +0000350 CriticalSectionScoped crit_scoped(crit_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000351 if (!src || !dest) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000352 return kNullPointerError;
353 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000354
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000355 RETURN_ON_ERR(MaybeInitializeLocked(input_sample_rate_hz,
356 output_sample_rate_hz,
357 rev_in_format_.rate(),
358 ChannelsFromLayout(input_layout),
359 ChannelsFromLayout(output_layout),
360 rev_in_format_.num_channels()));
361 if (samples_per_channel != fwd_in_format_.samples_per_channel()) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000362 return kBadDataLengthError;
363 }
364
365#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
366 if (debug_file_->Open()) {
367 event_msg_->set_type(audioproc::Event::STREAM);
368 audioproc::Stream* msg = event_msg_->mutable_stream();
369 const size_t channel_size = sizeof(float) * samples_per_channel;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000370 for (int i = 0; i < fwd_in_format_.num_channels(); ++i)
371 msg->add_input_channel(src[i], channel_size);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000372 }
373#endif
374
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000375 capture_audio_->CopyFrom(src, samples_per_channel, input_layout);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000376 RETURN_ON_ERR(ProcessStreamLocked());
377 if (output_copy_needed(is_data_processed())) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000378 capture_audio_->CopyTo(fwd_out_format_.samples_per_channel(),
379 output_layout,
380 dest);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000381 }
382
383#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
384 if (debug_file_->Open()) {
385 audioproc::Stream* msg = event_msg_->mutable_stream();
386 const size_t channel_size = sizeof(float) * samples_per_channel;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000387 for (int i = 0; i < fwd_proc_format_.num_channels(); ++i)
388 msg->add_output_channel(dest[i], channel_size);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000389 RETURN_ON_ERR(WriteMessageToDebugFile());
390 }
391#endif
392
393 return kNoError;
394}
395
396int AudioProcessingImpl::ProcessStream(AudioFrame* frame) {
397 CriticalSectionScoped crit_scoped(crit_);
398 if (!frame) {
399 return kNullPointerError;
400 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000401 // Must be a native rate.
402 if (frame->sample_rate_hz_ != kSampleRate8kHz &&
403 frame->sample_rate_hz_ != kSampleRate16kHz &&
404 frame->sample_rate_hz_ != kSampleRate32kHz) {
405 return kBadSampleRateError;
406 }
407 if (echo_control_mobile_->is_enabled() &&
408 frame->sample_rate_hz_ > kSampleRate16kHz) {
409 LOG(LS_ERROR) << "AECM only supports 16 or 8 kHz sample rates";
410 return kUnsupportedComponentError;
411 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000412
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000413 // TODO(ajm): The input and output rates and channels are currently
414 // constrained to be identical in the int16 interface.
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000415 RETURN_ON_ERR(MaybeInitializeLocked(frame->sample_rate_hz_,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000416 frame->sample_rate_hz_,
417 rev_in_format_.rate(),
418 frame->num_channels_,
419 frame->num_channels_,
420 rev_in_format_.num_channels()));
421 if (frame->samples_per_channel_ != fwd_in_format_.samples_per_channel()) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000422 return kBadDataLengthError;
423 }
424
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000425#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
niklase@google.com470e71d2011-07-07 08:21:25 +0000426 if (debug_file_->Open()) {
ajm@google.com808e0e02011-08-03 21:08:51 +0000427 event_msg_->set_type(audioproc::Event::STREAM);
428 audioproc::Stream* msg = event_msg_->mutable_stream();
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000429 const size_t data_size = sizeof(int16_t) *
andrew@webrtc.org63a50982012-05-02 23:56:37 +0000430 frame->samples_per_channel_ *
431 frame->num_channels_;
432 msg->set_input_data(frame->data_, data_size);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000433 }
434#endif
435
436 capture_audio_->DeinterleaveFrom(frame);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000437 RETURN_ON_ERR(ProcessStreamLocked());
438 capture_audio_->InterleaveTo(frame, output_copy_needed(is_data_processed()));
439
440#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
441 if (debug_file_->Open()) {
442 audioproc::Stream* msg = event_msg_->mutable_stream();
443 const size_t data_size = sizeof(int16_t) *
444 frame->samples_per_channel_ *
445 frame->num_channels_;
446 msg->set_output_data(frame->data_, data_size);
447 RETURN_ON_ERR(WriteMessageToDebugFile());
448 }
449#endif
450
451 return kNoError;
452}
453
454
455int AudioProcessingImpl::ProcessStreamLocked() {
456#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
457 if (debug_file_->Open()) {
458 audioproc::Stream* msg = event_msg_->mutable_stream();
ajm@google.com808e0e02011-08-03 21:08:51 +0000459 msg->set_delay(stream_delay_ms_);
460 msg->set_drift(echo_cancellation_->stream_drift_samples());
461 msg->set_level(gain_control_->stream_analog_level());
andrew@webrtc.orgce8e0772014-02-12 15:28:30 +0000462 msg->set_keypress(key_pressed_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000463 }
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000464#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000465
andrew@webrtc.org369166a2012-04-24 18:38:03 +0000466 bool data_processed = is_data_processed();
467 if (analysis_needed(data_processed)) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000468 for (int i = 0; i < fwd_proc_format_.num_channels(); i++) {
469 SplitFilterStates* filter_states = capture_audio_->filter_states(i);
niklase@google.com470e71d2011-07-07 08:21:25 +0000470 // Split into a low and high band.
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000471 WebRtcSpl_AnalysisQMF(capture_audio_->data(i),
472 capture_audio_->samples_per_channel(),
473 capture_audio_->low_pass_split_data(i),
474 capture_audio_->high_pass_split_data(i),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000475 filter_states->analysis_filter_state1,
476 filter_states->analysis_filter_state2);
niklase@google.com470e71d2011-07-07 08:21:25 +0000477 }
478 }
479
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000480 RETURN_ON_ERR(high_pass_filter_->ProcessCaptureAudio(capture_audio_.get()));
481 RETURN_ON_ERR(gain_control_->AnalyzeCaptureAudio(capture_audio_.get()));
482 RETURN_ON_ERR(echo_cancellation_->ProcessCaptureAudio(capture_audio_.get()));
niklase@google.com470e71d2011-07-07 08:21:25 +0000483
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000484 if (echo_control_mobile_->is_enabled() && noise_suppression_->is_enabled()) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000485 capture_audio_->CopyLowPassToReference();
486 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000487 RETURN_ON_ERR(noise_suppression_->ProcessCaptureAudio(capture_audio_.get()));
488 RETURN_ON_ERR(
489 echo_control_mobile_->ProcessCaptureAudio(capture_audio_.get()));
490 RETURN_ON_ERR(voice_detection_->ProcessCaptureAudio(capture_audio_.get()));
491 RETURN_ON_ERR(gain_control_->ProcessCaptureAudio(capture_audio_.get()));
niklase@google.com470e71d2011-07-07 08:21:25 +0000492
andrew@webrtc.org369166a2012-04-24 18:38:03 +0000493 if (synthesis_needed(data_processed)) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000494 for (int i = 0; i < fwd_proc_format_.num_channels(); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000495 // Recombine low and high bands.
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000496 SplitFilterStates* filter_states = capture_audio_->filter_states(i);
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000497 WebRtcSpl_SynthesisQMF(capture_audio_->low_pass_split_data(i),
498 capture_audio_->high_pass_split_data(i),
499 capture_audio_->samples_per_split_channel(),
500 capture_audio_->data(i),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000501 filter_states->synthesis_filter_state1,
502 filter_states->synthesis_filter_state2);
niklase@google.com470e71d2011-07-07 08:21:25 +0000503 }
504 }
505
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000506 // The level estimator operates on the recombined data.
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000507 RETURN_ON_ERR(level_estimator_->ProcessStream(capture_audio_.get()));
ajm@google.com808e0e02011-08-03 21:08:51 +0000508
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000509 was_stream_delay_set_ = false;
niklase@google.com470e71d2011-07-07 08:21:25 +0000510 return kNoError;
511}
512
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000513int AudioProcessingImpl::AnalyzeReverseStream(const float* const* data,
514 int samples_per_channel,
515 int sample_rate_hz,
516 ChannelLayout layout) {
517 CriticalSectionScoped crit_scoped(crit_);
518 if (data == NULL) {
519 return kNullPointerError;
520 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000521
522 const int num_channels = ChannelsFromLayout(layout);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000523 RETURN_ON_ERR(MaybeInitializeLocked(fwd_in_format_.rate(),
524 fwd_out_format_.rate(),
525 sample_rate_hz,
526 fwd_in_format_.num_channels(),
527 fwd_proc_format_.num_channels(),
528 num_channels));
529 if (samples_per_channel != rev_in_format_.samples_per_channel()) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000530 return kBadDataLengthError;
531 }
532
533#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
534 if (debug_file_->Open()) {
535 event_msg_->set_type(audioproc::Event::REVERSE_STREAM);
536 audioproc::ReverseStream* msg = event_msg_->mutable_reverse_stream();
537 const size_t channel_size = sizeof(float) * samples_per_channel;
538 for (int i = 0; i < num_channels; ++i)
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000539 msg->add_channel(data[i], channel_size);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000540 RETURN_ON_ERR(WriteMessageToDebugFile());
541 }
542#endif
543
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000544 render_audio_->CopyFrom(data, samples_per_channel, layout);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000545 return AnalyzeReverseStreamLocked();
546}
547
niklase@google.com470e71d2011-07-07 08:21:25 +0000548int AudioProcessingImpl::AnalyzeReverseStream(AudioFrame* frame) {
andrew@webrtc.org40654032012-01-30 20:51:15 +0000549 CriticalSectionScoped crit_scoped(crit_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000550 if (frame == NULL) {
551 return kNullPointerError;
552 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000553 // Must be a native rate.
554 if (frame->sample_rate_hz_ != kSampleRate8kHz &&
555 frame->sample_rate_hz_ != kSampleRate16kHz &&
556 frame->sample_rate_hz_ != kSampleRate32kHz) {
557 return kBadSampleRateError;
558 }
559 // This interface does not tolerate different forward and reverse rates.
560 if (frame->sample_rate_hz_ != fwd_in_format_.rate()) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000561 return kBadSampleRateError;
562 }
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000563
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000564 RETURN_ON_ERR(MaybeInitializeLocked(fwd_in_format_.rate(),
565 fwd_out_format_.rate(),
566 frame->sample_rate_hz_,
567 fwd_in_format_.num_channels(),
568 fwd_in_format_.num_channels(),
569 frame->num_channels_));
570 if (frame->samples_per_channel_ != rev_in_format_.samples_per_channel()) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000571 return kBadDataLengthError;
572 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000573
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000574#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
niklase@google.com470e71d2011-07-07 08:21:25 +0000575 if (debug_file_->Open()) {
ajm@google.com808e0e02011-08-03 21:08:51 +0000576 event_msg_->set_type(audioproc::Event::REVERSE_STREAM);
577 audioproc::ReverseStream* msg = event_msg_->mutable_reverse_stream();
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000578 const size_t data_size = sizeof(int16_t) *
andrew@webrtc.org63a50982012-05-02 23:56:37 +0000579 frame->samples_per_channel_ *
580 frame->num_channels_;
581 msg->set_data(frame->data_, data_size);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000582 RETURN_ON_ERR(WriteMessageToDebugFile());
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
586 render_audio_->DeinterleaveFrom(frame);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000587 return AnalyzeReverseStreamLocked();
588}
niklase@google.com470e71d2011-07-07 08:21:25 +0000589
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000590// TODO(ajm): Have AnalyzeReverseStream accept sample rates not matching the
591// primary stream and convert ourselves rather than having the user manage it.
592// We can be smarter and use the splitting filter when appropriate. Similarly,
593// perform downmixing here.
594int AudioProcessingImpl::AnalyzeReverseStreamLocked() {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000595 if (rev_proc_format_.rate() == kSampleRate32kHz) {
596 for (int i = 0; i < rev_proc_format_.num_channels(); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000597 // Split into low and high band.
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000598 SplitFilterStates* filter_states = render_audio_->filter_states(i);
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000599 WebRtcSpl_AnalysisQMF(render_audio_->data(i),
600 render_audio_->samples_per_channel(),
601 render_audio_->low_pass_split_data(i),
602 render_audio_->high_pass_split_data(i),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000603 filter_states->analysis_filter_state1,
604 filter_states->analysis_filter_state2);
niklase@google.com470e71d2011-07-07 08:21:25 +0000605 }
606 }
607
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000608 RETURN_ON_ERR(echo_cancellation_->ProcessRenderAudio(render_audio_.get()));
609 RETURN_ON_ERR(echo_control_mobile_->ProcessRenderAudio(render_audio_.get()));
610 RETURN_ON_ERR(gain_control_->ProcessRenderAudio(render_audio_.get()));
niklase@google.com470e71d2011-07-07 08:21:25 +0000611
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000612 return kNoError;
niklase@google.com470e71d2011-07-07 08:21:25 +0000613}
614
615int AudioProcessingImpl::set_stream_delay_ms(int delay) {
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000616 Error retval = kNoError;
niklase@google.com470e71d2011-07-07 08:21:25 +0000617 was_stream_delay_set_ = true;
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000618 delay += delay_offset_ms_;
619
niklase@google.com470e71d2011-07-07 08:21:25 +0000620 if (delay < 0) {
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000621 delay = 0;
622 retval = kBadStreamParameterWarning;
niklase@google.com470e71d2011-07-07 08:21:25 +0000623 }
624
625 // TODO(ajm): the max is rather arbitrarily chosen; investigate.
626 if (delay > 500) {
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000627 delay = 500;
628 retval = kBadStreamParameterWarning;
niklase@google.com470e71d2011-07-07 08:21:25 +0000629 }
630
631 stream_delay_ms_ = delay;
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000632 return retval;
niklase@google.com470e71d2011-07-07 08:21:25 +0000633}
634
635int AudioProcessingImpl::stream_delay_ms() const {
636 return stream_delay_ms_;
637}
638
639bool AudioProcessingImpl::was_stream_delay_set() const {
640 return was_stream_delay_set_;
641}
642
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000643void AudioProcessingImpl::set_stream_key_pressed(bool key_pressed) {
644 key_pressed_ = key_pressed;
645}
646
647bool AudioProcessingImpl::stream_key_pressed() const {
648 return key_pressed_;
649}
650
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000651void AudioProcessingImpl::set_delay_offset_ms(int offset) {
652 CriticalSectionScoped crit_scoped(crit_);
653 delay_offset_ms_ = offset;
654}
655
656int AudioProcessingImpl::delay_offset_ms() const {
657 return delay_offset_ms_;
658}
659
niklase@google.com470e71d2011-07-07 08:21:25 +0000660int AudioProcessingImpl::StartDebugRecording(
661 const char filename[AudioProcessing::kMaxFilenameSize]) {
andrew@webrtc.org40654032012-01-30 20:51:15 +0000662 CriticalSectionScoped crit_scoped(crit_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000663 assert(kMaxFilenameSize == FileWrapper::kMaxFileNameSize);
664
665 if (filename == NULL) {
666 return kNullPointerError;
667 }
668
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000669#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
niklase@google.com470e71d2011-07-07 08:21:25 +0000670 // Stop any ongoing recording.
671 if (debug_file_->Open()) {
672 if (debug_file_->CloseFile() == -1) {
673 return kFileError;
674 }
675 }
676
677 if (debug_file_->OpenFile(filename, false) == -1) {
678 debug_file_->CloseFile();
679 return kFileError;
680 }
681
ajm@google.com808e0e02011-08-03 21:08:51 +0000682 int err = WriteInitMessage();
683 if (err != kNoError) {
684 return err;
niklase@google.com470e71d2011-07-07 08:21:25 +0000685 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000686 return kNoError;
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000687#else
688 return kUnsupportedFunctionError;
689#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
niklase@google.com470e71d2011-07-07 08:21:25 +0000690}
691
henrikg@webrtc.org863b5362013-12-06 16:05:17 +0000692int AudioProcessingImpl::StartDebugRecording(FILE* handle) {
693 CriticalSectionScoped crit_scoped(crit_);
694
695 if (handle == NULL) {
696 return kNullPointerError;
697 }
698
699#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
700 // Stop any ongoing recording.
701 if (debug_file_->Open()) {
702 if (debug_file_->CloseFile() == -1) {
703 return kFileError;
704 }
705 }
706
707 if (debug_file_->OpenFromFileHandle(handle, true, false) == -1) {
708 return kFileError;
709 }
710
711 int err = WriteInitMessage();
712 if (err != kNoError) {
713 return err;
714 }
715 return kNoError;
716#else
717 return kUnsupportedFunctionError;
718#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
719}
720
niklase@google.com470e71d2011-07-07 08:21:25 +0000721int AudioProcessingImpl::StopDebugRecording() {
andrew@webrtc.org40654032012-01-30 20:51:15 +0000722 CriticalSectionScoped crit_scoped(crit_);
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000723
724#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
niklase@google.com470e71d2011-07-07 08:21:25 +0000725 // We just return if recording hasn't started.
726 if (debug_file_->Open()) {
727 if (debug_file_->CloseFile() == -1) {
728 return kFileError;
729 }
730 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000731 return kNoError;
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000732#else
733 return kUnsupportedFunctionError;
734#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
niklase@google.com470e71d2011-07-07 08:21:25 +0000735}
736
737EchoCancellation* AudioProcessingImpl::echo_cancellation() const {
738 return echo_cancellation_;
739}
740
741EchoControlMobile* AudioProcessingImpl::echo_control_mobile() const {
742 return echo_control_mobile_;
743}
744
745GainControl* AudioProcessingImpl::gain_control() const {
746 return gain_control_;
747}
748
749HighPassFilter* AudioProcessingImpl::high_pass_filter() const {
750 return high_pass_filter_;
751}
752
753LevelEstimator* AudioProcessingImpl::level_estimator() const {
754 return level_estimator_;
755}
756
757NoiseSuppression* AudioProcessingImpl::noise_suppression() const {
758 return noise_suppression_;
759}
760
761VoiceDetection* AudioProcessingImpl::voice_detection() const {
762 return voice_detection_;
763}
764
andrew@webrtc.org369166a2012-04-24 18:38:03 +0000765bool AudioProcessingImpl::is_data_processed() const {
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000766 int enabled_count = 0;
767 std::list<ProcessingComponent*>::const_iterator it;
768 for (it = component_list_.begin(); it != component_list_.end(); it++) {
769 if ((*it)->is_component_enabled()) {
770 enabled_count++;
771 }
772 }
773
774 // Data is unchanged if no components are enabled, or if only level_estimator_
775 // or voice_detection_ is enabled.
776 if (enabled_count == 0) {
777 return false;
778 } else if (enabled_count == 1) {
779 if (level_estimator_->is_enabled() || voice_detection_->is_enabled()) {
780 return false;
781 }
782 } else if (enabled_count == 2) {
783 if (level_estimator_->is_enabled() && voice_detection_->is_enabled()) {
784 return false;
785 }
786 }
787 return true;
788}
789
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000790bool AudioProcessingImpl::output_copy_needed(bool is_data_processed) const {
andrew@webrtc.org369166a2012-04-24 18:38:03 +0000791 // Check if we've upmixed or downmixed the audio.
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000792 return ((fwd_proc_format_.num_channels() != fwd_in_format_.num_channels()) ||
793 is_data_processed);
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000794}
795
andrew@webrtc.org369166a2012-04-24 18:38:03 +0000796bool AudioProcessingImpl::synthesis_needed(bool is_data_processed) const {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000797 return (is_data_processed && fwd_proc_format_.rate() == kSampleRate32kHz);
andrew@webrtc.org369166a2012-04-24 18:38:03 +0000798}
799
800bool AudioProcessingImpl::analysis_needed(bool is_data_processed) const {
801 if (!is_data_processed && !voice_detection_->is_enabled()) {
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000802 // Only level_estimator_ is enabled.
803 return false;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000804 } else if (fwd_proc_format_.rate() == kSampleRate32kHz) {
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000805 // Something besides level_estimator_ is enabled, and we have super-wb.
806 return true;
807 }
808 return false;
809}
810
811#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
ajm@google.com808e0e02011-08-03 21:08:51 +0000812int AudioProcessingImpl::WriteMessageToDebugFile() {
813 int32_t size = event_msg_->ByteSize();
814 if (size <= 0) {
815 return kUnspecifiedError;
816 }
andrew@webrtc.org621df672013-10-22 10:27:23 +0000817#if defined(WEBRTC_ARCH_BIG_ENDIAN)
ajm@google.com808e0e02011-08-03 21:08:51 +0000818 // TODO(ajm): Use little-endian "on the wire". For the moment, we can be
819 // pretty safe in assuming little-endian.
820#endif
821
822 if (!event_msg_->SerializeToString(&event_str_)) {
823 return kUnspecifiedError;
824 }
825
826 // Write message preceded by its size.
827 if (!debug_file_->Write(&size, sizeof(int32_t))) {
828 return kFileError;
829 }
830 if (!debug_file_->Write(event_str_.data(), event_str_.length())) {
831 return kFileError;
832 }
833
834 event_msg_->Clear();
835
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000836 return kNoError;
ajm@google.com808e0e02011-08-03 21:08:51 +0000837}
838
839int AudioProcessingImpl::WriteInitMessage() {
840 event_msg_->set_type(audioproc::Event::INIT);
841 audioproc::Init* msg = event_msg_->mutable_init();
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000842 msg->set_sample_rate(fwd_in_format_.rate());
843 msg->set_num_input_channels(fwd_in_format_.num_channels());
844 msg->set_num_output_channels(fwd_proc_format_.num_channels());
845 msg->set_num_reverse_channels(rev_in_format_.num_channels());
846 msg->set_reverse_sample_rate(rev_in_format_.rate());
847 msg->set_output_sample_rate(fwd_out_format_.rate());
ajm@google.com808e0e02011-08-03 21:08:51 +0000848
849 int err = WriteMessageToDebugFile();
850 if (err != kNoError) {
851 return err;
852 }
853
854 return kNoError;
855}
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000856#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000857
niklase@google.com470e71d2011-07-07 08:21:25 +0000858} // namespace webrtc