1 /*
2 * Copyright (c) 2012 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 // Unit tests for Merge class.
12
13 #include "modules/audio_coding/neteq/merge.h"
14
15 #include <algorithm>
16 #include <vector>
17
18 #include "modules/audio_coding/neteq/background_noise.h"
19 #include "modules/audio_coding/neteq/expand.h"
20 #include "modules/audio_coding/neteq/random_vector.h"
21 #include "modules/audio_coding/neteq/statistics_calculator.h"
22 #include "modules/audio_coding/neteq/sync_buffer.h"
23 #include "modules/audio_coding/neteq/tools/resample_input_audio_file.h"
24 #include "test/gtest.h"
25 #include "test/testsupport/file_utils.h"
26
27 namespace webrtc {
28
TEST(Merge,CreateAndDestroy)29 TEST(Merge, CreateAndDestroy) {
30 int fs = 8000;
31 size_t channels = 1;
32 BackgroundNoise bgn(channels);
33 SyncBuffer sync_buffer(1, 1000);
34 RandomVector random_vector;
35 StatisticsCalculator statistics;
36 Expand expand(&bgn, &sync_buffer, &random_vector, &statistics, fs, channels);
37 Merge merge(fs, channels, &expand, &sync_buffer);
38 }
39
40 namespace {
41 // This is the same size that is given to the SyncBuffer object in NetEq.
42 const size_t kNetEqSyncBufferLengthMs = 720;
43 } // namespace
44
45 class MergeTest : public testing::TestWithParam<size_t> {
46 protected:
MergeTest()47 MergeTest()
48 : input_file_(test::ResourcePath("audio_coding/testfile32kHz", "pcm"),
49 32000),
50 test_sample_rate_hz_(8000),
51 num_channels_(1),
52 background_noise_(num_channels_),
53 sync_buffer_(num_channels_,
54 kNetEqSyncBufferLengthMs * test_sample_rate_hz_ / 1000),
55 expand_(&background_noise_,
56 &sync_buffer_,
57 &random_vector_,
58 &statistics_,
59 test_sample_rate_hz_,
60 num_channels_),
61 merge_(test_sample_rate_hz_, num_channels_, &expand_, &sync_buffer_) {
62 input_file_.set_output_rate_hz(test_sample_rate_hz_);
63 }
64
SetUp()65 void SetUp() override {
66 // Fast-forward the input file until there is speech (about 1.1 second into
67 // the file).
68 const int speech_start_samples =
69 static_cast<int>(test_sample_rate_hz_ * 1.1f);
70 ASSERT_TRUE(input_file_.Seek(speech_start_samples));
71
72 // Pre-load the sync buffer with speech data.
73 std::unique_ptr<int16_t[]> temp(new int16_t[sync_buffer_.Size()]);
74 ASSERT_TRUE(input_file_.Read(sync_buffer_.Size(), temp.get()));
75 sync_buffer_.Channel(0).OverwriteAt(temp.get(), sync_buffer_.Size(), 0);
76 // Move index such that the sync buffer appears to have 5 ms left to play.
77 sync_buffer_.set_next_index(sync_buffer_.next_index() -
78 test_sample_rate_hz_ * 5 / 1000);
79 ASSERT_EQ(1u, num_channels_) << "Fix: Must populate all channels.";
80 ASSERT_GT(sync_buffer_.FutureLength(), 0u);
81 }
82
83 test::ResampleInputAudioFile input_file_;
84 int test_sample_rate_hz_;
85 size_t num_channels_;
86 BackgroundNoise background_noise_;
87 SyncBuffer sync_buffer_;
88 RandomVector random_vector_;
89 StatisticsCalculator statistics_;
90 Expand expand_;
91 Merge merge_;
92 };
93
TEST_P(MergeTest,Process)94 TEST_P(MergeTest, Process) {
95 AudioMultiVector output(num_channels_);
96 // Start by calling Expand once, to prime the state.
97 EXPECT_EQ(0, expand_.Process(&output));
98 EXPECT_GT(output.Size(), 0u);
99 output.Clear();
100 // Now call Merge, but with a very short decoded input. Try different length
101 // if the input.
102 const size_t input_len = GetParam();
103 std::vector<int16_t> input(input_len, 17);
104 merge_.Process(input.data(), input_len, &output);
105 EXPECT_GT(output.Size(), 0u);
106 }
107
108 // Instantiate with values for the input length that are interesting in
109 // Merge::Downsample. Why are these values interesting?
110 // - In 8000 Hz sample rate, signal_offset in Merge::Downsample will be 2, so
111 // the values 1, 2, 3 are just around that value.
112 // - Also in 8000 Hz, the variable length_limit in the same method will be 80,
113 // so values 80 and 81 will be on either side of the branch point
114 // "input_length <= length_limit".
115 // - Finally, 160 is simply 20 ms in 8000 Hz, which is a common packet size.
116 INSTANTIATE_TEST_SUITE_P(DifferentInputLengths,
117 MergeTest,
118 testing::Values(1, 2, 3, 80, 81, 160));
119 // TODO(hlundin): Write more tests.
120
121 } // namespace webrtc
122