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 | |
andrew@webrtc.org | 65f9338 | 2014-04-30 16:44:13 +0000 | [diff] [blame] | 202 | const int16_t* AudioBuffer::data(int channel) const { |
kwiberg@webrtc.org | e364ac9 | 2014-07-18 07:50:29 +0000 | [diff] [blame] | 203 | return channels_->ibuf_const()->channel(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 | 2561d52 | 2014-07-17 08:27:39 +0000 | [diff] [blame] | 207 | mixed_low_pass_valid_ = false; |
kwiberg@webrtc.org | e364ac9 | 2014-07-18 07:50:29 +0000 | [diff] [blame] | 208 | return channels_->ibuf()->channel(channel); |
andrew@webrtc.org | 65f9338 | 2014-04-30 16:44:13 +0000 | [diff] [blame] | 209 | } |
| 210 | |
aluebs@webrtc.org | be05c74 | 2014-11-14 22:18:10 +0000 | [diff] [blame] | 211 | const int16_t* const* AudioBuffer::channels() const { |
| 212 | return channels_->ibuf_const()->channels(); |
| 213 | } |
| 214 | |
| 215 | int16_t* const* AudioBuffer::channels() { |
| 216 | mixed_low_pass_valid_ = false; |
| 217 | return channels_->ibuf()->channels(); |
| 218 | } |
| 219 | |
kwiberg@webrtc.org | 38214d5 | 2014-07-03 09:47:33 +0000 | [diff] [blame] | 220 | const float* AudioBuffer::data_f(int channel) const { |
kwiberg@webrtc.org | e364ac9 | 2014-07-18 07:50:29 +0000 | [diff] [blame] | 221 | return channels_->fbuf_const()->channel(channel); |
mflodman@webrtc.org | d5da250 | 2014-05-15 11:17:21 +0000 | [diff] [blame] | 222 | } |
| 223 | |
kwiberg@webrtc.org | 38214d5 | 2014-07-03 09:47:33 +0000 | [diff] [blame] | 224 | float* AudioBuffer::data_f(int channel) { |
aluebs@webrtc.org | 2561d52 | 2014-07-17 08:27:39 +0000 | [diff] [blame] | 225 | mixed_low_pass_valid_ = false; |
kwiberg@webrtc.org | e364ac9 | 2014-07-18 07:50:29 +0000 | [diff] [blame] | 226 | return channels_->fbuf()->channel(channel); |
kwiberg@webrtc.org | 38214d5 | 2014-07-03 09:47:33 +0000 | [diff] [blame] | 227 | } |
| 228 | |
claguna@google.com | bfacaab | 2014-09-25 20:52:08 +0000 | [diff] [blame] | 229 | const float* const* AudioBuffer::channels_f() const { |
| 230 | return channels_->fbuf_const()->channels(); |
| 231 | } |
| 232 | |
| 233 | float* const* AudioBuffer::channels_f() { |
| 234 | mixed_low_pass_valid_ = false; |
| 235 | return channels_->fbuf()->channels(); |
| 236 | } |
| 237 | |
andrew@webrtc.org | 65f9338 | 2014-04-30 16:44:13 +0000 | [diff] [blame] | 238 | const int16_t* AudioBuffer::low_pass_split_data(int channel) const { |
aluebs@webrtc.org | 79b9eba | 2014-11-26 20:21:38 +0000 | [diff] [blame] | 239 | return split_channels_.size() > 0 |
| 240 | ? split_channels_[0]->ibuf_const()->channel(channel) |
kwiberg@webrtc.org | 2b6bc8d | 2014-07-17 09:46:37 +0000 | [diff] [blame] | 241 | : data(channel); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 242 | } |
| 243 | |
andrew@webrtc.org | 65f9338 | 2014-04-30 16:44:13 +0000 | [diff] [blame] | 244 | int16_t* AudioBuffer::low_pass_split_data(int channel) { |
aluebs@webrtc.org | 2561d52 | 2014-07-17 08:27:39 +0000 | [diff] [blame] | 245 | mixed_low_pass_valid_ = false; |
aluebs@webrtc.org | 79b9eba | 2014-11-26 20:21:38 +0000 | [diff] [blame] | 246 | return split_channels_.size() > 0 |
| 247 | ? split_channels_[0]->ibuf()->channel(channel) |
kwiberg@webrtc.org | e364ac9 | 2014-07-18 07:50:29 +0000 | [diff] [blame] | 248 | : data(channel); |
andrew@webrtc.org | 65f9338 | 2014-04-30 16:44:13 +0000 | [diff] [blame] | 249 | } |
| 250 | |
aluebs@webrtc.org | be05c74 | 2014-11-14 22:18:10 +0000 | [diff] [blame] | 251 | const int16_t* const* AudioBuffer::low_pass_split_channels() const { |
aluebs@webrtc.org | 79b9eba | 2014-11-26 20:21:38 +0000 | [diff] [blame] | 252 | return split_channels_.size() > 0 |
| 253 | ? split_channels_[0]->ibuf_const()->channels() |
aluebs@webrtc.org | be05c74 | 2014-11-14 22:18:10 +0000 | [diff] [blame] | 254 | : channels(); |
| 255 | } |
| 256 | |
| 257 | int16_t* const* AudioBuffer::low_pass_split_channels() { |
| 258 | mixed_low_pass_valid_ = false; |
aluebs@webrtc.org | 79b9eba | 2014-11-26 20:21:38 +0000 | [diff] [blame] | 259 | return split_channels_.size() > 0 ? split_channels_[0]->ibuf()->channels() |
aluebs@webrtc.org | be05c74 | 2014-11-14 22:18:10 +0000 | [diff] [blame] | 260 | : channels(); |
| 261 | } |
| 262 | |
kwiberg@webrtc.org | 38214d5 | 2014-07-03 09:47:33 +0000 | [diff] [blame] | 263 | const float* AudioBuffer::low_pass_split_data_f(int channel) const { |
aluebs@webrtc.org | 79b9eba | 2014-11-26 20:21:38 +0000 | [diff] [blame] | 264 | return split_channels_.size() > 0 |
| 265 | ? split_channels_[0]->fbuf_const()->channel(channel) |
kwiberg@webrtc.org | 2b6bc8d | 2014-07-17 09:46:37 +0000 | [diff] [blame] | 266 | : data_f(channel); |
mflodman@webrtc.org | d5da250 | 2014-05-15 11:17:21 +0000 | [diff] [blame] | 267 | } |
| 268 | |
kwiberg@webrtc.org | 38214d5 | 2014-07-03 09:47:33 +0000 | [diff] [blame] | 269 | float* AudioBuffer::low_pass_split_data_f(int channel) { |
aluebs@webrtc.org | 2561d52 | 2014-07-17 08:27:39 +0000 | [diff] [blame] | 270 | mixed_low_pass_valid_ = false; |
aluebs@webrtc.org | 79b9eba | 2014-11-26 20:21:38 +0000 | [diff] [blame] | 271 | return split_channels_.size() > 0 |
| 272 | ? split_channels_[0]->fbuf()->channel(channel) |
kwiberg@webrtc.org | e364ac9 | 2014-07-18 07:50:29 +0000 | [diff] [blame] | 273 | : data_f(channel); |
kwiberg@webrtc.org | 38214d5 | 2014-07-03 09:47:33 +0000 | [diff] [blame] | 274 | } |
| 275 | |
claguna@google.com | bfacaab | 2014-09-25 20:52:08 +0000 | [diff] [blame] | 276 | const float* const* AudioBuffer::low_pass_split_channels_f() const { |
aluebs@webrtc.org | 79b9eba | 2014-11-26 20:21:38 +0000 | [diff] [blame] | 277 | return split_channels_.size() > 0 |
| 278 | ? split_channels_[0]->fbuf_const()->channels() |
claguna@google.com | bfacaab | 2014-09-25 20:52:08 +0000 | [diff] [blame] | 279 | : channels_f(); |
| 280 | } |
| 281 | |
| 282 | float* const* AudioBuffer::low_pass_split_channels_f() { |
| 283 | mixed_low_pass_valid_ = false; |
aluebs@webrtc.org | 79b9eba | 2014-11-26 20:21:38 +0000 | [diff] [blame] | 284 | return split_channels_.size() > 0 |
| 285 | ? split_channels_[0]->fbuf()->channels() |
claguna@google.com | bfacaab | 2014-09-25 20:52:08 +0000 | [diff] [blame] | 286 | : channels_f(); |
| 287 | } |
| 288 | |
andrew@webrtc.org | 65f9338 | 2014-04-30 16:44:13 +0000 | [diff] [blame] | 289 | const int16_t* AudioBuffer::high_pass_split_data(int channel) const { |
aluebs@webrtc.org | 79b9eba | 2014-11-26 20:21:38 +0000 | [diff] [blame] | 290 | return split_channels_.size() > 1 |
| 291 | ? split_channels_[1]->ibuf_const()->channel(channel) |
kwiberg@webrtc.org | e364ac9 | 2014-07-18 07:50:29 +0000 | [diff] [blame] | 292 | : NULL; |
| 293 | } |
| 294 | |
| 295 | int16_t* AudioBuffer::high_pass_split_data(int channel) { |
aluebs@webrtc.org | 79b9eba | 2014-11-26 20:21:38 +0000 | [diff] [blame] | 296 | return split_channels_.size() > 1 |
| 297 | ? split_channels_[1]->ibuf()->channel(channel) |
kwiberg@webrtc.org | 2b6bc8d | 2014-07-17 09:46:37 +0000 | [diff] [blame] | 298 | : NULL; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 299 | } |
| 300 | |
aluebs@webrtc.org | be05c74 | 2014-11-14 22:18:10 +0000 | [diff] [blame] | 301 | const int16_t* const* AudioBuffer::high_pass_split_channels() const { |
aluebs@webrtc.org | 79b9eba | 2014-11-26 20:21:38 +0000 | [diff] [blame] | 302 | return split_channels_.size() > 1 |
| 303 | ? split_channels_[1]->ibuf_const()->channels() |
aluebs@webrtc.org | be05c74 | 2014-11-14 22:18:10 +0000 | [diff] [blame] | 304 | : NULL; |
| 305 | } |
| 306 | |
| 307 | int16_t* const* AudioBuffer::high_pass_split_channels() { |
aluebs@webrtc.org | 79b9eba | 2014-11-26 20:21:38 +0000 | [diff] [blame] | 308 | return split_channels_.size() > 1 ? split_channels_[1]->ibuf()->channels() |
aluebs@webrtc.org | be05c74 | 2014-11-14 22:18:10 +0000 | [diff] [blame] | 309 | : NULL; |
| 310 | } |
| 311 | |
kwiberg@webrtc.org | 38214d5 | 2014-07-03 09:47:33 +0000 | [diff] [blame] | 312 | const float* AudioBuffer::high_pass_split_data_f(int channel) const { |
aluebs@webrtc.org | 79b9eba | 2014-11-26 20:21:38 +0000 | [diff] [blame] | 313 | return split_channels_.size() > 1 |
| 314 | ? split_channels_[1]->fbuf_const()->channel(channel) |
kwiberg@webrtc.org | 2b6bc8d | 2014-07-17 09:46:37 +0000 | [diff] [blame] | 315 | : NULL; |
mflodman@webrtc.org | d5da250 | 2014-05-15 11:17:21 +0000 | [diff] [blame] | 316 | } |
| 317 | |
kwiberg@webrtc.org | 38214d5 | 2014-07-03 09:47:33 +0000 | [diff] [blame] | 318 | float* AudioBuffer::high_pass_split_data_f(int channel) { |
aluebs@webrtc.org | 79b9eba | 2014-11-26 20:21:38 +0000 | [diff] [blame] | 319 | return split_channels_.size() > 1 |
| 320 | ? split_channels_[1]->fbuf()->channel(channel) |
kwiberg@webrtc.org | e364ac9 | 2014-07-18 07:50:29 +0000 | [diff] [blame] | 321 | : NULL; |
kwiberg@webrtc.org | 38214d5 | 2014-07-03 09:47:33 +0000 | [diff] [blame] | 322 | } |
| 323 | |
claguna@google.com | bfacaab | 2014-09-25 20:52:08 +0000 | [diff] [blame] | 324 | const float* const* AudioBuffer::high_pass_split_channels_f() const { |
aluebs@webrtc.org | 79b9eba | 2014-11-26 20:21:38 +0000 | [diff] [blame] | 325 | return split_channels_.size() > 1 |
| 326 | ? split_channels_[1]->fbuf_const()->channels() |
claguna@google.com | bfacaab | 2014-09-25 20:52:08 +0000 | [diff] [blame] | 327 | : NULL; |
| 328 | } |
| 329 | |
| 330 | float* const* AudioBuffer::high_pass_split_channels_f() { |
aluebs@webrtc.org | 79b9eba | 2014-11-26 20:21:38 +0000 | [diff] [blame] | 331 | return split_channels_.size() > 1 |
| 332 | ? split_channels_[1]->fbuf()->channels() |
claguna@google.com | bfacaab | 2014-09-25 20:52:08 +0000 | [diff] [blame] | 333 | : NULL; |
| 334 | } |
| 335 | |
aluebs@webrtc.org | 087da13 | 2014-11-17 23:01:23 +0000 | [diff] [blame] | 336 | const float* const* AudioBuffer::super_high_pass_split_channels_f() const { |
aluebs@webrtc.org | 79b9eba | 2014-11-26 20:21:38 +0000 | [diff] [blame] | 337 | return split_channels_.size() > 2 |
| 338 | ? split_channels_[2]->fbuf_const()->channels() |
aluebs@webrtc.org | 087da13 | 2014-11-17 23:01:23 +0000 | [diff] [blame] | 339 | : NULL; |
| 340 | } |
| 341 | |
| 342 | float* const* AudioBuffer::super_high_pass_split_channels_f() { |
aluebs@webrtc.org | 79b9eba | 2014-11-26 20:21:38 +0000 | [diff] [blame] | 343 | return split_channels_.size() > 2 |
| 344 | ? split_channels_[2]->fbuf()->channels() |
aluebs@webrtc.org | 087da13 | 2014-11-17 23:01:23 +0000 | [diff] [blame] | 345 | : NULL; |
| 346 | } |
| 347 | |
aluebs@webrtc.org | 2561d52 | 2014-07-17 08:27:39 +0000 | [diff] [blame] | 348 | const int16_t* AudioBuffer::mixed_low_pass_data() { |
| 349 | // Currently only mixing stereo to mono is supported. |
| 350 | assert(num_proc_channels_ == 1 || num_proc_channels_ == 2); |
andrew@webrtc.org | 755b04a | 2011-11-15 16:57:56 +0000 | [diff] [blame] | 351 | |
aluebs@webrtc.org | 2561d52 | 2014-07-17 08:27:39 +0000 | [diff] [blame] | 352 | if (num_proc_channels_ == 1) { |
| 353 | return low_pass_split_data(0); |
| 354 | } |
| 355 | |
| 356 | if (!mixed_low_pass_valid_) { |
| 357 | if (!mixed_low_pass_channels_.get()) { |
| 358 | mixed_low_pass_channels_.reset( |
| 359 | new ChannelBuffer<int16_t>(samples_per_split_channel_, 1)); |
| 360 | } |
| 361 | StereoToMono(low_pass_split_data(0), |
| 362 | low_pass_split_data(1), |
| 363 | mixed_low_pass_channels_->data(), |
| 364 | samples_per_split_channel_); |
| 365 | mixed_low_pass_valid_ = true; |
| 366 | } |
| 367 | return mixed_low_pass_channels_->data(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 368 | } |
| 369 | |
andrew@webrtc.org | 65f9338 | 2014-04-30 16:44:13 +0000 | [diff] [blame] | 370 | const int16_t* AudioBuffer::low_pass_reference(int channel) const { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 371 | if (!reference_copied_) { |
| 372 | return NULL; |
| 373 | } |
| 374 | |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 375 | return low_pass_reference_channels_->channel(channel); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 376 | } |
| 377 | |
andrew@webrtc.org | 103657b | 2014-04-24 18:28:56 +0000 | [diff] [blame] | 378 | const float* AudioBuffer::keyboard_data() const { |
| 379 | return keyboard_data_; |
| 380 | } |
| 381 | |
andrew@webrtc.org | ed083d4 | 2011-09-19 15:28:51 +0000 | [diff] [blame] | 382 | void AudioBuffer::set_activity(AudioFrame::VADActivity activity) { |
| 383 | activity_ = activity; |
| 384 | } |
| 385 | |
andrew@webrtc.org | 755b04a | 2011-11-15 16:57:56 +0000 | [diff] [blame] | 386 | AudioFrame::VADActivity AudioBuffer::activity() const { |
andrew@webrtc.org | ed083d4 | 2011-09-19 15:28:51 +0000 | [diff] [blame] | 387 | return activity_; |
| 388 | } |
| 389 | |
| 390 | int AudioBuffer::num_channels() const { |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 391 | return num_proc_channels_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 392 | } |
| 393 | |
andrew@webrtc.org | ed083d4 | 2011-09-19 15:28:51 +0000 | [diff] [blame] | 394 | int AudioBuffer::samples_per_channel() const { |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 395 | return proc_samples_per_channel_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 396 | } |
| 397 | |
andrew@webrtc.org | ed083d4 | 2011-09-19 15:28:51 +0000 | [diff] [blame] | 398 | int AudioBuffer::samples_per_split_channel() const { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 399 | return samples_per_split_channel_; |
| 400 | } |
| 401 | |
andrew@webrtc.org | 103657b | 2014-04-24 18:28:56 +0000 | [diff] [blame] | 402 | int AudioBuffer::samples_per_keyboard_channel() const { |
| 403 | // We don't resample the keyboard channel. |
| 404 | return input_samples_per_channel_; |
| 405 | } |
| 406 | |
andrew@webrtc.org | ed083d4 | 2011-09-19 15:28:51 +0000 | [diff] [blame] | 407 | // TODO(andrew): Do deinterleaving and mixing in one step? |
| 408 | void AudioBuffer::DeinterleaveFrom(AudioFrame* frame) { |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 409 | assert(proc_samples_per_channel_ == input_samples_per_channel_); |
andrew@webrtc.org | 30be827 | 2014-09-24 20:06:23 +0000 | [diff] [blame] | 410 | assert(frame->num_channels_ == num_input_channels_); |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 411 | assert(frame->samples_per_channel_ == proc_samples_per_channel_); |
| 412 | InitForNewData(); |
andrew@webrtc.org | 63a5098 | 2012-05-02 23:56:37 +0000 | [diff] [blame] | 413 | activity_ = frame->vad_activity_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 414 | |
andrew@webrtc.org | 30be827 | 2014-09-24 20:06:23 +0000 | [diff] [blame] | 415 | if (num_input_channels_ == 2 && num_proc_channels_ == 1) { |
| 416 | // Downmix directly; no explicit deinterleaving needed. |
| 417 | int16_t* downmixed = channels_->ibuf()->channel(0); |
| 418 | for (int i = 0; i < input_samples_per_channel_; ++i) { |
andrew@webrtc.org | 8328e7c | 2014-10-31 04:58:14 +0000 | [diff] [blame] | 419 | 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] | 420 | } |
| 421 | } else { |
| 422 | assert(num_proc_channels_ == num_input_channels_); |
| 423 | int16_t* interleaved = frame->data_; |
| 424 | for (int i = 0; i < num_proc_channels_; ++i) { |
| 425 | int16_t* deinterleaved = channels_->ibuf()->channel(i); |
| 426 | int interleaved_idx = i; |
| 427 | for (int j = 0; j < proc_samples_per_channel_; ++j) { |
| 428 | deinterleaved[j] = interleaved[interleaved_idx]; |
| 429 | interleaved_idx += num_proc_channels_; |
| 430 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 431 | } |
| 432 | } |
| 433 | } |
| 434 | |
andrew@webrtc.org | 755b04a | 2011-11-15 16:57:56 +0000 | [diff] [blame] | 435 | void AudioBuffer::InterleaveTo(AudioFrame* frame, bool data_changed) const { |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 436 | assert(proc_samples_per_channel_ == output_samples_per_channel_); |
| 437 | assert(num_proc_channels_ == num_input_channels_); |
| 438 | assert(frame->num_channels_ == num_proc_channels_); |
| 439 | assert(frame->samples_per_channel_ == proc_samples_per_channel_); |
andrew@webrtc.org | 63a5098 | 2012-05-02 23:56:37 +0000 | [diff] [blame] | 440 | frame->vad_activity_ = activity_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 441 | |
andrew@webrtc.org | 755b04a | 2011-11-15 16:57:56 +0000 | [diff] [blame] | 442 | if (!data_changed) { |
| 443 | return; |
| 444 | } |
| 445 | |
andrew@webrtc.org | 63a5098 | 2012-05-02 23:56:37 +0000 | [diff] [blame] | 446 | int16_t* interleaved = frame->data_; |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 447 | for (int i = 0; i < num_proc_channels_; i++) { |
mflodman@webrtc.org | d5da250 | 2014-05-15 11:17:21 +0000 | [diff] [blame] | 448 | int16_t* deinterleaved = channels_->ibuf()->channel(i); |
andrew@webrtc.org | ed083d4 | 2011-09-19 15:28:51 +0000 | [diff] [blame] | 449 | int interleaved_idx = i; |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 450 | for (int j = 0; j < proc_samples_per_channel_; j++) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 451 | interleaved[interleaved_idx] = deinterleaved[j]; |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 452 | interleaved_idx += num_proc_channels_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 453 | } |
| 454 | } |
| 455 | } |
| 456 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 457 | void AudioBuffer::CopyLowPassToReference() { |
| 458 | reference_copied_ = true; |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 459 | if (!low_pass_reference_channels_.get()) { |
| 460 | low_pass_reference_channels_.reset( |
| 461 | new ChannelBuffer<int16_t>(samples_per_split_channel_, |
| 462 | num_proc_channels_)); |
| 463 | } |
| 464 | for (int i = 0; i < num_proc_channels_; i++) { |
| 465 | low_pass_reference_channels_->CopyFrom(low_pass_split_data(i), i); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 466 | } |
| 467 | } |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 468 | |
aluebs@webrtc.org | be05c74 | 2014-11-14 22:18:10 +0000 | [diff] [blame] | 469 | void AudioBuffer::SplitIntoFrequencyBands() { |
aluebs@webrtc.org | 79b9eba | 2014-11-26 20:21:38 +0000 | [diff] [blame] | 470 | splitting_filter_->Analysis(channels_.get(), |
| 471 | split_channels_.get()); |
aluebs@webrtc.org | be05c74 | 2014-11-14 22:18:10 +0000 | [diff] [blame] | 472 | } |
| 473 | |
| 474 | void AudioBuffer::MergeFrequencyBands() { |
aluebs@webrtc.org | 79b9eba | 2014-11-26 20:21:38 +0000 | [diff] [blame] | 475 | splitting_filter_->Synthesis(split_channels_.get(), |
| 476 | channels_.get()); |
aluebs@webrtc.org | be05c74 | 2014-11-14 22:18:10 +0000 | [diff] [blame] | 477 | } |
| 478 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 479 | } // namespace webrtc |