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 // MSVC++ requires this to be set before any other includes to get M_PI.
11 #define _USE_MATH_DEFINES
12 #include "modules/audio_processing/rms_level.h"
13 
14 #include <cmath>
15 #include <memory>
16 #include <vector>
17 
18 #include "api/array_view.h"
19 #include "rtc_base/checks.h"
20 #include "rtc_base/numerics/safe_conversions.h"
21 #include "test/gtest.h"
22 
23 namespace webrtc {
24 namespace {
25 constexpr int kSampleRateHz = 48000;
26 constexpr size_t kBlockSizeSamples = kSampleRateHz / 100;
27 
RunTest(rtc::ArrayView<const int16_t> input)28 std::unique_ptr<RmsLevel> RunTest(rtc::ArrayView<const int16_t> input) {
29   std::unique_ptr<RmsLevel> level(new RmsLevel);
30   for (size_t n = 0; n + kBlockSizeSamples <= input.size();
31        n += kBlockSizeSamples) {
32     level->Analyze(input.subview(n, kBlockSizeSamples));
33   }
34   return level;
35 }
36 
RunTest(rtc::ArrayView<const float> input)37 std::unique_ptr<RmsLevel> RunTest(rtc::ArrayView<const float> input) {
38   std::unique_ptr<RmsLevel> level(new RmsLevel);
39   for (size_t n = 0; n + kBlockSizeSamples <= input.size();
40        n += kBlockSizeSamples) {
41     level->Analyze(input.subview(n, kBlockSizeSamples));
42   }
43   return level;
44 }
45 
CreateInt16Sinusoid(int frequency_hz,int amplitude,size_t num_samples)46 std::vector<int16_t> CreateInt16Sinusoid(int frequency_hz,
47                                          int amplitude,
48                                          size_t num_samples) {
49   std::vector<int16_t> x(num_samples);
50   for (size_t n = 0; n < num_samples; ++n) {
51     x[n] = rtc::saturated_cast<int16_t>(
52         amplitude * std::sin(2 * M_PI * n * frequency_hz / kSampleRateHz));
53   }
54   return x;
55 }
56 
CreateFloatSinusoid(int frequency_hz,int amplitude,size_t num_samples)57 std::vector<float> CreateFloatSinusoid(int frequency_hz,
58                                        int amplitude,
59                                        size_t num_samples) {
60   std::vector<int16_t> x16 =
61       CreateInt16Sinusoid(frequency_hz, amplitude, num_samples);
62   std::vector<float> x(x16.size());
63   for (size_t n = 0; n < x.size(); ++n) {
64     x[n] = x16[n];
65   }
66   return x;
67 }
68 
69 }  // namespace
70 
TEST(RmsLevelTest,VerifyIndentityBetweenFloatAndFix)71 TEST(RmsLevelTest, VerifyIndentityBetweenFloatAndFix) {
72   auto x_f = CreateFloatSinusoid(1000, INT16_MAX, kSampleRateHz);
73   auto x_i = CreateFloatSinusoid(1000, INT16_MAX, kSampleRateHz);
74   auto level_f = RunTest(x_f);
75   auto level_i = RunTest(x_i);
76   int avg_i = level_i->Average();
77   int avg_f = level_f->Average();
78   EXPECT_EQ(3, avg_i);  // -3 dBFS
79   EXPECT_EQ(avg_f, avg_i);
80 }
81 
TEST(RmsLevelTest,Run1000HzFullScale)82 TEST(RmsLevelTest, Run1000HzFullScale) {
83   auto x = CreateInt16Sinusoid(1000, INT16_MAX, kSampleRateHz);
84   auto level = RunTest(x);
85   EXPECT_EQ(3, level->Average());  // -3 dBFS
86 }
87 
TEST(RmsLevelTest,Run1000HzFullScaleAverageAndPeak)88 TEST(RmsLevelTest, Run1000HzFullScaleAverageAndPeak) {
89   auto x = CreateInt16Sinusoid(1000, INT16_MAX, kSampleRateHz);
90   auto level = RunTest(x);
91   auto stats = level->AverageAndPeak();
92   EXPECT_EQ(3, stats.average);  // -3 dBFS
93   EXPECT_EQ(3, stats.peak);
94 }
95 
TEST(RmsLevelTest,Run1000HzHalfScale)96 TEST(RmsLevelTest, Run1000HzHalfScale) {
97   auto x = CreateInt16Sinusoid(1000, INT16_MAX / 2, kSampleRateHz);
98   auto level = RunTest(x);
99   EXPECT_EQ(9, level->Average());  // -9 dBFS
100 }
101 
TEST(RmsLevelTest,RunZeros)102 TEST(RmsLevelTest, RunZeros) {
103   std::vector<int16_t> x(kSampleRateHz, 0);  // 1 second of pure silence.
104   auto level = RunTest(x);
105   EXPECT_EQ(127, level->Average());
106 }
107 
TEST(RmsLevelTest,RunZerosAverageAndPeak)108 TEST(RmsLevelTest, RunZerosAverageAndPeak) {
109   std::vector<int16_t> x(kSampleRateHz, 0);  // 1 second of pure silence.
110   auto level = RunTest(x);
111   auto stats = level->AverageAndPeak();
112   EXPECT_EQ(127, stats.average);
113   EXPECT_EQ(127, stats.peak);
114 }
115 
TEST(RmsLevelTest,NoSamples)116 TEST(RmsLevelTest, NoSamples) {
117   RmsLevel level;
118   EXPECT_EQ(127, level.Average());  // Return minimum if no samples are given.
119 }
120 
TEST(RmsLevelTest,NoSamplesAverageAndPeak)121 TEST(RmsLevelTest, NoSamplesAverageAndPeak) {
122   RmsLevel level;
123   auto stats = level.AverageAndPeak();
124   EXPECT_EQ(127, stats.average);
125   EXPECT_EQ(127, stats.peak);
126 }
127 
TEST(RmsLevelTest,PollTwice)128 TEST(RmsLevelTest, PollTwice) {
129   auto x = CreateInt16Sinusoid(1000, INT16_MAX, kSampleRateHz);
130   auto level = RunTest(x);
131   level->Average();
132   EXPECT_EQ(127, level->Average());  // Stats should be reset at this point.
133 }
134 
TEST(RmsLevelTest,Reset)135 TEST(RmsLevelTest, Reset) {
136   auto x = CreateInt16Sinusoid(1000, INT16_MAX, kSampleRateHz);
137   auto level = RunTest(x);
138   level->Reset();
139   EXPECT_EQ(127, level->Average());  // Stats should be reset at this point.
140 }
141 
142 // Inserts 1 second of full-scale sinusoid, followed by 1 second of muted.
TEST(RmsLevelTest,ProcessMuted)143 TEST(RmsLevelTest, ProcessMuted) {
144   auto x = CreateInt16Sinusoid(1000, INT16_MAX, kSampleRateHz);
145   auto level = RunTest(x);
146   const size_t kBlocksPerSecond = rtc::CheckedDivExact(
147       static_cast<size_t>(kSampleRateHz), kBlockSizeSamples);
148   for (size_t i = 0; i < kBlocksPerSecond; ++i) {
149     level->AnalyzeMuted(kBlockSizeSamples);
150   }
151   EXPECT_EQ(6, level->Average());  // Average RMS halved due to the silence.
152 }
153 
154 // Inserts 1 second of half-scale sinusoid, follwed by 10 ms of full-scale, and
155 // finally 1 second of half-scale again. Expect the average to be -9 dBFS due
156 // to the vast majority of the signal being half-scale, and the peak to be
157 // -3 dBFS.
TEST(RmsLevelTest,RunHalfScaleAndInsertFullScale)158 TEST(RmsLevelTest, RunHalfScaleAndInsertFullScale) {
159   auto half_scale = CreateInt16Sinusoid(1000, INT16_MAX / 2, kSampleRateHz);
160   auto full_scale = CreateInt16Sinusoid(1000, INT16_MAX, kSampleRateHz / 100);
161   auto x = half_scale;
162   x.insert(x.end(), full_scale.begin(), full_scale.end());
163   x.insert(x.end(), half_scale.begin(), half_scale.end());
164   ASSERT_EQ(static_cast<size_t>(2 * kSampleRateHz + kSampleRateHz / 100),
165             x.size());
166   auto level = RunTest(x);
167   auto stats = level->AverageAndPeak();
168   EXPECT_EQ(9, stats.average);
169   EXPECT_EQ(3, stats.peak);
170 }
171 
TEST(RmsLevelTest,ResetOnBlockSizeChange)172 TEST(RmsLevelTest, ResetOnBlockSizeChange) {
173   auto x = CreateInt16Sinusoid(1000, INT16_MAX, kSampleRateHz);
174   auto level = RunTest(x);
175   // Create a new signal with half amplitude, but double block length.
176   auto y = CreateInt16Sinusoid(1000, INT16_MAX / 2, kBlockSizeSamples * 2);
177   level->Analyze(y);
178   auto stats = level->AverageAndPeak();
179   // Expect all stats to only be influenced by the last signal (y), since the
180   // changed block size should reset the stats.
181   EXPECT_EQ(9, stats.average);
182   EXPECT_EQ(9, stats.peak);
183 }
184 
185 }  // namespace webrtc
186