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