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