blob: 7660b677fd13e110fdb5127270ca4161fa5b5bd8 [file] [log] [blame]
peah19b7b662016-03-20 08:36:28 -07001/*
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 <vector>
11
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020012#include "api/array_view.h"
13#include "modules/audio_processing/audio_buffer.h"
saza6787f232019-10-11 19:31:07 +020014#include "modules/audio_processing/level_estimator.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020015#include "modules/audio_processing/test/audio_buffer_tools.h"
16#include "modules/audio_processing/test/bitexactness_tools.h"
17#include "test/gtest.h"
peah19b7b662016-03-20 08:36:28 -070018
19namespace webrtc {
20namespace {
21
22const int kNumFramesToProcess = 1000;
23
24// Processes a specified amount of frames, verifies the results and reports
25// any errors.
26void RunBitexactnessTest(int sample_rate_hz,
27 size_t num_channels,
28 int rms_reference) {
saza6787f232019-10-11 19:31:07 +020029 LevelEstimator level_estimator;
peah19b7b662016-03-20 08:36:28 -070030 int samples_per_channel = rtc::CheckedDivExact(sample_rate_hz, 100);
31 StreamConfig capture_config(sample_rate_hz, num_channels, false);
32 AudioBuffer capture_buffer(
Per Åhgrend47941e2019-08-22 11:51:13 +020033 capture_config.sample_rate_hz(), capture_config.num_channels(),
34 capture_config.sample_rate_hz(), capture_config.num_channels(),
35 capture_config.sample_rate_hz(), capture_config.num_channels());
peah19b7b662016-03-20 08:36:28 -070036
37 test::InputAudioFile capture_file(
38 test::GetApmCaptureTestVectorFileName(sample_rate_hz));
39 std::vector<float> capture_input(samples_per_channel * num_channels);
40 for (size_t frame_no = 0; frame_no < kNumFramesToProcess; ++frame_no) {
41 ReadFloatSamplesFromStereoFile(samples_per_channel, num_channels,
42 &capture_file, capture_input);
43
44 test::CopyVectorToAudioBuffer(capture_config, capture_input,
45 &capture_buffer);
46
Per Åhgrend47941e2019-08-22 11:51:13 +020047 level_estimator.ProcessStream(capture_buffer);
peah19b7b662016-03-20 08:36:28 -070048 }
49
50 // Extract test results.
51 int rms = level_estimator.RMS();
52
53 // Compare the output to the reference.
54 EXPECT_EQ(rms_reference, rms);
55}
56
57} // namespace
58
59TEST(LevelEstimatorBitExactnessTest, Mono8kHz) {
60 const int kRmsReference = 31;
61
62 RunBitexactnessTest(8000, 1, kRmsReference);
63}
64
65TEST(LevelEstimatorBitExactnessTest, Mono16kHz) {
66 const int kRmsReference = 31;
67
68 RunBitexactnessTest(16000, 1, kRmsReference);
69}
70
71TEST(LevelEstimatorBitExactnessTest, Mono32kHz) {
72 const int kRmsReference = 31;
73
74 RunBitexactnessTest(32000, 1, kRmsReference);
75}
76
77TEST(LevelEstimatorBitExactnessTest, Mono48kHz) {
78 const int kRmsReference = 31;
79
80 RunBitexactnessTest(48000, 1, kRmsReference);
81}
82
83TEST(LevelEstimatorBitExactnessTest, Stereo16kHz) {
84 const int kRmsReference = 30;
85
86 RunBitexactnessTest(16000, 2, kRmsReference);
87}
88
89} // namespace webrtc