blob: 7b15717ca0a5fd5f1b985359559e21aa2041d56c [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
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
20namespace webrtc {
21namespace {
22constexpr int kSampleRateHz = 48000;
23constexpr size_t kBlockSizeSamples = kSampleRateHz / 100;
24
25std::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
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);
50 EXPECT_EQ(3, level->RMS()); // -3 dBFS
51}
52
53TEST(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
59TEST(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
65TEST(RmsLevelTest, NoSamples) {
66 RMSLevel level;
67 EXPECT_EQ(127, level.RMS()); // Return minimum if no samples are given.
68}
69
70TEST(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
77TEST(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.
85TEST(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