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