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