Add a wrapper around PushSincResampler and the old Resampler.
The old resampler is used whenever it supports the requested rates. Otherwise the sinc resampler is enabled. Integrated with output_mixer in order to test the change through output_mixer_unittest. The sinc resampler will not yet be used, since we don't feed VoE with any rates that trigger it. BUG=webrtc:1395 R=bjornv@webrtc.org Review URL: https://webrtc-codereview.appspot.com/1355004 git-svn-id: http://webrtc.googlecode.com/svn/trunk@3915 4adac7df-926f-26a2-2b94-8c16560cd09d
This commit is contained in:
133
webrtc/common_audio/resampler/push_resampler.cc
Normal file
133
webrtc/common_audio/resampler/push_resampler.cc
Normal file
@ -0,0 +1,133 @@
|
||||
/*
|
||||
* Copyright (c) 2013 The WebRTC project authors. All Rights Reserved.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#include "webrtc/common_audio/resampler/include/push_resampler.h"
|
||||
|
||||
#include <cstring>
|
||||
|
||||
#include "webrtc/common_audio/include/audio_util.h"
|
||||
#include "webrtc/common_audio/resampler/include/resampler.h"
|
||||
#include "webrtc/common_audio/resampler/push_sinc_resampler.h"
|
||||
|
||||
namespace webrtc {
|
||||
|
||||
PushResampler::PushResampler()
|
||||
// Requires valid values at construction, so give it something arbitrary.
|
||||
: resampler_(new Resampler(48000, 48000, kResamplerSynchronous)),
|
||||
sinc_resampler_(NULL),
|
||||
sinc_resampler_right_(NULL),
|
||||
src_sample_rate_hz_(0),
|
||||
dst_sample_rate_hz_(0),
|
||||
num_channels_(0),
|
||||
use_sinc_resampler_(false),
|
||||
src_left_(NULL),
|
||||
src_right_(NULL),
|
||||
dst_left_(NULL),
|
||||
dst_right_(NULL) {
|
||||
}
|
||||
|
||||
PushResampler::~PushResampler() {
|
||||
}
|
||||
|
||||
int PushResampler::InitializeIfNeeded(int src_sample_rate_hz,
|
||||
int dst_sample_rate_hz,
|
||||
int num_channels) {
|
||||
if (src_sample_rate_hz == src_sample_rate_hz_ &&
|
||||
dst_sample_rate_hz == dst_sample_rate_hz_ &&
|
||||
num_channels == num_channels_) {
|
||||
// No-op if settings haven't changed.
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (src_sample_rate_hz <= 0 || dst_sample_rate_hz <= 0 ||
|
||||
num_channels <= 0 || num_channels > 2) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
src_sample_rate_hz_ = src_sample_rate_hz;
|
||||
dst_sample_rate_hz_ = dst_sample_rate_hz;
|
||||
num_channels_ = num_channels;
|
||||
|
||||
const ResamplerType resampler_type =
|
||||
num_channels == 1 ? kResamplerSynchronous : kResamplerSynchronousStereo;
|
||||
if (resampler_->Reset(src_sample_rate_hz, dst_sample_rate_hz,
|
||||
resampler_type) == 0) {
|
||||
// The resampler supports these rates.
|
||||
use_sinc_resampler_ = false;
|
||||
return 0;
|
||||
}
|
||||
|
||||
use_sinc_resampler_ = true;
|
||||
const int src_size_10ms_mono = src_sample_rate_hz / 100;
|
||||
const int dst_size_10ms_mono = dst_sample_rate_hz / 100;
|
||||
sinc_resampler_.reset(new PushSincResampler(src_size_10ms_mono,
|
||||
dst_size_10ms_mono));
|
||||
if (num_channels_ == 2) {
|
||||
src_left_.reset(new int16_t[src_size_10ms_mono]);
|
||||
src_right_.reset(new int16_t[src_size_10ms_mono]);
|
||||
dst_left_.reset(new int16_t[dst_size_10ms_mono]);
|
||||
dst_right_.reset(new int16_t[dst_size_10ms_mono]);
|
||||
sinc_resampler_right_.reset(new PushSincResampler(src_size_10ms_mono,
|
||||
dst_size_10ms_mono));
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int PushResampler::Resample(const int16_t* src, int src_length,
|
||||
int16_t* dst, int dst_capacity) {
|
||||
const int src_size_10ms = src_sample_rate_hz_ * num_channels_ / 100;
|
||||
const int dst_size_10ms = dst_sample_rate_hz_ * num_channels_ / 100;
|
||||
if (src_length != src_size_10ms || dst_capacity < dst_size_10ms) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (use_sinc_resampler_) {
|
||||
return ResampleSinc(src, src_length, dst, dst_capacity);
|
||||
}
|
||||
|
||||
int resulting_length = 0;
|
||||
if (resampler_->Push(src, src_length, dst, dst_capacity,
|
||||
resulting_length) != 0) {
|
||||
return -1;
|
||||
}
|
||||
return resulting_length;
|
||||
}
|
||||
|
||||
int PushResampler::ResampleSinc(const int16_t* src, int src_length,
|
||||
int16_t* dst, int dst_capacity) {
|
||||
if (src_sample_rate_hz_ == dst_sample_rate_hz_) {
|
||||
// The old resampler provides this memcpy facility in the case of matching
|
||||
// sample rates, so reproduce it here for the sinc resampler.
|
||||
memcpy(dst, src, src_length * sizeof(int16_t));
|
||||
return src_length;
|
||||
}
|
||||
if (num_channels_ == 2) {
|
||||
const int src_length_mono = src_length / num_channels_;
|
||||
const int dst_capacity_mono = dst_capacity / num_channels_;
|
||||
int16_t* deinterleaved[] = {src_left_.get(), src_right_.get()};
|
||||
Deinterleave(src, src_length_mono, num_channels_, deinterleaved);
|
||||
|
||||
int dst_length_mono =
|
||||
sinc_resampler_->Resample(src_left_.get(), src_length_mono,
|
||||
dst_left_.get(), dst_capacity_mono);
|
||||
sinc_resampler_right_->Resample(src_right_.get(), src_length_mono,
|
||||
dst_right_.get(), dst_capacity_mono);
|
||||
|
||||
deinterleaved[0] = dst_left_.get();
|
||||
deinterleaved[1] = dst_right_.get();
|
||||
Interleave(deinterleaved, dst_length_mono, num_channels_, dst);
|
||||
return dst_length_mono * num_channels_;
|
||||
} else {
|
||||
return sinc_resampler_->Resample(src, src_length, dst, dst_capacity);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace webrtc
|
||||
Reference in New Issue
Block a user