blob: 3671ce4dfbdb3284bc114d40ae3cfb328ac24d9b [file] [log] [blame]
andrew@webrtc.org041035b2015-01-26 21:23:53 +00001/*
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 "webrtc/common_audio/audio_ring_buffer.h"
12
13#include "testing/gtest/include/gtest/gtest.h"
14#include "webrtc/modules/audio_processing/channel_buffer.h"
15
16namespace webrtc {
17
18class AudioRingBufferTest :
19 public ::testing::TestWithParam< ::testing::tuple<int, int, int, int> > {
20};
21
22void ReadAndWriteTest(const ChannelBuffer<float>& input,
23 size_t num_write_chunk_frames,
24 size_t num_read_chunk_frames,
25 size_t buffer_frames,
26 ChannelBuffer<float>* output) {
27 const size_t num_channels = input.num_channels();
28 const size_t total_frames = input.samples_per_channel();
29 AudioRingBuffer buf(num_channels, buffer_frames);
30 scoped_ptr<float*[]> slice(new float*[num_channels]);
31
32 size_t input_pos = 0;
33 size_t output_pos = 0;
34 while (input_pos + buf.WriteFramesAvailable() < total_frames) {
35 // Write until the buffer is as full as possible.
36 while (buf.WriteFramesAvailable() >= num_write_chunk_frames) {
37 buf.Write(input.Slice(slice.get(), static_cast<int>(input_pos)),
38 num_channels, num_write_chunk_frames);
39 input_pos += num_write_chunk_frames;
40 }
41 // Read until the buffer is as empty as possible.
42 while (buf.ReadFramesAvailable() >= num_read_chunk_frames) {
43 EXPECT_LT(output_pos, total_frames);
44 buf.Read(output->Slice(slice.get(), static_cast<int>(output_pos)),
45 num_channels, num_read_chunk_frames);
46 output_pos += num_read_chunk_frames;
47 }
48 }
49
50 // Write and read the last bit.
51 if (input_pos < total_frames)
52 buf.Write(input.Slice(slice.get(), static_cast<int>(input_pos)),
53 num_channels, total_frames - input_pos);
54 if (buf.ReadFramesAvailable())
55 buf.Read(output->Slice(slice.get(), static_cast<int>(output_pos)),
56 num_channels, buf.ReadFramesAvailable());
57 EXPECT_EQ(0u, buf.ReadFramesAvailable());
58}
59
60TEST_P(AudioRingBufferTest, ReadDataMatchesWrittenData) {
61 const size_t kFrames = 5000;
62 const size_t num_channels = ::testing::get<3>(GetParam());
63
64 // Initialize the input data to an increasing sequence.
65 ChannelBuffer<float> input(kFrames, static_cast<int>(num_channels));
66 for (size_t i = 0; i < num_channels; ++i)
67 for (size_t j = 0; j < kFrames; ++j)
andrew@webrtc.org922cfcd2015-01-27 21:59:33 +000068 input.channels()[i][j] = (i + 1) * (j + 1);
andrew@webrtc.org041035b2015-01-26 21:23:53 +000069
70 ChannelBuffer<float> output(kFrames, static_cast<int>(num_channels));
71 ReadAndWriteTest(input,
72 ::testing::get<0>(GetParam()),
73 ::testing::get<1>(GetParam()),
74 ::testing::get<2>(GetParam()),
75 &output);
76
77 // Verify the read data matches the input.
78 for (size_t i = 0; i < num_channels; ++i)
79 for (size_t j = 0; j < kFrames; ++j)
80 EXPECT_EQ(input.channels()[i][j], output.channels()[i][j]);
81}
82
83INSTANTIATE_TEST_CASE_P(
84 AudioRingBufferTest, AudioRingBufferTest,
85 ::testing::Combine(::testing::Values(10, 20, 42), // num_write_chunk_frames
86 ::testing::Values(1, 10, 17), // num_read_chunk_frames
87 ::testing::Values(100, 256), // buffer_frames
88 ::testing::Values(1, 4))); // num_channels
89
90TEST_F(AudioRingBufferTest, MoveReadPosition) {
91 const size_t kNumChannels = 1;
92 const float kInputArray[] = {1, 2, 3, 4};
93 const size_t kNumFrames = sizeof(kInputArray) / sizeof(*kInputArray);
94 ChannelBuffer<float> input(kInputArray, kNumFrames, kNumChannels);
95 AudioRingBuffer buf(kNumChannels, kNumFrames);
96 buf.Write(input.channels(), kNumChannels, kNumFrames);
97
98 buf.MoveReadPosition(3);
99 ChannelBuffer<float> output(1, kNumChannels);
100 buf.Read(output.channels(), kNumChannels, 1);
101 EXPECT_EQ(4, output.data()[0]);
102 buf.MoveReadPosition(-3);
103 buf.Read(output.channels(), kNumChannels, 1);
104 EXPECT_EQ(2, output.data()[0]);
105}
106
107} // namespace webrtc