henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 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 | |
Henrik Lundin | cf808d2 | 2015-05-27 14:33:29 +0200 | [diff] [blame] | 13 | #include <map> |
kwiberg | 2d0c332 | 2016-02-14 09:28:33 -0800 | [diff] [blame] | 14 | #include <memory> |
Henrik Lundin | cf808d2 | 2015-05-27 14:33:29 +0200 | [diff] [blame] | 15 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 16 | #include "common_audio/signal_processing/include/signal_processing_library.h" |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame^] | 17 | #include "modules/audio_coding/neteq/accelerate.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 18 | #include "modules/audio_coding/neteq/background_noise.h" |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame^] | 19 | #include "modules/audio_coding/neteq/preemptive_expand.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 20 | #include "modules/audio_coding/neteq/tools/input_audio_file.h" |
| 21 | #include "rtc_base/checks.h" |
| 22 | #include "test/gtest.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 23 | #include "test/testsupport/file_utils.h" |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 24 | |
| 25 | namespace webrtc { |
| 26 | |
Henrik Lundin | cf808d2 | 2015-05-27 14:33:29 +0200 | [diff] [blame] | 27 | namespace { |
| 28 | const size_t kNumChannels = 1; |
| 29 | } |
| 30 | |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 31 | TEST(TimeStretch, CreateAndDestroy) { |
henrik.lundin@webrtc.org | d9faa46 | 2014-01-14 10:18:45 +0000 | [diff] [blame] | 32 | const int kSampleRate = 8000; |
turaj@webrtc.org | 8d1cdaa | 2014-04-11 18:47:55 +0000 | [diff] [blame] | 33 | const int kOverlapSamples = 5 * kSampleRate / 8000; |
henrik.lundin@webrtc.org | d9faa46 | 2014-01-14 10:18:45 +0000 | [diff] [blame] | 34 | BackgroundNoise bgn(kNumChannels); |
| 35 | Accelerate accelerate(kSampleRate, kNumChannels, bgn); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 36 | PreemptiveExpand preemptive_expand(kSampleRate, kNumChannels, bgn, |
| 37 | kOverlapSamples); |
henrik.lundin@webrtc.org | d9faa46 | 2014-01-14 10:18:45 +0000 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | TEST(TimeStretch, CreateUsingFactory) { |
| 41 | const int kSampleRate = 8000; |
turaj@webrtc.org | 8d1cdaa | 2014-04-11 18:47:55 +0000 | [diff] [blame] | 42 | const int kOverlapSamples = 5 * kSampleRate / 8000; |
henrik.lundin@webrtc.org | d9faa46 | 2014-01-14 10:18:45 +0000 | [diff] [blame] | 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; |
turaj@webrtc.org | 8d1cdaa | 2014-04-11 18:47:55 +0000 | [diff] [blame] | 52 | PreemptiveExpand* preemptive_expand = preemptive_expand_factory.Create( |
| 53 | kSampleRate, kNumChannels, bgn, kOverlapSamples); |
henrik.lundin@webrtc.org | d9faa46 | 2014-01-14 10:18:45 +0000 | [diff] [blame] | 54 | EXPECT_TRUE(preemptive_expand != NULL); |
| 55 | delete preemptive_expand; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 56 | } |
| 57 | |
Henrik Lundin | cf808d2 | 2015-05-27 14:33:29 +0200 | [diff] [blame] | 58 | class TimeStretchTest : public ::testing::Test { |
| 59 | protected: |
| 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_]), |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame^] | 66 | background_noise_(kNumChannels) {} |
Henrik Lundin | cf808d2 | 2015-05-27 14:33:29 +0200 | [diff] [blame] | 67 | |
| 68 | const int16_t* Next30Ms() { |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 69 | RTC_CHECK(input_file_->Read(block_size_, audio_.get())); |
Henrik Lundin | cf808d2 | 2015-05-27 14:33:29 +0200 | [diff] [blame] | 70 | return audio_.get(); |
| 71 | } |
| 72 | |
| 73 | // Returns the total length change (in samples) that the accelerate operation |
| 74 | // resulted in during the run. |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 75 | size_t TestAccelerate(size_t loops, bool fast_mode) { |
Henrik Lundin | cf808d2 | 2015-05-27 14:33:29 +0200 | [diff] [blame] | 76 | Accelerate accelerate(sample_rate_hz_, kNumChannels, background_noise_); |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 77 | size_t total_length_change = 0; |
| 78 | for (size_t i = 0; i < loops; ++i) { |
Henrik Lundin | cf808d2 | 2015-05-27 14:33:29 +0200 | [diff] [blame] | 79 | AudioMultiVector output(kNumChannels); |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 80 | size_t length_change; |
Henrik Lundin | cf808d2 | 2015-05-27 14:33:29 +0200 | [diff] [blame] | 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 | |
| 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 | |
kwiberg | 2d0c332 | 2016-02-14 09:28:33 -0800 | [diff] [blame] | 100 | std::unique_ptr<test::InputAudioFile> input_file_; |
Henrik Lundin | cf808d2 | 2015-05-27 14:33:29 +0200 | [diff] [blame] | 101 | const int sample_rate_hz_; |
| 102 | const size_t block_size_; |
kwiberg | 2d0c332 | 2016-02-14 09:28:33 -0800 | [diff] [blame] | 103 | std::unique_ptr<int16_t[]> audio_; |
Henrik Lundin | cf808d2 | 2015-05-27 14:33:29 +0200 | [diff] [blame] | 104 | std::map<TimeStretch::ReturnCodes, int> return_stats_; |
| 105 | BackgroundNoise background_noise_; |
| 106 | }; |
| 107 | |
henrika | 1d34fe9 | 2015-06-16 10:04:20 +0200 | [diff] [blame] | 108 | TEST_F(TimeStretchTest, Accelerate) { |
Henrik Lundin | cf808d2 | 2015-05-27 14:33:29 +0200 | [diff] [blame] | 109 | // TestAccelerate returns the total length change in samples. |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 110 | EXPECT_EQ(15268U, TestAccelerate(100, false)); |
Henrik Lundin | cf808d2 | 2015-05-27 14:33:29 +0200 | [diff] [blame] | 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 | |
henrika | 1d34fe9 | 2015-06-16 10:04:20 +0200 | [diff] [blame] | 116 | TEST_F(TimeStretchTest, AccelerateFastMode) { |
Henrik Lundin | cf808d2 | 2015-05-27 14:33:29 +0200 | [diff] [blame] | 117 | // TestAccelerate returns the total length change in samples. |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 118 | EXPECT_EQ(21400U, TestAccelerate(100, true)); |
Henrik Lundin | cf808d2 | 2015-05-27 14:33:29 +0200 | [diff] [blame] | 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 | } |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 123 | |
| 124 | } // namespace webrtc |