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