blob: 94b84bbdc3555bb1b144de9f5e8ae224fffba70b [file] [log] [blame]
peah19b7b662016-03-20 15:36:281/*
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 04:47:3112#include "api/array_view.h"
13#include "modules/audio_processing/audio_buffer.h"
14#include "modules/audio_processing/level_estimator_impl.h"
15#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 15:36:2818
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) {
29 rtc::CriticalSection crit_capture;
30 LevelEstimatorImpl level_estimator(&crit_capture);
31 level_estimator.Initialize();
32 level_estimator.Enable(true);
33
34 int samples_per_channel = rtc::CheckedDivExact(sample_rate_hz, 100);
35 StreamConfig capture_config(sample_rate_hz, num_channels, false);
36 AudioBuffer capture_buffer(
37 capture_config.num_frames(), capture_config.num_channels(),
38 capture_config.num_frames(), capture_config.num_channels(),
39 capture_config.num_frames());
40
41 test::InputAudioFile capture_file(
42 test::GetApmCaptureTestVectorFileName(sample_rate_hz));
43 std::vector<float> capture_input(samples_per_channel * num_channels);
44 for (size_t frame_no = 0; frame_no < kNumFramesToProcess; ++frame_no) {
45 ReadFloatSamplesFromStereoFile(samples_per_channel, num_channels,
46 &capture_file, capture_input);
47
48 test::CopyVectorToAudioBuffer(capture_config, capture_input,
49 &capture_buffer);
50
51 level_estimator.ProcessStream(&capture_buffer);
52 }
53
54 // Extract test results.
55 int rms = level_estimator.RMS();
56
57 // Compare the output to the reference.
58 EXPECT_EQ(rms_reference, rms);
59}
60
61} // namespace
62
63TEST(LevelEstimatorBitExactnessTest, Mono8kHz) {
64 const int kRmsReference = 31;
65
66 RunBitexactnessTest(8000, 1, kRmsReference);
67}
68
69TEST(LevelEstimatorBitExactnessTest, Mono16kHz) {
70 const int kRmsReference = 31;
71
72 RunBitexactnessTest(16000, 1, kRmsReference);
73}
74
75TEST(LevelEstimatorBitExactnessTest, Mono32kHz) {
76 const int kRmsReference = 31;
77
78 RunBitexactnessTest(32000, 1, kRmsReference);
79}
80
81TEST(LevelEstimatorBitExactnessTest, Mono48kHz) {
82 const int kRmsReference = 31;
83
84 RunBitexactnessTest(48000, 1, kRmsReference);
85}
86
87TEST(LevelEstimatorBitExactnessTest, Stereo16kHz) {
88 const int kRmsReference = 30;
89
90 RunBitexactnessTest(16000, 2, kRmsReference);
91}
92
93} // namespace webrtc