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 | |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 306 | int AudioProcessingImpl::input_sample_rate_hz() const { |
andrew@webrtc.org | 4065403 | 2012-01-30 20:51:15 +0000 | [diff] [blame] | 307 | CriticalSectionScoped crit_scoped(crit_); |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 308 | return fwd_in_format_.rate(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 309 | } |
| 310 | |
andrew@webrtc.org | 46b31b1 | 2014-04-23 03:33:54 +0000 | [diff] [blame] | 311 | int AudioProcessingImpl::sample_rate_hz() const { |
| 312 | CriticalSectionScoped crit_scoped(crit_); |
| 313 | return fwd_in_format_.rate(); |
| 314 | } |
| 315 | |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 316 | int AudioProcessingImpl::proc_sample_rate_hz() const { |
| 317 | return fwd_proc_format_.rate(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 318 | } |
| 319 | |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 320 | int AudioProcessingImpl::proc_split_sample_rate_hz() const { |
| 321 | return split_rate_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | int AudioProcessingImpl::num_reverse_channels() const { |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 325 | return rev_proc_format_.num_channels(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | int AudioProcessingImpl::num_input_channels() const { |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 329 | return fwd_in_format_.num_channels(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | int AudioProcessingImpl::num_output_channels() const { |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 333 | return fwd_proc_format_.num_channels(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 334 | } |
| 335 | |
andrew@webrtc.org | 17342e5 | 2014-02-12 22:28:31 +0000 | [diff] [blame] | 336 | void AudioProcessingImpl::set_output_will_be_muted(bool muted) { |
| 337 | output_will_be_muted_ = muted; |
| 338 | } |
| 339 | |
| 340 | bool AudioProcessingImpl::output_will_be_muted() const { |
| 341 | return output_will_be_muted_; |
| 342 | } |
| 343 | |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 344 | int AudioProcessingImpl::ProcessStream(const float* const* src, |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 345 | int samples_per_channel, |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 346 | int input_sample_rate_hz, |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 347 | ChannelLayout input_layout, |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 348 | int output_sample_rate_hz, |
| 349 | ChannelLayout output_layout, |
| 350 | float* const* dest) { |
andrew@webrtc.org | 4065403 | 2012-01-30 20:51:15 +0000 | [diff] [blame] | 351 | CriticalSectionScoped crit_scoped(crit_); |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 352 | if (!src || !dest) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 353 | return kNullPointerError; |
| 354 | } |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 355 | |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 356 | RETURN_ON_ERR(MaybeInitializeLocked(input_sample_rate_hz, |
| 357 | output_sample_rate_hz, |
| 358 | rev_in_format_.rate(), |
| 359 | ChannelsFromLayout(input_layout), |
| 360 | ChannelsFromLayout(output_layout), |
| 361 | rev_in_format_.num_channels())); |
| 362 | if (samples_per_channel != fwd_in_format_.samples_per_channel()) { |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 363 | return kBadDataLengthError; |
| 364 | } |
| 365 | |
| 366 | #ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP |
| 367 | if (debug_file_->Open()) { |
| 368 | event_msg_->set_type(audioproc::Event::STREAM); |
| 369 | audioproc::Stream* msg = event_msg_->mutable_stream(); |
aluebs@webrtc.org | 59a1b1b | 2014-08-28 10:43:09 +0000 | [diff] [blame^] | 370 | const size_t channel_size = |
| 371 | sizeof(float) * fwd_in_format_.samples_per_channel(); |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 372 | for (int i = 0; i < fwd_in_format_.num_channels(); ++i) |
| 373 | msg->add_input_channel(src[i], channel_size); |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 374 | } |
| 375 | #endif |
| 376 | |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 377 | capture_audio_->CopyFrom(src, samples_per_channel, input_layout); |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 378 | RETURN_ON_ERR(ProcessStreamLocked()); |
| 379 | if (output_copy_needed(is_data_processed())) { |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 380 | capture_audio_->CopyTo(fwd_out_format_.samples_per_channel(), |
| 381 | output_layout, |
| 382 | dest); |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | #ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP |
| 386 | if (debug_file_->Open()) { |
| 387 | audioproc::Stream* msg = event_msg_->mutable_stream(); |
aluebs@webrtc.org | 59a1b1b | 2014-08-28 10:43:09 +0000 | [diff] [blame^] | 388 | const size_t channel_size = |
| 389 | sizeof(float) * fwd_out_format_.samples_per_channel(); |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 390 | for (int i = 0; i < fwd_proc_format_.num_channels(); ++i) |
| 391 | msg->add_output_channel(dest[i], channel_size); |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 392 | RETURN_ON_ERR(WriteMessageToDebugFile()); |
| 393 | } |
| 394 | #endif |
| 395 | |
| 396 | return kNoError; |
| 397 | } |
| 398 | |
| 399 | int AudioProcessingImpl::ProcessStream(AudioFrame* frame) { |
| 400 | CriticalSectionScoped crit_scoped(crit_); |
| 401 | if (!frame) { |
| 402 | return kNullPointerError; |
| 403 | } |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 404 | // Must be a native rate. |
| 405 | if (frame->sample_rate_hz_ != kSampleRate8kHz && |
| 406 | frame->sample_rate_hz_ != kSampleRate16kHz && |
| 407 | frame->sample_rate_hz_ != kSampleRate32kHz) { |
| 408 | return kBadSampleRateError; |
| 409 | } |
| 410 | if (echo_control_mobile_->is_enabled() && |
| 411 | frame->sample_rate_hz_ > kSampleRate16kHz) { |
| 412 | LOG(LS_ERROR) << "AECM only supports 16 or 8 kHz sample rates"; |
| 413 | return kUnsupportedComponentError; |
| 414 | } |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 415 | |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 416 | // TODO(ajm): The input and output rates and channels are currently |
| 417 | // constrained to be identical in the int16 interface. |
andrew@webrtc.org | 60730cf | 2014-01-07 17:45:09 +0000 | [diff] [blame] | 418 | RETURN_ON_ERR(MaybeInitializeLocked(frame->sample_rate_hz_, |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 419 | frame->sample_rate_hz_, |
| 420 | rev_in_format_.rate(), |
| 421 | frame->num_channels_, |
| 422 | frame->num_channels_, |
| 423 | rev_in_format_.num_channels())); |
| 424 | if (frame->samples_per_channel_ != fwd_in_format_.samples_per_channel()) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 425 | return kBadDataLengthError; |
| 426 | } |
| 427 | |
andrew@webrtc.org | 7bf2646 | 2011-12-03 00:03:31 +0000 | [diff] [blame] | 428 | #ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 429 | if (debug_file_->Open()) { |
ajm@google.com | 808e0e0 | 2011-08-03 21:08:51 +0000 | [diff] [blame] | 430 | event_msg_->set_type(audioproc::Event::STREAM); |
| 431 | audioproc::Stream* msg = event_msg_->mutable_stream(); |
andrew@webrtc.org | 755b04a | 2011-11-15 16:57:56 +0000 | [diff] [blame] | 432 | const size_t data_size = sizeof(int16_t) * |
andrew@webrtc.org | 63a5098 | 2012-05-02 23:56:37 +0000 | [diff] [blame] | 433 | frame->samples_per_channel_ * |
| 434 | frame->num_channels_; |
| 435 | msg->set_input_data(frame->data_, data_size); |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 436 | } |
| 437 | #endif |
| 438 | |
| 439 | capture_audio_->DeinterleaveFrom(frame); |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 440 | RETURN_ON_ERR(ProcessStreamLocked()); |
| 441 | capture_audio_->InterleaveTo(frame, output_copy_needed(is_data_processed())); |
| 442 | |
| 443 | #ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP |
| 444 | if (debug_file_->Open()) { |
| 445 | audioproc::Stream* msg = event_msg_->mutable_stream(); |
| 446 | const size_t data_size = sizeof(int16_t) * |
| 447 | frame->samples_per_channel_ * |
| 448 | frame->num_channels_; |
| 449 | msg->set_output_data(frame->data_, data_size); |
| 450 | RETURN_ON_ERR(WriteMessageToDebugFile()); |
| 451 | } |
| 452 | #endif |
| 453 | |
| 454 | return kNoError; |
| 455 | } |
| 456 | |
| 457 | |
| 458 | int AudioProcessingImpl::ProcessStreamLocked() { |
| 459 | #ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP |
| 460 | if (debug_file_->Open()) { |
| 461 | audioproc::Stream* msg = event_msg_->mutable_stream(); |
ajm@google.com | 808e0e0 | 2011-08-03 21:08:51 +0000 | [diff] [blame] | 462 | msg->set_delay(stream_delay_ms_); |
| 463 | msg->set_drift(echo_cancellation_->stream_drift_samples()); |
| 464 | msg->set_level(gain_control_->stream_analog_level()); |
andrew@webrtc.org | ce8e077 | 2014-02-12 15:28:30 +0000 | [diff] [blame] | 465 | msg->set_keypress(key_pressed_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 466 | } |
andrew@webrtc.org | 7bf2646 | 2011-12-03 00:03:31 +0000 | [diff] [blame] | 467 | #endif |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 468 | |
andrew@webrtc.org | 103657b | 2014-04-24 18:28:56 +0000 | [diff] [blame] | 469 | AudioBuffer* ca = capture_audio_.get(); // For brevity. |
andrew@webrtc.org | 369166a | 2012-04-24 18:38:03 +0000 | [diff] [blame] | 470 | bool data_processed = is_data_processed(); |
| 471 | if (analysis_needed(data_processed)) { |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 472 | for (int i = 0; i < fwd_proc_format_.num_channels(); i++) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 473 | // Split into a low and high band. |
andrew@webrtc.org | 103657b | 2014-04-24 18:28:56 +0000 | [diff] [blame] | 474 | WebRtcSpl_AnalysisQMF(ca->data(i), |
| 475 | ca->samples_per_channel(), |
| 476 | ca->low_pass_split_data(i), |
| 477 | ca->high_pass_split_data(i), |
| 478 | ca->filter_states(i)->analysis_filter_state1, |
| 479 | ca->filter_states(i)->analysis_filter_state2); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 480 | } |
| 481 | } |
| 482 | |
andrew@webrtc.org | 103657b | 2014-04-24 18:28:56 +0000 | [diff] [blame] | 483 | RETURN_ON_ERR(high_pass_filter_->ProcessCaptureAudio(ca)); |
| 484 | RETURN_ON_ERR(gain_control_->AnalyzeCaptureAudio(ca)); |
| 485 | RETURN_ON_ERR(echo_cancellation_->ProcessCaptureAudio(ca)); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 486 | |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 487 | if (echo_control_mobile_->is_enabled() && noise_suppression_->is_enabled()) { |
andrew@webrtc.org | 103657b | 2014-04-24 18:28:56 +0000 | [diff] [blame] | 488 | ca->CopyLowPassToReference(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 489 | } |
andrew@webrtc.org | 103657b | 2014-04-24 18:28:56 +0000 | [diff] [blame] | 490 | RETURN_ON_ERR(noise_suppression_->ProcessCaptureAudio(ca)); |
| 491 | RETURN_ON_ERR(echo_control_mobile_->ProcessCaptureAudio(ca)); |
| 492 | RETURN_ON_ERR(voice_detection_->ProcessCaptureAudio(ca)); |
| 493 | RETURN_ON_ERR(gain_control_->ProcessCaptureAudio(ca)); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 494 | |
andrew@webrtc.org | 369166a | 2012-04-24 18:38:03 +0000 | [diff] [blame] | 495 | if (synthesis_needed(data_processed)) { |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 496 | for (int i = 0; i < fwd_proc_format_.num_channels(); i++) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 497 | // Recombine low and high bands. |
andrew@webrtc.org | 103657b | 2014-04-24 18:28:56 +0000 | [diff] [blame] | 498 | WebRtcSpl_SynthesisQMF(ca->low_pass_split_data(i), |
| 499 | ca->high_pass_split_data(i), |
| 500 | ca->samples_per_split_channel(), |
| 501 | ca->data(i), |
| 502 | ca->filter_states(i)->synthesis_filter_state1, |
| 503 | ca->filter_states(i)->synthesis_filter_state2); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 504 | } |
| 505 | } |
| 506 | |
andrew@webrtc.org | 755b04a | 2011-11-15 16:57:56 +0000 | [diff] [blame] | 507 | // The level estimator operates on the recombined data. |
andrew@webrtc.org | 103657b | 2014-04-24 18:28:56 +0000 | [diff] [blame] | 508 | RETURN_ON_ERR(level_estimator_->ProcessStream(ca)); |
ajm@google.com | 808e0e0 | 2011-08-03 21:08:51 +0000 | [diff] [blame] | 509 | |
andrew@webrtc.org | 1e91693 | 2011-11-29 18:28:57 +0000 | [diff] [blame] | 510 | was_stream_delay_set_ = false; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 511 | return kNoError; |
| 512 | } |
| 513 | |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 514 | int AudioProcessingImpl::AnalyzeReverseStream(const float* const* data, |
| 515 | int samples_per_channel, |
| 516 | int sample_rate_hz, |
| 517 | ChannelLayout layout) { |
| 518 | CriticalSectionScoped crit_scoped(crit_); |
| 519 | if (data == NULL) { |
| 520 | return kNullPointerError; |
| 521 | } |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 522 | |
| 523 | const int num_channels = ChannelsFromLayout(layout); |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 524 | RETURN_ON_ERR(MaybeInitializeLocked(fwd_in_format_.rate(), |
| 525 | fwd_out_format_.rate(), |
| 526 | sample_rate_hz, |
| 527 | fwd_in_format_.num_channels(), |
| 528 | fwd_proc_format_.num_channels(), |
| 529 | num_channels)); |
| 530 | if (samples_per_channel != rev_in_format_.samples_per_channel()) { |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 531 | return kBadDataLengthError; |
| 532 | } |
| 533 | |
| 534 | #ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP |
| 535 | if (debug_file_->Open()) { |
| 536 | event_msg_->set_type(audioproc::Event::REVERSE_STREAM); |
| 537 | audioproc::ReverseStream* msg = event_msg_->mutable_reverse_stream(); |
aluebs@webrtc.org | 59a1b1b | 2014-08-28 10:43:09 +0000 | [diff] [blame^] | 538 | const size_t channel_size = |
| 539 | sizeof(float) * rev_in_format_.samples_per_channel(); |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 540 | for (int i = 0; i < num_channels; ++i) |
andrew@webrtc.org | a8b9737 | 2014-03-10 22:26:12 +0000 | [diff] [blame] | 541 | msg->add_channel(data[i], channel_size); |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 542 | RETURN_ON_ERR(WriteMessageToDebugFile()); |
| 543 | } |
| 544 | #endif |
| 545 | |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 546 | render_audio_->CopyFrom(data, samples_per_channel, layout); |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 547 | return AnalyzeReverseStreamLocked(); |
| 548 | } |
| 549 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 550 | int AudioProcessingImpl::AnalyzeReverseStream(AudioFrame* frame) { |
andrew@webrtc.org | 4065403 | 2012-01-30 20:51:15 +0000 | [diff] [blame] | 551 | CriticalSectionScoped crit_scoped(crit_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 552 | if (frame == NULL) { |
| 553 | return kNullPointerError; |
| 554 | } |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 555 | // Must be a native rate. |
| 556 | if (frame->sample_rate_hz_ != kSampleRate8kHz && |
| 557 | frame->sample_rate_hz_ != kSampleRate16kHz && |
| 558 | frame->sample_rate_hz_ != kSampleRate32kHz) { |
| 559 | return kBadSampleRateError; |
| 560 | } |
| 561 | // This interface does not tolerate different forward and reverse rates. |
| 562 | if (frame->sample_rate_hz_ != fwd_in_format_.rate()) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 563 | return kBadSampleRateError; |
| 564 | } |
andrew@webrtc.org | a8b9737 | 2014-03-10 22:26:12 +0000 | [diff] [blame] | 565 | |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 566 | RETURN_ON_ERR(MaybeInitializeLocked(fwd_in_format_.rate(), |
| 567 | fwd_out_format_.rate(), |
| 568 | frame->sample_rate_hz_, |
| 569 | fwd_in_format_.num_channels(), |
| 570 | fwd_in_format_.num_channels(), |
| 571 | frame->num_channels_)); |
| 572 | if (frame->samples_per_channel_ != rev_in_format_.samples_per_channel()) { |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 573 | return kBadDataLengthError; |
| 574 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 575 | |
andrew@webrtc.org | 7bf2646 | 2011-12-03 00:03:31 +0000 | [diff] [blame] | 576 | #ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 577 | if (debug_file_->Open()) { |
ajm@google.com | 808e0e0 | 2011-08-03 21:08:51 +0000 | [diff] [blame] | 578 | event_msg_->set_type(audioproc::Event::REVERSE_STREAM); |
| 579 | audioproc::ReverseStream* msg = event_msg_->mutable_reverse_stream(); |
andrew@webrtc.org | 755b04a | 2011-11-15 16:57:56 +0000 | [diff] [blame] | 580 | const size_t data_size = sizeof(int16_t) * |
andrew@webrtc.org | 63a5098 | 2012-05-02 23:56:37 +0000 | [diff] [blame] | 581 | frame->samples_per_channel_ * |
| 582 | frame->num_channels_; |
| 583 | msg->set_data(frame->data_, data_size); |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 584 | RETURN_ON_ERR(WriteMessageToDebugFile()); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 585 | } |
andrew@webrtc.org | 7bf2646 | 2011-12-03 00:03:31 +0000 | [diff] [blame] | 586 | #endif |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 587 | |
| 588 | render_audio_->DeinterleaveFrom(frame); |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 589 | return AnalyzeReverseStreamLocked(); |
| 590 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 591 | |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 592 | int AudioProcessingImpl::AnalyzeReverseStreamLocked() { |
andrew@webrtc.org | 103657b | 2014-04-24 18:28:56 +0000 | [diff] [blame] | 593 | AudioBuffer* ra = render_audio_.get(); // For brevity. |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 594 | if (rev_proc_format_.rate() == kSampleRate32kHz) { |
| 595 | for (int i = 0; i < rev_proc_format_.num_channels(); i++) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 596 | // Split into low and high band. |
andrew@webrtc.org | 103657b | 2014-04-24 18:28:56 +0000 | [diff] [blame] | 597 | WebRtcSpl_AnalysisQMF(ra->data(i), |
| 598 | ra->samples_per_channel(), |
| 599 | ra->low_pass_split_data(i), |
| 600 | ra->high_pass_split_data(i), |
| 601 | ra->filter_states(i)->analysis_filter_state1, |
| 602 | ra->filter_states(i)->analysis_filter_state2); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 603 | } |
| 604 | } |
| 605 | |
andrew@webrtc.org | 103657b | 2014-04-24 18:28:56 +0000 | [diff] [blame] | 606 | RETURN_ON_ERR(echo_cancellation_->ProcessRenderAudio(ra)); |
| 607 | RETURN_ON_ERR(echo_control_mobile_->ProcessRenderAudio(ra)); |
| 608 | RETURN_ON_ERR(gain_control_->ProcessRenderAudio(ra)); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 609 | |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 610 | return kNoError; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 611 | } |
| 612 | |
| 613 | int AudioProcessingImpl::set_stream_delay_ms(int delay) { |
andrew@webrtc.org | 5f23d64 | 2012-05-29 21:14:06 +0000 | [diff] [blame] | 614 | Error retval = kNoError; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 615 | was_stream_delay_set_ = true; |
andrew@webrtc.org | 6f9f817 | 2012-03-06 19:03:39 +0000 | [diff] [blame] | 616 | delay += delay_offset_ms_; |
| 617 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 618 | if (delay < 0) { |
andrew@webrtc.org | 5f23d64 | 2012-05-29 21:14:06 +0000 | [diff] [blame] | 619 | delay = 0; |
| 620 | retval = kBadStreamParameterWarning; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 621 | } |
| 622 | |
| 623 | // TODO(ajm): the max is rather arbitrarily chosen; investigate. |
| 624 | if (delay > 500) { |
andrew@webrtc.org | 5f23d64 | 2012-05-29 21:14:06 +0000 | [diff] [blame] | 625 | delay = 500; |
| 626 | retval = kBadStreamParameterWarning; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 627 | } |
| 628 | |
| 629 | stream_delay_ms_ = delay; |
andrew@webrtc.org | 5f23d64 | 2012-05-29 21:14:06 +0000 | [diff] [blame] | 630 | return retval; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 631 | } |
| 632 | |
| 633 | int AudioProcessingImpl::stream_delay_ms() const { |
| 634 | return stream_delay_ms_; |
| 635 | } |
| 636 | |
| 637 | bool AudioProcessingImpl::was_stream_delay_set() const { |
| 638 | return was_stream_delay_set_; |
| 639 | } |
| 640 | |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 641 | void AudioProcessingImpl::set_stream_key_pressed(bool key_pressed) { |
| 642 | key_pressed_ = key_pressed; |
| 643 | } |
| 644 | |
| 645 | bool AudioProcessingImpl::stream_key_pressed() const { |
| 646 | return key_pressed_; |
| 647 | } |
| 648 | |
andrew@webrtc.org | 6f9f817 | 2012-03-06 19:03:39 +0000 | [diff] [blame] | 649 | void AudioProcessingImpl::set_delay_offset_ms(int offset) { |
| 650 | CriticalSectionScoped crit_scoped(crit_); |
| 651 | delay_offset_ms_ = offset; |
| 652 | } |
| 653 | |
| 654 | int AudioProcessingImpl::delay_offset_ms() const { |
| 655 | return delay_offset_ms_; |
| 656 | } |
| 657 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 658 | int AudioProcessingImpl::StartDebugRecording( |
| 659 | const char filename[AudioProcessing::kMaxFilenameSize]) { |
andrew@webrtc.org | 4065403 | 2012-01-30 20:51:15 +0000 | [diff] [blame] | 660 | CriticalSectionScoped crit_scoped(crit_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 661 | assert(kMaxFilenameSize == FileWrapper::kMaxFileNameSize); |
| 662 | |
| 663 | if (filename == NULL) { |
| 664 | return kNullPointerError; |
| 665 | } |
| 666 | |
andrew@webrtc.org | 7bf2646 | 2011-12-03 00:03:31 +0000 | [diff] [blame] | 667 | #ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 668 | // Stop any ongoing recording. |
| 669 | if (debug_file_->Open()) { |
| 670 | if (debug_file_->CloseFile() == -1) { |
| 671 | return kFileError; |
| 672 | } |
| 673 | } |
| 674 | |
| 675 | if (debug_file_->OpenFile(filename, false) == -1) { |
| 676 | debug_file_->CloseFile(); |
| 677 | return kFileError; |
| 678 | } |
| 679 | |
ajm@google.com | 808e0e0 | 2011-08-03 21:08:51 +0000 | [diff] [blame] | 680 | int err = WriteInitMessage(); |
| 681 | if (err != kNoError) { |
| 682 | return err; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 683 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 684 | return kNoError; |
andrew@webrtc.org | 7bf2646 | 2011-12-03 00:03:31 +0000 | [diff] [blame] | 685 | #else |
| 686 | return kUnsupportedFunctionError; |
| 687 | #endif // WEBRTC_AUDIOPROC_DEBUG_DUMP |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 688 | } |
| 689 | |
henrikg@webrtc.org | 863b536 | 2013-12-06 16:05:17 +0000 | [diff] [blame] | 690 | int AudioProcessingImpl::StartDebugRecording(FILE* handle) { |
| 691 | CriticalSectionScoped crit_scoped(crit_); |
| 692 | |
| 693 | if (handle == NULL) { |
| 694 | return kNullPointerError; |
| 695 | } |
| 696 | |
| 697 | #ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP |
| 698 | // Stop any ongoing recording. |
| 699 | if (debug_file_->Open()) { |
| 700 | if (debug_file_->CloseFile() == -1) { |
| 701 | return kFileError; |
| 702 | } |
| 703 | } |
| 704 | |
| 705 | if (debug_file_->OpenFromFileHandle(handle, true, false) == -1) { |
| 706 | return kFileError; |
| 707 | } |
| 708 | |
| 709 | int err = WriteInitMessage(); |
| 710 | if (err != kNoError) { |
| 711 | return err; |
| 712 | } |
| 713 | return kNoError; |
| 714 | #else |
| 715 | return kUnsupportedFunctionError; |
| 716 | #endif // WEBRTC_AUDIOPROC_DEBUG_DUMP |
| 717 | } |
| 718 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 719 | int AudioProcessingImpl::StopDebugRecording() { |
andrew@webrtc.org | 4065403 | 2012-01-30 20:51:15 +0000 | [diff] [blame] | 720 | CriticalSectionScoped crit_scoped(crit_); |
andrew@webrtc.org | 7bf2646 | 2011-12-03 00:03:31 +0000 | [diff] [blame] | 721 | |
| 722 | #ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 723 | // We just return if recording hasn't started. |
| 724 | if (debug_file_->Open()) { |
| 725 | if (debug_file_->CloseFile() == -1) { |
| 726 | return kFileError; |
| 727 | } |
| 728 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 729 | return kNoError; |
andrew@webrtc.org | 7bf2646 | 2011-12-03 00:03:31 +0000 | [diff] [blame] | 730 | #else |
| 731 | return kUnsupportedFunctionError; |
| 732 | #endif // WEBRTC_AUDIOPROC_DEBUG_DUMP |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 733 | } |
| 734 | |
| 735 | EchoCancellation* AudioProcessingImpl::echo_cancellation() const { |
| 736 | return echo_cancellation_; |
| 737 | } |
| 738 | |
| 739 | EchoControlMobile* AudioProcessingImpl::echo_control_mobile() const { |
| 740 | return echo_control_mobile_; |
| 741 | } |
| 742 | |
| 743 | GainControl* AudioProcessingImpl::gain_control() const { |
| 744 | return gain_control_; |
| 745 | } |
| 746 | |
| 747 | HighPassFilter* AudioProcessingImpl::high_pass_filter() const { |
| 748 | return high_pass_filter_; |
| 749 | } |
| 750 | |
| 751 | LevelEstimator* AudioProcessingImpl::level_estimator() const { |
| 752 | return level_estimator_; |
| 753 | } |
| 754 | |
| 755 | NoiseSuppression* AudioProcessingImpl::noise_suppression() const { |
| 756 | return noise_suppression_; |
| 757 | } |
| 758 | |
| 759 | VoiceDetection* AudioProcessingImpl::voice_detection() const { |
| 760 | return voice_detection_; |
| 761 | } |
| 762 | |
andrew@webrtc.org | 369166a | 2012-04-24 18:38:03 +0000 | [diff] [blame] | 763 | bool AudioProcessingImpl::is_data_processed() const { |
andrew@webrtc.org | 7bf2646 | 2011-12-03 00:03:31 +0000 | [diff] [blame] | 764 | int enabled_count = 0; |
| 765 | std::list<ProcessingComponent*>::const_iterator it; |
| 766 | for (it = component_list_.begin(); it != component_list_.end(); it++) { |
| 767 | if ((*it)->is_component_enabled()) { |
| 768 | enabled_count++; |
| 769 | } |
| 770 | } |
| 771 | |
| 772 | // Data is unchanged if no components are enabled, or if only level_estimator_ |
| 773 | // or voice_detection_ is enabled. |
| 774 | if (enabled_count == 0) { |
| 775 | return false; |
| 776 | } else if (enabled_count == 1) { |
| 777 | if (level_estimator_->is_enabled() || voice_detection_->is_enabled()) { |
| 778 | return false; |
| 779 | } |
| 780 | } else if (enabled_count == 2) { |
| 781 | if (level_estimator_->is_enabled() && voice_detection_->is_enabled()) { |
| 782 | return false; |
| 783 | } |
| 784 | } |
| 785 | return true; |
| 786 | } |
| 787 | |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 788 | bool AudioProcessingImpl::output_copy_needed(bool is_data_processed) const { |
andrew@webrtc.org | 369166a | 2012-04-24 18:38:03 +0000 | [diff] [blame] | 789 | // Check if we've upmixed or downmixed the audio. |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 790 | return ((fwd_proc_format_.num_channels() != fwd_in_format_.num_channels()) || |
| 791 | is_data_processed); |
andrew@webrtc.org | 7bf2646 | 2011-12-03 00:03:31 +0000 | [diff] [blame] | 792 | } |
| 793 | |
andrew@webrtc.org | 369166a | 2012-04-24 18:38:03 +0000 | [diff] [blame] | 794 | bool AudioProcessingImpl::synthesis_needed(bool is_data_processed) const { |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 795 | return (is_data_processed && fwd_proc_format_.rate() == kSampleRate32kHz); |
andrew@webrtc.org | 369166a | 2012-04-24 18:38:03 +0000 | [diff] [blame] | 796 | } |
| 797 | |
| 798 | bool AudioProcessingImpl::analysis_needed(bool is_data_processed) const { |
| 799 | if (!is_data_processed && !voice_detection_->is_enabled()) { |
andrew@webrtc.org | 7bf2646 | 2011-12-03 00:03:31 +0000 | [diff] [blame] | 800 | // Only level_estimator_ is enabled. |
| 801 | return false; |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 802 | } else if (fwd_proc_format_.rate() == kSampleRate32kHz) { |
andrew@webrtc.org | 7bf2646 | 2011-12-03 00:03:31 +0000 | [diff] [blame] | 803 | // Something besides level_estimator_ is enabled, and we have super-wb. |
| 804 | return true; |
| 805 | } |
| 806 | return false; |
| 807 | } |
| 808 | |
| 809 | #ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP |
ajm@google.com | 808e0e0 | 2011-08-03 21:08:51 +0000 | [diff] [blame] | 810 | int AudioProcessingImpl::WriteMessageToDebugFile() { |
| 811 | int32_t size = event_msg_->ByteSize(); |
| 812 | if (size <= 0) { |
| 813 | return kUnspecifiedError; |
| 814 | } |
andrew@webrtc.org | 621df67 | 2013-10-22 10:27:23 +0000 | [diff] [blame] | 815 | #if defined(WEBRTC_ARCH_BIG_ENDIAN) |
ajm@google.com | 808e0e0 | 2011-08-03 21:08:51 +0000 | [diff] [blame] | 816 | // TODO(ajm): Use little-endian "on the wire". For the moment, we can be |
| 817 | // pretty safe in assuming little-endian. |
| 818 | #endif |
| 819 | |
| 820 | if (!event_msg_->SerializeToString(&event_str_)) { |
| 821 | return kUnspecifiedError; |
| 822 | } |
| 823 | |
| 824 | // Write message preceded by its size. |
| 825 | if (!debug_file_->Write(&size, sizeof(int32_t))) { |
| 826 | return kFileError; |
| 827 | } |
| 828 | if (!debug_file_->Write(event_str_.data(), event_str_.length())) { |
| 829 | return kFileError; |
| 830 | } |
| 831 | |
| 832 | event_msg_->Clear(); |
| 833 | |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 834 | return kNoError; |
ajm@google.com | 808e0e0 | 2011-08-03 21:08:51 +0000 | [diff] [blame] | 835 | } |
| 836 | |
| 837 | int AudioProcessingImpl::WriteInitMessage() { |
| 838 | event_msg_->set_type(audioproc::Event::INIT); |
| 839 | audioproc::Init* msg = event_msg_->mutable_init(); |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 840 | msg->set_sample_rate(fwd_in_format_.rate()); |
| 841 | msg->set_num_input_channels(fwd_in_format_.num_channels()); |
| 842 | msg->set_num_output_channels(fwd_proc_format_.num_channels()); |
| 843 | msg->set_num_reverse_channels(rev_in_format_.num_channels()); |
| 844 | msg->set_reverse_sample_rate(rev_in_format_.rate()); |
| 845 | msg->set_output_sample_rate(fwd_out_format_.rate()); |
ajm@google.com | 808e0e0 | 2011-08-03 21:08:51 +0000 | [diff] [blame] | 846 | |
| 847 | int err = WriteMessageToDebugFile(); |
| 848 | if (err != kNoError) { |
| 849 | return err; |
| 850 | } |
| 851 | |
| 852 | return kNoError; |
| 853 | } |
andrew@webrtc.org | 7bf2646 | 2011-12-03 00:03:31 +0000 | [diff] [blame] | 854 | #endif // WEBRTC_AUDIOPROC_DEBUG_DUMP |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 855 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 856 | } // namespace webrtc |