blob: c2d6a507ffe82824051f8a57b2020c3101468a4b [file] [log] [blame]
henrik.lundin76622ce2016-11-25 05:30:47 -08001/*
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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020014#include "api/array_view.h"
15#include "modules/audio_processing/rms_level.h"
16#include "rtc_base/checks.h"
Steve Anton10542f22019-01-11 09:11:00 -080017#include "rtc_base/numerics/math_utils.h"
Karl Wiberge40468b2017-11-22 10:42:26 +010018#include "rtc_base/numerics/safe_conversions.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "test/gtest.h"
henrik.lundin76622ce2016-11-25 05:30:47 -080020
21namespace webrtc {
22namespace {
23constexpr int kSampleRateHz = 48000;
24constexpr size_t kBlockSizeSamples = kSampleRateHz / 100;
25
henrik.lundin50499422016-11-29 04:26:24 -080026std::unique_ptr<RmsLevel> RunTest(rtc::ArrayView<const int16_t> input) {
27 std::unique_ptr<RmsLevel> level(new RmsLevel);
henrik.lundin76622ce2016-11-25 05:30:47 -080028 for (size_t n = 0; n + kBlockSizeSamples <= input.size();
29 n += kBlockSizeSamples) {
henrik.lundin50499422016-11-29 04:26:24 -080030 level->Analyze(input.subview(n, kBlockSizeSamples));
henrik.lundin76622ce2016-11-25 05:30:47 -080031 }
32 return level;
33}
34
35std::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
47TEST(RmsLevelTest, Run1000HzFullScale) {
48 auto x = CreateSinusoid(1000, INT16_MAX, kSampleRateHz);
49 auto level = RunTest(x);
henrik.lundin50499422016-11-29 04:26:24 -080050 EXPECT_EQ(3, level->Average()); // -3 dBFS
51}
52
53TEST(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);
henrik.lundin76622ce2016-11-25 05:30:47 -080059}
60
61TEST(RmsLevelTest, Run1000HzHalfScale) {
62 auto x = CreateSinusoid(1000, INT16_MAX / 2, kSampleRateHz);
63 auto level = RunTest(x);
henrik.lundin50499422016-11-29 04:26:24 -080064 EXPECT_EQ(9, level->Average()); // -9 dBFS
henrik.lundin76622ce2016-11-25 05:30:47 -080065}
66
67TEST(RmsLevelTest, RunZeros) {
68 std::vector<int16_t> x(kSampleRateHz, 0); // 1 second of pure silence.
69 auto level = RunTest(x);
henrik.lundin50499422016-11-29 04:26:24 -080070 EXPECT_EQ(127, level->Average());
71}
72
73TEST(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);
henrik.lundin76622ce2016-11-25 05:30:47 -080079}
80
81TEST(RmsLevelTest, NoSamples) {
henrik.lundin50499422016-11-29 04:26:24 -080082 RmsLevel level;
83 EXPECT_EQ(127, level.Average()); // Return minimum if no samples are given.
84}
85
86TEST(RmsLevelTest, NoSamplesAverageAndPeak) {
87 RmsLevel level;
88 auto stats = level.AverageAndPeak();
89 EXPECT_EQ(127, stats.average);
90 EXPECT_EQ(127, stats.peak);
henrik.lundin76622ce2016-11-25 05:30:47 -080091}
92
93TEST(RmsLevelTest, PollTwice) {
94 auto x = CreateSinusoid(1000, INT16_MAX, kSampleRateHz);
95 auto level = RunTest(x);
henrik.lundin50499422016-11-29 04:26:24 -080096 level->Average();
97 EXPECT_EQ(127, level->Average()); // Stats should be reset at this point.
henrik.lundin76622ce2016-11-25 05:30:47 -080098}
99
100TEST(RmsLevelTest, Reset) {
101 auto x = CreateSinusoid(1000, INT16_MAX, kSampleRateHz);
102 auto level = RunTest(x);
103 level->Reset();
henrik.lundin50499422016-11-29 04:26:24 -0800104 EXPECT_EQ(127, level->Average()); // Stats should be reset at this point.
henrik.lundin76622ce2016-11-25 05:30:47 -0800105}
106
107// Inserts 1 second of full-scale sinusoid, followed by 1 second of muted.
108TEST(RmsLevelTest, ProcessMuted) {
109 auto x = CreateSinusoid(1000, INT16_MAX, kSampleRateHz);
110 auto level = RunTest(x);
henrik.lundin50499422016-11-29 04:26:24 -0800111 const size_t kBlocksPerSecond = rtc::CheckedDivExact(
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.
123TEST(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
137TEST(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);
henrik.lundin76622ce2016-11-25 05:30:47 -0800148}
149
150} // namespace webrtc