blob: 6f875f03a35195435f7b3b0f8ce922def290beda [file] [log] [blame]
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001/*
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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020013#include "modules/audio_coding/neteq/accelerate.h"
14#include "modules/audio_coding/neteq/preemptive_expand.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000015
Henrik Lundincf808d22015-05-27 14:33:29 +020016#include <map>
kwiberg2d0c3322016-02-14 09:28:33 -080017#include <memory>
Henrik Lundincf808d22015-05-27 14:33:29 +020018
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "common_audio/signal_processing/include/signal_processing_library.h"
20#include "modules/audio_coding/neteq/background_noise.h"
21#include "modules/audio_coding/neteq/tools/input_audio_file.h"
22#include "rtc_base/checks.h"
23#include "test/gtest.h"
Steve Anton10542f22019-01-11 09:11:00 -080024#include "test/testsupport/file_utils.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000025
26namespace webrtc {
27
Henrik Lundincf808d22015-05-27 14:33:29 +020028namespace {
29const size_t kNumChannels = 1;
30}
31
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000032TEST(TimeStretch, CreateAndDestroy) {
henrik.lundin@webrtc.orgd9faa462014-01-14 10:18:45 +000033 const int kSampleRate = 8000;
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +000034 const int kOverlapSamples = 5 * kSampleRate / 8000;
henrik.lundin@webrtc.orgd9faa462014-01-14 10:18:45 +000035 BackgroundNoise bgn(kNumChannels);
36 Accelerate accelerate(kSampleRate, kNumChannels, bgn);
Yves Gerey665174f2018-06-19 15:03:05 +020037 PreemptiveExpand preemptive_expand(kSampleRate, kNumChannels, bgn,
38 kOverlapSamples);
henrik.lundin@webrtc.orgd9faa462014-01-14 10:18:45 +000039}
40
41TEST(TimeStretch, CreateUsingFactory) {
42 const int kSampleRate = 8000;
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +000043 const int kOverlapSamples = 5 * kSampleRate / 8000;
henrik.lundin@webrtc.orgd9faa462014-01-14 10:18:45 +000044 BackgroundNoise bgn(kNumChannels);
45
46 AccelerateFactory accelerate_factory;
47 Accelerate* accelerate =
48 accelerate_factory.Create(kSampleRate, kNumChannels, bgn);
49 EXPECT_TRUE(accelerate != NULL);
50 delete accelerate;
51
52 PreemptiveExpandFactory preemptive_expand_factory;
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +000053 PreemptiveExpand* preemptive_expand = preemptive_expand_factory.Create(
54 kSampleRate, kNumChannels, bgn, kOverlapSamples);
henrik.lundin@webrtc.orgd9faa462014-01-14 10:18:45 +000055 EXPECT_TRUE(preemptive_expand != NULL);
56 delete preemptive_expand;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000057}
58
Henrik Lundincf808d22015-05-27 14:33:29 +020059class TimeStretchTest : public ::testing::Test {
60 protected:
61 TimeStretchTest()
62 : input_file_(new test::InputAudioFile(
63 test::ResourcePath("audio_coding/testfile32kHz", "pcm"))),
64 sample_rate_hz_(32000),
65 block_size_(30 * sample_rate_hz_ / 1000), // 30 ms
66 audio_(new int16_t[block_size_]),
67 background_noise_(kNumChannels) {
Henrik Lundincf808d22015-05-27 14:33:29 +020068 }
69
70 const int16_t* Next30Ms() {
henrikg91d6ede2015-09-17 00:24:34 -070071 RTC_CHECK(input_file_->Read(block_size_, audio_.get()));
Henrik Lundincf808d22015-05-27 14:33:29 +020072 return audio_.get();
73 }
74
75 // Returns the total length change (in samples) that the accelerate operation
76 // resulted in during the run.
Peter Kastingdce40cf2015-08-24 14:52:23 -070077 size_t TestAccelerate(size_t loops, bool fast_mode) {
Henrik Lundincf808d22015-05-27 14:33:29 +020078 Accelerate accelerate(sample_rate_hz_, kNumChannels, background_noise_);
Peter Kastingdce40cf2015-08-24 14:52:23 -070079 size_t total_length_change = 0;
80 for (size_t i = 0; i < loops; ++i) {
Henrik Lundincf808d22015-05-27 14:33:29 +020081 AudioMultiVector output(kNumChannels);
Peter Kastingdce40cf2015-08-24 14:52:23 -070082 size_t length_change;
Henrik Lundincf808d22015-05-27 14:33:29 +020083 UpdateReturnStats(accelerate.Process(Next30Ms(), block_size_, fast_mode,
84 &output, &length_change));
85 total_length_change += length_change;
86 }
87 return total_length_change;
88 }
89
90 void UpdateReturnStats(TimeStretch::ReturnCodes ret) {
91 switch (ret) {
92 case TimeStretch::kSuccess:
93 case TimeStretch::kSuccessLowEnergy:
94 case TimeStretch::kNoStretch:
95 ++return_stats_[ret];
96 break;
97 case TimeStretch::kError:
98 FAIL() << "Process returned an error";
99 }
100 }
101
kwiberg2d0c3322016-02-14 09:28:33 -0800102 std::unique_ptr<test::InputAudioFile> input_file_;
Henrik Lundincf808d22015-05-27 14:33:29 +0200103 const int sample_rate_hz_;
104 const size_t block_size_;
kwiberg2d0c3322016-02-14 09:28:33 -0800105 std::unique_ptr<int16_t[]> audio_;
Henrik Lundincf808d22015-05-27 14:33:29 +0200106 std::map<TimeStretch::ReturnCodes, int> return_stats_;
107 BackgroundNoise background_noise_;
108};
109
henrika1d34fe92015-06-16 10:04:20 +0200110TEST_F(TimeStretchTest, Accelerate) {
Henrik Lundincf808d22015-05-27 14:33:29 +0200111 // TestAccelerate returns the total length change in samples.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700112 EXPECT_EQ(15268U, TestAccelerate(100, false));
Henrik Lundincf808d22015-05-27 14:33:29 +0200113 EXPECT_EQ(9, return_stats_[TimeStretch::kSuccess]);
114 EXPECT_EQ(58, return_stats_[TimeStretch::kSuccessLowEnergy]);
115 EXPECT_EQ(33, return_stats_[TimeStretch::kNoStretch]);
116}
117
henrika1d34fe92015-06-16 10:04:20 +0200118TEST_F(TimeStretchTest, AccelerateFastMode) {
Henrik Lundincf808d22015-05-27 14:33:29 +0200119 // TestAccelerate returns the total length change in samples.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700120 EXPECT_EQ(21400U, TestAccelerate(100, true));
Henrik Lundincf808d22015-05-27 14:33:29 +0200121 EXPECT_EQ(31, return_stats_[TimeStretch::kSuccess]);
122 EXPECT_EQ(58, return_stats_[TimeStretch::kSuccessLowEnergy]);
123 EXPECT_EQ(11, return_stats_[TimeStretch::kNoStretch]);
124}
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000125
126} // namespace webrtc