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