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