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

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

Issue 2535523002: Refactor RMSLevel and give it new functionality (Closed)
Patch Set: Rename to RmsLevel 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
« no previous file with comments | « webrtc/modules/audio_processing/rms_level.cc ('k') | webrtc/voice_engine/channel.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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/checks.h"
15 #include "webrtc/base/mathutils.h" 16 #include "webrtc/base/mathutils.h"
16 #include "webrtc/base/safe_conversions.h" 17 #include "webrtc/base/safe_conversions.h"
17 #include "webrtc/modules/audio_processing/rms_level.h" 18 #include "webrtc/modules/audio_processing/rms_level.h"
18 #include "webrtc/test/gtest.h" 19 #include "webrtc/test/gtest.h"
19 20
20 namespace webrtc { 21 namespace webrtc {
21 namespace { 22 namespace {
22 constexpr int kSampleRateHz = 48000; 23 constexpr int kSampleRateHz = 48000;
23 constexpr size_t kBlockSizeSamples = kSampleRateHz / 100; 24 constexpr size_t kBlockSizeSamples = kSampleRateHz / 100;
24 25
25 std::unique_ptr<RMSLevel> RunTest(rtc::ArrayView<const int16_t> input) { 26 std::unique_ptr<RmsLevel> RunTest(rtc::ArrayView<const int16_t> input) {
26 std::unique_ptr<RMSLevel> level(new RMSLevel); 27 std::unique_ptr<RmsLevel> level(new RmsLevel);
27 for (size_t n = 0; n + kBlockSizeSamples <= input.size(); 28 for (size_t n = 0; n + kBlockSizeSamples <= input.size();
28 n += kBlockSizeSamples) { 29 n += kBlockSizeSamples) {
29 level->Process(input.subview(n, kBlockSizeSamples).data(), 30 level->Analyze(input.subview(n, kBlockSizeSamples));
30 kBlockSizeSamples);
31 } 31 }
32 return level; 32 return level;
33 } 33 }
34 34
35 std::vector<int16_t> CreateSinusoid(int frequency_hz, 35 std::vector<int16_t> CreateSinusoid(int frequency_hz,
36 int amplitude, 36 int amplitude,
37 size_t num_samples) { 37 size_t num_samples) {
38 std::vector<int16_t> x(num_samples); 38 std::vector<int16_t> x(num_samples);
39 for (size_t n = 0; n < num_samples; ++n) { 39 for (size_t n = 0; n < num_samples; ++n) {
40 x[n] = rtc::saturated_cast<int16_t>( 40 x[n] = rtc::saturated_cast<int16_t>(
41 amplitude * std::sin(2 * M_PI * n * frequency_hz / kSampleRateHz)); 41 amplitude * std::sin(2 * M_PI * n * frequency_hz / kSampleRateHz));
42 } 42 }
43 return x; 43 return x;
44 } 44 }
45 } // namespace 45 } // namespace
46 46
47 TEST(RmsLevelTest, Run1000HzFullScale) { 47 TEST(RmsLevelTest, Run1000HzFullScale) {
48 auto x = CreateSinusoid(1000, INT16_MAX, kSampleRateHz); 48 auto x = CreateSinusoid(1000, INT16_MAX, kSampleRateHz);
49 auto level = RunTest(x); 49 auto level = RunTest(x);
50 EXPECT_EQ(3, level->RMS()); // -3 dBFS 50 EXPECT_EQ(3, level->Average()); // -3 dBFS
51 }
52
53 TEST(RmsLevelTest, Run1000HzFullScaleAverageAndPeak) {
54 auto x = CreateSinusoid(1000, INT16_MAX, kSampleRateHz);
55 auto level = RunTest(x);
56 auto stats = level->AverageAndPeak();
57 EXPECT_EQ(3, stats.average); // -3 dBFS
58 EXPECT_EQ(3, stats.peak);
51 } 59 }
52 60
53 TEST(RmsLevelTest, Run1000HzHalfScale) { 61 TEST(RmsLevelTest, Run1000HzHalfScale) {
54 auto x = CreateSinusoid(1000, INT16_MAX / 2, kSampleRateHz); 62 auto x = CreateSinusoid(1000, INT16_MAX / 2, kSampleRateHz);
55 auto level = RunTest(x); 63 auto level = RunTest(x);
56 EXPECT_EQ(9, level->RMS()); // -9 dBFS 64 EXPECT_EQ(9, level->Average()); // -9 dBFS
57 } 65 }
58 66
59 TEST(RmsLevelTest, RunZeros) { 67 TEST(RmsLevelTest, RunZeros) {
60 std::vector<int16_t> x(kSampleRateHz, 0); // 1 second of pure silence. 68 std::vector<int16_t> x(kSampleRateHz, 0); // 1 second of pure silence.
61 auto level = RunTest(x); 69 auto level = RunTest(x);
62 EXPECT_EQ(127, level->RMS()); 70 EXPECT_EQ(127, level->Average());
71 }
72
73 TEST(RmsLevelTest, RunZerosAverageAndPeak) {
74 std::vector<int16_t> x(kSampleRateHz, 0); // 1 second of pure silence.
75 auto level = RunTest(x);
76 auto stats = level->AverageAndPeak();
77 EXPECT_EQ(127, stats.average);
78 EXPECT_EQ(127, stats.peak);
63 } 79 }
64 80
65 TEST(RmsLevelTest, NoSamples) { 81 TEST(RmsLevelTest, NoSamples) {
66 RMSLevel level; 82 RmsLevel level;
67 EXPECT_EQ(127, level.RMS()); // Return minimum if no samples are given. 83 EXPECT_EQ(127, level.Average()); // Return minimum if no samples are given.
84 }
85
86 TEST(RmsLevelTest, NoSamplesAverageAndPeak) {
87 RmsLevel level;
88 auto stats = level.AverageAndPeak();
89 EXPECT_EQ(127, stats.average);
90 EXPECT_EQ(127, stats.peak);
68 } 91 }
69 92
70 TEST(RmsLevelTest, PollTwice) { 93 TEST(RmsLevelTest, PollTwice) {
71 auto x = CreateSinusoid(1000, INT16_MAX, kSampleRateHz); 94 auto x = CreateSinusoid(1000, INT16_MAX, kSampleRateHz);
72 auto level = RunTest(x); 95 auto level = RunTest(x);
73 level->RMS(); 96 level->Average();
74 EXPECT_EQ(127, level->RMS()); // Stats should be reset at this point. 97 EXPECT_EQ(127, level->Average()); // Stats should be reset at this point.
75 } 98 }
76 99
77 TEST(RmsLevelTest, Reset) { 100 TEST(RmsLevelTest, Reset) {
78 auto x = CreateSinusoid(1000, INT16_MAX, kSampleRateHz); 101 auto x = CreateSinusoid(1000, INT16_MAX, kSampleRateHz);
79 auto level = RunTest(x); 102 auto level = RunTest(x);
80 level->Reset(); 103 level->Reset();
81 EXPECT_EQ(127, level->RMS()); // Stats should be reset at this point. 104 EXPECT_EQ(127, level->Average()); // Stats should be reset at this point.
82 } 105 }
83 106
84 // Inserts 1 second of full-scale sinusoid, followed by 1 second of muted. 107 // Inserts 1 second of full-scale sinusoid, followed by 1 second of muted.
85 TEST(RmsLevelTest, ProcessMuted) { 108 TEST(RmsLevelTest, ProcessMuted) {
86 auto x = CreateSinusoid(1000, INT16_MAX, kSampleRateHz); 109 auto x = CreateSinusoid(1000, INT16_MAX, kSampleRateHz);
87 auto level = RunTest(x); 110 auto level = RunTest(x);
88 level->ProcessMuted(kSampleRateHz); 111 const size_t kBlocksPerSecond = rtc::CheckedDivExact(
89 EXPECT_EQ(6, level->RMS()); // Average RMS halved due to the silence. 112 static_cast<size_t>(kSampleRateHz), kBlockSizeSamples);
113 for (size_t i = 0; i < kBlocksPerSecond; ++i) {
114 level->AnalyzeMuted(kBlockSizeSamples);
115 }
116 EXPECT_EQ(6, level->Average()); // Average RMS halved due to the silence.
117 }
118
119 // Inserts 1 second of half-scale sinusoid, follwed by 10 ms of full-scale, and
120 // finally 1 second of half-scale again. Expect the average to be -9 dBFS due
121 // to the vast majority of the signal being half-scale, and the peak to be
122 // -3 dBFS.
123 TEST(RmsLevelTest, RunHalfScaleAndInsertFullScale) {
124 auto half_scale = CreateSinusoid(1000, INT16_MAX / 2, kSampleRateHz);
125 auto full_scale = CreateSinusoid(1000, INT16_MAX, kSampleRateHz / 100);
126 auto x = half_scale;
127 x.insert(x.end(), full_scale.begin(), full_scale.end());
128 x.insert(x.end(), half_scale.begin(), half_scale.end());
129 ASSERT_EQ(static_cast<size_t>(2 * kSampleRateHz + kSampleRateHz / 100),
130 x.size());
131 auto level = RunTest(x);
132 auto stats = level->AverageAndPeak();
133 EXPECT_EQ(9, stats.average);
134 EXPECT_EQ(3, stats.peak);
135 }
136
137 TEST(RmsLevelTest, ResetOnBlockSizeChange) {
138 auto x = CreateSinusoid(1000, INT16_MAX, kSampleRateHz);
139 auto level = RunTest(x);
140 // Create a new signal with half amplitude, but double block length.
141 auto y = CreateSinusoid(1000, INT16_MAX / 2, kBlockSizeSamples * 2);
142 level->Analyze(y);
143 auto stats = level->AverageAndPeak();
144 // Expect all stats to only be influenced by the last signal (y), since the
145 // changed block size should reset the stats.
146 EXPECT_EQ(9, stats.average);
147 EXPECT_EQ(9, stats.peak);
90 } 148 }
91 149
92 } // namespace webrtc 150 } // namespace webrtc
OLDNEW
« no previous file with comments | « webrtc/modules/audio_processing/rms_level.cc ('k') | webrtc/voice_engine/channel.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698