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

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

Issue 2524273003: Adding a unit test for RMSLevel (Closed)
Patch Set: Review comments 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/BUILD.gn ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 /*
2 * Copyright (c) 2016 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 #include <cmath>
11 #include <memory>
12 #include <vector>
13
14 #include "webrtc/base/array_view.h"
15 #include "webrtc/base/mathutils.h"
16 #include "webrtc/base/safe_conversions.h"
17 #include "webrtc/modules/audio_processing/rms_level.h"
18 #include "webrtc/test/gtest.h"
19
20 namespace webrtc {
21 namespace {
22 constexpr int kSampleRateHz = 48000;
23 constexpr size_t kBlockSizeSamples = kSampleRateHz / 100;
24
25 std::unique_ptr<RMSLevel> RunTest(rtc::ArrayView<const int16_t> input) {
26 std::unique_ptr<RMSLevel> level(new RMSLevel);
27 for (size_t n = 0; n + kBlockSizeSamples <= input.size();
28 n += kBlockSizeSamples) {
29 level->Process(input.subview(n, kBlockSizeSamples).data(),
30 kBlockSizeSamples);
31 }
32 return level;
33 }
34
35 std::vector<int16_t> CreateSinusoid(int frequency_hz,
36 int amplitude,
37 size_t num_samples) {
38 std::vector<int16_t> x(num_samples);
39 for (size_t n = 0; n < num_samples; ++n) {
40 x[n] = rtc::saturated_cast<int16_t>(
41 amplitude * std::sin(2 * M_PI * n * frequency_hz / kSampleRateHz));
42 }
43 return x;
44 }
45 } // namespace
46
47 TEST(RmsLevelTest, Run1000HzFullScale) {
48 auto x = CreateSinusoid(1000, INT16_MAX, kSampleRateHz);
49 auto level = RunTest(x);
50 EXPECT_EQ(3, level->RMS()); // -3 dBFS
51 }
52
53 TEST(RmsLevelTest, Run1000HzHalfScale) {
54 auto x = CreateSinusoid(1000, INT16_MAX / 2, kSampleRateHz);
55 auto level = RunTest(x);
56 EXPECT_EQ(9, level->RMS()); // -9 dBFS
57 }
58
59 TEST(RmsLevelTest, RunZeros) {
60 std::vector<int16_t> x(kSampleRateHz, 0); // 1 second of pure silence.
61 auto level = RunTest(x);
62 EXPECT_EQ(127, level->RMS());
63 }
64
65 TEST(RmsLevelTest, NoSamples) {
66 RMSLevel level;
67 EXPECT_EQ(127, level.RMS()); // Return minimum if no samples are given.
68 }
69
70 TEST(RmsLevelTest, PollTwice) {
71 auto x = CreateSinusoid(1000, INT16_MAX, kSampleRateHz);
72 auto level = RunTest(x);
73 level->RMS();
74 EXPECT_EQ(127, level->RMS()); // Stats should be reset at this point.
75 }
76
77 TEST(RmsLevelTest, Reset) {
78 auto x = CreateSinusoid(1000, INT16_MAX, kSampleRateHz);
79 auto level = RunTest(x);
80 level->Reset();
81 EXPECT_EQ(127, level->RMS()); // Stats should be reset at this point.
82 }
83
84 // Inserts 1 second of full-scale sinusoid, followed by 1 second of muted.
85 TEST(RmsLevelTest, ProcessMuted) {
86 auto x = CreateSinusoid(1000, INT16_MAX, kSampleRateHz);
87 auto level = RunTest(x);
88 level->ProcessMuted(kSampleRateHz);
89 EXPECT_EQ(6, level->RMS()); // Average RMS halved due to the silence.
90 }
91
92 } // namespace webrtc
OLDNEW
« no previous file with comments | « webrtc/modules/BUILD.gn ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698