Align Int16FrameData test class with AudioFrame
This updates test code that tests interleaved audio frames to use some of the same properties and types as AudioFrame (rather than copy). The CL also moves code from audio_processing_unittest.cc that modifies the buffer owned by Int16FrameData, into Int16FrameData. Bug: none Change-Id: Iab37227deb302bf4fc832633d312262e5249caad Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/355960 Reviewed-by: Per Åhgren <peah@webrtc.org> Commit-Queue: Tomas Gunnarsson <tommi@webrtc.org> Cr-Commit-Position: refs/heads/main@{#43424}
This commit is contained in:
parent
8337c966d4
commit
5f163fcaa0
File diff suppressed because it is too large
Load Diff
@ -10,6 +10,7 @@
|
||||
|
||||
#include "modules/audio_processing/test/test_utils.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
|
||||
@ -19,6 +20,52 @@
|
||||
|
||||
namespace webrtc {
|
||||
|
||||
void Int16FrameData::CopyFrom(const Int16FrameData& src) {
|
||||
sample_rate_hz = src.sample_rate_hz;
|
||||
view_ = InterleavedView<int16_t>(data.data(), src.samples_per_channel(),
|
||||
src.num_channels());
|
||||
RTC_CHECK_LE(view_.size(), kMaxDataSizeSamples);
|
||||
CopySamples(view_, src.view());
|
||||
}
|
||||
|
||||
bool Int16FrameData::IsEqual(const Int16FrameData& frame) const {
|
||||
return samples_per_channel() == frame.samples_per_channel() &&
|
||||
num_channels() == num_channels() &&
|
||||
memcmp(data.data(), frame.data.data(),
|
||||
samples_per_channel() * num_channels() * sizeof(int16_t)) == 0;
|
||||
}
|
||||
|
||||
void Int16FrameData::Scale(float f) {
|
||||
std::for_each(data.begin(), data.end(),
|
||||
[f](int16_t& sample) { sample = FloatS16ToS16(sample * f); });
|
||||
}
|
||||
|
||||
void Int16FrameData::SetProperties(size_t samples_per_channel,
|
||||
size_t num_channels) {
|
||||
sample_rate_hz = samples_per_channel * 100;
|
||||
view_ =
|
||||
InterleavedView<int16_t>(data.data(), samples_per_channel, num_channels);
|
||||
RTC_CHECK_LE(view_.size(), kMaxDataSizeSamples);
|
||||
}
|
||||
|
||||
void Int16FrameData::set_num_channels(size_t num_channels) {
|
||||
view_ = InterleavedView<int16_t>(data.data(), samples_per_channel(),
|
||||
num_channels);
|
||||
RTC_CHECK_LE(view_.size(), kMaxDataSizeSamples);
|
||||
}
|
||||
|
||||
void Int16FrameData::FillData(int16_t value) {
|
||||
std::fill(&data[0], &data[size()], value);
|
||||
}
|
||||
|
||||
void Int16FrameData::FillStereoData(int16_t left, int16_t right) {
|
||||
RTC_DCHECK_EQ(num_channels(), 2u);
|
||||
for (size_t i = 0; i < samples_per_channel() * 2u; i += 2u) {
|
||||
data[i] = left;
|
||||
data[i + 1] = right;
|
||||
}
|
||||
}
|
||||
|
||||
ChannelBufferWavReader::ChannelBufferWavReader(std::unique_ptr<WavReader> file)
|
||||
: file_(std::move(file)) {}
|
||||
|
||||
@ -86,10 +133,4 @@ FILE* OpenFile(absl::string_view filename, absl::string_view mode) {
|
||||
return file;
|
||||
}
|
||||
|
||||
void SetFrameSampleRate(Int16FrameData* frame, int sample_rate_hz) {
|
||||
frame->sample_rate_hz = sample_rate_hz;
|
||||
frame->samples_per_channel =
|
||||
AudioProcessing::kChunkSizeMs * sample_rate_hz / 1000;
|
||||
}
|
||||
|
||||
} // namespace webrtc
|
||||
|
||||
@ -20,41 +20,51 @@
|
||||
#include <vector>
|
||||
|
||||
#include "absl/strings/string_view.h"
|
||||
#include "api/audio/audio_frame.h"
|
||||
#include "api/audio/audio_processing.h"
|
||||
#include "api/audio/audio_view.h"
|
||||
#include "common_audio/channel_buffer.h"
|
||||
#include "common_audio/wav_file.h"
|
||||
|
||||
namespace webrtc {
|
||||
|
||||
static const AudioProcessing::Error kNoErr = AudioProcessing::kNoError;
|
||||
#define EXPECT_NOERR(expr) EXPECT_EQ(kNoErr, (expr))
|
||||
#define EXPECT_NOERR(expr) EXPECT_EQ(AudioProcessing::kNoError, (expr))
|
||||
|
||||
// Encapsulates samples and metadata for an integer frame.
|
||||
struct Int16FrameData {
|
||||
// Max data size that matches the data size of the AudioFrame class, providing
|
||||
// storage for 8 channels of 96 kHz data.
|
||||
static const int kMaxDataSizeSamples = 7680;
|
||||
static const int kMaxDataSizeSamples = AudioFrame::kMaxDataSizeSamples;
|
||||
|
||||
Int16FrameData() {
|
||||
sample_rate_hz = 0;
|
||||
num_channels = 0;
|
||||
samples_per_channel = 0;
|
||||
data.fill(0);
|
||||
}
|
||||
Int16FrameData() = default;
|
||||
|
||||
void CopyFrom(const Int16FrameData& src) {
|
||||
samples_per_channel = src.samples_per_channel;
|
||||
sample_rate_hz = src.sample_rate_hz;
|
||||
num_channels = src.num_channels;
|
||||
void CopyFrom(const Int16FrameData& src);
|
||||
bool IsEqual(const Int16FrameData& frame) const;
|
||||
void Scale(float f);
|
||||
|
||||
const size_t length = samples_per_channel * num_channels;
|
||||
RTC_CHECK_LE(length, kMaxDataSizeSamples);
|
||||
memcpy(data.data(), src.data.data(), sizeof(int16_t) * length);
|
||||
}
|
||||
std::array<int16_t, kMaxDataSizeSamples> data;
|
||||
int32_t sample_rate_hz;
|
||||
size_t num_channels;
|
||||
size_t samples_per_channel;
|
||||
// Sets `samples_per_channel`, `num_channels` and, implicitly, the sample
|
||||
// rate. The sample rate is set to 100x that of samples per channel. I.e. if
|
||||
// samples_per_channel is 320, the sample rate will be set to 32000.
|
||||
void SetProperties(size_t samples_per_channel, size_t num_channels);
|
||||
|
||||
size_t size() const { return view_.size(); }
|
||||
size_t samples_per_channel() const { return view_.samples_per_channel(); }
|
||||
size_t num_channels() const { return view_.num_channels(); }
|
||||
void set_num_channels(size_t num_channels);
|
||||
|
||||
InterleavedView<int16_t> view() { return view_; }
|
||||
InterleavedView<const int16_t> view() const { return view_; }
|
||||
|
||||
void FillData(int16_t value);
|
||||
void FillStereoData(int16_t left, int16_t right);
|
||||
|
||||
// public struct members.
|
||||
std::array<int16_t, kMaxDataSizeSamples> data = {};
|
||||
int32_t sample_rate_hz = 0;
|
||||
|
||||
private:
|
||||
InterleavedView<int16_t> view_;
|
||||
};
|
||||
|
||||
// Reads ChannelBuffers from a provided WavReader.
|
||||
@ -114,16 +124,13 @@ class ChannelBufferVectorWriter final {
|
||||
// Exits on failure; do not use in unit tests.
|
||||
FILE* OpenFile(absl::string_view filename, absl::string_view mode);
|
||||
|
||||
void SetFrameSampleRate(Int16FrameData* frame, int sample_rate_hz);
|
||||
|
||||
template <typename T>
|
||||
void SetContainerFormat(int sample_rate_hz,
|
||||
size_t num_channels,
|
||||
Int16FrameData* frame,
|
||||
std::unique_ptr<ChannelBuffer<T> >* cb) {
|
||||
SetFrameSampleRate(frame, sample_rate_hz);
|
||||
frame->num_channels = num_channels;
|
||||
cb->reset(new ChannelBuffer<T>(frame->samples_per_channel, num_channels));
|
||||
frame->SetProperties(sample_rate_hz / 100, num_channels);
|
||||
cb->reset(new ChannelBuffer<T>(frame->samples_per_channel(), num_channels));
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user