niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1 | /* |
andrew@webrtc.org | 63a5098 | 2012-05-02 23:56:37 +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 | |
pbos@webrtc.org | 7fad4b8 | 2013-05-28 08:11:59 +0000 | [diff] [blame] | 11 | #include "webrtc/modules/audio_processing/audio_buffer.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 12 | |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 13 | #include "webrtc/common_audio/resampler/push_sinc_resampler.h" |
pbos@webrtc.org | 7fad4b8 | 2013-05-28 08:11:59 +0000 | [diff] [blame] | 14 | #include "webrtc/common_audio/signal_processing/include/signal_processing_library.h" |
aluebs@webrtc.org | 79b9eba | 2014-11-26 20:21:38 +0000 | [diff] [blame] | 15 | #include "webrtc/modules/audio_processing/channel_buffer.h" |
aluebs@webrtc.org | 8789376 | 2014-11-27 23:40:25 +0000 | [diff] [blame] | 16 | #include "webrtc/modules/audio_processing/common.h" |
andrew@webrtc.org | 755b04a | 2011-11-15 16:57:56 +0000 | [diff] [blame] | 17 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 18 | namespace webrtc { |
| 19 | namespace { |
| 20 | |
andrew@webrtc.org | 103657b | 2014-04-24 18:28:56 +0000 | [diff] [blame] | 21 | bool HasKeyboardChannel(AudioProcessing::ChannelLayout layout) { |
| 22 | switch (layout) { |
| 23 | case AudioProcessing::kMono: |
| 24 | case AudioProcessing::kStereo: |
| 25 | return false; |
| 26 | case AudioProcessing::kMonoAndKeyboard: |
| 27 | case AudioProcessing::kStereoAndKeyboard: |
| 28 | return true; |
| 29 | } |
| 30 | assert(false); |
| 31 | return false; |
| 32 | } |
| 33 | |
| 34 | int KeyboardChannelIndex(AudioProcessing::ChannelLayout layout) { |
| 35 | switch (layout) { |
| 36 | case AudioProcessing::kMono: |
| 37 | case AudioProcessing::kStereo: |
| 38 | assert(false); |
| 39 | return -1; |
| 40 | case AudioProcessing::kMonoAndKeyboard: |
| 41 | return 1; |
| 42 | case AudioProcessing::kStereoAndKeyboard: |
| 43 | return 2; |
| 44 | } |
| 45 | assert(false); |
| 46 | return -1; |
| 47 | } |
| 48 | |
andrew@webrtc.org | 8328e7c | 2014-10-31 04:58:14 +0000 | [diff] [blame] | 49 | template <typename T> |
| 50 | void StereoToMono(const T* left, const T* right, T* out, |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 51 | int samples_per_channel) { |
andrew@webrtc.org | 8328e7c | 2014-10-31 04:58:14 +0000 | [diff] [blame] | 52 | for (int i = 0; i < samples_per_channel; ++i) |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 53 | out[i] = (left[i] + right[i]) / 2; |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 54 | } |
| 55 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 56 | } // namespace |
| 57 | |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 58 | AudioBuffer::AudioBuffer(int input_samples_per_channel, |
| 59 | int num_input_channels, |
| 60 | int process_samples_per_channel, |
| 61 | int num_process_channels, |
| 62 | int output_samples_per_channel) |
| 63 | : input_samples_per_channel_(input_samples_per_channel), |
| 64 | num_input_channels_(num_input_channels), |
| 65 | proc_samples_per_channel_(process_samples_per_channel), |
| 66 | num_proc_channels_(num_process_channels), |
| 67 | output_samples_per_channel_(output_samples_per_channel), |
| 68 | samples_per_split_channel_(proc_samples_per_channel_), |
aluebs@webrtc.org | 2561d52 | 2014-07-17 08:27:39 +0000 | [diff] [blame] | 69 | mixed_low_pass_valid_(false), |
andrew@webrtc.org | ed083d4 | 2011-09-19 15:28:51 +0000 | [diff] [blame] | 70 | reference_copied_(false), |
| 71 | activity_(AudioFrame::kVadUnknown), |
andrew@webrtc.org | 103657b | 2014-04-24 18:28:56 +0000 | [diff] [blame] | 72 | keyboard_data_(NULL), |
mflodman@webrtc.org | d5da250 | 2014-05-15 11:17:21 +0000 | [diff] [blame] | 73 | channels_(new IFChannelBuffer(proc_samples_per_channel_, |
| 74 | num_proc_channels_)) { |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 75 | assert(input_samples_per_channel_ > 0); |
| 76 | assert(proc_samples_per_channel_ > 0); |
| 77 | assert(output_samples_per_channel_ > 0); |
| 78 | assert(num_input_channels_ > 0 && num_input_channels_ <= 2); |
| 79 | assert(num_proc_channels_ <= num_input_channels); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 80 | |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 81 | if (num_input_channels_ == 2 && num_proc_channels_ == 1) { |
| 82 | input_buffer_.reset(new ChannelBuffer<float>(input_samples_per_channel_, |
| 83 | num_proc_channels_)); |
| 84 | } |
| 85 | |
| 86 | if (input_samples_per_channel_ != proc_samples_per_channel_ || |
| 87 | output_samples_per_channel_ != proc_samples_per_channel_) { |
| 88 | // Create an intermediate buffer for resampling. |
| 89 | process_buffer_.reset(new ChannelBuffer<float>(proc_samples_per_channel_, |
| 90 | num_proc_channels_)); |
| 91 | } |
| 92 | |
| 93 | if (input_samples_per_channel_ != proc_samples_per_channel_) { |
| 94 | input_resamplers_.reserve(num_proc_channels_); |
| 95 | for (int i = 0; i < num_proc_channels_; ++i) { |
| 96 | input_resamplers_.push_back( |
| 97 | new PushSincResampler(input_samples_per_channel_, |
| 98 | proc_samples_per_channel_)); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | if (output_samples_per_channel_ != proc_samples_per_channel_) { |
| 103 | output_resamplers_.reserve(num_proc_channels_); |
| 104 | for (int i = 0; i < num_proc_channels_; ++i) { |
| 105 | output_resamplers_.push_back( |
| 106 | new PushSincResampler(proc_samples_per_channel_, |
| 107 | output_samples_per_channel_)); |
| 108 | } |
| 109 | } |
| 110 | |
aluebs@webrtc.org | 087da13 | 2014-11-17 23:01:23 +0000 | [diff] [blame] | 111 | if (proc_samples_per_channel_ == kSamplesPer32kHzChannel || |
| 112 | proc_samples_per_channel_ == kSamplesPer48kHzChannel) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 113 | samples_per_split_channel_ = kSamplesPer16kHzChannel; |
aluebs@webrtc.org | 79b9eba | 2014-11-26 20:21:38 +0000 | [diff] [blame] | 114 | split_channels_.push_back(new IFChannelBuffer(samples_per_split_channel_, |
kwiberg@webrtc.org | 2b6bc8d | 2014-07-17 09:46:37 +0000 | [diff] [blame] | 115 | num_proc_channels_)); |
aluebs@webrtc.org | 79b9eba | 2014-11-26 20:21:38 +0000 | [diff] [blame] | 116 | split_channels_.push_back(new IFChannelBuffer(samples_per_split_channel_, |
| 117 | num_proc_channels_)); |
aluebs@webrtc.org | be05c74 | 2014-11-14 22:18:10 +0000 | [diff] [blame] | 118 | splitting_filter_.reset(new SplittingFilter(num_proc_channels_)); |
aluebs@webrtc.org | 087da13 | 2014-11-17 23:01:23 +0000 | [diff] [blame] | 119 | if (proc_samples_per_channel_ == kSamplesPer48kHzChannel) { |
aluebs@webrtc.org | 79b9eba | 2014-11-26 20:21:38 +0000 | [diff] [blame] | 120 | split_channels_.push_back(new IFChannelBuffer(samples_per_split_channel_, |
| 121 | num_proc_channels_)); |
aluebs@webrtc.org | 087da13 | 2014-11-17 23:01:23 +0000 | [diff] [blame] | 122 | } |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 123 | } |
| 124 | } |
| 125 | |
andrew@webrtc.org | 103657b | 2014-04-24 18:28:56 +0000 | [diff] [blame] | 126 | AudioBuffer::~AudioBuffer() {} |
| 127 | |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 128 | void AudioBuffer::CopyFrom(const float* const* data, |
| 129 | int samples_per_channel, |
| 130 | AudioProcessing::ChannelLayout layout) { |
| 131 | assert(samples_per_channel == input_samples_per_channel_); |
| 132 | assert(ChannelsFromLayout(layout) == num_input_channels_); |
| 133 | InitForNewData(); |
| 134 | |
andrew@webrtc.org | 103657b | 2014-04-24 18:28:56 +0000 | [diff] [blame] | 135 | if (HasKeyboardChannel(layout)) { |
| 136 | keyboard_data_ = data[KeyboardChannelIndex(layout)]; |
| 137 | } |
| 138 | |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 139 | // Downmix. |
| 140 | const float* const* data_ptr = data; |
| 141 | if (num_input_channels_ == 2 && num_proc_channels_ == 1) { |
| 142 | StereoToMono(data[0], |
| 143 | data[1], |
| 144 | input_buffer_->channel(0), |
| 145 | input_samples_per_channel_); |
| 146 | data_ptr = input_buffer_->channels(); |
| 147 | } |
| 148 | |
| 149 | // Resample. |
| 150 | if (input_samples_per_channel_ != proc_samples_per_channel_) { |
| 151 | for (int i = 0; i < num_proc_channels_; ++i) { |
| 152 | input_resamplers_[i]->Resample(data_ptr[i], |
| 153 | input_samples_per_channel_, |
| 154 | process_buffer_->channel(i), |
| 155 | proc_samples_per_channel_); |
| 156 | } |
| 157 | data_ptr = process_buffer_->channels(); |
| 158 | } |
| 159 | |
andrew@webrtc.org | 8328e7c | 2014-10-31 04:58:14 +0000 | [diff] [blame] | 160 | // Convert to the S16 range. |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 161 | for (int i = 0; i < num_proc_channels_; ++i) { |
andrew@webrtc.org | 8328e7c | 2014-10-31 04:58:14 +0000 | [diff] [blame] | 162 | FloatToFloatS16(data_ptr[i], proc_samples_per_channel_, |
| 163 | channels_->fbuf()->channel(i)); |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 164 | } |
| 165 | } |
| 166 | |
| 167 | void AudioBuffer::CopyTo(int samples_per_channel, |
| 168 | AudioProcessing::ChannelLayout layout, |
| 169 | float* const* data) { |
| 170 | assert(samples_per_channel == output_samples_per_channel_); |
| 171 | assert(ChannelsFromLayout(layout) == num_proc_channels_); |
| 172 | |
andrew@webrtc.org | 8328e7c | 2014-10-31 04:58:14 +0000 | [diff] [blame] | 173 | // Convert to the float range. |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 174 | float* const* data_ptr = data; |
| 175 | if (output_samples_per_channel_ != proc_samples_per_channel_) { |
| 176 | // Convert to an intermediate buffer for subsequent resampling. |
| 177 | data_ptr = process_buffer_->channels(); |
| 178 | } |
| 179 | for (int i = 0; i < num_proc_channels_; ++i) { |
andrew@webrtc.org | 8328e7c | 2014-10-31 04:58:14 +0000 | [diff] [blame] | 180 | FloatS16ToFloat(channels_->fbuf()->channel(i), proc_samples_per_channel_, |
| 181 | data_ptr[i]); |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | // Resample. |
| 185 | if (output_samples_per_channel_ != proc_samples_per_channel_) { |
| 186 | for (int i = 0; i < num_proc_channels_; ++i) { |
| 187 | output_resamplers_[i]->Resample(data_ptr[i], |
| 188 | proc_samples_per_channel_, |
| 189 | data[i], |
| 190 | output_samples_per_channel_); |
| 191 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 192 | } |
| 193 | } |
| 194 | |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 195 | void AudioBuffer::InitForNewData() { |
andrew@webrtc.org | 103657b | 2014-04-24 18:28:56 +0000 | [diff] [blame] | 196 | keyboard_data_ = NULL; |
aluebs@webrtc.org | 2561d52 | 2014-07-17 08:27:39 +0000 | [diff] [blame] | 197 | mixed_low_pass_valid_ = false; |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 198 | reference_copied_ = false; |
| 199 | activity_ = AudioFrame::kVadUnknown; |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 200 | } |
| 201 | |
aluebs@webrtc.org | a7384a1 | 2014-12-03 01:06:35 +0000 | [diff] [blame] | 202 | const int16_t* AudioBuffer::data_const(int channel) const { |
| 203 | return channels_const()[channel]; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 204 | } |
| 205 | |
andrew@webrtc.org | 65f9338 | 2014-04-30 16:44:13 +0000 | [diff] [blame] | 206 | int16_t* AudioBuffer::data(int channel) { |
aluebs@webrtc.org | a7384a1 | 2014-12-03 01:06:35 +0000 | [diff] [blame] | 207 | return channels()[channel]; |
andrew@webrtc.org | 65f9338 | 2014-04-30 16:44:13 +0000 | [diff] [blame] | 208 | } |
| 209 | |
aluebs@webrtc.org | a7384a1 | 2014-12-03 01:06:35 +0000 | [diff] [blame] | 210 | const int16_t* const* AudioBuffer::channels_const() const { |
aluebs@webrtc.org | be05c74 | 2014-11-14 22:18:10 +0000 | [diff] [blame] | 211 | return channels_->ibuf_const()->channels(); |
| 212 | } |
| 213 | |
| 214 | int16_t* const* AudioBuffer::channels() { |
| 215 | mixed_low_pass_valid_ = false; |
| 216 | return channels_->ibuf()->channels(); |
| 217 | } |
| 218 | |
aluebs@webrtc.org | a7384a1 | 2014-12-03 01:06:35 +0000 | [diff] [blame] | 219 | const int16_t* AudioBuffer::split_data_const(int channel, Band band) const { |
| 220 | const int16_t* const* chs = split_channels_const(band); |
| 221 | return chs ? chs[channel] : NULL; |
| 222 | } |
| 223 | |
| 224 | int16_t* AudioBuffer::split_data(int channel, Band band) { |
| 225 | int16_t* const* chs = split_channels(band); |
| 226 | return chs ? chs[channel] : NULL; |
| 227 | } |
| 228 | |
| 229 | const int16_t* const* AudioBuffer::split_channels_const(Band band) const { |
| 230 | if (split_channels_.size() > static_cast<size_t>(band)) { |
| 231 | return split_channels_[band]->ibuf_const()->channels(); |
| 232 | } else { |
| 233 | return band == kBand0To8kHz ? channels_->ibuf_const()->channels() : NULL; |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | int16_t* const* AudioBuffer::split_channels(Band band) { |
| 238 | mixed_low_pass_valid_ = false; |
| 239 | if (split_channels_.size() > static_cast<size_t>(band)) { |
| 240 | return split_channels_[band]->ibuf()->channels(); |
| 241 | } else { |
| 242 | return band == kBand0To8kHz ? channels_->ibuf()->channels() : NULL; |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | const float* AudioBuffer::data_const_f(int channel) const { |
| 247 | return channels_const_f()[channel]; |
mflodman@webrtc.org | d5da250 | 2014-05-15 11:17:21 +0000 | [diff] [blame] | 248 | } |
| 249 | |
kwiberg@webrtc.org | 38214d5 | 2014-07-03 09:47:33 +0000 | [diff] [blame] | 250 | float* AudioBuffer::data_f(int channel) { |
aluebs@webrtc.org | a7384a1 | 2014-12-03 01:06:35 +0000 | [diff] [blame] | 251 | return channels_f()[channel]; |
kwiberg@webrtc.org | 38214d5 | 2014-07-03 09:47:33 +0000 | [diff] [blame] | 252 | } |
| 253 | |
aluebs@webrtc.org | a7384a1 | 2014-12-03 01:06:35 +0000 | [diff] [blame] | 254 | const float* const* AudioBuffer::channels_const_f() const { |
claguna@google.com | bfacaab | 2014-09-25 20:52:08 +0000 | [diff] [blame] | 255 | return channels_->fbuf_const()->channels(); |
| 256 | } |
| 257 | |
| 258 | float* const* AudioBuffer::channels_f() { |
| 259 | mixed_low_pass_valid_ = false; |
| 260 | return channels_->fbuf()->channels(); |
| 261 | } |
| 262 | |
aluebs@webrtc.org | a7384a1 | 2014-12-03 01:06:35 +0000 | [diff] [blame] | 263 | const float* AudioBuffer::split_data_const_f(int channel, Band band) const { |
| 264 | const float* const* chs = split_channels_const_f(band); |
| 265 | return chs ? chs[channel] : NULL; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 266 | } |
| 267 | |
aluebs@webrtc.org | a7384a1 | 2014-12-03 01:06:35 +0000 | [diff] [blame] | 268 | float* AudioBuffer::split_data_f(int channel, Band band) { |
| 269 | float* const* chs = split_channels_f(band); |
| 270 | return chs ? chs[channel] : NULL; |
| 271 | } |
| 272 | |
| 273 | const float* const* AudioBuffer::split_channels_const_f(Band band) const { |
| 274 | if (split_channels_.size() > static_cast<size_t>(band)) { |
| 275 | return split_channels_[band]->fbuf_const()->channels(); |
| 276 | } else { |
| 277 | return band == kBand0To8kHz ? channels_->fbuf_const()->channels() : NULL; |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | float* const* AudioBuffer::split_channels_f(Band band) { |
aluebs@webrtc.org | 2561d52 | 2014-07-17 08:27:39 +0000 | [diff] [blame] | 282 | mixed_low_pass_valid_ = false; |
aluebs@webrtc.org | a7384a1 | 2014-12-03 01:06:35 +0000 | [diff] [blame] | 283 | if (split_channels_.size() > static_cast<size_t>(band)) { |
| 284 | return split_channels_[band]->fbuf()->channels(); |
| 285 | } else { |
| 286 | return band == kBand0To8kHz ? channels_->fbuf()->channels() : NULL; |
| 287 | } |
aluebs@webrtc.org | 087da13 | 2014-11-17 23:01:23 +0000 | [diff] [blame] | 288 | } |
| 289 | |
aluebs@webrtc.org | 2561d52 | 2014-07-17 08:27:39 +0000 | [diff] [blame] | 290 | const int16_t* AudioBuffer::mixed_low_pass_data() { |
| 291 | // Currently only mixing stereo to mono is supported. |
| 292 | assert(num_proc_channels_ == 1 || num_proc_channels_ == 2); |
andrew@webrtc.org | 755b04a | 2011-11-15 16:57:56 +0000 | [diff] [blame] | 293 | |
aluebs@webrtc.org | 2561d52 | 2014-07-17 08:27:39 +0000 | [diff] [blame] | 294 | if (num_proc_channels_ == 1) { |
aluebs@webrtc.org | a7384a1 | 2014-12-03 01:06:35 +0000 | [diff] [blame] | 295 | return split_data_const(0, kBand0To8kHz); |
aluebs@webrtc.org | 2561d52 | 2014-07-17 08:27:39 +0000 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | if (!mixed_low_pass_valid_) { |
| 299 | if (!mixed_low_pass_channels_.get()) { |
| 300 | mixed_low_pass_channels_.reset( |
| 301 | new ChannelBuffer<int16_t>(samples_per_split_channel_, 1)); |
| 302 | } |
aluebs@webrtc.org | a7384a1 | 2014-12-03 01:06:35 +0000 | [diff] [blame] | 303 | StereoToMono(split_data_const(0, kBand0To8kHz), |
| 304 | split_data_const(1, kBand0To8kHz), |
aluebs@webrtc.org | 2561d52 | 2014-07-17 08:27:39 +0000 | [diff] [blame] | 305 | mixed_low_pass_channels_->data(), |
| 306 | samples_per_split_channel_); |
| 307 | mixed_low_pass_valid_ = true; |
| 308 | } |
| 309 | return mixed_low_pass_channels_->data(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 310 | } |
| 311 | |
andrew@webrtc.org | 65f9338 | 2014-04-30 16:44:13 +0000 | [diff] [blame] | 312 | const int16_t* AudioBuffer::low_pass_reference(int channel) const { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 313 | if (!reference_copied_) { |
| 314 | return NULL; |
| 315 | } |
| 316 | |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 317 | return low_pass_reference_channels_->channel(channel); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 318 | } |
| 319 | |
andrew@webrtc.org | 103657b | 2014-04-24 18:28:56 +0000 | [diff] [blame] | 320 | const float* AudioBuffer::keyboard_data() const { |
| 321 | return keyboard_data_; |
| 322 | } |
| 323 | |
andrew@webrtc.org | ed083d4 | 2011-09-19 15:28:51 +0000 | [diff] [blame] | 324 | void AudioBuffer::set_activity(AudioFrame::VADActivity activity) { |
| 325 | activity_ = activity; |
| 326 | } |
| 327 | |
andrew@webrtc.org | 755b04a | 2011-11-15 16:57:56 +0000 | [diff] [blame] | 328 | AudioFrame::VADActivity AudioBuffer::activity() const { |
andrew@webrtc.org | ed083d4 | 2011-09-19 15:28:51 +0000 | [diff] [blame] | 329 | return activity_; |
| 330 | } |
| 331 | |
| 332 | int AudioBuffer::num_channels() const { |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 333 | return num_proc_channels_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 334 | } |
| 335 | |
andrew@webrtc.org | ed083d4 | 2011-09-19 15:28:51 +0000 | [diff] [blame] | 336 | int AudioBuffer::samples_per_channel() const { |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 337 | return proc_samples_per_channel_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 338 | } |
| 339 | |
andrew@webrtc.org | ed083d4 | 2011-09-19 15:28:51 +0000 | [diff] [blame] | 340 | int AudioBuffer::samples_per_split_channel() const { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 341 | return samples_per_split_channel_; |
| 342 | } |
| 343 | |
andrew@webrtc.org | 103657b | 2014-04-24 18:28:56 +0000 | [diff] [blame] | 344 | int AudioBuffer::samples_per_keyboard_channel() const { |
| 345 | // We don't resample the keyboard channel. |
| 346 | return input_samples_per_channel_; |
| 347 | } |
| 348 | |
andrew@webrtc.org | ed083d4 | 2011-09-19 15:28:51 +0000 | [diff] [blame] | 349 | // TODO(andrew): Do deinterleaving and mixing in one step? |
| 350 | void AudioBuffer::DeinterleaveFrom(AudioFrame* frame) { |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 351 | assert(proc_samples_per_channel_ == input_samples_per_channel_); |
andrew@webrtc.org | 30be827 | 2014-09-24 20:06:23 +0000 | [diff] [blame] | 352 | assert(frame->num_channels_ == num_input_channels_); |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 353 | assert(frame->samples_per_channel_ == proc_samples_per_channel_); |
| 354 | InitForNewData(); |
andrew@webrtc.org | 63a5098 | 2012-05-02 23:56:37 +0000 | [diff] [blame] | 355 | activity_ = frame->vad_activity_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 356 | |
andrew@webrtc.org | 30be827 | 2014-09-24 20:06:23 +0000 | [diff] [blame] | 357 | if (num_input_channels_ == 2 && num_proc_channels_ == 1) { |
| 358 | // Downmix directly; no explicit deinterleaving needed. |
| 359 | int16_t* downmixed = channels_->ibuf()->channel(0); |
| 360 | for (int i = 0; i < input_samples_per_channel_; ++i) { |
andrew@webrtc.org | 8328e7c | 2014-10-31 04:58:14 +0000 | [diff] [blame] | 361 | downmixed[i] = (frame->data_[i * 2] + frame->data_[i * 2 + 1]) / 2; |
andrew@webrtc.org | 30be827 | 2014-09-24 20:06:23 +0000 | [diff] [blame] | 362 | } |
| 363 | } else { |
| 364 | assert(num_proc_channels_ == num_input_channels_); |
| 365 | int16_t* interleaved = frame->data_; |
| 366 | for (int i = 0; i < num_proc_channels_; ++i) { |
| 367 | int16_t* deinterleaved = channels_->ibuf()->channel(i); |
| 368 | int interleaved_idx = i; |
| 369 | for (int j = 0; j < proc_samples_per_channel_; ++j) { |
| 370 | deinterleaved[j] = interleaved[interleaved_idx]; |
| 371 | interleaved_idx += num_proc_channels_; |
| 372 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 373 | } |
| 374 | } |
| 375 | } |
| 376 | |
andrew@webrtc.org | 755b04a | 2011-11-15 16:57:56 +0000 | [diff] [blame] | 377 | void AudioBuffer::InterleaveTo(AudioFrame* frame, bool data_changed) const { |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 378 | assert(proc_samples_per_channel_ == output_samples_per_channel_); |
| 379 | assert(num_proc_channels_ == num_input_channels_); |
| 380 | assert(frame->num_channels_ == num_proc_channels_); |
| 381 | assert(frame->samples_per_channel_ == proc_samples_per_channel_); |
andrew@webrtc.org | 63a5098 | 2012-05-02 23:56:37 +0000 | [diff] [blame] | 382 | frame->vad_activity_ = activity_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 383 | |
andrew@webrtc.org | 755b04a | 2011-11-15 16:57:56 +0000 | [diff] [blame] | 384 | if (!data_changed) { |
| 385 | return; |
| 386 | } |
| 387 | |
andrew@webrtc.org | 63a5098 | 2012-05-02 23:56:37 +0000 | [diff] [blame] | 388 | int16_t* interleaved = frame->data_; |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 389 | for (int i = 0; i < num_proc_channels_; i++) { |
mflodman@webrtc.org | d5da250 | 2014-05-15 11:17:21 +0000 | [diff] [blame] | 390 | int16_t* deinterleaved = channels_->ibuf()->channel(i); |
andrew@webrtc.org | ed083d4 | 2011-09-19 15:28:51 +0000 | [diff] [blame] | 391 | int interleaved_idx = i; |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 392 | for (int j = 0; j < proc_samples_per_channel_; j++) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 393 | interleaved[interleaved_idx] = deinterleaved[j]; |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 394 | interleaved_idx += num_proc_channels_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 395 | } |
| 396 | } |
| 397 | } |
| 398 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 399 | void AudioBuffer::CopyLowPassToReference() { |
| 400 | reference_copied_ = true; |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 401 | if (!low_pass_reference_channels_.get()) { |
| 402 | low_pass_reference_channels_.reset( |
| 403 | new ChannelBuffer<int16_t>(samples_per_split_channel_, |
| 404 | num_proc_channels_)); |
| 405 | } |
| 406 | for (int i = 0; i < num_proc_channels_; i++) { |
aluebs@webrtc.org | a7384a1 | 2014-12-03 01:06:35 +0000 | [diff] [blame] | 407 | low_pass_reference_channels_->CopyFrom(split_data_const(i, kBand0To8kHz), |
| 408 | i); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 409 | } |
| 410 | } |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 411 | |
aluebs@webrtc.org | be05c74 | 2014-11-14 22:18:10 +0000 | [diff] [blame] | 412 | void AudioBuffer::SplitIntoFrequencyBands() { |
aluebs@webrtc.org | 79b9eba | 2014-11-26 20:21:38 +0000 | [diff] [blame] | 413 | splitting_filter_->Analysis(channels_.get(), |
| 414 | split_channels_.get()); |
aluebs@webrtc.org | be05c74 | 2014-11-14 22:18:10 +0000 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | void AudioBuffer::MergeFrequencyBands() { |
aluebs@webrtc.org | 79b9eba | 2014-11-26 20:21:38 +0000 | [diff] [blame] | 418 | splitting_filter_->Synthesis(split_channels_.get(), |
| 419 | channels_.get()); |
aluebs@webrtc.org | be05c74 | 2014-11-14 22:18:10 +0000 | [diff] [blame] | 420 | } |
| 421 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 422 | } // namespace webrtc |