blob: a1ceaad7fb291c91a740355c855a6a22d54c3ddf [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 */
Yves Gerey14696c22019-04-11 13:51:10 +020010// MSVC++ requires this to be set before any other includes to get M_PI.
11#define _USE_MATH_DEFINES
Jonas Olssona4d87372019-07-05 19:08:33 +020012#include "modules/audio_processing/rms_level.h"
13
henrik.lundin76622ce2016-11-25 05:30:47 -080014#include <cmath>
15#include <memory>
16#include <vector>
17
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "api/array_view.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "rtc_base/checks.h"
Karl Wiberge40468b2017-11-22 10:42:26 +010020#include "rtc_base/numerics/safe_conversions.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "test/gtest.h"
henrik.lundin76622ce2016-11-25 05:30:47 -080022
23namespace webrtc {
24namespace {
25constexpr int kSampleRateHz = 48000;
26constexpr size_t kBlockSizeSamples = kSampleRateHz / 100;
27
henrik.lundin50499422016-11-29 04:26:24 -080028std::unique_ptr<RmsLevel> RunTest(rtc::ArrayView<const int16_t> input) {
29 std::unique_ptr<RmsLevel> level(new RmsLevel);
henrik.lundin76622ce2016-11-25 05:30:47 -080030 for (size_t n = 0; n + kBlockSizeSamples <= input.size();
31 n += kBlockSizeSamples) {
henrik.lundin50499422016-11-29 04:26:24 -080032 level->Analyze(input.subview(n, kBlockSizeSamples));
henrik.lundin76622ce2016-11-25 05:30:47 -080033 }
34 return level;
35}
36
37std::vector<int16_t> CreateSinusoid(int frequency_hz,
38 int amplitude,
39 size_t num_samples) {
40 std::vector<int16_t> x(num_samples);
41 for (size_t n = 0; n < num_samples; ++n) {
42 x[n] = rtc::saturated_cast<int16_t>(
43 amplitude * std::sin(2 * M_PI * n * frequency_hz / kSampleRateHz));
44 }
45 return x;
46}
47} // namespace
48
49TEST(RmsLevelTest, Run1000HzFullScale) {
50 auto x = CreateSinusoid(1000, INT16_MAX, kSampleRateHz);
51 auto level = RunTest(x);
henrik.lundin50499422016-11-29 04:26:24 -080052 EXPECT_EQ(3, level->Average()); // -3 dBFS
53}
54
55TEST(RmsLevelTest, Run1000HzFullScaleAverageAndPeak) {
56 auto x = CreateSinusoid(1000, INT16_MAX, kSampleRateHz);
57 auto level = RunTest(x);
58 auto stats = level->AverageAndPeak();
59 EXPECT_EQ(3, stats.average); // -3 dBFS
60 EXPECT_EQ(3, stats.peak);
henrik.lundin76622ce2016-11-25 05:30:47 -080061}
62
63TEST(RmsLevelTest, Run1000HzHalfScale) {
64 auto x = CreateSinusoid(1000, INT16_MAX / 2, kSampleRateHz);
65 auto level = RunTest(x);
henrik.lundin50499422016-11-29 04:26:24 -080066 EXPECT_EQ(9, level->Average()); // -9 dBFS
henrik.lundin76622ce2016-11-25 05:30:47 -080067}
68
69TEST(RmsLevelTest, RunZeros) {
70 std::vector<int16_t> x(kSampleRateHz, 0); // 1 second of pure silence.
71 auto level = RunTest(x);
henrik.lundin50499422016-11-29 04:26:24 -080072 EXPECT_EQ(127, level->Average());
73}
74
75TEST(RmsLevelTest, RunZerosAverageAndPeak) {
76 std::vector<int16_t> x(kSampleRateHz, 0); // 1 second of pure silence.
77 auto level = RunTest(x);
78 auto stats = level->AverageAndPeak();
79 EXPECT_EQ(127, stats.average);
80 EXPECT_EQ(127, stats.peak);
henrik.lundin76622ce2016-11-25 05:30:47 -080081}
82
83TEST(RmsLevelTest, NoSamples) {
henrik.lundin50499422016-11-29 04:26:24 -080084 RmsLevel level;
85 EXPECT_EQ(127, level.Average()); // Return minimum if no samples are given.
86}
87
88TEST(RmsLevelTest, NoSamplesAverageAndPeak) {
89 RmsLevel level;
90 auto stats = level.AverageAndPeak();
91 EXPECT_EQ(127, stats.average);
92 EXPECT_EQ(127, stats.peak);
henrik.lundin76622ce2016-11-25 05:30:47 -080093}
94
95TEST(RmsLevelTest, PollTwice) {
96 auto x = CreateSinusoid(1000, INT16_MAX, kSampleRateHz);
97 auto level = RunTest(x);
henrik.lundin50499422016-11-29 04:26:24 -080098 level->Average();
99 EXPECT_EQ(127, level->Average()); // Stats should be reset at this point.
henrik.lundin76622ce2016-11-25 05:30:47 -0800100}
101
102TEST(RmsLevelTest, Reset) {
103 auto x = CreateSinusoid(1000, INT16_MAX, kSampleRateHz);
104 auto level = RunTest(x);
105 level->Reset();
henrik.lundin50499422016-11-29 04:26:24 -0800106 EXPECT_EQ(127, level->Average()); // Stats should be reset at this point.
henrik.lundin76622ce2016-11-25 05:30:47 -0800107}
108
109// Inserts 1 second of full-scale sinusoid, followed by 1 second of muted.
110TEST(RmsLevelTest, ProcessMuted) {
111 auto x = CreateSinusoid(1000, INT16_MAX, kSampleRateHz);
112 auto level = RunTest(x);
henrik.lundin50499422016-11-29 04:26:24 -0800113 const size_t kBlocksPerSecond = rtc::CheckedDivExact(
114 static_cast<size_t>(kSampleRateHz), kBlockSizeSamples);
115 for (size_t i = 0; i < kBlocksPerSecond; ++i) {
116 level->AnalyzeMuted(kBlockSizeSamples);
117 }
118 EXPECT_EQ(6, level->Average()); // Average RMS halved due to the silence.
119}
120
121// Inserts 1 second of half-scale sinusoid, follwed by 10 ms of full-scale, and
122// finally 1 second of half-scale again. Expect the average to be -9 dBFS due
123// to the vast majority of the signal being half-scale, and the peak to be
124// -3 dBFS.
125TEST(RmsLevelTest, RunHalfScaleAndInsertFullScale) {
126 auto half_scale = CreateSinusoid(1000, INT16_MAX / 2, kSampleRateHz);
127 auto full_scale = CreateSinusoid(1000, INT16_MAX, kSampleRateHz / 100);
128 auto x = half_scale;
129 x.insert(x.end(), full_scale.begin(), full_scale.end());
130 x.insert(x.end(), half_scale.begin(), half_scale.end());
131 ASSERT_EQ(static_cast<size_t>(2 * kSampleRateHz + kSampleRateHz / 100),
132 x.size());
133 auto level = RunTest(x);
134 auto stats = level->AverageAndPeak();
135 EXPECT_EQ(9, stats.average);
136 EXPECT_EQ(3, stats.peak);
137}
138
139TEST(RmsLevelTest, ResetOnBlockSizeChange) {
140 auto x = CreateSinusoid(1000, INT16_MAX, kSampleRateHz);
141 auto level = RunTest(x);
142 // Create a new signal with half amplitude, but double block length.
143 auto y = CreateSinusoid(1000, INT16_MAX / 2, kBlockSizeSamples * 2);
144 level->Analyze(y);
145 auto stats = level->AverageAndPeak();
146 // Expect all stats to only be influenced by the last signal (y), since the
147 // changed block size should reset the stats.
148 EXPECT_EQ(9, stats.average);
149 EXPECT_EQ(9, stats.peak);
henrik.lundin76622ce2016-11-25 05:30:47 -0800150}
151
152} // namespace webrtc