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:
@ -18,6 +18,7 @@
|
|||||||
namespace webrtc {
|
namespace webrtc {
|
||||||
|
|
||||||
AudioRingBuffer::AudioRingBuffer(size_t channels, size_t max_frames) {
|
AudioRingBuffer::AudioRingBuffer(size_t channels, size_t max_frames) {
|
||||||
|
buffers_.reserve(channels);
|
||||||
for (size_t i = 0; i < channels; ++i)
|
for (size_t i = 0; i < channels; ++i)
|
||||||
buffers_.push_back(WebRtc_CreateBuffer(max_frames, sizeof(float)));
|
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) {
|
size_t frames) {
|
||||||
DCHECK_EQ(buffers_.size(), channels);
|
DCHECK_EQ(buffers_.size(), channels);
|
||||||
for (size_t i = 0; i < channels; ++i) {
|
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);
|
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) {
|
void AudioRingBuffer::Read(float* const* data, size_t channels, size_t frames) {
|
||||||
DCHECK_EQ(buffers_.size(), channels);
|
DCHECK_EQ(buffers_.size(), channels);
|
||||||
for (size_t i = 0; i < channels; ++i) {
|
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);
|
CHECK_EQ(read, frames);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -54,9 +56,18 @@ size_t AudioRingBuffer::WriteFramesAvailable() const {
|
|||||||
return WebRtc_available_write(buffers_[0]);
|
return WebRtc_available_write(buffers_[0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
void AudioRingBuffer::MoveReadPosition(int frames) {
|
void AudioRingBuffer::MoveReadPositionForward(size_t frames) {
|
||||||
for (auto buf : buffers_) {
|
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);
|
CHECK_EQ(moved, frames);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -27,20 +27,23 @@ class AudioRingBuffer final {
|
|||||||
AudioRingBuffer(size_t channels, size_t max_frames);
|
AudioRingBuffer(size_t channels, size_t max_frames);
|
||||||
~AudioRingBuffer();
|
~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.
|
// be the same as at creation time.
|
||||||
void Write(const float* const* data, size_t channels, size_t frames);
|
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.
|
// must be the same as at creation time.
|
||||||
void Read(float* const* data, size_t channels, size_t frames);
|
void Read(float* const* data, size_t channels, size_t frames);
|
||||||
|
|
||||||
size_t ReadFramesAvailable() const;
|
size_t ReadFramesAvailable() const;
|
||||||
size_t WriteFramesAvailable() const;
|
size_t WriteFramesAvailable() const;
|
||||||
|
|
||||||
// Positive values advance the read pointer and negative values withdraw
|
// Moves the read position. The forward version advances the read pointer
|
||||||
// the read pointer (i.e. flush and stuff the buffer respectively.)
|
// towards the write pointer and the backward verison withdraws the read
|
||||||
void MoveReadPosition(int frames);
|
// 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:
|
private:
|
||||||
// We don't use a ScopedVector because it doesn't support a specialized
|
// We don't use a ScopedVector because it doesn't support a specialized
|
||||||
|
@ -98,11 +98,11 @@ TEST_F(AudioRingBufferTest, MoveReadPosition) {
|
|||||||
AudioRingBuffer buf(kNumChannels, kNumFrames);
|
AudioRingBuffer buf(kNumChannels, kNumFrames);
|
||||||
buf.Write(input.channels(), kNumChannels, kNumFrames);
|
buf.Write(input.channels(), kNumChannels, kNumFrames);
|
||||||
|
|
||||||
buf.MoveReadPosition(3);
|
buf.MoveReadPositionForward(3);
|
||||||
ChannelBuffer<float> output(1, kNumChannels);
|
ChannelBuffer<float> output(1, kNumChannels);
|
||||||
buf.Read(output.channels(), kNumChannels, 1);
|
buf.Read(output.channels(), kNumChannels, 1);
|
||||||
EXPECT_EQ(4, output.channels()[0][0]);
|
EXPECT_EQ(4, output.channels()[0][0]);
|
||||||
buf.MoveReadPosition(-3);
|
buf.MoveReadPositionBackward(3);
|
||||||
buf.Read(output.channels(), kNumChannels, 1);
|
buf.Read(output.channels(), kNumChannels, 1);
|
||||||
EXPECT_EQ(2, output.channels()[0][0]);
|
EXPECT_EQ(2, output.channels()[0][0]);
|
||||||
}
|
}
|
||||||
|
@ -119,10 +119,10 @@ Blocker::Blocker(int chunk_size,
|
|||||||
shift_amount_(shift_amount),
|
shift_amount_(shift_amount),
|
||||||
callback_(callback) {
|
callback_(callback) {
|
||||||
CHECK_LE(num_output_channels_, num_input_channels_);
|
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()));
|
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:
|
// 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_) {
|
while (first_frame_in_block < chunk_size_) {
|
||||||
input_buffer_.Read(input_block_.channels(), num_input_channels,
|
input_buffer_.Read(input_block_.channels(), num_input_channels,
|
||||||
block_size_);
|
block_size_);
|
||||||
input_buffer_.MoveReadPosition(-block_size_ + shift_amount_);
|
input_buffer_.MoveReadPositionBackward(block_size_ - shift_amount_);
|
||||||
|
|
||||||
ApplyWindow(window_.get(),
|
ApplyWindow(window_.get(),
|
||||||
block_size_,
|
block_size_,
|
||||||
|
Reference in New Issue
Block a user