blob: 6936155a1bd6438ff5efbb14546b6889b479b2e6 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
andrew@webrtc.org63a50982012-05-02 23:56:37 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
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.org7fad4b82013-05-28 08:11:59 +000011#include "webrtc/modules/audio_processing/audio_buffer.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000012
andrew@webrtc.org17e40642014-03-04 20:58:13 +000013#include "webrtc/common_audio/include/audio_util.h"
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000014#include "webrtc/common_audio/resampler/push_sinc_resampler.h"
pbos@webrtc.org7fad4b82013-05-28 08:11:59 +000015#include "webrtc/common_audio/signal_processing/include/signal_processing_library.h"
andrew@webrtc.org755b04a2011-11-15 16:57:56 +000016
niklase@google.com470e71d2011-07-07 08:21:25 +000017namespace webrtc {
18namespace {
19
20enum {
21 kSamplesPer8kHzChannel = 80,
22 kSamplesPer16kHzChannel = 160,
23 kSamplesPer32kHzChannel = 320
24};
25
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000026void 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.com470e71d2011-07-07 08:21:25 +000031}
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000032
33void 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.com470e71d2011-07-07 08:21:25 +000039} // namespace
40
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000041class 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.com470e71d2011-07-07 08:21:25 +000046 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000047 ~SplitChannelBuffer() {}
niklase@google.com470e71d2011-07-07 08:21:25 +000048
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000049 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.com470e71d2011-07-07 08:21:25 +000055};
56
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000057AudioBuffer::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.orged083d42011-09-19 15:28:51 +000068 num_mixed_channels_(0),
69 num_mixed_low_pass_channels_(0),
andrew@webrtc.org755b04a2011-11-15 16:57:56 +000070 data_was_mixed_(false),
andrew@webrtc.orged083d42011-09-19 15:28:51 +000071 reference_copied_(false),
72 activity_(AudioFrame::kVadUnknown),
andrew@webrtc.org755b04a2011-11-15 16:57:56 +000073 is_muted_(false),
andrew@webrtc.orged083d42011-09-19 15:28:51 +000074 data_(NULL),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000075 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.com470e71d2011-07-07 08:21:25 +000082
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000083 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.com470e71d2011-07-07 08:21:25 +0000114 samples_per_split_channel_ = kSamplesPer16kHzChannel;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000115 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
121void 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
156void 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.com470e71d2011-07-07 08:21:25 +0000180 }
181}
182
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000183AudioBuffer::~AudioBuffer() {}
niklase@google.com470e71d2011-07-07 08:21:25 +0000184
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000185void AudioBuffer::InitForNewData() {
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000186 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.org755b04a2011-11-15 16:57:56 +0000195int16_t* AudioBuffer::data(int channel) const {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000196 assert(channel >= 0 && channel < num_proc_channels_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000197 if (data_ != NULL) {
198 return data_;
199 }
200
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000201 return channels_->channel(channel);
niklase@google.com470e71d2011-07-07 08:21:25 +0000202}
203
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000204int16_t* AudioBuffer::low_pass_split_data(int channel) const {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000205 assert(channel >= 0 && channel < num_proc_channels_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000206 if (split_channels_.get() == NULL) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000207 return data(channel);
208 }
209
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000210 return split_channels_->low_channel(channel);
niklase@google.com470e71d2011-07-07 08:21:25 +0000211}
212
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000213int16_t* AudioBuffer::high_pass_split_data(int channel) const {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000214 assert(channel >= 0 && channel < num_proc_channels_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000215 if (split_channels_.get() == NULL) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000216 return NULL;
217 }
218
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000219 return split_channels_->high_channel(channel);
niklase@google.com470e71d2011-07-07 08:21:25 +0000220}
221
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000222int16_t* AudioBuffer::mixed_data(int channel) const {
223 assert(channel >= 0 && channel < num_mixed_channels_);
224
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000225 return mixed_channels_->channel(channel);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000226}
227
228int16_t* AudioBuffer::mixed_low_pass_data(int channel) const {
niklase@google.com470e71d2011-07-07 08:21:25 +0000229 assert(channel >= 0 && channel < num_mixed_low_pass_channels_);
230
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000231 return mixed_low_pass_channels_->channel(channel);
niklase@google.com470e71d2011-07-07 08:21:25 +0000232}
233
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000234int16_t* AudioBuffer::low_pass_reference(int channel) const {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000235 assert(channel >= 0 && channel < num_proc_channels_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000236 if (!reference_copied_) {
237 return NULL;
238 }
239
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000240 return low_pass_reference_channels_->channel(channel);
niklase@google.com470e71d2011-07-07 08:21:25 +0000241}
242
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000243SplitFilterStates* AudioBuffer::filter_states(int channel) const {
244 assert(channel >= 0 && channel < num_proc_channels_);
245 return &filter_states_[channel];
niklase@google.com470e71d2011-07-07 08:21:25 +0000246}
247
andrew@webrtc.orged083d42011-09-19 15:28:51 +0000248void AudioBuffer::set_activity(AudioFrame::VADActivity activity) {
249 activity_ = activity;
250}
251
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000252AudioFrame::VADActivity AudioBuffer::activity() const {
andrew@webrtc.orged083d42011-09-19 15:28:51 +0000253 return activity_;
254}
255
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000256bool AudioBuffer::is_muted() const {
257 return is_muted_;
258}
259
andrew@webrtc.orged083d42011-09-19 15:28:51 +0000260int AudioBuffer::num_channels() const {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000261 return num_proc_channels_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000262}
263
andrew@webrtc.orged083d42011-09-19 15:28:51 +0000264int AudioBuffer::samples_per_channel() const {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000265 return proc_samples_per_channel_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000266}
267
andrew@webrtc.orged083d42011-09-19 15:28:51 +0000268int AudioBuffer::samples_per_split_channel() const {
niklase@google.com470e71d2011-07-07 08:21:25 +0000269 return samples_per_split_channel_;
270}
271
andrew@webrtc.orged083d42011-09-19 15:28:51 +0000272// TODO(andrew): Do deinterleaving and mixing in one step?
273void AudioBuffer::DeinterleaveFrom(AudioFrame* frame) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000274 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.org63a50982012-05-02 23:56:37 +0000279 activity_ = frame->vad_activity_;
andrew@webrtc.org63a50982012-05-02 23:56:37 +0000280 if (frame->energy_ == 0) {
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000281 is_muted_ = true;
282 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000283
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000284 if (num_proc_channels_ == 1) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000285 // We can get away with a pointer assignment in this case.
andrew@webrtc.org63a50982012-05-02 23:56:37 +0000286 data_ = frame->data_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000287 return;
288 }
289
andrew@webrtc.org63a50982012-05-02 23:56:37 +0000290 int16_t* interleaved = frame->data_;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000291 for (int i = 0; i < num_proc_channels_; i++) {
292 int16_t* deinterleaved = channels_->channel(i);
andrew@webrtc.orged083d42011-09-19 15:28:51 +0000293 int interleaved_idx = i;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000294 for (int j = 0; j < proc_samples_per_channel_; j++) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000295 deinterleaved[j] = interleaved[interleaved_idx];
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000296 interleaved_idx += num_proc_channels_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000297 }
298 }
299}
300
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000301void AudioBuffer::InterleaveTo(AudioFrame* frame, bool data_changed) const {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000302 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.org63a50982012-05-02 23:56:37 +0000306 frame->vad_activity_ = activity_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000307
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000308 if (!data_changed) {
309 return;
310 }
311
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000312 if (num_proc_channels_ == 1) {
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000313 if (data_was_mixed_) {
andrew@webrtc.org63a50982012-05-02 23:56:37 +0000314 memcpy(frame->data_,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000315 channels_->channel(0),
316 sizeof(int16_t) * proc_samples_per_channel_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000317 } else {
318 // These should point to the same buffer in this case.
andrew@webrtc.org63a50982012-05-02 23:56:37 +0000319 assert(data_ == frame->data_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000320 }
321
322 return;
323 }
324
andrew@webrtc.org63a50982012-05-02 23:56:37 +0000325 int16_t* interleaved = frame->data_;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000326 for (int i = 0; i < num_proc_channels_; i++) {
327 int16_t* deinterleaved = channels_->channel(i);
andrew@webrtc.orged083d42011-09-19 15:28:51 +0000328 int interleaved_idx = i;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000329 for (int j = 0; j < proc_samples_per_channel_; j++) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000330 interleaved[interleaved_idx] = deinterleaved[j];
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000331 interleaved_idx += num_proc_channels_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000332 }
333 }
334}
335
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000336void AudioBuffer::CopyAndMix(int num_mixed_channels) {
337 // We currently only support the stereo to mono case.
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000338 assert(num_proc_channels_ == 2);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000339 assert(num_mixed_channels == 1);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000340 if (!mixed_channels_.get()) {
341 mixed_channels_.reset(
342 new ChannelBuffer<int16_t>(proc_samples_per_channel_,
343 num_mixed_channels));
344 }
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000345
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000346 StereoToMono(channels_->channel(0),
347 channels_->channel(1),
348 mixed_channels_->channel(0),
349 proc_samples_per_channel_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000350
niklase@google.com470e71d2011-07-07 08:21:25 +0000351 num_mixed_channels_ = num_mixed_channels;
352}
353
andrew@webrtc.orged083d42011-09-19 15:28:51 +0000354void AudioBuffer::CopyAndMixLowPass(int num_mixed_channels) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000355 // We currently only support the stereo to mono case.
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000356 assert(num_proc_channels_ == 2);
niklase@google.com470e71d2011-07-07 08:21:25 +0000357 assert(num_mixed_channels == 1);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000358 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.com470e71d2011-07-07 08:21:25 +0000363
364 StereoToMono(low_pass_split_data(0),
365 low_pass_split_data(1),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000366 mixed_low_pass_channels_->channel(0),
niklase@google.com470e71d2011-07-07 08:21:25 +0000367 samples_per_split_channel_);
368
369 num_mixed_low_pass_channels_ = num_mixed_channels;
370}
371
372void AudioBuffer::CopyLowPassToReference() {
373 reference_copied_ = true;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000374 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.com470e71d2011-07-07 08:21:25 +0000381 }
382}
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000383
niklase@google.com470e71d2011-07-07 08:21:25 +0000384} // namespace webrtc