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