Commit 25f5454a authored by chcunningham's avatar chcunningham Committed by Commit bot

AudioBuffer: remove PadSilence()

Turned out not to be needed. See
https://codereview.chromium.org/2343543002

Mostly a revert of the original CL
1f7c987f

... but I've maintained the removal of the unused Read*() methods/tests.

Review-Url: https://codereview.chromium.org/2349873002
Cr-Commit-Position: refs/heads/master@{#419644}
parent 8889114b
......@@ -44,29 +44,15 @@ AudioBuffer::AudioBuffer(SampleFormat sample_format,
DCHECK(channel_layout == CHANNEL_LAYOUT_DISCRETE ||
ChannelLayoutToChannelCount(channel_layout) == channel_count);
int bytes_per_channel = SampleFormatToBytesPerChannel(sample_format);
DCHECK_LE(bytes_per_channel, kChannelAlignment);
// Empty buffer?
if (!create_buffer)
return;
AllocateAndCopy(data, frame_count, 0);
}
AudioBuffer::~AudioBuffer() {}
void AudioBuffer::AllocateAndCopy(const uint8_t* const* data,
int frame_count,
int silence_frames) {
if (!channel_data_.empty())
channel_data_.clear();
int bytes_per_channel = SampleFormatToBytesPerChannel(sample_format_);
DCHECK_LE(bytes_per_channel, kChannelAlignment);
DCHECK_GT(bytes_per_channel, 0);
int data_size_per_channel =
(frame_count + silence_frames) * bytes_per_channel;
if (IsPlanar(sample_format_)) {
int data_size_per_channel = frame_count * bytes_per_channel;
if (IsPlanar(sample_format)) {
// Planar data, so need to allocate buffer for each channel.
// Determine per channel data size, taking into account alignment.
int block_size_per_channel =
......@@ -80,23 +66,17 @@ void AudioBuffer::AllocateAndCopy(const uint8_t* const* data,
base::AlignedAlloc(data_size_, kChannelAlignment)));
channel_data_.reserve(channel_count_);
// Size of silence per channel.
int silence_bytes = silence_frames * bytes_per_channel;
// Copy each channel's data into the appropriate spot.
for (int i = 0; i < channel_count_; ++i) {
channel_data_.push_back(data_.get() + i * block_size_per_channel);
memset(channel_data_[i], 0, silence_bytes);
if (data) {
memcpy(channel_data_[i] + silence_bytes, data[i],
data_size_per_channel - silence_bytes);
}
if (data)
memcpy(channel_data_[i], data[i], data_size_per_channel);
}
return;
}
// Remaining formats are interleaved data.
DCHECK(IsInterleaved(sample_format_)) << sample_format_;
DCHECK(IsInterleaved(sample_format)) << sample_format_;
// Allocate our own buffer and copy the supplied data into it. Buffer must
// contain the data for all channels.
data_size_ = data_size_per_channel * channel_count_;
......@@ -104,31 +84,11 @@ void AudioBuffer::AllocateAndCopy(const uint8_t* const* data,
static_cast<uint8_t*>(base::AlignedAlloc(data_size_, kChannelAlignment)));
channel_data_.reserve(1);
channel_data_.push_back(data_.get());
int silence_bytes = silence_frames * channel_count_ * bytes_per_channel;
memset(data_.get(), 0, silence_bytes);
if (data)
memcpy(data_.get() + silence_bytes, data[0], data_size_ - silence_bytes);
memcpy(data_.get(), data[0], data_size_);
}
void AudioBuffer::PadStart(int silence_frames) {
DCHECK_GE(silence_frames, 0);
if (silence_frames > 0) {
// Only adjust allocation if not currently an empty buffer. Empty buffer's
// are implicitly silent, so just increase the frame count for that case.
if (data_) {
std::unique_ptr<uint8_t, base::AlignedFreeDeleter> orig_data =
std::move(data_);
std::vector<uint8_t*> orig_channel_data(channel_data_);
AllocateAndCopy(&orig_channel_data[0], adjusted_frame_count_,
silence_frames);
}
adjusted_frame_count_ += silence_frames;
duration_ = CalculateDuration(adjusted_frame_count_, sample_rate_);
}
}
AudioBuffer::~AudioBuffer() {}
// static
scoped_refptr<AudioBuffer> AudioBuffer::CopyFrom(
......@@ -197,7 +157,6 @@ inline float ConvertSample(int16_t value) {
: 1.0f / std::numeric_limits<int16_t>::max());
}
void AudioBuffer::AdjustSampleRate(int sample_rate) {
DCHECK(!end_of_stream_);
sample_rate_ = sample_rate;
......
......@@ -94,10 +94,6 @@ class MEDIA_EXPORT AudioBuffer
int dest_frame_offset,
AudioBus* dest);
// Add |silence_frames| frames of silence to the start of the buffer. Silence
// padding can be removed using any of the Trim*() methods.
void PadStart(int silence_frames);
// Trim an AudioBuffer by removing |frames_to_trim| frames from the start.
// Timestamp and duration are adjusted to reflect the fewer frames.
// Note that repeated calls to TrimStart() may result in timestamp() and
......@@ -165,10 +161,6 @@ class MEDIA_EXPORT AudioBuffer
virtual ~AudioBuffer();
void AllocateAndCopy(const uint8_t* const* data,
int frame_count,
int silence_frames);
const SampleFormat sample_format_;
const ChannelLayout channel_layout_;
const int channel_count_;
......
......@@ -24,7 +24,6 @@ static void VerifyBusWithOffset(AudioBus* bus,
float increment) {
for (int ch = 0; ch < bus->channels(); ++ch) {
const float v = start_offset + start + ch * bus->frames() * increment;
for (int i = offset; i < offset + frames; ++i) {
ASSERT_FLOAT_EQ(v + i * increment, bus->channel(ch)[i]) << "i=" << i
<< ", ch=" << ch;
......@@ -158,40 +157,6 @@ static void TrimRangeTest(SampleFormat sample_format) {
1);
}
void PadStartTest(SampleFormat sample_format) {
const ChannelLayout channel_layout = CHANNEL_LAYOUT_4_0;
const int channels = ChannelLayoutToChannelCount(channel_layout);
const int frames = 100;
const int silence_frames = frames / 5;
const base::TimeDelta start_time;
scoped_refptr<AudioBuffer> buffer =
MakeAudioBuffer<float>(sample_format, channel_layout, channels,
kSampleRate, 0.0f, 1.0f, frames, start_time);
// Read all 100 frames from the buffer.
std::unique_ptr<AudioBus> bus = AudioBus::Create(channels, frames);
buffer->ReadFrames(frames, 0, 0, bus.get());
VerifyBus(bus.get(), frames, 0, 1);
// Now trim off some frames and add in leading silence.
buffer->TrimStart(silence_frames);
buffer->PadStart(silence_frames);
// Re-read. Verify first 20 frames of silence, next 80 frames same as before.
bus->Zero();
buffer->ReadFrames(frames, 0, 0, bus.get());
VerifyBus(bus.get(), silence_frames, 0, 0);
VerifyBusWithOffset(bus.get(), silence_frames, frames - silence_frames, 0, 0,
1);
// Now trim off the silence. Verify silence is gone.
buffer->TrimStart(silence_frames);
bus->Zero();
buffer->ReadFrames(frames - silence_frames, 0, 0, bus.get());
VerifyBusWithOffset(bus.get(), 0, frames - silence_frames, silence_frames, 0,
1);
}
TEST(AudioBufferTest, CopyFrom) {
const ChannelLayout kChannelLayout = CHANNEL_LAYOUT_MONO;
scoped_refptr<AudioBuffer> original_buffer = MakeAudioBuffer<uint8_t>(
......@@ -469,49 +434,4 @@ TEST(AudioBufferTest, TrimRangeInterleaved) {
TrimRangeTest(kSampleFormatF32);
}
TEST(AudioBufferTest, PadStartPlanar) {
PadStartTest(kSampleFormatPlanarF32);
}
TEST(AudioBufferTest, PadStartInterleaved) {
PadStartTest(kSampleFormatF32);
}
TEST(AudioBufferTest, PadStartEmptyBuffer) {
const ChannelLayout channel_layout = CHANNEL_LAYOUT_4_0;
const int channels = ChannelLayoutToChannelCount(channel_layout);
const int frames = kSampleRate / 10;
const base::TimeDelta duration = base::TimeDelta::FromMilliseconds(100);
const base::TimeDelta start_time;
scoped_refptr<AudioBuffer> buffer = AudioBuffer::CreateEmptyBuffer(
channel_layout, channels, kSampleRate, frames, start_time);
// Empty buffer should zero data size with non-zero frame count and duration.
EXPECT_EQ(std::size_t{0}, buffer->data_size());
EXPECT_EQ(frames, buffer->frame_count());
EXPECT_EQ(duration, buffer->duration());
// Read all frames from the buffer. All data should be 0.
std::unique_ptr<AudioBus> bus = AudioBus::Create(channels, frames);
buffer->ReadFrames(frames, 0, 0, bus.get());
VerifyBus(bus.get(), frames, 0, 0);
// Double the number of frames by padding the start of the buffer with
// |frames| of silence.
buffer->PadStart(frames);
const int new_frame_count = frames * 2;
const base::TimeDelta new_duration = base::TimeDelta::FromMilliseconds(200);
// Adding silence should not trigger an allocation, but the frame count and
// duration should be increased.
EXPECT_EQ(std::size_t{0}, buffer->data_size());
EXPECT_EQ(new_frame_count, buffer->frame_count());
EXPECT_EQ(new_duration, buffer->duration());
// Read all frames from the buffer. All data should be 0.
bus = AudioBus::Create(channels, new_frame_count);
buffer->ReadFrames(new_frame_count, 0, 0, bus.get());
VerifyBus(bus.get(), new_frame_count, 0, 0);
}
} // namespace media
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment