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 | |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 71 | class SplitChannelBuffer { |
| 72 | public: |
| 73 | SplitChannelBuffer(int samples_per_split_channel, int num_channels) |
| 74 | : low_(samples_per_split_channel, num_channels), |
| 75 | high_(samples_per_split_channel, num_channels) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 76 | } |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 77 | ~SplitChannelBuffer() {} |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 78 | |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 79 | int16_t* low_channel(int i) { return low_.channel(i); } |
| 80 | int16_t* high_channel(int i) { return high_.channel(i); } |
| 81 | |
| 82 | private: |
| 83 | ChannelBuffer<int16_t> low_; |
| 84 | ChannelBuffer<int16_t> high_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 85 | }; |
| 86 | |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 87 | AudioBuffer::AudioBuffer(int input_samples_per_channel, |
| 88 | int num_input_channels, |
| 89 | int process_samples_per_channel, |
| 90 | int num_process_channels, |
| 91 | int output_samples_per_channel) |
| 92 | : input_samples_per_channel_(input_samples_per_channel), |
| 93 | num_input_channels_(num_input_channels), |
| 94 | proc_samples_per_channel_(process_samples_per_channel), |
| 95 | num_proc_channels_(num_process_channels), |
| 96 | output_samples_per_channel_(output_samples_per_channel), |
| 97 | samples_per_split_channel_(proc_samples_per_channel_), |
andrew@webrtc.org | ed083d4 | 2011-09-19 15:28:51 +0000 | [diff] [blame] | 98 | num_mixed_channels_(0), |
| 99 | num_mixed_low_pass_channels_(0), |
andrew@webrtc.org | 755b04a | 2011-11-15 16:57:56 +0000 | [diff] [blame] | 100 | data_was_mixed_(false), |
andrew@webrtc.org | ed083d4 | 2011-09-19 15:28:51 +0000 | [diff] [blame] | 101 | reference_copied_(false), |
| 102 | activity_(AudioFrame::kVadUnknown), |
andrew@webrtc.org | 755b04a | 2011-11-15 16:57:56 +0000 | [diff] [blame] | 103 | is_muted_(false), |
andrew@webrtc.org | ed083d4 | 2011-09-19 15:28:51 +0000 | [diff] [blame] | 104 | data_(NULL), |
andrew@webrtc.org | 103657b | 2014-04-24 18:28:56 +0000 | [diff] [blame] | 105 | keyboard_data_(NULL), |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 106 | channels_(new ChannelBuffer<int16_t>(proc_samples_per_channel_, |
| 107 | num_proc_channels_)) { |
| 108 | assert(input_samples_per_channel_ > 0); |
| 109 | assert(proc_samples_per_channel_ > 0); |
| 110 | assert(output_samples_per_channel_ > 0); |
| 111 | assert(num_input_channels_ > 0 && num_input_channels_ <= 2); |
| 112 | assert(num_proc_channels_ <= num_input_channels); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 113 | |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 114 | if (num_input_channels_ == 2 && num_proc_channels_ == 1) { |
| 115 | input_buffer_.reset(new ChannelBuffer<float>(input_samples_per_channel_, |
| 116 | num_proc_channels_)); |
| 117 | } |
| 118 | |
| 119 | if (input_samples_per_channel_ != proc_samples_per_channel_ || |
| 120 | output_samples_per_channel_ != proc_samples_per_channel_) { |
| 121 | // Create an intermediate buffer for resampling. |
| 122 | process_buffer_.reset(new ChannelBuffer<float>(proc_samples_per_channel_, |
| 123 | num_proc_channels_)); |
| 124 | } |
| 125 | |
| 126 | if (input_samples_per_channel_ != proc_samples_per_channel_) { |
| 127 | input_resamplers_.reserve(num_proc_channels_); |
| 128 | for (int i = 0; i < num_proc_channels_; ++i) { |
| 129 | input_resamplers_.push_back( |
| 130 | new PushSincResampler(input_samples_per_channel_, |
| 131 | proc_samples_per_channel_)); |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | if (output_samples_per_channel_ != proc_samples_per_channel_) { |
| 136 | output_resamplers_.reserve(num_proc_channels_); |
| 137 | for (int i = 0; i < num_proc_channels_; ++i) { |
| 138 | output_resamplers_.push_back( |
| 139 | new PushSincResampler(proc_samples_per_channel_, |
| 140 | output_samples_per_channel_)); |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | if (proc_samples_per_channel_ == kSamplesPer32kHzChannel) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 145 | samples_per_split_channel_ = kSamplesPer16kHzChannel; |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 146 | split_channels_.reset(new SplitChannelBuffer(samples_per_split_channel_, |
| 147 | num_proc_channels_)); |
| 148 | filter_states_.reset(new SplitFilterStates[num_proc_channels_]); |
| 149 | } |
| 150 | } |
| 151 | |
andrew@webrtc.org | 103657b | 2014-04-24 18:28:56 +0000 | [diff] [blame] | 152 | AudioBuffer::~AudioBuffer() {} |
| 153 | |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 154 | void AudioBuffer::CopyFrom(const float* const* data, |
| 155 | int samples_per_channel, |
| 156 | AudioProcessing::ChannelLayout layout) { |
| 157 | assert(samples_per_channel == input_samples_per_channel_); |
| 158 | assert(ChannelsFromLayout(layout) == num_input_channels_); |
| 159 | InitForNewData(); |
| 160 | |
andrew@webrtc.org | 103657b | 2014-04-24 18:28:56 +0000 | [diff] [blame] | 161 | if (HasKeyboardChannel(layout)) { |
| 162 | keyboard_data_ = data[KeyboardChannelIndex(layout)]; |
| 163 | } |
| 164 | |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 165 | // Downmix. |
| 166 | const float* const* data_ptr = data; |
| 167 | if (num_input_channels_ == 2 && num_proc_channels_ == 1) { |
| 168 | StereoToMono(data[0], |
| 169 | data[1], |
| 170 | input_buffer_->channel(0), |
| 171 | input_samples_per_channel_); |
| 172 | data_ptr = input_buffer_->channels(); |
| 173 | } |
| 174 | |
| 175 | // Resample. |
| 176 | if (input_samples_per_channel_ != proc_samples_per_channel_) { |
| 177 | for (int i = 0; i < num_proc_channels_; ++i) { |
| 178 | input_resamplers_[i]->Resample(data_ptr[i], |
| 179 | input_samples_per_channel_, |
| 180 | process_buffer_->channel(i), |
| 181 | proc_samples_per_channel_); |
| 182 | } |
| 183 | data_ptr = process_buffer_->channels(); |
| 184 | } |
| 185 | |
| 186 | // Convert to int16. |
| 187 | for (int i = 0; i < num_proc_channels_; ++i) { |
| 188 | ScaleAndRoundToInt16(data_ptr[i], proc_samples_per_channel_, |
| 189 | channels_->channel(i)); |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | void AudioBuffer::CopyTo(int samples_per_channel, |
| 194 | AudioProcessing::ChannelLayout layout, |
| 195 | float* const* data) { |
| 196 | assert(samples_per_channel == output_samples_per_channel_); |
| 197 | assert(ChannelsFromLayout(layout) == num_proc_channels_); |
| 198 | |
| 199 | // Convert to float. |
| 200 | float* const* data_ptr = data; |
| 201 | if (output_samples_per_channel_ != proc_samples_per_channel_) { |
| 202 | // Convert to an intermediate buffer for subsequent resampling. |
| 203 | data_ptr = process_buffer_->channels(); |
| 204 | } |
| 205 | for (int i = 0; i < num_proc_channels_; ++i) { |
| 206 | ScaleToFloat(channels_->channel(i), proc_samples_per_channel_, data_ptr[i]); |
| 207 | } |
| 208 | |
| 209 | // Resample. |
| 210 | if (output_samples_per_channel_ != proc_samples_per_channel_) { |
| 211 | for (int i = 0; i < num_proc_channels_; ++i) { |
| 212 | output_resamplers_[i]->Resample(data_ptr[i], |
| 213 | proc_samples_per_channel_, |
| 214 | data[i], |
| 215 | output_samples_per_channel_); |
| 216 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 217 | } |
| 218 | } |
| 219 | |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 220 | void AudioBuffer::InitForNewData() { |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 221 | data_ = NULL; |
andrew@webrtc.org | 103657b | 2014-04-24 18:28:56 +0000 | [diff] [blame] | 222 | keyboard_data_ = NULL; |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 223 | data_was_mixed_ = false; |
| 224 | num_mixed_channels_ = 0; |
| 225 | num_mixed_low_pass_channels_ = 0; |
| 226 | reference_copied_ = false; |
| 227 | activity_ = AudioFrame::kVadUnknown; |
| 228 | is_muted_ = false; |
| 229 | } |
| 230 | |
andrew@webrtc.org | 65f9338 | 2014-04-30 16:44:13 +0000 | [diff] [blame^] | 231 | const int16_t* AudioBuffer::data(int channel) const { |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 232 | assert(channel >= 0 && channel < num_proc_channels_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 233 | if (data_ != NULL) { |
| 234 | return data_; |
| 235 | } |
| 236 | |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 237 | return channels_->channel(channel); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 238 | } |
| 239 | |
andrew@webrtc.org | 65f9338 | 2014-04-30 16:44:13 +0000 | [diff] [blame^] | 240 | int16_t* AudioBuffer::data(int channel) { |
| 241 | const AudioBuffer* t = this; |
| 242 | return const_cast<int16_t*>(t->data(channel)); |
| 243 | } |
| 244 | |
| 245 | const int16_t* AudioBuffer::low_pass_split_data(int channel) const { |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 246 | assert(channel >= 0 && channel < num_proc_channels_); |
andrew@webrtc.org | 755b04a | 2011-11-15 16:57:56 +0000 | [diff] [blame] | 247 | if (split_channels_.get() == NULL) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 248 | return data(channel); |
| 249 | } |
| 250 | |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 251 | return split_channels_->low_channel(channel); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 252 | } |
| 253 | |
andrew@webrtc.org | 65f9338 | 2014-04-30 16:44:13 +0000 | [diff] [blame^] | 254 | int16_t* AudioBuffer::low_pass_split_data(int channel) { |
| 255 | const AudioBuffer* t = this; |
| 256 | return const_cast<int16_t*>(t->low_pass_split_data(channel)); |
| 257 | } |
| 258 | |
| 259 | const int16_t* AudioBuffer::high_pass_split_data(int channel) const { |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 260 | assert(channel >= 0 && channel < num_proc_channels_); |
andrew@webrtc.org | 755b04a | 2011-11-15 16:57:56 +0000 | [diff] [blame] | 261 | if (split_channels_.get() == NULL) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 262 | return NULL; |
| 263 | } |
| 264 | |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 265 | return split_channels_->high_channel(channel); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 266 | } |
| 267 | |
andrew@webrtc.org | 65f9338 | 2014-04-30 16:44:13 +0000 | [diff] [blame^] | 268 | int16_t* AudioBuffer::high_pass_split_data(int channel) { |
| 269 | const AudioBuffer* t = this; |
| 270 | return const_cast<int16_t*>(t->high_pass_split_data(channel)); |
| 271 | } |
| 272 | |
| 273 | const int16_t* AudioBuffer::mixed_data(int channel) const { |
andrew@webrtc.org | 755b04a | 2011-11-15 16:57:56 +0000 | [diff] [blame] | 274 | assert(channel >= 0 && channel < num_mixed_channels_); |
| 275 | |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 276 | return mixed_channels_->channel(channel); |
andrew@webrtc.org | 755b04a | 2011-11-15 16:57:56 +0000 | [diff] [blame] | 277 | } |
| 278 | |
andrew@webrtc.org | 65f9338 | 2014-04-30 16:44:13 +0000 | [diff] [blame^] | 279 | const int16_t* AudioBuffer::mixed_low_pass_data(int channel) const { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 280 | assert(channel >= 0 && channel < num_mixed_low_pass_channels_); |
| 281 | |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 282 | return mixed_low_pass_channels_->channel(channel); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 283 | } |
| 284 | |
andrew@webrtc.org | 65f9338 | 2014-04-30 16:44:13 +0000 | [diff] [blame^] | 285 | const int16_t* AudioBuffer::low_pass_reference(int channel) const { |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 286 | assert(channel >= 0 && channel < num_proc_channels_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 287 | if (!reference_copied_) { |
| 288 | return NULL; |
| 289 | } |
| 290 | |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 291 | return low_pass_reference_channels_->channel(channel); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 292 | } |
| 293 | |
andrew@webrtc.org | 103657b | 2014-04-24 18:28:56 +0000 | [diff] [blame] | 294 | const float* AudioBuffer::keyboard_data() const { |
| 295 | return keyboard_data_; |
| 296 | } |
| 297 | |
andrew@webrtc.org | 65f9338 | 2014-04-30 16:44:13 +0000 | [diff] [blame^] | 298 | SplitFilterStates* AudioBuffer::filter_states(int channel) { |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 299 | assert(channel >= 0 && channel < num_proc_channels_); |
| 300 | return &filter_states_[channel]; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 301 | } |
| 302 | |
andrew@webrtc.org | ed083d4 | 2011-09-19 15:28:51 +0000 | [diff] [blame] | 303 | void AudioBuffer::set_activity(AudioFrame::VADActivity activity) { |
| 304 | activity_ = activity; |
| 305 | } |
| 306 | |
andrew@webrtc.org | 755b04a | 2011-11-15 16:57:56 +0000 | [diff] [blame] | 307 | AudioFrame::VADActivity AudioBuffer::activity() const { |
andrew@webrtc.org | ed083d4 | 2011-09-19 15:28:51 +0000 | [diff] [blame] | 308 | return activity_; |
| 309 | } |
| 310 | |
andrew@webrtc.org | 755b04a | 2011-11-15 16:57:56 +0000 | [diff] [blame] | 311 | bool AudioBuffer::is_muted() const { |
| 312 | return is_muted_; |
| 313 | } |
| 314 | |
andrew@webrtc.org | ed083d4 | 2011-09-19 15:28:51 +0000 | [diff] [blame] | 315 | int AudioBuffer::num_channels() const { |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 316 | return num_proc_channels_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 317 | } |
| 318 | |
andrew@webrtc.org | ed083d4 | 2011-09-19 15:28:51 +0000 | [diff] [blame] | 319 | int AudioBuffer::samples_per_channel() const { |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 320 | return proc_samples_per_channel_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 321 | } |
| 322 | |
andrew@webrtc.org | ed083d4 | 2011-09-19 15:28:51 +0000 | [diff] [blame] | 323 | int AudioBuffer::samples_per_split_channel() const { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 324 | return samples_per_split_channel_; |
| 325 | } |
| 326 | |
andrew@webrtc.org | 103657b | 2014-04-24 18:28:56 +0000 | [diff] [blame] | 327 | int AudioBuffer::samples_per_keyboard_channel() const { |
| 328 | // We don't resample the keyboard channel. |
| 329 | return input_samples_per_channel_; |
| 330 | } |
| 331 | |
andrew@webrtc.org | ed083d4 | 2011-09-19 15:28:51 +0000 | [diff] [blame] | 332 | // TODO(andrew): Do deinterleaving and mixing in one step? |
| 333 | void AudioBuffer::DeinterleaveFrom(AudioFrame* frame) { |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 334 | assert(proc_samples_per_channel_ == input_samples_per_channel_); |
| 335 | assert(num_proc_channels_ == num_input_channels_); |
| 336 | assert(frame->num_channels_ == num_proc_channels_); |
| 337 | assert(frame->samples_per_channel_ == proc_samples_per_channel_); |
| 338 | InitForNewData(); |
andrew@webrtc.org | 63a5098 | 2012-05-02 23:56:37 +0000 | [diff] [blame] | 339 | activity_ = frame->vad_activity_; |
andrew@webrtc.org | 63a5098 | 2012-05-02 23:56:37 +0000 | [diff] [blame] | 340 | if (frame->energy_ == 0) { |
andrew@webrtc.org | 755b04a | 2011-11-15 16:57:56 +0000 | [diff] [blame] | 341 | is_muted_ = true; |
| 342 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 343 | |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 344 | if (num_proc_channels_ == 1) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 345 | // We can get away with a pointer assignment in this case. |
andrew@webrtc.org | 63a5098 | 2012-05-02 23:56:37 +0000 | [diff] [blame] | 346 | data_ = frame->data_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 347 | return; |
| 348 | } |
| 349 | |
andrew@webrtc.org | 63a5098 | 2012-05-02 23:56:37 +0000 | [diff] [blame] | 350 | int16_t* interleaved = frame->data_; |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 351 | for (int i = 0; i < num_proc_channels_; i++) { |
| 352 | int16_t* deinterleaved = channels_->channel(i); |
andrew@webrtc.org | ed083d4 | 2011-09-19 15:28:51 +0000 | [diff] [blame] | 353 | int interleaved_idx = i; |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 354 | for (int j = 0; j < proc_samples_per_channel_; j++) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 355 | deinterleaved[j] = interleaved[interleaved_idx]; |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 356 | interleaved_idx += num_proc_channels_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 357 | } |
| 358 | } |
| 359 | } |
| 360 | |
andrew@webrtc.org | 755b04a | 2011-11-15 16:57:56 +0000 | [diff] [blame] | 361 | void AudioBuffer::InterleaveTo(AudioFrame* frame, bool data_changed) const { |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 362 | assert(proc_samples_per_channel_ == output_samples_per_channel_); |
| 363 | assert(num_proc_channels_ == num_input_channels_); |
| 364 | assert(frame->num_channels_ == num_proc_channels_); |
| 365 | assert(frame->samples_per_channel_ == proc_samples_per_channel_); |
andrew@webrtc.org | 63a5098 | 2012-05-02 23:56:37 +0000 | [diff] [blame] | 366 | frame->vad_activity_ = activity_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 367 | |
andrew@webrtc.org | 755b04a | 2011-11-15 16:57:56 +0000 | [diff] [blame] | 368 | if (!data_changed) { |
| 369 | return; |
| 370 | } |
| 371 | |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 372 | if (num_proc_channels_ == 1) { |
andrew@webrtc.org | 755b04a | 2011-11-15 16:57:56 +0000 | [diff] [blame] | 373 | if (data_was_mixed_) { |
andrew@webrtc.org | 63a5098 | 2012-05-02 23:56:37 +0000 | [diff] [blame] | 374 | memcpy(frame->data_, |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 375 | channels_->channel(0), |
| 376 | sizeof(int16_t) * proc_samples_per_channel_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 377 | } else { |
| 378 | // These should point to the same buffer in this case. |
andrew@webrtc.org | 63a5098 | 2012-05-02 23:56:37 +0000 | [diff] [blame] | 379 | assert(data_ == frame->data_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | return; |
| 383 | } |
| 384 | |
andrew@webrtc.org | 63a5098 | 2012-05-02 23:56:37 +0000 | [diff] [blame] | 385 | int16_t* interleaved = frame->data_; |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 386 | for (int i = 0; i < num_proc_channels_; i++) { |
| 387 | int16_t* deinterleaved = channels_->channel(i); |
andrew@webrtc.org | ed083d4 | 2011-09-19 15:28:51 +0000 | [diff] [blame] | 388 | int interleaved_idx = i; |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 389 | for (int j = 0; j < proc_samples_per_channel_; j++) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 390 | interleaved[interleaved_idx] = deinterleaved[j]; |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 391 | interleaved_idx += num_proc_channels_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 392 | } |
| 393 | } |
| 394 | } |
| 395 | |
andrew@webrtc.org | 755b04a | 2011-11-15 16:57:56 +0000 | [diff] [blame] | 396 | void AudioBuffer::CopyAndMix(int num_mixed_channels) { |
| 397 | // We currently only support the stereo to mono case. |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 398 | assert(num_proc_channels_ == 2); |
andrew@webrtc.org | 755b04a | 2011-11-15 16:57:56 +0000 | [diff] [blame] | 399 | assert(num_mixed_channels == 1); |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 400 | if (!mixed_channels_.get()) { |
| 401 | mixed_channels_.reset( |
| 402 | new ChannelBuffer<int16_t>(proc_samples_per_channel_, |
| 403 | num_mixed_channels)); |
| 404 | } |
andrew@webrtc.org | 755b04a | 2011-11-15 16:57:56 +0000 | [diff] [blame] | 405 | |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 406 | StereoToMono(channels_->channel(0), |
| 407 | channels_->channel(1), |
| 408 | mixed_channels_->channel(0), |
| 409 | proc_samples_per_channel_); |
andrew@webrtc.org | 755b04a | 2011-11-15 16:57:56 +0000 | [diff] [blame] | 410 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 411 | num_mixed_channels_ = num_mixed_channels; |
| 412 | } |
| 413 | |
andrew@webrtc.org | ed083d4 | 2011-09-19 15:28:51 +0000 | [diff] [blame] | 414 | void AudioBuffer::CopyAndMixLowPass(int num_mixed_channels) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 415 | // We currently only support the stereo to mono case. |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 416 | assert(num_proc_channels_ == 2); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 417 | assert(num_mixed_channels == 1); |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 418 | if (!mixed_low_pass_channels_.get()) { |
| 419 | mixed_low_pass_channels_.reset( |
| 420 | new ChannelBuffer<int16_t>(samples_per_split_channel_, |
| 421 | num_mixed_channels)); |
| 422 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 423 | |
| 424 | StereoToMono(low_pass_split_data(0), |
| 425 | low_pass_split_data(1), |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 426 | mixed_low_pass_channels_->channel(0), |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 427 | samples_per_split_channel_); |
| 428 | |
| 429 | num_mixed_low_pass_channels_ = num_mixed_channels; |
| 430 | } |
| 431 | |
| 432 | void AudioBuffer::CopyLowPassToReference() { |
| 433 | reference_copied_ = true; |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 434 | if (!low_pass_reference_channels_.get()) { |
| 435 | low_pass_reference_channels_.reset( |
| 436 | new ChannelBuffer<int16_t>(samples_per_split_channel_, |
| 437 | num_proc_channels_)); |
| 438 | } |
| 439 | for (int i = 0; i < num_proc_channels_; i++) { |
| 440 | low_pass_reference_channels_->CopyFrom(low_pass_split_data(i), i); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 441 | } |
| 442 | } |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 443 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 444 | } // namespace webrtc |