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