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