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