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