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