peah | 19b7b66 | 2016-03-20 15:36:28 | [diff] [blame] | 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 12 | #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" |
peah | 19b7b66 | 2016-03-20 15:36:28 | [diff] [blame] | 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. |
| 26 | void 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 | |
| 63 | TEST(LevelEstimatorBitExactnessTest, Mono8kHz) { |
| 64 | const int kRmsReference = 31; |
| 65 | |
| 66 | RunBitexactnessTest(8000, 1, kRmsReference); |
| 67 | } |
| 68 | |
| 69 | TEST(LevelEstimatorBitExactnessTest, Mono16kHz) { |
| 70 | const int kRmsReference = 31; |
| 71 | |
| 72 | RunBitexactnessTest(16000, 1, kRmsReference); |
| 73 | } |
| 74 | |
| 75 | TEST(LevelEstimatorBitExactnessTest, Mono32kHz) { |
| 76 | const int kRmsReference = 31; |
| 77 | |
| 78 | RunBitexactnessTest(32000, 1, kRmsReference); |
| 79 | } |
| 80 | |
| 81 | TEST(LevelEstimatorBitExactnessTest, Mono48kHz) { |
| 82 | const int kRmsReference = 31; |
| 83 | |
| 84 | RunBitexactnessTest(48000, 1, kRmsReference); |
| 85 | } |
| 86 | |
| 87 | TEST(LevelEstimatorBitExactnessTest, Stereo16kHz) { |
| 88 | const int kRmsReference = 30; |
| 89 | |
| 90 | RunBitexactnessTest(16000, 2, kRmsReference); |
| 91 | } |
| 92 | |
| 93 | } // namespace webrtc |