C++ readability review for ajm.
As part of the review, refactored AudioConverter into internal derived classes, each focused on one type of conversion. A factory method returns the correct converter (or chain of converters, via CompositionConverter). BUG=b/18938079 R=rojer@google.com Review URL: https://webrtc-codereview.appspot.com/35699004 Cr-Commit-Position: refs/heads/master@{#8322} git-svn-id: http://webrtc.googlecode.com/svn/trunk@8322 4adac7df-926f-26a2-2b94-8c16560cd09d
This commit is contained in:
parent
5d608955cf
commit
2c29c2eae2
@ -8,39 +8,179 @@
|
|||||||
* be found in the AUTHORS file in the root of the source tree.
|
* be found in the AUTHORS file in the root of the source tree.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "webrtc/base/checks.h"
|
|
||||||
#include "webrtc/common_audio/audio_converter.h"
|
#include "webrtc/common_audio/audio_converter.h"
|
||||||
|
|
||||||
|
#include <cstring>
|
||||||
|
|
||||||
|
#include "webrtc/base/checks.h"
|
||||||
|
#include "webrtc/base/safe_conversions.h"
|
||||||
|
#include "webrtc/common_audio/channel_buffer.h"
|
||||||
#include "webrtc/common_audio/resampler/push_sinc_resampler.h"
|
#include "webrtc/common_audio/resampler/push_sinc_resampler.h"
|
||||||
|
#include "webrtc/system_wrappers/interface/scoped_vector.h"
|
||||||
|
|
||||||
|
using rtc::checked_cast;
|
||||||
|
|
||||||
namespace webrtc {
|
namespace webrtc {
|
||||||
namespace {
|
|
||||||
|
|
||||||
void DownmixToMono(const float* const* src,
|
class CopyConverter : public AudioConverter {
|
||||||
int src_channels,
|
public:
|
||||||
int frames,
|
CopyConverter(int src_channels, int src_frames, int dst_channels,
|
||||||
float* dst) {
|
int dst_frames)
|
||||||
DCHECK_GT(src_channels, 0);
|
: AudioConverter(src_channels, src_frames, dst_channels, dst_frames) {}
|
||||||
for (int i = 0; i < frames; ++i) {
|
~CopyConverter() override {};
|
||||||
float sum = 0;
|
|
||||||
for (int j = 0; j < src_channels; ++j)
|
void Convert(const float* const* src, size_t src_size, float* const* dst,
|
||||||
sum += src[j][i];
|
size_t dst_capacity) override {
|
||||||
dst[i] = sum / src_channels;
|
CheckSizes(src_size, dst_capacity);
|
||||||
|
if (src != dst) {
|
||||||
|
for (int i = 0; i < src_channels(); ++i)
|
||||||
|
std::memcpy(dst[i], src[i], dst_frames() * sizeof(*dst[i]));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
class UpmixConverter : public AudioConverter {
|
||||||
|
public:
|
||||||
|
UpmixConverter(int src_channels, int src_frames, int dst_channels,
|
||||||
|
int dst_frames)
|
||||||
|
: AudioConverter(src_channels, src_frames, dst_channels, dst_frames) {}
|
||||||
|
~UpmixConverter() override {};
|
||||||
|
|
||||||
|
void Convert(const float* const* src, size_t src_size, float* const* dst,
|
||||||
|
size_t dst_capacity) override {
|
||||||
|
CheckSizes(src_size, dst_capacity);
|
||||||
|
for (int i = 0; i < dst_frames(); ++i) {
|
||||||
|
const float value = src[0][i];
|
||||||
|
for (int j = 0; j < dst_channels(); ++j)
|
||||||
|
dst[j][i] = value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
class DownmixConverter : public AudioConverter {
|
||||||
|
public:
|
||||||
|
DownmixConverter(int src_channels, int src_frames, int dst_channels,
|
||||||
|
int dst_frames)
|
||||||
|
: AudioConverter(src_channels, src_frames, dst_channels, dst_frames) {
|
||||||
|
}
|
||||||
|
~DownmixConverter() override {};
|
||||||
|
|
||||||
|
void Convert(const float* const* src, size_t src_size, float* const* dst,
|
||||||
|
size_t dst_capacity) override {
|
||||||
|
CheckSizes(src_size, dst_capacity);
|
||||||
|
float* dst_mono = dst[0];
|
||||||
|
for (int i = 0; i < src_frames(); ++i) {
|
||||||
|
float sum = 0;
|
||||||
|
for (int j = 0; j < src_channels(); ++j)
|
||||||
|
sum += src[j][i];
|
||||||
|
dst_mono[i] = sum / src_channels();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
class ResampleConverter : public AudioConverter {
|
||||||
|
public:
|
||||||
|
ResampleConverter(int src_channels, int src_frames, int dst_channels,
|
||||||
|
int dst_frames)
|
||||||
|
: AudioConverter(src_channels, src_frames, dst_channels, dst_frames) {
|
||||||
|
resamplers_.reserve(src_channels);
|
||||||
|
for (int i = 0; i < src_channels; ++i)
|
||||||
|
resamplers_.push_back(new PushSincResampler(src_frames, dst_frames));
|
||||||
|
}
|
||||||
|
~ResampleConverter() override {};
|
||||||
|
|
||||||
|
void Convert(const float* const* src, size_t src_size, float* const* dst,
|
||||||
|
size_t dst_capacity) override {
|
||||||
|
CheckSizes(src_size, dst_capacity);
|
||||||
|
for (size_t i = 0; i < resamplers_.size(); ++i)
|
||||||
|
resamplers_[i]->Resample(src[i], src_frames(), dst[i], dst_frames());
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
ScopedVector<PushSincResampler> resamplers_;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Apply a vector of converters in serial, in the order given. At least two
|
||||||
|
// converters must be provided.
|
||||||
|
class CompositionConverter : public AudioConverter {
|
||||||
|
public:
|
||||||
|
CompositionConverter(ScopedVector<AudioConverter> converters)
|
||||||
|
: converters_(converters.Pass()) {
|
||||||
|
CHECK_GE(converters_.size(), 2u);
|
||||||
|
// We need an intermediate buffer after every converter.
|
||||||
|
for (auto it = converters_.begin(); it != converters_.end() - 1; ++it)
|
||||||
|
buffers_.push_back(new ChannelBuffer<float>((*it)->dst_frames(),
|
||||||
|
(*it)->dst_channels()));
|
||||||
|
}
|
||||||
|
~CompositionConverter() override {};
|
||||||
|
|
||||||
|
void Convert(const float* const* src, size_t src_size, float* const* dst,
|
||||||
|
size_t dst_capacity) override {
|
||||||
|
converters_.front()->Convert(src, src_size, buffers_.front()->channels(),
|
||||||
|
buffers_.front()->size());
|
||||||
|
for (size_t i = 2; i < converters_.size(); ++i) {
|
||||||
|
auto src_buffer = buffers_[i - 2];
|
||||||
|
auto dst_buffer = buffers_[i - 1];
|
||||||
|
converters_[i]->Convert(src_buffer->channels(),
|
||||||
|
src_buffer->size(),
|
||||||
|
dst_buffer->channels(),
|
||||||
|
dst_buffer->size());
|
||||||
|
}
|
||||||
|
converters_.back()->Convert(buffers_.back()->channels(),
|
||||||
|
buffers_.back()->size(), dst, dst_capacity);
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
ScopedVector<AudioConverter> converters_;
|
||||||
|
ScopedVector<ChannelBuffer<float>> buffers_;
|
||||||
|
};
|
||||||
|
|
||||||
|
scoped_ptr<AudioConverter> AudioConverter::Create(int src_channels,
|
||||||
|
int src_frames,
|
||||||
|
int dst_channels,
|
||||||
|
int dst_frames) {
|
||||||
|
scoped_ptr<AudioConverter> sp;
|
||||||
|
if (src_channels > dst_channels) {
|
||||||
|
if (src_frames != dst_frames) {
|
||||||
|
ScopedVector<AudioConverter> converters;
|
||||||
|
converters.push_back(new DownmixConverter(src_channels, src_frames,
|
||||||
|
dst_channels, src_frames));
|
||||||
|
converters.push_back(new ResampleConverter(dst_channels, src_frames,
|
||||||
|
dst_channels, dst_frames));
|
||||||
|
sp.reset(new CompositionConverter(converters.Pass()));
|
||||||
|
} else {
|
||||||
|
sp.reset(new DownmixConverter(src_channels, src_frames, dst_channels,
|
||||||
|
dst_frames));
|
||||||
|
}
|
||||||
|
} else if (src_channels < dst_channels) {
|
||||||
|
if (src_frames != dst_frames) {
|
||||||
|
ScopedVector<AudioConverter> converters;
|
||||||
|
converters.push_back(new ResampleConverter(src_channels, src_frames,
|
||||||
|
src_channels, dst_frames));
|
||||||
|
converters.push_back(new UpmixConverter(src_channels, dst_frames,
|
||||||
|
dst_channels, dst_frames));
|
||||||
|
sp.reset(new CompositionConverter(converters.Pass()));
|
||||||
|
} else {
|
||||||
|
sp.reset(new UpmixConverter(src_channels, src_frames, dst_channels,
|
||||||
|
dst_frames));
|
||||||
|
}
|
||||||
|
} else if (src_frames != dst_frames) {
|
||||||
|
sp.reset(new ResampleConverter(src_channels, src_frames, dst_channels,
|
||||||
|
dst_frames));
|
||||||
|
} else {
|
||||||
|
sp.reset(new CopyConverter(src_channels, src_frames, dst_channels,
|
||||||
|
dst_frames));
|
||||||
|
}
|
||||||
|
|
||||||
|
return sp.Pass();
|
||||||
}
|
}
|
||||||
|
|
||||||
void UpmixFromMono(const float* src,
|
// For CompositionConverter.
|
||||||
int dst_channels,
|
AudioConverter::AudioConverter()
|
||||||
int frames,
|
: src_channels_(0),
|
||||||
float* const* dst) {
|
src_frames_(0),
|
||||||
DCHECK_GT(dst_channels, 0);
|
dst_channels_(0),
|
||||||
for (int i = 0; i < frames; ++i) {
|
dst_frames_(0) {}
|
||||||
float value = src[i];
|
|
||||||
for (int j = 0; j < dst_channels; ++j)
|
|
||||||
dst[j][i] = value;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace
|
|
||||||
|
|
||||||
AudioConverter::AudioConverter(int src_channels, int src_frames,
|
AudioConverter::AudioConverter(int src_channels, int src_frames,
|
||||||
int dst_channels, int dst_frames)
|
int dst_channels, int dst_frames)
|
||||||
@ -49,62 +189,11 @@ AudioConverter::AudioConverter(int src_channels, int src_frames,
|
|||||||
dst_channels_(dst_channels),
|
dst_channels_(dst_channels),
|
||||||
dst_frames_(dst_frames) {
|
dst_frames_(dst_frames) {
|
||||||
CHECK(dst_channels == src_channels || dst_channels == 1 || src_channels == 1);
|
CHECK(dst_channels == src_channels || dst_channels == 1 || src_channels == 1);
|
||||||
const int resample_channels = std::min(src_channels, dst_channels);
|
|
||||||
|
|
||||||
// Prepare buffers as needed for intermediate stages.
|
|
||||||
if (dst_channels < src_channels)
|
|
||||||
downmix_buffer_.reset(new ChannelBuffer<float>(src_frames,
|
|
||||||
resample_channels));
|
|
||||||
|
|
||||||
if (src_frames != dst_frames) {
|
|
||||||
resamplers_.reserve(resample_channels);
|
|
||||||
for (int i = 0; i < resample_channels; ++i)
|
|
||||||
resamplers_.push_back(new PushSincResampler(src_frames, dst_frames));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void AudioConverter::Convert(const float* const* src,
|
void AudioConverter::CheckSizes(size_t src_size, size_t dst_capacity) const {
|
||||||
int src_channels,
|
CHECK_EQ(src_size, checked_cast<size_t>(src_channels() * src_frames()));
|
||||||
int src_frames,
|
CHECK_GE(dst_capacity, checked_cast<size_t>(dst_channels() * dst_frames()));
|
||||||
int dst_channels,
|
|
||||||
int dst_frames,
|
|
||||||
float* const* dst) {
|
|
||||||
DCHECK_EQ(src_channels_, src_channels);
|
|
||||||
DCHECK_EQ(src_frames_, src_frames);
|
|
||||||
DCHECK_EQ(dst_channels_, dst_channels);
|
|
||||||
DCHECK_EQ(dst_frames_, dst_frames);;
|
|
||||||
|
|
||||||
if (src_channels == dst_channels && src_frames == dst_frames) {
|
|
||||||
// Shortcut copy.
|
|
||||||
if (src != dst) {
|
|
||||||
for (int i = 0; i < src_channels; ++i)
|
|
||||||
memcpy(dst[i], src[i], dst_frames * sizeof(*dst[i]));
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const float* const* src_ptr = src;
|
|
||||||
if (dst_channels < src_channels) {
|
|
||||||
float* const* dst_ptr = dst;
|
|
||||||
if (src_frames != dst_frames) {
|
|
||||||
// Downmix to a buffer for subsequent resampling.
|
|
||||||
DCHECK_EQ(downmix_buffer_->num_channels(), dst_channels);
|
|
||||||
DCHECK_EQ(downmix_buffer_->num_frames(), src_frames);
|
|
||||||
dst_ptr = downmix_buffer_->channels();
|
|
||||||
}
|
|
||||||
|
|
||||||
DownmixToMono(src, src_channels, src_frames, dst_ptr[0]);
|
|
||||||
src_ptr = dst_ptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (src_frames != dst_frames) {
|
|
||||||
for (size_t i = 0; i < resamplers_.size(); ++i)
|
|
||||||
resamplers_[i]->Resample(src_ptr[i], src_frames, dst[i], dst_frames);
|
|
||||||
src_ptr = dst;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (dst_channels > src_channels)
|
|
||||||
UpmixFromMono(src_ptr[0], dst_channels, dst_frames, dst);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace webrtc
|
} // namespace webrtc
|
||||||
|
@ -11,16 +11,11 @@
|
|||||||
#ifndef WEBRTC_COMMON_AUDIO_AUDIO_CONVERTER_H_
|
#ifndef WEBRTC_COMMON_AUDIO_AUDIO_CONVERTER_H_
|
||||||
#define WEBRTC_COMMON_AUDIO_AUDIO_CONVERTER_H_
|
#define WEBRTC_COMMON_AUDIO_AUDIO_CONVERTER_H_
|
||||||
|
|
||||||
// TODO(ajm): Move channel buffer to common_audio.
|
|
||||||
#include "webrtc/base/constructormagic.h"
|
#include "webrtc/base/constructormagic.h"
|
||||||
#include "webrtc/common_audio/channel_buffer.h"
|
|
||||||
#include "webrtc/system_wrappers/interface/scoped_ptr.h"
|
#include "webrtc/system_wrappers/interface/scoped_ptr.h"
|
||||||
#include "webrtc/system_wrappers/interface/scoped_vector.h"
|
|
||||||
|
|
||||||
namespace webrtc {
|
namespace webrtc {
|
||||||
|
|
||||||
class PushSincResampler;
|
|
||||||
|
|
||||||
// Format conversion (remixing and resampling) for audio. Only simple remixing
|
// Format conversion (remixing and resampling) for audio. Only simple remixing
|
||||||
// conversions are supported: downmix to mono (i.e. |dst_channels| == 1) or
|
// conversions are supported: downmix to mono (i.e. |dst_channels| == 1) or
|
||||||
// upmix from mono (i.e. |src_channels == 1|).
|
// upmix from mono (i.e. |src_channels == 1|).
|
||||||
@ -29,23 +24,37 @@ class PushSincResampler;
|
|||||||
// the number of frames is equivalent to specifying the sample rates.
|
// the number of frames is equivalent to specifying the sample rates.
|
||||||
class AudioConverter {
|
class AudioConverter {
|
||||||
public:
|
public:
|
||||||
AudioConverter(int src_channels, int src_frames,
|
// Returns a new AudioConverter, which will use the supplied format for its
|
||||||
int dst_channels, int dst_frames);
|
// lifetime. Caller is responsible for the memory.
|
||||||
|
static scoped_ptr<AudioConverter> Create(int src_channels, int src_frames,
|
||||||
|
int dst_channels, int dst_frames);
|
||||||
|
virtual ~AudioConverter() {};
|
||||||
|
|
||||||
void Convert(const float* const* src,
|
// Convert |src|, containing |src_size| samples, to |dst|, having a sample
|
||||||
int src_channels,
|
// capacity of |dst_capacity|. Both point to a series of buffers containing
|
||||||
int src_frames,
|
// the samples for each channel. The sizes must correspond to the format
|
||||||
int dst_channels,
|
// passed to Create().
|
||||||
int dst_frames,
|
virtual void Convert(const float* const* src, size_t src_size,
|
||||||
float* const* dest);
|
float* const* dst, size_t dst_capacity) = 0;
|
||||||
|
|
||||||
|
int src_channels() const { return src_channels_; }
|
||||||
|
int src_frames() const { return src_frames_; }
|
||||||
|
int dst_channels() const { return dst_channels_; }
|
||||||
|
int dst_frames() const { return dst_frames_; }
|
||||||
|
|
||||||
|
protected:
|
||||||
|
AudioConverter();
|
||||||
|
AudioConverter(int src_channels, int src_frames, int dst_channels,
|
||||||
|
int dst_frames);
|
||||||
|
|
||||||
|
// Helper to CHECK that inputs are correctly sized.
|
||||||
|
void CheckSizes(size_t src_size, size_t dst_capacity) const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
const int src_channels_;
|
const int src_channels_;
|
||||||
const int src_frames_;
|
const int src_frames_;
|
||||||
const int dst_channels_;
|
const int dst_channels_;
|
||||||
const int dst_frames_;
|
const int dst_frames_;
|
||||||
scoped_ptr<ChannelBuffer<float>> downmix_buffer_;
|
|
||||||
ScopedVector<PushSincResampler> resamplers_;
|
|
||||||
|
|
||||||
DISALLOW_COPY_AND_ASSIGN(AudioConverter);
|
DISALLOW_COPY_AND_ASSIGN(AudioConverter);
|
||||||
};
|
};
|
||||||
|
@ -8,14 +8,14 @@
|
|||||||
* be found in the AUTHORS file in the root of the source tree.
|
* be found in the AUTHORS file in the root of the source tree.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <math.h>
|
#include <cmath>
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#include "testing/gtest/include/gtest/gtest.h"
|
#include "testing/gtest/include/gtest/gtest.h"
|
||||||
#include "webrtc/common_audio/audio_converter.h"
|
#include "webrtc/common_audio/audio_converter.h"
|
||||||
#include "webrtc/common_audio/resampler/push_sinc_resampler.h"
|
|
||||||
#include "webrtc/common_audio/channel_buffer.h"
|
#include "webrtc/common_audio/channel_buffer.h"
|
||||||
|
#include "webrtc/common_audio/resampler/push_sinc_resampler.h"
|
||||||
#include "webrtc/system_wrappers/interface/scoped_ptr.h"
|
#include "webrtc/system_wrappers/interface/scoped_ptr.h"
|
||||||
|
|
||||||
namespace webrtc {
|
namespace webrtc {
|
||||||
@ -63,6 +63,7 @@ float ComputeSNR(const ChannelBuffer<float>& ref,
|
|||||||
mean += ref.channels()[i][j];
|
mean += ref.channels()[i][j];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const int length = ref.num_channels() * (ref.num_frames() - delay);
|
const int length = ref.num_channels() * (ref.num_frames() - delay);
|
||||||
mse /= length;
|
mse /= length;
|
||||||
variance /= length;
|
variance /= length;
|
||||||
@ -70,7 +71,7 @@ float ComputeSNR(const ChannelBuffer<float>& ref,
|
|||||||
variance -= mean * mean;
|
variance -= mean * mean;
|
||||||
float snr = 100; // We assign 100 dB to the zero-error case.
|
float snr = 100; // We assign 100 dB to the zero-error case.
|
||||||
if (mse > 0)
|
if (mse > 0)
|
||||||
snr = 10 * log10(variance / mse);
|
snr = 10 * std::log10(variance / mse);
|
||||||
if (snr > best_snr) {
|
if (snr > best_snr) {
|
||||||
best_snr = snr;
|
best_snr = snr;
|
||||||
best_delay = delay;
|
best_delay = delay;
|
||||||
@ -127,9 +128,11 @@ void RunAudioConverterTest(int src_channels,
|
|||||||
printf("(%d, %d Hz) -> (%d, %d Hz) ", // SNR reported on the same line later.
|
printf("(%d, %d Hz) -> (%d, %d Hz) ", // SNR reported on the same line later.
|
||||||
src_channels, src_sample_rate_hz, dst_channels, dst_sample_rate_hz);
|
src_channels, src_sample_rate_hz, dst_channels, dst_sample_rate_hz);
|
||||||
|
|
||||||
AudioConverter converter(src_channels, src_frames, dst_channels, dst_frames);
|
scoped_ptr<AudioConverter> converter =
|
||||||
converter.Convert(src_buffer->channels(), src_channels, src_frames,
|
AudioConverter::Create(src_channels, src_frames, dst_channels,
|
||||||
dst_channels, dst_frames, dst_buffer->channels());
|
dst_frames);
|
||||||
|
converter->Convert(src_buffer->channels(), src_buffer->size(),
|
||||||
|
dst_buffer->channels(), dst_buffer->size());
|
||||||
|
|
||||||
EXPECT_LT(43.f,
|
EXPECT_LT(43.f,
|
||||||
ComputeSNR(*ref_buffer.get(), *dst_buffer.get(), delay_frames));
|
ComputeSNR(*ref_buffer.get(), *dst_buffer.get(), delay_frames));
|
||||||
|
@ -8,21 +8,21 @@
|
|||||||
* be found in the AUTHORS file in the root of the source tree.
|
* be found in the AUTHORS file in the root of the source tree.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "webrtc/common_audio/include/audio_util.h"
|
|
||||||
|
|
||||||
#include <assert.h>
|
|
||||||
#include <string.h>
|
|
||||||
|
|
||||||
#include "webrtc/common_audio/resampler/push_sinc_resampler.h"
|
#include "webrtc/common_audio/resampler/push_sinc_resampler.h"
|
||||||
|
|
||||||
|
#include <cstring>
|
||||||
|
|
||||||
|
#include "webrtc/base/checks.h"
|
||||||
|
#include "webrtc/common_audio/include/audio_util.h"
|
||||||
|
|
||||||
namespace webrtc {
|
namespace webrtc {
|
||||||
|
|
||||||
PushSincResampler::PushSincResampler(int source_frames, int destination_frames)
|
PushSincResampler::PushSincResampler(int source_frames, int destination_frames)
|
||||||
: resampler_(new SincResampler(source_frames * 1.0 / destination_frames,
|
: resampler_(new SincResampler(source_frames * 1.0 / destination_frames,
|
||||||
source_frames,
|
source_frames,
|
||||||
this)),
|
this)),
|
||||||
source_ptr_(NULL),
|
source_ptr_(nullptr),
|
||||||
source_ptr_int_(NULL),
|
source_ptr_int_(nullptr),
|
||||||
destination_frames_(destination_frames),
|
destination_frames_(destination_frames),
|
||||||
first_pass_(true),
|
first_pass_(true),
|
||||||
source_available_(0) {}
|
source_available_(0) {}
|
||||||
@ -38,10 +38,10 @@ int PushSincResampler::Resample(const int16_t* source,
|
|||||||
float_buffer_.reset(new float[destination_frames_]);
|
float_buffer_.reset(new float[destination_frames_]);
|
||||||
|
|
||||||
source_ptr_int_ = source;
|
source_ptr_int_ = source;
|
||||||
// Pass NULL as the float source to have Run() read from the int16 source.
|
// Pass nullptr as the float source to have Run() read from the int16 source.
|
||||||
Resample(NULL, source_length, float_buffer_.get(), destination_frames_);
|
Resample(nullptr, source_length, float_buffer_.get(), destination_frames_);
|
||||||
FloatS16ToS16(float_buffer_.get(), destination_frames_, destination);
|
FloatS16ToS16(float_buffer_.get(), destination_frames_, destination);
|
||||||
source_ptr_int_ = NULL;
|
source_ptr_int_ = nullptr;
|
||||||
return destination_frames_;
|
return destination_frames_;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -49,8 +49,8 @@ int PushSincResampler::Resample(const float* source,
|
|||||||
int source_length,
|
int source_length,
|
||||||
float* destination,
|
float* destination,
|
||||||
int destination_capacity) {
|
int destination_capacity) {
|
||||||
assert(source_length == resampler_->request_frames());
|
CHECK_EQ(source_length, resampler_->request_frames());
|
||||||
assert(destination_capacity >= destination_frames_);
|
CHECK_GE(destination_capacity, destination_frames_);
|
||||||
// Cache the source pointer. Calling Resample() will immediately trigger
|
// Cache the source pointer. Calling Resample() will immediately trigger
|
||||||
// the Run() callback whereupon we provide the cached value.
|
// the Run() callback whereupon we provide the cached value.
|
||||||
source_ptr_ = source;
|
source_ptr_ = source;
|
||||||
@ -73,25 +73,25 @@ int PushSincResampler::Resample(const float* source,
|
|||||||
resampler_->Resample(resampler_->ChunkSize(), destination);
|
resampler_->Resample(resampler_->ChunkSize(), destination);
|
||||||
|
|
||||||
resampler_->Resample(destination_frames_, destination);
|
resampler_->Resample(destination_frames_, destination);
|
||||||
source_ptr_ = NULL;
|
source_ptr_ = nullptr;
|
||||||
return destination_frames_;
|
return destination_frames_;
|
||||||
}
|
}
|
||||||
|
|
||||||
void PushSincResampler::Run(int frames, float* destination) {
|
void PushSincResampler::Run(int frames, float* destination) {
|
||||||
// Ensure we are only asked for the available samples. This would fail if
|
// Ensure we are only asked for the available samples. This would fail if
|
||||||
// Run() was triggered more than once per Resample() call.
|
// Run() was triggered more than once per Resample() call.
|
||||||
assert(source_available_ == frames);
|
CHECK_EQ(source_available_, frames);
|
||||||
|
|
||||||
if (first_pass_) {
|
if (first_pass_) {
|
||||||
// Provide dummy input on the first pass, the output of which will be
|
// Provide dummy input on the first pass, the output of which will be
|
||||||
// discarded, as described in Resample().
|
// discarded, as described in Resample().
|
||||||
memset(destination, 0, frames * sizeof(float));
|
std::memset(destination, 0, frames * sizeof(*destination));
|
||||||
first_pass_ = false;
|
first_pass_ = false;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (source_ptr_) {
|
if (source_ptr_) {
|
||||||
memcpy(destination, source_ptr_, frames * sizeof(float));
|
std::memcpy(destination, source_ptr_, frames * sizeof(*destination));
|
||||||
} else {
|
} else {
|
||||||
for (int i = 0; i < frames; ++i)
|
for (int i = 0; i < frames; ++i)
|
||||||
destination[i] = static_cast<float>(source_ptr_int_[i]);
|
destination[i] = static_cast<float>(source_ptr_int_[i]);
|
||||||
|
@ -19,14 +19,16 @@
|
|||||||
namespace webrtc {
|
namespace webrtc {
|
||||||
|
|
||||||
// A thin wrapper over SincResampler to provide a push-based interface as
|
// A thin wrapper over SincResampler to provide a push-based interface as
|
||||||
// required by WebRTC.
|
// 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 {
|
class PushSincResampler : public SincResamplerCallback {
|
||||||
public:
|
public:
|
||||||
// Provide the size of the source and destination blocks in samples. These
|
// 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
|
// must correspond to the same time duration (typically 10 ms) as the sample
|
||||||
// ratio is inferred from them.
|
// ratio is inferred from them.
|
||||||
PushSincResampler(int source_frames, int destination_frames);
|
PushSincResampler(int source_frames, int destination_frames);
|
||||||
virtual ~PushSincResampler();
|
~PushSincResampler() override;
|
||||||
|
|
||||||
// Perform the resampling. |source_frames| must always equal the
|
// Perform the resampling. |source_frames| must always equal the
|
||||||
// |source_frames| provided at construction. |destination_capacity| must be
|
// |source_frames| provided at construction. |destination_capacity| must be
|
||||||
@ -40,15 +42,20 @@ class PushSincResampler : public SincResamplerCallback {
|
|||||||
float* destination,
|
float* destination,
|
||||||
int destination_capacity);
|
int destination_capacity);
|
||||||
|
|
||||||
// Implements SincResamplerCallback.
|
// Delay due to the filter kernel. Essentially, the time after which an input
|
||||||
virtual void Run(int frames, float* destination) OVERRIDE;
|
// sample will appear in the resampled output.
|
||||||
|
|
||||||
SincResampler* get_resampler_for_testing() { return resampler_.get(); }
|
|
||||||
static float AlgorithmicDelaySeconds(int source_rate_hz) {
|
static float AlgorithmicDelaySeconds(int source_rate_hz) {
|
||||||
return 1.f / source_rate_hz * SincResampler::kKernelSize / 2;
|
return 1.f / source_rate_hz * SincResampler::kKernelSize / 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected:
|
||||||
|
// Implements SincResamplerCallback.
|
||||||
|
void Run(int frames, float* destination) override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
friend class PushSincResamplerTest;
|
||||||
|
SincResampler* get_resampler_for_testing() { return resampler_.get(); }
|
||||||
|
|
||||||
scoped_ptr<SincResampler> resampler_;
|
scoped_ptr<SincResampler> resampler_;
|
||||||
scoped_ptr<float[]> float_buffer_;
|
scoped_ptr<float[]> float_buffer_;
|
||||||
const float* source_ptr_;
|
const float* source_ptr_;
|
||||||
|
@ -8,7 +8,8 @@
|
|||||||
* be found in the AUTHORS file in the root of the source tree.
|
* be found in the AUTHORS file in the root of the source tree.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <math.h>
|
#include <cmath>
|
||||||
|
#include <cstring>
|
||||||
|
|
||||||
#include "testing/gmock/include/gmock/gmock.h"
|
#include "testing/gmock/include/gmock/gmock.h"
|
||||||
#include "testing/gtest/include/gtest/gtest.h"
|
#include "testing/gtest/include/gtest/gtest.h"
|
||||||
@ -20,19 +21,30 @@
|
|||||||
#include "webrtc/typedefs.h"
|
#include "webrtc/typedefs.h"
|
||||||
|
|
||||||
namespace webrtc {
|
namespace webrtc {
|
||||||
|
namespace {
|
||||||
|
|
||||||
typedef std::tr1::tuple<int, int, double, double> PushSincResamplerTestData;
|
// Almost all conversions have an RMS error of around -14 dbFS.
|
||||||
class PushSincResamplerTest
|
const double kResamplingRMSError = -14.42;
|
||||||
: public testing::TestWithParam<PushSincResamplerTestData> {
|
|
||||||
|
// Used to convert errors to dbFS.
|
||||||
|
template <typename T>
|
||||||
|
T DBFS(T x) {
|
||||||
|
return 20 * std::log10(x);
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace
|
||||||
|
|
||||||
|
class PushSincResamplerTest : public ::testing::TestWithParam<
|
||||||
|
::testing::tuple<int, int, double, double>> {
|
||||||
public:
|
public:
|
||||||
PushSincResamplerTest()
|
PushSincResamplerTest()
|
||||||
: input_rate_(std::tr1::get<0>(GetParam())),
|
: input_rate_(::testing::get<0>(GetParam())),
|
||||||
output_rate_(std::tr1::get<1>(GetParam())),
|
output_rate_(::testing::get<1>(GetParam())),
|
||||||
rms_error_(std::tr1::get<2>(GetParam())),
|
rms_error_(::testing::get<2>(GetParam())),
|
||||||
low_freq_error_(std::tr1::get<3>(GetParam())) {
|
low_freq_error_(::testing::get<3>(GetParam())) {
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual ~PushSincResamplerTest() {}
|
~PushSincResamplerTest() override {}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
void ResampleBenchmarkTest(bool int_format);
|
void ResampleBenchmarkTest(bool int_format);
|
||||||
@ -47,7 +59,7 @@ class PushSincResamplerTest
|
|||||||
class ZeroSource : public SincResamplerCallback {
|
class ZeroSource : public SincResamplerCallback {
|
||||||
public:
|
public:
|
||||||
void Run(int frames, float* destination) {
|
void Run(int frames, float* destination) {
|
||||||
memset(destination, 0, sizeof(float) * frames);
|
std::memset(destination, 0, sizeof(float) * frames);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -216,8 +228,6 @@ void PushSincResamplerTest::ResampleTest(bool int_format) {
|
|||||||
|
|
||||||
double rms_error = sqrt(sum_of_squares / output_samples);
|
double rms_error = sqrt(sum_of_squares / output_samples);
|
||||||
|
|
||||||
// Convert each error to dbFS.
|
|
||||||
#define DBFS(x) 20 * log10(x)
|
|
||||||
rms_error = DBFS(rms_error);
|
rms_error = DBFS(rms_error);
|
||||||
// In order to keep the thresholds in this test identical to SincResamplerTest
|
// In order to keep the thresholds in this test identical to SincResamplerTest
|
||||||
// we must account for the quantization error introduced by truncating from
|
// we must account for the quantization error introduced by truncating from
|
||||||
@ -241,15 +251,12 @@ TEST_P(PushSincResamplerTest, ResampleInt) { ResampleTest(true); }
|
|||||||
|
|
||||||
TEST_P(PushSincResamplerTest, ResampleFloat) { ResampleTest(false); }
|
TEST_P(PushSincResamplerTest, ResampleFloat) { ResampleTest(false); }
|
||||||
|
|
||||||
// Almost all conversions have an RMS error of around -14 dbFS.
|
|
||||||
static const double kResamplingRMSError = -14.42;
|
|
||||||
|
|
||||||
// Thresholds chosen arbitrarily based on what each resampling reported during
|
// Thresholds chosen arbitrarily based on what each resampling reported during
|
||||||
// testing. All thresholds are in dbFS, http://en.wikipedia.org/wiki/DBFS.
|
// testing. All thresholds are in dbFS, http://en.wikipedia.org/wiki/DBFS.
|
||||||
INSTANTIATE_TEST_CASE_P(
|
INSTANTIATE_TEST_CASE_P(
|
||||||
PushSincResamplerTest,
|
PushSincResamplerTest,
|
||||||
PushSincResamplerTest,
|
PushSincResamplerTest,
|
||||||
testing::Values(
|
::testing::Values(
|
||||||
// First run through the rates tested in SincResamplerTest. The
|
// First run through the rates tested in SincResamplerTest. The
|
||||||
// thresholds are identical.
|
// thresholds are identical.
|
||||||
//
|
//
|
||||||
@ -258,40 +265,40 @@ INSTANTIATE_TEST_CASE_P(
|
|||||||
// these rates in any case (for the same reason).
|
// these rates in any case (for the same reason).
|
||||||
|
|
||||||
// To 44.1kHz
|
// To 44.1kHz
|
||||||
std::tr1::make_tuple(8000, 44100, kResamplingRMSError, -62.73),
|
::testing::make_tuple(8000, 44100, kResamplingRMSError, -62.73),
|
||||||
std::tr1::make_tuple(16000, 44100, kResamplingRMSError, -62.54),
|
::testing::make_tuple(16000, 44100, kResamplingRMSError, -62.54),
|
||||||
std::tr1::make_tuple(32000, 44100, kResamplingRMSError, -63.32),
|
::testing::make_tuple(32000, 44100, kResamplingRMSError, -63.32),
|
||||||
std::tr1::make_tuple(44100, 44100, kResamplingRMSError, -73.53),
|
::testing::make_tuple(44100, 44100, kResamplingRMSError, -73.53),
|
||||||
std::tr1::make_tuple(48000, 44100, -15.01, -64.04),
|
::testing::make_tuple(48000, 44100, -15.01, -64.04),
|
||||||
std::tr1::make_tuple(96000, 44100, -18.49, -25.51),
|
::testing::make_tuple(96000, 44100, -18.49, -25.51),
|
||||||
std::tr1::make_tuple(192000, 44100, -20.50, -13.31),
|
::testing::make_tuple(192000, 44100, -20.50, -13.31),
|
||||||
|
|
||||||
// To 48kHz
|
// To 48kHz
|
||||||
std::tr1::make_tuple(8000, 48000, kResamplingRMSError, -63.43),
|
::testing::make_tuple(8000, 48000, kResamplingRMSError, -63.43),
|
||||||
std::tr1::make_tuple(16000, 48000, kResamplingRMSError, -63.96),
|
::testing::make_tuple(16000, 48000, kResamplingRMSError, -63.96),
|
||||||
std::tr1::make_tuple(32000, 48000, kResamplingRMSError, -64.04),
|
::testing::make_tuple(32000, 48000, kResamplingRMSError, -64.04),
|
||||||
std::tr1::make_tuple(44100, 48000, kResamplingRMSError, -62.63),
|
::testing::make_tuple(44100, 48000, kResamplingRMSError, -62.63),
|
||||||
std::tr1::make_tuple(48000, 48000, kResamplingRMSError, -73.52),
|
::testing::make_tuple(48000, 48000, kResamplingRMSError, -73.52),
|
||||||
std::tr1::make_tuple(96000, 48000, -18.40, -28.44),
|
::testing::make_tuple(96000, 48000, -18.40, -28.44),
|
||||||
std::tr1::make_tuple(192000, 48000, -20.43, -14.11),
|
::testing::make_tuple(192000, 48000, -20.43, -14.11),
|
||||||
|
|
||||||
// To 96kHz
|
// To 96kHz
|
||||||
std::tr1::make_tuple(8000, 96000, kResamplingRMSError, -63.19),
|
::testing::make_tuple(8000, 96000, kResamplingRMSError, -63.19),
|
||||||
std::tr1::make_tuple(16000, 96000, kResamplingRMSError, -63.39),
|
::testing::make_tuple(16000, 96000, kResamplingRMSError, -63.39),
|
||||||
std::tr1::make_tuple(32000, 96000, kResamplingRMSError, -63.95),
|
::testing::make_tuple(32000, 96000, kResamplingRMSError, -63.95),
|
||||||
std::tr1::make_tuple(44100, 96000, kResamplingRMSError, -62.63),
|
::testing::make_tuple(44100, 96000, kResamplingRMSError, -62.63),
|
||||||
std::tr1::make_tuple(48000, 96000, kResamplingRMSError, -73.52),
|
::testing::make_tuple(48000, 96000, kResamplingRMSError, -73.52),
|
||||||
std::tr1::make_tuple(96000, 96000, kResamplingRMSError, -73.52),
|
::testing::make_tuple(96000, 96000, kResamplingRMSError, -73.52),
|
||||||
std::tr1::make_tuple(192000, 96000, kResamplingRMSError, -28.41),
|
::testing::make_tuple(192000, 96000, kResamplingRMSError, -28.41),
|
||||||
|
|
||||||
// To 192kHz
|
// To 192kHz
|
||||||
std::tr1::make_tuple(8000, 192000, kResamplingRMSError, -63.10),
|
::testing::make_tuple(8000, 192000, kResamplingRMSError, -63.10),
|
||||||
std::tr1::make_tuple(16000, 192000, kResamplingRMSError, -63.14),
|
::testing::make_tuple(16000, 192000, kResamplingRMSError, -63.14),
|
||||||
std::tr1::make_tuple(32000, 192000, kResamplingRMSError, -63.38),
|
::testing::make_tuple(32000, 192000, kResamplingRMSError, -63.38),
|
||||||
std::tr1::make_tuple(44100, 192000, kResamplingRMSError, -62.63),
|
::testing::make_tuple(44100, 192000, kResamplingRMSError, -62.63),
|
||||||
std::tr1::make_tuple(48000, 192000, kResamplingRMSError, -73.44),
|
::testing::make_tuple(48000, 192000, kResamplingRMSError, -73.44),
|
||||||
std::tr1::make_tuple(96000, 192000, kResamplingRMSError, -73.52),
|
::testing::make_tuple(96000, 192000, kResamplingRMSError, -73.52),
|
||||||
std::tr1::make_tuple(192000, 192000, kResamplingRMSError, -73.52),
|
::testing::make_tuple(192000, 192000, kResamplingRMSError, -73.52),
|
||||||
|
|
||||||
// Next run through some additional cases interesting for WebRTC.
|
// Next run through some additional cases interesting for WebRTC.
|
||||||
// We skip some extreme downsampled cases (192 -> {8, 16}, 96 -> 8)
|
// We skip some extreme downsampled cases (192 -> {8, 16}, 96 -> 8)
|
||||||
@ -300,27 +307,27 @@ INSTANTIATE_TEST_CASE_P(
|
|||||||
// practice anyway.
|
// practice anyway.
|
||||||
|
|
||||||
// To 8 kHz
|
// To 8 kHz
|
||||||
std::tr1::make_tuple(8000, 8000, kResamplingRMSError, -75.50),
|
::testing::make_tuple(8000, 8000, kResamplingRMSError, -75.50),
|
||||||
std::tr1::make_tuple(16000, 8000, -18.56, -28.79),
|
::testing::make_tuple(16000, 8000, -18.56, -28.79),
|
||||||
std::tr1::make_tuple(32000, 8000, -20.36, -14.13),
|
::testing::make_tuple(32000, 8000, -20.36, -14.13),
|
||||||
std::tr1::make_tuple(44100, 8000, -21.00, -11.39),
|
::testing::make_tuple(44100, 8000, -21.00, -11.39),
|
||||||
std::tr1::make_tuple(48000, 8000, -20.96, -11.04),
|
::testing::make_tuple(48000, 8000, -20.96, -11.04),
|
||||||
|
|
||||||
// To 16 kHz
|
// To 16 kHz
|
||||||
std::tr1::make_tuple(8000, 16000, kResamplingRMSError, -70.30),
|
::testing::make_tuple(8000, 16000, kResamplingRMSError, -70.30),
|
||||||
std::tr1::make_tuple(16000, 16000, kResamplingRMSError, -75.51),
|
::testing::make_tuple(16000, 16000, kResamplingRMSError, -75.51),
|
||||||
std::tr1::make_tuple(32000, 16000, -18.48, -28.59),
|
::testing::make_tuple(32000, 16000, -18.48, -28.59),
|
||||||
std::tr1::make_tuple(44100, 16000, -19.30, -19.67),
|
::testing::make_tuple(44100, 16000, -19.30, -19.67),
|
||||||
std::tr1::make_tuple(48000, 16000, -19.81, -18.11),
|
::testing::make_tuple(48000, 16000, -19.81, -18.11),
|
||||||
std::tr1::make_tuple(96000, 16000, -20.95, -10.96),
|
::testing::make_tuple(96000, 16000, -20.95, -10.96),
|
||||||
|
|
||||||
// To 32 kHz
|
// To 32 kHz
|
||||||
std::tr1::make_tuple(8000, 32000, kResamplingRMSError, -70.30),
|
::testing::make_tuple(8000, 32000, kResamplingRMSError, -70.30),
|
||||||
std::tr1::make_tuple(16000, 32000, kResamplingRMSError, -75.51),
|
::testing::make_tuple(16000, 32000, kResamplingRMSError, -75.51),
|
||||||
std::tr1::make_tuple(32000, 32000, kResamplingRMSError, -75.51),
|
::testing::make_tuple(32000, 32000, kResamplingRMSError, -75.51),
|
||||||
std::tr1::make_tuple(44100, 32000, -16.44, -51.10),
|
::testing::make_tuple(44100, 32000, -16.44, -51.10),
|
||||||
std::tr1::make_tuple(48000, 32000, -16.90, -44.03),
|
::testing::make_tuple(48000, 32000, -16.90, -44.03),
|
||||||
std::tr1::make_tuple(96000, 32000, -19.61, -18.04),
|
::testing::make_tuple(96000, 32000, -19.61, -18.04),
|
||||||
std::tr1::make_tuple(192000, 32000, -21.02, -10.94)));
|
::testing::make_tuple(192000, 32000, -21.02, -10.94)));
|
||||||
|
|
||||||
} // namespace webrtc
|
} // namespace webrtc
|
||||||
|
Loading…
x
Reference in New Issue
Block a user