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