Split MoveReadPosition into Forward and Backward versions.

This makes the interface consistent in its use of size_t, and will
reduce casts in callers as they shift to size_t, e.g. this CL:
https://codereview.webrtc.org/1227203003/

Review URL: https://codereview.webrtc.org/1252943007

Cr-Commit-Position: refs/heads/master@{#9646}
This commit is contained in:
andrew
2015-07-28 00:52:59 -07:00
committed by Commit bot
parent b3cc77f4ba
commit d40af69278
4 changed files with 28 additions and 14 deletions

View File

@ -18,6 +18,7 @@
namespace webrtc {
AudioRingBuffer::AudioRingBuffer(size_t channels, size_t max_frames) {
buffers_.reserve(channels);
for (size_t i = 0; i < channels; ++i)
buffers_.push_back(WebRtc_CreateBuffer(max_frames, sizeof(float)));
}
@ -31,7 +32,7 @@ void AudioRingBuffer::Write(const float* const* data, size_t channels,
size_t frames) {
DCHECK_EQ(buffers_.size(), channels);
for (size_t i = 0; i < channels; ++i) {
size_t written = WebRtc_WriteBuffer(buffers_[i], data[i], frames);
const size_t written = WebRtc_WriteBuffer(buffers_[i], data[i], frames);
CHECK_EQ(written, frames);
}
}
@ -39,7 +40,8 @@ void AudioRingBuffer::Write(const float* const* data, size_t channels,
void AudioRingBuffer::Read(float* const* data, size_t channels, size_t frames) {
DCHECK_EQ(buffers_.size(), channels);
for (size_t i = 0; i < channels; ++i) {
size_t read = WebRtc_ReadBuffer(buffers_[i], nullptr, data[i], frames);
const size_t read =
WebRtc_ReadBuffer(buffers_[i], nullptr, data[i], frames);
CHECK_EQ(read, frames);
}
}
@ -54,9 +56,18 @@ size_t AudioRingBuffer::WriteFramesAvailable() const {
return WebRtc_available_write(buffers_[0]);
}
void AudioRingBuffer::MoveReadPosition(int frames) {
void AudioRingBuffer::MoveReadPositionForward(size_t frames) {
for (auto buf : buffers_) {
int moved = WebRtc_MoveReadPtr(buf, frames);
const size_t moved =
static_cast<size_t>(WebRtc_MoveReadPtr(buf, static_cast<int>(frames)));
CHECK_EQ(moved, frames);
}
}
void AudioRingBuffer::MoveReadPositionBackward(size_t frames) {
for (auto buf : buffers_) {
const size_t moved = static_cast<size_t>(
-WebRtc_MoveReadPtr(buf, -static_cast<int>(frames)));
CHECK_EQ(moved, frames);
}
}

View File

@ -27,20 +27,23 @@ class AudioRingBuffer final {
AudioRingBuffer(size_t channels, size_t max_frames);
~AudioRingBuffer();
// Copy |data| to the buffer and advance the write pointer. |channels| must
// Copies |data| to the buffer and advances the write pointer. |channels| must
// be the same as at creation time.
void Write(const float* const* data, size_t channels, size_t frames);
// Copy from the buffer to |data| and advance the read pointer. |channels|
// Copies from the buffer to |data| and advances the read pointer. |channels|
// must be the same as at creation time.
void Read(float* const* data, size_t channels, size_t frames);
size_t ReadFramesAvailable() const;
size_t WriteFramesAvailable() const;
// Positive values advance the read pointer and negative values withdraw
// the read pointer (i.e. flush and stuff the buffer respectively.)
void MoveReadPosition(int frames);
// Moves the read position. The forward version advances the read pointer
// towards the write pointer and the backward verison withdraws the read
// pointer away from the write pointer (i.e. flushing and stuffing the buffer
// respectively.)
void MoveReadPositionForward(size_t frames);
void MoveReadPositionBackward(size_t frames);
private:
// We don't use a ScopedVector because it doesn't support a specialized

View File

@ -98,11 +98,11 @@ TEST_F(AudioRingBufferTest, MoveReadPosition) {
AudioRingBuffer buf(kNumChannels, kNumFrames);
buf.Write(input.channels(), kNumChannels, kNumFrames);
buf.MoveReadPosition(3);
buf.MoveReadPositionForward(3);
ChannelBuffer<float> output(1, kNumChannels);
buf.Read(output.channels(), kNumChannels, 1);
EXPECT_EQ(4, output.channels()[0][0]);
buf.MoveReadPosition(-3);
buf.MoveReadPositionBackward(3);
buf.Read(output.channels(), kNumChannels, 1);
EXPECT_EQ(2, output.channels()[0][0]);
}

View File

@ -119,10 +119,10 @@ Blocker::Blocker(int chunk_size,
shift_amount_(shift_amount),
callback_(callback) {
CHECK_LE(num_output_channels_, num_input_channels_);
CHECK(window);
CHECK_LE(shift_amount_, block_size_);
memcpy(window_.get(), window, block_size_ * sizeof(*window_.get()));
input_buffer_.MoveReadPosition(-initial_delay_);
input_buffer_.MoveReadPositionBackward(initial_delay_);
}
// When block_size < chunk_size the input and output buffers look like this:
@ -180,7 +180,7 @@ void Blocker::ProcessChunk(const float* const* input,
while (first_frame_in_block < chunk_size_) {
input_buffer_.Read(input_block_.channels(), num_input_channels,
block_size_);
input_buffer_.MoveReadPosition(-block_size_ + shift_amount_);
input_buffer_.MoveReadPositionBackward(block_size_ - shift_amount_);
ApplyWindow(window_.get(),
block_size_,