2011-07-07 08:21:25 +00:00
|
|
|
/*
|
2012-05-02 23:56:37 +00:00
|
|
|
* Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
|
2011-07-07 08:21:25 +00:00
|
|
|
*
|
|
|
|
|
* Use of this source code is governed by a BSD-style license
|
|
|
|
|
* that can be found in the LICENSE file in the root of the source
|
|
|
|
|
* tree. An additional intellectual property rights grant can be found
|
|
|
|
|
* in the file PATENTS. All contributing project authors may
|
|
|
|
|
* be found in the AUTHORS file in the root of the source tree.
|
|
|
|
|
*/
|
|
|
|
|
|
2013-05-28 08:11:59 +00:00
|
|
|
#include "webrtc/modules/audio_processing/audio_buffer.h"
|
2011-07-07 08:21:25 +00:00
|
|
|
|
2014-04-22 21:00:04 +00:00
|
|
|
#include "webrtc/common_audio/resampler/push_sinc_resampler.h"
|
2013-05-28 08:11:59 +00:00
|
|
|
#include "webrtc/common_audio/signal_processing/include/signal_processing_library.h"
|
2015-01-28 19:57:00 +00:00
|
|
|
#include "webrtc/common_audio/channel_buffer.h"
|
2014-11-27 23:40:25 +00:00
|
|
|
#include "webrtc/modules/audio_processing/common.h"
|
2011-11-15 16:57:56 +00:00
|
|
|
|
2011-07-07 08:21:25 +00:00
|
|
|
namespace webrtc {
|
|
|
|
|
namespace {
|
|
|
|
|
|
2014-04-24 18:28:56 +00:00
|
|
|
bool HasKeyboardChannel(AudioProcessing::ChannelLayout layout) {
|
|
|
|
|
switch (layout) {
|
|
|
|
|
case AudioProcessing::kMono:
|
|
|
|
|
case AudioProcessing::kStereo:
|
|
|
|
|
return false;
|
|
|
|
|
case AudioProcessing::kMonoAndKeyboard:
|
|
|
|
|
case AudioProcessing::kStereoAndKeyboard:
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
assert(false);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int KeyboardChannelIndex(AudioProcessing::ChannelLayout layout) {
|
|
|
|
|
switch (layout) {
|
|
|
|
|
case AudioProcessing::kMono:
|
|
|
|
|
case AudioProcessing::kStereo:
|
|
|
|
|
assert(false);
|
|
|
|
|
return -1;
|
|
|
|
|
case AudioProcessing::kMonoAndKeyboard:
|
|
|
|
|
return 1;
|
|
|
|
|
case AudioProcessing::kStereoAndKeyboard:
|
|
|
|
|
return 2;
|
|
|
|
|
}
|
|
|
|
|
assert(false);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-31 04:58:14 +00:00
|
|
|
template <typename T>
|
|
|
|
|
void StereoToMono(const T* left, const T* right, T* out,
|
2015-02-10 22:52:15 +00:00
|
|
|
int num_frames) {
|
|
|
|
|
for (int i = 0; i < num_frames; ++i)
|
2014-04-22 21:00:04 +00:00
|
|
|
out[i] = (left[i] + right[i]) / 2;
|
|
|
|
|
}
|
2011-07-07 08:21:25 +00:00
|
|
|
|
2015-02-10 22:52:15 +00:00
|
|
|
int NumBandsFromSamplesPerChannel(int num_frames) {
|
|
|
|
|
int num_bands = 1;
|
|
|
|
|
if (num_frames == kSamplesPer32kHzChannel ||
|
|
|
|
|
num_frames == kSamplesPer48kHzChannel) {
|
|
|
|
|
num_bands = rtc::CheckedDivExact(num_frames,
|
|
|
|
|
static_cast<int>(kSamplesPer16kHzChannel));
|
|
|
|
|
}
|
|
|
|
|
return num_bands;
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-22 21:00:04 +00:00
|
|
|
} // namespace
|
2011-07-07 08:21:25 +00:00
|
|
|
|
2015-02-10 22:52:15 +00:00
|
|
|
AudioBuffer::AudioBuffer(int input_num_frames,
|
2014-04-22 21:00:04 +00:00
|
|
|
int num_input_channels,
|
2015-02-10 22:52:15 +00:00
|
|
|
int process_num_frames,
|
2014-04-22 21:00:04 +00:00
|
|
|
int num_process_channels,
|
2015-02-10 22:52:15 +00:00
|
|
|
int output_num_frames)
|
|
|
|
|
: input_num_frames_(input_num_frames),
|
2014-04-22 21:00:04 +00:00
|
|
|
num_input_channels_(num_input_channels),
|
2015-02-10 22:52:15 +00:00
|
|
|
proc_num_frames_(process_num_frames),
|
2014-04-22 21:00:04 +00:00
|
|
|
num_proc_channels_(num_process_channels),
|
2015-02-10 22:52:15 +00:00
|
|
|
output_num_frames_(output_num_frames),
|
2014-12-11 17:09:21 +00:00
|
|
|
num_channels_(num_process_channels),
|
2015-02-10 22:52:15 +00:00
|
|
|
num_bands_(NumBandsFromSamplesPerChannel(proc_num_frames_)),
|
|
|
|
|
num_split_frames_(rtc::CheckedDivExact(
|
|
|
|
|
proc_num_frames_, num_bands_)),
|
2014-07-17 08:27:39 +00:00
|
|
|
mixed_low_pass_valid_(false),
|
2011-09-19 15:28:51 +00:00
|
|
|
reference_copied_(false),
|
|
|
|
|
activity_(AudioFrame::kVadUnknown),
|
2014-04-24 18:28:56 +00:00
|
|
|
keyboard_data_(NULL),
|
2015-02-10 22:52:15 +00:00
|
|
|
data_(new IFChannelBuffer(proc_num_frames_, num_proc_channels_)) {
|
|
|
|
|
assert(input_num_frames_ > 0);
|
|
|
|
|
assert(proc_num_frames_ > 0);
|
|
|
|
|
assert(output_num_frames_ > 0);
|
2014-04-22 21:00:04 +00:00
|
|
|
assert(num_input_channels_ > 0 && num_input_channels_ <= 2);
|
2015-02-10 22:52:15 +00:00
|
|
|
assert(num_proc_channels_ > 0 && num_proc_channels_ <= num_input_channels_);
|
2014-04-22 21:00:04 +00:00
|
|
|
|
|
|
|
|
if (num_input_channels_ == 2 && num_proc_channels_ == 1) {
|
2015-02-10 22:52:15 +00:00
|
|
|
input_buffer_.reset(new ChannelBuffer<float>(input_num_frames_,
|
2014-04-22 21:00:04 +00:00
|
|
|
num_proc_channels_));
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-10 22:52:15 +00:00
|
|
|
if (input_num_frames_ != proc_num_frames_ ||
|
|
|
|
|
output_num_frames_ != proc_num_frames_) {
|
2014-04-22 21:00:04 +00:00
|
|
|
// Create an intermediate buffer for resampling.
|
2015-02-10 22:52:15 +00:00
|
|
|
process_buffer_.reset(new ChannelBuffer<float>(proc_num_frames_,
|
2014-04-22 21:00:04 +00:00
|
|
|
num_proc_channels_));
|
|
|
|
|
|
2015-02-10 22:52:15 +00:00
|
|
|
if (input_num_frames_ != proc_num_frames_) {
|
|
|
|
|
for (int i = 0; i < num_proc_channels_; ++i) {
|
|
|
|
|
input_resamplers_.push_back(
|
|
|
|
|
new PushSincResampler(input_num_frames_,
|
|
|
|
|
proc_num_frames_));
|
|
|
|
|
}
|
2014-04-22 21:00:04 +00:00
|
|
|
}
|
|
|
|
|
|
2015-02-10 22:52:15 +00:00
|
|
|
if (output_num_frames_ != proc_num_frames_) {
|
|
|
|
|
for (int i = 0; i < num_proc_channels_; ++i) {
|
|
|
|
|
output_resamplers_.push_back(
|
|
|
|
|
new PushSincResampler(proc_num_frames_,
|
|
|
|
|
output_num_frames_));
|
|
|
|
|
}
|
2014-04-22 21:00:04 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-10 22:52:15 +00:00
|
|
|
if (num_bands_ > 1) {
|
|
|
|
|
split_data_.reset(new IFChannelBuffer(proc_num_frames_,
|
|
|
|
|
num_proc_channels_,
|
|
|
|
|
num_bands_));
|
2014-11-14 22:18:10 +00:00
|
|
|
splitting_filter_.reset(new SplittingFilter(num_proc_channels_));
|
2014-04-22 21:00:04 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-24 18:28:56 +00:00
|
|
|
AudioBuffer::~AudioBuffer() {}
|
|
|
|
|
|
2014-04-22 21:00:04 +00:00
|
|
|
void AudioBuffer::CopyFrom(const float* const* data,
|
2015-02-10 22:52:15 +00:00
|
|
|
int num_frames,
|
2014-04-22 21:00:04 +00:00
|
|
|
AudioProcessing::ChannelLayout layout) {
|
2015-02-10 22:52:15 +00:00
|
|
|
assert(num_frames == input_num_frames_);
|
2014-04-22 21:00:04 +00:00
|
|
|
assert(ChannelsFromLayout(layout) == num_input_channels_);
|
|
|
|
|
InitForNewData();
|
|
|
|
|
|
2014-04-24 18:28:56 +00:00
|
|
|
if (HasKeyboardChannel(layout)) {
|
|
|
|
|
keyboard_data_ = data[KeyboardChannelIndex(layout)];
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-22 21:00:04 +00:00
|
|
|
// Downmix.
|
|
|
|
|
const float* const* data_ptr = data;
|
|
|
|
|
if (num_input_channels_ == 2 && num_proc_channels_ == 1) {
|
|
|
|
|
StereoToMono(data[0],
|
|
|
|
|
data[1],
|
2015-02-10 22:52:15 +00:00
|
|
|
input_buffer_->channels()[0],
|
|
|
|
|
input_num_frames_);
|
2014-04-22 21:00:04 +00:00
|
|
|
data_ptr = input_buffer_->channels();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Resample.
|
2015-02-10 22:52:15 +00:00
|
|
|
if (input_num_frames_ != proc_num_frames_) {
|
2014-04-22 21:00:04 +00:00
|
|
|
for (int i = 0; i < num_proc_channels_; ++i) {
|
|
|
|
|
input_resamplers_[i]->Resample(data_ptr[i],
|
2015-02-10 22:52:15 +00:00
|
|
|
input_num_frames_,
|
|
|
|
|
process_buffer_->channels()[i],
|
|
|
|
|
proc_num_frames_);
|
2014-04-22 21:00:04 +00:00
|
|
|
}
|
|
|
|
|
data_ptr = process_buffer_->channels();
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-31 04:58:14 +00:00
|
|
|
// Convert to the S16 range.
|
2014-04-22 21:00:04 +00:00
|
|
|
for (int i = 0; i < num_proc_channels_; ++i) {
|
2015-02-10 22:52:15 +00:00
|
|
|
FloatToFloatS16(data_ptr[i],
|
|
|
|
|
proc_num_frames_,
|
|
|
|
|
data_->fbuf()->channels()[i]);
|
2014-04-22 21:00:04 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-10 22:52:15 +00:00
|
|
|
void AudioBuffer::CopyTo(int num_frames,
|
2014-04-22 21:00:04 +00:00
|
|
|
AudioProcessing::ChannelLayout layout,
|
|
|
|
|
float* const* data) {
|
2015-02-10 22:52:15 +00:00
|
|
|
assert(num_frames == output_num_frames_);
|
2014-12-11 17:09:21 +00:00
|
|
|
assert(ChannelsFromLayout(layout) == num_channels_);
|
2014-04-22 21:00:04 +00:00
|
|
|
|
2014-10-31 04:58:14 +00:00
|
|
|
// Convert to the float range.
|
2014-04-22 21:00:04 +00:00
|
|
|
float* const* data_ptr = data;
|
2015-02-10 22:52:15 +00:00
|
|
|
if (output_num_frames_ != proc_num_frames_) {
|
2014-04-22 21:00:04 +00:00
|
|
|
// Convert to an intermediate buffer for subsequent resampling.
|
|
|
|
|
data_ptr = process_buffer_->channels();
|
|
|
|
|
}
|
2014-12-11 17:09:21 +00:00
|
|
|
for (int i = 0; i < num_channels_; ++i) {
|
2015-02-10 22:52:15 +00:00
|
|
|
FloatS16ToFloat(data_->fbuf()->channels()[i],
|
|
|
|
|
proc_num_frames_,
|
2014-10-31 04:58:14 +00:00
|
|
|
data_ptr[i]);
|
2014-04-22 21:00:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Resample.
|
2015-02-10 22:52:15 +00:00
|
|
|
if (output_num_frames_ != proc_num_frames_) {
|
2014-12-11 17:09:21 +00:00
|
|
|
for (int i = 0; i < num_channels_; ++i) {
|
2014-04-22 21:00:04 +00:00
|
|
|
output_resamplers_[i]->Resample(data_ptr[i],
|
2015-02-10 22:52:15 +00:00
|
|
|
proc_num_frames_,
|
2014-04-22 21:00:04 +00:00
|
|
|
data[i],
|
2015-02-10 22:52:15 +00:00
|
|
|
output_num_frames_);
|
2014-04-22 21:00:04 +00:00
|
|
|
}
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-22 21:00:04 +00:00
|
|
|
void AudioBuffer::InitForNewData() {
|
2014-04-24 18:28:56 +00:00
|
|
|
keyboard_data_ = NULL;
|
2014-07-17 08:27:39 +00:00
|
|
|
mixed_low_pass_valid_ = false;
|
2014-03-04 20:58:13 +00:00
|
|
|
reference_copied_ = false;
|
|
|
|
|
activity_ = AudioFrame::kVadUnknown;
|
2014-12-11 17:09:21 +00:00
|
|
|
num_channels_ = num_proc_channels_;
|
2014-03-04 20:58:13 +00:00
|
|
|
}
|
|
|
|
|
|
2014-12-03 01:06:35 +00:00
|
|
|
const int16_t* const* AudioBuffer::channels_const() const {
|
2015-02-10 22:52:15 +00:00
|
|
|
return data_->ibuf_const()->channels();
|
2014-11-14 22:18:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int16_t* const* AudioBuffer::channels() {
|
|
|
|
|
mixed_low_pass_valid_ = false;
|
2015-02-10 22:52:15 +00:00
|
|
|
return data_->ibuf()->channels();
|
2014-11-14 22:18:10 +00:00
|
|
|
}
|
|
|
|
|
|
2014-12-10 19:30:57 +00:00
|
|
|
const int16_t* const* AudioBuffer::split_bands_const(int channel) const {
|
2015-02-10 22:52:15 +00:00
|
|
|
return split_data_.get() ?
|
|
|
|
|
split_data_->ibuf_const()->bands(channel) :
|
|
|
|
|
data_->ibuf_const()->bands(channel);
|
2014-05-15 11:17:21 +00:00
|
|
|
}
|
|
|
|
|
|
2014-12-10 19:30:57 +00:00
|
|
|
int16_t* const* AudioBuffer::split_bands(int channel) {
|
|
|
|
|
mixed_low_pass_valid_ = false;
|
2015-02-10 22:52:15 +00:00
|
|
|
return split_data_.get() ?
|
|
|
|
|
split_data_->ibuf()->bands(channel) :
|
|
|
|
|
data_->ibuf()->bands(channel);
|
2014-07-03 09:47:33 +00:00
|
|
|
}
|
|
|
|
|
|
2014-12-03 01:06:35 +00:00
|
|
|
const int16_t* const* AudioBuffer::split_channels_const(Band band) const {
|
2015-02-10 22:52:15 +00:00
|
|
|
if (split_data_.get()) {
|
|
|
|
|
return split_data_->ibuf_const()->channels(band);
|
2014-12-03 01:06:35 +00:00
|
|
|
} else {
|
2015-02-10 22:52:15 +00:00
|
|
|
return band == kBand0To8kHz ? data_->ibuf_const()->channels() : nullptr;
|
2014-12-03 01:06:35 +00:00
|
|
|
}
|
2014-09-25 20:52:08 +00:00
|
|
|
}
|
|
|
|
|
|
2014-12-03 01:06:35 +00:00
|
|
|
int16_t* const* AudioBuffer::split_channels(Band band) {
|
2014-09-25 20:52:08 +00:00
|
|
|
mixed_low_pass_valid_ = false;
|
2015-02-10 22:52:15 +00:00
|
|
|
if (split_data_.get()) {
|
|
|
|
|
return split_data_->ibuf()->channels(band);
|
2014-12-03 01:06:35 +00:00
|
|
|
} else {
|
2015-02-10 22:52:15 +00:00
|
|
|
return band == kBand0To8kHz ? data_->ibuf()->channels() : nullptr;
|
2014-12-03 01:06:35 +00:00
|
|
|
}
|
2014-09-25 20:52:08 +00:00
|
|
|
}
|
|
|
|
|
|
2014-12-03 01:06:35 +00:00
|
|
|
const float* const* AudioBuffer::channels_const_f() const {
|
2015-02-10 22:52:15 +00:00
|
|
|
return data_->fbuf_const()->channels();
|
2014-11-14 22:18:10 +00:00
|
|
|
}
|
|
|
|
|
|
2014-12-03 01:06:35 +00:00
|
|
|
float* const* AudioBuffer::channels_f() {
|
2014-11-14 22:18:10 +00:00
|
|
|
mixed_low_pass_valid_ = false;
|
2015-02-10 22:52:15 +00:00
|
|
|
return data_->fbuf()->channels();
|
2014-11-14 22:18:10 +00:00
|
|
|
}
|
|
|
|
|
|
2014-12-10 19:30:57 +00:00
|
|
|
const float* const* AudioBuffer::split_bands_const_f(int channel) const {
|
2015-02-10 22:52:15 +00:00
|
|
|
return split_data_.get() ?
|
|
|
|
|
split_data_->fbuf_const()->bands(channel) :
|
|
|
|
|
data_->fbuf_const()->bands(channel);
|
2014-05-15 11:17:21 +00:00
|
|
|
}
|
|
|
|
|
|
2014-12-10 19:30:57 +00:00
|
|
|
float* const* AudioBuffer::split_bands_f(int channel) {
|
|
|
|
|
mixed_low_pass_valid_ = false;
|
2015-02-10 22:52:15 +00:00
|
|
|
return split_data_.get() ?
|
|
|
|
|
split_data_->fbuf()->bands(channel) :
|
|
|
|
|
data_->fbuf()->bands(channel);
|
2014-07-03 09:47:33 +00:00
|
|
|
}
|
|
|
|
|
|
2014-12-03 01:06:35 +00:00
|
|
|
const float* const* AudioBuffer::split_channels_const_f(Band band) const {
|
2015-02-10 22:52:15 +00:00
|
|
|
if (split_data_.get()) {
|
|
|
|
|
return split_data_->fbuf_const()->channels(band);
|
2014-12-03 01:06:35 +00:00
|
|
|
} else {
|
2015-02-10 22:52:15 +00:00
|
|
|
return band == kBand0To8kHz ? data_->fbuf_const()->channels() : nullptr;
|
2014-12-03 01:06:35 +00:00
|
|
|
}
|
2014-09-25 20:52:08 +00:00
|
|
|
}
|
|
|
|
|
|
2014-12-03 01:06:35 +00:00
|
|
|
float* const* AudioBuffer::split_channels_f(Band band) {
|
2014-09-25 20:52:08 +00:00
|
|
|
mixed_low_pass_valid_ = false;
|
2015-02-10 22:52:15 +00:00
|
|
|
if (split_data_.get()) {
|
|
|
|
|
return split_data_->fbuf()->channels(band);
|
2014-12-03 01:06:35 +00:00
|
|
|
} else {
|
2015-02-10 22:52:15 +00:00
|
|
|
return band == kBand0To8kHz ? data_->fbuf()->channels() : nullptr;
|
2014-12-03 01:06:35 +00:00
|
|
|
}
|
2014-11-17 23:01:23 +00:00
|
|
|
}
|
|
|
|
|
|
2014-07-17 08:27:39 +00:00
|
|
|
const int16_t* AudioBuffer::mixed_low_pass_data() {
|
|
|
|
|
// Currently only mixing stereo to mono is supported.
|
|
|
|
|
assert(num_proc_channels_ == 1 || num_proc_channels_ == 2);
|
2011-11-15 16:57:56 +00:00
|
|
|
|
2014-07-17 08:27:39 +00:00
|
|
|
if (num_proc_channels_ == 1) {
|
2014-12-10 19:30:57 +00:00
|
|
|
return split_bands_const(0)[kBand0To8kHz];
|
2014-07-17 08:27:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!mixed_low_pass_valid_) {
|
|
|
|
|
if (!mixed_low_pass_channels_.get()) {
|
|
|
|
|
mixed_low_pass_channels_.reset(
|
2015-02-10 22:52:15 +00:00
|
|
|
new ChannelBuffer<int16_t>(num_split_frames_, 1));
|
2014-07-17 08:27:39 +00:00
|
|
|
}
|
2014-12-10 19:30:57 +00:00
|
|
|
StereoToMono(split_bands_const(0)[kBand0To8kHz],
|
|
|
|
|
split_bands_const(1)[kBand0To8kHz],
|
2015-02-10 22:52:15 +00:00
|
|
|
mixed_low_pass_channels_->channels()[0],
|
|
|
|
|
num_split_frames_);
|
2014-07-17 08:27:39 +00:00
|
|
|
mixed_low_pass_valid_ = true;
|
|
|
|
|
}
|
2015-02-10 22:52:15 +00:00
|
|
|
return mixed_low_pass_channels_->channels()[0];
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2014-04-30 16:44:13 +00:00
|
|
|
const int16_t* AudioBuffer::low_pass_reference(int channel) const {
|
2011-07-07 08:21:25 +00:00
|
|
|
if (!reference_copied_) {
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-10 22:52:15 +00:00
|
|
|
return low_pass_reference_channels_->channels()[channel];
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2014-04-24 18:28:56 +00:00
|
|
|
const float* AudioBuffer::keyboard_data() const {
|
|
|
|
|
return keyboard_data_;
|
|
|
|
|
}
|
|
|
|
|
|
2011-09-19 15:28:51 +00:00
|
|
|
void AudioBuffer::set_activity(AudioFrame::VADActivity activity) {
|
|
|
|
|
activity_ = activity;
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-15 16:57:56 +00:00
|
|
|
AudioFrame::VADActivity AudioBuffer::activity() const {
|
2011-09-19 15:28:51 +00:00
|
|
|
return activity_;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int AudioBuffer::num_channels() const {
|
2014-12-11 17:09:21 +00:00
|
|
|
return num_channels_;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void AudioBuffer::set_num_channels(int num_channels) {
|
|
|
|
|
num_channels_ = num_channels;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2015-02-10 22:52:15 +00:00
|
|
|
int AudioBuffer::num_frames() const {
|
|
|
|
|
return proc_num_frames_;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2015-02-10 22:52:15 +00:00
|
|
|
int AudioBuffer::num_frames_per_band() const {
|
|
|
|
|
return num_split_frames_;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2015-02-10 22:52:15 +00:00
|
|
|
int AudioBuffer::num_keyboard_frames() const {
|
2014-04-24 18:28:56 +00:00
|
|
|
// We don't resample the keyboard channel.
|
2015-02-10 22:52:15 +00:00
|
|
|
return input_num_frames_;
|
2014-04-24 18:28:56 +00:00
|
|
|
}
|
|
|
|
|
|
2014-12-10 19:30:57 +00:00
|
|
|
int AudioBuffer::num_bands() const {
|
|
|
|
|
return num_bands_;
|
|
|
|
|
}
|
|
|
|
|
|
2011-09-19 15:28:51 +00:00
|
|
|
// TODO(andrew): Do deinterleaving and mixing in one step?
|
|
|
|
|
void AudioBuffer::DeinterleaveFrom(AudioFrame* frame) {
|
2015-02-10 22:52:15 +00:00
|
|
|
assert(proc_num_frames_ == input_num_frames_);
|
Enable render downmixing to mono in AudioProcessing.
In practice, we have been doing this since time immemorial, but have
relied on the user to do the downmixing (first voice engine then
Chromium). It's more logical for this burden to fall on AudioProcessing,
however, who can be expected to know that this is a reasonable approach
for AEC. Permitting two render channels results in running two AECs
serially.
Critically, in my recent change to have Chromium adopt the float
interface:
https://codereview.chromium.org/420603004
I removed the downmixing by Chromium, forgetting that we hadn't yet
enabled this feature in AudioProcessing. This corrects that oversight.
The change in paths hit by production users is very minor. As commented
it required adding downmixing to the int16_t path to satisfy
bit-exactness tests.
For reference, find the ApmTest.Process errors here:
https://paste.googleplex.com/6372007910309888
BUG=webrtc:3853
TESTED=listened to the files output from the Process test, and verified
that they sound as expected: higher echo while the AEC is adapting, but
afterwards very close.
R=aluebs@webrtc.org, bjornv@webrtc.org, kwiberg@webrtc.org
Review URL: https://webrtc-codereview.appspot.com/31459004
git-svn-id: http://webrtc.googlecode.com/svn/trunk@7292 4adac7df-926f-26a2-2b94-8c16560cd09d
2014-09-24 20:06:23 +00:00
|
|
|
assert(frame->num_channels_ == num_input_channels_);
|
2015-02-10 22:52:15 +00:00
|
|
|
assert(frame->samples_per_channel_ == proc_num_frames_);
|
2014-04-22 21:00:04 +00:00
|
|
|
InitForNewData();
|
2012-05-02 23:56:37 +00:00
|
|
|
activity_ = frame->vad_activity_;
|
2011-07-07 08:21:25 +00:00
|
|
|
|
Enable render downmixing to mono in AudioProcessing.
In practice, we have been doing this since time immemorial, but have
relied on the user to do the downmixing (first voice engine then
Chromium). It's more logical for this burden to fall on AudioProcessing,
however, who can be expected to know that this is a reasonable approach
for AEC. Permitting two render channels results in running two AECs
serially.
Critically, in my recent change to have Chromium adopt the float
interface:
https://codereview.chromium.org/420603004
I removed the downmixing by Chromium, forgetting that we hadn't yet
enabled this feature in AudioProcessing. This corrects that oversight.
The change in paths hit by production users is very minor. As commented
it required adding downmixing to the int16_t path to satisfy
bit-exactness tests.
For reference, find the ApmTest.Process errors here:
https://paste.googleplex.com/6372007910309888
BUG=webrtc:3853
TESTED=listened to the files output from the Process test, and verified
that they sound as expected: higher echo while the AEC is adapting, but
afterwards very close.
R=aluebs@webrtc.org, bjornv@webrtc.org, kwiberg@webrtc.org
Review URL: https://webrtc-codereview.appspot.com/31459004
git-svn-id: http://webrtc.googlecode.com/svn/trunk@7292 4adac7df-926f-26a2-2b94-8c16560cd09d
2014-09-24 20:06:23 +00:00
|
|
|
if (num_input_channels_ == 2 && num_proc_channels_ == 1) {
|
|
|
|
|
// Downmix directly; no explicit deinterleaving needed.
|
2015-02-10 22:52:15 +00:00
|
|
|
int16_t* downmixed = data_->ibuf()->channels()[0];
|
|
|
|
|
for (int i = 0; i < input_num_frames_; ++i) {
|
2014-10-31 04:58:14 +00:00
|
|
|
downmixed[i] = (frame->data_[i * 2] + frame->data_[i * 2 + 1]) / 2;
|
Enable render downmixing to mono in AudioProcessing.
In practice, we have been doing this since time immemorial, but have
relied on the user to do the downmixing (first voice engine then
Chromium). It's more logical for this burden to fall on AudioProcessing,
however, who can be expected to know that this is a reasonable approach
for AEC. Permitting two render channels results in running two AECs
serially.
Critically, in my recent change to have Chromium adopt the float
interface:
https://codereview.chromium.org/420603004
I removed the downmixing by Chromium, forgetting that we hadn't yet
enabled this feature in AudioProcessing. This corrects that oversight.
The change in paths hit by production users is very minor. As commented
it required adding downmixing to the int16_t path to satisfy
bit-exactness tests.
For reference, find the ApmTest.Process errors here:
https://paste.googleplex.com/6372007910309888
BUG=webrtc:3853
TESTED=listened to the files output from the Process test, and verified
that they sound as expected: higher echo while the AEC is adapting, but
afterwards very close.
R=aluebs@webrtc.org, bjornv@webrtc.org, kwiberg@webrtc.org
Review URL: https://webrtc-codereview.appspot.com/31459004
git-svn-id: http://webrtc.googlecode.com/svn/trunk@7292 4adac7df-926f-26a2-2b94-8c16560cd09d
2014-09-24 20:06:23 +00:00
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
assert(num_proc_channels_ == num_input_channels_);
|
|
|
|
|
int16_t* interleaved = frame->data_;
|
|
|
|
|
for (int i = 0; i < num_proc_channels_; ++i) {
|
2015-02-10 22:52:15 +00:00
|
|
|
int16_t* deinterleaved = data_->ibuf()->channels()[i];
|
Enable render downmixing to mono in AudioProcessing.
In practice, we have been doing this since time immemorial, but have
relied on the user to do the downmixing (first voice engine then
Chromium). It's more logical for this burden to fall on AudioProcessing,
however, who can be expected to know that this is a reasonable approach
for AEC. Permitting two render channels results in running two AECs
serially.
Critically, in my recent change to have Chromium adopt the float
interface:
https://codereview.chromium.org/420603004
I removed the downmixing by Chromium, forgetting that we hadn't yet
enabled this feature in AudioProcessing. This corrects that oversight.
The change in paths hit by production users is very minor. As commented
it required adding downmixing to the int16_t path to satisfy
bit-exactness tests.
For reference, find the ApmTest.Process errors here:
https://paste.googleplex.com/6372007910309888
BUG=webrtc:3853
TESTED=listened to the files output from the Process test, and verified
that they sound as expected: higher echo while the AEC is adapting, but
afterwards very close.
R=aluebs@webrtc.org, bjornv@webrtc.org, kwiberg@webrtc.org
Review URL: https://webrtc-codereview.appspot.com/31459004
git-svn-id: http://webrtc.googlecode.com/svn/trunk@7292 4adac7df-926f-26a2-2b94-8c16560cd09d
2014-09-24 20:06:23 +00:00
|
|
|
int interleaved_idx = i;
|
2015-02-10 22:52:15 +00:00
|
|
|
for (int j = 0; j < proc_num_frames_; ++j) {
|
Enable render downmixing to mono in AudioProcessing.
In practice, we have been doing this since time immemorial, but have
relied on the user to do the downmixing (first voice engine then
Chromium). It's more logical for this burden to fall on AudioProcessing,
however, who can be expected to know that this is a reasonable approach
for AEC. Permitting two render channels results in running two AECs
serially.
Critically, in my recent change to have Chromium adopt the float
interface:
https://codereview.chromium.org/420603004
I removed the downmixing by Chromium, forgetting that we hadn't yet
enabled this feature in AudioProcessing. This corrects that oversight.
The change in paths hit by production users is very minor. As commented
it required adding downmixing to the int16_t path to satisfy
bit-exactness tests.
For reference, find the ApmTest.Process errors here:
https://paste.googleplex.com/6372007910309888
BUG=webrtc:3853
TESTED=listened to the files output from the Process test, and verified
that they sound as expected: higher echo while the AEC is adapting, but
afterwards very close.
R=aluebs@webrtc.org, bjornv@webrtc.org, kwiberg@webrtc.org
Review URL: https://webrtc-codereview.appspot.com/31459004
git-svn-id: http://webrtc.googlecode.com/svn/trunk@7292 4adac7df-926f-26a2-2b94-8c16560cd09d
2014-09-24 20:06:23 +00:00
|
|
|
deinterleaved[j] = interleaved[interleaved_idx];
|
|
|
|
|
interleaved_idx += num_proc_channels_;
|
|
|
|
|
}
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-15 16:57:56 +00:00
|
|
|
void AudioBuffer::InterleaveTo(AudioFrame* frame, bool data_changed) const {
|
2015-02-10 22:52:15 +00:00
|
|
|
assert(proc_num_frames_ == output_num_frames_);
|
2014-12-11 17:09:21 +00:00
|
|
|
assert(num_channels_ == num_input_channels_);
|
|
|
|
|
assert(frame->num_channels_ == num_channels_);
|
2015-02-10 22:52:15 +00:00
|
|
|
assert(frame->samples_per_channel_ == proc_num_frames_);
|
2012-05-02 23:56:37 +00:00
|
|
|
frame->vad_activity_ = activity_;
|
2011-07-07 08:21:25 +00:00
|
|
|
|
2011-11-15 16:57:56 +00:00
|
|
|
if (!data_changed) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2012-05-02 23:56:37 +00:00
|
|
|
int16_t* interleaved = frame->data_;
|
2014-12-11 17:09:21 +00:00
|
|
|
for (int i = 0; i < num_channels_; i++) {
|
2015-02-10 22:52:15 +00:00
|
|
|
int16_t* deinterleaved = data_->ibuf()->channels()[i];
|
2011-09-19 15:28:51 +00:00
|
|
|
int interleaved_idx = i;
|
2015-02-10 22:52:15 +00:00
|
|
|
for (int j = 0; j < proc_num_frames_; j++) {
|
2011-07-07 08:21:25 +00:00
|
|
|
interleaved[interleaved_idx] = deinterleaved[j];
|
2014-12-11 17:09:21 +00:00
|
|
|
interleaved_idx += num_channels_;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void AudioBuffer::CopyLowPassToReference() {
|
|
|
|
|
reference_copied_ = true;
|
2014-12-11 17:09:21 +00:00
|
|
|
if (!low_pass_reference_channels_.get() ||
|
|
|
|
|
low_pass_reference_channels_->num_channels() != num_channels_) {
|
2014-04-22 21:00:04 +00:00
|
|
|
low_pass_reference_channels_.reset(
|
2015-02-10 22:52:15 +00:00
|
|
|
new ChannelBuffer<int16_t>(num_split_frames_,
|
2014-04-22 21:00:04 +00:00
|
|
|
num_proc_channels_));
|
|
|
|
|
}
|
|
|
|
|
for (int i = 0; i < num_proc_channels_; i++) {
|
2015-02-10 22:52:15 +00:00
|
|
|
memcpy(low_pass_reference_channels_->channels()[i],
|
|
|
|
|
split_bands_const(i)[kBand0To8kHz],
|
|
|
|
|
low_pass_reference_channels_->num_frames_per_band() *
|
|
|
|
|
sizeof(split_bands_const(i)[kBand0To8kHz][0]));
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
}
|
2014-04-22 21:00:04 +00:00
|
|
|
|
2014-11-14 22:18:10 +00:00
|
|
|
void AudioBuffer::SplitIntoFrequencyBands() {
|
2015-02-10 22:52:15 +00:00
|
|
|
splitting_filter_->Analysis(data_.get(), split_data_.get());
|
2014-11-14 22:18:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void AudioBuffer::MergeFrequencyBands() {
|
2015-02-10 22:52:15 +00:00
|
|
|
splitting_filter_->Synthesis(split_data_.get(), data_.get());
|
2014-11-14 22:18:10 +00:00
|
|
|
}
|
|
|
|
|
|
2011-07-07 08:21:25 +00:00
|
|
|
} // namespace webrtc
|