update code.
This commit is contained in:
102
VocieProcess/common_audio/resampler/push_sinc_resampler.cc
Normal file
102
VocieProcess/common_audio/resampler/push_sinc_resampler.cc
Normal file
@ -0,0 +1,102 @@
|
||||
/*
|
||||
* 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 "common_audio/resampler/push_sinc_resampler.h"
|
||||
|
||||
#include <cstring>
|
||||
|
||||
#include "common_audio/include/audio_util.h"
|
||||
#include "rtc_base/checks.h"
|
||||
|
||||
namespace webrtc {
|
||||
|
||||
PushSincResampler::PushSincResampler(size_t source_frames,
|
||||
size_t destination_frames)
|
||||
: resampler_(new SincResampler(source_frames * 1.0 / destination_frames,
|
||||
source_frames,
|
||||
this)),
|
||||
source_ptr_(nullptr),
|
||||
source_ptr_int_(nullptr),
|
||||
destination_frames_(destination_frames),
|
||||
first_pass_(true),
|
||||
source_available_(0) {}
|
||||
|
||||
PushSincResampler::~PushSincResampler() {}
|
||||
|
||||
size_t PushSincResampler::Resample(const int16_t* source,
|
||||
size_t source_length,
|
||||
int16_t* destination,
|
||||
size_t destination_capacity) {
|
||||
if (!float_buffer_.get())
|
||||
float_buffer_.reset(new float[destination_frames_]);
|
||||
|
||||
source_ptr_int_ = source;
|
||||
// Pass nullptr as the float source to have Run() read from the int16 source.
|
||||
Resample(nullptr, source_length, float_buffer_.get(), destination_frames_);
|
||||
FloatS16ToS16(float_buffer_.get(), destination_frames_, destination);
|
||||
source_ptr_int_ = nullptr;
|
||||
return destination_frames_;
|
||||
}
|
||||
|
||||
size_t PushSincResampler::Resample(const float* source,
|
||||
size_t source_length,
|
||||
float* destination,
|
||||
size_t destination_capacity) {
|
||||
RTC_CHECK_EQ(source_length, resampler_->request_frames());
|
||||
RTC_CHECK_GE(destination_capacity, destination_frames_);
|
||||
// Cache the source pointer. Calling Resample() will immediately trigger
|
||||
// the Run() callback whereupon we provide the cached value.
|
||||
source_ptr_ = source;
|
||||
source_available_ = source_length;
|
||||
|
||||
// On the first pass, we call Resample() twice. During the first call, we
|
||||
// provide dummy input and discard the output. This is done to prime the
|
||||
// SincResampler buffer with the correct delay (half the kernel size), thereby
|
||||
// ensuring that all later Resample() calls will only result in one input
|
||||
// request through Run().
|
||||
//
|
||||
// If this wasn't done, SincResampler would call Run() twice on the first
|
||||
// pass, and we'd have to introduce an entire `source_frames` of delay, rather
|
||||
// than the minimum half kernel.
|
||||
//
|
||||
// It works out that ChunkSize() is exactly the amount of output we need to
|
||||
// request in order to prime the buffer with a single Run() request for
|
||||
// `source_frames`.
|
||||
if (first_pass_)
|
||||
resampler_->Resample(resampler_->ChunkSize(), destination);
|
||||
|
||||
resampler_->Resample(destination_frames_, destination);
|
||||
source_ptr_ = nullptr;
|
||||
return destination_frames_;
|
||||
}
|
||||
|
||||
void PushSincResampler::Run(size_t frames, float* destination) {
|
||||
// Ensure we are only asked for the available samples. This would fail if
|
||||
// Run() was triggered more than once per Resample() call.
|
||||
RTC_CHECK_EQ(source_available_, frames);
|
||||
|
||||
if (first_pass_) {
|
||||
// Provide dummy input on the first pass, the output of which will be
|
||||
// discarded, as described in Resample().
|
||||
std::memset(destination, 0, frames * sizeof(*destination));
|
||||
first_pass_ = false;
|
||||
return;
|
||||
}
|
||||
|
||||
if (source_ptr_) {
|
||||
std::memcpy(destination, source_ptr_, frames * sizeof(*destination));
|
||||
} else {
|
||||
for (size_t i = 0; i < frames; ++i)
|
||||
destination[i] = static_cast<float>(source_ptr_int_[i]);
|
||||
}
|
||||
source_available_ -= frames;
|
||||
}
|
||||
|
||||
} // namespace webrtc
|
88
VocieProcess/common_audio/resampler/push_sinc_resampler.h
Normal file
88
VocieProcess/common_audio/resampler/push_sinc_resampler.h
Normal file
@ -0,0 +1,88 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#ifndef COMMON_AUDIO_RESAMPLER_PUSH_SINC_RESAMPLER_H_
|
||||
#define COMMON_AUDIO_RESAMPLER_PUSH_SINC_RESAMPLER_H_
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include "api/audio/audio_view.h"
|
||||
#include "common_audio/resampler/sinc_resampler.h"
|
||||
|
||||
namespace webrtc {
|
||||
|
||||
// A thin wrapper over SincResampler to provide a push-based interface as
|
||||
// required by WebRTC. SincResampler uses a pull-based interface, and will
|
||||
// use SincResamplerCallback::Run() to request data upon a call to Resample().
|
||||
// These Run() calls will happen on the same thread Resample() is called on.
|
||||
class PushSincResampler : public SincResamplerCallback {
|
||||
public:
|
||||
// Provide the size of the source and destination blocks in samples. These
|
||||
// must correspond to the same time duration (typically 10 ms) as the sample
|
||||
// ratio is inferred from them.
|
||||
PushSincResampler(size_t source_frames, size_t destination_frames);
|
||||
~PushSincResampler() override;
|
||||
|
||||
PushSincResampler(const PushSincResampler&) = delete;
|
||||
PushSincResampler& operator=(const PushSincResampler&) = delete;
|
||||
|
||||
// Perform the resampling. `source_frames` must always equal the
|
||||
// `source_frames` provided at construction. `destination_capacity` must be
|
||||
// at least as large as `destination_frames`. Returns the number of samples
|
||||
// provided in destination (for convenience, since this will always be equal
|
||||
// to `destination_frames`).
|
||||
template <typename S, typename D>
|
||||
size_t Resample(const MonoView<S>& source, const MonoView<D>& destination) {
|
||||
return Resample(&source[0], SamplesPerChannel(source), &destination[0],
|
||||
SamplesPerChannel(destination));
|
||||
}
|
||||
|
||||
size_t Resample(const int16_t* source,
|
||||
size_t source_frames,
|
||||
int16_t* destination,
|
||||
size_t destination_capacity);
|
||||
size_t Resample(const float* source,
|
||||
size_t source_frames,
|
||||
float* destination,
|
||||
size_t destination_capacity);
|
||||
|
||||
// Delay due to the filter kernel. Essentially, the time after which an input
|
||||
// sample will appear in the resampled output.
|
||||
static float AlgorithmicDelaySeconds(int source_rate_hz) {
|
||||
return 1.f / source_rate_hz * SincResampler::kKernelSize / 2;
|
||||
}
|
||||
|
||||
protected:
|
||||
// Implements SincResamplerCallback.
|
||||
void Run(size_t frames, float* destination) override;
|
||||
|
||||
private:
|
||||
friend class PushSincResamplerTest;
|
||||
SincResampler* get_resampler_for_testing() { return resampler_.get(); }
|
||||
|
||||
std::unique_ptr<SincResampler> resampler_;
|
||||
std::unique_ptr<float[]> float_buffer_;
|
||||
const float* source_ptr_;
|
||||
const int16_t* source_ptr_int_;
|
||||
const size_t destination_frames_;
|
||||
|
||||
// True on the first call to Resample(), to prime the SincResampler buffer.
|
||||
bool first_pass_;
|
||||
|
||||
// Used to assert we are only requested for as much data as is available.
|
||||
size_t source_available_;
|
||||
};
|
||||
|
||||
} // namespace webrtc
|
||||
|
||||
#endif // COMMON_AUDIO_RESAMPLER_PUSH_SINC_RESAMPLER_H_
|
366
VocieProcess/common_audio/resampler/sinc_resampler.cc
Normal file
366
VocieProcess/common_audio/resampler/sinc_resampler.cc
Normal file
@ -0,0 +1,366 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
// Modified from the Chromium original:
|
||||
// src/media/base/sinc_resampler.cc
|
||||
|
||||
// Initial input buffer layout, dividing into regions r0_ to r4_ (note: r0_, r3_
|
||||
// and r4_ will move after the first load):
|
||||
//
|
||||
// |----------------|-----------------------------------------|----------------|
|
||||
//
|
||||
// request_frames_
|
||||
// <--------------------------------------------------------->
|
||||
// r0_ (during first load)
|
||||
//
|
||||
// kKernelSize / 2 kKernelSize / 2 kKernelSize / 2 kKernelSize / 2
|
||||
// <---------------> <---------------> <---------------> <--------------->
|
||||
// r1_ r2_ r3_ r4_
|
||||
//
|
||||
// block_size_ == r4_ - r2_
|
||||
// <--------------------------------------->
|
||||
//
|
||||
// request_frames_
|
||||
// <------------------ ... ----------------->
|
||||
// r0_ (during second load)
|
||||
//
|
||||
// On the second request r0_ slides to the right by kKernelSize / 2 and r3_, r4_
|
||||
// and block_size_ are reinitialized via step (3) in the algorithm below.
|
||||
//
|
||||
// These new regions remain constant until a Flush() occurs. While complicated,
|
||||
// this allows us to reduce jitter by always requesting the same amount from the
|
||||
// provided callback.
|
||||
//
|
||||
// The algorithm:
|
||||
//
|
||||
// 1) Allocate input_buffer of size: request_frames_ + kKernelSize; this ensures
|
||||
// there's enough room to read request_frames_ from the callback into region
|
||||
// r0_ (which will move between the first and subsequent passes).
|
||||
//
|
||||
// 2) Let r1_, r2_ each represent half the kernel centered around r0_:
|
||||
//
|
||||
// r0_ = input_buffer_ + kKernelSize / 2
|
||||
// r1_ = input_buffer_
|
||||
// r2_ = r0_
|
||||
//
|
||||
// r0_ is always request_frames_ in size. r1_, r2_ are kKernelSize / 2 in
|
||||
// size. r1_ must be zero initialized to avoid convolution with garbage (see
|
||||
// step (5) for why).
|
||||
//
|
||||
// 3) Let r3_, r4_ each represent half the kernel right aligned with the end of
|
||||
// r0_ and choose block_size_ as the distance in frames between r4_ and r2_:
|
||||
//
|
||||
// r3_ = r0_ + request_frames_ - kKernelSize
|
||||
// r4_ = r0_ + request_frames_ - kKernelSize / 2
|
||||
// block_size_ = r4_ - r2_ = request_frames_ - kKernelSize / 2
|
||||
//
|
||||
// 4) Consume request_frames_ frames into r0_.
|
||||
//
|
||||
// 5) Position kernel centered at start of r2_ and generate output frames until
|
||||
// the kernel is centered at the start of r4_ or we've finished generating
|
||||
// all the output frames.
|
||||
//
|
||||
// 6) Wrap left over data from the r3_ to r1_ and r4_ to r2_.
|
||||
//
|
||||
// 7) If we're on the second load, in order to avoid overwriting the frames we
|
||||
// just wrapped from r4_ we need to slide r0_ to the right by the size of
|
||||
// r4_, which is kKernelSize / 2:
|
||||
//
|
||||
// r0_ = r0_ + kKernelSize / 2 = input_buffer_ + kKernelSize
|
||||
//
|
||||
// r3_, r4_, and block_size_ then need to be reinitialized, so goto (3).
|
||||
//
|
||||
// 8) Else, if we're not on the second load, goto (4).
|
||||
//
|
||||
// Note: we're glossing over how the sub-sample handling works with
|
||||
// `virtual_source_idx_`, etc.
|
||||
|
||||
// MSVC++ requires this to be set before any other includes to get M_PI.
|
||||
#define _USE_MATH_DEFINES
|
||||
|
||||
#include "common_audio/resampler/sinc_resampler.h"
|
||||
|
||||
#include <math.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <limits>
|
||||
|
||||
#include "rtc_base/checks.h"
|
||||
#include "rtc_base/system/arch.h"
|
||||
#include "system_wrappers/include/cpu_features_wrapper.h" // kSSE2, WebRtc_G...
|
||||
|
||||
namespace webrtc {
|
||||
|
||||
namespace {
|
||||
|
||||
double SincScaleFactor(double io_ratio) {
|
||||
// `sinc_scale_factor` is basically the normalized cutoff frequency of the
|
||||
// low-pass filter.
|
||||
double sinc_scale_factor = io_ratio > 1.0 ? 1.0 / io_ratio : 1.0;
|
||||
|
||||
// The sinc function is an idealized brick-wall filter, but since we're
|
||||
// windowing it the transition from pass to stop does not happen right away.
|
||||
// So we should adjust the low pass filter cutoff slightly downward to avoid
|
||||
// some aliasing at the very high-end.
|
||||
// TODO(crogers): this value is empirical and to be more exact should vary
|
||||
// depending on kKernelSize.
|
||||
sinc_scale_factor *= 0.9;
|
||||
|
||||
return sinc_scale_factor;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
const size_t SincResampler::kKernelSize;
|
||||
|
||||
// If we know the minimum architecture at compile time, avoid CPU detection.
|
||||
void SincResampler::InitializeCPUSpecificFeatures() {
|
||||
#if defined(WEBRTC_HAS_NEON)
|
||||
convolve_proc_ = Convolve_NEON;
|
||||
#elif defined(WEBRTC_ARCH_X86_FAMILY)
|
||||
// Using AVX2 instead of SSE2 when AVX2/FMA3 supported.
|
||||
if (GetCPUInfo(kAVX2) && GetCPUInfo(kFMA3))
|
||||
convolve_proc_ = Convolve_AVX2;
|
||||
else if (GetCPUInfo(kSSE2))
|
||||
convolve_proc_ = Convolve_SSE;
|
||||
else
|
||||
convolve_proc_ = Convolve_C;
|
||||
#else
|
||||
// Unknown architecture.
|
||||
convolve_proc_ = Convolve_C;
|
||||
#endif
|
||||
}
|
||||
|
||||
SincResampler::SincResampler(double io_sample_rate_ratio,
|
||||
size_t request_frames,
|
||||
SincResamplerCallback* read_cb)
|
||||
: io_sample_rate_ratio_(io_sample_rate_ratio),
|
||||
read_cb_(read_cb),
|
||||
request_frames_(request_frames),
|
||||
input_buffer_size_(request_frames_ + kKernelSize),
|
||||
// Create input buffers with a 32-byte alignment for SIMD optimizations.
|
||||
kernel_storage_(static_cast<float*>(
|
||||
AlignedMalloc(sizeof(float) * kKernelStorageSize, 32))),
|
||||
kernel_pre_sinc_storage_(static_cast<float*>(
|
||||
AlignedMalloc(sizeof(float) * kKernelStorageSize, 32))),
|
||||
kernel_window_storage_(static_cast<float*>(
|
||||
AlignedMalloc(sizeof(float) * kKernelStorageSize, 32))),
|
||||
input_buffer_(static_cast<float*>(
|
||||
AlignedMalloc(sizeof(float) * input_buffer_size_, 32))),
|
||||
convolve_proc_(nullptr),
|
||||
r1_(input_buffer_.get()),
|
||||
r2_(input_buffer_.get() + kKernelSize / 2) {
|
||||
InitializeCPUSpecificFeatures();
|
||||
RTC_DCHECK(convolve_proc_);
|
||||
RTC_DCHECK_GT(request_frames_, 0);
|
||||
Flush();
|
||||
RTC_DCHECK_GT(block_size_, kKernelSize);
|
||||
|
||||
memset(kernel_storage_.get(), 0,
|
||||
sizeof(*kernel_storage_.get()) * kKernelStorageSize);
|
||||
memset(kernel_pre_sinc_storage_.get(), 0,
|
||||
sizeof(*kernel_pre_sinc_storage_.get()) * kKernelStorageSize);
|
||||
memset(kernel_window_storage_.get(), 0,
|
||||
sizeof(*kernel_window_storage_.get()) * kKernelStorageSize);
|
||||
|
||||
InitializeKernel();
|
||||
}
|
||||
|
||||
SincResampler::~SincResampler() {}
|
||||
|
||||
void SincResampler::UpdateRegions(bool second_load) {
|
||||
// Setup various region pointers in the buffer (see diagram above). If we're
|
||||
// on the second load we need to slide r0_ to the right by kKernelSize / 2.
|
||||
r0_ = input_buffer_.get() + (second_load ? kKernelSize : kKernelSize / 2);
|
||||
r3_ = r0_ + request_frames_ - kKernelSize;
|
||||
r4_ = r0_ + request_frames_ - kKernelSize / 2;
|
||||
block_size_ = r4_ - r2_;
|
||||
|
||||
// r1_ at the beginning of the buffer.
|
||||
RTC_DCHECK_EQ(r1_, input_buffer_.get());
|
||||
// r1_ left of r2_, r4_ left of r3_ and size correct.
|
||||
RTC_DCHECK_EQ(r2_ - r1_, r4_ - r3_);
|
||||
// r2_ left of r3.
|
||||
RTC_DCHECK_LT(r2_, r3_);
|
||||
}
|
||||
|
||||
void SincResampler::InitializeKernel() {
|
||||
// Blackman window parameters.
|
||||
static const double kAlpha = 0.16;
|
||||
static const double kA0 = 0.5 * (1.0 - kAlpha);
|
||||
static const double kA1 = 0.5;
|
||||
static const double kA2 = 0.5 * kAlpha;
|
||||
|
||||
// Generates a set of windowed sinc() kernels.
|
||||
// We generate a range of sub-sample offsets from 0.0 to 1.0.
|
||||
const double sinc_scale_factor = SincScaleFactor(io_sample_rate_ratio_);
|
||||
for (size_t offset_idx = 0; offset_idx <= kKernelOffsetCount; ++offset_idx) {
|
||||
const float subsample_offset =
|
||||
static_cast<float>(offset_idx) / kKernelOffsetCount;
|
||||
|
||||
for (size_t i = 0; i < kKernelSize; ++i) {
|
||||
const size_t idx = i + offset_idx * kKernelSize;
|
||||
const float pre_sinc = static_cast<float>(
|
||||
M_PI * (static_cast<int>(i) - static_cast<int>(kKernelSize / 2) -
|
||||
subsample_offset));
|
||||
kernel_pre_sinc_storage_[idx] = pre_sinc;
|
||||
|
||||
// Compute Blackman window, matching the offset of the sinc().
|
||||
const float x = (i - subsample_offset) / kKernelSize;
|
||||
const float window = static_cast<float>(kA0 - kA1 * cos(2.0 * M_PI * x) +
|
||||
kA2 * cos(4.0 * M_PI * x));
|
||||
kernel_window_storage_[idx] = window;
|
||||
|
||||
// Compute the sinc with offset, then window the sinc() function and store
|
||||
// at the correct offset.
|
||||
kernel_storage_[idx] = static_cast<float>(
|
||||
window * ((pre_sinc == 0)
|
||||
? sinc_scale_factor
|
||||
: (sin(sinc_scale_factor * pre_sinc) / pre_sinc)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void SincResampler::SetRatio(double io_sample_rate_ratio) {
|
||||
if (fabs(io_sample_rate_ratio_ - io_sample_rate_ratio) <
|
||||
std::numeric_limits<double>::epsilon()) {
|
||||
return;
|
||||
}
|
||||
|
||||
io_sample_rate_ratio_ = io_sample_rate_ratio;
|
||||
|
||||
// Optimize reinitialization by reusing values which are independent of
|
||||
// `sinc_scale_factor`. Provides a 3x speedup.
|
||||
const double sinc_scale_factor = SincScaleFactor(io_sample_rate_ratio_);
|
||||
for (size_t offset_idx = 0; offset_idx <= kKernelOffsetCount; ++offset_idx) {
|
||||
for (size_t i = 0; i < kKernelSize; ++i) {
|
||||
const size_t idx = i + offset_idx * kKernelSize;
|
||||
const float window = kernel_window_storage_[idx];
|
||||
const float pre_sinc = kernel_pre_sinc_storage_[idx];
|
||||
|
||||
kernel_storage_[idx] = static_cast<float>(
|
||||
window * ((pre_sinc == 0)
|
||||
? sinc_scale_factor
|
||||
: (sin(sinc_scale_factor * pre_sinc) / pre_sinc)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void SincResampler::Resample(size_t frames, float* destination) {
|
||||
size_t remaining_frames = frames;
|
||||
|
||||
// Step (1) -- Prime the input buffer at the start of the input stream.
|
||||
if (!buffer_primed_ && remaining_frames) {
|
||||
read_cb_->Run(request_frames_, r0_);
|
||||
buffer_primed_ = true;
|
||||
}
|
||||
|
||||
// Step (2) -- Resample! const what we can outside of the loop for speed. It
|
||||
// actually has an impact on ARM performance. See inner loop comment below.
|
||||
const double current_io_ratio = io_sample_rate_ratio_;
|
||||
const float* const kernel_ptr = kernel_storage_.get();
|
||||
while (remaining_frames) {
|
||||
// `i` may be negative if the last Resample() call ended on an iteration
|
||||
// that put `virtual_source_idx_` over the limit.
|
||||
//
|
||||
// Note: The loop construct here can severely impact performance on ARM
|
||||
// or when built with clang. See https://codereview.chromium.org/18566009/
|
||||
for (int i = static_cast<int>(
|
||||
ceil((block_size_ - virtual_source_idx_) / current_io_ratio));
|
||||
i > 0; --i) {
|
||||
RTC_DCHECK_LT(virtual_source_idx_, block_size_);
|
||||
|
||||
// `virtual_source_idx_` lies in between two kernel offsets so figure out
|
||||
// what they are.
|
||||
const int source_idx = static_cast<int>(virtual_source_idx_);
|
||||
const double subsample_remainder = virtual_source_idx_ - source_idx;
|
||||
|
||||
const double virtual_offset_idx =
|
||||
subsample_remainder * kKernelOffsetCount;
|
||||
const int offset_idx = static_cast<int>(virtual_offset_idx);
|
||||
|
||||
// We'll compute "convolutions" for the two kernels which straddle
|
||||
// `virtual_source_idx_`.
|
||||
const float* const k1 = kernel_ptr + offset_idx * kKernelSize;
|
||||
const float* const k2 = k1 + kKernelSize;
|
||||
|
||||
// Ensure `k1`, `k2` are 32-byte aligned for SIMD usage. Should always be
|
||||
// true so long as kKernelSize is a multiple of 32.
|
||||
RTC_DCHECK_EQ(0, reinterpret_cast<uintptr_t>(k1) % 32);
|
||||
RTC_DCHECK_EQ(0, reinterpret_cast<uintptr_t>(k2) % 32);
|
||||
|
||||
// Initialize input pointer based on quantized `virtual_source_idx_`.
|
||||
const float* const input_ptr = r1_ + source_idx;
|
||||
|
||||
// Figure out how much to weight each kernel's "convolution".
|
||||
const double kernel_interpolation_factor =
|
||||
virtual_offset_idx - offset_idx;
|
||||
*destination++ =
|
||||
convolve_proc_(input_ptr, k1, k2, kernel_interpolation_factor);
|
||||
|
||||
// Advance the virtual index.
|
||||
virtual_source_idx_ += current_io_ratio;
|
||||
|
||||
if (!--remaining_frames)
|
||||
return;
|
||||
}
|
||||
|
||||
// Wrap back around to the start.
|
||||
virtual_source_idx_ -= block_size_;
|
||||
|
||||
// Step (3) -- Copy r3_, r4_ to r1_, r2_.
|
||||
// This wraps the last input frames back to the start of the buffer.
|
||||
memcpy(r1_, r3_, sizeof(*input_buffer_.get()) * kKernelSize);
|
||||
|
||||
// Step (4) -- Reinitialize regions if necessary.
|
||||
if (r0_ == r2_)
|
||||
UpdateRegions(true);
|
||||
|
||||
// Step (5) -- Refresh the buffer with more input.
|
||||
read_cb_->Run(request_frames_, r0_);
|
||||
}
|
||||
}
|
||||
|
||||
#undef CONVOLVE_FUNC
|
||||
|
||||
size_t SincResampler::ChunkSize() const {
|
||||
return static_cast<size_t>(block_size_ / io_sample_rate_ratio_);
|
||||
}
|
||||
|
||||
void SincResampler::Flush() {
|
||||
virtual_source_idx_ = 0;
|
||||
buffer_primed_ = false;
|
||||
memset(input_buffer_.get(), 0,
|
||||
sizeof(*input_buffer_.get()) * input_buffer_size_);
|
||||
UpdateRegions(false);
|
||||
}
|
||||
|
||||
float SincResampler::Convolve_C(const float* input_ptr,
|
||||
const float* k1,
|
||||
const float* k2,
|
||||
double kernel_interpolation_factor) {
|
||||
float sum1 = 0;
|
||||
float sum2 = 0;
|
||||
|
||||
// Generate a single output sample. Unrolling this loop hurt performance in
|
||||
// local testing.
|
||||
size_t n = kKernelSize;
|
||||
while (n--) {
|
||||
sum1 += *input_ptr * *k1++;
|
||||
sum2 += *input_ptr++ * *k2++;
|
||||
}
|
||||
|
||||
// Linearly interpolate the two "convolutions".
|
||||
return static_cast<float>((1.0 - kernel_interpolation_factor) * sum1 +
|
||||
kernel_interpolation_factor * sum2);
|
||||
}
|
||||
|
||||
} // namespace webrtc
|
181
VocieProcess/common_audio/resampler/sinc_resampler.h
Normal file
181
VocieProcess/common_audio/resampler/sinc_resampler.h
Normal file
@ -0,0 +1,181 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
// Modified from the Chromium original here:
|
||||
// src/media/base/sinc_resampler.h
|
||||
|
||||
#ifndef COMMON_AUDIO_RESAMPLER_SINC_RESAMPLER_H_
|
||||
#define COMMON_AUDIO_RESAMPLER_SINC_RESAMPLER_H_
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include "rtc_base/gtest_prod_util.h"
|
||||
#include "rtc_base/memory/aligned_malloc.h"
|
||||
#include "rtc_base/system/arch.h"
|
||||
|
||||
namespace webrtc {
|
||||
|
||||
// Callback class for providing more data into the resampler. Expects `frames`
|
||||
// of data to be rendered into `destination`; zero padded if not enough frames
|
||||
// are available to satisfy the request.
|
||||
class SincResamplerCallback {
|
||||
public:
|
||||
virtual ~SincResamplerCallback() {}
|
||||
virtual void Run(size_t frames, float* destination) = 0;
|
||||
};
|
||||
|
||||
// SincResampler is a high-quality single-channel sample-rate converter.
|
||||
class SincResampler {
|
||||
public:
|
||||
// The kernel size can be adjusted for quality (higher is better) at the
|
||||
// expense of performance. Must be a multiple of 32.
|
||||
// TODO(dalecurtis): Test performance to see if we can jack this up to 64+.
|
||||
static const size_t kKernelSize = 32;
|
||||
|
||||
// Default request size. Affects how often and for how much SincResampler
|
||||
// calls back for input. Must be greater than kKernelSize.
|
||||
static const size_t kDefaultRequestSize = 512;
|
||||
|
||||
// The kernel offset count is used for interpolation and is the number of
|
||||
// sub-sample kernel shifts. Can be adjusted for quality (higher is better)
|
||||
// at the expense of allocating more memory.
|
||||
static const size_t kKernelOffsetCount = 32;
|
||||
static const size_t kKernelStorageSize =
|
||||
kKernelSize * (kKernelOffsetCount + 1);
|
||||
|
||||
// Constructs a SincResampler with the specified `read_cb`, which is used to
|
||||
// acquire audio data for resampling. `io_sample_rate_ratio` is the ratio
|
||||
// of input / output sample rates. `request_frames` controls the size in
|
||||
// frames of the buffer requested by each `read_cb` call. The value must be
|
||||
// greater than kKernelSize. Specify kDefaultRequestSize if there are no
|
||||
// request size constraints.
|
||||
SincResampler(double io_sample_rate_ratio,
|
||||
size_t request_frames,
|
||||
SincResamplerCallback* read_cb);
|
||||
virtual ~SincResampler();
|
||||
|
||||
SincResampler(const SincResampler&) = delete;
|
||||
SincResampler& operator=(const SincResampler&) = delete;
|
||||
|
||||
// Resample `frames` of data from `read_cb_` into `destination`.
|
||||
void Resample(size_t frames, float* destination);
|
||||
|
||||
// The maximum size in frames that guarantees Resample() will only make a
|
||||
// single call to `read_cb_` for more data.
|
||||
size_t ChunkSize() const;
|
||||
|
||||
size_t request_frames() const { return request_frames_; }
|
||||
|
||||
// Flush all buffered data and reset internal indices. Not thread safe, do
|
||||
// not call while Resample() is in progress.
|
||||
void Flush();
|
||||
|
||||
// Update `io_sample_rate_ratio_`. SetRatio() will cause a reconstruction of
|
||||
// the kernels used for resampling. Not thread safe, do not call while
|
||||
// Resample() is in progress.
|
||||
//
|
||||
// TODO(ajm): Use this in PushSincResampler rather than reconstructing
|
||||
// SincResampler. We would also need a way to update `request_frames_`.
|
||||
void SetRatio(double io_sample_rate_ratio);
|
||||
|
||||
float* get_kernel_for_testing() { return kernel_storage_.get(); }
|
||||
|
||||
private:
|
||||
FRIEND_TEST_ALL_PREFIXES(SincResamplerTest, Convolve);
|
||||
FRIEND_TEST_ALL_PREFIXES(SincResamplerTest, ConvolveBenchmark);
|
||||
|
||||
void InitializeKernel();
|
||||
void UpdateRegions(bool second_load);
|
||||
|
||||
// Selects runtime specific CPU features like SSE. Must be called before
|
||||
// using SincResampler.
|
||||
// TODO(ajm): Currently managed by the class internally. See the note with
|
||||
// `convolve_proc_` below.
|
||||
void InitializeCPUSpecificFeatures();
|
||||
|
||||
// Compute convolution of `k1` and `k2` over `input_ptr`, resultant sums are
|
||||
// linearly interpolated using `kernel_interpolation_factor`. On x86 and ARM
|
||||
// the underlying implementation is chosen at run time.
|
||||
static float Convolve_C(const float* input_ptr,
|
||||
const float* k1,
|
||||
const float* k2,
|
||||
double kernel_interpolation_factor);
|
||||
#if defined(WEBRTC_ARCH_X86_FAMILY)
|
||||
static float Convolve_SSE(const float* input_ptr,
|
||||
const float* k1,
|
||||
const float* k2,
|
||||
double kernel_interpolation_factor);
|
||||
static float Convolve_AVX2(const float* input_ptr,
|
||||
const float* k1,
|
||||
const float* k2,
|
||||
double kernel_interpolation_factor);
|
||||
#elif defined(WEBRTC_HAS_NEON)
|
||||
static float Convolve_NEON(const float* input_ptr,
|
||||
const float* k1,
|
||||
const float* k2,
|
||||
double kernel_interpolation_factor);
|
||||
#endif
|
||||
|
||||
// The ratio of input / output sample rates.
|
||||
double io_sample_rate_ratio_;
|
||||
|
||||
// An index on the source input buffer with sub-sample precision. It must be
|
||||
// double precision to avoid drift.
|
||||
double virtual_source_idx_;
|
||||
|
||||
// The buffer is primed once at the very beginning of processing.
|
||||
bool buffer_primed_;
|
||||
|
||||
// Source of data for resampling.
|
||||
SincResamplerCallback* read_cb_;
|
||||
|
||||
// The size (in samples) to request from each `read_cb_` execution.
|
||||
const size_t request_frames_;
|
||||
|
||||
// The number of source frames processed per pass.
|
||||
size_t block_size_;
|
||||
|
||||
// The size (in samples) of the internal buffer used by the resampler.
|
||||
const size_t input_buffer_size_;
|
||||
|
||||
// Contains kKernelOffsetCount kernels back-to-back, each of size kKernelSize.
|
||||
// The kernel offsets are sub-sample shifts of a windowed sinc shifted from
|
||||
// 0.0 to 1.0 sample.
|
||||
std::unique_ptr<float[], AlignedFreeDeleter> kernel_storage_;
|
||||
std::unique_ptr<float[], AlignedFreeDeleter> kernel_pre_sinc_storage_;
|
||||
std::unique_ptr<float[], AlignedFreeDeleter> kernel_window_storage_;
|
||||
|
||||
// Data from the source is copied into this buffer for each processing pass.
|
||||
std::unique_ptr<float[], AlignedFreeDeleter> input_buffer_;
|
||||
|
||||
// Stores the runtime selection of which Convolve function to use.
|
||||
// TODO(ajm): Move to using a global static which must only be initialized
|
||||
// once by the user. We're not doing this initially, because we don't have
|
||||
// e.g. a LazyInstance helper in webrtc.
|
||||
typedef float (*ConvolveProc)(const float*,
|
||||
const float*,
|
||||
const float*,
|
||||
double);
|
||||
ConvolveProc convolve_proc_;
|
||||
|
||||
// Pointers to the various regions inside `input_buffer_`. See the diagram at
|
||||
// the top of the .cc file for more information.
|
||||
float* r0_;
|
||||
float* const r1_;
|
||||
float* const r2_;
|
||||
float* r3_;
|
||||
float* r4_;
|
||||
};
|
||||
|
||||
} // namespace webrtc
|
||||
|
||||
#endif // COMMON_AUDIO_RESAMPLER_SINC_RESAMPLER_H_
|
Reference in New Issue
Block a user