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