niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1 | /* |
andrew@webrtc.org | 63a5098 | 2012-05-02 23:56:37 +0000 | [diff] [blame] | 2 | * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
pbos@webrtc.org | 7fad4b8 | 2013-05-28 08:11:59 +0000 | [diff] [blame] | 11 | #include "webrtc/modules/audio_processing/audio_buffer.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 12 | |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 13 | #include "webrtc/common_audio/include/audio_util.h" |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 14 | #include "webrtc/common_audio/resampler/push_sinc_resampler.h" |
pbos@webrtc.org | 7fad4b8 | 2013-05-28 08:11:59 +0000 | [diff] [blame] | 15 | #include "webrtc/common_audio/signal_processing/include/signal_processing_library.h" |
andrew@webrtc.org | 755b04a | 2011-11-15 16:57:56 +0000 | [diff] [blame] | 16 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 17 | namespace webrtc { |
| 18 | namespace { |
| 19 | |
| 20 | enum { |
| 21 | kSamplesPer8kHzChannel = 80, |
| 22 | kSamplesPer16kHzChannel = 160, |
| 23 | kSamplesPer32kHzChannel = 320 |
| 24 | }; |
| 25 | |
andrew@webrtc.org | 103657b | 2014-04-24 18:28:56 +0000 | [diff] [blame] | 26 | bool HasKeyboardChannel(AudioProcessing::ChannelLayout layout) { |
| 27 | switch (layout) { |
| 28 | case AudioProcessing::kMono: |
| 29 | case AudioProcessing::kStereo: |
| 30 | return false; |
| 31 | case AudioProcessing::kMonoAndKeyboard: |
| 32 | case AudioProcessing::kStereoAndKeyboard: |
| 33 | return true; |
| 34 | } |
| 35 | assert(false); |
| 36 | return false; |
| 37 | } |
| 38 | |
| 39 | int KeyboardChannelIndex(AudioProcessing::ChannelLayout layout) { |
| 40 | switch (layout) { |
| 41 | case AudioProcessing::kMono: |
| 42 | case AudioProcessing::kStereo: |
| 43 | assert(false); |
| 44 | return -1; |
| 45 | case AudioProcessing::kMonoAndKeyboard: |
| 46 | return 1; |
| 47 | case AudioProcessing::kStereoAndKeyboard: |
| 48 | return 2; |
| 49 | } |
| 50 | assert(false); |
| 51 | return -1; |
| 52 | } |
| 53 | |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 54 | void StereoToMono(const float* left, const float* right, float* out, |
| 55 | int samples_per_channel) { |
| 56 | for (int i = 0; i < samples_per_channel; ++i) { |
| 57 | out[i] = (left[i] + right[i]) / 2; |
| 58 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 59 | } |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 60 | |
| 61 | void StereoToMono(const int16_t* left, const int16_t* right, int16_t* out, |
| 62 | int samples_per_channel) { |
andrew@webrtc.org | 103657b | 2014-04-24 18:28:56 +0000 | [diff] [blame] | 63 | for (int i = 0; i < samples_per_channel; ++i) { |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 64 | out[i] = (left[i] + right[i]) >> 1; |
andrew@webrtc.org | 103657b | 2014-04-24 18:28:56 +0000 | [diff] [blame] | 65 | } |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 66 | } |
| 67 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 68 | } // namespace |
| 69 | |
mflodman@webrtc.org | d5da250 | 2014-05-15 11:17:21 +0000 | [diff] [blame] | 70 | // One int16_t and one float ChannelBuffer that are kept in sync. The sync is |
| 71 | // broken when someone requests write access to either ChannelBuffer, and |
| 72 | // reestablished when someone requests the outdated ChannelBuffer. It is |
| 73 | // therefore safe to use the return value of ibuf() and fbuf() until the next |
| 74 | // call to the other method. |
| 75 | class IFChannelBuffer { |
| 76 | public: |
| 77 | IFChannelBuffer(int samples_per_channel, int num_channels) |
| 78 | : ivalid_(true), |
| 79 | ibuf_(samples_per_channel, num_channels), |
| 80 | fvalid_(true), |
| 81 | fbuf_(samples_per_channel, num_channels) {} |
| 82 | |
| 83 | ChannelBuffer<int16_t>* ibuf() { |
| 84 | RefreshI(); |
| 85 | fvalid_ = false; |
| 86 | return &ibuf_; |
| 87 | } |
| 88 | |
| 89 | ChannelBuffer<float>* fbuf() { |
| 90 | RefreshF(); |
| 91 | ivalid_ = false; |
| 92 | return &fbuf_; |
| 93 | } |
| 94 | |
| 95 | private: |
| 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_); |
| 111 | const float* const float_data = fbuf_.data(); |
| 112 | int16_t* const int_data = ibuf_.data(); |
| 113 | const int length = ibuf_.length(); |
| 114 | for (int i = 0; i < length; ++i) |
| 115 | int_data[i] = WEBRTC_SPL_SAT(std::numeric_limits<int16_t>::max(), |
| 116 | float_data[i], |
| 117 | std::numeric_limits<int16_t>::min()); |
| 118 | ivalid_ = true; |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | bool ivalid_; |
| 123 | ChannelBuffer<int16_t> ibuf_; |
| 124 | bool fvalid_; |
| 125 | ChannelBuffer<float> fbuf_; |
| 126 | }; |
| 127 | |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 128 | class SplitChannelBuffer { |
| 129 | public: |
| 130 | SplitChannelBuffer(int samples_per_split_channel, int num_channels) |
| 131 | : low_(samples_per_split_channel, num_channels), |
| 132 | high_(samples_per_split_channel, num_channels) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 133 | } |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 134 | ~SplitChannelBuffer() {} |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 135 | |
mflodman@webrtc.org | d5da250 | 2014-05-15 11:17:21 +0000 | [diff] [blame] | 136 | int16_t* low_channel(int i) { return low_.ibuf()->channel(i); } |
| 137 | int16_t* high_channel(int i) { return high_.ibuf()->channel(i); } |
| 138 | float* low_channel_f(int i) { return low_.fbuf()->channel(i); } |
| 139 | float* high_channel_f(int i) { return high_.fbuf()->channel(i); } |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 140 | |
| 141 | private: |
mflodman@webrtc.org | d5da250 | 2014-05-15 11:17:21 +0000 | [diff] [blame] | 142 | IFChannelBuffer low_; |
| 143 | IFChannelBuffer high_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 144 | }; |
| 145 | |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 146 | AudioBuffer::AudioBuffer(int input_samples_per_channel, |
| 147 | int num_input_channels, |
| 148 | int process_samples_per_channel, |
| 149 | int num_process_channels, |
| 150 | int output_samples_per_channel) |
| 151 | : input_samples_per_channel_(input_samples_per_channel), |
| 152 | num_input_channels_(num_input_channels), |
| 153 | proc_samples_per_channel_(process_samples_per_channel), |
| 154 | num_proc_channels_(num_process_channels), |
| 155 | output_samples_per_channel_(output_samples_per_channel), |
| 156 | samples_per_split_channel_(proc_samples_per_channel_), |
aluebs@webrtc.org | 2561d52 | 2014-07-17 08:27:39 +0000 | [diff] [blame^] | 157 | mixed_low_pass_valid_(false), |
andrew@webrtc.org | ed083d4 | 2011-09-19 15:28:51 +0000 | [diff] [blame] | 158 | reference_copied_(false), |
| 159 | activity_(AudioFrame::kVadUnknown), |
andrew@webrtc.org | 103657b | 2014-04-24 18:28:56 +0000 | [diff] [blame] | 160 | keyboard_data_(NULL), |
mflodman@webrtc.org | d5da250 | 2014-05-15 11:17:21 +0000 | [diff] [blame] | 161 | channels_(new IFChannelBuffer(proc_samples_per_channel_, |
| 162 | num_proc_channels_)) { |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 163 | assert(input_samples_per_channel_ > 0); |
| 164 | assert(proc_samples_per_channel_ > 0); |
| 165 | assert(output_samples_per_channel_ > 0); |
| 166 | assert(num_input_channels_ > 0 && num_input_channels_ <= 2); |
| 167 | assert(num_proc_channels_ <= num_input_channels); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 168 | |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 169 | if (num_input_channels_ == 2 && num_proc_channels_ == 1) { |
| 170 | input_buffer_.reset(new ChannelBuffer<float>(input_samples_per_channel_, |
| 171 | num_proc_channels_)); |
| 172 | } |
| 173 | |
| 174 | if (input_samples_per_channel_ != proc_samples_per_channel_ || |
| 175 | output_samples_per_channel_ != proc_samples_per_channel_) { |
| 176 | // Create an intermediate buffer for resampling. |
| 177 | process_buffer_.reset(new ChannelBuffer<float>(proc_samples_per_channel_, |
| 178 | num_proc_channels_)); |
| 179 | } |
| 180 | |
| 181 | if (input_samples_per_channel_ != proc_samples_per_channel_) { |
| 182 | input_resamplers_.reserve(num_proc_channels_); |
| 183 | for (int i = 0; i < num_proc_channels_; ++i) { |
| 184 | input_resamplers_.push_back( |
| 185 | new PushSincResampler(input_samples_per_channel_, |
| 186 | proc_samples_per_channel_)); |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | if (output_samples_per_channel_ != proc_samples_per_channel_) { |
| 191 | output_resamplers_.reserve(num_proc_channels_); |
| 192 | for (int i = 0; i < num_proc_channels_; ++i) { |
| 193 | output_resamplers_.push_back( |
| 194 | new PushSincResampler(proc_samples_per_channel_, |
| 195 | output_samples_per_channel_)); |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | if (proc_samples_per_channel_ == kSamplesPer32kHzChannel) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 200 | samples_per_split_channel_ = kSamplesPer16kHzChannel; |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 201 | split_channels_.reset(new SplitChannelBuffer(samples_per_split_channel_, |
| 202 | num_proc_channels_)); |
| 203 | filter_states_.reset(new SplitFilterStates[num_proc_channels_]); |
| 204 | } |
| 205 | } |
| 206 | |
andrew@webrtc.org | 103657b | 2014-04-24 18:28:56 +0000 | [diff] [blame] | 207 | AudioBuffer::~AudioBuffer() {} |
| 208 | |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 209 | void AudioBuffer::CopyFrom(const float* const* data, |
| 210 | int samples_per_channel, |
| 211 | AudioProcessing::ChannelLayout layout) { |
| 212 | assert(samples_per_channel == input_samples_per_channel_); |
| 213 | assert(ChannelsFromLayout(layout) == num_input_channels_); |
| 214 | InitForNewData(); |
| 215 | |
andrew@webrtc.org | 103657b | 2014-04-24 18:28:56 +0000 | [diff] [blame] | 216 | if (HasKeyboardChannel(layout)) { |
| 217 | keyboard_data_ = data[KeyboardChannelIndex(layout)]; |
| 218 | } |
| 219 | |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 220 | // Downmix. |
| 221 | const float* const* data_ptr = data; |
| 222 | if (num_input_channels_ == 2 && num_proc_channels_ == 1) { |
| 223 | StereoToMono(data[0], |
| 224 | data[1], |
| 225 | input_buffer_->channel(0), |
| 226 | input_samples_per_channel_); |
| 227 | data_ptr = input_buffer_->channels(); |
| 228 | } |
| 229 | |
| 230 | // Resample. |
| 231 | if (input_samples_per_channel_ != proc_samples_per_channel_) { |
| 232 | for (int i = 0; i < num_proc_channels_; ++i) { |
| 233 | input_resamplers_[i]->Resample(data_ptr[i], |
| 234 | input_samples_per_channel_, |
| 235 | process_buffer_->channel(i), |
| 236 | proc_samples_per_channel_); |
| 237 | } |
| 238 | data_ptr = process_buffer_->channels(); |
| 239 | } |
| 240 | |
| 241 | // Convert to int16. |
| 242 | for (int i = 0; i < num_proc_channels_; ++i) { |
| 243 | ScaleAndRoundToInt16(data_ptr[i], proc_samples_per_channel_, |
mflodman@webrtc.org | d5da250 | 2014-05-15 11:17:21 +0000 | [diff] [blame] | 244 | channels_->ibuf()->channel(i)); |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 245 | } |
| 246 | } |
| 247 | |
| 248 | void AudioBuffer::CopyTo(int samples_per_channel, |
| 249 | AudioProcessing::ChannelLayout layout, |
| 250 | float* const* data) { |
| 251 | assert(samples_per_channel == output_samples_per_channel_); |
| 252 | assert(ChannelsFromLayout(layout) == num_proc_channels_); |
| 253 | |
| 254 | // Convert to float. |
| 255 | float* const* data_ptr = data; |
| 256 | if (output_samples_per_channel_ != proc_samples_per_channel_) { |
| 257 | // Convert to an intermediate buffer for subsequent resampling. |
| 258 | data_ptr = process_buffer_->channels(); |
| 259 | } |
| 260 | for (int i = 0; i < num_proc_channels_; ++i) { |
mflodman@webrtc.org | d5da250 | 2014-05-15 11:17:21 +0000 | [diff] [blame] | 261 | ScaleToFloat(channels_->ibuf()->channel(i), |
| 262 | proc_samples_per_channel_, |
| 263 | data_ptr[i]); |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | // Resample. |
| 267 | if (output_samples_per_channel_ != proc_samples_per_channel_) { |
| 268 | for (int i = 0; i < num_proc_channels_; ++i) { |
| 269 | output_resamplers_[i]->Resample(data_ptr[i], |
| 270 | proc_samples_per_channel_, |
| 271 | data[i], |
| 272 | output_samples_per_channel_); |
| 273 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 274 | } |
| 275 | } |
| 276 | |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 277 | void AudioBuffer::InitForNewData() { |
andrew@webrtc.org | 103657b | 2014-04-24 18:28:56 +0000 | [diff] [blame] | 278 | keyboard_data_ = NULL; |
aluebs@webrtc.org | 2561d52 | 2014-07-17 08:27:39 +0000 | [diff] [blame^] | 279 | mixed_low_pass_valid_ = false; |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 280 | reference_copied_ = false; |
| 281 | activity_ = AudioFrame::kVadUnknown; |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 282 | } |
| 283 | |
andrew@webrtc.org | 65f9338 | 2014-04-30 16:44:13 +0000 | [diff] [blame] | 284 | const int16_t* AudioBuffer::data(int channel) const { |
mflodman@webrtc.org | d5da250 | 2014-05-15 11:17:21 +0000 | [diff] [blame] | 285 | return channels_->ibuf()->channel(channel); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 286 | } |
| 287 | |
andrew@webrtc.org | 65f9338 | 2014-04-30 16:44:13 +0000 | [diff] [blame] | 288 | int16_t* AudioBuffer::data(int channel) { |
aluebs@webrtc.org | 2561d52 | 2014-07-17 08:27:39 +0000 | [diff] [blame^] | 289 | mixed_low_pass_valid_ = false; |
andrew@webrtc.org | 65f9338 | 2014-04-30 16:44:13 +0000 | [diff] [blame] | 290 | const AudioBuffer* t = this; |
| 291 | return const_cast<int16_t*>(t->data(channel)); |
| 292 | } |
| 293 | |
kwiberg@webrtc.org | 38214d5 | 2014-07-03 09:47:33 +0000 | [diff] [blame] | 294 | const float* AudioBuffer::data_f(int channel) const { |
mflodman@webrtc.org | d5da250 | 2014-05-15 11:17:21 +0000 | [diff] [blame] | 295 | return channels_->fbuf()->channel(channel); |
| 296 | } |
| 297 | |
kwiberg@webrtc.org | 38214d5 | 2014-07-03 09:47:33 +0000 | [diff] [blame] | 298 | float* AudioBuffer::data_f(int channel) { |
aluebs@webrtc.org | 2561d52 | 2014-07-17 08:27:39 +0000 | [diff] [blame^] | 299 | mixed_low_pass_valid_ = false; |
kwiberg@webrtc.org | 38214d5 | 2014-07-03 09:47:33 +0000 | [diff] [blame] | 300 | const AudioBuffer* t = this; |
| 301 | return const_cast<float*>(t->data_f(channel)); |
| 302 | } |
| 303 | |
andrew@webrtc.org | 65f9338 | 2014-04-30 16:44:13 +0000 | [diff] [blame] | 304 | const int16_t* AudioBuffer::low_pass_split_data(int channel) const { |
kwiberg@webrtc.org | 8e4401b | 2014-06-03 10:04:13 +0000 | [diff] [blame] | 305 | return split_channels_.get() ? split_channels_->low_channel(channel) |
| 306 | : data(channel); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 307 | } |
| 308 | |
andrew@webrtc.org | 65f9338 | 2014-04-30 16:44:13 +0000 | [diff] [blame] | 309 | int16_t* AudioBuffer::low_pass_split_data(int channel) { |
aluebs@webrtc.org | 2561d52 | 2014-07-17 08:27:39 +0000 | [diff] [blame^] | 310 | mixed_low_pass_valid_ = false; |
andrew@webrtc.org | 65f9338 | 2014-04-30 16:44:13 +0000 | [diff] [blame] | 311 | const AudioBuffer* t = this; |
| 312 | return const_cast<int16_t*>(t->low_pass_split_data(channel)); |
| 313 | } |
| 314 | |
kwiberg@webrtc.org | 38214d5 | 2014-07-03 09:47:33 +0000 | [diff] [blame] | 315 | const float* AudioBuffer::low_pass_split_data_f(int channel) const { |
mflodman@webrtc.org | d5da250 | 2014-05-15 11:17:21 +0000 | [diff] [blame] | 316 | return split_channels_.get() ? split_channels_->low_channel_f(channel) |
| 317 | : data_f(channel); |
| 318 | } |
| 319 | |
kwiberg@webrtc.org | 38214d5 | 2014-07-03 09:47:33 +0000 | [diff] [blame] | 320 | float* AudioBuffer::low_pass_split_data_f(int channel) { |
aluebs@webrtc.org | 2561d52 | 2014-07-17 08:27:39 +0000 | [diff] [blame^] | 321 | mixed_low_pass_valid_ = false; |
kwiberg@webrtc.org | 38214d5 | 2014-07-03 09:47:33 +0000 | [diff] [blame] | 322 | const AudioBuffer* t = this; |
| 323 | return const_cast<float*>(t->low_pass_split_data_f(channel)); |
| 324 | } |
| 325 | |
andrew@webrtc.org | 65f9338 | 2014-04-30 16:44:13 +0000 | [diff] [blame] | 326 | const int16_t* AudioBuffer::high_pass_split_data(int channel) const { |
kwiberg@webrtc.org | 8e4401b | 2014-06-03 10:04:13 +0000 | [diff] [blame] | 327 | return split_channels_.get() ? split_channels_->high_channel(channel) : NULL; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 328 | } |
| 329 | |
andrew@webrtc.org | 65f9338 | 2014-04-30 16:44:13 +0000 | [diff] [blame] | 330 | int16_t* AudioBuffer::high_pass_split_data(int channel) { |
| 331 | const AudioBuffer* t = this; |
| 332 | return const_cast<int16_t*>(t->high_pass_split_data(channel)); |
| 333 | } |
| 334 | |
kwiberg@webrtc.org | 38214d5 | 2014-07-03 09:47:33 +0000 | [diff] [blame] | 335 | const float* AudioBuffer::high_pass_split_data_f(int channel) const { |
mflodman@webrtc.org | d5da250 | 2014-05-15 11:17:21 +0000 | [diff] [blame] | 336 | return split_channels_.get() ? split_channels_->high_channel_f(channel) |
| 337 | : NULL; |
| 338 | } |
| 339 | |
kwiberg@webrtc.org | 38214d5 | 2014-07-03 09:47:33 +0000 | [diff] [blame] | 340 | float* AudioBuffer::high_pass_split_data_f(int channel) { |
| 341 | const AudioBuffer* t = this; |
| 342 | return const_cast<float*>(t->high_pass_split_data_f(channel)); |
| 343 | } |
| 344 | |
aluebs@webrtc.org | 2561d52 | 2014-07-17 08:27:39 +0000 | [diff] [blame^] | 345 | const int16_t* AudioBuffer::mixed_low_pass_data() { |
| 346 | // Currently only mixing stereo to mono is supported. |
| 347 | assert(num_proc_channels_ == 1 || num_proc_channels_ == 2); |
andrew@webrtc.org | 755b04a | 2011-11-15 16:57:56 +0000 | [diff] [blame] | 348 | |
aluebs@webrtc.org | 2561d52 | 2014-07-17 08:27:39 +0000 | [diff] [blame^] | 349 | if (num_proc_channels_ == 1) { |
| 350 | return low_pass_split_data(0); |
| 351 | } |
| 352 | |
| 353 | if (!mixed_low_pass_valid_) { |
| 354 | if (!mixed_low_pass_channels_.get()) { |
| 355 | mixed_low_pass_channels_.reset( |
| 356 | new ChannelBuffer<int16_t>(samples_per_split_channel_, 1)); |
| 357 | } |
| 358 | StereoToMono(low_pass_split_data(0), |
| 359 | low_pass_split_data(1), |
| 360 | mixed_low_pass_channels_->data(), |
| 361 | samples_per_split_channel_); |
| 362 | mixed_low_pass_valid_ = true; |
| 363 | } |
| 364 | return mixed_low_pass_channels_->data(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 365 | } |
| 366 | |
andrew@webrtc.org | 65f9338 | 2014-04-30 16:44:13 +0000 | [diff] [blame] | 367 | const int16_t* AudioBuffer::low_pass_reference(int channel) const { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 368 | if (!reference_copied_) { |
| 369 | return NULL; |
| 370 | } |
| 371 | |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 372 | return low_pass_reference_channels_->channel(channel); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 373 | } |
| 374 | |
andrew@webrtc.org | 103657b | 2014-04-24 18:28:56 +0000 | [diff] [blame] | 375 | const float* AudioBuffer::keyboard_data() const { |
| 376 | return keyboard_data_; |
| 377 | } |
| 378 | |
andrew@webrtc.org | 65f9338 | 2014-04-30 16:44:13 +0000 | [diff] [blame] | 379 | SplitFilterStates* AudioBuffer::filter_states(int channel) { |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 380 | assert(channel >= 0 && channel < num_proc_channels_); |
| 381 | return &filter_states_[channel]; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 382 | } |
| 383 | |
andrew@webrtc.org | ed083d4 | 2011-09-19 15:28:51 +0000 | [diff] [blame] | 384 | void AudioBuffer::set_activity(AudioFrame::VADActivity activity) { |
| 385 | activity_ = activity; |
| 386 | } |
| 387 | |
andrew@webrtc.org | 755b04a | 2011-11-15 16:57:56 +0000 | [diff] [blame] | 388 | AudioFrame::VADActivity AudioBuffer::activity() const { |
andrew@webrtc.org | ed083d4 | 2011-09-19 15:28:51 +0000 | [diff] [blame] | 389 | return activity_; |
| 390 | } |
| 391 | |
| 392 | int AudioBuffer::num_channels() const { |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 393 | return num_proc_channels_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 394 | } |
| 395 | |
andrew@webrtc.org | ed083d4 | 2011-09-19 15:28:51 +0000 | [diff] [blame] | 396 | int AudioBuffer::samples_per_channel() const { |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 397 | return proc_samples_per_channel_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 398 | } |
| 399 | |
andrew@webrtc.org | ed083d4 | 2011-09-19 15:28:51 +0000 | [diff] [blame] | 400 | int AudioBuffer::samples_per_split_channel() const { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 401 | return samples_per_split_channel_; |
| 402 | } |
| 403 | |
andrew@webrtc.org | 103657b | 2014-04-24 18:28:56 +0000 | [diff] [blame] | 404 | int AudioBuffer::samples_per_keyboard_channel() const { |
| 405 | // We don't resample the keyboard channel. |
| 406 | return input_samples_per_channel_; |
| 407 | } |
| 408 | |
andrew@webrtc.org | ed083d4 | 2011-09-19 15:28:51 +0000 | [diff] [blame] | 409 | // TODO(andrew): Do deinterleaving and mixing in one step? |
| 410 | void AudioBuffer::DeinterleaveFrom(AudioFrame* frame) { |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 411 | assert(proc_samples_per_channel_ == input_samples_per_channel_); |
| 412 | assert(num_proc_channels_ == num_input_channels_); |
| 413 | assert(frame->num_channels_ == num_proc_channels_); |
| 414 | assert(frame->samples_per_channel_ == proc_samples_per_channel_); |
| 415 | InitForNewData(); |
andrew@webrtc.org | 63a5098 | 2012-05-02 23:56:37 +0000 | [diff] [blame] | 416 | activity_ = frame->vad_activity_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 417 | |
andrew@webrtc.org | 63a5098 | 2012-05-02 23:56:37 +0000 | [diff] [blame] | 418 | int16_t* interleaved = frame->data_; |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 419 | for (int i = 0; i < num_proc_channels_; i++) { |
mflodman@webrtc.org | d5da250 | 2014-05-15 11:17:21 +0000 | [diff] [blame] | 420 | int16_t* deinterleaved = channels_->ibuf()->channel(i); |
andrew@webrtc.org | ed083d4 | 2011-09-19 15:28:51 +0000 | [diff] [blame] | 421 | int interleaved_idx = i; |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 422 | for (int j = 0; j < proc_samples_per_channel_; j++) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 423 | deinterleaved[j] = interleaved[interleaved_idx]; |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 424 | interleaved_idx += num_proc_channels_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 425 | } |
| 426 | } |
| 427 | } |
| 428 | |
andrew@webrtc.org | 755b04a | 2011-11-15 16:57:56 +0000 | [diff] [blame] | 429 | void AudioBuffer::InterleaveTo(AudioFrame* frame, bool data_changed) const { |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 430 | assert(proc_samples_per_channel_ == output_samples_per_channel_); |
| 431 | assert(num_proc_channels_ == num_input_channels_); |
| 432 | assert(frame->num_channels_ == num_proc_channels_); |
| 433 | assert(frame->samples_per_channel_ == proc_samples_per_channel_); |
andrew@webrtc.org | 63a5098 | 2012-05-02 23:56:37 +0000 | [diff] [blame] | 434 | frame->vad_activity_ = activity_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 435 | |
andrew@webrtc.org | 755b04a | 2011-11-15 16:57:56 +0000 | [diff] [blame] | 436 | if (!data_changed) { |
| 437 | return; |
| 438 | } |
| 439 | |
andrew@webrtc.org | 63a5098 | 2012-05-02 23:56:37 +0000 | [diff] [blame] | 440 | int16_t* interleaved = frame->data_; |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 441 | for (int i = 0; i < num_proc_channels_; i++) { |
mflodman@webrtc.org | d5da250 | 2014-05-15 11:17:21 +0000 | [diff] [blame] | 442 | int16_t* deinterleaved = channels_->ibuf()->channel(i); |
andrew@webrtc.org | ed083d4 | 2011-09-19 15:28:51 +0000 | [diff] [blame] | 443 | int interleaved_idx = i; |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 444 | for (int j = 0; j < proc_samples_per_channel_; j++) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 445 | interleaved[interleaved_idx] = deinterleaved[j]; |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 446 | interleaved_idx += num_proc_channels_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 447 | } |
| 448 | } |
| 449 | } |
| 450 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 451 | void AudioBuffer::CopyLowPassToReference() { |
| 452 | reference_copied_ = true; |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 453 | if (!low_pass_reference_channels_.get()) { |
| 454 | low_pass_reference_channels_.reset( |
| 455 | new ChannelBuffer<int16_t>(samples_per_split_channel_, |
| 456 | num_proc_channels_)); |
| 457 | } |
| 458 | for (int i = 0; i < num_proc_channels_; i++) { |
| 459 | low_pass_reference_channels_->CopyFrom(low_pass_split_data(i), i); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 460 | } |
| 461 | } |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 462 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 463 | } // namespace webrtc |