| OLD | NEW |
| (Empty) |
| 1 /* | |
| 2 * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. | |
| 3 * | |
| 4 * Use of this source code is governed by a BSD-style license | |
| 5 * that can be found in the LICENSE file in the root of the source | |
| 6 * tree. An additional intellectual property rights grant can be found | |
| 7 * in the file PATENTS. All contributing project authors may | |
| 8 * be found in the AUTHORS file in the root of the source tree. | |
| 9 */ | |
| 10 | |
| 11 #include <memory> | |
| 12 #include <tuple> | |
| 13 | |
| 14 #include "webrtc/modules/audio_processing/utility/audio_ring_buffer.h" | |
| 15 | |
| 16 #include "testing/gtest/include/gtest/gtest.h" | |
| 17 #include "webrtc/common_audio/channel_buffer.h" | |
| 18 | |
| 19 namespace webrtc { | |
| 20 | |
| 21 class AudioRingBufferTest : | |
| 22 public ::testing::TestWithParam< ::testing::tuple<int, int, int, int> > { | |
| 23 }; | |
| 24 | |
| 25 void ReadAndWriteTest(const ChannelBuffer<float>& input, | |
| 26 size_t num_write_chunk_frames, | |
| 27 size_t num_read_chunk_frames, | |
| 28 size_t buffer_frames, | |
| 29 ChannelBuffer<float>* output) { | |
| 30 const size_t num_channels = input.num_channels(); | |
| 31 const size_t total_frames = input.num_frames(); | |
| 32 AudioRingBuffer buf(num_channels, buffer_frames); | |
| 33 std::unique_ptr<float* []> slice(new float*[num_channels]); | |
| 34 | |
| 35 size_t input_pos = 0; | |
| 36 size_t output_pos = 0; | |
| 37 while (input_pos + buf.WriteFramesAvailable() < total_frames) { | |
| 38 // Write until the buffer is as full as possible. | |
| 39 while (buf.WriteFramesAvailable() >= num_write_chunk_frames) { | |
| 40 buf.Write(input.Slice(slice.get(), input_pos), num_channels, | |
| 41 num_write_chunk_frames); | |
| 42 input_pos += num_write_chunk_frames; | |
| 43 } | |
| 44 // Read until the buffer is as empty as possible. | |
| 45 while (buf.ReadFramesAvailable() >= num_read_chunk_frames) { | |
| 46 EXPECT_LT(output_pos, total_frames); | |
| 47 buf.Read(output->Slice(slice.get(), output_pos), num_channels, | |
| 48 num_read_chunk_frames); | |
| 49 output_pos += num_read_chunk_frames; | |
| 50 } | |
| 51 } | |
| 52 | |
| 53 // Write and read the last bit. | |
| 54 if (input_pos < total_frames) { | |
| 55 buf.Write(input.Slice(slice.get(), input_pos), num_channels, | |
| 56 total_frames - input_pos); | |
| 57 } | |
| 58 if (buf.ReadFramesAvailable()) { | |
| 59 buf.Read(output->Slice(slice.get(), output_pos), num_channels, | |
| 60 buf.ReadFramesAvailable()); | |
| 61 } | |
| 62 EXPECT_EQ(0u, buf.ReadFramesAvailable()); | |
| 63 } | |
| 64 | |
| 65 TEST_P(AudioRingBufferTest, ReadDataMatchesWrittenData) { | |
| 66 const size_t kFrames = 5000; | |
| 67 const size_t num_channels = ::testing::get<3>(GetParam()); | |
| 68 | |
| 69 // Initialize the input data to an increasing sequence. | |
| 70 ChannelBuffer<float> input(kFrames, static_cast<int>(num_channels)); | |
| 71 for (size_t i = 0; i < num_channels; ++i) | |
| 72 for (size_t j = 0; j < kFrames; ++j) | |
| 73 input.channels()[i][j] = (i + 1) * (j + 1); | |
| 74 | |
| 75 ChannelBuffer<float> output(kFrames, static_cast<int>(num_channels)); | |
| 76 ReadAndWriteTest(input, | |
| 77 ::testing::get<0>(GetParam()), | |
| 78 ::testing::get<1>(GetParam()), | |
| 79 ::testing::get<2>(GetParam()), | |
| 80 &output); | |
| 81 | |
| 82 // Verify the read data matches the input. | |
| 83 for (size_t i = 0; i < num_channels; ++i) | |
| 84 for (size_t j = 0; j < kFrames; ++j) | |
| 85 EXPECT_EQ(input.channels()[i][j], output.channels()[i][j]); | |
| 86 } | |
| 87 | |
| 88 INSTANTIATE_TEST_CASE_P( | |
| 89 AudioRingBufferTest, AudioRingBufferTest, | |
| 90 ::testing::Combine(::testing::Values(10, 20, 42), // num_write_chunk_frames | |
| 91 ::testing::Values(1, 10, 17), // num_read_chunk_frames | |
| 92 ::testing::Values(100, 256), // buffer_frames | |
| 93 ::testing::Values(1, 4))); // num_channels | |
| 94 | |
| 95 TEST_F(AudioRingBufferTest, MoveReadPosition) { | |
| 96 const size_t kNumChannels = 1; | |
| 97 const float kInputArray[] = {1, 2, 3, 4}; | |
| 98 const size_t kNumFrames = sizeof(kInputArray) / sizeof(*kInputArray); | |
| 99 ChannelBuffer<float> input(kNumFrames, kNumChannels); | |
| 100 input.SetDataForTesting(kInputArray, kNumFrames); | |
| 101 AudioRingBuffer buf(kNumChannels, kNumFrames); | |
| 102 buf.Write(input.channels(), kNumChannels, kNumFrames); | |
| 103 | |
| 104 buf.MoveReadPositionForward(3); | |
| 105 ChannelBuffer<float> output(1, kNumChannels); | |
| 106 buf.Read(output.channels(), kNumChannels, 1); | |
| 107 EXPECT_EQ(4, output.channels()[0][0]); | |
| 108 buf.MoveReadPositionBackward(3); | |
| 109 buf.Read(output.channels(), kNumChannels, 1); | |
| 110 EXPECT_EQ(2, output.channels()[0][0]); | |
| 111 } | |
| 112 | |
| 113 } // namespace webrtc | |
| OLD | NEW |