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