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 | |
peah | 103ac7e | 2017-04-12 05:40:55 -0700 | [diff] [blame] | 13 | #include <math.h> |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 14 | #include <algorithm> |
alessiob | 3ec96df | 2017-05-22 06:57:06 -0700 | [diff] [blame] | 15 | #include <string> |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 16 | |
Bjorn Volcker | 1ca324f | 2015-06-29 14:57:29 +0200 | [diff] [blame] | 17 | #include "webrtc/base/checks.h" |
nisse | 368f5cf | 2017-04-05 05:00:33 -0700 | [diff] [blame] | 18 | #include "webrtc/base/logging.h" |
xians@webrtc.org | e46bc77 | 2014-10-10 08:36:56 +0000 | [diff] [blame] | 19 | #include "webrtc/base/platform_file.h" |
peah | 369f828 | 2015-12-17 06:42:29 -0800 | [diff] [blame] | 20 | #include "webrtc/base/trace_event.h" |
ekmeyerson | 60d9b33 | 2015-08-14 10:35:55 -0700 | [diff] [blame] | 21 | #include "webrtc/common_audio/audio_converter.h" |
Michael Graczyk | dfa3605 | 2015-03-25 16:37:27 -0700 | [diff] [blame] | 22 | #include "webrtc/common_audio/channel_buffer.h" |
ekmeyerson | 60d9b33 | 2015-08-14 10:35:55 -0700 | [diff] [blame] | 23 | #include "webrtc/common_audio/include/audio_util.h" |
andrew@webrtc.org | 60730cf | 2014-01-07 17:45:09 +0000 | [diff] [blame] | 24 | #include "webrtc/common_audio/signal_processing/include/signal_processing_library.h" |
Bjorn Volcker | 1ca324f | 2015-06-29 14:57:29 +0200 | [diff] [blame] | 25 | #include "webrtc/modules/audio_processing/aec/aec_core.h" |
peah | e0eae3c | 2016-12-14 01:16:23 -0800 | [diff] [blame] | 26 | #include "webrtc/modules/audio_processing/aec3/echo_canceller3.h" |
alessiob | 3ec96df | 2017-05-22 06:57:06 -0700 | [diff] [blame] | 27 | #include "webrtc/modules/audio_processing/agc2/gain_controller2.h" |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 28 | #include "webrtc/modules/audio_processing/agc/agc_manager_direct.h" |
andrew@webrtc.org | 78693fe | 2013-03-01 16:36:19 +0000 | [diff] [blame] | 29 | #include "webrtc/modules/audio_processing/audio_buffer.h" |
mgraczyk@chromium.org | 0f663de | 2015-03-13 00:13:32 +0000 | [diff] [blame] | 30 | #include "webrtc/modules/audio_processing/beamformer/nonlinear_beamformer.h" |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 31 | #include "webrtc/modules/audio_processing/common.h" |
andrew@webrtc.org | 56e4a05 | 2014-02-27 22:23:17 +0000 | [diff] [blame] | 32 | #include "webrtc/modules/audio_processing/echo_cancellation_impl.h" |
andrew@webrtc.org | 78693fe | 2013-03-01 16:36:19 +0000 | [diff] [blame] | 33 | #include "webrtc/modules/audio_processing/echo_control_mobile_impl.h" |
peah | be61562 | 2016-02-13 16:40:47 -0800 | [diff] [blame] | 34 | #include "webrtc/modules/audio_processing/gain_control_for_experimental_agc.h" |
andrew@webrtc.org | 78693fe | 2013-03-01 16:36:19 +0000 | [diff] [blame] | 35 | #include "webrtc/modules/audio_processing/gain_control_impl.h" |
peah | 1bcfce5 | 2016-08-26 07:16:04 -0700 | [diff] [blame] | 36 | #if WEBRTC_INTELLIGIBILITY_ENHANCER |
ekmeyerson | 60d9b33 | 2015-08-14 10:35:55 -0700 | [diff] [blame] | 37 | #include "webrtc/modules/audio_processing/intelligibility/intelligibility_enhancer.h" |
peah | 1bcfce5 | 2016-08-26 07:16:04 -0700 | [diff] [blame] | 38 | #endif |
peah | ca4cac7 | 2016-06-29 15:26:12 -0700 | [diff] [blame] | 39 | #include "webrtc/modules/audio_processing/level_controller/level_controller.h" |
andrew@webrtc.org | 78693fe | 2013-03-01 16:36:19 +0000 | [diff] [blame] | 40 | #include "webrtc/modules/audio_processing/level_estimator_impl.h" |
peah | 8271d04 | 2016-11-22 07:24:52 -0800 | [diff] [blame] | 41 | #include "webrtc/modules/audio_processing/low_cut_filter.h" |
andrew@webrtc.org | 78693fe | 2013-03-01 16:36:19 +0000 | [diff] [blame] | 42 | #include "webrtc/modules/audio_processing/noise_suppression_impl.h" |
ivoc | 9f4a4a0 | 2016-10-28 05:39:16 -0700 | [diff] [blame] | 43 | #include "webrtc/modules/audio_processing/residual_echo_detector.h" |
aluebs@webrtc.org | ae643ce | 2014-12-19 19:57:34 +0000 | [diff] [blame] | 44 | #include "webrtc/modules/audio_processing/transient/transient_suppressor.h" |
andrew@webrtc.org | 78693fe | 2013-03-01 16:36:19 +0000 | [diff] [blame] | 45 | #include "webrtc/modules/audio_processing/voice_detection_impl.h" |
Henrik Kjellander | ff761fb | 2015-11-04 08:31:52 +0100 | [diff] [blame] | 46 | #include "webrtc/modules/include/module_common_types.h" |
Henrik Kjellander | 98f5351 | 2015-10-28 18:17:40 +0100 | [diff] [blame] | 47 | #include "webrtc/system_wrappers/include/file_wrapper.h" |
Henrik Kjellander | 98f5351 | 2015-10-28 18:17:40 +0100 | [diff] [blame] | 48 | #include "webrtc/system_wrappers/include/metrics.h" |
andrew@webrtc.org | 7bf2646 | 2011-12-03 00:03:31 +0000 | [diff] [blame] | 49 | |
| 50 | #ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP |
| 51 | // Files generated at build-time by the protobuf compiler. |
leozwang@webrtc.org | a373634 | 2012-03-16 21:36:00 +0000 | [diff] [blame] | 52 | #ifdef WEBRTC_ANDROID_PLATFORM_BUILD |
leozwang@webrtc.org | 534e495 | 2012-10-22 21:21:52 +0000 | [diff] [blame] | 53 | #include "external/webrtc/webrtc/modules/audio_processing/debug.pb.h" |
leozwang@google.com | ce9bfbb | 2011-08-03 23:34:31 +0000 | [diff] [blame] | 54 | #else |
kjellander | 78ddd73 | 2016-02-09 08:13:06 -0800 | [diff] [blame] | 55 | #include "webrtc/modules/audio_processing/debug.pb.h" |
leozwang@google.com | ce9bfbb | 2011-08-03 23:34:31 +0000 | [diff] [blame] | 56 | #endif |
andrew@webrtc.org | 7bf2646 | 2011-12-03 00:03:31 +0000 | [diff] [blame] | 57 | #endif // WEBRTC_AUDIOPROC_DEBUG_DUMP |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 58 | |
peah | 1bcfce5 | 2016-08-26 07:16:04 -0700 | [diff] [blame] | 59 | // Check to verify that the define for the intelligibility enhancer is properly |
| 60 | // set. |
| 61 | #if !defined(WEBRTC_INTELLIGIBILITY_ENHANCER) || \ |
| 62 | (WEBRTC_INTELLIGIBILITY_ENHANCER != 0 && \ |
| 63 | WEBRTC_INTELLIGIBILITY_ENHANCER != 1) |
| 64 | #error "Set WEBRTC_INTELLIGIBILITY_ENHANCER to either 0 or 1" |
| 65 | #endif |
| 66 | |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 67 | #define RETURN_ON_ERR(expr) \ |
| 68 | do { \ |
| 69 | int err = (expr); \ |
| 70 | if (err != kNoError) { \ |
| 71 | return err; \ |
| 72 | } \ |
andrew@webrtc.org | 60730cf | 2014-01-07 17:45:09 +0000 | [diff] [blame] | 73 | } while (0) |
| 74 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 75 | namespace webrtc { |
aluebs | df6416a | 2016-03-16 18:26:35 -0700 | [diff] [blame] | 76 | |
kwiberg | d59d3bb | 2016-09-13 07:49:33 -0700 | [diff] [blame] | 77 | constexpr int AudioProcessing::kNativeSampleRatesHz[]; |
aluebs | df6416a | 2016-03-16 18:26:35 -0700 | [diff] [blame] | 78 | |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 79 | namespace { |
| 80 | |
| 81 | static bool LayoutHasKeyboard(AudioProcessing::ChannelLayout layout) { |
| 82 | switch (layout) { |
| 83 | case AudioProcessing::kMono: |
| 84 | case AudioProcessing::kStereo: |
| 85 | return false; |
| 86 | case AudioProcessing::kMonoAndKeyboard: |
| 87 | case AudioProcessing::kStereoAndKeyboard: |
| 88 | return true; |
| 89 | } |
| 90 | |
kwiberg | 9e2be5f | 2016-09-14 05:23:22 -0700 | [diff] [blame] | 91 | RTC_NOTREACHED(); |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 92 | return false; |
| 93 | } |
aluebs | df6416a | 2016-03-16 18:26:35 -0700 | [diff] [blame] | 94 | |
peah | 2ace3f9 | 2016-09-10 04:42:27 -0700 | [diff] [blame] | 95 | bool SampleRateSupportsMultiBand(int sample_rate_hz) { |
aluebs | df6416a | 2016-03-16 18:26:35 -0700 | [diff] [blame] | 96 | return sample_rate_hz == AudioProcessing::kSampleRate32kHz || |
| 97 | sample_rate_hz == AudioProcessing::kSampleRate48kHz; |
| 98 | } |
| 99 | |
peah | 2ace3f9 | 2016-09-10 04:42:27 -0700 | [diff] [blame] | 100 | int FindNativeProcessRateToUse(int minimum_rate, bool band_splitting_required) { |
| 101 | #ifdef WEBRTC_ARCH_ARM_FAMILY |
kwiberg | d59d3bb | 2016-09-13 07:49:33 -0700 | [diff] [blame] | 102 | constexpr int kMaxSplittingNativeProcessRate = |
| 103 | AudioProcessing::kSampleRate32kHz; |
peah | 2ace3f9 | 2016-09-10 04:42:27 -0700 | [diff] [blame] | 104 | #else |
kwiberg | d59d3bb | 2016-09-13 07:49:33 -0700 | [diff] [blame] | 105 | constexpr int kMaxSplittingNativeProcessRate = |
| 106 | AudioProcessing::kSampleRate48kHz; |
peah | 2ace3f9 | 2016-09-10 04:42:27 -0700 | [diff] [blame] | 107 | #endif |
kwiberg | d59d3bb | 2016-09-13 07:49:33 -0700 | [diff] [blame] | 108 | static_assert( |
| 109 | kMaxSplittingNativeProcessRate <= AudioProcessing::kMaxNativeSampleRateHz, |
| 110 | ""); |
peah | 2ace3f9 | 2016-09-10 04:42:27 -0700 | [diff] [blame] | 111 | const int uppermost_native_rate = band_splitting_required |
| 112 | ? kMaxSplittingNativeProcessRate |
| 113 | : AudioProcessing::kSampleRate48kHz; |
| 114 | |
| 115 | for (auto rate : AudioProcessing::kNativeSampleRatesHz) { |
| 116 | if (rate >= uppermost_native_rate) { |
| 117 | return uppermost_native_rate; |
| 118 | } |
| 119 | if (rate >= minimum_rate) { |
aluebs | df6416a | 2016-03-16 18:26:35 -0700 | [diff] [blame] | 120 | return rate; |
| 121 | } |
| 122 | } |
peah | 2ace3f9 | 2016-09-10 04:42:27 -0700 | [diff] [blame] | 123 | RTC_NOTREACHED(); |
| 124 | return uppermost_native_rate; |
aluebs | df6416a | 2016-03-16 18:26:35 -0700 | [diff] [blame] | 125 | } |
| 126 | |
peah | 9e6a290 | 2017-05-15 07:19:21 -0700 | [diff] [blame] | 127 | // Maximum lengths that frame of samples being passed from the render side to |
| 128 | // the capture side can have (does not apply to AEC3). |
| 129 | static const size_t kMaxAllowedValuesOfSamplesPerBand = 160; |
| 130 | static const size_t kMaxAllowedValuesOfSamplesPerFrame = 480; |
| 131 | |
peah | 764e364 | 2016-10-22 05:04:30 -0700 | [diff] [blame] | 132 | // Maximum number of frames to buffer in the render queue. |
| 133 | // TODO(peah): Decrease this once we properly handle hugely unbalanced |
| 134 | // reverse and forward call numbers. |
| 135 | static const size_t kMaxNumFramesToBuffer = 100; |
| 136 | |
peah | 8271d04 | 2016-11-22 07:24:52 -0800 | [diff] [blame] | 137 | class HighPassFilterImpl : public HighPassFilter { |
| 138 | public: |
| 139 | explicit HighPassFilterImpl(AudioProcessingImpl* apm) : apm_(apm) {} |
| 140 | ~HighPassFilterImpl() override = default; |
| 141 | |
| 142 | // HighPassFilter implementation. |
| 143 | int Enable(bool enable) override { |
| 144 | apm_->MutateConfig([enable](AudioProcessing::Config* config) { |
| 145 | config->high_pass_filter.enabled = enable; |
| 146 | }); |
| 147 | |
| 148 | return AudioProcessing::kNoError; |
| 149 | } |
| 150 | |
| 151 | bool is_enabled() const override { |
| 152 | return apm_->GetConfig().high_pass_filter.enabled; |
| 153 | } |
| 154 | |
| 155 | private: |
| 156 | AudioProcessingImpl* apm_; |
| 157 | RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(HighPassFilterImpl); |
| 158 | }; |
| 159 | |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 160 | } // namespace |
andrew@webrtc.org | 60730cf | 2014-01-07 17:45:09 +0000 | [diff] [blame] | 161 | |
| 162 | // Throughout webrtc, it's assumed that success is represented by zero. |
kwiberg@webrtc.org | 2ebfac5 | 2015-01-14 10:51:54 +0000 | [diff] [blame] | 163 | static_assert(AudioProcessing::kNoError == 0, "kNoError must be zero"); |
andrew@webrtc.org | 60730cf | 2014-01-07 17:45:09 +0000 | [diff] [blame] | 164 | |
peah | 2ace3f9 | 2016-09-10 04:42:27 -0700 | [diff] [blame] | 165 | AudioProcessingImpl::ApmSubmoduleStates::ApmSubmoduleStates() {} |
| 166 | |
| 167 | bool AudioProcessingImpl::ApmSubmoduleStates::Update( |
peah | 8271d04 | 2016-11-22 07:24:52 -0800 | [diff] [blame] | 168 | bool low_cut_filter_enabled, |
peah | 2ace3f9 | 2016-09-10 04:42:27 -0700 | [diff] [blame] | 169 | bool echo_canceller_enabled, |
| 170 | bool mobile_echo_controller_enabled, |
ivoc | 9f4a4a0 | 2016-10-28 05:39:16 -0700 | [diff] [blame] | 171 | bool residual_echo_detector_enabled, |
peah | 2ace3f9 | 2016-09-10 04:42:27 -0700 | [diff] [blame] | 172 | bool noise_suppressor_enabled, |
| 173 | bool intelligibility_enhancer_enabled, |
| 174 | bool beamformer_enabled, |
| 175 | bool adaptive_gain_controller_enabled, |
alessiob | 3ec96df | 2017-05-22 06:57:06 -0700 | [diff] [blame] | 176 | bool gain_controller2_enabled, |
peah | 2ace3f9 | 2016-09-10 04:42:27 -0700 | [diff] [blame] | 177 | bool level_controller_enabled, |
peah | e0eae3c | 2016-12-14 01:16:23 -0800 | [diff] [blame] | 178 | bool echo_canceller3_enabled, |
peah | 2ace3f9 | 2016-09-10 04:42:27 -0700 | [diff] [blame] | 179 | bool voice_activity_detector_enabled, |
| 180 | bool level_estimator_enabled, |
| 181 | bool transient_suppressor_enabled) { |
| 182 | bool changed = false; |
peah | 8271d04 | 2016-11-22 07:24:52 -0800 | [diff] [blame] | 183 | changed |= (low_cut_filter_enabled != low_cut_filter_enabled_); |
peah | 2ace3f9 | 2016-09-10 04:42:27 -0700 | [diff] [blame] | 184 | changed |= (echo_canceller_enabled != echo_canceller_enabled_); |
| 185 | changed |= |
| 186 | (mobile_echo_controller_enabled != mobile_echo_controller_enabled_); |
ivoc | 9f4a4a0 | 2016-10-28 05:39:16 -0700 | [diff] [blame] | 187 | changed |= |
| 188 | (residual_echo_detector_enabled != residual_echo_detector_enabled_); |
peah | 2ace3f9 | 2016-09-10 04:42:27 -0700 | [diff] [blame] | 189 | changed |= (noise_suppressor_enabled != noise_suppressor_enabled_); |
| 190 | changed |= |
| 191 | (intelligibility_enhancer_enabled != intelligibility_enhancer_enabled_); |
| 192 | changed |= (beamformer_enabled != beamformer_enabled_); |
| 193 | changed |= |
| 194 | (adaptive_gain_controller_enabled != adaptive_gain_controller_enabled_); |
alessiob | 3ec96df | 2017-05-22 06:57:06 -0700 | [diff] [blame] | 195 | changed |= |
| 196 | (gain_controller2_enabled != gain_controller2_enabled_); |
peah | 2ace3f9 | 2016-09-10 04:42:27 -0700 | [diff] [blame] | 197 | changed |= (level_controller_enabled != level_controller_enabled_); |
peah | e0eae3c | 2016-12-14 01:16:23 -0800 | [diff] [blame] | 198 | changed |= (echo_canceller3_enabled != echo_canceller3_enabled_); |
peah | 2ace3f9 | 2016-09-10 04:42:27 -0700 | [diff] [blame] | 199 | changed |= (level_estimator_enabled != level_estimator_enabled_); |
| 200 | changed |= |
| 201 | (voice_activity_detector_enabled != voice_activity_detector_enabled_); |
| 202 | changed |= (transient_suppressor_enabled != transient_suppressor_enabled_); |
| 203 | if (changed) { |
peah | 8271d04 | 2016-11-22 07:24:52 -0800 | [diff] [blame] | 204 | low_cut_filter_enabled_ = low_cut_filter_enabled; |
peah | 2ace3f9 | 2016-09-10 04:42:27 -0700 | [diff] [blame] | 205 | echo_canceller_enabled_ = echo_canceller_enabled; |
| 206 | mobile_echo_controller_enabled_ = mobile_echo_controller_enabled; |
ivoc | 9f4a4a0 | 2016-10-28 05:39:16 -0700 | [diff] [blame] | 207 | residual_echo_detector_enabled_ = residual_echo_detector_enabled; |
peah | 2ace3f9 | 2016-09-10 04:42:27 -0700 | [diff] [blame] | 208 | noise_suppressor_enabled_ = noise_suppressor_enabled; |
| 209 | intelligibility_enhancer_enabled_ = intelligibility_enhancer_enabled; |
| 210 | beamformer_enabled_ = beamformer_enabled; |
| 211 | adaptive_gain_controller_enabled_ = adaptive_gain_controller_enabled; |
alessiob | 3ec96df | 2017-05-22 06:57:06 -0700 | [diff] [blame] | 212 | gain_controller2_enabled_ = gain_controller2_enabled; |
peah | 2ace3f9 | 2016-09-10 04:42:27 -0700 | [diff] [blame] | 213 | level_controller_enabled_ = level_controller_enabled; |
peah | e0eae3c | 2016-12-14 01:16:23 -0800 | [diff] [blame] | 214 | echo_canceller3_enabled_ = echo_canceller3_enabled; |
peah | 2ace3f9 | 2016-09-10 04:42:27 -0700 | [diff] [blame] | 215 | level_estimator_enabled_ = level_estimator_enabled; |
| 216 | voice_activity_detector_enabled_ = voice_activity_detector_enabled; |
| 217 | transient_suppressor_enabled_ = transient_suppressor_enabled; |
| 218 | } |
| 219 | |
| 220 | changed |= first_update_; |
| 221 | first_update_ = false; |
| 222 | return changed; |
| 223 | } |
| 224 | |
| 225 | bool AudioProcessingImpl::ApmSubmoduleStates::CaptureMultiBandSubModulesActive() |
| 226 | const { |
| 227 | #if WEBRTC_INTELLIGIBILITY_ENHANCER |
| 228 | return CaptureMultiBandProcessingActive() || |
peah | 5277584 | 2017-05-16 06:14:09 -0700 | [diff] [blame] | 229 | intelligibility_enhancer_enabled_ || voice_activity_detector_enabled_; |
peah | 2ace3f9 | 2016-09-10 04:42:27 -0700 | [diff] [blame] | 230 | #else |
peah | 5277584 | 2017-05-16 06:14:09 -0700 | [diff] [blame] | 231 | return CaptureMultiBandProcessingActive() || voice_activity_detector_enabled_; |
peah | 2ace3f9 | 2016-09-10 04:42:27 -0700 | [diff] [blame] | 232 | #endif |
| 233 | } |
| 234 | |
| 235 | bool AudioProcessingImpl::ApmSubmoduleStates::CaptureMultiBandProcessingActive() |
| 236 | const { |
peah | 8271d04 | 2016-11-22 07:24:52 -0800 | [diff] [blame] | 237 | return low_cut_filter_enabled_ || echo_canceller_enabled_ || |
peah | 2ace3f9 | 2016-09-10 04:42:27 -0700 | [diff] [blame] | 238 | mobile_echo_controller_enabled_ || noise_suppressor_enabled_ || |
peah | e0eae3c | 2016-12-14 01:16:23 -0800 | [diff] [blame] | 239 | beamformer_enabled_ || adaptive_gain_controller_enabled_ || |
| 240 | echo_canceller3_enabled_; |
peah | 2ace3f9 | 2016-09-10 04:42:27 -0700 | [diff] [blame] | 241 | } |
| 242 | |
peah | 23ac8b4 | 2017-05-23 05:33:56 -0700 | [diff] [blame^] | 243 | bool AudioProcessingImpl::ApmSubmoduleStates::CaptureFullBandProcessingActive() |
| 244 | const { |
| 245 | return level_controller_enabled_; |
| 246 | } |
| 247 | |
peah | 2ace3f9 | 2016-09-10 04:42:27 -0700 | [diff] [blame] | 248 | bool AudioProcessingImpl::ApmSubmoduleStates::RenderMultiBandSubModulesActive() |
| 249 | const { |
| 250 | return RenderMultiBandProcessingActive() || echo_canceller_enabled_ || |
ivoc | 20270be | 2016-11-15 05:24:35 -0800 | [diff] [blame] | 251 | mobile_echo_controller_enabled_ || adaptive_gain_controller_enabled_ || |
peah | 5277584 | 2017-05-16 06:14:09 -0700 | [diff] [blame] | 252 | echo_canceller3_enabled_; |
peah | 2ace3f9 | 2016-09-10 04:42:27 -0700 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | bool AudioProcessingImpl::ApmSubmoduleStates::RenderMultiBandProcessingActive() |
| 256 | const { |
| 257 | #if WEBRTC_INTELLIGIBILITY_ENHANCER |
| 258 | return intelligibility_enhancer_enabled_; |
| 259 | #else |
| 260 | return false; |
| 261 | #endif |
| 262 | } |
| 263 | |
solenberg | 5e465c3 | 2015-12-08 13:22:33 -0800 | [diff] [blame] | 264 | struct AudioProcessingImpl::ApmPublicSubmodules { |
peah | bfa9711 | 2016-03-10 21:09:04 -0800 | [diff] [blame] | 265 | ApmPublicSubmodules() {} |
solenberg | 5e465c3 | 2015-12-08 13:22:33 -0800 | [diff] [blame] | 266 | // Accessed externally of APM without any lock acquired. |
peah | b624d8c | 2016-03-05 03:01:14 -0800 | [diff] [blame] | 267 | std::unique_ptr<EchoCancellationImpl> echo_cancellation; |
peah | bb9edbd | 2016-03-10 12:54:25 -0800 | [diff] [blame] | 268 | std::unique_ptr<EchoControlMobileImpl> echo_control_mobile; |
peah | bfa9711 | 2016-03-10 21:09:04 -0800 | [diff] [blame] | 269 | std::unique_ptr<GainControlImpl> gain_control; |
kwiberg | 88788ad | 2016-02-19 07:04:49 -0800 | [diff] [blame] | 270 | std::unique_ptr<LevelEstimatorImpl> level_estimator; |
| 271 | std::unique_ptr<NoiseSuppressionImpl> noise_suppression; |
| 272 | std::unique_ptr<VoiceDetectionImpl> voice_detection; |
| 273 | std::unique_ptr<GainControlForExperimentalAgc> |
peah | be61562 | 2016-02-13 16:40:47 -0800 | [diff] [blame] | 274 | gain_control_for_experimental_agc; |
solenberg | 5e465c3 | 2015-12-08 13:22:33 -0800 | [diff] [blame] | 275 | |
| 276 | // Accessed internally from both render and capture. |
kwiberg | 88788ad | 2016-02-19 07:04:49 -0800 | [diff] [blame] | 277 | std::unique_ptr<TransientSuppressor> transient_suppressor; |
peah | 1bcfce5 | 2016-08-26 07:16:04 -0700 | [diff] [blame] | 278 | #if WEBRTC_INTELLIGIBILITY_ENHANCER |
kwiberg | 88788ad | 2016-02-19 07:04:49 -0800 | [diff] [blame] | 279 | std::unique_ptr<IntelligibilityEnhancer> intelligibility_enhancer; |
peah | 1bcfce5 | 2016-08-26 07:16:04 -0700 | [diff] [blame] | 280 | #endif |
solenberg | 5e465c3 | 2015-12-08 13:22:33 -0800 | [diff] [blame] | 281 | }; |
| 282 | |
| 283 | struct AudioProcessingImpl::ApmPrivateSubmodules { |
Alejandro Luebs | f4022ff | 2016-07-01 17:19:09 -0700 | [diff] [blame] | 284 | explicit ApmPrivateSubmodules(NonlinearBeamformer* beamformer) |
solenberg | 5e465c3 | 2015-12-08 13:22:33 -0800 | [diff] [blame] | 285 | : beamformer(beamformer) {} |
| 286 | // Accessed internally from capture or during initialization |
Alejandro Luebs | f4022ff | 2016-07-01 17:19:09 -0700 | [diff] [blame] | 287 | std::unique_ptr<NonlinearBeamformer> beamformer; |
kwiberg | 88788ad | 2016-02-19 07:04:49 -0800 | [diff] [blame] | 288 | std::unique_ptr<AgcManagerDirect> agc_manager; |
alessiob | 3ec96df | 2017-05-22 06:57:06 -0700 | [diff] [blame] | 289 | std::unique_ptr<GainController2> gain_controller2; |
peah | 8271d04 | 2016-11-22 07:24:52 -0800 | [diff] [blame] | 290 | std::unique_ptr<LowCutFilter> low_cut_filter; |
peah | ca4cac7 | 2016-06-29 15:26:12 -0700 | [diff] [blame] | 291 | std::unique_ptr<LevelController> level_controller; |
ivoc | 9f4a4a0 | 2016-10-28 05:39:16 -0700 | [diff] [blame] | 292 | std::unique_ptr<ResidualEchoDetector> residual_echo_detector; |
peah | e0eae3c | 2016-12-14 01:16:23 -0800 | [diff] [blame] | 293 | std::unique_ptr<EchoCanceller3> echo_canceller3; |
solenberg | 5e465c3 | 2015-12-08 13:22:33 -0800 | [diff] [blame] | 294 | }; |
| 295 | |
andrew@webrtc.org | e84978f | 2014-01-25 02:09:06 +0000 | [diff] [blame] | 296 | AudioProcessing* AudioProcessing::Create() { |
peah | 88ac853 | 2016-09-12 16:47:25 -0700 | [diff] [blame] | 297 | webrtc::Config config; |
aluebs@webrtc.org | d82f55d | 2015-01-15 18:07:21 +0000 | [diff] [blame] | 298 | return Create(config, nullptr); |
andrew@webrtc.org | e84978f | 2014-01-25 02:09:06 +0000 | [diff] [blame] | 299 | } |
| 300 | |
peah | 88ac853 | 2016-09-12 16:47:25 -0700 | [diff] [blame] | 301 | AudioProcessing* AudioProcessing::Create(const webrtc::Config& config) { |
aluebs@webrtc.org | d82f55d | 2015-01-15 18:07:21 +0000 | [diff] [blame] | 302 | return Create(config, nullptr); |
| 303 | } |
| 304 | |
peah | 88ac853 | 2016-09-12 16:47:25 -0700 | [diff] [blame] | 305 | AudioProcessing* AudioProcessing::Create(const webrtc::Config& config, |
Alejandro Luebs | f4022ff | 2016-07-01 17:19:09 -0700 | [diff] [blame] | 306 | NonlinearBeamformer* beamformer) { |
aluebs@webrtc.org | d82f55d | 2015-01-15 18:07:21 +0000 | [diff] [blame] | 307 | AudioProcessingImpl* apm = new AudioProcessingImpl(config, beamformer); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 308 | if (apm->Initialize() != kNoError) { |
| 309 | delete apm; |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 310 | apm = nullptr; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | return apm; |
| 314 | } |
| 315 | |
peah | 88ac853 | 2016-09-12 16:47:25 -0700 | [diff] [blame] | 316 | AudioProcessingImpl::AudioProcessingImpl(const webrtc::Config& config) |
aluebs@webrtc.org | d82f55d | 2015-01-15 18:07:21 +0000 | [diff] [blame] | 317 | : AudioProcessingImpl(config, nullptr) {} |
| 318 | |
peah | 88ac853 | 2016-09-12 16:47:25 -0700 | [diff] [blame] | 319 | AudioProcessingImpl::AudioProcessingImpl(const webrtc::Config& config, |
Alejandro Luebs | f4022ff | 2016-07-01 17:19:09 -0700 | [diff] [blame] | 320 | NonlinearBeamformer* beamformer) |
peah | 8271d04 | 2016-11-22 07:24:52 -0800 | [diff] [blame] | 321 | : high_pass_filter_impl_(new HighPassFilterImpl(this)), |
| 322 | public_submodules_(new ApmPublicSubmodules()), |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 323 | private_submodules_(new ApmPrivateSubmodules(beamformer)), |
| 324 | constants_(config.Get<ExperimentalAgc>().startup_min_volume, |
henrik.lundin | bd681b9 | 2016-12-05 09:08:42 -0800 | [diff] [blame] | 325 | config.Get<ExperimentalAgc>().clipped_level_min, |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 326 | #if defined(WEBRTC_ANDROID) || defined(WEBRTC_IOS) |
Alejandro Luebs | c9b0c26 | 2016-05-16 15:32:38 -0700 | [diff] [blame] | 327 | false), |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 328 | #else |
Alejandro Luebs | c9b0c26 | 2016-05-16 15:32:38 -0700 | [diff] [blame] | 329 | config.Get<ExperimentalAgc>().enabled), |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 330 | #endif |
andrew | 1c7075f | 2015-06-24 18:14:14 -0700 | [diff] [blame] | 331 | #if defined(WEBRTC_ANDROID) || defined(WEBRTC_IOS) |
aluebs | 2a34688 | 2016-01-11 18:04:30 -0800 | [diff] [blame] | 332 | capture_(false, |
andrew | 1c7075f | 2015-06-24 18:14:14 -0700 | [diff] [blame] | 333 | #else |
aluebs | 2a34688 | 2016-01-11 18:04:30 -0800 | [diff] [blame] | 334 | capture_(config.Get<ExperimentalNs>().enabled, |
andrew | 1c7075f | 2015-06-24 18:14:14 -0700 | [diff] [blame] | 335 | #endif |
aluebs | 2a34688 | 2016-01-11 18:04:30 -0800 | [diff] [blame] | 336 | config.Get<Beamforming>().array_geometry, |
aluebs | b2328d1 | 2016-01-11 20:32:29 -0800 | [diff] [blame] | 337 | config.Get<Beamforming>().target_direction), |
Alejandro Luebs | c9b0c26 | 2016-05-16 15:32:38 -0700 | [diff] [blame] | 338 | capture_nonlocked_(config.Get<Beamforming>().enabled, |
peah | 88ac853 | 2016-09-12 16:47:25 -0700 | [diff] [blame] | 339 | config.Get<Intelligibility>().enabled) { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 340 | { |
| 341 | rtc::CritScope cs_render(&crit_render_); |
| 342 | rtc::CritScope cs_capture(&crit_capture_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 343 | |
peah | b624d8c | 2016-03-05 03:01:14 -0800 | [diff] [blame] | 344 | public_submodules_->echo_cancellation.reset( |
peah | b58a158 | 2016-03-15 09:34:24 -0700 | [diff] [blame] | 345 | new EchoCancellationImpl(&crit_render_, &crit_capture_)); |
peah | bb9edbd | 2016-03-10 12:54:25 -0800 | [diff] [blame] | 346 | public_submodules_->echo_control_mobile.reset( |
peah | 253534d | 2016-03-15 04:32:28 -0700 | [diff] [blame] | 347 | new EchoControlMobileImpl(&crit_render_, &crit_capture_)); |
peah | bfa9711 | 2016-03-10 21:09:04 -0800 | [diff] [blame] | 348 | public_submodules_->gain_control.reset( |
peah | b8fbb54 | 2016-03-15 02:28:08 -0700 | [diff] [blame] | 349 | new GainControlImpl(&crit_capture_, &crit_capture_)); |
solenberg | 949028f | 2015-12-15 11:39:38 -0800 | [diff] [blame] | 350 | public_submodules_->level_estimator.reset( |
| 351 | new LevelEstimatorImpl(&crit_capture_)); |
solenberg | 5e465c3 | 2015-12-08 13:22:33 -0800 | [diff] [blame] | 352 | public_submodules_->noise_suppression.reset( |
| 353 | new NoiseSuppressionImpl(&crit_capture_)); |
solenberg | a29386c | 2015-12-16 03:31:12 -0800 | [diff] [blame] | 354 | public_submodules_->voice_detection.reset( |
| 355 | new VoiceDetectionImpl(&crit_capture_)); |
peah | be61562 | 2016-02-13 16:40:47 -0800 | [diff] [blame] | 356 | public_submodules_->gain_control_for_experimental_agc.reset( |
peah | bfa9711 | 2016-03-10 21:09:04 -0800 | [diff] [blame] | 357 | new GainControlForExperimentalAgc( |
| 358 | public_submodules_->gain_control.get(), &crit_capture_)); |
ivoc | 9f4a4a0 | 2016-10-28 05:39:16 -0700 | [diff] [blame] | 359 | private_submodules_->residual_echo_detector.reset( |
| 360 | new ResidualEchoDetector()); |
peah | ca4cac7 | 2016-06-29 15:26:12 -0700 | [diff] [blame] | 361 | |
peah | c19f312 | 2016-10-07 14:54:10 -0700 | [diff] [blame] | 362 | // TODO(peah): Move this creation to happen only when the level controller |
| 363 | // is enabled. |
peah | ca4cac7 | 2016-06-29 15:26:12 -0700 | [diff] [blame] | 364 | private_submodules_->level_controller.reset(new LevelController()); |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 365 | } |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 366 | |
andrew@webrtc.org | e84978f | 2014-01-25 02:09:06 +0000 | [diff] [blame] | 367 | SetExtraOptions(config); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | AudioProcessingImpl::~AudioProcessingImpl() { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 371 | // Depends on gain_control_ and |
peah | be61562 | 2016-02-13 16:40:47 -0800 | [diff] [blame] | 372 | // public_submodules_->gain_control_for_experimental_agc. |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 373 | private_submodules_->agc_manager.reset(); |
| 374 | // Depends on gain_control_. |
peah | be61562 | 2016-02-13 16:40:47 -0800 | [diff] [blame] | 375 | public_submodules_->gain_control_for_experimental_agc.reset(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 376 | |
andrew@webrtc.org | 7bf2646 | 2011-12-03 00:03:31 +0000 | [diff] [blame] | 377 | #ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP |
tommi | a6219cc | 2016-06-15 10:30:14 -0700 | [diff] [blame] | 378 | debug_dump_.debug_file->CloseFile(); |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 379 | #endif |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 380 | } |
| 381 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 382 | int AudioProcessingImpl::Initialize() { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 383 | // Run in a single-threaded manner during initialization. |
| 384 | rtc::CritScope cs_render(&crit_render_); |
| 385 | rtc::CritScope cs_capture(&crit_capture_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 386 | return InitializeLocked(); |
| 387 | } |
| 388 | |
peah | de65ddc | 2016-09-16 15:02:15 -0700 | [diff] [blame] | 389 | int AudioProcessingImpl::Initialize(int capture_input_sample_rate_hz, |
| 390 | int capture_output_sample_rate_hz, |
| 391 | int render_input_sample_rate_hz, |
| 392 | ChannelLayout capture_input_layout, |
| 393 | ChannelLayout capture_output_layout, |
| 394 | ChannelLayout render_input_layout) { |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 395 | const ProcessingConfig processing_config = { |
peah | de65ddc | 2016-09-16 15:02:15 -0700 | [diff] [blame] | 396 | {{capture_input_sample_rate_hz, ChannelsFromLayout(capture_input_layout), |
| 397 | LayoutHasKeyboard(capture_input_layout)}, |
| 398 | {capture_output_sample_rate_hz, |
| 399 | ChannelsFromLayout(capture_output_layout), |
| 400 | LayoutHasKeyboard(capture_output_layout)}, |
| 401 | {render_input_sample_rate_hz, ChannelsFromLayout(render_input_layout), |
| 402 | LayoutHasKeyboard(render_input_layout)}, |
| 403 | {render_input_sample_rate_hz, ChannelsFromLayout(render_input_layout), |
| 404 | LayoutHasKeyboard(render_input_layout)}}}; |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 405 | |
| 406 | return Initialize(processing_config); |
| 407 | } |
| 408 | |
| 409 | int AudioProcessingImpl::Initialize(const ProcessingConfig& processing_config) { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 410 | // Run in a single-threaded manner during initialization. |
| 411 | rtc::CritScope cs_render(&crit_render_); |
| 412 | rtc::CritScope cs_capture(&crit_capture_); |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 413 | return InitializeLocked(processing_config); |
andrew@webrtc.org | a8b9737 | 2014-03-10 22:26:12 +0000 | [diff] [blame] | 414 | } |
| 415 | |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 416 | int AudioProcessingImpl::MaybeInitializeRender( |
peah | 81b9bfe | 2015-11-27 02:47:28 -0800 | [diff] [blame] | 417 | const ProcessingConfig& processing_config) { |
peah | 2ace3f9 | 2016-09-10 04:42:27 -0700 | [diff] [blame] | 418 | return MaybeInitialize(processing_config, false); |
peah | 81b9bfe | 2015-11-27 02:47:28 -0800 | [diff] [blame] | 419 | } |
| 420 | |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 421 | int AudioProcessingImpl::MaybeInitializeCapture( |
peah | 2ace3f9 | 2016-09-10 04:42:27 -0700 | [diff] [blame] | 422 | const ProcessingConfig& processing_config, |
| 423 | bool force_initialization) { |
| 424 | return MaybeInitialize(processing_config, force_initialization); |
peah | 81b9bfe | 2015-11-27 02:47:28 -0800 | [diff] [blame] | 425 | } |
| 426 | |
kwiberg | 83ffe45 | 2016-08-29 14:46:07 -0700 | [diff] [blame] | 427 | #ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP |
| 428 | |
| 429 | AudioProcessingImpl::ApmDebugDumpThreadState::ApmDebugDumpThreadState() |
| 430 | : event_msg(new audioproc::Event()) {} |
| 431 | |
| 432 | AudioProcessingImpl::ApmDebugDumpThreadState::~ApmDebugDumpThreadState() {} |
| 433 | |
| 434 | AudioProcessingImpl::ApmDebugDumpState::ApmDebugDumpState() |
| 435 | : debug_file(FileWrapper::Create()) {} |
| 436 | |
| 437 | AudioProcessingImpl::ApmDebugDumpState::~ApmDebugDumpState() {} |
| 438 | |
| 439 | #endif // WEBRTC_AUDIOPROC_DEBUG_DUMP |
| 440 | |
peah | 192164e | 2015-11-17 02:16:45 -0800 | [diff] [blame] | 441 | // Calls InitializeLocked() if any of the audio parameters have changed from |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 442 | // their current values (needs to be called while holding the crit_render_lock). |
| 443 | int AudioProcessingImpl::MaybeInitialize( |
peah | 2ace3f9 | 2016-09-10 04:42:27 -0700 | [diff] [blame] | 444 | const ProcessingConfig& processing_config, |
| 445 | bool force_initialization) { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 446 | // Called from both threads. Thread check is therefore not possible. |
peah | 2ace3f9 | 2016-09-10 04:42:27 -0700 | [diff] [blame] | 447 | if (processing_config == formats_.api_format && !force_initialization) { |
peah | 192164e | 2015-11-17 02:16:45 -0800 | [diff] [blame] | 448 | return kNoError; |
| 449 | } |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 450 | |
| 451 | rtc::CritScope cs_capture(&crit_capture_); |
peah | 192164e | 2015-11-17 02:16:45 -0800 | [diff] [blame] | 452 | return InitializeLocked(processing_config); |
| 453 | } |
| 454 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 455 | int AudioProcessingImpl::InitializeLocked() { |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 456 | const int capture_audiobuffer_num_channels = |
| 457 | capture_nonlocked_.beamformer_enabled |
| 458 | ? formats_.api_format.input_stream().num_channels() |
| 459 | : formats_.api_format.output_stream().num_channels(); |
| 460 | |
peah | de65ddc | 2016-09-16 15:02:15 -0700 | [diff] [blame] | 461 | const int render_audiobuffer_num_output_frames = |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 462 | formats_.api_format.reverse_output_stream().num_frames() == 0 |
peah | de65ddc | 2016-09-16 15:02:15 -0700 | [diff] [blame] | 463 | ? formats_.render_processing_format.num_frames() |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 464 | : formats_.api_format.reverse_output_stream().num_frames(); |
| 465 | if (formats_.api_format.reverse_input_stream().num_channels() > 0) { |
| 466 | render_.render_audio.reset(new AudioBuffer( |
| 467 | formats_.api_format.reverse_input_stream().num_frames(), |
| 468 | formats_.api_format.reverse_input_stream().num_channels(), |
peah | de65ddc | 2016-09-16 15:02:15 -0700 | [diff] [blame] | 469 | formats_.render_processing_format.num_frames(), |
| 470 | formats_.render_processing_format.num_channels(), |
| 471 | render_audiobuffer_num_output_frames)); |
peah | 2ace3f9 | 2016-09-10 04:42:27 -0700 | [diff] [blame] | 472 | if (formats_.api_format.reverse_input_stream() != |
| 473 | formats_.api_format.reverse_output_stream()) { |
kwiberg | c2b785d | 2016-02-24 05:22:32 -0800 | [diff] [blame] | 474 | render_.render_converter = AudioConverter::Create( |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 475 | formats_.api_format.reverse_input_stream().num_channels(), |
| 476 | formats_.api_format.reverse_input_stream().num_frames(), |
| 477 | formats_.api_format.reverse_output_stream().num_channels(), |
kwiberg | c2b785d | 2016-02-24 05:22:32 -0800 | [diff] [blame] | 478 | formats_.api_format.reverse_output_stream().num_frames()); |
ekmeyerson | 60d9b33 | 2015-08-14 10:35:55 -0700 | [diff] [blame] | 479 | } else { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 480 | render_.render_converter.reset(nullptr); |
ekmeyerson | 60d9b33 | 2015-08-14 10:35:55 -0700 | [diff] [blame] | 481 | } |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 482 | } else { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 483 | render_.render_audio.reset(nullptr); |
| 484 | render_.render_converter.reset(nullptr); |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 485 | } |
peah | ce4d915 | 2017-05-19 01:28:05 -0700 | [diff] [blame] | 486 | |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 487 | capture_.capture_audio.reset( |
| 488 | new AudioBuffer(formats_.api_format.input_stream().num_frames(), |
| 489 | formats_.api_format.input_stream().num_channels(), |
peah | de65ddc | 2016-09-16 15:02:15 -0700 | [diff] [blame] | 490 | capture_nonlocked_.capture_processing_format.num_frames(), |
| 491 | capture_audiobuffer_num_channels, |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 492 | formats_.api_format.output_stream().num_frames())); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 493 | |
peah | de65ddc | 2016-09-16 15:02:15 -0700 | [diff] [blame] | 494 | public_submodules_->echo_cancellation->Initialize( |
| 495 | proc_sample_rate_hz(), num_reverse_channels(), num_output_channels(), |
| 496 | num_proc_channels()); |
peah | 764e364 | 2016-10-22 05:04:30 -0700 | [diff] [blame] | 497 | AllocateRenderQueue(); |
| 498 | |
ivoc | 3e9a537 | 2016-10-28 07:55:33 -0700 | [diff] [blame] | 499 | int success = public_submodules_->echo_cancellation->enable_metrics(true); |
| 500 | RTC_DCHECK_EQ(0, success); |
| 501 | success = public_submodules_->echo_cancellation->enable_delay_logging(true); |
| 502 | RTC_DCHECK_EQ(0, success); |
peah | de65ddc | 2016-09-16 15:02:15 -0700 | [diff] [blame] | 503 | public_submodules_->echo_control_mobile->Initialize( |
| 504 | proc_split_sample_rate_hz(), num_reverse_channels(), |
| 505 | num_output_channels()); |
peah | 135259a | 2016-10-28 03:12:11 -0700 | [diff] [blame] | 506 | |
| 507 | public_submodules_->gain_control->Initialize(num_proc_channels(), |
| 508 | proc_sample_rate_hz()); |
peah | de65ddc | 2016-09-16 15:02:15 -0700 | [diff] [blame] | 509 | if (constants_.use_experimental_agc) { |
| 510 | if (!private_submodules_->agc_manager.get()) { |
| 511 | private_submodules_->agc_manager.reset(new AgcManagerDirect( |
| 512 | public_submodules_->gain_control.get(), |
| 513 | public_submodules_->gain_control_for_experimental_agc.get(), |
henrik.lundin | bd681b9 | 2016-12-05 09:08:42 -0800 | [diff] [blame] | 514 | constants_.agc_startup_min_volume, constants_.agc_clipped_level_min)); |
peah | de65ddc | 2016-09-16 15:02:15 -0700 | [diff] [blame] | 515 | } |
| 516 | private_submodules_->agc_manager->Initialize(); |
| 517 | private_submodules_->agc_manager->SetCaptureMuted( |
| 518 | capture_.output_will_be_muted); |
peah | 135259a | 2016-10-28 03:12:11 -0700 | [diff] [blame] | 519 | public_submodules_->gain_control_for_experimental_agc->Initialize(); |
peah | de65ddc | 2016-09-16 15:02:15 -0700 | [diff] [blame] | 520 | } |
Bjorn Volcker | adc46c4 | 2015-04-15 11:42:40 +0200 | [diff] [blame] | 521 | InitializeTransient(); |
aluebs@webrtc.org | ae643ce | 2014-12-19 19:57:34 +0000 | [diff] [blame] | 522 | InitializeBeamformer(); |
peah | 1bcfce5 | 2016-08-26 07:16:04 -0700 | [diff] [blame] | 523 | #if WEBRTC_INTELLIGIBILITY_ENHANCER |
ekmeyerson | 60d9b33 | 2015-08-14 10:35:55 -0700 | [diff] [blame] | 524 | InitializeIntelligibility(); |
peah | 1bcfce5 | 2016-08-26 07:16:04 -0700 | [diff] [blame] | 525 | #endif |
peah | 8271d04 | 2016-11-22 07:24:52 -0800 | [diff] [blame] | 526 | InitializeLowCutFilter(); |
peah | de65ddc | 2016-09-16 15:02:15 -0700 | [diff] [blame] | 527 | public_submodules_->noise_suppression->Initialize(num_proc_channels(), |
| 528 | proc_sample_rate_hz()); |
| 529 | public_submodules_->voice_detection->Initialize(proc_split_sample_rate_hz()); |
| 530 | public_submodules_->level_estimator->Initialize(); |
peah | ca4cac7 | 2016-06-29 15:26:12 -0700 | [diff] [blame] | 531 | InitializeLevelController(); |
ivoc | 9f4a4a0 | 2016-10-28 05:39:16 -0700 | [diff] [blame] | 532 | InitializeResidualEchoDetector(); |
peah | e0eae3c | 2016-12-14 01:16:23 -0800 | [diff] [blame] | 533 | InitializeEchoCanceller3(); |
alessiob | 3ec96df | 2017-05-22 06:57:06 -0700 | [diff] [blame] | 534 | InitializeGainController2(); |
solenberg | 70f9903 | 2015-12-08 11:07:32 -0800 | [diff] [blame] | 535 | |
andrew@webrtc.org | 7bf2646 | 2011-12-03 00:03:31 +0000 | [diff] [blame] | 536 | #ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP |
tommi | a6219cc | 2016-06-15 10:30:14 -0700 | [diff] [blame] | 537 | if (debug_dump_.debug_file->is_open()) { |
ajm@google.com | 808e0e0 | 2011-08-03 21:08:51 +0000 | [diff] [blame] | 538 | int err = WriteInitMessage(); |
| 539 | if (err != kNoError) { |
| 540 | return err; |
| 541 | } |
| 542 | } |
andrew@webrtc.org | 7bf2646 | 2011-12-03 00:03:31 +0000 | [diff] [blame] | 543 | #endif |
ajm@google.com | 808e0e0 | 2011-08-03 21:08:51 +0000 | [diff] [blame] | 544 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 545 | return kNoError; |
| 546 | } |
| 547 | |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 548 | int AudioProcessingImpl::InitializeLocked(const ProcessingConfig& config) { |
| 549 | for (const auto& stream : config.streams) { |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 550 | if (stream.num_channels() > 0 && stream.sample_rate_hz() <= 0) { |
| 551 | return kBadSampleRateError; |
| 552 | } |
andrew@webrtc.org | a8b9737 | 2014-03-10 22:26:12 +0000 | [diff] [blame] | 553 | } |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 554 | |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 555 | const size_t num_in_channels = config.input_stream().num_channels(); |
| 556 | const size_t num_out_channels = config.output_stream().num_channels(); |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 557 | |
| 558 | // Need at least one input channel. |
| 559 | // Need either one output channel or as many outputs as there are inputs. |
| 560 | if (num_in_channels == 0 || |
| 561 | !(num_out_channels == 1 || num_out_channels == num_in_channels)) { |
Michael Graczyk | c204754 | 2015-07-22 21:06:11 -0700 | [diff] [blame] | 562 | return kBadNumberChannelsError; |
| 563 | } |
| 564 | |
aluebs | b2328d1 | 2016-01-11 20:32:29 -0800 | [diff] [blame] | 565 | if (capture_nonlocked_.beamformer_enabled && |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 566 | num_in_channels != capture_.array_geometry.size()) { |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 567 | return kBadNumberChannelsError; |
| 568 | } |
| 569 | |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 570 | formats_.api_format = config; |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 571 | |
peah | de65ddc | 2016-09-16 15:02:15 -0700 | [diff] [blame] | 572 | int capture_processing_rate = FindNativeProcessRateToUse( |
peah | 423d236 | 2016-04-09 16:06:52 -0700 | [diff] [blame] | 573 | std::min(formats_.api_format.input_stream().sample_rate_hz(), |
peah | 2ace3f9 | 2016-09-10 04:42:27 -0700 | [diff] [blame] | 574 | formats_.api_format.output_stream().sample_rate_hz()), |
| 575 | submodule_states_.CaptureMultiBandSubModulesActive() || |
| 576 | submodule_states_.RenderMultiBandSubModulesActive()); |
andrew@webrtc.org | a8b9737 | 2014-03-10 22:26:12 +0000 | [diff] [blame] | 577 | |
peah | de65ddc | 2016-09-16 15:02:15 -0700 | [diff] [blame] | 578 | capture_nonlocked_.capture_processing_format = |
| 579 | StreamConfig(capture_processing_rate); |
peah | 2ace3f9 | 2016-09-10 04:42:27 -0700 | [diff] [blame] | 580 | |
peah | 2ce640f | 2017-04-07 03:57:48 -0700 | [diff] [blame] | 581 | int render_processing_rate; |
| 582 | if (!config_.echo_canceller3.enabled) { |
| 583 | render_processing_rate = FindNativeProcessRateToUse( |
| 584 | std::min(formats_.api_format.reverse_input_stream().sample_rate_hz(), |
| 585 | formats_.api_format.reverse_output_stream().sample_rate_hz()), |
| 586 | submodule_states_.CaptureMultiBandSubModulesActive() || |
| 587 | submodule_states_.RenderMultiBandSubModulesActive()); |
| 588 | } else { |
| 589 | render_processing_rate = capture_processing_rate; |
| 590 | } |
| 591 | |
aluebs | eb3603b | 2016-04-20 15:27:58 -0700 | [diff] [blame] | 592 | // TODO(aluebs): Remove this restriction once we figure out why the 3-band |
| 593 | // splitting filter degrades the AEC performance. |
peah | cf02cf1 | 2017-04-05 14:18:07 -0700 | [diff] [blame] | 594 | if (render_processing_rate > kSampleRate32kHz && |
| 595 | !config_.echo_canceller3.enabled) { |
peah | de65ddc | 2016-09-16 15:02:15 -0700 | [diff] [blame] | 596 | render_processing_rate = submodule_states_.RenderMultiBandProcessingActive() |
| 597 | ? kSampleRate32kHz |
| 598 | : kSampleRate16kHz; |
aluebs | eb3603b | 2016-04-20 15:27:58 -0700 | [diff] [blame] | 599 | } |
peah | 2ce640f | 2017-04-07 03:57:48 -0700 | [diff] [blame] | 600 | |
peah | de65ddc | 2016-09-16 15:02:15 -0700 | [diff] [blame] | 601 | // If the forward sample rate is 8 kHz, the render stream is also processed |
aluebs | eb3603b | 2016-04-20 15:27:58 -0700 | [diff] [blame] | 602 | // at this rate. |
peah | de65ddc | 2016-09-16 15:02:15 -0700 | [diff] [blame] | 603 | if (capture_nonlocked_.capture_processing_format.sample_rate_hz() == |
| 604 | kSampleRate8kHz) { |
| 605 | render_processing_rate = kSampleRate8kHz; |
andrew@webrtc.org | a8b9737 | 2014-03-10 22:26:12 +0000 | [diff] [blame] | 606 | } else { |
peah | de65ddc | 2016-09-16 15:02:15 -0700 | [diff] [blame] | 607 | render_processing_rate = |
| 608 | std::max(render_processing_rate, static_cast<int>(kSampleRate16kHz)); |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 609 | } |
| 610 | |
peah | de65ddc | 2016-09-16 15:02:15 -0700 | [diff] [blame] | 611 | // Always downmix the render stream to mono for analysis. This has been |
andrew@webrtc.org | 30be827 | 2014-09-24 20:06:23 +0000 | [diff] [blame] | 612 | // demonstrated to work well for AEC in most practical scenarios. |
peah | ce4d915 | 2017-05-19 01:28:05 -0700 | [diff] [blame] | 613 | if (submodule_states_.RenderMultiBandSubModulesActive()) { |
| 614 | formats_.render_processing_format = StreamConfig(render_processing_rate, 1); |
| 615 | } else { |
| 616 | formats_.render_processing_format = StreamConfig( |
| 617 | formats_.api_format.reverse_input_stream().sample_rate_hz(), |
| 618 | formats_.api_format.reverse_input_stream().num_channels()); |
| 619 | } |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 620 | |
peah | de65ddc | 2016-09-16 15:02:15 -0700 | [diff] [blame] | 621 | if (capture_nonlocked_.capture_processing_format.sample_rate_hz() == |
| 622 | kSampleRate32kHz || |
| 623 | capture_nonlocked_.capture_processing_format.sample_rate_hz() == |
| 624 | kSampleRate48kHz) { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 625 | capture_nonlocked_.split_rate = kSampleRate16kHz; |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 626 | } else { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 627 | capture_nonlocked_.split_rate = |
peah | de65ddc | 2016-09-16 15:02:15 -0700 | [diff] [blame] | 628 | capture_nonlocked_.capture_processing_format.sample_rate_hz(); |
andrew@webrtc.org | a8b9737 | 2014-03-10 22:26:12 +0000 | [diff] [blame] | 629 | } |
| 630 | |
| 631 | return InitializeLocked(); |
| 632 | } |
| 633 | |
peah | 88ac853 | 2016-09-12 16:47:25 -0700 | [diff] [blame] | 634 | void AudioProcessingImpl::ApplyConfig(const AudioProcessing::Config& config) { |
peah | c19f312 | 2016-10-07 14:54:10 -0700 | [diff] [blame] | 635 | config_ = config; |
peah | 88ac853 | 2016-09-12 16:47:25 -0700 | [diff] [blame] | 636 | |
peah | c19f312 | 2016-10-07 14:54:10 -0700 | [diff] [blame] | 637 | bool config_ok = LevelController::Validate(config_.level_controller); |
peah | 88ac853 | 2016-09-12 16:47:25 -0700 | [diff] [blame] | 638 | if (!config_ok) { |
| 639 | LOG(LS_ERROR) << "AudioProcessing module config error" << std::endl |
| 640 | << "level_controller: " |
peah | c19f312 | 2016-10-07 14:54:10 -0700 | [diff] [blame] | 641 | << LevelController::ToString(config_.level_controller) |
peah | 88ac853 | 2016-09-12 16:47:25 -0700 | [diff] [blame] | 642 | << std::endl |
| 643 | << "Reverting to default parameter set"; |
peah | c19f312 | 2016-10-07 14:54:10 -0700 | [diff] [blame] | 644 | config_.level_controller = AudioProcessing::Config::LevelController(); |
peah | 88ac853 | 2016-09-12 16:47:25 -0700 | [diff] [blame] | 645 | } |
| 646 | |
| 647 | // Run in a single-threaded manner when applying the settings. |
| 648 | rtc::CritScope cs_render(&crit_render_); |
| 649 | rtc::CritScope cs_capture(&crit_capture_); |
| 650 | |
peah | c19f312 | 2016-10-07 14:54:10 -0700 | [diff] [blame] | 651 | // TODO(peah): Replace the use of capture_nonlocked_.level_controller_enabled |
| 652 | // with the value in config_ everywhere in the code. |
| 653 | if (capture_nonlocked_.level_controller_enabled != |
| 654 | config_.level_controller.enabled) { |
peah | 88ac853 | 2016-09-12 16:47:25 -0700 | [diff] [blame] | 655 | capture_nonlocked_.level_controller_enabled = |
peah | c19f312 | 2016-10-07 14:54:10 -0700 | [diff] [blame] | 656 | config_.level_controller.enabled; |
| 657 | // TODO(peah): Remove the conditional initialization to always initialize |
| 658 | // the level controller regardless of whether it is enabled or not. |
| 659 | InitializeLevelController(); |
peah | 88ac853 | 2016-09-12 16:47:25 -0700 | [diff] [blame] | 660 | } |
peah | c19f312 | 2016-10-07 14:54:10 -0700 | [diff] [blame] | 661 | LOG(LS_INFO) << "Level controller activated: " |
| 662 | << capture_nonlocked_.level_controller_enabled; |
| 663 | |
| 664 | private_submodules_->level_controller->ApplyConfig(config_.level_controller); |
peah | 8271d04 | 2016-11-22 07:24:52 -0800 | [diff] [blame] | 665 | |
| 666 | InitializeLowCutFilter(); |
| 667 | |
| 668 | LOG(LS_INFO) << "Highpass filter activated: " |
| 669 | << config_.high_pass_filter.enabled; |
peah | e0eae3c | 2016-12-14 01:16:23 -0800 | [diff] [blame] | 670 | |
| 671 | config_ok = EchoCanceller3::Validate(config_.echo_canceller3); |
| 672 | if (!config_ok) { |
| 673 | LOG(LS_ERROR) << "AudioProcessing module config error" << std::endl |
| 674 | << "echo canceller 3: " |
| 675 | << EchoCanceller3::ToString(config_.echo_canceller3) |
| 676 | << std::endl |
| 677 | << "Reverting to default parameter set"; |
| 678 | config_.echo_canceller3 = AudioProcessing::Config::EchoCanceller3(); |
| 679 | } |
| 680 | |
| 681 | if (config.echo_canceller3.enabled != |
| 682 | capture_nonlocked_.echo_canceller3_enabled) { |
| 683 | capture_nonlocked_.echo_canceller3_enabled = |
| 684 | config_.echo_canceller3.enabled; |
| 685 | InitializeEchoCanceller3(); |
| 686 | LOG(LS_INFO) << "Echo canceller 3 activated: " |
| 687 | << capture_nonlocked_.echo_canceller3_enabled; |
| 688 | } |
alessiob | 3ec96df | 2017-05-22 06:57:06 -0700 | [diff] [blame] | 689 | |
| 690 | config_ok = GainController2::Validate(config_.gain_controller2); |
| 691 | if (!config_ok) { |
| 692 | LOG(LS_ERROR) << "AudioProcessing module config error" << std::endl |
| 693 | << "gain_controller2: " |
| 694 | << GainController2::ToString(config_.gain_controller2) |
| 695 | << std::endl |
| 696 | << "Reverting to default parameter set"; |
| 697 | config_.gain_controller2 = AudioProcessing::Config::GainController2(); |
| 698 | } |
| 699 | |
| 700 | if (config.gain_controller2.enabled != |
| 701 | capture_nonlocked_.gain_controller2_enabled) { |
| 702 | capture_nonlocked_.gain_controller2_enabled = |
| 703 | config_.gain_controller2.enabled; |
| 704 | InitializeGainController2(); |
| 705 | LOG(LS_INFO) << "Gain controller 2 activated: " |
| 706 | << capture_nonlocked_.gain_controller2_enabled; |
| 707 | } |
peah | 88ac853 | 2016-09-12 16:47:25 -0700 | [diff] [blame] | 708 | } |
| 709 | |
| 710 | void AudioProcessingImpl::SetExtraOptions(const webrtc::Config& config) { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 711 | // Run in a single-threaded manner when setting the extra options. |
| 712 | rtc::CritScope cs_render(&crit_render_); |
| 713 | rtc::CritScope cs_capture(&crit_capture_); |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 714 | |
peah | b624d8c | 2016-03-05 03:01:14 -0800 | [diff] [blame] | 715 | public_submodules_->echo_cancellation->SetExtraOptions(config); |
| 716 | |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 717 | if (capture_.transient_suppressor_enabled != |
| 718 | config.Get<ExperimentalNs>().enabled) { |
| 719 | capture_.transient_suppressor_enabled = |
| 720 | config.Get<ExperimentalNs>().enabled; |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 721 | InitializeTransient(); |
| 722 | } |
aluebs | 2a34688 | 2016-01-11 18:04:30 -0800 | [diff] [blame] | 723 | |
peah | 1bcfce5 | 2016-08-26 07:16:04 -0700 | [diff] [blame] | 724 | #if WEBRTC_INTELLIGIBILITY_ENHANCER |
alessiob | 3ec96df | 2017-05-22 06:57:06 -0700 | [diff] [blame] | 725 | if (capture_nonlocked_.intelligibility_enabled != |
Alejandro Luebs | c9b0c26 | 2016-05-16 15:32:38 -0700 | [diff] [blame] | 726 | config.Get<Intelligibility>().enabled) { |
| 727 | capture_nonlocked_.intelligibility_enabled = |
| 728 | config.Get<Intelligibility>().enabled; |
| 729 | InitializeIntelligibility(); |
| 730 | } |
peah | 1bcfce5 | 2016-08-26 07:16:04 -0700 | [diff] [blame] | 731 | #endif |
Alejandro Luebs | c9b0c26 | 2016-05-16 15:32:38 -0700 | [diff] [blame] | 732 | |
aluebs | 2a34688 | 2016-01-11 18:04:30 -0800 | [diff] [blame] | 733 | #ifdef WEBRTC_ANDROID_PLATFORM_BUILD |
aluebs | b2328d1 | 2016-01-11 20:32:29 -0800 | [diff] [blame] | 734 | if (capture_nonlocked_.beamformer_enabled != |
| 735 | config.Get<Beamforming>().enabled) { |
| 736 | capture_nonlocked_.beamformer_enabled = config.Get<Beamforming>().enabled; |
aluebs | 2a34688 | 2016-01-11 18:04:30 -0800 | [diff] [blame] | 737 | if (config.Get<Beamforming>().array_geometry.size() > 1) { |
| 738 | capture_.array_geometry = config.Get<Beamforming>().array_geometry; |
| 739 | } |
| 740 | capture_.target_direction = config.Get<Beamforming>().target_direction; |
| 741 | InitializeBeamformer(); |
| 742 | } |
| 743 | #endif // WEBRTC_ANDROID_PLATFORM_BUILD |
andrew@webrtc.org | 61e596f | 2013-07-25 18:28:29 +0000 | [diff] [blame] | 744 | } |
| 745 | |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 746 | int AudioProcessingImpl::proc_sample_rate_hz() const { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 747 | // Used as callback from submodules, hence locking is not allowed. |
peah | de65ddc | 2016-09-16 15:02:15 -0700 | [diff] [blame] | 748 | return capture_nonlocked_.capture_processing_format.sample_rate_hz(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 749 | } |
| 750 | |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 751 | int AudioProcessingImpl::proc_split_sample_rate_hz() const { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 752 | // Used as callback from submodules, hence locking is not allowed. |
| 753 | return capture_nonlocked_.split_rate; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 754 | } |
| 755 | |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 756 | size_t AudioProcessingImpl::num_reverse_channels() const { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 757 | // Used as callback from submodules, hence locking is not allowed. |
peah | de65ddc | 2016-09-16 15:02:15 -0700 | [diff] [blame] | 758 | return formats_.render_processing_format.num_channels(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 759 | } |
| 760 | |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 761 | size_t AudioProcessingImpl::num_input_channels() const { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 762 | // Used as callback from submodules, hence locking is not allowed. |
| 763 | return formats_.api_format.input_stream().num_channels(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 764 | } |
| 765 | |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 766 | size_t AudioProcessingImpl::num_proc_channels() const { |
aluebs | b2328d1 | 2016-01-11 20:32:29 -0800 | [diff] [blame] | 767 | // Used as callback from submodules, hence locking is not allowed. |
peah | edddac5 | 2017-05-16 01:08:58 -0700 | [diff] [blame] | 768 | return (capture_nonlocked_.beamformer_enabled || |
| 769 | capture_nonlocked_.echo_canceller3_enabled) |
| 770 | ? 1 |
| 771 | : num_output_channels(); |
aluebs | b2328d1 | 2016-01-11 20:32:29 -0800 | [diff] [blame] | 772 | } |
| 773 | |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 774 | size_t AudioProcessingImpl::num_output_channels() const { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 775 | // Used as callback from submodules, hence locking is not allowed. |
| 776 | return formats_.api_format.output_stream().num_channels(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 777 | } |
| 778 | |
andrew@webrtc.org | 17342e5 | 2014-02-12 22:28:31 +0000 | [diff] [blame] | 779 | void AudioProcessingImpl::set_output_will_be_muted(bool muted) { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 780 | rtc::CritScope cs(&crit_capture_); |
| 781 | capture_.output_will_be_muted = muted; |
| 782 | if (private_submodules_->agc_manager.get()) { |
| 783 | private_submodules_->agc_manager->SetCaptureMuted( |
| 784 | capture_.output_will_be_muted); |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 785 | } |
andrew@webrtc.org | 17342e5 | 2014-02-12 22:28:31 +0000 | [diff] [blame] | 786 | } |
| 787 | |
andrew@webrtc.org | 17342e5 | 2014-02-12 22:28:31 +0000 | [diff] [blame] | 788 | |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 789 | int AudioProcessingImpl::ProcessStream(const float* const* src, |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 790 | size_t samples_per_channel, |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 791 | int input_sample_rate_hz, |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 792 | ChannelLayout input_layout, |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 793 | int output_sample_rate_hz, |
| 794 | ChannelLayout output_layout, |
| 795 | float* const* dest) { |
peah | 369f828 | 2015-12-17 06:42:29 -0800 | [diff] [blame] | 796 | TRACE_EVENT0("webrtc", "AudioProcessing::ProcessStream_ChannelLayout"); |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 797 | StreamConfig input_stream; |
| 798 | StreamConfig output_stream; |
| 799 | { |
| 800 | // Access the formats_.api_format.input_stream beneath the capture lock. |
| 801 | // The lock must be released as it is later required in the call |
| 802 | // to ProcessStream(,,,); |
| 803 | rtc::CritScope cs(&crit_capture_); |
| 804 | input_stream = formats_.api_format.input_stream(); |
| 805 | output_stream = formats_.api_format.output_stream(); |
| 806 | } |
| 807 | |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 808 | input_stream.set_sample_rate_hz(input_sample_rate_hz); |
| 809 | input_stream.set_num_channels(ChannelsFromLayout(input_layout)); |
| 810 | input_stream.set_has_keyboard(LayoutHasKeyboard(input_layout)); |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 811 | output_stream.set_sample_rate_hz(output_sample_rate_hz); |
| 812 | output_stream.set_num_channels(ChannelsFromLayout(output_layout)); |
| 813 | output_stream.set_has_keyboard(LayoutHasKeyboard(output_layout)); |
| 814 | |
| 815 | if (samples_per_channel != input_stream.num_frames()) { |
| 816 | return kBadDataLengthError; |
| 817 | } |
| 818 | return ProcessStream(src, input_stream, output_stream, dest); |
| 819 | } |
| 820 | |
| 821 | int AudioProcessingImpl::ProcessStream(const float* const* src, |
| 822 | const StreamConfig& input_config, |
| 823 | const StreamConfig& output_config, |
| 824 | float* const* dest) { |
peah | 369f828 | 2015-12-17 06:42:29 -0800 | [diff] [blame] | 825 | TRACE_EVENT0("webrtc", "AudioProcessing::ProcessStream_StreamConfig"); |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 826 | ProcessingConfig processing_config; |
peah | 2ace3f9 | 2016-09-10 04:42:27 -0700 | [diff] [blame] | 827 | bool reinitialization_required = false; |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 828 | { |
| 829 | // Acquire the capture lock in order to safely call the function |
| 830 | // that retrieves the render side data. This function accesses apm |
| 831 | // getters that need the capture lock held when being called. |
| 832 | rtc::CritScope cs_capture(&crit_capture_); |
peah | 764e364 | 2016-10-22 05:04:30 -0700 | [diff] [blame] | 833 | EmptyQueuedRenderAudio(); |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 834 | |
| 835 | if (!src || !dest) { |
| 836 | return kNullPointerError; |
| 837 | } |
| 838 | |
| 839 | processing_config = formats_.api_format; |
peah | 2ace3f9 | 2016-09-10 04:42:27 -0700 | [diff] [blame] | 840 | reinitialization_required = UpdateActiveSubmoduleStates(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 841 | } |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 842 | |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 843 | processing_config.input_stream() = input_config; |
| 844 | processing_config.output_stream() = output_config; |
| 845 | |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 846 | { |
| 847 | // Do conditional reinitialization. |
| 848 | rtc::CritScope cs_render(&crit_render_); |
peah | 2ace3f9 | 2016-09-10 04:42:27 -0700 | [diff] [blame] | 849 | RETURN_ON_ERR( |
| 850 | MaybeInitializeCapture(processing_config, reinitialization_required)); |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 851 | } |
| 852 | rtc::CritScope cs_capture(&crit_capture_); |
kwiberg | 9e2be5f | 2016-09-14 05:23:22 -0700 | [diff] [blame] | 853 | RTC_DCHECK_EQ(processing_config.input_stream().num_frames(), |
| 854 | formats_.api_format.input_stream().num_frames()); |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 855 | |
| 856 | #ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP |
tommi | a6219cc | 2016-06-15 10:30:14 -0700 | [diff] [blame] | 857 | if (debug_dump_.debug_file->is_open()) { |
Minyue | 13b96ba | 2015-10-03 00:39:14 +0200 | [diff] [blame] | 858 | RETURN_ON_ERR(WriteConfigMessage(false)); |
| 859 | |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 860 | debug_dump_.capture.event_msg->set_type(audioproc::Event::STREAM); |
| 861 | audioproc::Stream* msg = debug_dump_.capture.event_msg->mutable_stream(); |
aluebs@webrtc.org | 59a1b1b | 2014-08-28 10:43:09 +0000 | [diff] [blame] | 862 | const size_t channel_size = |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 863 | sizeof(float) * formats_.api_format.input_stream().num_frames(); |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 864 | for (size_t i = 0; i < formats_.api_format.input_stream().num_channels(); |
| 865 | ++i) |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 866 | msg->add_input_channel(src[i], channel_size); |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 867 | } |
| 868 | #endif |
| 869 | |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 870 | capture_.capture_audio->CopyFrom(src, formats_.api_format.input_stream()); |
peah | de65ddc | 2016-09-16 15:02:15 -0700 | [diff] [blame] | 871 | RETURN_ON_ERR(ProcessCaptureStreamLocked()); |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 872 | capture_.capture_audio->CopyTo(formats_.api_format.output_stream(), dest); |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 873 | |
| 874 | #ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP |
tommi | a6219cc | 2016-06-15 10:30:14 -0700 | [diff] [blame] | 875 | if (debug_dump_.debug_file->is_open()) { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 876 | audioproc::Stream* msg = debug_dump_.capture.event_msg->mutable_stream(); |
aluebs@webrtc.org | 59a1b1b | 2014-08-28 10:43:09 +0000 | [diff] [blame] | 877 | const size_t channel_size = |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 878 | sizeof(float) * formats_.api_format.output_stream().num_frames(); |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 879 | for (size_t i = 0; i < formats_.api_format.output_stream().num_channels(); |
| 880 | ++i) |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 881 | msg->add_output_channel(dest[i], channel_size); |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 882 | RETURN_ON_ERR(WriteMessageToDebugFile(debug_dump_.debug_file.get(), |
ivoc | d66b44d | 2016-01-15 03:06:36 -0800 | [diff] [blame] | 883 | &debug_dump_.num_bytes_left_for_log_, |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 884 | &crit_debug_, &debug_dump_.capture)); |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 885 | } |
| 886 | #endif |
| 887 | |
| 888 | return kNoError; |
| 889 | } |
| 890 | |
peah | 9e6a290 | 2017-05-15 07:19:21 -0700 | [diff] [blame] | 891 | void AudioProcessingImpl::QueueBandedRenderAudio(AudioBuffer* audio) { |
peah | 764e364 | 2016-10-22 05:04:30 -0700 | [diff] [blame] | 892 | EchoCancellationImpl::PackRenderAudioBuffer(audio, num_output_channels(), |
| 893 | num_reverse_channels(), |
peah | 701d628 | 2016-10-25 05:42:20 -0700 | [diff] [blame] | 894 | &aec_render_queue_buffer_); |
peah | 764e364 | 2016-10-22 05:04:30 -0700 | [diff] [blame] | 895 | |
kwiberg | af476c7 | 2016-11-28 15:21:39 -0800 | [diff] [blame] | 896 | RTC_DCHECK_GE(160, audio->num_frames_per_band()); |
peah | 764e364 | 2016-10-22 05:04:30 -0700 | [diff] [blame] | 897 | |
| 898 | // Insert the samples into the queue. |
peah | 701d628 | 2016-10-25 05:42:20 -0700 | [diff] [blame] | 899 | if (!aec_render_signal_queue_->Insert(&aec_render_queue_buffer_)) { |
peah | 764e364 | 2016-10-22 05:04:30 -0700 | [diff] [blame] | 900 | // The data queue is full and needs to be emptied. |
| 901 | EmptyQueuedRenderAudio(); |
| 902 | |
| 903 | // Retry the insert (should always work). |
peah | 701d628 | 2016-10-25 05:42:20 -0700 | [diff] [blame] | 904 | bool result = aec_render_signal_queue_->Insert(&aec_render_queue_buffer_); |
peah | a062460 | 2016-10-25 04:45:24 -0700 | [diff] [blame] | 905 | RTC_DCHECK(result); |
| 906 | } |
| 907 | |
| 908 | EchoControlMobileImpl::PackRenderAudioBuffer(audio, num_output_channels(), |
| 909 | num_reverse_channels(), |
peah | 701d628 | 2016-10-25 05:42:20 -0700 | [diff] [blame] | 910 | &aecm_render_queue_buffer_); |
peah | a062460 | 2016-10-25 04:45:24 -0700 | [diff] [blame] | 911 | |
| 912 | // Insert the samples into the queue. |
peah | 701d628 | 2016-10-25 05:42:20 -0700 | [diff] [blame] | 913 | if (!aecm_render_signal_queue_->Insert(&aecm_render_queue_buffer_)) { |
peah | a062460 | 2016-10-25 04:45:24 -0700 | [diff] [blame] | 914 | // The data queue is full and needs to be emptied. |
| 915 | EmptyQueuedRenderAudio(); |
| 916 | |
| 917 | // Retry the insert (should always work). |
peah | 701d628 | 2016-10-25 05:42:20 -0700 | [diff] [blame] | 918 | bool result = aecm_render_signal_queue_->Insert(&aecm_render_queue_buffer_); |
peah | 764e364 | 2016-10-22 05:04:30 -0700 | [diff] [blame] | 919 | RTC_DCHECK(result); |
| 920 | } |
peah | 701d628 | 2016-10-25 05:42:20 -0700 | [diff] [blame] | 921 | |
| 922 | if (!constants_.use_experimental_agc) { |
| 923 | GainControlImpl::PackRenderAudioBuffer(audio, &agc_render_queue_buffer_); |
| 924 | // Insert the samples into the queue. |
| 925 | if (!agc_render_signal_queue_->Insert(&agc_render_queue_buffer_)) { |
| 926 | // The data queue is full and needs to be emptied. |
| 927 | EmptyQueuedRenderAudio(); |
| 928 | |
| 929 | // Retry the insert (should always work). |
| 930 | bool result = agc_render_signal_queue_->Insert(&agc_render_queue_buffer_); |
| 931 | RTC_DCHECK(result); |
| 932 | } |
| 933 | } |
peah | 9e6a290 | 2017-05-15 07:19:21 -0700 | [diff] [blame] | 934 | } |
ivoc | 9f4a4a0 | 2016-10-28 05:39:16 -0700 | [diff] [blame] | 935 | |
peah | 9e6a290 | 2017-05-15 07:19:21 -0700 | [diff] [blame] | 936 | void AudioProcessingImpl::QueueNonbandedRenderAudio(AudioBuffer* audio) { |
ivoc | 9f4a4a0 | 2016-10-28 05:39:16 -0700 | [diff] [blame] | 937 | ResidualEchoDetector::PackRenderAudioBuffer(audio, &red_render_queue_buffer_); |
| 938 | |
| 939 | // Insert the samples into the queue. |
| 940 | if (!red_render_signal_queue_->Insert(&red_render_queue_buffer_)) { |
| 941 | // The data queue is full and needs to be emptied. |
| 942 | EmptyQueuedRenderAudio(); |
| 943 | |
| 944 | // Retry the insert (should always work). |
| 945 | bool result = red_render_signal_queue_->Insert(&red_render_queue_buffer_); |
| 946 | RTC_DCHECK(result); |
| 947 | } |
peah | 764e364 | 2016-10-22 05:04:30 -0700 | [diff] [blame] | 948 | } |
| 949 | |
| 950 | void AudioProcessingImpl::AllocateRenderQueue() { |
peah | 701d628 | 2016-10-25 05:42:20 -0700 | [diff] [blame] | 951 | const size_t new_aec_render_queue_element_max_size = |
peah | 764e364 | 2016-10-22 05:04:30 -0700 | [diff] [blame] | 952 | std::max(static_cast<size_t>(1), |
peah | 9e6a290 | 2017-05-15 07:19:21 -0700 | [diff] [blame] | 953 | kMaxAllowedValuesOfSamplesPerBand * |
peah | 764e364 | 2016-10-22 05:04:30 -0700 | [diff] [blame] | 954 | EchoCancellationImpl::NumCancellersRequired( |
| 955 | num_output_channels(), num_reverse_channels())); |
| 956 | |
peah | 701d628 | 2016-10-25 05:42:20 -0700 | [diff] [blame] | 957 | const size_t new_aecm_render_queue_element_max_size = |
peah | a062460 | 2016-10-25 04:45:24 -0700 | [diff] [blame] | 958 | std::max(static_cast<size_t>(1), |
peah | 9e6a290 | 2017-05-15 07:19:21 -0700 | [diff] [blame] | 959 | kMaxAllowedValuesOfSamplesPerBand * |
peah | a062460 | 2016-10-25 04:45:24 -0700 | [diff] [blame] | 960 | EchoControlMobileImpl::NumCancellersRequired( |
| 961 | num_output_channels(), num_reverse_channels())); |
peah | 764e364 | 2016-10-22 05:04:30 -0700 | [diff] [blame] | 962 | |
peah | 701d628 | 2016-10-25 05:42:20 -0700 | [diff] [blame] | 963 | const size_t new_agc_render_queue_element_max_size = |
peah | 9e6a290 | 2017-05-15 07:19:21 -0700 | [diff] [blame] | 964 | std::max(static_cast<size_t>(1), kMaxAllowedValuesOfSamplesPerBand); |
peah | 701d628 | 2016-10-25 05:42:20 -0700 | [diff] [blame] | 965 | |
ivoc | 9f4a4a0 | 2016-10-28 05:39:16 -0700 | [diff] [blame] | 966 | const size_t new_red_render_queue_element_max_size = |
| 967 | std::max(static_cast<size_t>(1), kMaxAllowedValuesOfSamplesPerFrame); |
| 968 | |
peah | a062460 | 2016-10-25 04:45:24 -0700 | [diff] [blame] | 969 | // Reallocate the queues if the queue item sizes are too small to fit the |
| 970 | // data to put in the queues. |
peah | 701d628 | 2016-10-25 05:42:20 -0700 | [diff] [blame] | 971 | if (aec_render_queue_element_max_size_ < |
| 972 | new_aec_render_queue_element_max_size) { |
| 973 | aec_render_queue_element_max_size_ = new_aec_render_queue_element_max_size; |
peah | 764e364 | 2016-10-22 05:04:30 -0700 | [diff] [blame] | 974 | |
peah | a062460 | 2016-10-25 04:45:24 -0700 | [diff] [blame] | 975 | std::vector<float> template_queue_element( |
peah | 701d628 | 2016-10-25 05:42:20 -0700 | [diff] [blame] | 976 | aec_render_queue_element_max_size_); |
peah | a062460 | 2016-10-25 04:45:24 -0700 | [diff] [blame] | 977 | |
peah | 701d628 | 2016-10-25 05:42:20 -0700 | [diff] [blame] | 978 | aec_render_signal_queue_.reset( |
peah | 764e364 | 2016-10-22 05:04:30 -0700 | [diff] [blame] | 979 | new SwapQueue<std::vector<float>, RenderQueueItemVerifier<float>>( |
| 980 | kMaxNumFramesToBuffer, template_queue_element, |
peah | a062460 | 2016-10-25 04:45:24 -0700 | [diff] [blame] | 981 | RenderQueueItemVerifier<float>( |
peah | 701d628 | 2016-10-25 05:42:20 -0700 | [diff] [blame] | 982 | aec_render_queue_element_max_size_))); |
peah | 764e364 | 2016-10-22 05:04:30 -0700 | [diff] [blame] | 983 | |
peah | 701d628 | 2016-10-25 05:42:20 -0700 | [diff] [blame] | 984 | aec_render_queue_buffer_.resize(aec_render_queue_element_max_size_); |
| 985 | aec_capture_queue_buffer_.resize(aec_render_queue_element_max_size_); |
peah | 764e364 | 2016-10-22 05:04:30 -0700 | [diff] [blame] | 986 | } else { |
peah | 701d628 | 2016-10-25 05:42:20 -0700 | [diff] [blame] | 987 | aec_render_signal_queue_->Clear(); |
peah | a062460 | 2016-10-25 04:45:24 -0700 | [diff] [blame] | 988 | } |
| 989 | |
peah | 701d628 | 2016-10-25 05:42:20 -0700 | [diff] [blame] | 990 | if (aecm_render_queue_element_max_size_ < |
| 991 | new_aecm_render_queue_element_max_size) { |
| 992 | aecm_render_queue_element_max_size_ = |
| 993 | new_aecm_render_queue_element_max_size; |
peah | a062460 | 2016-10-25 04:45:24 -0700 | [diff] [blame] | 994 | |
| 995 | std::vector<int16_t> template_queue_element( |
peah | 701d628 | 2016-10-25 05:42:20 -0700 | [diff] [blame] | 996 | aecm_render_queue_element_max_size_); |
peah | a062460 | 2016-10-25 04:45:24 -0700 | [diff] [blame] | 997 | |
peah | 701d628 | 2016-10-25 05:42:20 -0700 | [diff] [blame] | 998 | aecm_render_signal_queue_.reset( |
peah | a062460 | 2016-10-25 04:45:24 -0700 | [diff] [blame] | 999 | new SwapQueue<std::vector<int16_t>, RenderQueueItemVerifier<int16_t>>( |
| 1000 | kMaxNumFramesToBuffer, template_queue_element, |
| 1001 | RenderQueueItemVerifier<int16_t>( |
peah | 701d628 | 2016-10-25 05:42:20 -0700 | [diff] [blame] | 1002 | aecm_render_queue_element_max_size_))); |
peah | a062460 | 2016-10-25 04:45:24 -0700 | [diff] [blame] | 1003 | |
peah | 701d628 | 2016-10-25 05:42:20 -0700 | [diff] [blame] | 1004 | aecm_render_queue_buffer_.resize(aecm_render_queue_element_max_size_); |
| 1005 | aecm_capture_queue_buffer_.resize(aecm_render_queue_element_max_size_); |
peah | a062460 | 2016-10-25 04:45:24 -0700 | [diff] [blame] | 1006 | } else { |
peah | 701d628 | 2016-10-25 05:42:20 -0700 | [diff] [blame] | 1007 | aecm_render_signal_queue_->Clear(); |
| 1008 | } |
| 1009 | |
| 1010 | if (agc_render_queue_element_max_size_ < |
| 1011 | new_agc_render_queue_element_max_size) { |
| 1012 | agc_render_queue_element_max_size_ = new_agc_render_queue_element_max_size; |
| 1013 | |
| 1014 | std::vector<int16_t> template_queue_element( |
| 1015 | agc_render_queue_element_max_size_); |
| 1016 | |
| 1017 | agc_render_signal_queue_.reset( |
| 1018 | new SwapQueue<std::vector<int16_t>, RenderQueueItemVerifier<int16_t>>( |
| 1019 | kMaxNumFramesToBuffer, template_queue_element, |
| 1020 | RenderQueueItemVerifier<int16_t>( |
| 1021 | agc_render_queue_element_max_size_))); |
| 1022 | |
| 1023 | agc_render_queue_buffer_.resize(agc_render_queue_element_max_size_); |
| 1024 | agc_capture_queue_buffer_.resize(agc_render_queue_element_max_size_); |
| 1025 | } else { |
| 1026 | agc_render_signal_queue_->Clear(); |
peah | 764e364 | 2016-10-22 05:04:30 -0700 | [diff] [blame] | 1027 | } |
ivoc | 9f4a4a0 | 2016-10-28 05:39:16 -0700 | [diff] [blame] | 1028 | |
| 1029 | if (red_render_queue_element_max_size_ < |
| 1030 | new_red_render_queue_element_max_size) { |
| 1031 | red_render_queue_element_max_size_ = new_red_render_queue_element_max_size; |
| 1032 | |
| 1033 | std::vector<float> template_queue_element( |
| 1034 | red_render_queue_element_max_size_); |
| 1035 | |
| 1036 | red_render_signal_queue_.reset( |
| 1037 | new SwapQueue<std::vector<float>, RenderQueueItemVerifier<float>>( |
| 1038 | kMaxNumFramesToBuffer, template_queue_element, |
| 1039 | RenderQueueItemVerifier<float>( |
| 1040 | red_render_queue_element_max_size_))); |
| 1041 | |
| 1042 | red_render_queue_buffer_.resize(red_render_queue_element_max_size_); |
| 1043 | red_capture_queue_buffer_.resize(red_render_queue_element_max_size_); |
| 1044 | } else { |
| 1045 | red_render_signal_queue_->Clear(); |
| 1046 | } |
peah | 764e364 | 2016-10-22 05:04:30 -0700 | [diff] [blame] | 1047 | } |
| 1048 | |
| 1049 | void AudioProcessingImpl::EmptyQueuedRenderAudio() { |
| 1050 | rtc::CritScope cs_capture(&crit_capture_); |
peah | 701d628 | 2016-10-25 05:42:20 -0700 | [diff] [blame] | 1051 | while (aec_render_signal_queue_->Remove(&aec_capture_queue_buffer_)) { |
peah | 764e364 | 2016-10-22 05:04:30 -0700 | [diff] [blame] | 1052 | public_submodules_->echo_cancellation->ProcessRenderAudio( |
peah | 701d628 | 2016-10-25 05:42:20 -0700 | [diff] [blame] | 1053 | aec_capture_queue_buffer_); |
peah | a062460 | 2016-10-25 04:45:24 -0700 | [diff] [blame] | 1054 | } |
| 1055 | |
peah | 701d628 | 2016-10-25 05:42:20 -0700 | [diff] [blame] | 1056 | while (aecm_render_signal_queue_->Remove(&aecm_capture_queue_buffer_)) { |
peah | a062460 | 2016-10-25 04:45:24 -0700 | [diff] [blame] | 1057 | public_submodules_->echo_control_mobile->ProcessRenderAudio( |
peah | 701d628 | 2016-10-25 05:42:20 -0700 | [diff] [blame] | 1058 | aecm_capture_queue_buffer_); |
| 1059 | } |
| 1060 | |
| 1061 | while (agc_render_signal_queue_->Remove(&agc_capture_queue_buffer_)) { |
| 1062 | public_submodules_->gain_control->ProcessRenderAudio( |
| 1063 | agc_capture_queue_buffer_); |
peah | 764e364 | 2016-10-22 05:04:30 -0700 | [diff] [blame] | 1064 | } |
ivoc | 9f4a4a0 | 2016-10-28 05:39:16 -0700 | [diff] [blame] | 1065 | |
| 1066 | while (red_render_signal_queue_->Remove(&red_capture_queue_buffer_)) { |
| 1067 | private_submodules_->residual_echo_detector->AnalyzeRenderAudio( |
| 1068 | red_capture_queue_buffer_); |
| 1069 | } |
peah | 764e364 | 2016-10-22 05:04:30 -0700 | [diff] [blame] | 1070 | } |
| 1071 | |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 1072 | int AudioProcessingImpl::ProcessStream(AudioFrame* frame) { |
peah | 369f828 | 2015-12-17 06:42:29 -0800 | [diff] [blame] | 1073 | TRACE_EVENT0("webrtc", "AudioProcessing::ProcessStream_AudioFrame"); |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1074 | { |
| 1075 | // Acquire the capture lock in order to safely call the function |
| 1076 | // that retrieves the render side data. This function accesses apm |
| 1077 | // getters that need the capture lock held when being called. |
| 1078 | // The lock needs to be released as |
| 1079 | // public_submodules_->echo_control_mobile->is_enabled() aquires this lock |
| 1080 | // as well. |
| 1081 | rtc::CritScope cs_capture(&crit_capture_); |
peah | 764e364 | 2016-10-22 05:04:30 -0700 | [diff] [blame] | 1082 | EmptyQueuedRenderAudio(); |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1083 | } |
peah | fa6228e | 2015-11-16 16:27:42 -0800 | [diff] [blame] | 1084 | |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 1085 | if (!frame) { |
| 1086 | return kNullPointerError; |
| 1087 | } |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 1088 | // Must be a native rate. |
| 1089 | if (frame->sample_rate_hz_ != kSampleRate8kHz && |
| 1090 | frame->sample_rate_hz_ != kSampleRate16kHz && |
aluebs@webrtc.org | 087da13 | 2014-11-17 23:01:23 +0000 | [diff] [blame] | 1091 | frame->sample_rate_hz_ != kSampleRate32kHz && |
| 1092 | frame->sample_rate_hz_ != kSampleRate48kHz) { |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 1093 | return kBadSampleRateError; |
| 1094 | } |
peah | 192164e | 2015-11-17 02:16:45 -0800 | [diff] [blame] | 1095 | |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1096 | ProcessingConfig processing_config; |
peah | 2ace3f9 | 2016-09-10 04:42:27 -0700 | [diff] [blame] | 1097 | bool reinitialization_required = false; |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1098 | { |
| 1099 | // Aquire lock for the access of api_format. |
| 1100 | // The lock is released immediately due to the conditional |
| 1101 | // reinitialization. |
| 1102 | rtc::CritScope cs_capture(&crit_capture_); |
| 1103 | // TODO(ajm): The input and output rates and channels are currently |
| 1104 | // constrained to be identical in the int16 interface. |
| 1105 | processing_config = formats_.api_format; |
peah | 2ace3f9 | 2016-09-10 04:42:27 -0700 | [diff] [blame] | 1106 | |
| 1107 | reinitialization_required = UpdateActiveSubmoduleStates(); |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1108 | } |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 1109 | processing_config.input_stream().set_sample_rate_hz(frame->sample_rate_hz_); |
| 1110 | processing_config.input_stream().set_num_channels(frame->num_channels_); |
| 1111 | processing_config.output_stream().set_sample_rate_hz(frame->sample_rate_hz_); |
| 1112 | processing_config.output_stream().set_num_channels(frame->num_channels_); |
| 1113 | |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1114 | { |
| 1115 | // Do conditional reinitialization. |
| 1116 | rtc::CritScope cs_render(&crit_render_); |
peah | 2ace3f9 | 2016-09-10 04:42:27 -0700 | [diff] [blame] | 1117 | RETURN_ON_ERR( |
| 1118 | MaybeInitializeCapture(processing_config, reinitialization_required)); |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1119 | } |
| 1120 | rtc::CritScope cs_capture(&crit_capture_); |
peah | 192164e | 2015-11-17 02:16:45 -0800 | [diff] [blame] | 1121 | if (frame->samples_per_channel_ != |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1122 | formats_.api_format.input_stream().num_frames()) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1123 | return kBadDataLengthError; |
| 1124 | } |
| 1125 | |
andrew@webrtc.org | 7bf2646 | 2011-12-03 00:03:31 +0000 | [diff] [blame] | 1126 | #ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP |
tommi | a6219cc | 2016-06-15 10:30:14 -0700 | [diff] [blame] | 1127 | if (debug_dump_.debug_file->is_open()) { |
peah | 644fa96 | 2016-08-18 06:48:33 -0700 | [diff] [blame] | 1128 | RETURN_ON_ERR(WriteConfigMessage(false)); |
| 1129 | |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1130 | debug_dump_.capture.event_msg->set_type(audioproc::Event::STREAM); |
| 1131 | audioproc::Stream* msg = debug_dump_.capture.event_msg->mutable_stream(); |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 1132 | const size_t data_size = |
| 1133 | sizeof(int16_t) * frame->samples_per_channel_ * frame->num_channels_; |
andrew@webrtc.org | 63a5098 | 2012-05-02 23:56:37 +0000 | [diff] [blame] | 1134 | msg->set_input_data(frame->data_, data_size); |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 1135 | } |
| 1136 | #endif |
| 1137 | |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1138 | capture_.capture_audio->DeinterleaveFrom(frame); |
peah | de65ddc | 2016-09-16 15:02:15 -0700 | [diff] [blame] | 1139 | RETURN_ON_ERR(ProcessCaptureStreamLocked()); |
peah | 2ace3f9 | 2016-09-10 04:42:27 -0700 | [diff] [blame] | 1140 | capture_.capture_audio->InterleaveTo( |
peah | 23ac8b4 | 2017-05-23 05:33:56 -0700 | [diff] [blame^] | 1141 | frame, submodule_states_.CaptureMultiBandProcessingActive() || |
| 1142 | submodule_states_.CaptureFullBandProcessingActive()); |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 1143 | |
| 1144 | #ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP |
tommi | a6219cc | 2016-06-15 10:30:14 -0700 | [diff] [blame] | 1145 | if (debug_dump_.debug_file->is_open()) { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1146 | audioproc::Stream* msg = debug_dump_.capture.event_msg->mutable_stream(); |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 1147 | const size_t data_size = |
| 1148 | sizeof(int16_t) * frame->samples_per_channel_ * frame->num_channels_; |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 1149 | msg->set_output_data(frame->data_, data_size); |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1150 | RETURN_ON_ERR(WriteMessageToDebugFile(debug_dump_.debug_file.get(), |
ivoc | d66b44d | 2016-01-15 03:06:36 -0800 | [diff] [blame] | 1151 | &debug_dump_.num_bytes_left_for_log_, |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1152 | &crit_debug_, &debug_dump_.capture)); |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 1153 | } |
| 1154 | #endif |
| 1155 | |
| 1156 | return kNoError; |
| 1157 | } |
| 1158 | |
peah | de65ddc | 2016-09-16 15:02:15 -0700 | [diff] [blame] | 1159 | int AudioProcessingImpl::ProcessCaptureStreamLocked() { |
peah | b58a158 | 2016-03-15 09:34:24 -0700 | [diff] [blame] | 1160 | // Ensure that not both the AEC and AECM are active at the same time. |
| 1161 | // TODO(peah): Simplify once the public API Enable functions for these |
| 1162 | // are moved to APM. |
| 1163 | RTC_DCHECK(!(public_submodules_->echo_cancellation->is_enabled() && |
| 1164 | public_submodules_->echo_control_mobile->is_enabled())); |
| 1165 | |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 1166 | #ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP |
tommi | a6219cc | 2016-06-15 10:30:14 -0700 | [diff] [blame] | 1167 | if (debug_dump_.debug_file->is_open()) { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1168 | audioproc::Stream* msg = debug_dump_.capture.event_msg->mutable_stream(); |
| 1169 | msg->set_delay(capture_nonlocked_.stream_delay_ms); |
| 1170 | msg->set_drift( |
| 1171 | public_submodules_->echo_cancellation->stream_drift_samples()); |
bjornv@webrtc.org | 63da1dd | 2015-02-06 19:44:21 +0000 | [diff] [blame] | 1172 | msg->set_level(gain_control()->stream_analog_level()); |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1173 | msg->set_keypress(capture_.key_pressed); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1174 | } |
andrew@webrtc.org | 7bf2646 | 2011-12-03 00:03:31 +0000 | [diff] [blame] | 1175 | #endif |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1176 | |
Bjorn Volcker | 1ca324f | 2015-06-29 14:57:29 +0200 | [diff] [blame] | 1177 | MaybeUpdateHistograms(); |
| 1178 | |
peah | de65ddc | 2016-09-16 15:02:15 -0700 | [diff] [blame] | 1179 | AudioBuffer* capture_buffer = capture_.capture_audio.get(); // For brevity. |
ekmeyerson | 60d9b33 | 2015-08-14 10:35:55 -0700 | [diff] [blame] | 1180 | |
peah | 1b08dc3 | 2016-12-20 13:45:58 -0800 | [diff] [blame] | 1181 | capture_input_rms_.Analyze(rtc::ArrayView<const int16_t>( |
henrik.lundin | 290d43a | 2016-11-29 08:09:09 -0800 | [diff] [blame] | 1182 | capture_buffer->channels_const()[0], |
| 1183 | capture_nonlocked_.capture_processing_format.num_frames())); |
peah | 1b08dc3 | 2016-12-20 13:45:58 -0800 | [diff] [blame] | 1184 | const bool log_rms = ++capture_rms_interval_counter_ >= 1000; |
| 1185 | if (log_rms) { |
| 1186 | capture_rms_interval_counter_ = 0; |
| 1187 | RmsLevel::Levels levels = capture_input_rms_.AverageAndPeak(); |
henrik.lundin | 45bb513 | 2016-12-06 04:28:04 -0800 | [diff] [blame] | 1188 | RTC_HISTOGRAM_COUNTS_LINEAR("WebRTC.Audio.ApmCaptureInputLevelAverageRms", |
| 1189 | levels.average, 1, RmsLevel::kMinLevelDb, 64); |
| 1190 | RTC_HISTOGRAM_COUNTS_LINEAR("WebRTC.Audio.ApmCaptureInputLevelPeakRms", |
| 1191 | levels.peak, 1, RmsLevel::kMinLevelDb, 64); |
henrik.lundin | 290d43a | 2016-11-29 08:09:09 -0800 | [diff] [blame] | 1192 | } |
| 1193 | |
peah | e0eae3c | 2016-12-14 01:16:23 -0800 | [diff] [blame] | 1194 | if (private_submodules_->echo_canceller3) { |
peah | 6799553 | 2017-04-10 14:12:41 -0700 | [diff] [blame] | 1195 | const int new_agc_level = gain_control()->stream_analog_level(); |
| 1196 | capture_.echo_path_gain_change = |
peah | 103ac7e | 2017-04-12 05:40:55 -0700 | [diff] [blame] | 1197 | abs(capture_.previous_agc_level - new_agc_level) > 5; |
peah | 6799553 | 2017-04-10 14:12:41 -0700 | [diff] [blame] | 1198 | capture_.previous_agc_level = new_agc_level; |
peah | e0eae3c | 2016-12-14 01:16:23 -0800 | [diff] [blame] | 1199 | private_submodules_->echo_canceller3->AnalyzeCapture(capture_buffer); |
| 1200 | } |
| 1201 | |
peah | be61562 | 2016-02-13 16:40:47 -0800 | [diff] [blame] | 1202 | if (constants_.use_experimental_agc && |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1203 | public_submodules_->gain_control->is_enabled()) { |
| 1204 | private_submodules_->agc_manager->AnalyzePreProcess( |
peah | de65ddc | 2016-09-16 15:02:15 -0700 | [diff] [blame] | 1205 | capture_buffer->channels()[0], capture_buffer->num_channels(), |
| 1206 | capture_nonlocked_.capture_processing_format.num_frames()); |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 1207 | } |
| 1208 | |
peah | 2ace3f9 | 2016-09-10 04:42:27 -0700 | [diff] [blame] | 1209 | if (submodule_states_.CaptureMultiBandSubModulesActive() && |
| 1210 | SampleRateSupportsMultiBand( |
peah | de65ddc | 2016-09-16 15:02:15 -0700 | [diff] [blame] | 1211 | capture_nonlocked_.capture_processing_format.sample_rate_hz())) { |
| 1212 | capture_buffer->SplitIntoFrequencyBands(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1213 | } |
| 1214 | |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 1215 | if (private_submodules_->echo_canceller3) { |
| 1216 | // Force down-mixing of the number of channels after the detection of |
| 1217 | // capture signal saturation. |
| 1218 | // TODO(peah): Look into ensuring that this kind of tampering with the |
| 1219 | // AudioBuffer functionality should not be needed. |
| 1220 | capture_buffer->set_num_channels(1); |
| 1221 | } |
| 1222 | |
aluebs | b2328d1 | 2016-01-11 20:32:29 -0800 | [diff] [blame] | 1223 | if (capture_nonlocked_.beamformer_enabled) { |
peah | de65ddc | 2016-09-16 15:02:15 -0700 | [diff] [blame] | 1224 | private_submodules_->beamformer->AnalyzeChunk( |
| 1225 | *capture_buffer->split_data_f()); |
Alejandro Luebs | f4022ff | 2016-07-01 17:19:09 -0700 | [diff] [blame] | 1226 | // Discards all channels by the leftmost one. |
peah | de65ddc | 2016-09-16 15:02:15 -0700 | [diff] [blame] | 1227 | capture_buffer->set_num_channels(1); |
aluebs@webrtc.org | ae643ce | 2014-12-19 19:57:34 +0000 | [diff] [blame] | 1228 | } |
aluebs@webrtc.org | ae643ce | 2014-12-19 19:57:34 +0000 | [diff] [blame] | 1229 | |
peah | e0eae3c | 2016-12-14 01:16:23 -0800 | [diff] [blame] | 1230 | // TODO(peah): Move the AEC3 low-cut filter to this place. |
| 1231 | if (private_submodules_->low_cut_filter && |
| 1232 | !private_submodules_->echo_canceller3) { |
peah | 8271d04 | 2016-11-22 07:24:52 -0800 | [diff] [blame] | 1233 | private_submodules_->low_cut_filter->Process(capture_buffer); |
| 1234 | } |
peah | de65ddc | 2016-09-16 15:02:15 -0700 | [diff] [blame] | 1235 | RETURN_ON_ERR( |
| 1236 | public_submodules_->gain_control->AnalyzeCaptureAudio(capture_buffer)); |
| 1237 | public_submodules_->noise_suppression->AnalyzeCaptureAudio(capture_buffer); |
peah | b58a158 | 2016-03-15 09:34:24 -0700 | [diff] [blame] | 1238 | |
| 1239 | // Ensure that the stream delay was set before the call to the |
| 1240 | // AEC ProcessCaptureAudio function. |
| 1241 | if (public_submodules_->echo_cancellation->is_enabled() && |
| 1242 | !was_stream_delay_set()) { |
| 1243 | return AudioProcessing::kStreamParameterNotSetError; |
| 1244 | } |
| 1245 | |
peah | e0eae3c | 2016-12-14 01:16:23 -0800 | [diff] [blame] | 1246 | if (private_submodules_->echo_canceller3) { |
peah | 6799553 | 2017-04-10 14:12:41 -0700 | [diff] [blame] | 1247 | private_submodules_->echo_canceller3->ProcessCapture( |
| 1248 | capture_buffer, capture_.echo_path_gain_change); |
peah | 61202ac | 2017-02-06 03:39:42 -0800 | [diff] [blame] | 1249 | } else { |
| 1250 | RETURN_ON_ERR(public_submodules_->echo_cancellation->ProcessCaptureAudio( |
| 1251 | capture_buffer, stream_delay_ms())); |
peah | e0eae3c | 2016-12-14 01:16:23 -0800 | [diff] [blame] | 1252 | } |
| 1253 | |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1254 | if (public_submodules_->echo_control_mobile->is_enabled() && |
| 1255 | public_submodules_->noise_suppression->is_enabled()) { |
peah | de65ddc | 2016-09-16 15:02:15 -0700 | [diff] [blame] | 1256 | capture_buffer->CopyLowPassToReference(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1257 | } |
peah | de65ddc | 2016-09-16 15:02:15 -0700 | [diff] [blame] | 1258 | public_submodules_->noise_suppression->ProcessCaptureAudio(capture_buffer); |
peah | 1bcfce5 | 2016-08-26 07:16:04 -0700 | [diff] [blame] | 1259 | #if WEBRTC_INTELLIGIBILITY_ENHANCER |
Alejandro Luebs | c9b0c26 | 2016-05-16 15:32:38 -0700 | [diff] [blame] | 1260 | if (capture_nonlocked_.intelligibility_enabled) { |
aluebs | c466bad | 2016-02-10 12:03:00 -0800 | [diff] [blame] | 1261 | RTC_DCHECK(public_submodules_->noise_suppression->is_enabled()); |
Alejandro Luebs | c9b0c26 | 2016-05-16 15:32:38 -0700 | [diff] [blame] | 1262 | int gain_db = public_submodules_->gain_control->is_enabled() ? |
| 1263 | public_submodules_->gain_control->compression_gain_db() : |
| 1264 | 0; |
Alejandro Luebs | 5041110 | 2016-06-30 15:35:41 -0700 | [diff] [blame] | 1265 | float gain = std::pow(10.f, gain_db / 20.f); |
| 1266 | gain *= capture_nonlocked_.level_controller_enabled ? |
| 1267 | private_submodules_->level_controller->GetLastGain() : |
| 1268 | 1.f; |
aluebs | c466bad | 2016-02-10 12:03:00 -0800 | [diff] [blame] | 1269 | public_submodules_->intelligibility_enhancer->SetCaptureNoiseEstimate( |
Alejandro Luebs | 5041110 | 2016-06-30 15:35:41 -0700 | [diff] [blame] | 1270 | public_submodules_->noise_suppression->NoiseEstimate(), gain); |
aluebs | c466bad | 2016-02-10 12:03:00 -0800 | [diff] [blame] | 1271 | } |
peah | 1bcfce5 | 2016-08-26 07:16:04 -0700 | [diff] [blame] | 1272 | #endif |
peah | 253534d | 2016-03-15 04:32:28 -0700 | [diff] [blame] | 1273 | |
| 1274 | // Ensure that the stream delay was set before the call to the |
| 1275 | // AECM ProcessCaptureAudio function. |
| 1276 | if (public_submodules_->echo_control_mobile->is_enabled() && |
| 1277 | !was_stream_delay_set()) { |
| 1278 | return AudioProcessing::kStreamParameterNotSetError; |
| 1279 | } |
| 1280 | |
| 1281 | RETURN_ON_ERR(public_submodules_->echo_control_mobile->ProcessCaptureAudio( |
peah | de65ddc | 2016-09-16 15:02:15 -0700 | [diff] [blame] | 1282 | capture_buffer, stream_delay_ms())); |
peah | 253534d | 2016-03-15 04:32:28 -0700 | [diff] [blame] | 1283 | |
ivoc | 9f4a4a0 | 2016-10-28 05:39:16 -0700 | [diff] [blame] | 1284 | |
Alejandro Luebs | f4022ff | 2016-07-01 17:19:09 -0700 | [diff] [blame] | 1285 | if (capture_nonlocked_.beamformer_enabled) { |
peah | de65ddc | 2016-09-16 15:02:15 -0700 | [diff] [blame] | 1286 | private_submodules_->beamformer->PostFilter(capture_buffer->split_data_f()); |
Alejandro Luebs | f4022ff | 2016-07-01 17:19:09 -0700 | [diff] [blame] | 1287 | } |
| 1288 | |
peah | de65ddc | 2016-09-16 15:02:15 -0700 | [diff] [blame] | 1289 | public_submodules_->voice_detection->ProcessCaptureAudio(capture_buffer); |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 1290 | |
peah | be61562 | 2016-02-13 16:40:47 -0800 | [diff] [blame] | 1291 | if (constants_.use_experimental_agc && |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1292 | public_submodules_->gain_control->is_enabled() && |
aluebs | b2328d1 | 2016-01-11 20:32:29 -0800 | [diff] [blame] | 1293 | (!capture_nonlocked_.beamformer_enabled || |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1294 | private_submodules_->beamformer->is_target_present())) { |
| 1295 | private_submodules_->agc_manager->Process( |
peah | de65ddc | 2016-09-16 15:02:15 -0700 | [diff] [blame] | 1296 | capture_buffer->split_bands_const(0)[kBand0To8kHz], |
| 1297 | capture_buffer->num_frames_per_band(), capture_nonlocked_.split_rate); |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 1298 | } |
peah | b8fbb54 | 2016-03-15 02:28:08 -0700 | [diff] [blame] | 1299 | RETURN_ON_ERR(public_submodules_->gain_control->ProcessCaptureAudio( |
peah | de65ddc | 2016-09-16 15:02:15 -0700 | [diff] [blame] | 1300 | capture_buffer, echo_cancellation()->stream_has_echo())); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1301 | |
peah | 2ace3f9 | 2016-09-10 04:42:27 -0700 | [diff] [blame] | 1302 | if (submodule_states_.CaptureMultiBandProcessingActive() && |
| 1303 | SampleRateSupportsMultiBand( |
peah | de65ddc | 2016-09-16 15:02:15 -0700 | [diff] [blame] | 1304 | capture_nonlocked_.capture_processing_format.sample_rate_hz())) { |
| 1305 | capture_buffer->MergeFrequencyBands(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1306 | } |
| 1307 | |
peah | 9e6a290 | 2017-05-15 07:19:21 -0700 | [diff] [blame] | 1308 | if (config_.residual_echo_detector.enabled) { |
| 1309 | private_submodules_->residual_echo_detector->AnalyzeCaptureAudio( |
| 1310 | rtc::ArrayView<const float>(capture_buffer->channels_f()[0], |
| 1311 | capture_buffer->num_frames())); |
| 1312 | } |
| 1313 | |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 1314 | // TODO(aluebs): Investigate if the transient suppression placement should be |
| 1315 | // before or after the AGC. |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1316 | if (capture_.transient_suppressor_enabled) { |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 1317 | float voice_probability = |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1318 | private_submodules_->agc_manager.get() |
| 1319 | ? private_submodules_->agc_manager->voice_probability() |
| 1320 | : 1.f; |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 1321 | |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1322 | public_submodules_->transient_suppressor->Suppress( |
peah | de65ddc | 2016-09-16 15:02:15 -0700 | [diff] [blame] | 1323 | capture_buffer->channels_f()[0], capture_buffer->num_frames(), |
| 1324 | capture_buffer->num_channels(), |
| 1325 | capture_buffer->split_bands_const_f(0)[kBand0To8kHz], |
| 1326 | capture_buffer->num_frames_per_band(), capture_buffer->keyboard_data(), |
| 1327 | capture_buffer->num_keyboard_frames(), voice_probability, |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1328 | capture_.key_pressed); |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 1329 | } |
| 1330 | |
alessiob | 3ec96df | 2017-05-22 06:57:06 -0700 | [diff] [blame] | 1331 | if (capture_nonlocked_.gain_controller2_enabled) { |
| 1332 | private_submodules_->gain_controller2->Process(capture_buffer); |
| 1333 | } |
| 1334 | |
peah | ca4cac7 | 2016-06-29 15:26:12 -0700 | [diff] [blame] | 1335 | if (capture_nonlocked_.level_controller_enabled) { |
peah | de65ddc | 2016-09-16 15:02:15 -0700 | [diff] [blame] | 1336 | private_submodules_->level_controller->Process(capture_buffer); |
peah | ca4cac7 | 2016-06-29 15:26:12 -0700 | [diff] [blame] | 1337 | } |
| 1338 | |
andrew@webrtc.org | 755b04a | 2011-11-15 16:57:56 +0000 | [diff] [blame] | 1339 | // The level estimator operates on the recombined data. |
peah | de65ddc | 2016-09-16 15:02:15 -0700 | [diff] [blame] | 1340 | public_submodules_->level_estimator->ProcessStream(capture_buffer); |
ajm@google.com | 808e0e0 | 2011-08-03 21:08:51 +0000 | [diff] [blame] | 1341 | |
peah | 1b08dc3 | 2016-12-20 13:45:58 -0800 | [diff] [blame] | 1342 | capture_output_rms_.Analyze(rtc::ArrayView<const int16_t>( |
| 1343 | capture_buffer->channels_const()[0], |
| 1344 | capture_nonlocked_.capture_processing_format.num_frames())); |
| 1345 | if (log_rms) { |
| 1346 | RmsLevel::Levels levels = capture_output_rms_.AverageAndPeak(); |
| 1347 | RTC_HISTOGRAM_COUNTS_LINEAR("WebRTC.Audio.ApmCaptureOutputLevelAverageRms", |
| 1348 | levels.average, 1, RmsLevel::kMinLevelDb, 64); |
| 1349 | RTC_HISTOGRAM_COUNTS_LINEAR("WebRTC.Audio.ApmCaptureOutputLevelPeakRms", |
| 1350 | levels.peak, 1, RmsLevel::kMinLevelDb, 64); |
| 1351 | } |
| 1352 | |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1353 | capture_.was_stream_delay_set = false; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1354 | return kNoError; |
| 1355 | } |
| 1356 | |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 1357 | int AudioProcessingImpl::AnalyzeReverseStream(const float* const* data, |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 1358 | size_t samples_per_channel, |
peah | de65ddc | 2016-09-16 15:02:15 -0700 | [diff] [blame] | 1359 | int sample_rate_hz, |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 1360 | ChannelLayout layout) { |
peah | 369f828 | 2015-12-17 06:42:29 -0800 | [diff] [blame] | 1361 | TRACE_EVENT0("webrtc", "AudioProcessing::AnalyzeReverseStream_ChannelLayout"); |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1362 | rtc::CritScope cs(&crit_render_); |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 1363 | const StreamConfig reverse_config = { |
peah | de65ddc | 2016-09-16 15:02:15 -0700 | [diff] [blame] | 1364 | sample_rate_hz, ChannelsFromLayout(layout), LayoutHasKeyboard(layout), |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 1365 | }; |
| 1366 | if (samples_per_channel != reverse_config.num_frames()) { |
| 1367 | return kBadDataLengthError; |
| 1368 | } |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1369 | return AnalyzeReverseStreamLocked(data, reverse_config, reverse_config); |
ekmeyerson | 60d9b33 | 2015-08-14 10:35:55 -0700 | [diff] [blame] | 1370 | } |
| 1371 | |
peah | de65ddc | 2016-09-16 15:02:15 -0700 | [diff] [blame] | 1372 | int AudioProcessingImpl::ProcessReverseStream(const float* const* src, |
| 1373 | const StreamConfig& input_config, |
| 1374 | const StreamConfig& output_config, |
| 1375 | float* const* dest) { |
peah | 369f828 | 2015-12-17 06:42:29 -0800 | [diff] [blame] | 1376 | TRACE_EVENT0("webrtc", "AudioProcessing::ProcessReverseStream_StreamConfig"); |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1377 | rtc::CritScope cs(&crit_render_); |
peah | de65ddc | 2016-09-16 15:02:15 -0700 | [diff] [blame] | 1378 | RETURN_ON_ERR(AnalyzeReverseStreamLocked(src, input_config, output_config)); |
peah | 2ace3f9 | 2016-09-10 04:42:27 -0700 | [diff] [blame] | 1379 | if (submodule_states_.RenderMultiBandProcessingActive()) { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1380 | render_.render_audio->CopyTo(formats_.api_format.reverse_output_stream(), |
| 1381 | dest); |
peah | 2ace3f9 | 2016-09-10 04:42:27 -0700 | [diff] [blame] | 1382 | } else if (formats_.api_format.reverse_input_stream() != |
| 1383 | formats_.api_format.reverse_output_stream()) { |
peah | de65ddc | 2016-09-16 15:02:15 -0700 | [diff] [blame] | 1384 | render_.render_converter->Convert(src, input_config.num_samples(), dest, |
| 1385 | output_config.num_samples()); |
ekmeyerson | 60d9b33 | 2015-08-14 10:35:55 -0700 | [diff] [blame] | 1386 | } else { |
peah | de65ddc | 2016-09-16 15:02:15 -0700 | [diff] [blame] | 1387 | CopyAudioIfNeeded(src, input_config.num_frames(), |
| 1388 | input_config.num_channels(), dest); |
ekmeyerson | 60d9b33 | 2015-08-14 10:35:55 -0700 | [diff] [blame] | 1389 | } |
| 1390 | |
| 1391 | return kNoError; |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 1392 | } |
| 1393 | |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1394 | int AudioProcessingImpl::AnalyzeReverseStreamLocked( |
ekmeyerson | 60d9b33 | 2015-08-14 10:35:55 -0700 | [diff] [blame] | 1395 | const float* const* src, |
peah | de65ddc | 2016-09-16 15:02:15 -0700 | [diff] [blame] | 1396 | const StreamConfig& input_config, |
| 1397 | const StreamConfig& output_config) { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1398 | if (src == nullptr) { |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 1399 | return kNullPointerError; |
| 1400 | } |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 1401 | |
peah | de65ddc | 2016-09-16 15:02:15 -0700 | [diff] [blame] | 1402 | if (input_config.num_channels() == 0) { |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 1403 | return kBadNumberChannelsError; |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 1404 | } |
| 1405 | |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1406 | ProcessingConfig processing_config = formats_.api_format; |
peah | de65ddc | 2016-09-16 15:02:15 -0700 | [diff] [blame] | 1407 | processing_config.reverse_input_stream() = input_config; |
| 1408 | processing_config.reverse_output_stream() = output_config; |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 1409 | |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1410 | RETURN_ON_ERR(MaybeInitializeRender(processing_config)); |
peah | de65ddc | 2016-09-16 15:02:15 -0700 | [diff] [blame] | 1411 | assert(input_config.num_frames() == |
| 1412 | formats_.api_format.reverse_input_stream().num_frames()); |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 1413 | |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 1414 | #ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP |
tommi | a6219cc | 2016-06-15 10:30:14 -0700 | [diff] [blame] | 1415 | if (debug_dump_.debug_file->is_open()) { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1416 | debug_dump_.render.event_msg->set_type(audioproc::Event::REVERSE_STREAM); |
| 1417 | audioproc::ReverseStream* msg = |
| 1418 | debug_dump_.render.event_msg->mutable_reverse_stream(); |
aluebs@webrtc.org | 59a1b1b | 2014-08-28 10:43:09 +0000 | [diff] [blame] | 1419 | const size_t channel_size = |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1420 | sizeof(float) * formats_.api_format.reverse_input_stream().num_frames(); |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 1421 | for (size_t i = 0; |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1422 | i < formats_.api_format.reverse_input_stream().num_channels(); ++i) |
ekmeyerson | 60d9b33 | 2015-08-14 10:35:55 -0700 | [diff] [blame] | 1423 | msg->add_channel(src[i], channel_size); |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1424 | RETURN_ON_ERR(WriteMessageToDebugFile(debug_dump_.debug_file.get(), |
ivoc | d66b44d | 2016-01-15 03:06:36 -0800 | [diff] [blame] | 1425 | &debug_dump_.num_bytes_left_for_log_, |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1426 | &crit_debug_, &debug_dump_.render)); |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 1427 | } |
| 1428 | #endif |
| 1429 | |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1430 | render_.render_audio->CopyFrom(src, |
| 1431 | formats_.api_format.reverse_input_stream()); |
peah | de65ddc | 2016-09-16 15:02:15 -0700 | [diff] [blame] | 1432 | return ProcessRenderStreamLocked(); |
ekmeyerson | 60d9b33 | 2015-08-14 10:35:55 -0700 | [diff] [blame] | 1433 | } |
| 1434 | |
| 1435 | int AudioProcessingImpl::ProcessReverseStream(AudioFrame* frame) { |
peah | 369f828 | 2015-12-17 06:42:29 -0800 | [diff] [blame] | 1436 | TRACE_EVENT0("webrtc", "AudioProcessing::ProcessReverseStream_AudioFrame"); |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1437 | rtc::CritScope cs(&crit_render_); |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1438 | if (frame == nullptr) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1439 | return kNullPointerError; |
| 1440 | } |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 1441 | // Must be a native rate. |
| 1442 | if (frame->sample_rate_hz_ != kSampleRate8kHz && |
| 1443 | frame->sample_rate_hz_ != kSampleRate16kHz && |
aluebs@webrtc.org | 087da13 | 2014-11-17 23:01:23 +0000 | [diff] [blame] | 1444 | frame->sample_rate_hz_ != kSampleRate32kHz && |
| 1445 | frame->sample_rate_hz_ != kSampleRate48kHz) { |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 1446 | return kBadSampleRateError; |
| 1447 | } |
andrew@webrtc.org | a8b9737 | 2014-03-10 22:26:12 +0000 | [diff] [blame] | 1448 | |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 1449 | if (frame->num_channels_ <= 0) { |
| 1450 | return kBadNumberChannelsError; |
| 1451 | } |
| 1452 | |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1453 | ProcessingConfig processing_config = formats_.api_format; |
ekmeyerson | 60d9b33 | 2015-08-14 10:35:55 -0700 | [diff] [blame] | 1454 | processing_config.reverse_input_stream().set_sample_rate_hz( |
| 1455 | frame->sample_rate_hz_); |
| 1456 | processing_config.reverse_input_stream().set_num_channels( |
| 1457 | frame->num_channels_); |
| 1458 | processing_config.reverse_output_stream().set_sample_rate_hz( |
| 1459 | frame->sample_rate_hz_); |
| 1460 | processing_config.reverse_output_stream().set_num_channels( |
| 1461 | frame->num_channels_); |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 1462 | |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1463 | RETURN_ON_ERR(MaybeInitializeRender(processing_config)); |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 1464 | if (frame->samples_per_channel_ != |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1465 | formats_.api_format.reverse_input_stream().num_frames()) { |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 1466 | return kBadDataLengthError; |
| 1467 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1468 | |
andrew@webrtc.org | 7bf2646 | 2011-12-03 00:03:31 +0000 | [diff] [blame] | 1469 | #ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP |
tommi | a6219cc | 2016-06-15 10:30:14 -0700 | [diff] [blame] | 1470 | if (debug_dump_.debug_file->is_open()) { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1471 | debug_dump_.render.event_msg->set_type(audioproc::Event::REVERSE_STREAM); |
| 1472 | audioproc::ReverseStream* msg = |
| 1473 | debug_dump_.render.event_msg->mutable_reverse_stream(); |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 1474 | const size_t data_size = |
| 1475 | sizeof(int16_t) * frame->samples_per_channel_ * frame->num_channels_; |
andrew@webrtc.org | 63a5098 | 2012-05-02 23:56:37 +0000 | [diff] [blame] | 1476 | msg->set_data(frame->data_, data_size); |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1477 | RETURN_ON_ERR(WriteMessageToDebugFile(debug_dump_.debug_file.get(), |
ivoc | d66b44d | 2016-01-15 03:06:36 -0800 | [diff] [blame] | 1478 | &debug_dump_.num_bytes_left_for_log_, |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1479 | &crit_debug_, &debug_dump_.render)); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1480 | } |
andrew@webrtc.org | 7bf2646 | 2011-12-03 00:03:31 +0000 | [diff] [blame] | 1481 | #endif |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1482 | render_.render_audio->DeinterleaveFrom(frame); |
peah | de65ddc | 2016-09-16 15:02:15 -0700 | [diff] [blame] | 1483 | RETURN_ON_ERR(ProcessRenderStreamLocked()); |
peah | 2ace3f9 | 2016-09-10 04:42:27 -0700 | [diff] [blame] | 1484 | render_.render_audio->InterleaveTo( |
| 1485 | frame, submodule_states_.RenderMultiBandProcessingActive()); |
aluebs | b031955 | 2016-03-17 20:39:53 -0700 | [diff] [blame] | 1486 | return kNoError; |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 1487 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1488 | |
peah | de65ddc | 2016-09-16 15:02:15 -0700 | [diff] [blame] | 1489 | int AudioProcessingImpl::ProcessRenderStreamLocked() { |
| 1490 | AudioBuffer* render_buffer = render_.render_audio.get(); // For brevity. |
peah | 9e6a290 | 2017-05-15 07:19:21 -0700 | [diff] [blame] | 1491 | |
| 1492 | QueueNonbandedRenderAudio(render_buffer); |
| 1493 | |
peah | 2ace3f9 | 2016-09-10 04:42:27 -0700 | [diff] [blame] | 1494 | if (submodule_states_.RenderMultiBandSubModulesActive() && |
peah | de65ddc | 2016-09-16 15:02:15 -0700 | [diff] [blame] | 1495 | SampleRateSupportsMultiBand( |
| 1496 | formats_.render_processing_format.sample_rate_hz())) { |
| 1497 | render_buffer->SplitIntoFrequencyBands(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1498 | } |
| 1499 | |
peah | 1bcfce5 | 2016-08-26 07:16:04 -0700 | [diff] [blame] | 1500 | #if WEBRTC_INTELLIGIBILITY_ENHANCER |
Alejandro Luebs | c9b0c26 | 2016-05-16 15:32:38 -0700 | [diff] [blame] | 1501 | if (capture_nonlocked_.intelligibility_enabled) { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1502 | public_submodules_->intelligibility_enhancer->ProcessRenderAudio( |
Alejandro Luebs | ef00925 | 2016-09-20 14:51:56 -0700 | [diff] [blame] | 1503 | render_buffer); |
ekmeyerson | 60d9b33 | 2015-08-14 10:35:55 -0700 | [diff] [blame] | 1504 | } |
peah | 1bcfce5 | 2016-08-26 07:16:04 -0700 | [diff] [blame] | 1505 | #endif |
ekmeyerson | 60d9b33 | 2015-08-14 10:35:55 -0700 | [diff] [blame] | 1506 | |
peah | ce4d915 | 2017-05-19 01:28:05 -0700 | [diff] [blame] | 1507 | if (submodule_states_.RenderMultiBandSubModulesActive()) { |
| 1508 | QueueBandedRenderAudio(render_buffer); |
| 1509 | } |
| 1510 | |
peah | e0eae3c | 2016-12-14 01:16:23 -0800 | [diff] [blame] | 1511 | // TODO(peah): Perform the queueing Ãnside QueueRenderAudiuo(). |
| 1512 | if (private_submodules_->echo_canceller3) { |
peah | cf02cf1 | 2017-04-05 14:18:07 -0700 | [diff] [blame] | 1513 | private_submodules_->echo_canceller3->AnalyzeRender(render_buffer); |
peah | e0eae3c | 2016-12-14 01:16:23 -0800 | [diff] [blame] | 1514 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1515 | |
peah | 2ace3f9 | 2016-09-10 04:42:27 -0700 | [diff] [blame] | 1516 | if (submodule_states_.RenderMultiBandProcessingActive() && |
peah | de65ddc | 2016-09-16 15:02:15 -0700 | [diff] [blame] | 1517 | SampleRateSupportsMultiBand( |
| 1518 | formats_.render_processing_format.sample_rate_hz())) { |
| 1519 | render_buffer->MergeFrequencyBands(); |
ekmeyerson | 60d9b33 | 2015-08-14 10:35:55 -0700 | [diff] [blame] | 1520 | } |
| 1521 | |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 1522 | return kNoError; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1523 | } |
| 1524 | |
| 1525 | int AudioProcessingImpl::set_stream_delay_ms(int delay) { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1526 | rtc::CritScope cs(&crit_capture_); |
andrew@webrtc.org | 5f23d64 | 2012-05-29 21:14:06 +0000 | [diff] [blame] | 1527 | Error retval = kNoError; |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1528 | capture_.was_stream_delay_set = true; |
| 1529 | delay += capture_.delay_offset_ms; |
andrew@webrtc.org | 6f9f817 | 2012-03-06 19:03:39 +0000 | [diff] [blame] | 1530 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1531 | if (delay < 0) { |
andrew@webrtc.org | 5f23d64 | 2012-05-29 21:14:06 +0000 | [diff] [blame] | 1532 | delay = 0; |
| 1533 | retval = kBadStreamParameterWarning; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1534 | } |
| 1535 | |
| 1536 | // TODO(ajm): the max is rather arbitrarily chosen; investigate. |
| 1537 | if (delay > 500) { |
andrew@webrtc.org | 5f23d64 | 2012-05-29 21:14:06 +0000 | [diff] [blame] | 1538 | delay = 500; |
| 1539 | retval = kBadStreamParameterWarning; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1540 | } |
| 1541 | |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1542 | capture_nonlocked_.stream_delay_ms = delay; |
andrew@webrtc.org | 5f23d64 | 2012-05-29 21:14:06 +0000 | [diff] [blame] | 1543 | return retval; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1544 | } |
| 1545 | |
| 1546 | int AudioProcessingImpl::stream_delay_ms() const { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1547 | // Used as callback from submodules, hence locking is not allowed. |
| 1548 | return capture_nonlocked_.stream_delay_ms; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1549 | } |
| 1550 | |
| 1551 | bool AudioProcessingImpl::was_stream_delay_set() const { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1552 | // Used as callback from submodules, hence locking is not allowed. |
| 1553 | return capture_.was_stream_delay_set; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1554 | } |
| 1555 | |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 1556 | void AudioProcessingImpl::set_stream_key_pressed(bool key_pressed) { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1557 | rtc::CritScope cs(&crit_capture_); |
| 1558 | capture_.key_pressed = key_pressed; |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 1559 | } |
| 1560 | |
andrew@webrtc.org | 6f9f817 | 2012-03-06 19:03:39 +0000 | [diff] [blame] | 1561 | void AudioProcessingImpl::set_delay_offset_ms(int offset) { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1562 | rtc::CritScope cs(&crit_capture_); |
| 1563 | capture_.delay_offset_ms = offset; |
andrew@webrtc.org | 6f9f817 | 2012-03-06 19:03:39 +0000 | [diff] [blame] | 1564 | } |
| 1565 | |
| 1566 | int AudioProcessingImpl::delay_offset_ms() const { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1567 | rtc::CritScope cs(&crit_capture_); |
| 1568 | return capture_.delay_offset_ms; |
andrew@webrtc.org | 6f9f817 | 2012-03-06 19:03:39 +0000 | [diff] [blame] | 1569 | } |
| 1570 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1571 | int AudioProcessingImpl::StartDebugRecording( |
ivoc | d66b44d | 2016-01-15 03:06:36 -0800 | [diff] [blame] | 1572 | const char filename[AudioProcessing::kMaxFilenameSize], |
| 1573 | int64_t max_log_size_bytes) { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1574 | // Run in a single-threaded manner. |
| 1575 | rtc::CritScope cs_render(&crit_render_); |
| 1576 | rtc::CritScope cs_capture(&crit_capture_); |
André Susano Pinto | 664cdaf | 2015-05-20 11:11:07 +0200 | [diff] [blame] | 1577 | static_assert(kMaxFilenameSize == FileWrapper::kMaxFileNameSize, ""); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1578 | |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1579 | if (filename == nullptr) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1580 | return kNullPointerError; |
| 1581 | } |
| 1582 | |
andrew@webrtc.org | 7bf2646 | 2011-12-03 00:03:31 +0000 | [diff] [blame] | 1583 | #ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP |
ivoc | d66b44d | 2016-01-15 03:06:36 -0800 | [diff] [blame] | 1584 | debug_dump_.num_bytes_left_for_log_ = max_log_size_bytes; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1585 | // Stop any ongoing recording. |
tommi | a6219cc | 2016-06-15 10:30:14 -0700 | [diff] [blame] | 1586 | debug_dump_.debug_file->CloseFile(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1587 | |
tommi | a6219cc | 2016-06-15 10:30:14 -0700 | [diff] [blame] | 1588 | if (!debug_dump_.debug_file->OpenFile(filename, false)) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1589 | return kFileError; |
| 1590 | } |
| 1591 | |
Minyue | 13b96ba | 2015-10-03 00:39:14 +0200 | [diff] [blame] | 1592 | RETURN_ON_ERR(WriteConfigMessage(true)); |
| 1593 | RETURN_ON_ERR(WriteInitMessage()); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1594 | return kNoError; |
andrew@webrtc.org | 7bf2646 | 2011-12-03 00:03:31 +0000 | [diff] [blame] | 1595 | #else |
| 1596 | return kUnsupportedFunctionError; |
| 1597 | #endif // WEBRTC_AUDIOPROC_DEBUG_DUMP |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1598 | } |
| 1599 | |
ivoc | d66b44d | 2016-01-15 03:06:36 -0800 | [diff] [blame] | 1600 | int AudioProcessingImpl::StartDebugRecording(FILE* handle, |
| 1601 | int64_t max_log_size_bytes) { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1602 | // Run in a single-threaded manner. |
| 1603 | rtc::CritScope cs_render(&crit_render_); |
| 1604 | rtc::CritScope cs_capture(&crit_capture_); |
henrikg@webrtc.org | 863b536 | 2013-12-06 16:05:17 +0000 | [diff] [blame] | 1605 | |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1606 | if (handle == nullptr) { |
henrikg@webrtc.org | 863b536 | 2013-12-06 16:05:17 +0000 | [diff] [blame] | 1607 | return kNullPointerError; |
| 1608 | } |
| 1609 | |
| 1610 | #ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP |
ivoc | d66b44d | 2016-01-15 03:06:36 -0800 | [diff] [blame] | 1611 | debug_dump_.num_bytes_left_for_log_ = max_log_size_bytes; |
| 1612 | |
henrikg@webrtc.org | 863b536 | 2013-12-06 16:05:17 +0000 | [diff] [blame] | 1613 | // Stop any ongoing recording. |
tommi | a6219cc | 2016-06-15 10:30:14 -0700 | [diff] [blame] | 1614 | debug_dump_.debug_file->CloseFile(); |
henrikg@webrtc.org | 863b536 | 2013-12-06 16:05:17 +0000 | [diff] [blame] | 1615 | |
tommi | a6219cc | 2016-06-15 10:30:14 -0700 | [diff] [blame] | 1616 | if (!debug_dump_.debug_file->OpenFromFileHandle(handle)) { |
henrikg@webrtc.org | 863b536 | 2013-12-06 16:05:17 +0000 | [diff] [blame] | 1617 | return kFileError; |
| 1618 | } |
| 1619 | |
Minyue | 13b96ba | 2015-10-03 00:39:14 +0200 | [diff] [blame] | 1620 | RETURN_ON_ERR(WriteConfigMessage(true)); |
| 1621 | RETURN_ON_ERR(WriteInitMessage()); |
henrikg@webrtc.org | 863b536 | 2013-12-06 16:05:17 +0000 | [diff] [blame] | 1622 | return kNoError; |
| 1623 | #else |
| 1624 | return kUnsupportedFunctionError; |
| 1625 | #endif // WEBRTC_AUDIOPROC_DEBUG_DUMP |
| 1626 | } |
| 1627 | |
peah | 73a28ee | 2016-10-12 03:01:49 -0700 | [diff] [blame] | 1628 | int AudioProcessingImpl::StartDebugRecording(FILE* handle) { |
| 1629 | return StartDebugRecording(handle, -1); |
| 1630 | } |
| 1631 | |
xians@webrtc.org | e46bc77 | 2014-10-10 08:36:56 +0000 | [diff] [blame] | 1632 | int AudioProcessingImpl::StartDebugRecordingForPlatformFile( |
| 1633 | rtc::PlatformFile handle) { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1634 | // Run in a single-threaded manner. |
| 1635 | rtc::CritScope cs_render(&crit_render_); |
| 1636 | rtc::CritScope cs_capture(&crit_capture_); |
xians@webrtc.org | e46bc77 | 2014-10-10 08:36:56 +0000 | [diff] [blame] | 1637 | FILE* stream = rtc::FdopenPlatformFileForWriting(handle); |
ivoc | d66b44d | 2016-01-15 03:06:36 -0800 | [diff] [blame] | 1638 | return StartDebugRecording(stream, -1); |
xians@webrtc.org | e46bc77 | 2014-10-10 08:36:56 +0000 | [diff] [blame] | 1639 | } |
| 1640 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1641 | int AudioProcessingImpl::StopDebugRecording() { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1642 | // Run in a single-threaded manner. |
| 1643 | rtc::CritScope cs_render(&crit_render_); |
| 1644 | rtc::CritScope cs_capture(&crit_capture_); |
andrew@webrtc.org | 7bf2646 | 2011-12-03 00:03:31 +0000 | [diff] [blame] | 1645 | |
| 1646 | #ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1647 | // We just return if recording hasn't started. |
tommi | a6219cc | 2016-06-15 10:30:14 -0700 | [diff] [blame] | 1648 | debug_dump_.debug_file->CloseFile(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1649 | return kNoError; |
andrew@webrtc.org | 7bf2646 | 2011-12-03 00:03:31 +0000 | [diff] [blame] | 1650 | #else |
| 1651 | return kUnsupportedFunctionError; |
| 1652 | #endif // WEBRTC_AUDIOPROC_DEBUG_DUMP |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1653 | } |
| 1654 | |
ivoc | 4e477a1 | 2017-01-15 08:29:46 -0800 | [diff] [blame] | 1655 | AudioProcessing::AudioProcessingStatistics::AudioProcessingStatistics() { |
| 1656 | residual_echo_return_loss.Set(-100.0f, -100.0f, -100.0f, -100.0f); |
| 1657 | echo_return_loss.Set(-100.0f, -100.0f, -100.0f, -100.0f); |
| 1658 | echo_return_loss_enhancement.Set(-100.0f, -100.0f, -100.0f, -100.0f); |
| 1659 | a_nlp.Set(-100.0f, -100.0f, -100.0f, -100.0f); |
| 1660 | } |
| 1661 | |
| 1662 | AudioProcessing::AudioProcessingStatistics::AudioProcessingStatistics( |
| 1663 | const AudioProcessingStatistics& other) = default; |
| 1664 | |
| 1665 | AudioProcessing::AudioProcessingStatistics::~AudioProcessingStatistics() = |
| 1666 | default; |
| 1667 | |
ivoc | 3e9a537 | 2016-10-28 07:55:33 -0700 | [diff] [blame] | 1668 | // TODO(ivoc): Remove this when GetStatistics() becomes pure virtual. |
| 1669 | AudioProcessing::AudioProcessingStatistics AudioProcessing::GetStatistics() |
| 1670 | const { |
| 1671 | return AudioProcessingStatistics(); |
| 1672 | } |
| 1673 | |
| 1674 | AudioProcessing::AudioProcessingStatistics AudioProcessingImpl::GetStatistics() |
| 1675 | const { |
| 1676 | AudioProcessingStatistics stats; |
| 1677 | EchoCancellation::Metrics metrics; |
ivoc | d0a151c | 2016-11-02 09:14:37 -0700 | [diff] [blame] | 1678 | int success = public_submodules_->echo_cancellation->GetMetrics(&metrics); |
| 1679 | if (success == Error::kNoError) { |
| 1680 | stats.a_nlp.Set(metrics.a_nlp); |
| 1681 | stats.divergent_filter_fraction = metrics.divergent_filter_fraction; |
| 1682 | stats.echo_return_loss.Set(metrics.echo_return_loss); |
| 1683 | stats.echo_return_loss_enhancement.Set( |
| 1684 | metrics.echo_return_loss_enhancement); |
| 1685 | stats.residual_echo_return_loss.Set(metrics.residual_echo_return_loss); |
| 1686 | } |
ivoc | 9c192b2 | 2017-03-16 04:22:14 -0700 | [diff] [blame] | 1687 | { |
| 1688 | rtc::CritScope cs_capture(&crit_capture_); |
| 1689 | stats.residual_echo_likelihood = |
| 1690 | private_submodules_->residual_echo_detector->echo_likelihood(); |
| 1691 | stats.residual_echo_likelihood_recent_max = |
| 1692 | private_submodules_->residual_echo_detector |
| 1693 | ->echo_likelihood_recent_max(); |
| 1694 | } |
ivoc | 3e9a537 | 2016-10-28 07:55:33 -0700 | [diff] [blame] | 1695 | public_submodules_->echo_cancellation->GetDelayMetrics( |
| 1696 | &stats.delay_median, &stats.delay_standard_deviation, |
| 1697 | &stats.fraction_poor_delays); |
| 1698 | return stats; |
| 1699 | } |
| 1700 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1701 | EchoCancellation* AudioProcessingImpl::echo_cancellation() const { |
peah | b624d8c | 2016-03-05 03:01:14 -0800 | [diff] [blame] | 1702 | return public_submodules_->echo_cancellation.get(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1703 | } |
| 1704 | |
| 1705 | EchoControlMobile* AudioProcessingImpl::echo_control_mobile() const { |
peah | bb9edbd | 2016-03-10 12:54:25 -0800 | [diff] [blame] | 1706 | return public_submodules_->echo_control_mobile.get(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1707 | } |
| 1708 | |
| 1709 | GainControl* AudioProcessingImpl::gain_control() const { |
peah | be61562 | 2016-02-13 16:40:47 -0800 | [diff] [blame] | 1710 | if (constants_.use_experimental_agc) { |
| 1711 | return public_submodules_->gain_control_for_experimental_agc.get(); |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 1712 | } |
peah | bfa9711 | 2016-03-10 21:09:04 -0800 | [diff] [blame] | 1713 | return public_submodules_->gain_control.get(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1714 | } |
| 1715 | |
| 1716 | HighPassFilter* AudioProcessingImpl::high_pass_filter() const { |
peah | 8271d04 | 2016-11-22 07:24:52 -0800 | [diff] [blame] | 1717 | return high_pass_filter_impl_.get(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1718 | } |
| 1719 | |
| 1720 | LevelEstimator* AudioProcessingImpl::level_estimator() const { |
solenberg | 949028f | 2015-12-15 11:39:38 -0800 | [diff] [blame] | 1721 | return public_submodules_->level_estimator.get(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1722 | } |
| 1723 | |
| 1724 | NoiseSuppression* AudioProcessingImpl::noise_suppression() const { |
solenberg | 5e465c3 | 2015-12-08 13:22:33 -0800 | [diff] [blame] | 1725 | return public_submodules_->noise_suppression.get(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1726 | } |
| 1727 | |
| 1728 | VoiceDetection* AudioProcessingImpl::voice_detection() const { |
solenberg | a29386c | 2015-12-16 03:31:12 -0800 | [diff] [blame] | 1729 | return public_submodules_->voice_detection.get(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1730 | } |
| 1731 | |
peah | 8271d04 | 2016-11-22 07:24:52 -0800 | [diff] [blame] | 1732 | void AudioProcessingImpl::MutateConfig( |
| 1733 | rtc::FunctionView<void(AudioProcessing::Config*)> mutator) { |
| 1734 | rtc::CritScope cs_render(&crit_render_); |
| 1735 | rtc::CritScope cs_capture(&crit_capture_); |
| 1736 | mutator(&config_); |
| 1737 | ApplyConfig(config_); |
| 1738 | } |
| 1739 | |
| 1740 | AudioProcessing::Config AudioProcessingImpl::GetConfig() const { |
| 1741 | rtc::CritScope cs_render(&crit_render_); |
| 1742 | rtc::CritScope cs_capture(&crit_capture_); |
| 1743 | return config_; |
| 1744 | } |
| 1745 | |
peah | 2ace3f9 | 2016-09-10 04:42:27 -0700 | [diff] [blame] | 1746 | bool AudioProcessingImpl::UpdateActiveSubmoduleStates() { |
| 1747 | return submodule_states_.Update( |
peah | 8271d04 | 2016-11-22 07:24:52 -0800 | [diff] [blame] | 1748 | config_.high_pass_filter.enabled, |
peah | 2ace3f9 | 2016-09-10 04:42:27 -0700 | [diff] [blame] | 1749 | public_submodules_->echo_cancellation->is_enabled(), |
| 1750 | public_submodules_->echo_control_mobile->is_enabled(), |
ivoc | 9f4a4a0 | 2016-10-28 05:39:16 -0700 | [diff] [blame] | 1751 | config_.residual_echo_detector.enabled, |
peah | 2ace3f9 | 2016-09-10 04:42:27 -0700 | [diff] [blame] | 1752 | public_submodules_->noise_suppression->is_enabled(), |
| 1753 | capture_nonlocked_.intelligibility_enabled, |
| 1754 | capture_nonlocked_.beamformer_enabled, |
| 1755 | public_submodules_->gain_control->is_enabled(), |
alessiob | 3ec96df | 2017-05-22 06:57:06 -0700 | [diff] [blame] | 1756 | capture_nonlocked_.gain_controller2_enabled, |
peah | 2ace3f9 | 2016-09-10 04:42:27 -0700 | [diff] [blame] | 1757 | capture_nonlocked_.level_controller_enabled, |
peah | e0eae3c | 2016-12-14 01:16:23 -0800 | [diff] [blame] | 1758 | capture_nonlocked_.echo_canceller3_enabled, |
peah | 2ace3f9 | 2016-09-10 04:42:27 -0700 | [diff] [blame] | 1759 | public_submodules_->voice_detection->is_enabled(), |
| 1760 | public_submodules_->level_estimator->is_enabled(), |
| 1761 | capture_.transient_suppressor_enabled); |
ekmeyerson | 60d9b33 | 2015-08-14 10:35:55 -0700 | [diff] [blame] | 1762 | } |
| 1763 | |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 1764 | |
Bjorn Volcker | adc46c4 | 2015-04-15 11:42:40 +0200 | [diff] [blame] | 1765 | void AudioProcessingImpl::InitializeTransient() { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1766 | if (capture_.transient_suppressor_enabled) { |
| 1767 | if (!public_submodules_->transient_suppressor.get()) { |
| 1768 | public_submodules_->transient_suppressor.reset(new TransientSuppressor()); |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 1769 | } |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1770 | public_submodules_->transient_suppressor->Initialize( |
peah | de65ddc | 2016-09-16 15:02:15 -0700 | [diff] [blame] | 1771 | capture_nonlocked_.capture_processing_format.sample_rate_hz(), |
| 1772 | capture_nonlocked_.split_rate, num_proc_channels()); |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 1773 | } |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 1774 | } |
| 1775 | |
aluebs@webrtc.org | ae643ce | 2014-12-19 19:57:34 +0000 | [diff] [blame] | 1776 | void AudioProcessingImpl::InitializeBeamformer() { |
aluebs | b2328d1 | 2016-01-11 20:32:29 -0800 | [diff] [blame] | 1777 | if (capture_nonlocked_.beamformer_enabled) { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1778 | if (!private_submodules_->beamformer) { |
| 1779 | private_submodules_->beamformer.reset(new NonlinearBeamformer( |
Alejandro Luebs | f4022ff | 2016-07-01 17:19:09 -0700 | [diff] [blame] | 1780 | capture_.array_geometry, 1u, capture_.target_direction)); |
aluebs@webrtc.org | d82f55d | 2015-01-15 18:07:21 +0000 | [diff] [blame] | 1781 | } |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1782 | private_submodules_->beamformer->Initialize(kChunkSizeMs, |
| 1783 | capture_nonlocked_.split_rate); |
aluebs@webrtc.org | ae643ce | 2014-12-19 19:57:34 +0000 | [diff] [blame] | 1784 | } |
| 1785 | } |
| 1786 | |
ekmeyerson | 60d9b33 | 2015-08-14 10:35:55 -0700 | [diff] [blame] | 1787 | void AudioProcessingImpl::InitializeIntelligibility() { |
peah | 1bcfce5 | 2016-08-26 07:16:04 -0700 | [diff] [blame] | 1788 | #if WEBRTC_INTELLIGIBILITY_ENHANCER |
Alejandro Luebs | c9b0c26 | 2016-05-16 15:32:38 -0700 | [diff] [blame] | 1789 | if (capture_nonlocked_.intelligibility_enabled) { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1790 | public_submodules_->intelligibility_enhancer.reset( |
Alejandro Luebs | 18fcbcf | 2016-02-22 15:57:38 -0800 | [diff] [blame] | 1791 | new IntelligibilityEnhancer(capture_nonlocked_.split_rate, |
Alex Luebs | 57ae829 | 2016-03-09 16:24:34 +0100 | [diff] [blame] | 1792 | render_.render_audio->num_channels(), |
Alejandro Luebs | ef00925 | 2016-09-20 14:51:56 -0700 | [diff] [blame] | 1793 | render_.render_audio->num_bands(), |
Alex Luebs | 57ae829 | 2016-03-09 16:24:34 +0100 | [diff] [blame] | 1794 | NoiseSuppressionImpl::num_noise_bins())); |
ekmeyerson | 60d9b33 | 2015-08-14 10:35:55 -0700 | [diff] [blame] | 1795 | } |
peah | 1bcfce5 | 2016-08-26 07:16:04 -0700 | [diff] [blame] | 1796 | #endif |
ekmeyerson | 60d9b33 | 2015-08-14 10:35:55 -0700 | [diff] [blame] | 1797 | } |
| 1798 | |
peah | 8271d04 | 2016-11-22 07:24:52 -0800 | [diff] [blame] | 1799 | void AudioProcessingImpl::InitializeLowCutFilter() { |
| 1800 | if (config_.high_pass_filter.enabled) { |
| 1801 | private_submodules_->low_cut_filter.reset( |
| 1802 | new LowCutFilter(num_proc_channels(), proc_sample_rate_hz())); |
| 1803 | } else { |
| 1804 | private_submodules_->low_cut_filter.reset(); |
| 1805 | } |
| 1806 | } |
alessiob | 3ec96df | 2017-05-22 06:57:06 -0700 | [diff] [blame] | 1807 | |
peah | e0eae3c | 2016-12-14 01:16:23 -0800 | [diff] [blame] | 1808 | void AudioProcessingImpl::InitializeEchoCanceller3() { |
| 1809 | if (capture_nonlocked_.echo_canceller3_enabled) { |
| 1810 | private_submodules_->echo_canceller3.reset( |
| 1811 | new EchoCanceller3(proc_sample_rate_hz(), true)); |
| 1812 | } else { |
| 1813 | private_submodules_->echo_canceller3.reset(); |
| 1814 | } |
| 1815 | } |
peah | 8271d04 | 2016-11-22 07:24:52 -0800 | [diff] [blame] | 1816 | |
alessiob | 3ec96df | 2017-05-22 06:57:06 -0700 | [diff] [blame] | 1817 | void AudioProcessingImpl::InitializeGainController2() { |
| 1818 | if (capture_nonlocked_.gain_controller2_enabled) { |
| 1819 | private_submodules_->gain_controller2.reset( |
| 1820 | new GainController2(proc_sample_rate_hz())); |
| 1821 | } else { |
| 1822 | private_submodules_->gain_controller2.reset(); |
| 1823 | } |
| 1824 | } |
| 1825 | |
peah | ca4cac7 | 2016-06-29 15:26:12 -0700 | [diff] [blame] | 1826 | void AudioProcessingImpl::InitializeLevelController() { |
| 1827 | private_submodules_->level_controller->Initialize(proc_sample_rate_hz()); |
| 1828 | } |
| 1829 | |
ivoc | 9f4a4a0 | 2016-10-28 05:39:16 -0700 | [diff] [blame] | 1830 | void AudioProcessingImpl::InitializeResidualEchoDetector() { |
| 1831 | private_submodules_->residual_echo_detector->Initialize(); |
| 1832 | } |
| 1833 | |
Bjorn Volcker | 1ca324f | 2015-06-29 14:57:29 +0200 | [diff] [blame] | 1834 | void AudioProcessingImpl::MaybeUpdateHistograms() { |
Bjorn Volcker | d92f267 | 2015-07-05 10:46:01 +0200 | [diff] [blame] | 1835 | static const int kMinDiffDelayMs = 60; |
Bjorn Volcker | 1ca324f | 2015-06-29 14:57:29 +0200 | [diff] [blame] | 1836 | |
| 1837 | if (echo_cancellation()->is_enabled()) { |
Bjorn Volcker | 4e7aa43 | 2015-07-07 11:50:05 +0200 | [diff] [blame] | 1838 | // Activate delay_jumps_ counters if we know echo_cancellation is runnning. |
| 1839 | // If a stream has echo we know that the echo_cancellation is in process. |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1840 | if (capture_.stream_delay_jumps == -1 && |
Bjorn Volcker | 4e7aa43 | 2015-07-07 11:50:05 +0200 | [diff] [blame] | 1841 | echo_cancellation()->stream_has_echo()) { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1842 | capture_.stream_delay_jumps = 0; |
| 1843 | } |
| 1844 | if (capture_.aec_system_delay_jumps == -1 && |
| 1845 | echo_cancellation()->stream_has_echo()) { |
| 1846 | capture_.aec_system_delay_jumps = 0; |
Bjorn Volcker | 4e7aa43 | 2015-07-07 11:50:05 +0200 | [diff] [blame] | 1847 | } |
| 1848 | |
Bjorn Volcker | 1ca324f | 2015-06-29 14:57:29 +0200 | [diff] [blame] | 1849 | // Detect a jump in platform reported system delay and log the difference. |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1850 | const int diff_stream_delay_ms = |
| 1851 | capture_nonlocked_.stream_delay_ms - capture_.last_stream_delay_ms; |
| 1852 | if (diff_stream_delay_ms > kMinDiffDelayMs && |
| 1853 | capture_.last_stream_delay_ms != 0) { |
asapersson | a2c58e2 | 2016-03-07 01:52:59 -0800 | [diff] [blame] | 1854 | RTC_HISTOGRAM_COUNTS("WebRTC.Audio.PlatformReportedStreamDelayJump", |
| 1855 | diff_stream_delay_ms, kMinDiffDelayMs, 1000, 100); |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1856 | if (capture_.stream_delay_jumps == -1) { |
| 1857 | capture_.stream_delay_jumps = 0; // Activate counter if needed. |
Bjorn Volcker | 4e7aa43 | 2015-07-07 11:50:05 +0200 | [diff] [blame] | 1858 | } |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1859 | capture_.stream_delay_jumps++; |
Bjorn Volcker | 1ca324f | 2015-06-29 14:57:29 +0200 | [diff] [blame] | 1860 | } |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1861 | capture_.last_stream_delay_ms = capture_nonlocked_.stream_delay_ms; |
Bjorn Volcker | 1ca324f | 2015-06-29 14:57:29 +0200 | [diff] [blame] | 1862 | |
| 1863 | // Detect a jump in AEC system delay and log the difference. |
peah | 20028c4 | 2016-03-04 11:50:54 -0800 | [diff] [blame] | 1864 | const int samples_per_ms = |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1865 | rtc::CheckedDivExact(capture_nonlocked_.split_rate, 1000); |
peah | 20028c4 | 2016-03-04 11:50:54 -0800 | [diff] [blame] | 1866 | RTC_DCHECK_LT(0, samples_per_ms); |
Bjorn Volcker | 1ca324f | 2015-06-29 14:57:29 +0200 | [diff] [blame] | 1867 | const int aec_system_delay_ms = |
peah | 20028c4 | 2016-03-04 11:50:54 -0800 | [diff] [blame] | 1868 | public_submodules_->echo_cancellation->GetSystemDelayInSamples() / |
| 1869 | samples_per_ms; |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 1870 | const int diff_aec_system_delay_ms = |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1871 | aec_system_delay_ms - capture_.last_aec_system_delay_ms; |
Bjorn Volcker | 1ca324f | 2015-06-29 14:57:29 +0200 | [diff] [blame] | 1872 | if (diff_aec_system_delay_ms > kMinDiffDelayMs && |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1873 | capture_.last_aec_system_delay_ms != 0) { |
asapersson | a2c58e2 | 2016-03-07 01:52:59 -0800 | [diff] [blame] | 1874 | RTC_HISTOGRAM_COUNTS("WebRTC.Audio.AecSystemDelayJump", |
| 1875 | diff_aec_system_delay_ms, kMinDiffDelayMs, 1000, |
| 1876 | 100); |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1877 | if (capture_.aec_system_delay_jumps == -1) { |
| 1878 | capture_.aec_system_delay_jumps = 0; // Activate counter if needed. |
Bjorn Volcker | 4e7aa43 | 2015-07-07 11:50:05 +0200 | [diff] [blame] | 1879 | } |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1880 | capture_.aec_system_delay_jumps++; |
Bjorn Volcker | 1ca324f | 2015-06-29 14:57:29 +0200 | [diff] [blame] | 1881 | } |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1882 | capture_.last_aec_system_delay_ms = aec_system_delay_ms; |
Bjorn Volcker | 1ca324f | 2015-06-29 14:57:29 +0200 | [diff] [blame] | 1883 | } |
| 1884 | } |
| 1885 | |
Bjorn Volcker | 4e7aa43 | 2015-07-07 11:50:05 +0200 | [diff] [blame] | 1886 | void AudioProcessingImpl::UpdateHistogramsOnCallEnd() { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1887 | // Run in a single-threaded manner. |
| 1888 | rtc::CritScope cs_render(&crit_render_); |
| 1889 | rtc::CritScope cs_capture(&crit_capture_); |
| 1890 | |
| 1891 | if (capture_.stream_delay_jumps > -1) { |
asapersson | a2c58e2 | 2016-03-07 01:52:59 -0800 | [diff] [blame] | 1892 | RTC_HISTOGRAM_ENUMERATION( |
Bjorn Volcker | 4e7aa43 | 2015-07-07 11:50:05 +0200 | [diff] [blame] | 1893 | "WebRTC.Audio.NumOfPlatformReportedStreamDelayJumps", |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1894 | capture_.stream_delay_jumps, 51); |
Bjorn Volcker | 4e7aa43 | 2015-07-07 11:50:05 +0200 | [diff] [blame] | 1895 | } |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1896 | capture_.stream_delay_jumps = -1; |
| 1897 | capture_.last_stream_delay_ms = 0; |
Bjorn Volcker | 4e7aa43 | 2015-07-07 11:50:05 +0200 | [diff] [blame] | 1898 | |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1899 | if (capture_.aec_system_delay_jumps > -1) { |
asapersson | a2c58e2 | 2016-03-07 01:52:59 -0800 | [diff] [blame] | 1900 | RTC_HISTOGRAM_ENUMERATION("WebRTC.Audio.NumOfAecSystemDelayJumps", |
| 1901 | capture_.aec_system_delay_jumps, 51); |
Bjorn Volcker | 4e7aa43 | 2015-07-07 11:50:05 +0200 | [diff] [blame] | 1902 | } |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1903 | capture_.aec_system_delay_jumps = -1; |
| 1904 | capture_.last_aec_system_delay_ms = 0; |
Bjorn Volcker | 4e7aa43 | 2015-07-07 11:50:05 +0200 | [diff] [blame] | 1905 | } |
| 1906 | |
andrew@webrtc.org | 7bf2646 | 2011-12-03 00:03:31 +0000 | [diff] [blame] | 1907 | #ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1908 | int AudioProcessingImpl::WriteMessageToDebugFile( |
| 1909 | FileWrapper* debug_file, |
ivoc | d66b44d | 2016-01-15 03:06:36 -0800 | [diff] [blame] | 1910 | int64_t* filesize_limit_bytes, |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1911 | rtc::CriticalSection* crit_debug, |
| 1912 | ApmDebugDumpThreadState* debug_state) { |
| 1913 | int32_t size = debug_state->event_msg->ByteSize(); |
ajm@google.com | 808e0e0 | 2011-08-03 21:08:51 +0000 | [diff] [blame] | 1914 | if (size <= 0) { |
| 1915 | return kUnspecifiedError; |
| 1916 | } |
andrew@webrtc.org | 621df67 | 2013-10-22 10:27:23 +0000 | [diff] [blame] | 1917 | #if defined(WEBRTC_ARCH_BIG_ENDIAN) |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 1918 | // TODO(ajm): Use little-endian "on the wire". For the moment, we can be |
| 1919 | // pretty safe in assuming little-endian. |
ajm@google.com | 808e0e0 | 2011-08-03 21:08:51 +0000 | [diff] [blame] | 1920 | #endif |
| 1921 | |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1922 | if (!debug_state->event_msg->SerializeToString(&debug_state->event_str)) { |
ajm@google.com | 808e0e0 | 2011-08-03 21:08:51 +0000 | [diff] [blame] | 1923 | return kUnspecifiedError; |
| 1924 | } |
| 1925 | |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1926 | { |
| 1927 | // Ensure atomic writes of the message. |
ivoc | d66b44d | 2016-01-15 03:06:36 -0800 | [diff] [blame] | 1928 | rtc::CritScope cs_debug(crit_debug); |
| 1929 | |
tommi | a6219cc | 2016-06-15 10:30:14 -0700 | [diff] [blame] | 1930 | RTC_DCHECK(debug_file->is_open()); |
ivoc | d66b44d | 2016-01-15 03:06:36 -0800 | [diff] [blame] | 1931 | // Update the byte counter. |
| 1932 | if (*filesize_limit_bytes >= 0) { |
| 1933 | *filesize_limit_bytes -= |
| 1934 | (sizeof(int32_t) + debug_state->event_str.length()); |
| 1935 | if (*filesize_limit_bytes < 0) { |
| 1936 | // Not enough bytes are left to write this message, so stop logging. |
| 1937 | debug_file->CloseFile(); |
| 1938 | return kNoError; |
| 1939 | } |
| 1940 | } |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1941 | // Write message preceded by its size. |
| 1942 | if (!debug_file->Write(&size, sizeof(int32_t))) { |
| 1943 | return kFileError; |
| 1944 | } |
| 1945 | if (!debug_file->Write(debug_state->event_str.data(), |
| 1946 | debug_state->event_str.length())) { |
| 1947 | return kFileError; |
| 1948 | } |
ajm@google.com | 808e0e0 | 2011-08-03 21:08:51 +0000 | [diff] [blame] | 1949 | } |
| 1950 | |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1951 | debug_state->event_msg->Clear(); |
ajm@google.com | 808e0e0 | 2011-08-03 21:08:51 +0000 | [diff] [blame] | 1952 | |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 1953 | return kNoError; |
ajm@google.com | 808e0e0 | 2011-08-03 21:08:51 +0000 | [diff] [blame] | 1954 | } |
| 1955 | |
| 1956 | int AudioProcessingImpl::WriteInitMessage() { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1957 | debug_dump_.capture.event_msg->set_type(audioproc::Event::INIT); |
| 1958 | audioproc::Init* msg = debug_dump_.capture.event_msg->mutable_init(); |
| 1959 | msg->set_sample_rate(formats_.api_format.input_stream().sample_rate_hz()); |
ajm@google.com | 808e0e0 | 2011-08-03 21:08:51 +0000 | [diff] [blame] | 1960 | |
mbonadei | 7c2c843 | 2017-04-07 00:59:12 -0700 | [diff] [blame] | 1961 | msg->set_num_input_channels(static_cast<int32_t>( |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 1962 | formats_.api_format.input_stream().num_channels())); |
mbonadei | 7c2c843 | 2017-04-07 00:59:12 -0700 | [diff] [blame] | 1963 | msg->set_num_output_channels(static_cast<int32_t>( |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 1964 | formats_.api_format.output_stream().num_channels())); |
mbonadei | 7c2c843 | 2017-04-07 00:59:12 -0700 | [diff] [blame] | 1965 | msg->set_num_reverse_channels(static_cast<int32_t>( |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 1966 | formats_.api_format.reverse_input_stream().num_channels())); |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1967 | msg->set_reverse_sample_rate( |
| 1968 | formats_.api_format.reverse_input_stream().sample_rate_hz()); |
| 1969 | msg->set_output_sample_rate( |
| 1970 | formats_.api_format.output_stream().sample_rate_hz()); |
peah | c7bdf8a | 2016-04-11 07:05:53 -0700 | [diff] [blame] | 1971 | msg->set_reverse_output_sample_rate( |
| 1972 | formats_.api_format.reverse_output_stream().sample_rate_hz()); |
| 1973 | msg->set_num_reverse_output_channels( |
| 1974 | formats_.api_format.reverse_output_stream().num_channels()); |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1975 | |
| 1976 | RETURN_ON_ERR(WriteMessageToDebugFile(debug_dump_.debug_file.get(), |
ivoc | d66b44d | 2016-01-15 03:06:36 -0800 | [diff] [blame] | 1977 | &debug_dump_.num_bytes_left_for_log_, |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1978 | &crit_debug_, &debug_dump_.capture)); |
Minyue | 13b96ba | 2015-10-03 00:39:14 +0200 | [diff] [blame] | 1979 | return kNoError; |
| 1980 | } |
| 1981 | |
| 1982 | int AudioProcessingImpl::WriteConfigMessage(bool forced) { |
| 1983 | audioproc::Config config; |
| 1984 | |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1985 | config.set_aec_enabled(public_submodules_->echo_cancellation->is_enabled()); |
Minyue | 13b96ba | 2015-10-03 00:39:14 +0200 | [diff] [blame] | 1986 | config.set_aec_delay_agnostic_enabled( |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1987 | public_submodules_->echo_cancellation->is_delay_agnostic_enabled()); |
Minyue | 13b96ba | 2015-10-03 00:39:14 +0200 | [diff] [blame] | 1988 | config.set_aec_drift_compensation_enabled( |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1989 | public_submodules_->echo_cancellation->is_drift_compensation_enabled()); |
Minyue | 13b96ba | 2015-10-03 00:39:14 +0200 | [diff] [blame] | 1990 | config.set_aec_extended_filter_enabled( |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1991 | public_submodules_->echo_cancellation->is_extended_filter_enabled()); |
| 1992 | config.set_aec_suppression_level(static_cast<int>( |
| 1993 | public_submodules_->echo_cancellation->suppression_level())); |
Minyue | 13b96ba | 2015-10-03 00:39:14 +0200 | [diff] [blame] | 1994 | |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1995 | config.set_aecm_enabled( |
| 1996 | public_submodules_->echo_control_mobile->is_enabled()); |
Minyue | 13b96ba | 2015-10-03 00:39:14 +0200 | [diff] [blame] | 1997 | config.set_aecm_comfort_noise_enabled( |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 1998 | public_submodules_->echo_control_mobile->is_comfort_noise_enabled()); |
| 1999 | config.set_aecm_routing_mode(static_cast<int>( |
| 2000 | public_submodules_->echo_control_mobile->routing_mode())); |
Minyue | 13b96ba | 2015-10-03 00:39:14 +0200 | [diff] [blame] | 2001 | |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 2002 | config.set_agc_enabled(public_submodules_->gain_control->is_enabled()); |
| 2003 | config.set_agc_mode( |
| 2004 | static_cast<int>(public_submodules_->gain_control->mode())); |
| 2005 | config.set_agc_limiter_enabled( |
| 2006 | public_submodules_->gain_control->is_limiter_enabled()); |
peah | be61562 | 2016-02-13 16:40:47 -0800 | [diff] [blame] | 2007 | config.set_noise_robust_agc_enabled(constants_.use_experimental_agc); |
Minyue | 13b96ba | 2015-10-03 00:39:14 +0200 | [diff] [blame] | 2008 | |
peah | 8271d04 | 2016-11-22 07:24:52 -0800 | [diff] [blame] | 2009 | config.set_hpf_enabled(config_.high_pass_filter.enabled); |
Minyue | 13b96ba | 2015-10-03 00:39:14 +0200 | [diff] [blame] | 2010 | |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 2011 | config.set_ns_enabled(public_submodules_->noise_suppression->is_enabled()); |
| 2012 | config.set_ns_level( |
| 2013 | static_cast<int>(public_submodules_->noise_suppression->level())); |
Minyue | 13b96ba | 2015-10-03 00:39:14 +0200 | [diff] [blame] | 2014 | |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 2015 | config.set_transient_suppression_enabled( |
| 2016 | capture_.transient_suppressor_enabled); |
Alejandro Luebs | c9b0c26 | 2016-05-16 15:32:38 -0700 | [diff] [blame] | 2017 | config.set_intelligibility_enhancer_enabled( |
| 2018 | capture_nonlocked_.intelligibility_enabled); |
Minyue | 13b96ba | 2015-10-03 00:39:14 +0200 | [diff] [blame] | 2019 | |
peah | 7789fe7 | 2016-04-15 01:19:44 -0700 | [diff] [blame] | 2020 | std::string experiments_description = |
| 2021 | public_submodules_->echo_cancellation->GetExperimentsDescription(); |
| 2022 | // TODO(peah): Add semicolon-separated concatenations of experiment |
| 2023 | // descriptions for other submodules. |
peah | ca4cac7 | 2016-06-29 15:26:12 -0700 | [diff] [blame] | 2024 | if (capture_nonlocked_.level_controller_enabled) { |
| 2025 | experiments_description += "LevelController;"; |
| 2026 | } |
henrik.lundin | bd681b9 | 2016-12-05 09:08:42 -0800 | [diff] [blame] | 2027 | if (constants_.agc_clipped_level_min != kClippedLevelMin) { |
| 2028 | experiments_description += "AgcClippingLevelExperiment;"; |
| 2029 | } |
peah | e0eae3c | 2016-12-14 01:16:23 -0800 | [diff] [blame] | 2030 | if (capture_nonlocked_.echo_canceller3_enabled) { |
| 2031 | experiments_description += "EchoCanceller3;"; |
| 2032 | } |
peah | 7789fe7 | 2016-04-15 01:19:44 -0700 | [diff] [blame] | 2033 | config.set_experiments_description(experiments_description); |
| 2034 | |
mbonadei | 7c2c843 | 2017-04-07 00:59:12 -0700 | [diff] [blame] | 2035 | ProtoString serialized_config = config.SerializeAsString(); |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 2036 | if (!forced && |
| 2037 | debug_dump_.capture.last_serialized_config == serialized_config) { |
Minyue | 13b96ba | 2015-10-03 00:39:14 +0200 | [diff] [blame] | 2038 | return kNoError; |
ajm@google.com | 808e0e0 | 2011-08-03 21:08:51 +0000 | [diff] [blame] | 2039 | } |
| 2040 | |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 2041 | debug_dump_.capture.last_serialized_config = serialized_config; |
Minyue | 13b96ba | 2015-10-03 00:39:14 +0200 | [diff] [blame] | 2042 | |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 2043 | debug_dump_.capture.event_msg->set_type(audioproc::Event::CONFIG); |
| 2044 | debug_dump_.capture.event_msg->mutable_config()->CopyFrom(config); |
Minyue | 13b96ba | 2015-10-03 00:39:14 +0200 | [diff] [blame] | 2045 | |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 2046 | RETURN_ON_ERR(WriteMessageToDebugFile(debug_dump_.debug_file.get(), |
ivoc | d66b44d | 2016-01-15 03:06:36 -0800 | [diff] [blame] | 2047 | &debug_dump_.num_bytes_left_for_log_, |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 2048 | &crit_debug_, &debug_dump_.capture)); |
ajm@google.com | 808e0e0 | 2011-08-03 21:08:51 +0000 | [diff] [blame] | 2049 | return kNoError; |
| 2050 | } |
andrew@webrtc.org | 7bf2646 | 2011-12-03 00:03:31 +0000 | [diff] [blame] | 2051 | #endif // WEBRTC_AUDIOPROC_DEBUG_DUMP |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 2052 | |
kwiberg | 83ffe45 | 2016-08-29 14:46:07 -0700 | [diff] [blame] | 2053 | AudioProcessingImpl::ApmCaptureState::ApmCaptureState( |
| 2054 | bool transient_suppressor_enabled, |
| 2055 | const std::vector<Point>& array_geometry, |
| 2056 | SphericalPointf target_direction) |
| 2057 | : aec_system_delay_jumps(-1), |
| 2058 | delay_offset_ms(0), |
| 2059 | was_stream_delay_set(false), |
| 2060 | last_stream_delay_ms(0), |
| 2061 | last_aec_system_delay_ms(0), |
| 2062 | stream_delay_jumps(-1), |
| 2063 | output_will_be_muted(false), |
| 2064 | key_pressed(false), |
| 2065 | transient_suppressor_enabled(transient_suppressor_enabled), |
| 2066 | array_geometry(array_geometry), |
| 2067 | target_direction(target_direction), |
peah | de65ddc | 2016-09-16 15:02:15 -0700 | [diff] [blame] | 2068 | capture_processing_format(kSampleRate16kHz), |
peah | 6799553 | 2017-04-10 14:12:41 -0700 | [diff] [blame] | 2069 | split_rate(kSampleRate16kHz), |
| 2070 | previous_agc_level(0), |
| 2071 | echo_path_gain_change(false) {} |
kwiberg | 83ffe45 | 2016-08-29 14:46:07 -0700 | [diff] [blame] | 2072 | |
| 2073 | AudioProcessingImpl::ApmCaptureState::~ApmCaptureState() = default; |
| 2074 | |
| 2075 | AudioProcessingImpl::ApmRenderState::ApmRenderState() = default; |
| 2076 | |
| 2077 | AudioProcessingImpl::ApmRenderState::~ApmRenderState() = default; |
| 2078 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 2079 | } // namespace webrtc |