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