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