Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(502)

Side by Side Diff: webrtc/modules/audio_processing/rms_level_unittest.cc

Issue 2535523002: Refactor RMSLevel and give it new functionality (Closed)
Patch Set: Fixing win64 compile Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. 2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license 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 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 6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may 7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree. 8 * be found in the AUTHORS file in the root of the source tree.
9 */ 9 */
10 #include <cmath> 10 #include <cmath>
11 #include <memory> 11 #include <memory>
12 #include <vector> 12 #include <vector>
13 13
14 #include "webrtc/base/array_view.h" 14 #include "webrtc/base/array_view.h"
15 #include "webrtc/base/mathutils.h" 15 #include "webrtc/base/mathutils.h"
16 #include "webrtc/base/safe_conversions.h" 16 #include "webrtc/base/safe_conversions.h"
17 #include "webrtc/modules/audio_processing/rms_level.h" 17 #include "webrtc/modules/audio_processing/rms_level.h"
18 #include "webrtc/test/gtest.h" 18 #include "webrtc/test/gtest.h"
19 19
20 namespace webrtc { 20 namespace webrtc {
21 namespace { 21 namespace {
22 constexpr int kSampleRateHz = 48000; 22 constexpr int kSampleRateHz = 48000;
23 constexpr size_t kBlockSizeSamples = kSampleRateHz / 100; 23 constexpr size_t kBlockSizeSamples = kSampleRateHz / 100;
24 24
25 std::unique_ptr<RMSLevel> RunTest(rtc::ArrayView<const int16_t> input) { 25 std::unique_ptr<RMSLevel> RunTest(rtc::ArrayView<const int16_t> input) {
26 std::unique_ptr<RMSLevel> level(new RMSLevel); 26 std::unique_ptr<RMSLevel> level(new RMSLevel);
27 for (size_t n = 0; n + kBlockSizeSamples <= input.size(); 27 for (size_t n = 0; n + kBlockSizeSamples <= input.size();
28 n += kBlockSizeSamples) { 28 n += kBlockSizeSamples) {
29 level->Process(input.subview(n, kBlockSizeSamples).data(), 29 level->Process(input.subview(n, kBlockSizeSamples));
30 kBlockSizeSamples);
31 } 30 }
32 return level; 31 return level;
33 } 32 }
34 33
35 std::vector<int16_t> CreateSinusoid(int frequency_hz, 34 std::vector<int16_t> CreateSinusoid(int frequency_hz,
36 int amplitude, 35 int amplitude,
37 size_t num_samples) { 36 size_t num_samples) {
38 std::vector<int16_t> x(num_samples); 37 std::vector<int16_t> x(num_samples);
39 for (size_t n = 0; n < num_samples; ++n) { 38 for (size_t n = 0; n < num_samples; ++n) {
40 x[n] = rtc::saturated_cast<int16_t>( 39 x[n] = rtc::saturated_cast<int16_t>(
41 amplitude * std::sin(2 * M_PI * n * frequency_hz / kSampleRateHz)); 40 amplitude * std::sin(2 * M_PI * n * frequency_hz / kSampleRateHz));
42 } 41 }
43 return x; 42 return x;
44 } 43 }
45 } // namespace 44 } // namespace
46 45
47 TEST(RmsLevelTest, Run1000HzFullScale) { 46 TEST(RmsLevelTest, Run1000HzFullScale) {
48 auto x = CreateSinusoid(1000, INT16_MAX, kSampleRateHz); 47 auto x = CreateSinusoid(1000, INT16_MAX, kSampleRateHz);
49 auto level = RunTest(x); 48 auto level = RunTest(x);
50 EXPECT_EQ(3, level->RMS()); // -3 dBFS 49 EXPECT_EQ(3, level->RMS()); // -3 dBFS
51 } 50 }
52 51
52 TEST(RmsLevelTest, Run1000HzFullScaleAverageAndPeak) {
53 auto x = CreateSinusoid(1000, INT16_MAX, kSampleRateHz);
54 auto level = RunTest(x);
55 auto stats = level->AverageAndPeak();
56 EXPECT_EQ(3, stats.average); // -3 dBFS
57 EXPECT_EQ(3, stats.peak);
58 }
59
53 TEST(RmsLevelTest, Run1000HzHalfScale) { 60 TEST(RmsLevelTest, Run1000HzHalfScale) {
54 auto x = CreateSinusoid(1000, INT16_MAX / 2, kSampleRateHz); 61 auto x = CreateSinusoid(1000, INT16_MAX / 2, kSampleRateHz);
55 auto level = RunTest(x); 62 auto level = RunTest(x);
56 EXPECT_EQ(9, level->RMS()); // -9 dBFS 63 EXPECT_EQ(9, level->RMS()); // -9 dBFS
57 } 64 }
58 65
59 TEST(RmsLevelTest, RunZeros) { 66 TEST(RmsLevelTest, RunZeros) {
60 std::vector<int16_t> x(kSampleRateHz, 0); // 1 second of pure silence. 67 std::vector<int16_t> x(kSampleRateHz, 0); // 1 second of pure silence.
61 auto level = RunTest(x); 68 auto level = RunTest(x);
62 EXPECT_EQ(127, level->RMS()); 69 EXPECT_EQ(127, level->RMS());
63 } 70 }
64 71
72 TEST(RmsLevelTest, RunZerosAverageAndPeak) {
73 std::vector<int16_t> x(kSampleRateHz, 0); // 1 second of pure silence.
74 auto level = RunTest(x);
75 auto stats = level->AverageAndPeak();
76 EXPECT_EQ(127, stats.average);
77 EXPECT_EQ(127, stats.peak);
78 }
79
65 TEST(RmsLevelTest, NoSamples) { 80 TEST(RmsLevelTest, NoSamples) {
66 RMSLevel level; 81 RMSLevel level;
67 EXPECT_EQ(127, level.RMS()); // Return minimum if no samples are given. 82 EXPECT_EQ(127, level.RMS()); // Return minimum if no samples are given.
68 } 83 }
69 84
85 TEST(RmsLevelTest, NoSamplesAverageAndPeak) {
86 RMSLevel level;
87 auto stats = level.AverageAndPeak();
88 EXPECT_EQ(127, stats.average);
89 EXPECT_EQ(127, stats.peak);
90 }
91
70 TEST(RmsLevelTest, PollTwice) { 92 TEST(RmsLevelTest, PollTwice) {
71 auto x = CreateSinusoid(1000, INT16_MAX, kSampleRateHz); 93 auto x = CreateSinusoid(1000, INT16_MAX, kSampleRateHz);
72 auto level = RunTest(x); 94 auto level = RunTest(x);
73 level->RMS(); 95 level->RMS();
74 EXPECT_EQ(127, level->RMS()); // Stats should be reset at this point. 96 EXPECT_EQ(127, level->RMS()); // Stats should be reset at this point.
75 } 97 }
76 98
77 TEST(RmsLevelTest, Reset) { 99 TEST(RmsLevelTest, Reset) {
78 auto x = CreateSinusoid(1000, INT16_MAX, kSampleRateHz); 100 auto x = CreateSinusoid(1000, INT16_MAX, kSampleRateHz);
79 auto level = RunTest(x); 101 auto level = RunTest(x);
80 level->Reset(); 102 level->Reset();
81 EXPECT_EQ(127, level->RMS()); // Stats should be reset at this point. 103 EXPECT_EQ(127, level->RMS()); // Stats should be reset at this point.
82 } 104 }
83 105
84 // Inserts 1 second of full-scale sinusoid, followed by 1 second of muted. 106 // Inserts 1 second of full-scale sinusoid, followed by 1 second of muted.
85 TEST(RmsLevelTest, ProcessMuted) { 107 TEST(RmsLevelTest, ProcessMuted) {
86 auto x = CreateSinusoid(1000, INT16_MAX, kSampleRateHz); 108 auto x = CreateSinusoid(1000, INT16_MAX, kSampleRateHz);
87 auto level = RunTest(x); 109 auto level = RunTest(x);
88 level->ProcessMuted(kSampleRateHz); 110 level->ProcessMuted(kSampleRateHz);
89 EXPECT_EQ(6, level->RMS()); // Average RMS halved due to the silence. 111 EXPECT_EQ(6, level->RMS()); // Average RMS halved due to the silence.
90 } 112 }
91 113
114 // Inserts 1 second of half-scale sinusoid, follwed by 10 ms of full-scale, and
115 // finally 1 second of half-scale again. Expect the average to be -9 dBFS due
116 // to the vast majority of the signal being half-scale, and the peak to be
117 // -3 dBFS.
118 TEST(RmsLevelTest, RunHalfScaleAndInsertFullScale) {
119 auto half_scale = CreateSinusoid(1000, INT16_MAX / 2, kSampleRateHz);
120 auto full_scale = CreateSinusoid(1000, INT16_MAX, kSampleRateHz / 100);
121 auto x = half_scale;
122 x.insert(x.end(), full_scale.begin(), full_scale.end());
123 x.insert(x.end(), half_scale.begin(), half_scale.end());
124 ASSERT_EQ(static_cast<size_t>(2 * kSampleRateHz + kSampleRateHz / 100),
125 x.size());
126 auto level = RunTest(x);
127 auto stats = level->AverageAndPeak();
128 EXPECT_EQ(9, stats.average);
129 EXPECT_EQ(3, stats.peak);
130 }
131
92 } // namespace webrtc 132 } // namespace webrtc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698