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 | |
mflodman@webrtc.org | d5da250 | 2014-05-15 11:17:21 +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 | |
mflodman@webrtc.org | d5da250 | 2014-05-15 11:17:21 +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: |
mflodman@webrtc.org | d5da250 | 2014-05-15 11:17:21 +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), |
| 162 | data_(NULL), |
andrew@webrtc.org | 103657b | 2014-04-24 18:28:56 +0000 | [diff] [blame] | 163 | keyboard_data_(NULL), |
mflodman@webrtc.org | d5da250 | 2014-05-15 11:17:21 +0000 | [diff] [blame^] | 164 | channels_(new IFChannelBuffer(proc_samples_per_channel_, |
| 165 | num_proc_channels_)) { |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 166 | assert(input_samples_per_channel_ > 0); |
| 167 | assert(proc_samples_per_channel_ > 0); |
| 168 | assert(output_samples_per_channel_ > 0); |
| 169 | assert(num_input_channels_ > 0 && num_input_channels_ <= 2); |
| 170 | assert(num_proc_channels_ <= num_input_channels); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 171 | |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 172 | if (num_input_channels_ == 2 && num_proc_channels_ == 1) { |
| 173 | input_buffer_.reset(new ChannelBuffer<float>(input_samples_per_channel_, |
| 174 | num_proc_channels_)); |
| 175 | } |
| 176 | |
| 177 | if (input_samples_per_channel_ != proc_samples_per_channel_ || |
| 178 | output_samples_per_channel_ != proc_samples_per_channel_) { |
| 179 | // Create an intermediate buffer for resampling. |
| 180 | process_buffer_.reset(new ChannelBuffer<float>(proc_samples_per_channel_, |
| 181 | num_proc_channels_)); |
| 182 | } |
| 183 | |
| 184 | if (input_samples_per_channel_ != proc_samples_per_channel_) { |
| 185 | input_resamplers_.reserve(num_proc_channels_); |
| 186 | for (int i = 0; i < num_proc_channels_; ++i) { |
| 187 | input_resamplers_.push_back( |
| 188 | new PushSincResampler(input_samples_per_channel_, |
| 189 | proc_samples_per_channel_)); |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | if (output_samples_per_channel_ != proc_samples_per_channel_) { |
| 194 | output_resamplers_.reserve(num_proc_channels_); |
| 195 | for (int i = 0; i < num_proc_channels_; ++i) { |
| 196 | output_resamplers_.push_back( |
| 197 | new PushSincResampler(proc_samples_per_channel_, |
| 198 | output_samples_per_channel_)); |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | if (proc_samples_per_channel_ == kSamplesPer32kHzChannel) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 203 | samples_per_split_channel_ = kSamplesPer16kHzChannel; |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 204 | split_channels_.reset(new SplitChannelBuffer(samples_per_split_channel_, |
| 205 | num_proc_channels_)); |
| 206 | filter_states_.reset(new SplitFilterStates[num_proc_channels_]); |
| 207 | } |
| 208 | } |
| 209 | |
andrew@webrtc.org | 103657b | 2014-04-24 18:28:56 +0000 | [diff] [blame] | 210 | AudioBuffer::~AudioBuffer() {} |
| 211 | |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 212 | void AudioBuffer::CopyFrom(const float* const* data, |
| 213 | int samples_per_channel, |
| 214 | AudioProcessing::ChannelLayout layout) { |
| 215 | assert(samples_per_channel == input_samples_per_channel_); |
| 216 | assert(ChannelsFromLayout(layout) == num_input_channels_); |
| 217 | InitForNewData(); |
| 218 | |
andrew@webrtc.org | 103657b | 2014-04-24 18:28:56 +0000 | [diff] [blame] | 219 | if (HasKeyboardChannel(layout)) { |
| 220 | keyboard_data_ = data[KeyboardChannelIndex(layout)]; |
| 221 | } |
| 222 | |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 223 | // Downmix. |
| 224 | const float* const* data_ptr = data; |
| 225 | if (num_input_channels_ == 2 && num_proc_channels_ == 1) { |
| 226 | StereoToMono(data[0], |
| 227 | data[1], |
| 228 | input_buffer_->channel(0), |
| 229 | input_samples_per_channel_); |
| 230 | data_ptr = input_buffer_->channels(); |
| 231 | } |
| 232 | |
| 233 | // Resample. |
| 234 | if (input_samples_per_channel_ != proc_samples_per_channel_) { |
| 235 | for (int i = 0; i < num_proc_channels_; ++i) { |
| 236 | input_resamplers_[i]->Resample(data_ptr[i], |
| 237 | input_samples_per_channel_, |
| 238 | process_buffer_->channel(i), |
| 239 | proc_samples_per_channel_); |
| 240 | } |
| 241 | data_ptr = process_buffer_->channels(); |
| 242 | } |
| 243 | |
| 244 | // Convert to int16. |
| 245 | for (int i = 0; i < num_proc_channels_; ++i) { |
| 246 | ScaleAndRoundToInt16(data_ptr[i], proc_samples_per_channel_, |
mflodman@webrtc.org | d5da250 | 2014-05-15 11:17:21 +0000 | [diff] [blame^] | 247 | channels_->ibuf()->channel(i)); |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 248 | } |
| 249 | } |
| 250 | |
| 251 | void AudioBuffer::CopyTo(int samples_per_channel, |
| 252 | AudioProcessing::ChannelLayout layout, |
| 253 | float* const* data) { |
| 254 | assert(samples_per_channel == output_samples_per_channel_); |
| 255 | assert(ChannelsFromLayout(layout) == num_proc_channels_); |
| 256 | |
| 257 | // Convert to float. |
| 258 | float* const* data_ptr = data; |
| 259 | if (output_samples_per_channel_ != proc_samples_per_channel_) { |
| 260 | // Convert to an intermediate buffer for subsequent resampling. |
| 261 | data_ptr = process_buffer_->channels(); |
| 262 | } |
| 263 | for (int i = 0; i < num_proc_channels_; ++i) { |
mflodman@webrtc.org | d5da250 | 2014-05-15 11:17:21 +0000 | [diff] [blame^] | 264 | ScaleToFloat(channels_->ibuf()->channel(i), |
| 265 | proc_samples_per_channel_, |
| 266 | data_ptr[i]); |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | // Resample. |
| 270 | if (output_samples_per_channel_ != proc_samples_per_channel_) { |
| 271 | for (int i = 0; i < num_proc_channels_; ++i) { |
| 272 | output_resamplers_[i]->Resample(data_ptr[i], |
| 273 | proc_samples_per_channel_, |
| 274 | data[i], |
| 275 | output_samples_per_channel_); |
| 276 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 277 | } |
| 278 | } |
| 279 | |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 280 | void AudioBuffer::InitForNewData() { |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 281 | data_ = NULL; |
andrew@webrtc.org | 103657b | 2014-04-24 18:28:56 +0000 | [diff] [blame] | 282 | keyboard_data_ = NULL; |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 283 | num_mixed_channels_ = 0; |
| 284 | num_mixed_low_pass_channels_ = 0; |
| 285 | reference_copied_ = false; |
| 286 | activity_ = AudioFrame::kVadUnknown; |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 287 | } |
| 288 | |
andrew@webrtc.org | 65f9338 | 2014-04-30 16:44:13 +0000 | [diff] [blame] | 289 | const int16_t* AudioBuffer::data(int channel) const { |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 290 | assert(channel >= 0 && channel < num_proc_channels_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 291 | if (data_ != NULL) { |
kwiberg@webrtc.org | 4cc7636 | 2014-05-08 07:10:11 +0000 | [diff] [blame] | 292 | assert(channel == 0 && num_proc_channels_ == 1); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 293 | return data_; |
| 294 | } |
| 295 | |
mflodman@webrtc.org | d5da250 | 2014-05-15 11:17:21 +0000 | [diff] [blame^] | 296 | return channels_->ibuf()->channel(channel); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 297 | } |
| 298 | |
andrew@webrtc.org | 65f9338 | 2014-04-30 16:44:13 +0000 | [diff] [blame] | 299 | int16_t* AudioBuffer::data(int channel) { |
| 300 | const AudioBuffer* t = this; |
| 301 | return const_cast<int16_t*>(t->data(channel)); |
| 302 | } |
| 303 | |
mflodman@webrtc.org | d5da250 | 2014-05-15 11:17:21 +0000 | [diff] [blame^] | 304 | float* AudioBuffer::data_f(int channel) { |
| 305 | assert(channel >= 0 && channel < num_proc_channels_); |
| 306 | if (data_ != NULL) { |
| 307 | // Need to make a copy of the data instead of just pointing to it, since |
| 308 | // we're about to convert it to float. |
| 309 | assert(channel == 0 && num_proc_channels_ == 1); |
| 310 | memcpy(channels_->ibuf()->channel(0), data_, |
| 311 | sizeof(*data_) * proc_samples_per_channel_); |
| 312 | data_ = NULL; |
| 313 | } |
| 314 | return channels_->fbuf()->channel(channel); |
| 315 | } |
| 316 | |
andrew@webrtc.org | 65f9338 | 2014-04-30 16:44:13 +0000 | [diff] [blame] | 317 | const int16_t* AudioBuffer::low_pass_split_data(int channel) const { |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 318 | assert(channel >= 0 && channel < num_proc_channels_); |
andrew@webrtc.org | 755b04a | 2011-11-15 16:57:56 +0000 | [diff] [blame] | 319 | if (split_channels_.get() == NULL) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 320 | return data(channel); |
| 321 | } |
| 322 | |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 323 | return split_channels_->low_channel(channel); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 324 | } |
| 325 | |
andrew@webrtc.org | 65f9338 | 2014-04-30 16:44:13 +0000 | [diff] [blame] | 326 | int16_t* AudioBuffer::low_pass_split_data(int channel) { |
| 327 | const AudioBuffer* t = this; |
| 328 | return const_cast<int16_t*>(t->low_pass_split_data(channel)); |
| 329 | } |
| 330 | |
mflodman@webrtc.org | d5da250 | 2014-05-15 11:17:21 +0000 | [diff] [blame^] | 331 | float* AudioBuffer::low_pass_split_data_f(int channel) { |
| 332 | assert(channel >= 0 && channel < num_proc_channels_); |
| 333 | return split_channels_.get() ? split_channels_->low_channel_f(channel) |
| 334 | : data_f(channel); |
| 335 | } |
| 336 | |
andrew@webrtc.org | 65f9338 | 2014-04-30 16:44:13 +0000 | [diff] [blame] | 337 | const int16_t* AudioBuffer::high_pass_split_data(int channel) const { |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 338 | assert(channel >= 0 && channel < num_proc_channels_); |
andrew@webrtc.org | 755b04a | 2011-11-15 16:57:56 +0000 | [diff] [blame] | 339 | if (split_channels_.get() == NULL) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 340 | return NULL; |
| 341 | } |
| 342 | |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 343 | return split_channels_->high_channel(channel); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 344 | } |
| 345 | |
andrew@webrtc.org | 65f9338 | 2014-04-30 16:44:13 +0000 | [diff] [blame] | 346 | int16_t* AudioBuffer::high_pass_split_data(int channel) { |
| 347 | const AudioBuffer* t = this; |
| 348 | return const_cast<int16_t*>(t->high_pass_split_data(channel)); |
| 349 | } |
| 350 | |
mflodman@webrtc.org | d5da250 | 2014-05-15 11:17:21 +0000 | [diff] [blame^] | 351 | float* AudioBuffer::high_pass_split_data_f(int channel) { |
| 352 | assert(channel >= 0 && channel < num_proc_channels_); |
| 353 | return split_channels_.get() ? split_channels_->high_channel_f(channel) |
| 354 | : NULL; |
| 355 | } |
| 356 | |
andrew@webrtc.org | 65f9338 | 2014-04-30 16:44:13 +0000 | [diff] [blame] | 357 | const int16_t* AudioBuffer::mixed_data(int channel) const { |
andrew@webrtc.org | 755b04a | 2011-11-15 16:57:56 +0000 | [diff] [blame] | 358 | assert(channel >= 0 && channel < num_mixed_channels_); |
| 359 | |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 360 | return mixed_channels_->channel(channel); |
andrew@webrtc.org | 755b04a | 2011-11-15 16:57:56 +0000 | [diff] [blame] | 361 | } |
| 362 | |
andrew@webrtc.org | 65f9338 | 2014-04-30 16:44:13 +0000 | [diff] [blame] | 363 | const int16_t* AudioBuffer::mixed_low_pass_data(int channel) const { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 364 | assert(channel >= 0 && channel < num_mixed_low_pass_channels_); |
| 365 | |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 366 | return mixed_low_pass_channels_->channel(channel); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 367 | } |
| 368 | |
andrew@webrtc.org | 65f9338 | 2014-04-30 16:44:13 +0000 | [diff] [blame] | 369 | const int16_t* AudioBuffer::low_pass_reference(int channel) const { |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 370 | assert(channel >= 0 && channel < num_proc_channels_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 371 | if (!reference_copied_) { |
| 372 | return NULL; |
| 373 | } |
| 374 | |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 375 | return low_pass_reference_channels_->channel(channel); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 376 | } |
| 377 | |
andrew@webrtc.org | 103657b | 2014-04-24 18:28:56 +0000 | [diff] [blame] | 378 | const float* AudioBuffer::keyboard_data() const { |
| 379 | return keyboard_data_; |
| 380 | } |
| 381 | |
andrew@webrtc.org | 65f9338 | 2014-04-30 16:44:13 +0000 | [diff] [blame] | 382 | SplitFilterStates* AudioBuffer::filter_states(int channel) { |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 383 | assert(channel >= 0 && channel < num_proc_channels_); |
| 384 | return &filter_states_[channel]; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 385 | } |
| 386 | |
andrew@webrtc.org | ed083d4 | 2011-09-19 15:28:51 +0000 | [diff] [blame] | 387 | void AudioBuffer::set_activity(AudioFrame::VADActivity activity) { |
| 388 | activity_ = activity; |
| 389 | } |
| 390 | |
andrew@webrtc.org | 755b04a | 2011-11-15 16:57:56 +0000 | [diff] [blame] | 391 | AudioFrame::VADActivity AudioBuffer::activity() const { |
andrew@webrtc.org | ed083d4 | 2011-09-19 15:28:51 +0000 | [diff] [blame] | 392 | return activity_; |
| 393 | } |
| 394 | |
| 395 | int AudioBuffer::num_channels() const { |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 396 | return num_proc_channels_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 397 | } |
| 398 | |
andrew@webrtc.org | ed083d4 | 2011-09-19 15:28:51 +0000 | [diff] [blame] | 399 | int AudioBuffer::samples_per_channel() const { |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 400 | return proc_samples_per_channel_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 401 | } |
| 402 | |
andrew@webrtc.org | ed083d4 | 2011-09-19 15:28:51 +0000 | [diff] [blame] | 403 | int AudioBuffer::samples_per_split_channel() const { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 404 | return samples_per_split_channel_; |
| 405 | } |
| 406 | |
andrew@webrtc.org | 103657b | 2014-04-24 18:28:56 +0000 | [diff] [blame] | 407 | int AudioBuffer::samples_per_keyboard_channel() const { |
| 408 | // We don't resample the keyboard channel. |
| 409 | return input_samples_per_channel_; |
| 410 | } |
| 411 | |
andrew@webrtc.org | ed083d4 | 2011-09-19 15:28:51 +0000 | [diff] [blame] | 412 | // TODO(andrew): Do deinterleaving and mixing in one step? |
| 413 | void AudioBuffer::DeinterleaveFrom(AudioFrame* frame) { |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 414 | assert(proc_samples_per_channel_ == input_samples_per_channel_); |
| 415 | assert(num_proc_channels_ == num_input_channels_); |
| 416 | assert(frame->num_channels_ == num_proc_channels_); |
| 417 | assert(frame->samples_per_channel_ == proc_samples_per_channel_); |
| 418 | InitForNewData(); |
andrew@webrtc.org | 63a5098 | 2012-05-02 23:56:37 +0000 | [diff] [blame] | 419 | activity_ = frame->vad_activity_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 420 | |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 421 | if (num_proc_channels_ == 1) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 422 | // We can get away with a pointer assignment in this case. |
andrew@webrtc.org | 63a5098 | 2012-05-02 23:56:37 +0000 | [diff] [blame] | 423 | data_ = frame->data_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 424 | return; |
| 425 | } |
| 426 | |
andrew@webrtc.org | 63a5098 | 2012-05-02 23:56:37 +0000 | [diff] [blame] | 427 | int16_t* interleaved = frame->data_; |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 428 | for (int i = 0; i < num_proc_channels_; i++) { |
mflodman@webrtc.org | d5da250 | 2014-05-15 11:17:21 +0000 | [diff] [blame^] | 429 | int16_t* deinterleaved = channels_->ibuf()->channel(i); |
andrew@webrtc.org | ed083d4 | 2011-09-19 15:28:51 +0000 | [diff] [blame] | 430 | int interleaved_idx = i; |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 431 | for (int j = 0; j < proc_samples_per_channel_; j++) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 432 | deinterleaved[j] = interleaved[interleaved_idx]; |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 433 | interleaved_idx += num_proc_channels_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 434 | } |
| 435 | } |
| 436 | } |
| 437 | |
andrew@webrtc.org | 755b04a | 2011-11-15 16:57:56 +0000 | [diff] [blame] | 438 | void AudioBuffer::InterleaveTo(AudioFrame* frame, bool data_changed) const { |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 439 | assert(proc_samples_per_channel_ == output_samples_per_channel_); |
| 440 | assert(num_proc_channels_ == num_input_channels_); |
| 441 | assert(frame->num_channels_ == num_proc_channels_); |
| 442 | assert(frame->samples_per_channel_ == proc_samples_per_channel_); |
andrew@webrtc.org | 63a5098 | 2012-05-02 23:56:37 +0000 | [diff] [blame] | 443 | frame->vad_activity_ = activity_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 444 | |
andrew@webrtc.org | 755b04a | 2011-11-15 16:57:56 +0000 | [diff] [blame] | 445 | if (!data_changed) { |
| 446 | return; |
| 447 | } |
| 448 | |
mflodman@webrtc.org | d5da250 | 2014-05-15 11:17:21 +0000 | [diff] [blame^] | 449 | if (data_) { |
| 450 | assert(num_proc_channels_ == 1); |
kwiberg@webrtc.org | 4cc7636 | 2014-05-08 07:10:11 +0000 | [diff] [blame] | 451 | assert(data_ == frame->data_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 452 | return; |
| 453 | } |
| 454 | |
andrew@webrtc.org | 63a5098 | 2012-05-02 23:56:37 +0000 | [diff] [blame] | 455 | int16_t* interleaved = frame->data_; |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 456 | for (int i = 0; i < num_proc_channels_; i++) { |
mflodman@webrtc.org | d5da250 | 2014-05-15 11:17:21 +0000 | [diff] [blame^] | 457 | int16_t* deinterleaved = channels_->ibuf()->channel(i); |
andrew@webrtc.org | ed083d4 | 2011-09-19 15:28:51 +0000 | [diff] [blame] | 458 | int interleaved_idx = i; |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 459 | for (int j = 0; j < proc_samples_per_channel_; j++) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 460 | interleaved[interleaved_idx] = deinterleaved[j]; |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 461 | interleaved_idx += num_proc_channels_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 462 | } |
| 463 | } |
| 464 | } |
| 465 | |
andrew@webrtc.org | 755b04a | 2011-11-15 16:57:56 +0000 | [diff] [blame] | 466 | void AudioBuffer::CopyAndMix(int num_mixed_channels) { |
| 467 | // We currently only support the stereo to mono case. |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 468 | assert(num_proc_channels_ == 2); |
andrew@webrtc.org | 755b04a | 2011-11-15 16:57:56 +0000 | [diff] [blame] | 469 | assert(num_mixed_channels == 1); |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 470 | if (!mixed_channels_.get()) { |
| 471 | mixed_channels_.reset( |
| 472 | new ChannelBuffer<int16_t>(proc_samples_per_channel_, |
| 473 | num_mixed_channels)); |
| 474 | } |
andrew@webrtc.org | 755b04a | 2011-11-15 16:57:56 +0000 | [diff] [blame] | 475 | |
mflodman@webrtc.org | d5da250 | 2014-05-15 11:17:21 +0000 | [diff] [blame^] | 476 | StereoToMono(channels_->ibuf()->channel(0), |
| 477 | channels_->ibuf()->channel(1), |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 478 | mixed_channels_->channel(0), |
| 479 | proc_samples_per_channel_); |
andrew@webrtc.org | 755b04a | 2011-11-15 16:57:56 +0000 | [diff] [blame] | 480 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 481 | num_mixed_channels_ = num_mixed_channels; |
| 482 | } |
| 483 | |
andrew@webrtc.org | ed083d4 | 2011-09-19 15:28:51 +0000 | [diff] [blame] | 484 | void AudioBuffer::CopyAndMixLowPass(int num_mixed_channels) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 485 | // We currently only support the stereo to mono case. |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 486 | assert(num_proc_channels_ == 2); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 487 | assert(num_mixed_channels == 1); |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 488 | if (!mixed_low_pass_channels_.get()) { |
| 489 | mixed_low_pass_channels_.reset( |
| 490 | new ChannelBuffer<int16_t>(samples_per_split_channel_, |
| 491 | num_mixed_channels)); |
| 492 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 493 | |
| 494 | StereoToMono(low_pass_split_data(0), |
| 495 | low_pass_split_data(1), |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 496 | mixed_low_pass_channels_->channel(0), |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 497 | samples_per_split_channel_); |
| 498 | |
| 499 | num_mixed_low_pass_channels_ = num_mixed_channels; |
| 500 | } |
| 501 | |
| 502 | void AudioBuffer::CopyLowPassToReference() { |
| 503 | reference_copied_ = true; |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 504 | if (!low_pass_reference_channels_.get()) { |
| 505 | low_pass_reference_channels_.reset( |
| 506 | new ChannelBuffer<int16_t>(samples_per_split_channel_, |
| 507 | num_proc_channels_)); |
| 508 | } |
| 509 | for (int i = 0; i < num_proc_channels_; i++) { |
| 510 | low_pass_reference_channels_->CopyFrom(low_pass_split_data(i), i); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 511 | } |
| 512 | } |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 513 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 514 | } // namespace webrtc |