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> |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 14 | |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 15 | #include "webrtc/common_audio/include/audio_util.h" |
andrew@webrtc.org | 60730cf | 2014-01-07 17:45:09 +0000 | [diff] [blame] | 16 | #include "webrtc/common_audio/signal_processing/include/signal_processing_library.h" |
andrew@webrtc.org | 78693fe | 2013-03-01 16:36:19 +0000 | [diff] [blame] | 17 | #include "webrtc/modules/audio_processing/audio_buffer.h" |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 18 | #include "webrtc/modules/audio_processing/common.h" |
andrew@webrtc.org | 56e4a05 | 2014-02-27 22:23:17 +0000 | [diff] [blame] | 19 | #include "webrtc/modules/audio_processing/echo_cancellation_impl.h" |
andrew@webrtc.org | 78693fe | 2013-03-01 16:36:19 +0000 | [diff] [blame] | 20 | #include "webrtc/modules/audio_processing/echo_control_mobile_impl.h" |
| 21 | #include "webrtc/modules/audio_processing/gain_control_impl.h" |
| 22 | #include "webrtc/modules/audio_processing/high_pass_filter_impl.h" |
| 23 | #include "webrtc/modules/audio_processing/level_estimator_impl.h" |
| 24 | #include "webrtc/modules/audio_processing/noise_suppression_impl.h" |
| 25 | #include "webrtc/modules/audio_processing/processing_component.h" |
andrew@webrtc.org | 78693fe | 2013-03-01 16:36:19 +0000 | [diff] [blame] | 26 | #include "webrtc/modules/audio_processing/voice_detection_impl.h" |
| 27 | #include "webrtc/modules/interface/module_common_types.h" |
andrew@webrtc.org | 60730cf | 2014-01-07 17:45:09 +0000 | [diff] [blame] | 28 | #include "webrtc/system_wrappers/interface/compile_assert.h" |
andrew@webrtc.org | 78693fe | 2013-03-01 16:36:19 +0000 | [diff] [blame] | 29 | #include "webrtc/system_wrappers/interface/critical_section_wrapper.h" |
| 30 | #include "webrtc/system_wrappers/interface/file_wrapper.h" |
| 31 | #include "webrtc/system_wrappers/interface/logging.h" |
andrew@webrtc.org | 7bf2646 | 2011-12-03 00:03:31 +0000 | [diff] [blame] | 32 | |
| 33 | #ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP |
| 34 | // Files generated at build-time by the protobuf compiler. |
leozwang@webrtc.org | a373634 | 2012-03-16 21:36:00 +0000 | [diff] [blame] | 35 | #ifdef WEBRTC_ANDROID_PLATFORM_BUILD |
leozwang@webrtc.org | 534e495 | 2012-10-22 21:21:52 +0000 | [diff] [blame] | 36 | #include "external/webrtc/webrtc/modules/audio_processing/debug.pb.h" |
leozwang@google.com | ce9bfbb | 2011-08-03 23:34:31 +0000 | [diff] [blame] | 37 | #else |
ajm@google.com | 808e0e0 | 2011-08-03 21:08:51 +0000 | [diff] [blame] | 38 | #include "webrtc/audio_processing/debug.pb.h" |
leozwang@google.com | ce9bfbb | 2011-08-03 23:34:31 +0000 | [diff] [blame] | 39 | #endif |
andrew@webrtc.org | 7bf2646 | 2011-12-03 00:03:31 +0000 | [diff] [blame] | 40 | #endif // WEBRTC_AUDIOPROC_DEBUG_DUMP |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 41 | |
andrew@webrtc.org | 60730cf | 2014-01-07 17:45:09 +0000 | [diff] [blame] | 42 | #define RETURN_ON_ERR(expr) \ |
| 43 | do { \ |
| 44 | int err = expr; \ |
| 45 | if (err != kNoError) { \ |
| 46 | return err; \ |
| 47 | } \ |
| 48 | } while (0) |
| 49 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 50 | namespace webrtc { |
andrew@webrtc.org | 60730cf | 2014-01-07 17:45:09 +0000 | [diff] [blame] | 51 | |
| 52 | // Throughout webrtc, it's assumed that success is represented by zero. |
| 53 | COMPILE_ASSERT(AudioProcessing::kNoError == 0, no_error_must_be_zero); |
| 54 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 55 | AudioProcessing* AudioProcessing::Create(int id) { |
andrew@webrtc.org | e84978f | 2014-01-25 02:09:06 +0000 | [diff] [blame] | 56 | return Create(); |
| 57 | } |
| 58 | |
| 59 | AudioProcessing* AudioProcessing::Create() { |
| 60 | Config config; |
| 61 | return Create(config); |
| 62 | } |
| 63 | |
| 64 | AudioProcessing* AudioProcessing::Create(const Config& config) { |
| 65 | AudioProcessingImpl* apm = new AudioProcessingImpl(config); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 66 | if (apm->Initialize() != kNoError) { |
| 67 | delete apm; |
| 68 | apm = NULL; |
| 69 | } |
| 70 | |
| 71 | return apm; |
| 72 | } |
| 73 | |
andrew@webrtc.org | e84978f | 2014-01-25 02:09:06 +0000 | [diff] [blame] | 74 | AudioProcessingImpl::AudioProcessingImpl(const Config& config) |
andrew@webrtc.org | 60730cf | 2014-01-07 17:45:09 +0000 | [diff] [blame] | 75 | : echo_cancellation_(NULL), |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 76 | echo_control_mobile_(NULL), |
| 77 | gain_control_(NULL), |
| 78 | high_pass_filter_(NULL), |
| 79 | level_estimator_(NULL), |
| 80 | noise_suppression_(NULL), |
| 81 | voice_detection_(NULL), |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 82 | crit_(CriticalSectionWrapper::CreateCriticalSection()), |
andrew@webrtc.org | 7bf2646 | 2011-12-03 00:03:31 +0000 | [diff] [blame] | 83 | #ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP |
| 84 | debug_file_(FileWrapper::Create()), |
| 85 | event_msg_(new audioproc::Event()), |
| 86 | #endif |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 87 | fwd_in_format_(kSampleRate16kHz, 1), |
| 88 | fwd_proc_format_(kSampleRate16kHz, 1), |
| 89 | fwd_out_format_(kSampleRate16kHz), |
| 90 | rev_in_format_(kSampleRate16kHz, 1), |
| 91 | rev_proc_format_(kSampleRate16kHz, 1), |
| 92 | split_rate_(kSampleRate16kHz), |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 93 | stream_delay_ms_(0), |
andrew@webrtc.org | 6f9f817 | 2012-03-06 19:03:39 +0000 | [diff] [blame] | 94 | delay_offset_ms_(0), |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 95 | was_stream_delay_set_(false), |
andrew@webrtc.org | 38bf249 | 2014-02-13 17:43:44 +0000 | [diff] [blame] | 96 | output_will_be_muted_(false), |
andrew@webrtc.org | 07b5950 | 2014-02-12 16:41:13 +0000 | [diff] [blame] | 97 | key_pressed_(false) { |
andrew@webrtc.org | 56e4a05 | 2014-02-27 22:23:17 +0000 | [diff] [blame] | 98 | echo_cancellation_ = new EchoCancellationImpl(this, crit_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 99 | component_list_.push_back(echo_cancellation_); |
| 100 | |
andrew@webrtc.org | 56e4a05 | 2014-02-27 22:23:17 +0000 | [diff] [blame] | 101 | echo_control_mobile_ = new EchoControlMobileImpl(this, crit_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 102 | component_list_.push_back(echo_control_mobile_); |
| 103 | |
andrew@webrtc.org | 56e4a05 | 2014-02-27 22:23:17 +0000 | [diff] [blame] | 104 | gain_control_ = new GainControlImpl(this, crit_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 105 | component_list_.push_back(gain_control_); |
| 106 | |
andrew@webrtc.org | 56e4a05 | 2014-02-27 22:23:17 +0000 | [diff] [blame] | 107 | high_pass_filter_ = new HighPassFilterImpl(this, crit_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 108 | component_list_.push_back(high_pass_filter_); |
| 109 | |
andrew@webrtc.org | 56e4a05 | 2014-02-27 22:23:17 +0000 | [diff] [blame] | 110 | level_estimator_ = new LevelEstimatorImpl(this, crit_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 111 | component_list_.push_back(level_estimator_); |
| 112 | |
andrew@webrtc.org | 56e4a05 | 2014-02-27 22:23:17 +0000 | [diff] [blame] | 113 | noise_suppression_ = new NoiseSuppressionImpl(this, crit_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 114 | component_list_.push_back(noise_suppression_); |
| 115 | |
andrew@webrtc.org | 56e4a05 | 2014-02-27 22:23:17 +0000 | [diff] [blame] | 116 | voice_detection_ = new VoiceDetectionImpl(this, crit_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 117 | component_list_.push_back(voice_detection_); |
andrew@webrtc.org | e84978f | 2014-01-25 02:09:06 +0000 | [diff] [blame] | 118 | |
| 119 | SetExtraOptions(config); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | AudioProcessingImpl::~AudioProcessingImpl() { |
andrew@webrtc.org | 8186534 | 2012-10-27 00:28:27 +0000 | [diff] [blame] | 123 | { |
| 124 | CriticalSectionScoped crit_scoped(crit_); |
| 125 | while (!component_list_.empty()) { |
| 126 | ProcessingComponent* component = component_list_.front(); |
| 127 | component->Destroy(); |
| 128 | delete component; |
| 129 | component_list_.pop_front(); |
| 130 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 131 | |
andrew@webrtc.org | 7bf2646 | 2011-12-03 00:03:31 +0000 | [diff] [blame] | 132 | #ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP |
andrew@webrtc.org | 8186534 | 2012-10-27 00:28:27 +0000 | [diff] [blame] | 133 | if (debug_file_->Open()) { |
| 134 | debug_file_->CloseFile(); |
| 135 | } |
andrew@webrtc.org | 7bf2646 | 2011-12-03 00:03:31 +0000 | [diff] [blame] | 136 | #endif |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 137 | } |
andrew@webrtc.org | 16cfbe2 | 2012-08-29 16:58:25 +0000 | [diff] [blame] | 138 | delete crit_; |
| 139 | crit_ = NULL; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 140 | } |
| 141 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 142 | int AudioProcessingImpl::Initialize() { |
andrew@webrtc.org | 4065403 | 2012-01-30 20:51:15 +0000 | [diff] [blame] | 143 | CriticalSectionScoped crit_scoped(crit_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 144 | return InitializeLocked(); |
| 145 | } |
| 146 | |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 147 | int AudioProcessingImpl::set_sample_rate_hz(int rate) { |
andrew@webrtc.org | a8b9737 | 2014-03-10 22:26:12 +0000 | [diff] [blame] | 148 | CriticalSectionScoped crit_scoped(crit_); |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 149 | return InitializeLocked(rate, |
| 150 | rate, |
| 151 | rev_in_format_.rate(), |
| 152 | fwd_in_format_.num_channels(), |
| 153 | fwd_proc_format_.num_channels(), |
| 154 | rev_in_format_.num_channels()); |
| 155 | } |
| 156 | |
| 157 | int AudioProcessingImpl::Initialize(int input_sample_rate_hz, |
| 158 | int output_sample_rate_hz, |
| 159 | int reverse_sample_rate_hz, |
| 160 | ChannelLayout input_layout, |
| 161 | ChannelLayout output_layout, |
| 162 | ChannelLayout reverse_layout) { |
| 163 | CriticalSectionScoped crit_scoped(crit_); |
| 164 | return InitializeLocked(input_sample_rate_hz, |
| 165 | output_sample_rate_hz, |
andrew@webrtc.org | a8b9737 | 2014-03-10 22:26:12 +0000 | [diff] [blame] | 166 | reverse_sample_rate_hz, |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 167 | ChannelsFromLayout(input_layout), |
| 168 | ChannelsFromLayout(output_layout), |
| 169 | ChannelsFromLayout(reverse_layout)); |
andrew@webrtc.org | a8b9737 | 2014-03-10 22:26:12 +0000 | [diff] [blame] | 170 | } |
| 171 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 172 | int AudioProcessingImpl::InitializeLocked() { |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 173 | render_audio_.reset(new AudioBuffer(rev_in_format_.samples_per_channel(), |
| 174 | rev_in_format_.num_channels(), |
| 175 | rev_proc_format_.samples_per_channel(), |
| 176 | rev_proc_format_.num_channels(), |
| 177 | rev_proc_format_.samples_per_channel())); |
| 178 | capture_audio_.reset(new AudioBuffer(fwd_in_format_.samples_per_channel(), |
| 179 | fwd_in_format_.num_channels(), |
| 180 | fwd_proc_format_.samples_per_channel(), |
| 181 | fwd_proc_format_.num_channels(), |
| 182 | fwd_out_format_.samples_per_channel())); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 183 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 184 | // Initialize all components. |
| 185 | std::list<ProcessingComponent*>::iterator it; |
andrew@webrtc.org | 8186534 | 2012-10-27 00:28:27 +0000 | [diff] [blame] | 186 | for (it = component_list_.begin(); it != component_list_.end(); ++it) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 187 | int err = (*it)->Initialize(); |
| 188 | if (err != kNoError) { |
| 189 | return err; |
| 190 | } |
| 191 | } |
| 192 | |
andrew@webrtc.org | 7bf2646 | 2011-12-03 00:03:31 +0000 | [diff] [blame] | 193 | #ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP |
ajm@google.com | 808e0e0 | 2011-08-03 21:08:51 +0000 | [diff] [blame] | 194 | if (debug_file_->Open()) { |
| 195 | int err = WriteInitMessage(); |
| 196 | if (err != kNoError) { |
| 197 | return err; |
| 198 | } |
| 199 | } |
andrew@webrtc.org | 7bf2646 | 2011-12-03 00:03:31 +0000 | [diff] [blame] | 200 | #endif |
ajm@google.com | 808e0e0 | 2011-08-03 21:08:51 +0000 | [diff] [blame] | 201 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 202 | return kNoError; |
| 203 | } |
| 204 | |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 205 | int AudioProcessingImpl::InitializeLocked(int input_sample_rate_hz, |
| 206 | int output_sample_rate_hz, |
andrew@webrtc.org | a8b9737 | 2014-03-10 22:26:12 +0000 | [diff] [blame] | 207 | int reverse_sample_rate_hz, |
| 208 | int num_input_channels, |
| 209 | int num_output_channels, |
| 210 | int num_reverse_channels) { |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 211 | if (input_sample_rate_hz <= 0 || |
| 212 | output_sample_rate_hz <= 0 || |
| 213 | reverse_sample_rate_hz <= 0) { |
andrew@webrtc.org | a8b9737 | 2014-03-10 22:26:12 +0000 | [diff] [blame] | 214 | return kBadSampleRateError; |
| 215 | } |
| 216 | if (num_output_channels > num_input_channels) { |
| 217 | return kBadNumberChannelsError; |
| 218 | } |
| 219 | // Only mono and stereo supported currently. |
| 220 | if (num_input_channels > 2 || num_input_channels < 1 || |
| 221 | num_output_channels > 2 || num_output_channels < 1 || |
| 222 | num_reverse_channels > 2 || num_reverse_channels < 1) { |
| 223 | return kBadNumberChannelsError; |
| 224 | } |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 225 | |
| 226 | fwd_in_format_.set(input_sample_rate_hz, num_input_channels); |
| 227 | fwd_out_format_.set(output_sample_rate_hz); |
| 228 | rev_in_format_.set(reverse_sample_rate_hz, num_reverse_channels); |
| 229 | |
| 230 | // We process at the closest native rate >= min(input rate, output rate)... |
| 231 | int min_proc_rate = std::min(fwd_in_format_.rate(), fwd_out_format_.rate()); |
| 232 | int fwd_proc_rate; |
| 233 | if (min_proc_rate > kSampleRate16kHz) { |
| 234 | fwd_proc_rate = kSampleRate32kHz; |
| 235 | } else if (min_proc_rate > kSampleRate8kHz) { |
| 236 | fwd_proc_rate = kSampleRate16kHz; |
| 237 | } else { |
| 238 | fwd_proc_rate = kSampleRate8kHz; |
| 239 | } |
| 240 | // ...with one exception. |
| 241 | if (echo_control_mobile_->is_enabled() && min_proc_rate > kSampleRate16kHz) { |
| 242 | fwd_proc_rate = kSampleRate16kHz; |
andrew@webrtc.org | a8b9737 | 2014-03-10 22:26:12 +0000 | [diff] [blame] | 243 | } |
| 244 | |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 245 | fwd_proc_format_.set(fwd_proc_rate, num_output_channels); |
andrew@webrtc.org | a8b9737 | 2014-03-10 22:26:12 +0000 | [diff] [blame] | 246 | |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 247 | // We normally process the reverse stream at 16 kHz. Unless... |
| 248 | int rev_proc_rate = kSampleRate16kHz; |
| 249 | if (fwd_proc_format_.rate() == kSampleRate8kHz) { |
| 250 | // ...the forward stream is at 8 kHz. |
| 251 | rev_proc_rate = kSampleRate8kHz; |
andrew@webrtc.org | a8b9737 | 2014-03-10 22:26:12 +0000 | [diff] [blame] | 252 | } else { |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 253 | if (rev_in_format_.rate() == kSampleRate32kHz) { |
| 254 | // ...or the input is at 32 kHz, in which case we use the splitting |
| 255 | // filter rather than the resampler. |
| 256 | rev_proc_rate = kSampleRate32kHz; |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | // TODO(ajm): Enable this. |
| 261 | // Always downmix the reverse stream to mono for analysis. |
| 262 | //rev_proc_format_.set(rev_proc_rate, 1); |
| 263 | rev_proc_format_.set(rev_proc_rate, rev_in_format_.num_channels()); |
| 264 | |
| 265 | if (fwd_proc_format_.rate() == kSampleRate32kHz) { |
| 266 | split_rate_ = kSampleRate16kHz; |
| 267 | } else { |
| 268 | split_rate_ = fwd_proc_format_.rate(); |
andrew@webrtc.org | a8b9737 | 2014-03-10 22:26:12 +0000 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | return InitializeLocked(); |
| 272 | } |
| 273 | |
| 274 | // Calls InitializeLocked() if any of the audio parameters have changed from |
| 275 | // their current values. |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 276 | int AudioProcessingImpl::MaybeInitializeLocked(int input_sample_rate_hz, |
| 277 | int output_sample_rate_hz, |
andrew@webrtc.org | a8b9737 | 2014-03-10 22:26:12 +0000 | [diff] [blame] | 278 | int reverse_sample_rate_hz, |
| 279 | int num_input_channels, |
| 280 | int num_output_channels, |
| 281 | int num_reverse_channels) { |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 282 | if (input_sample_rate_hz == fwd_in_format_.rate() && |
| 283 | output_sample_rate_hz == fwd_out_format_.rate() && |
| 284 | reverse_sample_rate_hz == rev_in_format_.rate() && |
| 285 | num_input_channels == fwd_in_format_.num_channels() && |
| 286 | num_output_channels == fwd_proc_format_.num_channels() && |
| 287 | num_reverse_channels == rev_in_format_.num_channels()) { |
andrew@webrtc.org | a8b9737 | 2014-03-10 22:26:12 +0000 | [diff] [blame] | 288 | return kNoError; |
| 289 | } |
| 290 | |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 291 | return InitializeLocked(input_sample_rate_hz, |
| 292 | output_sample_rate_hz, |
andrew@webrtc.org | a8b9737 | 2014-03-10 22:26:12 +0000 | [diff] [blame] | 293 | reverse_sample_rate_hz, |
| 294 | num_input_channels, |
| 295 | num_output_channels, |
| 296 | num_reverse_channels); |
| 297 | } |
| 298 | |
andrew@webrtc.org | 61e596f | 2013-07-25 18:28:29 +0000 | [diff] [blame] | 299 | void AudioProcessingImpl::SetExtraOptions(const Config& config) { |
andrew@webrtc.org | e84978f | 2014-01-25 02:09:06 +0000 | [diff] [blame] | 300 | CriticalSectionScoped crit_scoped(crit_); |
andrew@webrtc.org | 61e596f | 2013-07-25 18:28:29 +0000 | [diff] [blame] | 301 | std::list<ProcessingComponent*>::iterator it; |
| 302 | for (it = component_list_.begin(); it != component_list_.end(); ++it) |
| 303 | (*it)->SetExtraOptions(config); |
| 304 | } |
| 305 | |
aluebs@webrtc.org | 0b72f58 | 2013-11-19 15:17:51 +0000 | [diff] [blame] | 306 | int AudioProcessingImpl::EnableExperimentalNs(bool enable) { |
| 307 | return kNoError; |
| 308 | } |
| 309 | |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 310 | int AudioProcessingImpl::input_sample_rate_hz() const { |
andrew@webrtc.org | 4065403 | 2012-01-30 20:51:15 +0000 | [diff] [blame] | 311 | CriticalSectionScoped crit_scoped(crit_); |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 312 | return fwd_in_format_.rate(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 313 | } |
| 314 | |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 315 | int AudioProcessingImpl::proc_sample_rate_hz() const { |
| 316 | return fwd_proc_format_.rate(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 317 | } |
| 318 | |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 319 | int AudioProcessingImpl::proc_split_sample_rate_hz() const { |
| 320 | return split_rate_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | int AudioProcessingImpl::num_reverse_channels() const { |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 324 | return rev_proc_format_.num_channels(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | int AudioProcessingImpl::num_input_channels() const { |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 328 | return fwd_in_format_.num_channels(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | int AudioProcessingImpl::num_output_channels() const { |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 332 | return fwd_proc_format_.num_channels(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 333 | } |
| 334 | |
andrew@webrtc.org | 17342e5 | 2014-02-12 22:28:31 +0000 | [diff] [blame] | 335 | void AudioProcessingImpl::set_output_will_be_muted(bool muted) { |
| 336 | output_will_be_muted_ = muted; |
| 337 | } |
| 338 | |
| 339 | bool AudioProcessingImpl::output_will_be_muted() const { |
| 340 | return output_will_be_muted_; |
| 341 | } |
| 342 | |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 343 | int AudioProcessingImpl::ProcessStream(const float* const* src, |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 344 | int samples_per_channel, |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 345 | int input_sample_rate_hz, |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 346 | ChannelLayout input_layout, |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 347 | int output_sample_rate_hz, |
| 348 | ChannelLayout output_layout, |
| 349 | float* const* dest) { |
andrew@webrtc.org | 4065403 | 2012-01-30 20:51:15 +0000 | [diff] [blame] | 350 | CriticalSectionScoped crit_scoped(crit_); |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 351 | if (!src || !dest) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 352 | return kNullPointerError; |
| 353 | } |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 354 | |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 355 | RETURN_ON_ERR(MaybeInitializeLocked(input_sample_rate_hz, |
| 356 | output_sample_rate_hz, |
| 357 | rev_in_format_.rate(), |
| 358 | ChannelsFromLayout(input_layout), |
| 359 | ChannelsFromLayout(output_layout), |
| 360 | rev_in_format_.num_channels())); |
| 361 | if (samples_per_channel != fwd_in_format_.samples_per_channel()) { |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 362 | return kBadDataLengthError; |
| 363 | } |
| 364 | |
| 365 | #ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP |
| 366 | if (debug_file_->Open()) { |
| 367 | event_msg_->set_type(audioproc::Event::STREAM); |
| 368 | audioproc::Stream* msg = event_msg_->mutable_stream(); |
| 369 | const size_t channel_size = sizeof(float) * samples_per_channel; |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 370 | for (int i = 0; i < fwd_in_format_.num_channels(); ++i) |
| 371 | msg->add_input_channel(src[i], channel_size); |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 372 | } |
| 373 | #endif |
| 374 | |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 375 | capture_audio_->CopyFrom(src, samples_per_channel, input_layout); |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 376 | RETURN_ON_ERR(ProcessStreamLocked()); |
| 377 | if (output_copy_needed(is_data_processed())) { |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 378 | capture_audio_->CopyTo(fwd_out_format_.samples_per_channel(), |
| 379 | output_layout, |
| 380 | dest); |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | #ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP |
| 384 | if (debug_file_->Open()) { |
| 385 | audioproc::Stream* msg = event_msg_->mutable_stream(); |
| 386 | const size_t channel_size = sizeof(float) * samples_per_channel; |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 387 | for (int i = 0; i < fwd_proc_format_.num_channels(); ++i) |
| 388 | msg->add_output_channel(dest[i], channel_size); |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 389 | RETURN_ON_ERR(WriteMessageToDebugFile()); |
| 390 | } |
| 391 | #endif |
| 392 | |
| 393 | return kNoError; |
| 394 | } |
| 395 | |
| 396 | int AudioProcessingImpl::ProcessStream(AudioFrame* frame) { |
| 397 | CriticalSectionScoped crit_scoped(crit_); |
| 398 | if (!frame) { |
| 399 | return kNullPointerError; |
| 400 | } |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 401 | // Must be a native rate. |
| 402 | if (frame->sample_rate_hz_ != kSampleRate8kHz && |
| 403 | frame->sample_rate_hz_ != kSampleRate16kHz && |
| 404 | frame->sample_rate_hz_ != kSampleRate32kHz) { |
| 405 | return kBadSampleRateError; |
| 406 | } |
| 407 | if (echo_control_mobile_->is_enabled() && |
| 408 | frame->sample_rate_hz_ > kSampleRate16kHz) { |
| 409 | LOG(LS_ERROR) << "AECM only supports 16 or 8 kHz sample rates"; |
| 410 | return kUnsupportedComponentError; |
| 411 | } |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 412 | |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 413 | // TODO(ajm): The input and output rates and channels are currently |
| 414 | // constrained to be identical in the int16 interface. |
andrew@webrtc.org | 60730cf | 2014-01-07 17:45:09 +0000 | [diff] [blame] | 415 | RETURN_ON_ERR(MaybeInitializeLocked(frame->sample_rate_hz_, |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 416 | frame->sample_rate_hz_, |
| 417 | rev_in_format_.rate(), |
| 418 | frame->num_channels_, |
| 419 | frame->num_channels_, |
| 420 | rev_in_format_.num_channels())); |
| 421 | if (frame->samples_per_channel_ != fwd_in_format_.samples_per_channel()) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 422 | return kBadDataLengthError; |
| 423 | } |
| 424 | |
andrew@webrtc.org | 7bf2646 | 2011-12-03 00:03:31 +0000 | [diff] [blame] | 425 | #ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 426 | if (debug_file_->Open()) { |
ajm@google.com | 808e0e0 | 2011-08-03 21:08:51 +0000 | [diff] [blame] | 427 | event_msg_->set_type(audioproc::Event::STREAM); |
| 428 | audioproc::Stream* msg = event_msg_->mutable_stream(); |
andrew@webrtc.org | 755b04a | 2011-11-15 16:57:56 +0000 | [diff] [blame] | 429 | const size_t data_size = sizeof(int16_t) * |
andrew@webrtc.org | 63a5098 | 2012-05-02 23:56:37 +0000 | [diff] [blame] | 430 | frame->samples_per_channel_ * |
| 431 | frame->num_channels_; |
| 432 | msg->set_input_data(frame->data_, data_size); |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 433 | } |
| 434 | #endif |
| 435 | |
| 436 | capture_audio_->DeinterleaveFrom(frame); |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 437 | RETURN_ON_ERR(ProcessStreamLocked()); |
| 438 | capture_audio_->InterleaveTo(frame, output_copy_needed(is_data_processed())); |
| 439 | |
| 440 | #ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP |
| 441 | if (debug_file_->Open()) { |
| 442 | audioproc::Stream* msg = event_msg_->mutable_stream(); |
| 443 | const size_t data_size = sizeof(int16_t) * |
| 444 | frame->samples_per_channel_ * |
| 445 | frame->num_channels_; |
| 446 | msg->set_output_data(frame->data_, data_size); |
| 447 | RETURN_ON_ERR(WriteMessageToDebugFile()); |
| 448 | } |
| 449 | #endif |
| 450 | |
| 451 | return kNoError; |
| 452 | } |
| 453 | |
| 454 | |
| 455 | int AudioProcessingImpl::ProcessStreamLocked() { |
| 456 | #ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP |
| 457 | if (debug_file_->Open()) { |
| 458 | audioproc::Stream* msg = event_msg_->mutable_stream(); |
ajm@google.com | 808e0e0 | 2011-08-03 21:08:51 +0000 | [diff] [blame] | 459 | msg->set_delay(stream_delay_ms_); |
| 460 | msg->set_drift(echo_cancellation_->stream_drift_samples()); |
| 461 | msg->set_level(gain_control_->stream_analog_level()); |
andrew@webrtc.org | ce8e077 | 2014-02-12 15:28:30 +0000 | [diff] [blame] | 462 | msg->set_keypress(key_pressed_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 463 | } |
andrew@webrtc.org | 7bf2646 | 2011-12-03 00:03:31 +0000 | [diff] [blame] | 464 | #endif |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 465 | |
andrew@webrtc.org | 369166a | 2012-04-24 18:38:03 +0000 | [diff] [blame] | 466 | bool data_processed = is_data_processed(); |
| 467 | if (analysis_needed(data_processed)) { |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 468 | for (int i = 0; i < fwd_proc_format_.num_channels(); i++) { |
| 469 | SplitFilterStates* filter_states = capture_audio_->filter_states(i); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 470 | // Split into a low and high band. |
andrew@webrtc.org | 60730cf | 2014-01-07 17:45:09 +0000 | [diff] [blame] | 471 | WebRtcSpl_AnalysisQMF(capture_audio_->data(i), |
| 472 | capture_audio_->samples_per_channel(), |
| 473 | capture_audio_->low_pass_split_data(i), |
| 474 | capture_audio_->high_pass_split_data(i), |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 475 | filter_states->analysis_filter_state1, |
| 476 | filter_states->analysis_filter_state2); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 477 | } |
| 478 | } |
| 479 | |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 480 | RETURN_ON_ERR(high_pass_filter_->ProcessCaptureAudio(capture_audio_.get())); |
| 481 | RETURN_ON_ERR(gain_control_->AnalyzeCaptureAudio(capture_audio_.get())); |
| 482 | RETURN_ON_ERR(echo_cancellation_->ProcessCaptureAudio(capture_audio_.get())); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 483 | |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 484 | if (echo_control_mobile_->is_enabled() && noise_suppression_->is_enabled()) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 485 | capture_audio_->CopyLowPassToReference(); |
| 486 | } |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 487 | RETURN_ON_ERR(noise_suppression_->ProcessCaptureAudio(capture_audio_.get())); |
| 488 | RETURN_ON_ERR( |
| 489 | echo_control_mobile_->ProcessCaptureAudio(capture_audio_.get())); |
| 490 | RETURN_ON_ERR(voice_detection_->ProcessCaptureAudio(capture_audio_.get())); |
| 491 | RETURN_ON_ERR(gain_control_->ProcessCaptureAudio(capture_audio_.get())); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 492 | |
andrew@webrtc.org | 369166a | 2012-04-24 18:38:03 +0000 | [diff] [blame] | 493 | if (synthesis_needed(data_processed)) { |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 494 | for (int i = 0; i < fwd_proc_format_.num_channels(); i++) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 495 | // Recombine low and high bands. |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 496 | SplitFilterStates* filter_states = capture_audio_->filter_states(i); |
andrew@webrtc.org | 60730cf | 2014-01-07 17:45:09 +0000 | [diff] [blame] | 497 | WebRtcSpl_SynthesisQMF(capture_audio_->low_pass_split_data(i), |
| 498 | capture_audio_->high_pass_split_data(i), |
| 499 | capture_audio_->samples_per_split_channel(), |
| 500 | capture_audio_->data(i), |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 501 | filter_states->synthesis_filter_state1, |
| 502 | filter_states->synthesis_filter_state2); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 503 | } |
| 504 | } |
| 505 | |
andrew@webrtc.org | 755b04a | 2011-11-15 16:57:56 +0000 | [diff] [blame] | 506 | // The level estimator operates on the recombined data. |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 507 | RETURN_ON_ERR(level_estimator_->ProcessStream(capture_audio_.get())); |
ajm@google.com | 808e0e0 | 2011-08-03 21:08:51 +0000 | [diff] [blame] | 508 | |
andrew@webrtc.org | 1e91693 | 2011-11-29 18:28:57 +0000 | [diff] [blame] | 509 | was_stream_delay_set_ = false; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 510 | return kNoError; |
| 511 | } |
| 512 | |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 513 | int AudioProcessingImpl::AnalyzeReverseStream(const float* const* data, |
| 514 | int samples_per_channel, |
| 515 | int sample_rate_hz, |
| 516 | ChannelLayout layout) { |
| 517 | CriticalSectionScoped crit_scoped(crit_); |
| 518 | if (data == NULL) { |
| 519 | return kNullPointerError; |
| 520 | } |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 521 | |
| 522 | const int num_channels = ChannelsFromLayout(layout); |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 523 | RETURN_ON_ERR(MaybeInitializeLocked(fwd_in_format_.rate(), |
| 524 | fwd_out_format_.rate(), |
| 525 | sample_rate_hz, |
| 526 | fwd_in_format_.num_channels(), |
| 527 | fwd_proc_format_.num_channels(), |
| 528 | num_channels)); |
| 529 | if (samples_per_channel != rev_in_format_.samples_per_channel()) { |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 530 | return kBadDataLengthError; |
| 531 | } |
| 532 | |
| 533 | #ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP |
| 534 | if (debug_file_->Open()) { |
| 535 | event_msg_->set_type(audioproc::Event::REVERSE_STREAM); |
| 536 | audioproc::ReverseStream* msg = event_msg_->mutable_reverse_stream(); |
| 537 | const size_t channel_size = sizeof(float) * samples_per_channel; |
| 538 | for (int i = 0; i < num_channels; ++i) |
andrew@webrtc.org | a8b9737 | 2014-03-10 22:26:12 +0000 | [diff] [blame] | 539 | msg->add_channel(data[i], channel_size); |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 540 | RETURN_ON_ERR(WriteMessageToDebugFile()); |
| 541 | } |
| 542 | #endif |
| 543 | |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 544 | render_audio_->CopyFrom(data, samples_per_channel, layout); |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 545 | return AnalyzeReverseStreamLocked(); |
| 546 | } |
| 547 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 548 | int AudioProcessingImpl::AnalyzeReverseStream(AudioFrame* frame) { |
andrew@webrtc.org | 4065403 | 2012-01-30 20:51:15 +0000 | [diff] [blame] | 549 | CriticalSectionScoped crit_scoped(crit_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 550 | if (frame == NULL) { |
| 551 | return kNullPointerError; |
| 552 | } |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 553 | // Must be a native rate. |
| 554 | if (frame->sample_rate_hz_ != kSampleRate8kHz && |
| 555 | frame->sample_rate_hz_ != kSampleRate16kHz && |
| 556 | frame->sample_rate_hz_ != kSampleRate32kHz) { |
| 557 | return kBadSampleRateError; |
| 558 | } |
| 559 | // This interface does not tolerate different forward and reverse rates. |
| 560 | if (frame->sample_rate_hz_ != fwd_in_format_.rate()) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 561 | return kBadSampleRateError; |
| 562 | } |
andrew@webrtc.org | a8b9737 | 2014-03-10 22:26:12 +0000 | [diff] [blame] | 563 | |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 564 | RETURN_ON_ERR(MaybeInitializeLocked(fwd_in_format_.rate(), |
| 565 | fwd_out_format_.rate(), |
| 566 | frame->sample_rate_hz_, |
| 567 | fwd_in_format_.num_channels(), |
| 568 | fwd_in_format_.num_channels(), |
| 569 | frame->num_channels_)); |
| 570 | if (frame->samples_per_channel_ != rev_in_format_.samples_per_channel()) { |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 571 | return kBadDataLengthError; |
| 572 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 573 | |
andrew@webrtc.org | 7bf2646 | 2011-12-03 00:03:31 +0000 | [diff] [blame] | 574 | #ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 575 | if (debug_file_->Open()) { |
ajm@google.com | 808e0e0 | 2011-08-03 21:08:51 +0000 | [diff] [blame] | 576 | event_msg_->set_type(audioproc::Event::REVERSE_STREAM); |
| 577 | audioproc::ReverseStream* msg = event_msg_->mutable_reverse_stream(); |
andrew@webrtc.org | 755b04a | 2011-11-15 16:57:56 +0000 | [diff] [blame] | 578 | const size_t data_size = sizeof(int16_t) * |
andrew@webrtc.org | 63a5098 | 2012-05-02 23:56:37 +0000 | [diff] [blame] | 579 | frame->samples_per_channel_ * |
| 580 | frame->num_channels_; |
| 581 | msg->set_data(frame->data_, data_size); |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 582 | RETURN_ON_ERR(WriteMessageToDebugFile()); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 583 | } |
andrew@webrtc.org | 7bf2646 | 2011-12-03 00:03:31 +0000 | [diff] [blame] | 584 | #endif |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 585 | |
| 586 | render_audio_->DeinterleaveFrom(frame); |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 587 | return AnalyzeReverseStreamLocked(); |
| 588 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 589 | |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 590 | // TODO(ajm): Have AnalyzeReverseStream accept sample rates not matching the |
| 591 | // primary stream and convert ourselves rather than having the user manage it. |
| 592 | // We can be smarter and use the splitting filter when appropriate. Similarly, |
| 593 | // perform downmixing here. |
| 594 | int AudioProcessingImpl::AnalyzeReverseStreamLocked() { |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 595 | if (rev_proc_format_.rate() == kSampleRate32kHz) { |
| 596 | for (int i = 0; i < rev_proc_format_.num_channels(); i++) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 597 | // Split into low and high band. |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 598 | SplitFilterStates* filter_states = render_audio_->filter_states(i); |
andrew@webrtc.org | 60730cf | 2014-01-07 17:45:09 +0000 | [diff] [blame] | 599 | WebRtcSpl_AnalysisQMF(render_audio_->data(i), |
| 600 | render_audio_->samples_per_channel(), |
| 601 | render_audio_->low_pass_split_data(i), |
| 602 | render_audio_->high_pass_split_data(i), |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 603 | filter_states->analysis_filter_state1, |
| 604 | filter_states->analysis_filter_state2); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 605 | } |
| 606 | } |
| 607 | |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 608 | RETURN_ON_ERR(echo_cancellation_->ProcessRenderAudio(render_audio_.get())); |
| 609 | RETURN_ON_ERR(echo_control_mobile_->ProcessRenderAudio(render_audio_.get())); |
| 610 | RETURN_ON_ERR(gain_control_->ProcessRenderAudio(render_audio_.get())); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 611 | |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 612 | return kNoError; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 613 | } |
| 614 | |
| 615 | int AudioProcessingImpl::set_stream_delay_ms(int delay) { |
andrew@webrtc.org | 5f23d64 | 2012-05-29 21:14:06 +0000 | [diff] [blame] | 616 | Error retval = kNoError; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 617 | was_stream_delay_set_ = true; |
andrew@webrtc.org | 6f9f817 | 2012-03-06 19:03:39 +0000 | [diff] [blame] | 618 | delay += delay_offset_ms_; |
| 619 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 620 | if (delay < 0) { |
andrew@webrtc.org | 5f23d64 | 2012-05-29 21:14:06 +0000 | [diff] [blame] | 621 | delay = 0; |
| 622 | retval = kBadStreamParameterWarning; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 623 | } |
| 624 | |
| 625 | // TODO(ajm): the max is rather arbitrarily chosen; investigate. |
| 626 | if (delay > 500) { |
andrew@webrtc.org | 5f23d64 | 2012-05-29 21:14:06 +0000 | [diff] [blame] | 627 | delay = 500; |
| 628 | retval = kBadStreamParameterWarning; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 629 | } |
| 630 | |
| 631 | stream_delay_ms_ = delay; |
andrew@webrtc.org | 5f23d64 | 2012-05-29 21:14:06 +0000 | [diff] [blame] | 632 | return retval; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 633 | } |
| 634 | |
| 635 | int AudioProcessingImpl::stream_delay_ms() const { |
| 636 | return stream_delay_ms_; |
| 637 | } |
| 638 | |
| 639 | bool AudioProcessingImpl::was_stream_delay_set() const { |
| 640 | return was_stream_delay_set_; |
| 641 | } |
| 642 | |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 643 | void AudioProcessingImpl::set_stream_key_pressed(bool key_pressed) { |
| 644 | key_pressed_ = key_pressed; |
| 645 | } |
| 646 | |
| 647 | bool AudioProcessingImpl::stream_key_pressed() const { |
| 648 | return key_pressed_; |
| 649 | } |
| 650 | |
andrew@webrtc.org | 6f9f817 | 2012-03-06 19:03:39 +0000 | [diff] [blame] | 651 | void AudioProcessingImpl::set_delay_offset_ms(int offset) { |
| 652 | CriticalSectionScoped crit_scoped(crit_); |
| 653 | delay_offset_ms_ = offset; |
| 654 | } |
| 655 | |
| 656 | int AudioProcessingImpl::delay_offset_ms() const { |
| 657 | return delay_offset_ms_; |
| 658 | } |
| 659 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 660 | int AudioProcessingImpl::StartDebugRecording( |
| 661 | const char filename[AudioProcessing::kMaxFilenameSize]) { |
andrew@webrtc.org | 4065403 | 2012-01-30 20:51:15 +0000 | [diff] [blame] | 662 | CriticalSectionScoped crit_scoped(crit_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 663 | assert(kMaxFilenameSize == FileWrapper::kMaxFileNameSize); |
| 664 | |
| 665 | if (filename == NULL) { |
| 666 | return kNullPointerError; |
| 667 | } |
| 668 | |
andrew@webrtc.org | 7bf2646 | 2011-12-03 00:03:31 +0000 | [diff] [blame] | 669 | #ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 670 | // Stop any ongoing recording. |
| 671 | if (debug_file_->Open()) { |
| 672 | if (debug_file_->CloseFile() == -1) { |
| 673 | return kFileError; |
| 674 | } |
| 675 | } |
| 676 | |
| 677 | if (debug_file_->OpenFile(filename, false) == -1) { |
| 678 | debug_file_->CloseFile(); |
| 679 | return kFileError; |
| 680 | } |
| 681 | |
ajm@google.com | 808e0e0 | 2011-08-03 21:08:51 +0000 | [diff] [blame] | 682 | int err = WriteInitMessage(); |
| 683 | if (err != kNoError) { |
| 684 | return err; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 685 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 686 | return kNoError; |
andrew@webrtc.org | 7bf2646 | 2011-12-03 00:03:31 +0000 | [diff] [blame] | 687 | #else |
| 688 | return kUnsupportedFunctionError; |
| 689 | #endif // WEBRTC_AUDIOPROC_DEBUG_DUMP |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 690 | } |
| 691 | |
henrikg@webrtc.org | 863b536 | 2013-12-06 16:05:17 +0000 | [diff] [blame] | 692 | int AudioProcessingImpl::StartDebugRecording(FILE* handle) { |
| 693 | CriticalSectionScoped crit_scoped(crit_); |
| 694 | |
| 695 | if (handle == NULL) { |
| 696 | return kNullPointerError; |
| 697 | } |
| 698 | |
| 699 | #ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP |
| 700 | // Stop any ongoing recording. |
| 701 | if (debug_file_->Open()) { |
| 702 | if (debug_file_->CloseFile() == -1) { |
| 703 | return kFileError; |
| 704 | } |
| 705 | } |
| 706 | |
| 707 | if (debug_file_->OpenFromFileHandle(handle, true, false) == -1) { |
| 708 | return kFileError; |
| 709 | } |
| 710 | |
| 711 | int err = WriteInitMessage(); |
| 712 | if (err != kNoError) { |
| 713 | return err; |
| 714 | } |
| 715 | return kNoError; |
| 716 | #else |
| 717 | return kUnsupportedFunctionError; |
| 718 | #endif // WEBRTC_AUDIOPROC_DEBUG_DUMP |
| 719 | } |
| 720 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 721 | int AudioProcessingImpl::StopDebugRecording() { |
andrew@webrtc.org | 4065403 | 2012-01-30 20:51:15 +0000 | [diff] [blame] | 722 | CriticalSectionScoped crit_scoped(crit_); |
andrew@webrtc.org | 7bf2646 | 2011-12-03 00:03:31 +0000 | [diff] [blame] | 723 | |
| 724 | #ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 725 | // We just return if recording hasn't started. |
| 726 | if (debug_file_->Open()) { |
| 727 | if (debug_file_->CloseFile() == -1) { |
| 728 | return kFileError; |
| 729 | } |
| 730 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 731 | return kNoError; |
andrew@webrtc.org | 7bf2646 | 2011-12-03 00:03:31 +0000 | [diff] [blame] | 732 | #else |
| 733 | return kUnsupportedFunctionError; |
| 734 | #endif // WEBRTC_AUDIOPROC_DEBUG_DUMP |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 735 | } |
| 736 | |
| 737 | EchoCancellation* AudioProcessingImpl::echo_cancellation() const { |
| 738 | return echo_cancellation_; |
| 739 | } |
| 740 | |
| 741 | EchoControlMobile* AudioProcessingImpl::echo_control_mobile() const { |
| 742 | return echo_control_mobile_; |
| 743 | } |
| 744 | |
| 745 | GainControl* AudioProcessingImpl::gain_control() const { |
| 746 | return gain_control_; |
| 747 | } |
| 748 | |
| 749 | HighPassFilter* AudioProcessingImpl::high_pass_filter() const { |
| 750 | return high_pass_filter_; |
| 751 | } |
| 752 | |
| 753 | LevelEstimator* AudioProcessingImpl::level_estimator() const { |
| 754 | return level_estimator_; |
| 755 | } |
| 756 | |
| 757 | NoiseSuppression* AudioProcessingImpl::noise_suppression() const { |
| 758 | return noise_suppression_; |
| 759 | } |
| 760 | |
| 761 | VoiceDetection* AudioProcessingImpl::voice_detection() const { |
| 762 | return voice_detection_; |
| 763 | } |
| 764 | |
andrew@webrtc.org | 369166a | 2012-04-24 18:38:03 +0000 | [diff] [blame] | 765 | bool AudioProcessingImpl::is_data_processed() const { |
andrew@webrtc.org | 7bf2646 | 2011-12-03 00:03:31 +0000 | [diff] [blame] | 766 | int enabled_count = 0; |
| 767 | std::list<ProcessingComponent*>::const_iterator it; |
| 768 | for (it = component_list_.begin(); it != component_list_.end(); it++) { |
| 769 | if ((*it)->is_component_enabled()) { |
| 770 | enabled_count++; |
| 771 | } |
| 772 | } |
| 773 | |
| 774 | // Data is unchanged if no components are enabled, or if only level_estimator_ |
| 775 | // or voice_detection_ is enabled. |
| 776 | if (enabled_count == 0) { |
| 777 | return false; |
| 778 | } else if (enabled_count == 1) { |
| 779 | if (level_estimator_->is_enabled() || voice_detection_->is_enabled()) { |
| 780 | return false; |
| 781 | } |
| 782 | } else if (enabled_count == 2) { |
| 783 | if (level_estimator_->is_enabled() && voice_detection_->is_enabled()) { |
| 784 | return false; |
| 785 | } |
| 786 | } |
| 787 | return true; |
| 788 | } |
| 789 | |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 790 | bool AudioProcessingImpl::output_copy_needed(bool is_data_processed) const { |
andrew@webrtc.org | 369166a | 2012-04-24 18:38:03 +0000 | [diff] [blame] | 791 | // Check if we've upmixed or downmixed the audio. |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 792 | return ((fwd_proc_format_.num_channels() != fwd_in_format_.num_channels()) || |
| 793 | is_data_processed); |
andrew@webrtc.org | 7bf2646 | 2011-12-03 00:03:31 +0000 | [diff] [blame] | 794 | } |
| 795 | |
andrew@webrtc.org | 369166a | 2012-04-24 18:38:03 +0000 | [diff] [blame] | 796 | bool AudioProcessingImpl::synthesis_needed(bool is_data_processed) const { |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 797 | return (is_data_processed && fwd_proc_format_.rate() == kSampleRate32kHz); |
andrew@webrtc.org | 369166a | 2012-04-24 18:38:03 +0000 | [diff] [blame] | 798 | } |
| 799 | |
| 800 | bool AudioProcessingImpl::analysis_needed(bool is_data_processed) const { |
| 801 | if (!is_data_processed && !voice_detection_->is_enabled()) { |
andrew@webrtc.org | 7bf2646 | 2011-12-03 00:03:31 +0000 | [diff] [blame] | 802 | // Only level_estimator_ is enabled. |
| 803 | return false; |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 804 | } else if (fwd_proc_format_.rate() == kSampleRate32kHz) { |
andrew@webrtc.org | 7bf2646 | 2011-12-03 00:03:31 +0000 | [diff] [blame] | 805 | // Something besides level_estimator_ is enabled, and we have super-wb. |
| 806 | return true; |
| 807 | } |
| 808 | return false; |
| 809 | } |
| 810 | |
| 811 | #ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP |
ajm@google.com | 808e0e0 | 2011-08-03 21:08:51 +0000 | [diff] [blame] | 812 | int AudioProcessingImpl::WriteMessageToDebugFile() { |
| 813 | int32_t size = event_msg_->ByteSize(); |
| 814 | if (size <= 0) { |
| 815 | return kUnspecifiedError; |
| 816 | } |
andrew@webrtc.org | 621df67 | 2013-10-22 10:27:23 +0000 | [diff] [blame] | 817 | #if defined(WEBRTC_ARCH_BIG_ENDIAN) |
ajm@google.com | 808e0e0 | 2011-08-03 21:08:51 +0000 | [diff] [blame] | 818 | // TODO(ajm): Use little-endian "on the wire". For the moment, we can be |
| 819 | // pretty safe in assuming little-endian. |
| 820 | #endif |
| 821 | |
| 822 | if (!event_msg_->SerializeToString(&event_str_)) { |
| 823 | return kUnspecifiedError; |
| 824 | } |
| 825 | |
| 826 | // Write message preceded by its size. |
| 827 | if (!debug_file_->Write(&size, sizeof(int32_t))) { |
| 828 | return kFileError; |
| 829 | } |
| 830 | if (!debug_file_->Write(event_str_.data(), event_str_.length())) { |
| 831 | return kFileError; |
| 832 | } |
| 833 | |
| 834 | event_msg_->Clear(); |
| 835 | |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 836 | return kNoError; |
ajm@google.com | 808e0e0 | 2011-08-03 21:08:51 +0000 | [diff] [blame] | 837 | } |
| 838 | |
| 839 | int AudioProcessingImpl::WriteInitMessage() { |
| 840 | event_msg_->set_type(audioproc::Event::INIT); |
| 841 | audioproc::Init* msg = event_msg_->mutable_init(); |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 842 | msg->set_sample_rate(fwd_in_format_.rate()); |
| 843 | msg->set_num_input_channels(fwd_in_format_.num_channels()); |
| 844 | msg->set_num_output_channels(fwd_proc_format_.num_channels()); |
| 845 | msg->set_num_reverse_channels(rev_in_format_.num_channels()); |
| 846 | msg->set_reverse_sample_rate(rev_in_format_.rate()); |
| 847 | msg->set_output_sample_rate(fwd_out_format_.rate()); |
ajm@google.com | 808e0e0 | 2011-08-03 21:08:51 +0000 | [diff] [blame] | 848 | |
| 849 | int err = WriteMessageToDebugFile(); |
| 850 | if (err != kNoError) { |
| 851 | return err; |
| 852 | } |
| 853 | |
| 854 | return kNoError; |
| 855 | } |
andrew@webrtc.org | 7bf2646 | 2011-12-03 00:03:31 +0000 | [diff] [blame] | 856 | #endif // WEBRTC_AUDIOPROC_DEBUG_DUMP |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 857 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 858 | } // namespace webrtc |