niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1 | /* |
andrew@webrtc.org | 4065403 | 2012-01-30 20:51:15 +0000 | [diff] [blame] | 2 | * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 3 | * |
| 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.org | 78693fe | 2013-03-01 16:36:19 +0000 | [diff] [blame] | 11 | #include "webrtc/modules/audio_processing/audio_processing_impl.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 12 | |
ajm@google.com | 808e0e0 | 2011-08-03 21:08:51 +0000 | [diff] [blame] | 13 | #include <assert.h> |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 14 | |
xians@webrtc.org | e46bc77 | 2014-10-10 08:36:56 +0000 | [diff] [blame] | 15 | #include "webrtc/base/platform_file.h" |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 16 | #include "webrtc/common_audio/include/audio_util.h" |
andrew@webrtc.org | 60730cf | 2014-01-07 17:45:09 +0000 | [diff] [blame] | 17 | #include "webrtc/common_audio/signal_processing/include/signal_processing_library.h" |
andrew@webrtc.org | 78693fe | 2013-03-01 16:36:19 +0000 | [diff] [blame] | 18 | #include "webrtc/modules/audio_processing/audio_buffer.h" |
aluebs@webrtc.org | 8789376 | 2014-11-27 23:40:25 +0000 | [diff] [blame] | 19 | #include "webrtc/modules/audio_processing/channel_buffer.h" |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 20 | #include "webrtc/modules/audio_processing/common.h" |
andrew@webrtc.org | 56e4a05 | 2014-02-27 22:23:17 +0000 | [diff] [blame] | 21 | #include "webrtc/modules/audio_processing/echo_cancellation_impl.h" |
andrew@webrtc.org | 78693fe | 2013-03-01 16:36:19 +0000 | [diff] [blame] | 22 | #include "webrtc/modules/audio_processing/echo_control_mobile_impl.h" |
| 23 | #include "webrtc/modules/audio_processing/gain_control_impl.h" |
| 24 | #include "webrtc/modules/audio_processing/high_pass_filter_impl.h" |
| 25 | #include "webrtc/modules/audio_processing/level_estimator_impl.h" |
| 26 | #include "webrtc/modules/audio_processing/noise_suppression_impl.h" |
| 27 | #include "webrtc/modules/audio_processing/processing_component.h" |
andrew@webrtc.org | 78693fe | 2013-03-01 16:36:19 +0000 | [diff] [blame] | 28 | #include "webrtc/modules/audio_processing/voice_detection_impl.h" |
| 29 | #include "webrtc/modules/interface/module_common_types.h" |
andrew@webrtc.org | 60730cf | 2014-01-07 17:45:09 +0000 | [diff] [blame] | 30 | #include "webrtc/system_wrappers/interface/compile_assert.h" |
andrew@webrtc.org | 78693fe | 2013-03-01 16:36:19 +0000 | [diff] [blame] | 31 | #include "webrtc/system_wrappers/interface/critical_section_wrapper.h" |
| 32 | #include "webrtc/system_wrappers/interface/file_wrapper.h" |
| 33 | #include "webrtc/system_wrappers/interface/logging.h" |
andrew@webrtc.org | 7bf2646 | 2011-12-03 00:03:31 +0000 | [diff] [blame] | 34 | |
| 35 | #ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP |
| 36 | // Files generated at build-time by the protobuf compiler. |
leozwang@webrtc.org | a373634 | 2012-03-16 21:36:00 +0000 | [diff] [blame] | 37 | #ifdef WEBRTC_ANDROID_PLATFORM_BUILD |
leozwang@webrtc.org | 534e495 | 2012-10-22 21:21:52 +0000 | [diff] [blame] | 38 | #include "external/webrtc/webrtc/modules/audio_processing/debug.pb.h" |
leozwang@google.com | ce9bfbb | 2011-08-03 23:34:31 +0000 | [diff] [blame] | 39 | #else |
ajm@google.com | 808e0e0 | 2011-08-03 21:08:51 +0000 | [diff] [blame] | 40 | #include "webrtc/audio_processing/debug.pb.h" |
leozwang@google.com | ce9bfbb | 2011-08-03 23:34:31 +0000 | [diff] [blame] | 41 | #endif |
andrew@webrtc.org | 7bf2646 | 2011-12-03 00:03:31 +0000 | [diff] [blame] | 42 | #endif // WEBRTC_AUDIOPROC_DEBUG_DUMP |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 43 | |
andrew@webrtc.org | 60730cf | 2014-01-07 17:45:09 +0000 | [diff] [blame] | 44 | #define RETURN_ON_ERR(expr) \ |
| 45 | do { \ |
| 46 | int err = expr; \ |
| 47 | if (err != kNoError) { \ |
| 48 | return err; \ |
| 49 | } \ |
| 50 | } while (0) |
| 51 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 52 | namespace webrtc { |
andrew@webrtc.org | 60730cf | 2014-01-07 17:45:09 +0000 | [diff] [blame] | 53 | |
| 54 | // Throughout webrtc, it's assumed that success is represented by zero. |
| 55 | COMPILE_ASSERT(AudioProcessing::kNoError == 0, no_error_must_be_zero); |
| 56 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 57 | AudioProcessing* AudioProcessing::Create(int id) { |
andrew@webrtc.org | e84978f | 2014-01-25 02:09:06 +0000 | [diff] [blame] | 58 | return Create(); |
| 59 | } |
| 60 | |
| 61 | AudioProcessing* AudioProcessing::Create() { |
| 62 | Config config; |
| 63 | return Create(config); |
| 64 | } |
| 65 | |
| 66 | AudioProcessing* AudioProcessing::Create(const Config& config) { |
| 67 | AudioProcessingImpl* apm = new AudioProcessingImpl(config); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 68 | if (apm->Initialize() != kNoError) { |
| 69 | delete apm; |
| 70 | apm = NULL; |
| 71 | } |
| 72 | |
| 73 | return apm; |
| 74 | } |
| 75 | |
andrew@webrtc.org | e84978f | 2014-01-25 02:09:06 +0000 | [diff] [blame] | 76 | AudioProcessingImpl::AudioProcessingImpl(const Config& config) |
andrew@webrtc.org | 60730cf | 2014-01-07 17:45:09 +0000 | [diff] [blame] | 77 | : echo_cancellation_(NULL), |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 78 | echo_control_mobile_(NULL), |
| 79 | gain_control_(NULL), |
| 80 | high_pass_filter_(NULL), |
| 81 | level_estimator_(NULL), |
| 82 | noise_suppression_(NULL), |
| 83 | voice_detection_(NULL), |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 84 | crit_(CriticalSectionWrapper::CreateCriticalSection()), |
andrew@webrtc.org | 7bf2646 | 2011-12-03 00:03:31 +0000 | [diff] [blame] | 85 | #ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP |
| 86 | debug_file_(FileWrapper::Create()), |
| 87 | event_msg_(new audioproc::Event()), |
| 88 | #endif |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 89 | fwd_in_format_(kSampleRate16kHz, 1), |
aluebs@webrtc.org | 27d106b | 2014-12-11 17:09:21 +0000 | [diff] [blame] | 90 | fwd_proc_format_(kSampleRate16kHz), |
| 91 | fwd_out_format_(kSampleRate16kHz, 1), |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 92 | rev_in_format_(kSampleRate16kHz, 1), |
| 93 | rev_proc_format_(kSampleRate16kHz, 1), |
| 94 | split_rate_(kSampleRate16kHz), |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 95 | stream_delay_ms_(0), |
andrew@webrtc.org | 6f9f817 | 2012-03-06 19:03:39 +0000 | [diff] [blame] | 96 | delay_offset_ms_(0), |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 97 | was_stream_delay_set_(false), |
andrew@webrtc.org | 38bf249 | 2014-02-13 17:43:44 +0000 | [diff] [blame] | 98 | output_will_be_muted_(false), |
andrew@webrtc.org | 07b5950 | 2014-02-12 16:41:13 +0000 | [diff] [blame] | 99 | key_pressed_(false) { |
andrew@webrtc.org | 56e4a05 | 2014-02-27 22:23:17 +0000 | [diff] [blame] | 100 | echo_cancellation_ = new EchoCancellationImpl(this, crit_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 101 | component_list_.push_back(echo_cancellation_); |
| 102 | |
andrew@webrtc.org | 56e4a05 | 2014-02-27 22:23:17 +0000 | [diff] [blame] | 103 | echo_control_mobile_ = new EchoControlMobileImpl(this, crit_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 104 | component_list_.push_back(echo_control_mobile_); |
| 105 | |
andrew@webrtc.org | 56e4a05 | 2014-02-27 22:23:17 +0000 | [diff] [blame] | 106 | gain_control_ = new GainControlImpl(this, crit_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 107 | component_list_.push_back(gain_control_); |
| 108 | |
andrew@webrtc.org | 56e4a05 | 2014-02-27 22:23:17 +0000 | [diff] [blame] | 109 | high_pass_filter_ = new HighPassFilterImpl(this, crit_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 110 | component_list_.push_back(high_pass_filter_); |
| 111 | |
andrew@webrtc.org | 56e4a05 | 2014-02-27 22:23:17 +0000 | [diff] [blame] | 112 | level_estimator_ = new LevelEstimatorImpl(this, crit_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 113 | component_list_.push_back(level_estimator_); |
| 114 | |
andrew@webrtc.org | 56e4a05 | 2014-02-27 22:23:17 +0000 | [diff] [blame] | 115 | noise_suppression_ = new NoiseSuppressionImpl(this, crit_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 116 | component_list_.push_back(noise_suppression_); |
| 117 | |
andrew@webrtc.org | 56e4a05 | 2014-02-27 22:23:17 +0000 | [diff] [blame] | 118 | voice_detection_ = new VoiceDetectionImpl(this, crit_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 119 | component_list_.push_back(voice_detection_); |
andrew@webrtc.org | e84978f | 2014-01-25 02:09:06 +0000 | [diff] [blame] | 120 | |
| 121 | SetExtraOptions(config); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | AudioProcessingImpl::~AudioProcessingImpl() { |
andrew@webrtc.org | 8186534 | 2012-10-27 00:28:27 +0000 | [diff] [blame] | 125 | { |
| 126 | CriticalSectionScoped crit_scoped(crit_); |
| 127 | while (!component_list_.empty()) { |
| 128 | ProcessingComponent* component = component_list_.front(); |
| 129 | component->Destroy(); |
| 130 | delete component; |
| 131 | component_list_.pop_front(); |
| 132 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 133 | |
andrew@webrtc.org | 7bf2646 | 2011-12-03 00:03:31 +0000 | [diff] [blame] | 134 | #ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP |
andrew@webrtc.org | 8186534 | 2012-10-27 00:28:27 +0000 | [diff] [blame] | 135 | if (debug_file_->Open()) { |
| 136 | debug_file_->CloseFile(); |
| 137 | } |
andrew@webrtc.org | 7bf2646 | 2011-12-03 00:03:31 +0000 | [diff] [blame] | 138 | #endif |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 139 | } |
andrew@webrtc.org | 16cfbe2 | 2012-08-29 16:58:25 +0000 | [diff] [blame] | 140 | delete crit_; |
| 141 | crit_ = NULL; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 142 | } |
| 143 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 144 | int AudioProcessingImpl::Initialize() { |
andrew@webrtc.org | 4065403 | 2012-01-30 20:51:15 +0000 | [diff] [blame] | 145 | CriticalSectionScoped crit_scoped(crit_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 146 | return InitializeLocked(); |
| 147 | } |
| 148 | |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 149 | int AudioProcessingImpl::set_sample_rate_hz(int rate) { |
andrew@webrtc.org | a8b9737 | 2014-03-10 22:26:12 +0000 | [diff] [blame] | 150 | CriticalSectionScoped crit_scoped(crit_); |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 151 | return InitializeLocked(rate, |
| 152 | rate, |
| 153 | rev_in_format_.rate(), |
| 154 | fwd_in_format_.num_channels(), |
aluebs@webrtc.org | 27d106b | 2014-12-11 17:09:21 +0000 | [diff] [blame] | 155 | fwd_out_format_.num_channels(), |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 156 | rev_in_format_.num_channels()); |
| 157 | } |
| 158 | |
| 159 | int AudioProcessingImpl::Initialize(int input_sample_rate_hz, |
| 160 | int output_sample_rate_hz, |
| 161 | int reverse_sample_rate_hz, |
| 162 | ChannelLayout input_layout, |
| 163 | ChannelLayout output_layout, |
| 164 | ChannelLayout reverse_layout) { |
| 165 | CriticalSectionScoped crit_scoped(crit_); |
| 166 | return InitializeLocked(input_sample_rate_hz, |
| 167 | output_sample_rate_hz, |
andrew@webrtc.org | a8b9737 | 2014-03-10 22:26:12 +0000 | [diff] [blame] | 168 | reverse_sample_rate_hz, |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 169 | ChannelsFromLayout(input_layout), |
| 170 | ChannelsFromLayout(output_layout), |
| 171 | ChannelsFromLayout(reverse_layout)); |
andrew@webrtc.org | a8b9737 | 2014-03-10 22:26:12 +0000 | [diff] [blame] | 172 | } |
| 173 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 174 | int AudioProcessingImpl::InitializeLocked() { |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 175 | render_audio_.reset(new AudioBuffer(rev_in_format_.samples_per_channel(), |
| 176 | rev_in_format_.num_channels(), |
| 177 | rev_proc_format_.samples_per_channel(), |
| 178 | rev_proc_format_.num_channels(), |
| 179 | rev_proc_format_.samples_per_channel())); |
| 180 | capture_audio_.reset(new AudioBuffer(fwd_in_format_.samples_per_channel(), |
| 181 | fwd_in_format_.num_channels(), |
| 182 | fwd_proc_format_.samples_per_channel(), |
aluebs@webrtc.org | 27d106b | 2014-12-11 17:09:21 +0000 | [diff] [blame] | 183 | fwd_out_format_.num_channels(), |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 184 | fwd_out_format_.samples_per_channel())); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 185 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 186 | // Initialize all components. |
| 187 | std::list<ProcessingComponent*>::iterator it; |
andrew@webrtc.org | 8186534 | 2012-10-27 00:28:27 +0000 | [diff] [blame] | 188 | for (it = component_list_.begin(); it != component_list_.end(); ++it) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 189 | int err = (*it)->Initialize(); |
| 190 | if (err != kNoError) { |
| 191 | return err; |
| 192 | } |
| 193 | } |
| 194 | |
andrew@webrtc.org | 7bf2646 | 2011-12-03 00:03:31 +0000 | [diff] [blame] | 195 | #ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP |
ajm@google.com | 808e0e0 | 2011-08-03 21:08:51 +0000 | [diff] [blame] | 196 | if (debug_file_->Open()) { |
| 197 | int err = WriteInitMessage(); |
| 198 | if (err != kNoError) { |
| 199 | return err; |
| 200 | } |
| 201 | } |
andrew@webrtc.org | 7bf2646 | 2011-12-03 00:03:31 +0000 | [diff] [blame] | 202 | #endif |
ajm@google.com | 808e0e0 | 2011-08-03 21:08:51 +0000 | [diff] [blame] | 203 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 204 | return kNoError; |
| 205 | } |
| 206 | |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 207 | int AudioProcessingImpl::InitializeLocked(int input_sample_rate_hz, |
| 208 | int output_sample_rate_hz, |
andrew@webrtc.org | a8b9737 | 2014-03-10 22:26:12 +0000 | [diff] [blame] | 209 | int reverse_sample_rate_hz, |
| 210 | int num_input_channels, |
| 211 | int num_output_channels, |
| 212 | int num_reverse_channels) { |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 213 | if (input_sample_rate_hz <= 0 || |
| 214 | output_sample_rate_hz <= 0 || |
| 215 | reverse_sample_rate_hz <= 0) { |
andrew@webrtc.org | a8b9737 | 2014-03-10 22:26:12 +0000 | [diff] [blame] | 216 | return kBadSampleRateError; |
| 217 | } |
| 218 | if (num_output_channels > num_input_channels) { |
| 219 | return kBadNumberChannelsError; |
| 220 | } |
| 221 | // Only mono and stereo supported currently. |
| 222 | if (num_input_channels > 2 || num_input_channels < 1 || |
| 223 | num_output_channels > 2 || num_output_channels < 1 || |
| 224 | num_reverse_channels > 2 || num_reverse_channels < 1) { |
| 225 | return kBadNumberChannelsError; |
| 226 | } |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 227 | |
| 228 | fwd_in_format_.set(input_sample_rate_hz, num_input_channels); |
aluebs@webrtc.org | 27d106b | 2014-12-11 17:09:21 +0000 | [diff] [blame] | 229 | fwd_out_format_.set(output_sample_rate_hz, num_output_channels); |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 230 | rev_in_format_.set(reverse_sample_rate_hz, num_reverse_channels); |
| 231 | |
| 232 | // We process at the closest native rate >= min(input rate, output rate)... |
| 233 | int min_proc_rate = std::min(fwd_in_format_.rate(), fwd_out_format_.rate()); |
| 234 | int fwd_proc_rate; |
| 235 | if (min_proc_rate > kSampleRate16kHz) { |
| 236 | fwd_proc_rate = kSampleRate32kHz; |
| 237 | } else if (min_proc_rate > kSampleRate8kHz) { |
| 238 | fwd_proc_rate = kSampleRate16kHz; |
| 239 | } else { |
| 240 | fwd_proc_rate = kSampleRate8kHz; |
| 241 | } |
| 242 | // ...with one exception. |
| 243 | if (echo_control_mobile_->is_enabled() && min_proc_rate > kSampleRate16kHz) { |
| 244 | fwd_proc_rate = kSampleRate16kHz; |
andrew@webrtc.org | a8b9737 | 2014-03-10 22:26:12 +0000 | [diff] [blame] | 245 | } |
| 246 | |
aluebs@webrtc.org | 27d106b | 2014-12-11 17:09:21 +0000 | [diff] [blame] | 247 | fwd_proc_format_.set(fwd_proc_rate); |
andrew@webrtc.org | a8b9737 | 2014-03-10 22:26:12 +0000 | [diff] [blame] | 248 | |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 249 | // We normally process the reverse stream at 16 kHz. Unless... |
| 250 | int rev_proc_rate = kSampleRate16kHz; |
| 251 | if (fwd_proc_format_.rate() == kSampleRate8kHz) { |
| 252 | // ...the forward stream is at 8 kHz. |
| 253 | rev_proc_rate = kSampleRate8kHz; |
andrew@webrtc.org | a8b9737 | 2014-03-10 22:26:12 +0000 | [diff] [blame] | 254 | } else { |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 255 | if (rev_in_format_.rate() == kSampleRate32kHz) { |
| 256 | // ...or the input is at 32 kHz, in which case we use the splitting |
| 257 | // filter rather than the resampler. |
| 258 | rev_proc_rate = kSampleRate32kHz; |
| 259 | } |
| 260 | } |
| 261 | |
andrew@webrtc.org | 30be827 | 2014-09-24 20:06:23 +0000 | [diff] [blame] | 262 | // Always downmix the reverse stream to mono for analysis. This has been |
| 263 | // demonstrated to work well for AEC in most practical scenarios. |
| 264 | rev_proc_format_.set(rev_proc_rate, 1); |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 265 | |
aluebs@webrtc.org | 087da13 | 2014-11-17 23:01:23 +0000 | [diff] [blame] | 266 | if (fwd_proc_format_.rate() == kSampleRate32kHz || |
| 267 | fwd_proc_format_.rate() == kSampleRate48kHz) { |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 268 | split_rate_ = kSampleRate16kHz; |
| 269 | } else { |
| 270 | split_rate_ = fwd_proc_format_.rate(); |
andrew@webrtc.org | a8b9737 | 2014-03-10 22:26:12 +0000 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | return InitializeLocked(); |
| 274 | } |
| 275 | |
| 276 | // Calls InitializeLocked() if any of the audio parameters have changed from |
| 277 | // their current values. |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 278 | int AudioProcessingImpl::MaybeInitializeLocked(int input_sample_rate_hz, |
| 279 | int output_sample_rate_hz, |
andrew@webrtc.org | a8b9737 | 2014-03-10 22:26:12 +0000 | [diff] [blame] | 280 | int reverse_sample_rate_hz, |
| 281 | int num_input_channels, |
| 282 | int num_output_channels, |
| 283 | int num_reverse_channels) { |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 284 | if (input_sample_rate_hz == fwd_in_format_.rate() && |
| 285 | output_sample_rate_hz == fwd_out_format_.rate() && |
| 286 | reverse_sample_rate_hz == rev_in_format_.rate() && |
| 287 | num_input_channels == fwd_in_format_.num_channels() && |
aluebs@webrtc.org | 27d106b | 2014-12-11 17:09:21 +0000 | [diff] [blame] | 288 | num_output_channels == fwd_out_format_.num_channels() && |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 289 | num_reverse_channels == rev_in_format_.num_channels()) { |
andrew@webrtc.org | a8b9737 | 2014-03-10 22:26:12 +0000 | [diff] [blame] | 290 | return kNoError; |
| 291 | } |
| 292 | |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 293 | return InitializeLocked(input_sample_rate_hz, |
| 294 | output_sample_rate_hz, |
andrew@webrtc.org | a8b9737 | 2014-03-10 22:26:12 +0000 | [diff] [blame] | 295 | reverse_sample_rate_hz, |
| 296 | num_input_channels, |
| 297 | num_output_channels, |
| 298 | num_reverse_channels); |
| 299 | } |
| 300 | |
andrew@webrtc.org | 61e596f | 2013-07-25 18:28:29 +0000 | [diff] [blame] | 301 | void AudioProcessingImpl::SetExtraOptions(const Config& config) { |
andrew@webrtc.org | e84978f | 2014-01-25 02:09:06 +0000 | [diff] [blame] | 302 | CriticalSectionScoped crit_scoped(crit_); |
andrew@webrtc.org | 61e596f | 2013-07-25 18:28:29 +0000 | [diff] [blame] | 303 | std::list<ProcessingComponent*>::iterator it; |
| 304 | for (it = component_list_.begin(); it != component_list_.end(); ++it) |
| 305 | (*it)->SetExtraOptions(config); |
| 306 | } |
| 307 | |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 308 | int AudioProcessingImpl::input_sample_rate_hz() const { |
andrew@webrtc.org | 4065403 | 2012-01-30 20:51:15 +0000 | [diff] [blame] | 309 | CriticalSectionScoped crit_scoped(crit_); |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 310 | return fwd_in_format_.rate(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 311 | } |
| 312 | |
andrew@webrtc.org | 46b31b1 | 2014-04-23 03:33:54 +0000 | [diff] [blame] | 313 | int AudioProcessingImpl::sample_rate_hz() const { |
| 314 | CriticalSectionScoped crit_scoped(crit_); |
| 315 | return fwd_in_format_.rate(); |
| 316 | } |
| 317 | |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 318 | int AudioProcessingImpl::proc_sample_rate_hz() const { |
| 319 | return fwd_proc_format_.rate(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 320 | } |
| 321 | |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 322 | int AudioProcessingImpl::proc_split_sample_rate_hz() const { |
| 323 | return split_rate_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | int AudioProcessingImpl::num_reverse_channels() const { |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 327 | return rev_proc_format_.num_channels(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | int AudioProcessingImpl::num_input_channels() const { |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 331 | return fwd_in_format_.num_channels(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | int AudioProcessingImpl::num_output_channels() const { |
aluebs@webrtc.org | 27d106b | 2014-12-11 17:09:21 +0000 | [diff] [blame] | 335 | return fwd_out_format_.num_channels(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 336 | } |
| 337 | |
andrew@webrtc.org | 17342e5 | 2014-02-12 22:28:31 +0000 | [diff] [blame] | 338 | void AudioProcessingImpl::set_output_will_be_muted(bool muted) { |
| 339 | output_will_be_muted_ = muted; |
| 340 | } |
| 341 | |
| 342 | bool AudioProcessingImpl::output_will_be_muted() const { |
| 343 | return output_will_be_muted_; |
| 344 | } |
| 345 | |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 346 | int AudioProcessingImpl::ProcessStream(const float* const* src, |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 347 | int samples_per_channel, |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 348 | int input_sample_rate_hz, |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 349 | ChannelLayout input_layout, |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 350 | int output_sample_rate_hz, |
| 351 | ChannelLayout output_layout, |
| 352 | float* const* dest) { |
andrew@webrtc.org | 4065403 | 2012-01-30 20:51:15 +0000 | [diff] [blame] | 353 | CriticalSectionScoped crit_scoped(crit_); |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 354 | if (!src || !dest) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 355 | return kNullPointerError; |
| 356 | } |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 357 | |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 358 | RETURN_ON_ERR(MaybeInitializeLocked(input_sample_rate_hz, |
| 359 | output_sample_rate_hz, |
| 360 | rev_in_format_.rate(), |
| 361 | ChannelsFromLayout(input_layout), |
| 362 | ChannelsFromLayout(output_layout), |
| 363 | rev_in_format_.num_channels())); |
| 364 | if (samples_per_channel != fwd_in_format_.samples_per_channel()) { |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 365 | return kBadDataLengthError; |
| 366 | } |
| 367 | |
| 368 | #ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP |
| 369 | if (debug_file_->Open()) { |
| 370 | event_msg_->set_type(audioproc::Event::STREAM); |
| 371 | audioproc::Stream* msg = event_msg_->mutable_stream(); |
aluebs@webrtc.org | 59a1b1b | 2014-08-28 10:43:09 +0000 | [diff] [blame] | 372 | const size_t channel_size = |
| 373 | sizeof(float) * fwd_in_format_.samples_per_channel(); |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 374 | for (int i = 0; i < fwd_in_format_.num_channels(); ++i) |
| 375 | msg->add_input_channel(src[i], channel_size); |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 376 | } |
| 377 | #endif |
| 378 | |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 379 | capture_audio_->CopyFrom(src, samples_per_channel, input_layout); |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 380 | RETURN_ON_ERR(ProcessStreamLocked()); |
| 381 | if (output_copy_needed(is_data_processed())) { |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 382 | capture_audio_->CopyTo(fwd_out_format_.samples_per_channel(), |
| 383 | output_layout, |
| 384 | dest); |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 385 | } |
| 386 | |
| 387 | #ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP |
| 388 | if (debug_file_->Open()) { |
| 389 | audioproc::Stream* msg = event_msg_->mutable_stream(); |
aluebs@webrtc.org | 59a1b1b | 2014-08-28 10:43:09 +0000 | [diff] [blame] | 390 | const size_t channel_size = |
| 391 | sizeof(float) * fwd_out_format_.samples_per_channel(); |
aluebs@webrtc.org | 27d106b | 2014-12-11 17:09:21 +0000 | [diff] [blame] | 392 | for (int i = 0; i < fwd_out_format_.num_channels(); ++i) |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 393 | msg->add_output_channel(dest[i], channel_size); |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 394 | RETURN_ON_ERR(WriteMessageToDebugFile()); |
| 395 | } |
| 396 | #endif |
| 397 | |
| 398 | return kNoError; |
| 399 | } |
| 400 | |
| 401 | int AudioProcessingImpl::ProcessStream(AudioFrame* frame) { |
| 402 | CriticalSectionScoped crit_scoped(crit_); |
| 403 | if (!frame) { |
| 404 | return kNullPointerError; |
| 405 | } |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 406 | // Must be a native rate. |
| 407 | if (frame->sample_rate_hz_ != kSampleRate8kHz && |
| 408 | frame->sample_rate_hz_ != kSampleRate16kHz && |
aluebs@webrtc.org | 087da13 | 2014-11-17 23:01:23 +0000 | [diff] [blame] | 409 | frame->sample_rate_hz_ != kSampleRate32kHz && |
| 410 | frame->sample_rate_hz_ != kSampleRate48kHz) { |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 411 | return kBadSampleRateError; |
| 412 | } |
| 413 | if (echo_control_mobile_->is_enabled() && |
| 414 | frame->sample_rate_hz_ > kSampleRate16kHz) { |
| 415 | LOG(LS_ERROR) << "AECM only supports 16 or 8 kHz sample rates"; |
| 416 | return kUnsupportedComponentError; |
| 417 | } |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 418 | |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 419 | // TODO(ajm): The input and output rates and channels are currently |
| 420 | // constrained to be identical in the int16 interface. |
andrew@webrtc.org | 60730cf | 2014-01-07 17:45:09 +0000 | [diff] [blame] | 421 | RETURN_ON_ERR(MaybeInitializeLocked(frame->sample_rate_hz_, |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 422 | frame->sample_rate_hz_, |
| 423 | rev_in_format_.rate(), |
| 424 | frame->num_channels_, |
| 425 | frame->num_channels_, |
| 426 | rev_in_format_.num_channels())); |
| 427 | if (frame->samples_per_channel_ != fwd_in_format_.samples_per_channel()) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 428 | return kBadDataLengthError; |
| 429 | } |
| 430 | |
andrew@webrtc.org | 7bf2646 | 2011-12-03 00:03:31 +0000 | [diff] [blame] | 431 | #ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 432 | if (debug_file_->Open()) { |
ajm@google.com | 808e0e0 | 2011-08-03 21:08:51 +0000 | [diff] [blame] | 433 | event_msg_->set_type(audioproc::Event::STREAM); |
| 434 | audioproc::Stream* msg = event_msg_->mutable_stream(); |
andrew@webrtc.org | 755b04a | 2011-11-15 16:57:56 +0000 | [diff] [blame] | 435 | const size_t data_size = sizeof(int16_t) * |
andrew@webrtc.org | 63a5098 | 2012-05-02 23:56:37 +0000 | [diff] [blame] | 436 | frame->samples_per_channel_ * |
| 437 | frame->num_channels_; |
| 438 | msg->set_input_data(frame->data_, data_size); |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 439 | } |
| 440 | #endif |
| 441 | |
| 442 | capture_audio_->DeinterleaveFrom(frame); |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 443 | RETURN_ON_ERR(ProcessStreamLocked()); |
| 444 | capture_audio_->InterleaveTo(frame, output_copy_needed(is_data_processed())); |
| 445 | |
| 446 | #ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP |
| 447 | if (debug_file_->Open()) { |
| 448 | audioproc::Stream* msg = event_msg_->mutable_stream(); |
| 449 | const size_t data_size = sizeof(int16_t) * |
| 450 | frame->samples_per_channel_ * |
| 451 | frame->num_channels_; |
| 452 | msg->set_output_data(frame->data_, data_size); |
| 453 | RETURN_ON_ERR(WriteMessageToDebugFile()); |
| 454 | } |
| 455 | #endif |
| 456 | |
| 457 | return kNoError; |
| 458 | } |
| 459 | |
| 460 | |
| 461 | int AudioProcessingImpl::ProcessStreamLocked() { |
| 462 | #ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP |
| 463 | if (debug_file_->Open()) { |
| 464 | audioproc::Stream* msg = event_msg_->mutable_stream(); |
ajm@google.com | 808e0e0 | 2011-08-03 21:08:51 +0000 | [diff] [blame] | 465 | msg->set_delay(stream_delay_ms_); |
| 466 | msg->set_drift(echo_cancellation_->stream_drift_samples()); |
| 467 | msg->set_level(gain_control_->stream_analog_level()); |
andrew@webrtc.org | ce8e077 | 2014-02-12 15:28:30 +0000 | [diff] [blame] | 468 | msg->set_keypress(key_pressed_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 469 | } |
andrew@webrtc.org | 7bf2646 | 2011-12-03 00:03:31 +0000 | [diff] [blame] | 470 | #endif |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 471 | |
andrew@webrtc.org | 103657b | 2014-04-24 18:28:56 +0000 | [diff] [blame] | 472 | AudioBuffer* ca = capture_audio_.get(); // For brevity. |
andrew@webrtc.org | 369166a | 2012-04-24 18:38:03 +0000 | [diff] [blame] | 473 | bool data_processed = is_data_processed(); |
| 474 | if (analysis_needed(data_processed)) { |
aluebs@webrtc.org | be05c74 | 2014-11-14 22:18:10 +0000 | [diff] [blame] | 475 | ca->SplitIntoFrequencyBands(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 476 | } |
| 477 | |
andrew@webrtc.org | 103657b | 2014-04-24 18:28:56 +0000 | [diff] [blame] | 478 | RETURN_ON_ERR(high_pass_filter_->ProcessCaptureAudio(ca)); |
| 479 | RETURN_ON_ERR(gain_control_->AnalyzeCaptureAudio(ca)); |
aluebs@webrtc.org | a0ce9fa | 2014-09-24 14:18:03 +0000 | [diff] [blame] | 480 | RETURN_ON_ERR(noise_suppression_->AnalyzeCaptureAudio(ca)); |
andrew@webrtc.org | 103657b | 2014-04-24 18:28:56 +0000 | [diff] [blame] | 481 | RETURN_ON_ERR(echo_cancellation_->ProcessCaptureAudio(ca)); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 482 | |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 483 | if (echo_control_mobile_->is_enabled() && noise_suppression_->is_enabled()) { |
andrew@webrtc.org | 103657b | 2014-04-24 18:28:56 +0000 | [diff] [blame] | 484 | ca->CopyLowPassToReference(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 485 | } |
andrew@webrtc.org | 103657b | 2014-04-24 18:28:56 +0000 | [diff] [blame] | 486 | RETURN_ON_ERR(noise_suppression_->ProcessCaptureAudio(ca)); |
| 487 | RETURN_ON_ERR(echo_control_mobile_->ProcessCaptureAudio(ca)); |
| 488 | RETURN_ON_ERR(voice_detection_->ProcessCaptureAudio(ca)); |
| 489 | RETURN_ON_ERR(gain_control_->ProcessCaptureAudio(ca)); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 490 | |
andrew@webrtc.org | 369166a | 2012-04-24 18:38:03 +0000 | [diff] [blame] | 491 | if (synthesis_needed(data_processed)) { |
aluebs@webrtc.org | be05c74 | 2014-11-14 22:18:10 +0000 | [diff] [blame] | 492 | ca->MergeFrequencyBands(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 493 | } |
| 494 | |
andrew@webrtc.org | 755b04a | 2011-11-15 16:57:56 +0000 | [diff] [blame] | 495 | // The level estimator operates on the recombined data. |
andrew@webrtc.org | 103657b | 2014-04-24 18:28:56 +0000 | [diff] [blame] | 496 | RETURN_ON_ERR(level_estimator_->ProcessStream(ca)); |
ajm@google.com | 808e0e0 | 2011-08-03 21:08:51 +0000 | [diff] [blame] | 497 | |
andrew@webrtc.org | 1e91693 | 2011-11-29 18:28:57 +0000 | [diff] [blame] | 498 | was_stream_delay_set_ = false; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 499 | return kNoError; |
| 500 | } |
| 501 | |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 502 | int AudioProcessingImpl::AnalyzeReverseStream(const float* const* data, |
| 503 | int samples_per_channel, |
| 504 | int sample_rate_hz, |
| 505 | ChannelLayout layout) { |
| 506 | CriticalSectionScoped crit_scoped(crit_); |
| 507 | if (data == NULL) { |
| 508 | return kNullPointerError; |
| 509 | } |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 510 | |
| 511 | const int num_channels = ChannelsFromLayout(layout); |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 512 | RETURN_ON_ERR(MaybeInitializeLocked(fwd_in_format_.rate(), |
| 513 | fwd_out_format_.rate(), |
| 514 | sample_rate_hz, |
| 515 | fwd_in_format_.num_channels(), |
aluebs@webrtc.org | 27d106b | 2014-12-11 17:09:21 +0000 | [diff] [blame] | 516 | fwd_out_format_.num_channels(), |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 517 | num_channels)); |
| 518 | if (samples_per_channel != rev_in_format_.samples_per_channel()) { |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 519 | return kBadDataLengthError; |
| 520 | } |
| 521 | |
| 522 | #ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP |
| 523 | if (debug_file_->Open()) { |
| 524 | event_msg_->set_type(audioproc::Event::REVERSE_STREAM); |
| 525 | audioproc::ReverseStream* msg = event_msg_->mutable_reverse_stream(); |
aluebs@webrtc.org | 59a1b1b | 2014-08-28 10:43:09 +0000 | [diff] [blame] | 526 | const size_t channel_size = |
| 527 | sizeof(float) * rev_in_format_.samples_per_channel(); |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 528 | for (int i = 0; i < num_channels; ++i) |
andrew@webrtc.org | a8b9737 | 2014-03-10 22:26:12 +0000 | [diff] [blame] | 529 | msg->add_channel(data[i], channel_size); |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 530 | RETURN_ON_ERR(WriteMessageToDebugFile()); |
| 531 | } |
| 532 | #endif |
| 533 | |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 534 | render_audio_->CopyFrom(data, samples_per_channel, layout); |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 535 | return AnalyzeReverseStreamLocked(); |
| 536 | } |
| 537 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 538 | int AudioProcessingImpl::AnalyzeReverseStream(AudioFrame* frame) { |
andrew@webrtc.org | 4065403 | 2012-01-30 20:51:15 +0000 | [diff] [blame] | 539 | CriticalSectionScoped crit_scoped(crit_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 540 | if (frame == NULL) { |
| 541 | return kNullPointerError; |
| 542 | } |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 543 | // Must be a native rate. |
| 544 | if (frame->sample_rate_hz_ != kSampleRate8kHz && |
| 545 | frame->sample_rate_hz_ != kSampleRate16kHz && |
aluebs@webrtc.org | 087da13 | 2014-11-17 23:01:23 +0000 | [diff] [blame] | 546 | frame->sample_rate_hz_ != kSampleRate32kHz && |
| 547 | frame->sample_rate_hz_ != kSampleRate48kHz) { |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 548 | return kBadSampleRateError; |
| 549 | } |
| 550 | // This interface does not tolerate different forward and reverse rates. |
| 551 | if (frame->sample_rate_hz_ != fwd_in_format_.rate()) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 552 | return kBadSampleRateError; |
| 553 | } |
andrew@webrtc.org | a8b9737 | 2014-03-10 22:26:12 +0000 | [diff] [blame] | 554 | |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 555 | RETURN_ON_ERR(MaybeInitializeLocked(fwd_in_format_.rate(), |
| 556 | fwd_out_format_.rate(), |
| 557 | frame->sample_rate_hz_, |
| 558 | fwd_in_format_.num_channels(), |
| 559 | fwd_in_format_.num_channels(), |
| 560 | frame->num_channels_)); |
| 561 | if (frame->samples_per_channel_ != rev_in_format_.samples_per_channel()) { |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 562 | return kBadDataLengthError; |
| 563 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 564 | |
andrew@webrtc.org | 7bf2646 | 2011-12-03 00:03:31 +0000 | [diff] [blame] | 565 | #ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 566 | if (debug_file_->Open()) { |
ajm@google.com | 808e0e0 | 2011-08-03 21:08:51 +0000 | [diff] [blame] | 567 | event_msg_->set_type(audioproc::Event::REVERSE_STREAM); |
| 568 | audioproc::ReverseStream* msg = event_msg_->mutable_reverse_stream(); |
andrew@webrtc.org | 755b04a | 2011-11-15 16:57:56 +0000 | [diff] [blame] | 569 | const size_t data_size = sizeof(int16_t) * |
andrew@webrtc.org | 63a5098 | 2012-05-02 23:56:37 +0000 | [diff] [blame] | 570 | frame->samples_per_channel_ * |
| 571 | frame->num_channels_; |
| 572 | msg->set_data(frame->data_, data_size); |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 573 | RETURN_ON_ERR(WriteMessageToDebugFile()); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 574 | } |
andrew@webrtc.org | 7bf2646 | 2011-12-03 00:03:31 +0000 | [diff] [blame] | 575 | #endif |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 576 | |
| 577 | render_audio_->DeinterleaveFrom(frame); |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 578 | return AnalyzeReverseStreamLocked(); |
| 579 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 580 | |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 581 | int AudioProcessingImpl::AnalyzeReverseStreamLocked() { |
andrew@webrtc.org | 103657b | 2014-04-24 18:28:56 +0000 | [diff] [blame] | 582 | AudioBuffer* ra = render_audio_.get(); // For brevity. |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 583 | if (rev_proc_format_.rate() == kSampleRate32kHz) { |
aluebs@webrtc.org | be05c74 | 2014-11-14 22:18:10 +0000 | [diff] [blame] | 584 | ra->SplitIntoFrequencyBands(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 585 | } |
| 586 | |
andrew@webrtc.org | 103657b | 2014-04-24 18:28:56 +0000 | [diff] [blame] | 587 | RETURN_ON_ERR(echo_cancellation_->ProcessRenderAudio(ra)); |
| 588 | RETURN_ON_ERR(echo_control_mobile_->ProcessRenderAudio(ra)); |
| 589 | RETURN_ON_ERR(gain_control_->ProcessRenderAudio(ra)); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 590 | |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 591 | return kNoError; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 592 | } |
| 593 | |
| 594 | int AudioProcessingImpl::set_stream_delay_ms(int delay) { |
andrew@webrtc.org | 5f23d64 | 2012-05-29 21:14:06 +0000 | [diff] [blame] | 595 | Error retval = kNoError; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 596 | was_stream_delay_set_ = true; |
andrew@webrtc.org | 6f9f817 | 2012-03-06 19:03:39 +0000 | [diff] [blame] | 597 | delay += delay_offset_ms_; |
| 598 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 599 | if (delay < 0) { |
andrew@webrtc.org | 5f23d64 | 2012-05-29 21:14:06 +0000 | [diff] [blame] | 600 | delay = 0; |
| 601 | retval = kBadStreamParameterWarning; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 602 | } |
| 603 | |
| 604 | // TODO(ajm): the max is rather arbitrarily chosen; investigate. |
| 605 | if (delay > 500) { |
andrew@webrtc.org | 5f23d64 | 2012-05-29 21:14:06 +0000 | [diff] [blame] | 606 | delay = 500; |
| 607 | retval = kBadStreamParameterWarning; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 608 | } |
| 609 | |
| 610 | stream_delay_ms_ = delay; |
andrew@webrtc.org | 5f23d64 | 2012-05-29 21:14:06 +0000 | [diff] [blame] | 611 | return retval; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 612 | } |
| 613 | |
| 614 | int AudioProcessingImpl::stream_delay_ms() const { |
| 615 | return stream_delay_ms_; |
| 616 | } |
| 617 | |
| 618 | bool AudioProcessingImpl::was_stream_delay_set() const { |
| 619 | return was_stream_delay_set_; |
| 620 | } |
| 621 | |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 622 | void AudioProcessingImpl::set_stream_key_pressed(bool key_pressed) { |
| 623 | key_pressed_ = key_pressed; |
| 624 | } |
| 625 | |
| 626 | bool AudioProcessingImpl::stream_key_pressed() const { |
| 627 | return key_pressed_; |
| 628 | } |
| 629 | |
andrew@webrtc.org | 6f9f817 | 2012-03-06 19:03:39 +0000 | [diff] [blame] | 630 | void AudioProcessingImpl::set_delay_offset_ms(int offset) { |
| 631 | CriticalSectionScoped crit_scoped(crit_); |
| 632 | delay_offset_ms_ = offset; |
| 633 | } |
| 634 | |
| 635 | int AudioProcessingImpl::delay_offset_ms() const { |
| 636 | return delay_offset_ms_; |
| 637 | } |
| 638 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 639 | int AudioProcessingImpl::StartDebugRecording( |
| 640 | const char filename[AudioProcessing::kMaxFilenameSize]) { |
andrew@webrtc.org | 4065403 | 2012-01-30 20:51:15 +0000 | [diff] [blame] | 641 | CriticalSectionScoped crit_scoped(crit_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 642 | assert(kMaxFilenameSize == FileWrapper::kMaxFileNameSize); |
| 643 | |
| 644 | if (filename == NULL) { |
| 645 | return kNullPointerError; |
| 646 | } |
| 647 | |
andrew@webrtc.org | 7bf2646 | 2011-12-03 00:03:31 +0000 | [diff] [blame] | 648 | #ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 649 | // Stop any ongoing recording. |
| 650 | if (debug_file_->Open()) { |
| 651 | if (debug_file_->CloseFile() == -1) { |
| 652 | return kFileError; |
| 653 | } |
| 654 | } |
| 655 | |
| 656 | if (debug_file_->OpenFile(filename, false) == -1) { |
| 657 | debug_file_->CloseFile(); |
| 658 | return kFileError; |
| 659 | } |
| 660 | |
ajm@google.com | 808e0e0 | 2011-08-03 21:08:51 +0000 | [diff] [blame] | 661 | int err = WriteInitMessage(); |
| 662 | if (err != kNoError) { |
| 663 | return err; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 664 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 665 | return kNoError; |
andrew@webrtc.org | 7bf2646 | 2011-12-03 00:03:31 +0000 | [diff] [blame] | 666 | #else |
| 667 | return kUnsupportedFunctionError; |
| 668 | #endif // WEBRTC_AUDIOPROC_DEBUG_DUMP |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 669 | } |
| 670 | |
henrikg@webrtc.org | 863b536 | 2013-12-06 16:05:17 +0000 | [diff] [blame] | 671 | int AudioProcessingImpl::StartDebugRecording(FILE* handle) { |
| 672 | CriticalSectionScoped crit_scoped(crit_); |
| 673 | |
| 674 | if (handle == NULL) { |
| 675 | return kNullPointerError; |
| 676 | } |
| 677 | |
| 678 | #ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP |
| 679 | // Stop any ongoing recording. |
| 680 | if (debug_file_->Open()) { |
| 681 | if (debug_file_->CloseFile() == -1) { |
| 682 | return kFileError; |
| 683 | } |
| 684 | } |
| 685 | |
| 686 | if (debug_file_->OpenFromFileHandle(handle, true, false) == -1) { |
| 687 | return kFileError; |
| 688 | } |
| 689 | |
| 690 | int err = WriteInitMessage(); |
| 691 | if (err != kNoError) { |
| 692 | return err; |
| 693 | } |
| 694 | return kNoError; |
| 695 | #else |
| 696 | return kUnsupportedFunctionError; |
| 697 | #endif // WEBRTC_AUDIOPROC_DEBUG_DUMP |
| 698 | } |
| 699 | |
xians@webrtc.org | e46bc77 | 2014-10-10 08:36:56 +0000 | [diff] [blame] | 700 | int AudioProcessingImpl::StartDebugRecordingForPlatformFile( |
| 701 | rtc::PlatformFile handle) { |
| 702 | FILE* stream = rtc::FdopenPlatformFileForWriting(handle); |
| 703 | return StartDebugRecording(stream); |
| 704 | } |
| 705 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 706 | int AudioProcessingImpl::StopDebugRecording() { |
andrew@webrtc.org | 4065403 | 2012-01-30 20:51:15 +0000 | [diff] [blame] | 707 | CriticalSectionScoped crit_scoped(crit_); |
andrew@webrtc.org | 7bf2646 | 2011-12-03 00:03:31 +0000 | [diff] [blame] | 708 | |
| 709 | #ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 710 | // We just return if recording hasn't started. |
| 711 | if (debug_file_->Open()) { |
| 712 | if (debug_file_->CloseFile() == -1) { |
| 713 | return kFileError; |
| 714 | } |
| 715 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 716 | return kNoError; |
andrew@webrtc.org | 7bf2646 | 2011-12-03 00:03:31 +0000 | [diff] [blame] | 717 | #else |
| 718 | return kUnsupportedFunctionError; |
| 719 | #endif // WEBRTC_AUDIOPROC_DEBUG_DUMP |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 720 | } |
| 721 | |
| 722 | EchoCancellation* AudioProcessingImpl::echo_cancellation() const { |
| 723 | return echo_cancellation_; |
| 724 | } |
| 725 | |
| 726 | EchoControlMobile* AudioProcessingImpl::echo_control_mobile() const { |
| 727 | return echo_control_mobile_; |
| 728 | } |
| 729 | |
| 730 | GainControl* AudioProcessingImpl::gain_control() const { |
| 731 | return gain_control_; |
| 732 | } |
| 733 | |
| 734 | HighPassFilter* AudioProcessingImpl::high_pass_filter() const { |
| 735 | return high_pass_filter_; |
| 736 | } |
| 737 | |
| 738 | LevelEstimator* AudioProcessingImpl::level_estimator() const { |
| 739 | return level_estimator_; |
| 740 | } |
| 741 | |
| 742 | NoiseSuppression* AudioProcessingImpl::noise_suppression() const { |
| 743 | return noise_suppression_; |
| 744 | } |
| 745 | |
| 746 | VoiceDetection* AudioProcessingImpl::voice_detection() const { |
| 747 | return voice_detection_; |
| 748 | } |
| 749 | |
andrew@webrtc.org | 369166a | 2012-04-24 18:38:03 +0000 | [diff] [blame] | 750 | bool AudioProcessingImpl::is_data_processed() const { |
andrew@webrtc.org | 7bf2646 | 2011-12-03 00:03:31 +0000 | [diff] [blame] | 751 | int enabled_count = 0; |
| 752 | std::list<ProcessingComponent*>::const_iterator it; |
| 753 | for (it = component_list_.begin(); it != component_list_.end(); it++) { |
| 754 | if ((*it)->is_component_enabled()) { |
| 755 | enabled_count++; |
| 756 | } |
| 757 | } |
| 758 | |
| 759 | // Data is unchanged if no components are enabled, or if only level_estimator_ |
| 760 | // or voice_detection_ is enabled. |
| 761 | if (enabled_count == 0) { |
| 762 | return false; |
| 763 | } else if (enabled_count == 1) { |
| 764 | if (level_estimator_->is_enabled() || voice_detection_->is_enabled()) { |
| 765 | return false; |
| 766 | } |
| 767 | } else if (enabled_count == 2) { |
| 768 | if (level_estimator_->is_enabled() && voice_detection_->is_enabled()) { |
| 769 | return false; |
| 770 | } |
| 771 | } |
| 772 | return true; |
| 773 | } |
| 774 | |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 775 | bool AudioProcessingImpl::output_copy_needed(bool is_data_processed) const { |
andrew@webrtc.org | 369166a | 2012-04-24 18:38:03 +0000 | [diff] [blame] | 776 | // Check if we've upmixed or downmixed the audio. |
aluebs@webrtc.org | 27d106b | 2014-12-11 17:09:21 +0000 | [diff] [blame] | 777 | return ((fwd_out_format_.num_channels() != fwd_in_format_.num_channels()) || |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 778 | is_data_processed); |
andrew@webrtc.org | 7bf2646 | 2011-12-03 00:03:31 +0000 | [diff] [blame] | 779 | } |
| 780 | |
andrew@webrtc.org | 369166a | 2012-04-24 18:38:03 +0000 | [diff] [blame] | 781 | bool AudioProcessingImpl::synthesis_needed(bool is_data_processed) const { |
aluebs@webrtc.org | 087da13 | 2014-11-17 23:01:23 +0000 | [diff] [blame] | 782 | return (is_data_processed && (fwd_proc_format_.rate() == kSampleRate32kHz || |
| 783 | fwd_proc_format_.rate() == kSampleRate48kHz)); |
andrew@webrtc.org | 369166a | 2012-04-24 18:38:03 +0000 | [diff] [blame] | 784 | } |
| 785 | |
| 786 | bool AudioProcessingImpl::analysis_needed(bool is_data_processed) const { |
| 787 | if (!is_data_processed && !voice_detection_->is_enabled()) { |
andrew@webrtc.org | 7bf2646 | 2011-12-03 00:03:31 +0000 | [diff] [blame] | 788 | // Only level_estimator_ is enabled. |
| 789 | return false; |
aluebs@webrtc.org | 087da13 | 2014-11-17 23:01:23 +0000 | [diff] [blame] | 790 | } else if (fwd_proc_format_.rate() == kSampleRate32kHz || |
| 791 | fwd_proc_format_.rate() == kSampleRate48kHz) { |
andrew@webrtc.org | 7bf2646 | 2011-12-03 00:03:31 +0000 | [diff] [blame] | 792 | // Something besides level_estimator_ is enabled, and we have super-wb. |
| 793 | return true; |
| 794 | } |
| 795 | return false; |
| 796 | } |
| 797 | |
| 798 | #ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP |
ajm@google.com | 808e0e0 | 2011-08-03 21:08:51 +0000 | [diff] [blame] | 799 | int AudioProcessingImpl::WriteMessageToDebugFile() { |
| 800 | int32_t size = event_msg_->ByteSize(); |
| 801 | if (size <= 0) { |
| 802 | return kUnspecifiedError; |
| 803 | } |
andrew@webrtc.org | 621df67 | 2013-10-22 10:27:23 +0000 | [diff] [blame] | 804 | #if defined(WEBRTC_ARCH_BIG_ENDIAN) |
ajm@google.com | 808e0e0 | 2011-08-03 21:08:51 +0000 | [diff] [blame] | 805 | // TODO(ajm): Use little-endian "on the wire". For the moment, we can be |
| 806 | // pretty safe in assuming little-endian. |
| 807 | #endif |
| 808 | |
| 809 | if (!event_msg_->SerializeToString(&event_str_)) { |
| 810 | return kUnspecifiedError; |
| 811 | } |
| 812 | |
| 813 | // Write message preceded by its size. |
| 814 | if (!debug_file_->Write(&size, sizeof(int32_t))) { |
| 815 | return kFileError; |
| 816 | } |
| 817 | if (!debug_file_->Write(event_str_.data(), event_str_.length())) { |
| 818 | return kFileError; |
| 819 | } |
| 820 | |
| 821 | event_msg_->Clear(); |
| 822 | |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 823 | return kNoError; |
ajm@google.com | 808e0e0 | 2011-08-03 21:08:51 +0000 | [diff] [blame] | 824 | } |
| 825 | |
| 826 | int AudioProcessingImpl::WriteInitMessage() { |
| 827 | event_msg_->set_type(audioproc::Event::INIT); |
| 828 | audioproc::Init* msg = event_msg_->mutable_init(); |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 829 | msg->set_sample_rate(fwd_in_format_.rate()); |
| 830 | msg->set_num_input_channels(fwd_in_format_.num_channels()); |
aluebs@webrtc.org | 27d106b | 2014-12-11 17:09:21 +0000 | [diff] [blame] | 831 | msg->set_num_output_channels(fwd_out_format_.num_channels()); |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 832 | msg->set_num_reverse_channels(rev_in_format_.num_channels()); |
| 833 | msg->set_reverse_sample_rate(rev_in_format_.rate()); |
| 834 | msg->set_output_sample_rate(fwd_out_format_.rate()); |
ajm@google.com | 808e0e0 | 2011-08-03 21:08:51 +0000 | [diff] [blame] | 835 | |
| 836 | int err = WriteMessageToDebugFile(); |
| 837 | if (err != kNoError) { |
| 838 | return err; |
| 839 | } |
| 840 | |
| 841 | return kNoError; |
| 842 | } |
andrew@webrtc.org | 7bf2646 | 2011-12-03 00:03:31 +0000 | [diff] [blame] | 843 | #endif // WEBRTC_AUDIOPROC_DEBUG_DUMP |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 844 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 845 | } // namespace webrtc |