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" |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 18 | #include "webrtc/modules/audio_processing/agc/agc_manager_direct.h" |
andrew@webrtc.org | 78693fe | 2013-03-01 16:36:19 +0000 | [diff] [blame] | 19 | #include "webrtc/modules/audio_processing/audio_buffer.h" |
aluebs@webrtc.org | ae643ce | 2014-12-19 19:57:34 +0000 | [diff] [blame] | 20 | #include "webrtc/modules/audio_processing/beamformer/beamformer.h" |
aluebs@webrtc.org | 8789376 | 2014-11-27 23:40:25 +0000 | [diff] [blame] | 21 | #include "webrtc/modules/audio_processing/channel_buffer.h" |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 22 | #include "webrtc/modules/audio_processing/common.h" |
andrew@webrtc.org | 56e4a05 | 2014-02-27 22:23:17 +0000 | [diff] [blame] | 23 | #include "webrtc/modules/audio_processing/echo_cancellation_impl.h" |
andrew@webrtc.org | 78693fe | 2013-03-01 16:36:19 +0000 | [diff] [blame] | 24 | #include "webrtc/modules/audio_processing/echo_control_mobile_impl.h" |
| 25 | #include "webrtc/modules/audio_processing/gain_control_impl.h" |
| 26 | #include "webrtc/modules/audio_processing/high_pass_filter_impl.h" |
| 27 | #include "webrtc/modules/audio_processing/level_estimator_impl.h" |
| 28 | #include "webrtc/modules/audio_processing/noise_suppression_impl.h" |
| 29 | #include "webrtc/modules/audio_processing/processing_component.h" |
aluebs@webrtc.org | ae643ce | 2014-12-19 19:57:34 +0000 | [diff] [blame] | 30 | #include "webrtc/modules/audio_processing/transient/transient_suppressor.h" |
andrew@webrtc.org | 78693fe | 2013-03-01 16:36:19 +0000 | [diff] [blame] | 31 | #include "webrtc/modules/audio_processing/voice_detection_impl.h" |
| 32 | #include "webrtc/modules/interface/module_common_types.h" |
andrew@webrtc.org | 60730cf | 2014-01-07 17:45:09 +0000 | [diff] [blame] | 33 | #include "webrtc/system_wrappers/interface/compile_assert.h" |
andrew@webrtc.org | 78693fe | 2013-03-01 16:36:19 +0000 | [diff] [blame] | 34 | #include "webrtc/system_wrappers/interface/critical_section_wrapper.h" |
| 35 | #include "webrtc/system_wrappers/interface/file_wrapper.h" |
| 36 | #include "webrtc/system_wrappers/interface/logging.h" |
andrew@webrtc.org | 7bf2646 | 2011-12-03 00:03:31 +0000 | [diff] [blame] | 37 | |
| 38 | #ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP |
| 39 | // Files generated at build-time by the protobuf compiler. |
leozwang@webrtc.org | a373634 | 2012-03-16 21:36:00 +0000 | [diff] [blame] | 40 | #ifdef WEBRTC_ANDROID_PLATFORM_BUILD |
leozwang@webrtc.org | 534e495 | 2012-10-22 21:21:52 +0000 | [diff] [blame] | 41 | #include "external/webrtc/webrtc/modules/audio_processing/debug.pb.h" |
leozwang@google.com | ce9bfbb | 2011-08-03 23:34:31 +0000 | [diff] [blame] | 42 | #else |
ajm@google.com | 808e0e0 | 2011-08-03 21:08:51 +0000 | [diff] [blame] | 43 | #include "webrtc/audio_processing/debug.pb.h" |
leozwang@google.com | ce9bfbb | 2011-08-03 23:34:31 +0000 | [diff] [blame] | 44 | #endif |
andrew@webrtc.org | 7bf2646 | 2011-12-03 00:03:31 +0000 | [diff] [blame] | 45 | #endif // WEBRTC_AUDIOPROC_DEBUG_DUMP |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 46 | |
andrew@webrtc.org | 60730cf | 2014-01-07 17:45:09 +0000 | [diff] [blame] | 47 | #define RETURN_ON_ERR(expr) \ |
| 48 | do { \ |
| 49 | int err = expr; \ |
| 50 | if (err != kNoError) { \ |
| 51 | return err; \ |
| 52 | } \ |
| 53 | } while (0) |
| 54 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 55 | namespace webrtc { |
andrew@webrtc.org | 60730cf | 2014-01-07 17:45:09 +0000 | [diff] [blame] | 56 | |
| 57 | // Throughout webrtc, it's assumed that success is represented by zero. |
| 58 | COMPILE_ASSERT(AudioProcessing::kNoError == 0, no_error_must_be_zero); |
| 59 | |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 60 | // This class has two main functionalities: |
| 61 | // |
| 62 | // 1) It is returned instead of the real GainControl after the new AGC has been |
| 63 | // enabled in order to prevent an outside user from overriding compression |
| 64 | // settings. It doesn't do anything in its implementation, except for |
| 65 | // delegating the const methods and Enable calls to the real GainControl, so |
| 66 | // AGC can still be disabled. |
| 67 | // |
| 68 | // 2) It is injected into AgcManagerDirect and implements volume callbacks for |
| 69 | // getting and setting the volume level. It just caches this value to be used |
| 70 | // in VoiceEngine later. |
| 71 | class GainControlForNewAgc : public GainControl, public VolumeCallbacks { |
| 72 | public: |
| 73 | explicit GainControlForNewAgc(GainControlImpl* gain_control) |
| 74 | : real_gain_control_(gain_control), |
| 75 | volume_(0) { |
| 76 | } |
| 77 | |
| 78 | // GainControl implementation. |
| 79 | virtual int Enable(bool enable) OVERRIDE { |
| 80 | return real_gain_control_->Enable(enable); |
| 81 | } |
| 82 | virtual bool is_enabled() const OVERRIDE { |
| 83 | return real_gain_control_->is_enabled(); |
| 84 | } |
| 85 | virtual int set_stream_analog_level(int level) OVERRIDE { |
| 86 | volume_ = level; |
| 87 | return AudioProcessing::kNoError; |
| 88 | } |
| 89 | virtual int stream_analog_level() OVERRIDE { |
| 90 | return volume_; |
| 91 | } |
| 92 | virtual int set_mode(Mode mode) OVERRIDE { return AudioProcessing::kNoError; } |
| 93 | virtual Mode mode() const OVERRIDE { return GainControl::kAdaptiveAnalog; } |
| 94 | virtual int set_target_level_dbfs(int level) OVERRIDE { |
| 95 | return AudioProcessing::kNoError; |
| 96 | } |
| 97 | virtual int target_level_dbfs() const OVERRIDE { |
| 98 | return real_gain_control_->target_level_dbfs(); |
| 99 | } |
| 100 | virtual int set_compression_gain_db(int gain) OVERRIDE { |
| 101 | return AudioProcessing::kNoError; |
| 102 | } |
| 103 | virtual int compression_gain_db() const OVERRIDE { |
| 104 | return real_gain_control_->compression_gain_db(); |
| 105 | } |
| 106 | virtual int enable_limiter(bool enable) OVERRIDE { |
| 107 | return AudioProcessing::kNoError; |
| 108 | } |
| 109 | virtual bool is_limiter_enabled() const OVERRIDE { |
| 110 | return real_gain_control_->is_limiter_enabled(); |
| 111 | } |
| 112 | virtual int set_analog_level_limits(int minimum, |
| 113 | int maximum) OVERRIDE { |
| 114 | return AudioProcessing::kNoError; |
| 115 | } |
| 116 | virtual int analog_level_minimum() const OVERRIDE { |
| 117 | return real_gain_control_->analog_level_minimum(); |
| 118 | } |
| 119 | virtual int analog_level_maximum() const OVERRIDE { |
| 120 | return real_gain_control_->analog_level_maximum(); |
| 121 | } |
| 122 | virtual bool stream_is_saturated() const OVERRIDE { |
| 123 | return real_gain_control_->stream_is_saturated(); |
| 124 | } |
| 125 | |
| 126 | // VolumeCallbacks implementation. |
| 127 | virtual void SetMicVolume(int volume) OVERRIDE { |
| 128 | volume_ = volume; |
| 129 | } |
| 130 | virtual int GetMicVolume() OVERRIDE { |
| 131 | return volume_; |
| 132 | } |
| 133 | |
| 134 | private: |
| 135 | GainControl* real_gain_control_; |
| 136 | int volume_; |
| 137 | }; |
| 138 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 139 | AudioProcessing* AudioProcessing::Create(int id) { |
andrew@webrtc.org | e84978f | 2014-01-25 02:09:06 +0000 | [diff] [blame] | 140 | return Create(); |
| 141 | } |
| 142 | |
| 143 | AudioProcessing* AudioProcessing::Create() { |
| 144 | Config config; |
| 145 | return Create(config); |
| 146 | } |
| 147 | |
| 148 | AudioProcessing* AudioProcessing::Create(const Config& config) { |
| 149 | AudioProcessingImpl* apm = new AudioProcessingImpl(config); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 150 | if (apm->Initialize() != kNoError) { |
| 151 | delete apm; |
| 152 | apm = NULL; |
| 153 | } |
| 154 | |
| 155 | return apm; |
| 156 | } |
| 157 | |
andrew@webrtc.org | e84978f | 2014-01-25 02:09:06 +0000 | [diff] [blame] | 158 | AudioProcessingImpl::AudioProcessingImpl(const Config& config) |
andrew@webrtc.org | 60730cf | 2014-01-07 17:45:09 +0000 | [diff] [blame] | 159 | : echo_cancellation_(NULL), |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 160 | echo_control_mobile_(NULL), |
| 161 | gain_control_(NULL), |
| 162 | high_pass_filter_(NULL), |
| 163 | level_estimator_(NULL), |
| 164 | noise_suppression_(NULL), |
| 165 | voice_detection_(NULL), |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 166 | crit_(CriticalSectionWrapper::CreateCriticalSection()), |
andrew@webrtc.org | 7bf2646 | 2011-12-03 00:03:31 +0000 | [diff] [blame] | 167 | #ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP |
| 168 | debug_file_(FileWrapper::Create()), |
| 169 | event_msg_(new audioproc::Event()), |
| 170 | #endif |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 171 | fwd_in_format_(kSampleRate16kHz, 1), |
aluebs@webrtc.org | 27d106b | 2014-12-11 17:09:21 +0000 | [diff] [blame] | 172 | fwd_proc_format_(kSampleRate16kHz), |
| 173 | fwd_out_format_(kSampleRate16kHz, 1), |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 174 | rev_in_format_(kSampleRate16kHz, 1), |
| 175 | rev_proc_format_(kSampleRate16kHz, 1), |
| 176 | split_rate_(kSampleRate16kHz), |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 177 | stream_delay_ms_(0), |
andrew@webrtc.org | 6f9f817 | 2012-03-06 19:03:39 +0000 | [diff] [blame] | 178 | delay_offset_ms_(0), |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 179 | was_stream_delay_set_(false), |
andrew@webrtc.org | 38bf249 | 2014-02-13 17:43:44 +0000 | [diff] [blame] | 180 | output_will_be_muted_(false), |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 181 | key_pressed_(false), |
| 182 | #if defined(WEBRTC_ANDROID) || defined(WEBRTC_IOS) |
| 183 | use_new_agc_(false), |
| 184 | #else |
| 185 | use_new_agc_(config.Get<ExperimentalAgc>().enabled), |
| 186 | #endif |
aluebs@webrtc.org | ae643ce | 2014-12-19 19:57:34 +0000 | [diff] [blame] | 187 | transient_suppressor_enabled_(config.Get<ExperimentalNs>().enabled), |
| 188 | beamformer_enabled_(config.Get<Beamforming>().enabled) { |
andrew@webrtc.org | 56e4a05 | 2014-02-27 22:23:17 +0000 | [diff] [blame] | 189 | echo_cancellation_ = new EchoCancellationImpl(this, crit_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 190 | component_list_.push_back(echo_cancellation_); |
| 191 | |
andrew@webrtc.org | 56e4a05 | 2014-02-27 22:23:17 +0000 | [diff] [blame] | 192 | echo_control_mobile_ = new EchoControlMobileImpl(this, crit_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 193 | component_list_.push_back(echo_control_mobile_); |
| 194 | |
andrew@webrtc.org | 56e4a05 | 2014-02-27 22:23:17 +0000 | [diff] [blame] | 195 | gain_control_ = new GainControlImpl(this, crit_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 196 | component_list_.push_back(gain_control_); |
| 197 | |
andrew@webrtc.org | 56e4a05 | 2014-02-27 22:23:17 +0000 | [diff] [blame] | 198 | high_pass_filter_ = new HighPassFilterImpl(this, crit_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 199 | component_list_.push_back(high_pass_filter_); |
| 200 | |
andrew@webrtc.org | 56e4a05 | 2014-02-27 22:23:17 +0000 | [diff] [blame] | 201 | level_estimator_ = new LevelEstimatorImpl(this, crit_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 202 | component_list_.push_back(level_estimator_); |
| 203 | |
andrew@webrtc.org | 56e4a05 | 2014-02-27 22:23:17 +0000 | [diff] [blame] | 204 | noise_suppression_ = new NoiseSuppressionImpl(this, crit_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 205 | component_list_.push_back(noise_suppression_); |
| 206 | |
andrew@webrtc.org | 56e4a05 | 2014-02-27 22:23:17 +0000 | [diff] [blame] | 207 | voice_detection_ = new VoiceDetectionImpl(this, crit_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 208 | component_list_.push_back(voice_detection_); |
andrew@webrtc.org | e84978f | 2014-01-25 02:09:06 +0000 | [diff] [blame] | 209 | |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 210 | gain_control_for_new_agc_.reset(new GainControlForNewAgc(gain_control_)); |
| 211 | |
andrew@webrtc.org | e84978f | 2014-01-25 02:09:06 +0000 | [diff] [blame] | 212 | SetExtraOptions(config); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | AudioProcessingImpl::~AudioProcessingImpl() { |
andrew@webrtc.org | 8186534 | 2012-10-27 00:28:27 +0000 | [diff] [blame] | 216 | { |
| 217 | CriticalSectionScoped crit_scoped(crit_); |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 218 | // Depends on gain_control_ and gain_control_for_new_agc_. |
| 219 | agc_manager_.reset(); |
| 220 | // Depends on gain_control_. |
| 221 | gain_control_for_new_agc_.reset(); |
andrew@webrtc.org | 8186534 | 2012-10-27 00:28:27 +0000 | [diff] [blame] | 222 | while (!component_list_.empty()) { |
| 223 | ProcessingComponent* component = component_list_.front(); |
| 224 | component->Destroy(); |
| 225 | delete component; |
| 226 | component_list_.pop_front(); |
| 227 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 228 | |
andrew@webrtc.org | 7bf2646 | 2011-12-03 00:03:31 +0000 | [diff] [blame] | 229 | #ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP |
andrew@webrtc.org | 8186534 | 2012-10-27 00:28:27 +0000 | [diff] [blame] | 230 | if (debug_file_->Open()) { |
| 231 | debug_file_->CloseFile(); |
| 232 | } |
andrew@webrtc.org | 7bf2646 | 2011-12-03 00:03:31 +0000 | [diff] [blame] | 233 | #endif |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 234 | } |
andrew@webrtc.org | 16cfbe2 | 2012-08-29 16:58:25 +0000 | [diff] [blame] | 235 | delete crit_; |
| 236 | crit_ = NULL; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 237 | } |
| 238 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 239 | int AudioProcessingImpl::Initialize() { |
andrew@webrtc.org | 4065403 | 2012-01-30 20:51:15 +0000 | [diff] [blame] | 240 | CriticalSectionScoped crit_scoped(crit_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 241 | return InitializeLocked(); |
| 242 | } |
| 243 | |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 244 | int AudioProcessingImpl::set_sample_rate_hz(int rate) { |
andrew@webrtc.org | a8b9737 | 2014-03-10 22:26:12 +0000 | [diff] [blame] | 245 | CriticalSectionScoped crit_scoped(crit_); |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 246 | return InitializeLocked(rate, |
| 247 | rate, |
| 248 | rev_in_format_.rate(), |
| 249 | fwd_in_format_.num_channels(), |
aluebs@webrtc.org | 27d106b | 2014-12-11 17:09:21 +0000 | [diff] [blame] | 250 | fwd_out_format_.num_channels(), |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 251 | rev_in_format_.num_channels()); |
| 252 | } |
| 253 | |
| 254 | int AudioProcessingImpl::Initialize(int input_sample_rate_hz, |
| 255 | int output_sample_rate_hz, |
| 256 | int reverse_sample_rate_hz, |
| 257 | ChannelLayout input_layout, |
| 258 | ChannelLayout output_layout, |
| 259 | ChannelLayout reverse_layout) { |
| 260 | CriticalSectionScoped crit_scoped(crit_); |
| 261 | return InitializeLocked(input_sample_rate_hz, |
| 262 | output_sample_rate_hz, |
andrew@webrtc.org | a8b9737 | 2014-03-10 22:26:12 +0000 | [diff] [blame] | 263 | reverse_sample_rate_hz, |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 264 | ChannelsFromLayout(input_layout), |
| 265 | ChannelsFromLayout(output_layout), |
| 266 | ChannelsFromLayout(reverse_layout)); |
andrew@webrtc.org | a8b9737 | 2014-03-10 22:26:12 +0000 | [diff] [blame] | 267 | } |
| 268 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 269 | int AudioProcessingImpl::InitializeLocked() { |
aluebs@webrtc.org | ae643ce | 2014-12-19 19:57:34 +0000 | [diff] [blame] | 270 | const int fwd_audio_buffer_channels = beamformer_enabled_ ? |
| 271 | fwd_in_format_.num_channels() : |
| 272 | fwd_out_format_.num_channels(); |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 273 | render_audio_.reset(new AudioBuffer(rev_in_format_.samples_per_channel(), |
| 274 | rev_in_format_.num_channels(), |
| 275 | rev_proc_format_.samples_per_channel(), |
| 276 | rev_proc_format_.num_channels(), |
| 277 | rev_proc_format_.samples_per_channel())); |
| 278 | capture_audio_.reset(new AudioBuffer(fwd_in_format_.samples_per_channel(), |
| 279 | fwd_in_format_.num_channels(), |
| 280 | fwd_proc_format_.samples_per_channel(), |
aluebs@webrtc.org | ae643ce | 2014-12-19 19:57:34 +0000 | [diff] [blame] | 281 | fwd_audio_buffer_channels, |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 282 | fwd_out_format_.samples_per_channel())); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 283 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 284 | // Initialize all components. |
| 285 | std::list<ProcessingComponent*>::iterator it; |
andrew@webrtc.org | 8186534 | 2012-10-27 00:28:27 +0000 | [diff] [blame] | 286 | for (it = component_list_.begin(); it != component_list_.end(); ++it) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 287 | int err = (*it)->Initialize(); |
| 288 | if (err != kNoError) { |
| 289 | return err; |
| 290 | } |
| 291 | } |
| 292 | |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 293 | int err = InitializeExperimentalAgc(); |
| 294 | if (err != kNoError) { |
| 295 | return err; |
| 296 | } |
| 297 | |
| 298 | err = InitializeTransient(); |
| 299 | if (err != kNoError) { |
| 300 | return err; |
| 301 | } |
| 302 | |
aluebs@webrtc.org | ae643ce | 2014-12-19 19:57:34 +0000 | [diff] [blame] | 303 | InitializeBeamformer(); |
| 304 | |
andrew@webrtc.org | 7bf2646 | 2011-12-03 00:03:31 +0000 | [diff] [blame] | 305 | #ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP |
ajm@google.com | 808e0e0 | 2011-08-03 21:08:51 +0000 | [diff] [blame] | 306 | if (debug_file_->Open()) { |
| 307 | int err = WriteInitMessage(); |
| 308 | if (err != kNoError) { |
| 309 | return err; |
| 310 | } |
| 311 | } |
andrew@webrtc.org | 7bf2646 | 2011-12-03 00:03:31 +0000 | [diff] [blame] | 312 | #endif |
ajm@google.com | 808e0e0 | 2011-08-03 21:08:51 +0000 | [diff] [blame] | 313 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 314 | return kNoError; |
| 315 | } |
| 316 | |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 317 | int AudioProcessingImpl::InitializeLocked(int input_sample_rate_hz, |
| 318 | int output_sample_rate_hz, |
andrew@webrtc.org | a8b9737 | 2014-03-10 22:26:12 +0000 | [diff] [blame] | 319 | int reverse_sample_rate_hz, |
| 320 | int num_input_channels, |
| 321 | int num_output_channels, |
| 322 | int num_reverse_channels) { |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 323 | if (input_sample_rate_hz <= 0 || |
| 324 | output_sample_rate_hz <= 0 || |
| 325 | reverse_sample_rate_hz <= 0) { |
andrew@webrtc.org | a8b9737 | 2014-03-10 22:26:12 +0000 | [diff] [blame] | 326 | return kBadSampleRateError; |
| 327 | } |
| 328 | if (num_output_channels > num_input_channels) { |
| 329 | return kBadNumberChannelsError; |
| 330 | } |
| 331 | // Only mono and stereo supported currently. |
| 332 | if (num_input_channels > 2 || num_input_channels < 1 || |
| 333 | num_output_channels > 2 || num_output_channels < 1 || |
| 334 | num_reverse_channels > 2 || num_reverse_channels < 1) { |
| 335 | return kBadNumberChannelsError; |
| 336 | } |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 337 | |
| 338 | fwd_in_format_.set(input_sample_rate_hz, num_input_channels); |
aluebs@webrtc.org | 27d106b | 2014-12-11 17:09:21 +0000 | [diff] [blame] | 339 | fwd_out_format_.set(output_sample_rate_hz, num_output_channels); |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 340 | rev_in_format_.set(reverse_sample_rate_hz, num_reverse_channels); |
| 341 | |
| 342 | // We process at the closest native rate >= min(input rate, output rate)... |
| 343 | int min_proc_rate = std::min(fwd_in_format_.rate(), fwd_out_format_.rate()); |
| 344 | int fwd_proc_rate; |
| 345 | if (min_proc_rate > kSampleRate16kHz) { |
| 346 | fwd_proc_rate = kSampleRate32kHz; |
| 347 | } else if (min_proc_rate > kSampleRate8kHz) { |
| 348 | fwd_proc_rate = kSampleRate16kHz; |
| 349 | } else { |
| 350 | fwd_proc_rate = kSampleRate8kHz; |
| 351 | } |
| 352 | // ...with one exception. |
| 353 | if (echo_control_mobile_->is_enabled() && min_proc_rate > kSampleRate16kHz) { |
| 354 | fwd_proc_rate = kSampleRate16kHz; |
andrew@webrtc.org | a8b9737 | 2014-03-10 22:26:12 +0000 | [diff] [blame] | 355 | } |
| 356 | |
aluebs@webrtc.org | 27d106b | 2014-12-11 17:09:21 +0000 | [diff] [blame] | 357 | fwd_proc_format_.set(fwd_proc_rate); |
andrew@webrtc.org | a8b9737 | 2014-03-10 22:26:12 +0000 | [diff] [blame] | 358 | |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 359 | // We normally process the reverse stream at 16 kHz. Unless... |
| 360 | int rev_proc_rate = kSampleRate16kHz; |
| 361 | if (fwd_proc_format_.rate() == kSampleRate8kHz) { |
| 362 | // ...the forward stream is at 8 kHz. |
| 363 | rev_proc_rate = kSampleRate8kHz; |
andrew@webrtc.org | a8b9737 | 2014-03-10 22:26:12 +0000 | [diff] [blame] | 364 | } else { |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 365 | if (rev_in_format_.rate() == kSampleRate32kHz) { |
| 366 | // ...or the input is at 32 kHz, in which case we use the splitting |
| 367 | // filter rather than the resampler. |
| 368 | rev_proc_rate = kSampleRate32kHz; |
| 369 | } |
| 370 | } |
| 371 | |
andrew@webrtc.org | 30be827 | 2014-09-24 20:06:23 +0000 | [diff] [blame] | 372 | // Always downmix the reverse stream to mono for analysis. This has been |
| 373 | // demonstrated to work well for AEC in most practical scenarios. |
| 374 | rev_proc_format_.set(rev_proc_rate, 1); |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 375 | |
aluebs@webrtc.org | 087da13 | 2014-11-17 23:01:23 +0000 | [diff] [blame] | 376 | if (fwd_proc_format_.rate() == kSampleRate32kHz || |
| 377 | fwd_proc_format_.rate() == kSampleRate48kHz) { |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 378 | split_rate_ = kSampleRate16kHz; |
| 379 | } else { |
| 380 | split_rate_ = fwd_proc_format_.rate(); |
andrew@webrtc.org | a8b9737 | 2014-03-10 22:26:12 +0000 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | return InitializeLocked(); |
| 384 | } |
| 385 | |
| 386 | // Calls InitializeLocked() if any of the audio parameters have changed from |
| 387 | // their current values. |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 388 | int AudioProcessingImpl::MaybeInitializeLocked(int input_sample_rate_hz, |
| 389 | int output_sample_rate_hz, |
andrew@webrtc.org | a8b9737 | 2014-03-10 22:26:12 +0000 | [diff] [blame] | 390 | int reverse_sample_rate_hz, |
| 391 | int num_input_channels, |
| 392 | int num_output_channels, |
| 393 | int num_reverse_channels) { |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 394 | if (input_sample_rate_hz == fwd_in_format_.rate() && |
| 395 | output_sample_rate_hz == fwd_out_format_.rate() && |
| 396 | reverse_sample_rate_hz == rev_in_format_.rate() && |
| 397 | num_input_channels == fwd_in_format_.num_channels() && |
aluebs@webrtc.org | 27d106b | 2014-12-11 17:09:21 +0000 | [diff] [blame] | 398 | num_output_channels == fwd_out_format_.num_channels() && |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 399 | num_reverse_channels == rev_in_format_.num_channels()) { |
andrew@webrtc.org | a8b9737 | 2014-03-10 22:26:12 +0000 | [diff] [blame] | 400 | return kNoError; |
| 401 | } |
aluebs@webrtc.org | ae643ce | 2014-12-19 19:57:34 +0000 | [diff] [blame] | 402 | if (beamformer_enabled_ && |
| 403 | (num_input_channels < 2 || num_output_channels > 1)) { |
| 404 | return kBadNumberChannelsError; |
| 405 | } |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 406 | return InitializeLocked(input_sample_rate_hz, |
| 407 | output_sample_rate_hz, |
andrew@webrtc.org | a8b9737 | 2014-03-10 22:26:12 +0000 | [diff] [blame] | 408 | reverse_sample_rate_hz, |
| 409 | num_input_channels, |
| 410 | num_output_channels, |
| 411 | num_reverse_channels); |
| 412 | } |
| 413 | |
andrew@webrtc.org | 61e596f | 2013-07-25 18:28:29 +0000 | [diff] [blame] | 414 | void AudioProcessingImpl::SetExtraOptions(const Config& config) { |
andrew@webrtc.org | e84978f | 2014-01-25 02:09:06 +0000 | [diff] [blame] | 415 | CriticalSectionScoped crit_scoped(crit_); |
andrew@webrtc.org | 61e596f | 2013-07-25 18:28:29 +0000 | [diff] [blame] | 416 | std::list<ProcessingComponent*>::iterator it; |
| 417 | for (it = component_list_.begin(); it != component_list_.end(); ++it) |
| 418 | (*it)->SetExtraOptions(config); |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 419 | |
| 420 | if (transient_suppressor_enabled_ != config.Get<ExperimentalNs>().enabled) { |
| 421 | transient_suppressor_enabled_ = config.Get<ExperimentalNs>().enabled; |
| 422 | InitializeTransient(); |
| 423 | } |
andrew@webrtc.org | 61e596f | 2013-07-25 18:28:29 +0000 | [diff] [blame] | 424 | } |
| 425 | |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 426 | int AudioProcessingImpl::input_sample_rate_hz() const { |
andrew@webrtc.org | 4065403 | 2012-01-30 20:51:15 +0000 | [diff] [blame] | 427 | CriticalSectionScoped crit_scoped(crit_); |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 428 | return fwd_in_format_.rate(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 429 | } |
| 430 | |
andrew@webrtc.org | 46b31b1 | 2014-04-23 03:33:54 +0000 | [diff] [blame] | 431 | int AudioProcessingImpl::sample_rate_hz() const { |
| 432 | CriticalSectionScoped crit_scoped(crit_); |
| 433 | return fwd_in_format_.rate(); |
| 434 | } |
| 435 | |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 436 | int AudioProcessingImpl::proc_sample_rate_hz() const { |
| 437 | return fwd_proc_format_.rate(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 438 | } |
| 439 | |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 440 | int AudioProcessingImpl::proc_split_sample_rate_hz() const { |
| 441 | return split_rate_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 442 | } |
| 443 | |
| 444 | int AudioProcessingImpl::num_reverse_channels() const { |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 445 | return rev_proc_format_.num_channels(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | int AudioProcessingImpl::num_input_channels() const { |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 449 | return fwd_in_format_.num_channels(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | int AudioProcessingImpl::num_output_channels() const { |
aluebs@webrtc.org | 27d106b | 2014-12-11 17:09:21 +0000 | [diff] [blame] | 453 | return fwd_out_format_.num_channels(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 454 | } |
| 455 | |
andrew@webrtc.org | 17342e5 | 2014-02-12 22:28:31 +0000 | [diff] [blame] | 456 | void AudioProcessingImpl::set_output_will_be_muted(bool muted) { |
| 457 | output_will_be_muted_ = muted; |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 458 | CriticalSectionScoped lock(crit_); |
| 459 | if (agc_manager_.get()) { |
| 460 | agc_manager_->SetCaptureMuted(output_will_be_muted_); |
| 461 | } |
andrew@webrtc.org | 17342e5 | 2014-02-12 22:28:31 +0000 | [diff] [blame] | 462 | } |
| 463 | |
| 464 | bool AudioProcessingImpl::output_will_be_muted() const { |
| 465 | return output_will_be_muted_; |
| 466 | } |
| 467 | |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 468 | int AudioProcessingImpl::ProcessStream(const float* const* src, |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 469 | int samples_per_channel, |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 470 | int input_sample_rate_hz, |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 471 | ChannelLayout input_layout, |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 472 | int output_sample_rate_hz, |
| 473 | ChannelLayout output_layout, |
| 474 | float* const* dest) { |
andrew@webrtc.org | 4065403 | 2012-01-30 20:51:15 +0000 | [diff] [blame] | 475 | CriticalSectionScoped crit_scoped(crit_); |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 476 | if (!src || !dest) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 477 | return kNullPointerError; |
| 478 | } |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 479 | |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 480 | RETURN_ON_ERR(MaybeInitializeLocked(input_sample_rate_hz, |
| 481 | output_sample_rate_hz, |
| 482 | rev_in_format_.rate(), |
| 483 | ChannelsFromLayout(input_layout), |
| 484 | ChannelsFromLayout(output_layout), |
| 485 | rev_in_format_.num_channels())); |
| 486 | if (samples_per_channel != fwd_in_format_.samples_per_channel()) { |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 487 | return kBadDataLengthError; |
| 488 | } |
| 489 | |
| 490 | #ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP |
| 491 | if (debug_file_->Open()) { |
| 492 | event_msg_->set_type(audioproc::Event::STREAM); |
| 493 | audioproc::Stream* msg = event_msg_->mutable_stream(); |
aluebs@webrtc.org | 59a1b1b | 2014-08-28 10:43:09 +0000 | [diff] [blame] | 494 | const size_t channel_size = |
| 495 | sizeof(float) * fwd_in_format_.samples_per_channel(); |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 496 | for (int i = 0; i < fwd_in_format_.num_channels(); ++i) |
| 497 | msg->add_input_channel(src[i], channel_size); |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 498 | } |
| 499 | #endif |
| 500 | |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 501 | capture_audio_->CopyFrom(src, samples_per_channel, input_layout); |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 502 | RETURN_ON_ERR(ProcessStreamLocked()); |
| 503 | if (output_copy_needed(is_data_processed())) { |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 504 | capture_audio_->CopyTo(fwd_out_format_.samples_per_channel(), |
| 505 | output_layout, |
| 506 | dest); |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 507 | } |
| 508 | |
| 509 | #ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP |
| 510 | if (debug_file_->Open()) { |
| 511 | audioproc::Stream* msg = event_msg_->mutable_stream(); |
aluebs@webrtc.org | 59a1b1b | 2014-08-28 10:43:09 +0000 | [diff] [blame] | 512 | const size_t channel_size = |
| 513 | sizeof(float) * fwd_out_format_.samples_per_channel(); |
aluebs@webrtc.org | 27d106b | 2014-12-11 17:09:21 +0000 | [diff] [blame] | 514 | for (int i = 0; i < fwd_out_format_.num_channels(); ++i) |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 515 | msg->add_output_channel(dest[i], channel_size); |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 516 | RETURN_ON_ERR(WriteMessageToDebugFile()); |
| 517 | } |
| 518 | #endif |
| 519 | |
| 520 | return kNoError; |
| 521 | } |
| 522 | |
| 523 | int AudioProcessingImpl::ProcessStream(AudioFrame* frame) { |
| 524 | CriticalSectionScoped crit_scoped(crit_); |
| 525 | if (!frame) { |
| 526 | return kNullPointerError; |
| 527 | } |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 528 | // Must be a native rate. |
| 529 | if (frame->sample_rate_hz_ != kSampleRate8kHz && |
| 530 | frame->sample_rate_hz_ != kSampleRate16kHz && |
aluebs@webrtc.org | 087da13 | 2014-11-17 23:01:23 +0000 | [diff] [blame] | 531 | frame->sample_rate_hz_ != kSampleRate32kHz && |
| 532 | frame->sample_rate_hz_ != kSampleRate48kHz) { |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 533 | return kBadSampleRateError; |
| 534 | } |
| 535 | if (echo_control_mobile_->is_enabled() && |
| 536 | frame->sample_rate_hz_ > kSampleRate16kHz) { |
| 537 | LOG(LS_ERROR) << "AECM only supports 16 or 8 kHz sample rates"; |
| 538 | return kUnsupportedComponentError; |
| 539 | } |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 540 | |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 541 | // TODO(ajm): The input and output rates and channels are currently |
| 542 | // constrained to be identical in the int16 interface. |
andrew@webrtc.org | 60730cf | 2014-01-07 17:45:09 +0000 | [diff] [blame] | 543 | RETURN_ON_ERR(MaybeInitializeLocked(frame->sample_rate_hz_, |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 544 | frame->sample_rate_hz_, |
| 545 | rev_in_format_.rate(), |
| 546 | frame->num_channels_, |
| 547 | frame->num_channels_, |
| 548 | rev_in_format_.num_channels())); |
| 549 | if (frame->samples_per_channel_ != fwd_in_format_.samples_per_channel()) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 550 | return kBadDataLengthError; |
| 551 | } |
| 552 | |
andrew@webrtc.org | 7bf2646 | 2011-12-03 00:03:31 +0000 | [diff] [blame] | 553 | #ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 554 | if (debug_file_->Open()) { |
ajm@google.com | 808e0e0 | 2011-08-03 21:08:51 +0000 | [diff] [blame] | 555 | event_msg_->set_type(audioproc::Event::STREAM); |
| 556 | audioproc::Stream* msg = event_msg_->mutable_stream(); |
andrew@webrtc.org | 755b04a | 2011-11-15 16:57:56 +0000 | [diff] [blame] | 557 | const size_t data_size = sizeof(int16_t) * |
andrew@webrtc.org | 63a5098 | 2012-05-02 23:56:37 +0000 | [diff] [blame] | 558 | frame->samples_per_channel_ * |
| 559 | frame->num_channels_; |
| 560 | msg->set_input_data(frame->data_, data_size); |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 561 | } |
| 562 | #endif |
| 563 | |
| 564 | capture_audio_->DeinterleaveFrom(frame); |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 565 | RETURN_ON_ERR(ProcessStreamLocked()); |
| 566 | capture_audio_->InterleaveTo(frame, output_copy_needed(is_data_processed())); |
| 567 | |
| 568 | #ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP |
| 569 | if (debug_file_->Open()) { |
| 570 | audioproc::Stream* msg = event_msg_->mutable_stream(); |
| 571 | const size_t data_size = sizeof(int16_t) * |
| 572 | frame->samples_per_channel_ * |
| 573 | frame->num_channels_; |
| 574 | msg->set_output_data(frame->data_, data_size); |
| 575 | RETURN_ON_ERR(WriteMessageToDebugFile()); |
| 576 | } |
| 577 | #endif |
| 578 | |
| 579 | return kNoError; |
| 580 | } |
| 581 | |
| 582 | |
| 583 | int AudioProcessingImpl::ProcessStreamLocked() { |
| 584 | #ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP |
| 585 | if (debug_file_->Open()) { |
| 586 | audioproc::Stream* msg = event_msg_->mutable_stream(); |
ajm@google.com | 808e0e0 | 2011-08-03 21:08:51 +0000 | [diff] [blame] | 587 | msg->set_delay(stream_delay_ms_); |
| 588 | msg->set_drift(echo_cancellation_->stream_drift_samples()); |
| 589 | msg->set_level(gain_control_->stream_analog_level()); |
andrew@webrtc.org | ce8e077 | 2014-02-12 15:28:30 +0000 | [diff] [blame] | 590 | msg->set_keypress(key_pressed_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 591 | } |
andrew@webrtc.org | 7bf2646 | 2011-12-03 00:03:31 +0000 | [diff] [blame] | 592 | #endif |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 593 | |
andrew@webrtc.org | 103657b | 2014-04-24 18:28:56 +0000 | [diff] [blame] | 594 | AudioBuffer* ca = capture_audio_.get(); // For brevity. |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 595 | if (use_new_agc_ && gain_control_->is_enabled()) { |
| 596 | agc_manager_->AnalyzePreProcess(ca->data(0), |
| 597 | ca->num_channels(), |
| 598 | fwd_proc_format_.samples_per_channel()); |
| 599 | } |
| 600 | |
andrew@webrtc.org | 369166a | 2012-04-24 18:38:03 +0000 | [diff] [blame] | 601 | bool data_processed = is_data_processed(); |
| 602 | if (analysis_needed(data_processed)) { |
aluebs@webrtc.org | be05c74 | 2014-11-14 22:18:10 +0000 | [diff] [blame] | 603 | ca->SplitIntoFrequencyBands(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 604 | } |
| 605 | |
aluebs@webrtc.org | ae643ce | 2014-12-19 19:57:34 +0000 | [diff] [blame] | 606 | #ifdef WEBRTC_BEAMFORMER |
| 607 | if (beamformer_enabled_) { |
| 608 | beamformer_->ProcessChunk(ca->split_channels_const_f(kBand0To8kHz), |
| 609 | ca->split_channels_const_f(kBand8To16kHz), |
| 610 | ca->num_channels(), |
| 611 | ca->samples_per_split_channel(), |
| 612 | ca->split_channels_f(kBand0To8kHz), |
| 613 | ca->split_channels_f(kBand8To16kHz)); |
| 614 | ca->set_num_channels(1); |
| 615 | } |
| 616 | #endif |
| 617 | |
andrew@webrtc.org | 103657b | 2014-04-24 18:28:56 +0000 | [diff] [blame] | 618 | RETURN_ON_ERR(high_pass_filter_->ProcessCaptureAudio(ca)); |
| 619 | RETURN_ON_ERR(gain_control_->AnalyzeCaptureAudio(ca)); |
aluebs@webrtc.org | a0ce9fa | 2014-09-24 14:18:03 +0000 | [diff] [blame] | 620 | RETURN_ON_ERR(noise_suppression_->AnalyzeCaptureAudio(ca)); |
andrew@webrtc.org | 103657b | 2014-04-24 18:28:56 +0000 | [diff] [blame] | 621 | RETURN_ON_ERR(echo_cancellation_->ProcessCaptureAudio(ca)); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 622 | |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 623 | if (echo_control_mobile_->is_enabled() && noise_suppression_->is_enabled()) { |
andrew@webrtc.org | 103657b | 2014-04-24 18:28:56 +0000 | [diff] [blame] | 624 | ca->CopyLowPassToReference(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 625 | } |
andrew@webrtc.org | 103657b | 2014-04-24 18:28:56 +0000 | [diff] [blame] | 626 | RETURN_ON_ERR(noise_suppression_->ProcessCaptureAudio(ca)); |
| 627 | RETURN_ON_ERR(echo_control_mobile_->ProcessCaptureAudio(ca)); |
| 628 | RETURN_ON_ERR(voice_detection_->ProcessCaptureAudio(ca)); |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 629 | |
| 630 | if (use_new_agc_ && gain_control_->is_enabled()) { |
| 631 | agc_manager_->Process(ca->split_bands_const(0)[kBand0To8kHz], |
| 632 | ca->samples_per_split_channel(), |
| 633 | split_rate_); |
| 634 | } |
andrew@webrtc.org | 103657b | 2014-04-24 18:28:56 +0000 | [diff] [blame] | 635 | RETURN_ON_ERR(gain_control_->ProcessCaptureAudio(ca)); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 636 | |
andrew@webrtc.org | 369166a | 2012-04-24 18:38:03 +0000 | [diff] [blame] | 637 | if (synthesis_needed(data_processed)) { |
aluebs@webrtc.org | be05c74 | 2014-11-14 22:18:10 +0000 | [diff] [blame] | 638 | ca->MergeFrequencyBands(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 639 | } |
| 640 | |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 641 | // TODO(aluebs): Investigate if the transient suppression placement should be |
| 642 | // before or after the AGC. |
| 643 | if (transient_suppressor_enabled_) { |
| 644 | float voice_probability = |
| 645 | agc_manager_.get() ? agc_manager_->voice_probability() : 1.f; |
| 646 | |
| 647 | transient_suppressor_->Suppress(ca->data_f(0), |
| 648 | ca->samples_per_channel(), |
| 649 | ca->num_channels(), |
| 650 | ca->split_bands_const_f(0)[kBand0To8kHz], |
| 651 | ca->samples_per_split_channel(), |
| 652 | ca->keyboard_data(), |
| 653 | ca->samples_per_keyboard_channel(), |
| 654 | voice_probability, |
| 655 | key_pressed_); |
| 656 | } |
| 657 | |
andrew@webrtc.org | 755b04a | 2011-11-15 16:57:56 +0000 | [diff] [blame] | 658 | // The level estimator operates on the recombined data. |
andrew@webrtc.org | 103657b | 2014-04-24 18:28:56 +0000 | [diff] [blame] | 659 | RETURN_ON_ERR(level_estimator_->ProcessStream(ca)); |
ajm@google.com | 808e0e0 | 2011-08-03 21:08:51 +0000 | [diff] [blame] | 660 | |
andrew@webrtc.org | 1e91693 | 2011-11-29 18:28:57 +0000 | [diff] [blame] | 661 | was_stream_delay_set_ = false; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 662 | return kNoError; |
| 663 | } |
| 664 | |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 665 | int AudioProcessingImpl::AnalyzeReverseStream(const float* const* data, |
| 666 | int samples_per_channel, |
| 667 | int sample_rate_hz, |
| 668 | ChannelLayout layout) { |
| 669 | CriticalSectionScoped crit_scoped(crit_); |
| 670 | if (data == NULL) { |
| 671 | return kNullPointerError; |
| 672 | } |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 673 | |
| 674 | const int num_channels = ChannelsFromLayout(layout); |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 675 | RETURN_ON_ERR(MaybeInitializeLocked(fwd_in_format_.rate(), |
| 676 | fwd_out_format_.rate(), |
| 677 | sample_rate_hz, |
| 678 | fwd_in_format_.num_channels(), |
aluebs@webrtc.org | 27d106b | 2014-12-11 17:09:21 +0000 | [diff] [blame] | 679 | fwd_out_format_.num_channels(), |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 680 | num_channels)); |
| 681 | if (samples_per_channel != rev_in_format_.samples_per_channel()) { |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 682 | return kBadDataLengthError; |
| 683 | } |
| 684 | |
| 685 | #ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP |
| 686 | if (debug_file_->Open()) { |
| 687 | event_msg_->set_type(audioproc::Event::REVERSE_STREAM); |
| 688 | audioproc::ReverseStream* msg = event_msg_->mutable_reverse_stream(); |
aluebs@webrtc.org | 59a1b1b | 2014-08-28 10:43:09 +0000 | [diff] [blame] | 689 | const size_t channel_size = |
| 690 | sizeof(float) * rev_in_format_.samples_per_channel(); |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 691 | for (int i = 0; i < num_channels; ++i) |
andrew@webrtc.org | a8b9737 | 2014-03-10 22:26:12 +0000 | [diff] [blame] | 692 | msg->add_channel(data[i], channel_size); |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 693 | RETURN_ON_ERR(WriteMessageToDebugFile()); |
| 694 | } |
| 695 | #endif |
| 696 | |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 697 | render_audio_->CopyFrom(data, samples_per_channel, layout); |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 698 | return AnalyzeReverseStreamLocked(); |
| 699 | } |
| 700 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 701 | int AudioProcessingImpl::AnalyzeReverseStream(AudioFrame* frame) { |
andrew@webrtc.org | 4065403 | 2012-01-30 20:51:15 +0000 | [diff] [blame] | 702 | CriticalSectionScoped crit_scoped(crit_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 703 | if (frame == NULL) { |
| 704 | return kNullPointerError; |
| 705 | } |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 706 | // Must be a native rate. |
| 707 | if (frame->sample_rate_hz_ != kSampleRate8kHz && |
| 708 | frame->sample_rate_hz_ != kSampleRate16kHz && |
aluebs@webrtc.org | 087da13 | 2014-11-17 23:01:23 +0000 | [diff] [blame] | 709 | frame->sample_rate_hz_ != kSampleRate32kHz && |
| 710 | frame->sample_rate_hz_ != kSampleRate48kHz) { |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 711 | return kBadSampleRateError; |
| 712 | } |
| 713 | // This interface does not tolerate different forward and reverse rates. |
| 714 | if (frame->sample_rate_hz_ != fwd_in_format_.rate()) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 715 | return kBadSampleRateError; |
| 716 | } |
andrew@webrtc.org | a8b9737 | 2014-03-10 22:26:12 +0000 | [diff] [blame] | 717 | |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 718 | RETURN_ON_ERR(MaybeInitializeLocked(fwd_in_format_.rate(), |
| 719 | fwd_out_format_.rate(), |
| 720 | frame->sample_rate_hz_, |
| 721 | fwd_in_format_.num_channels(), |
| 722 | fwd_in_format_.num_channels(), |
| 723 | frame->num_channels_)); |
| 724 | if (frame->samples_per_channel_ != rev_in_format_.samples_per_channel()) { |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 725 | return kBadDataLengthError; |
| 726 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 727 | |
andrew@webrtc.org | 7bf2646 | 2011-12-03 00:03:31 +0000 | [diff] [blame] | 728 | #ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 729 | if (debug_file_->Open()) { |
ajm@google.com | 808e0e0 | 2011-08-03 21:08:51 +0000 | [diff] [blame] | 730 | event_msg_->set_type(audioproc::Event::REVERSE_STREAM); |
| 731 | audioproc::ReverseStream* msg = event_msg_->mutable_reverse_stream(); |
andrew@webrtc.org | 755b04a | 2011-11-15 16:57:56 +0000 | [diff] [blame] | 732 | const size_t data_size = sizeof(int16_t) * |
andrew@webrtc.org | 63a5098 | 2012-05-02 23:56:37 +0000 | [diff] [blame] | 733 | frame->samples_per_channel_ * |
| 734 | frame->num_channels_; |
| 735 | msg->set_data(frame->data_, data_size); |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 736 | RETURN_ON_ERR(WriteMessageToDebugFile()); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 737 | } |
andrew@webrtc.org | 7bf2646 | 2011-12-03 00:03:31 +0000 | [diff] [blame] | 738 | #endif |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 739 | |
| 740 | render_audio_->DeinterleaveFrom(frame); |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 741 | return AnalyzeReverseStreamLocked(); |
| 742 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 743 | |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 744 | int AudioProcessingImpl::AnalyzeReverseStreamLocked() { |
andrew@webrtc.org | 103657b | 2014-04-24 18:28:56 +0000 | [diff] [blame] | 745 | AudioBuffer* ra = render_audio_.get(); // For brevity. |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 746 | if (rev_proc_format_.rate() == kSampleRate32kHz) { |
aluebs@webrtc.org | be05c74 | 2014-11-14 22:18:10 +0000 | [diff] [blame] | 747 | ra->SplitIntoFrequencyBands(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 748 | } |
| 749 | |
andrew@webrtc.org | 103657b | 2014-04-24 18:28:56 +0000 | [diff] [blame] | 750 | RETURN_ON_ERR(echo_cancellation_->ProcessRenderAudio(ra)); |
| 751 | RETURN_ON_ERR(echo_control_mobile_->ProcessRenderAudio(ra)); |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 752 | if (!use_new_agc_) { |
| 753 | RETURN_ON_ERR(gain_control_->ProcessRenderAudio(ra)); |
| 754 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 755 | |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 756 | return kNoError; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 757 | } |
| 758 | |
| 759 | int AudioProcessingImpl::set_stream_delay_ms(int delay) { |
andrew@webrtc.org | 5f23d64 | 2012-05-29 21:14:06 +0000 | [diff] [blame] | 760 | Error retval = kNoError; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 761 | was_stream_delay_set_ = true; |
andrew@webrtc.org | 6f9f817 | 2012-03-06 19:03:39 +0000 | [diff] [blame] | 762 | delay += delay_offset_ms_; |
| 763 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 764 | if (delay < 0) { |
andrew@webrtc.org | 5f23d64 | 2012-05-29 21:14:06 +0000 | [diff] [blame] | 765 | delay = 0; |
| 766 | retval = kBadStreamParameterWarning; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 767 | } |
| 768 | |
| 769 | // TODO(ajm): the max is rather arbitrarily chosen; investigate. |
| 770 | if (delay > 500) { |
andrew@webrtc.org | 5f23d64 | 2012-05-29 21:14:06 +0000 | [diff] [blame] | 771 | delay = 500; |
| 772 | retval = kBadStreamParameterWarning; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 773 | } |
| 774 | |
| 775 | stream_delay_ms_ = delay; |
andrew@webrtc.org | 5f23d64 | 2012-05-29 21:14:06 +0000 | [diff] [blame] | 776 | return retval; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 777 | } |
| 778 | |
| 779 | int AudioProcessingImpl::stream_delay_ms() const { |
| 780 | return stream_delay_ms_; |
| 781 | } |
| 782 | |
| 783 | bool AudioProcessingImpl::was_stream_delay_set() const { |
| 784 | return was_stream_delay_set_; |
| 785 | } |
| 786 | |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 787 | void AudioProcessingImpl::set_stream_key_pressed(bool key_pressed) { |
| 788 | key_pressed_ = key_pressed; |
| 789 | } |
| 790 | |
| 791 | bool AudioProcessingImpl::stream_key_pressed() const { |
| 792 | return key_pressed_; |
| 793 | } |
| 794 | |
andrew@webrtc.org | 6f9f817 | 2012-03-06 19:03:39 +0000 | [diff] [blame] | 795 | void AudioProcessingImpl::set_delay_offset_ms(int offset) { |
| 796 | CriticalSectionScoped crit_scoped(crit_); |
| 797 | delay_offset_ms_ = offset; |
| 798 | } |
| 799 | |
| 800 | int AudioProcessingImpl::delay_offset_ms() const { |
| 801 | return delay_offset_ms_; |
| 802 | } |
| 803 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 804 | int AudioProcessingImpl::StartDebugRecording( |
| 805 | const char filename[AudioProcessing::kMaxFilenameSize]) { |
andrew@webrtc.org | 4065403 | 2012-01-30 20:51:15 +0000 | [diff] [blame] | 806 | CriticalSectionScoped crit_scoped(crit_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 807 | assert(kMaxFilenameSize == FileWrapper::kMaxFileNameSize); |
| 808 | |
| 809 | if (filename == NULL) { |
| 810 | return kNullPointerError; |
| 811 | } |
| 812 | |
andrew@webrtc.org | 7bf2646 | 2011-12-03 00:03:31 +0000 | [diff] [blame] | 813 | #ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 814 | // Stop any ongoing recording. |
| 815 | if (debug_file_->Open()) { |
| 816 | if (debug_file_->CloseFile() == -1) { |
| 817 | return kFileError; |
| 818 | } |
| 819 | } |
| 820 | |
| 821 | if (debug_file_->OpenFile(filename, false) == -1) { |
| 822 | debug_file_->CloseFile(); |
| 823 | return kFileError; |
| 824 | } |
| 825 | |
ajm@google.com | 808e0e0 | 2011-08-03 21:08:51 +0000 | [diff] [blame] | 826 | int err = WriteInitMessage(); |
| 827 | if (err != kNoError) { |
| 828 | return err; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 829 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 830 | return kNoError; |
andrew@webrtc.org | 7bf2646 | 2011-12-03 00:03:31 +0000 | [diff] [blame] | 831 | #else |
| 832 | return kUnsupportedFunctionError; |
| 833 | #endif // WEBRTC_AUDIOPROC_DEBUG_DUMP |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 834 | } |
| 835 | |
henrikg@webrtc.org | 863b536 | 2013-12-06 16:05:17 +0000 | [diff] [blame] | 836 | int AudioProcessingImpl::StartDebugRecording(FILE* handle) { |
| 837 | CriticalSectionScoped crit_scoped(crit_); |
| 838 | |
| 839 | if (handle == NULL) { |
| 840 | return kNullPointerError; |
| 841 | } |
| 842 | |
| 843 | #ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP |
| 844 | // Stop any ongoing recording. |
| 845 | if (debug_file_->Open()) { |
| 846 | if (debug_file_->CloseFile() == -1) { |
| 847 | return kFileError; |
| 848 | } |
| 849 | } |
| 850 | |
| 851 | if (debug_file_->OpenFromFileHandle(handle, true, false) == -1) { |
| 852 | return kFileError; |
| 853 | } |
| 854 | |
| 855 | int err = WriteInitMessage(); |
| 856 | if (err != kNoError) { |
| 857 | return err; |
| 858 | } |
| 859 | return kNoError; |
| 860 | #else |
| 861 | return kUnsupportedFunctionError; |
| 862 | #endif // WEBRTC_AUDIOPROC_DEBUG_DUMP |
| 863 | } |
| 864 | |
xians@webrtc.org | e46bc77 | 2014-10-10 08:36:56 +0000 | [diff] [blame] | 865 | int AudioProcessingImpl::StartDebugRecordingForPlatformFile( |
| 866 | rtc::PlatformFile handle) { |
| 867 | FILE* stream = rtc::FdopenPlatformFileForWriting(handle); |
| 868 | return StartDebugRecording(stream); |
| 869 | } |
| 870 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 871 | int AudioProcessingImpl::StopDebugRecording() { |
andrew@webrtc.org | 4065403 | 2012-01-30 20:51:15 +0000 | [diff] [blame] | 872 | CriticalSectionScoped crit_scoped(crit_); |
andrew@webrtc.org | 7bf2646 | 2011-12-03 00:03:31 +0000 | [diff] [blame] | 873 | |
| 874 | #ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 875 | // We just return if recording hasn't started. |
| 876 | if (debug_file_->Open()) { |
| 877 | if (debug_file_->CloseFile() == -1) { |
| 878 | return kFileError; |
| 879 | } |
| 880 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 881 | return kNoError; |
andrew@webrtc.org | 7bf2646 | 2011-12-03 00:03:31 +0000 | [diff] [blame] | 882 | #else |
| 883 | return kUnsupportedFunctionError; |
| 884 | #endif // WEBRTC_AUDIOPROC_DEBUG_DUMP |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 885 | } |
| 886 | |
| 887 | EchoCancellation* AudioProcessingImpl::echo_cancellation() const { |
| 888 | return echo_cancellation_; |
| 889 | } |
| 890 | |
| 891 | EchoControlMobile* AudioProcessingImpl::echo_control_mobile() const { |
| 892 | return echo_control_mobile_; |
| 893 | } |
| 894 | |
| 895 | GainControl* AudioProcessingImpl::gain_control() const { |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 896 | if (use_new_agc_) { |
| 897 | return gain_control_for_new_agc_.get(); |
| 898 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 899 | return gain_control_; |
| 900 | } |
| 901 | |
| 902 | HighPassFilter* AudioProcessingImpl::high_pass_filter() const { |
| 903 | return high_pass_filter_; |
| 904 | } |
| 905 | |
| 906 | LevelEstimator* AudioProcessingImpl::level_estimator() const { |
| 907 | return level_estimator_; |
| 908 | } |
| 909 | |
| 910 | NoiseSuppression* AudioProcessingImpl::noise_suppression() const { |
| 911 | return noise_suppression_; |
| 912 | } |
| 913 | |
| 914 | VoiceDetection* AudioProcessingImpl::voice_detection() const { |
| 915 | return voice_detection_; |
| 916 | } |
| 917 | |
andrew@webrtc.org | 369166a | 2012-04-24 18:38:03 +0000 | [diff] [blame] | 918 | bool AudioProcessingImpl::is_data_processed() const { |
aluebs@webrtc.org | ae643ce | 2014-12-19 19:57:34 +0000 | [diff] [blame] | 919 | if (beamformer_enabled_) { |
| 920 | return true; |
| 921 | } |
| 922 | |
andrew@webrtc.org | 7bf2646 | 2011-12-03 00:03:31 +0000 | [diff] [blame] | 923 | int enabled_count = 0; |
| 924 | std::list<ProcessingComponent*>::const_iterator it; |
| 925 | for (it = component_list_.begin(); it != component_list_.end(); it++) { |
| 926 | if ((*it)->is_component_enabled()) { |
| 927 | enabled_count++; |
| 928 | } |
| 929 | } |
| 930 | |
| 931 | // Data is unchanged if no components are enabled, or if only level_estimator_ |
| 932 | // or voice_detection_ is enabled. |
| 933 | if (enabled_count == 0) { |
| 934 | return false; |
| 935 | } else if (enabled_count == 1) { |
| 936 | if (level_estimator_->is_enabled() || voice_detection_->is_enabled()) { |
| 937 | return false; |
| 938 | } |
| 939 | } else if (enabled_count == 2) { |
| 940 | if (level_estimator_->is_enabled() && voice_detection_->is_enabled()) { |
| 941 | return false; |
| 942 | } |
| 943 | } |
| 944 | return true; |
| 945 | } |
| 946 | |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 947 | bool AudioProcessingImpl::output_copy_needed(bool is_data_processed) const { |
andrew@webrtc.org | 369166a | 2012-04-24 18:38:03 +0000 | [diff] [blame] | 948 | // Check if we've upmixed or downmixed the audio. |
aluebs@webrtc.org | 27d106b | 2014-12-11 17:09:21 +0000 | [diff] [blame] | 949 | return ((fwd_out_format_.num_channels() != fwd_in_format_.num_channels()) || |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 950 | is_data_processed || transient_suppressor_enabled_); |
andrew@webrtc.org | 7bf2646 | 2011-12-03 00:03:31 +0000 | [diff] [blame] | 951 | } |
| 952 | |
andrew@webrtc.org | 369166a | 2012-04-24 18:38:03 +0000 | [diff] [blame] | 953 | bool AudioProcessingImpl::synthesis_needed(bool is_data_processed) const { |
aluebs@webrtc.org | 087da13 | 2014-11-17 23:01:23 +0000 | [diff] [blame] | 954 | return (is_data_processed && (fwd_proc_format_.rate() == kSampleRate32kHz || |
| 955 | fwd_proc_format_.rate() == kSampleRate48kHz)); |
andrew@webrtc.org | 369166a | 2012-04-24 18:38:03 +0000 | [diff] [blame] | 956 | } |
| 957 | |
| 958 | bool AudioProcessingImpl::analysis_needed(bool is_data_processed) const { |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 959 | if (!is_data_processed && !voice_detection_->is_enabled() && |
| 960 | !transient_suppressor_enabled_) { |
andrew@webrtc.org | 7bf2646 | 2011-12-03 00:03:31 +0000 | [diff] [blame] | 961 | // Only level_estimator_ is enabled. |
| 962 | return false; |
aluebs@webrtc.org | 087da13 | 2014-11-17 23:01:23 +0000 | [diff] [blame] | 963 | } else if (fwd_proc_format_.rate() == kSampleRate32kHz || |
| 964 | fwd_proc_format_.rate() == kSampleRate48kHz) { |
andrew@webrtc.org | 7bf2646 | 2011-12-03 00:03:31 +0000 | [diff] [blame] | 965 | // Something besides level_estimator_ is enabled, and we have super-wb. |
| 966 | return true; |
| 967 | } |
| 968 | return false; |
| 969 | } |
| 970 | |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 971 | int AudioProcessingImpl::InitializeExperimentalAgc() { |
| 972 | if (use_new_agc_) { |
| 973 | if (!agc_manager_.get()) { |
| 974 | agc_manager_.reset( |
| 975 | new AgcManagerDirect(gain_control_, gain_control_for_new_agc_.get())); |
| 976 | } |
| 977 | agc_manager_->Initialize(); |
| 978 | agc_manager_->SetCaptureMuted(output_will_be_muted_); |
| 979 | } |
| 980 | return kNoError; |
| 981 | } |
| 982 | |
| 983 | int AudioProcessingImpl::InitializeTransient() { |
| 984 | if (transient_suppressor_enabled_) { |
| 985 | if (!transient_suppressor_.get()) { |
| 986 | transient_suppressor_.reset(new TransientSuppressor()); |
| 987 | } |
| 988 | transient_suppressor_->Initialize(fwd_proc_format_.rate(), |
| 989 | split_rate_, |
| 990 | fwd_out_format_.num_channels()); |
| 991 | } |
| 992 | return kNoError; |
| 993 | } |
| 994 | |
aluebs@webrtc.org | ae643ce | 2014-12-19 19:57:34 +0000 | [diff] [blame] | 995 | void AudioProcessingImpl::InitializeBeamformer() { |
| 996 | if (beamformer_enabled_) { |
| 997 | #ifdef WEBRTC_BEAMFORMER |
| 998 | // TODO(aluebs): Don't use a hard-coded microphone spacing. |
| 999 | beamformer_.reset(new Beamformer(kChunkSizeMs, |
| 1000 | split_rate_, |
| 1001 | fwd_in_format_.num_channels(), |
| 1002 | 0.05f)); |
| 1003 | #else |
| 1004 | assert(false); |
| 1005 | #endif |
| 1006 | } |
| 1007 | } |
| 1008 | |
andrew@webrtc.org | 7bf2646 | 2011-12-03 00:03:31 +0000 | [diff] [blame] | 1009 | #ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP |
ajm@google.com | 808e0e0 | 2011-08-03 21:08:51 +0000 | [diff] [blame] | 1010 | int AudioProcessingImpl::WriteMessageToDebugFile() { |
| 1011 | int32_t size = event_msg_->ByteSize(); |
| 1012 | if (size <= 0) { |
| 1013 | return kUnspecifiedError; |
| 1014 | } |
andrew@webrtc.org | 621df67 | 2013-10-22 10:27:23 +0000 | [diff] [blame] | 1015 | #if defined(WEBRTC_ARCH_BIG_ENDIAN) |
ajm@google.com | 808e0e0 | 2011-08-03 21:08:51 +0000 | [diff] [blame] | 1016 | // TODO(ajm): Use little-endian "on the wire". For the moment, we can be |
| 1017 | // pretty safe in assuming little-endian. |
| 1018 | #endif |
| 1019 | |
| 1020 | if (!event_msg_->SerializeToString(&event_str_)) { |
| 1021 | return kUnspecifiedError; |
| 1022 | } |
| 1023 | |
| 1024 | // Write message preceded by its size. |
| 1025 | if (!debug_file_->Write(&size, sizeof(int32_t))) { |
| 1026 | return kFileError; |
| 1027 | } |
| 1028 | if (!debug_file_->Write(event_str_.data(), event_str_.length())) { |
| 1029 | return kFileError; |
| 1030 | } |
| 1031 | |
| 1032 | event_msg_->Clear(); |
| 1033 | |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 1034 | return kNoError; |
ajm@google.com | 808e0e0 | 2011-08-03 21:08:51 +0000 | [diff] [blame] | 1035 | } |
| 1036 | |
| 1037 | int AudioProcessingImpl::WriteInitMessage() { |
| 1038 | event_msg_->set_type(audioproc::Event::INIT); |
| 1039 | audioproc::Init* msg = event_msg_->mutable_init(); |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 1040 | msg->set_sample_rate(fwd_in_format_.rate()); |
| 1041 | msg->set_num_input_channels(fwd_in_format_.num_channels()); |
aluebs@webrtc.org | 27d106b | 2014-12-11 17:09:21 +0000 | [diff] [blame] | 1042 | msg->set_num_output_channels(fwd_out_format_.num_channels()); |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 1043 | msg->set_num_reverse_channels(rev_in_format_.num_channels()); |
| 1044 | msg->set_reverse_sample_rate(rev_in_format_.rate()); |
| 1045 | msg->set_output_sample_rate(fwd_out_format_.rate()); |
ajm@google.com | 808e0e0 | 2011-08-03 21:08:51 +0000 | [diff] [blame] | 1046 | |
| 1047 | int err = WriteMessageToDebugFile(); |
| 1048 | if (err != kNoError) { |
| 1049 | return err; |
| 1050 | } |
| 1051 | |
| 1052 | return kNoError; |
| 1053 | } |
andrew@webrtc.org | 7bf2646 | 2011-12-03 00:03:31 +0000 | [diff] [blame] | 1054 | #endif // WEBRTC_AUDIOPROC_DEBUG_DUMP |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 1055 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1056 | } // namespace webrtc |