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