time_stretch_unittest.cc (4355B)
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 Accelerate and PreemptiveExpand classes. 12 13 #include "modules/audio_coding/neteq/time_stretch.h" 14 15 #include <cstddef> 16 #include <cstdint> 17 #include <map> 18 #include <memory> 19 20 #include "modules/audio_coding/neteq/accelerate.h" 21 #include "modules/audio_coding/neteq/background_noise.h" 22 #include "modules/audio_coding/neteq/preemptive_expand.h" 23 #include "modules/audio_coding/neteq/tools/input_audio_file.h" 24 #include "rtc_base/checks.h" 25 #include "test/gtest.h" 26 #include "test/testsupport/file_utils.h" 27 28 namespace webrtc { 29 30 namespace { 31 constexpr size_t kNumChannels = 1; 32 } // namespace 33 34 TEST(TimeStretch, CreateAndDestroy) { 35 const int kSampleRate = 8000; 36 const int kOverlapSamples = 5 * kSampleRate / 8000; 37 BackgroundNoise bgn(kNumChannels); 38 Accelerate accelerate(kSampleRate, kNumChannels, bgn); 39 PreemptiveExpand preemptive_expand(kSampleRate, kNumChannels, bgn, 40 kOverlapSamples); 41 } 42 43 TEST(TimeStretch, CreateUsingFactory) { 44 const int kSampleRate = 8000; 45 const int kOverlapSamples = 5 * kSampleRate / 8000; 46 BackgroundNoise bgn(kNumChannels); 47 48 AccelerateFactory accelerate_factory; 49 Accelerate* accelerate = 50 accelerate_factory.Create(kSampleRate, kNumChannels, bgn); 51 EXPECT_TRUE(accelerate != nullptr); 52 delete accelerate; 53 54 PreemptiveExpandFactory preemptive_expand_factory; 55 PreemptiveExpand* preemptive_expand = preemptive_expand_factory.Create( 56 kSampleRate, kNumChannels, bgn, kOverlapSamples); 57 EXPECT_TRUE(preemptive_expand != nullptr); 58 delete preemptive_expand; 59 } 60 61 class TimeStretchTest : public ::testing::Test { 62 protected: 63 TimeStretchTest() 64 : input_file_(new test::InputAudioFile( 65 test::ResourcePath("audio_coding/testfile32kHz", "pcm"))), 66 sample_rate_hz_(32000), 67 block_size_(30 * sample_rate_hz_ / 1000), // 30 ms 68 audio_(new int16_t[block_size_]), 69 background_noise_(kNumChannels) {} 70 71 const int16_t* Next30Ms() { 72 RTC_CHECK(input_file_->Read(block_size_, audio_.get())); 73 return audio_.get(); 74 } 75 76 // Returns the total length change (in samples) that the accelerate operation 77 // resulted in during the run. 78 size_t TestAccelerate(size_t loops, bool fast_mode) { 79 Accelerate accelerate(sample_rate_hz_, kNumChannels, background_noise_); 80 size_t total_length_change = 0; 81 for (size_t i = 0; i < loops; ++i) { 82 AudioMultiVector output(kNumChannels); 83 size_t length_change; 84 UpdateReturnStats(accelerate.Process(Next30Ms(), block_size_, fast_mode, 85 &output, &length_change)); 86 total_length_change += length_change; 87 } 88 return total_length_change; 89 } 90 91 void UpdateReturnStats(TimeStretch::ReturnCodes ret) { 92 switch (ret) { 93 case TimeStretch::kSuccess: 94 case TimeStretch::kSuccessLowEnergy: 95 case TimeStretch::kNoStretch: 96 ++return_stats_[ret]; 97 break; 98 case TimeStretch::kError: 99 FAIL() << "Process returned an error"; 100 } 101 } 102 103 std::unique_ptr<test::InputAudioFile> input_file_; 104 const int sample_rate_hz_; 105 const size_t block_size_; 106 std::unique_ptr<int16_t[]> audio_; 107 std::map<TimeStretch::ReturnCodes, int> return_stats_; 108 BackgroundNoise background_noise_; 109 }; 110 111 TEST_F(TimeStretchTest, Accelerate) { 112 // TestAccelerate returns the total length change in samples. 113 EXPECT_EQ(15268U, TestAccelerate(100, false)); 114 EXPECT_EQ(9, return_stats_[TimeStretch::kSuccess]); 115 EXPECT_EQ(58, return_stats_[TimeStretch::kSuccessLowEnergy]); 116 EXPECT_EQ(33, return_stats_[TimeStretch::kNoStretch]); 117 } 118 119 TEST_F(TimeStretchTest, AccelerateFastMode) { 120 // TestAccelerate returns the total length change in samples. 121 EXPECT_EQ(21400U, TestAccelerate(100, true)); 122 EXPECT_EQ(31, return_stats_[TimeStretch::kSuccess]); 123 EXPECT_EQ(58, return_stats_[TimeStretch::kSuccessLowEnergy]); 124 EXPECT_EQ(11, return_stats_[TimeStretch::kNoStretch]); 125 } 126 127 } // namespace webrtc