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 #include <vector>
11 
12 #include "api/array_view.h"
13 #include "modules/audio_processing/audio_buffer.h"
14 #include "modules/audio_processing/level_estimator.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"
18 
19 namespace webrtc {
20 namespace {
21 
22 const int kNumFramesToProcess = 1000;
23 
24 // Processes a specified amount of frames, verifies the results and reports
25 // any errors.
RunBitexactnessTest(int sample_rate_hz,size_t num_channels,int rms_reference)26 void RunBitexactnessTest(int sample_rate_hz,
27                          size_t num_channels,
28                          int rms_reference) {
29   LevelEstimator level_estimator;
30   int samples_per_channel = rtc::CheckedDivExact(sample_rate_hz, 100);
31   StreamConfig capture_config(sample_rate_hz, num_channels, false);
32   AudioBuffer capture_buffer(
33       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());
36 
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 
47     level_estimator.ProcessStream(capture_buffer);
48   }
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 
TEST(LevelEstimatorBitExactnessTest,Mono8kHz)59 TEST(LevelEstimatorBitExactnessTest, Mono8kHz) {
60   const int kRmsReference = 31;
61 
62   RunBitexactnessTest(8000, 1, kRmsReference);
63 }
64 
TEST(LevelEstimatorBitExactnessTest,Mono16kHz)65 TEST(LevelEstimatorBitExactnessTest, Mono16kHz) {
66   const int kRmsReference = 31;
67 
68   RunBitexactnessTest(16000, 1, kRmsReference);
69 }
70 
TEST(LevelEstimatorBitExactnessTest,Mono32kHz)71 TEST(LevelEstimatorBitExactnessTest, Mono32kHz) {
72   const int kRmsReference = 31;
73 
74   RunBitexactnessTest(32000, 1, kRmsReference);
75 }
76 
TEST(LevelEstimatorBitExactnessTest,Mono48kHz)77 TEST(LevelEstimatorBitExactnessTest, Mono48kHz) {
78   const int kRmsReference = 31;
79 
80   RunBitexactnessTest(48000, 1, kRmsReference);
81 }
82 
TEST(LevelEstimatorBitExactnessTest,Stereo16kHz)83 TEST(LevelEstimatorBitExactnessTest, Stereo16kHz) {
84   const int kRmsReference = 30;
85 
86   RunBitexactnessTest(16000, 2, kRmsReference);
87 }
88 
89 }  // namespace webrtc
90