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