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/test/audio_buffer_tools.h"
15 #include "modules/audio_processing/test/bitexactness_tools.h"
16 #include "modules/audio_processing/voice_detection.h"
17 #include "test/gtest.h"
18 
19 namespace webrtc {
20 namespace {
21 
22 const int kNumFramesToProcess = 1000;
23 
24 // Process one frame of data and produce the output.
ProcessOneFrame(int sample_rate_hz,AudioBuffer * audio_buffer,VoiceDetection * voice_detection)25 bool ProcessOneFrame(int sample_rate_hz,
26                      AudioBuffer* audio_buffer,
27                      VoiceDetection* voice_detection) {
28   if (sample_rate_hz > AudioProcessing::kSampleRate16kHz) {
29     audio_buffer->SplitIntoFrequencyBands();
30   }
31 
32   return voice_detection->ProcessCaptureAudio(audio_buffer);
33 }
34 
35 // Processes a specified amount of frames, verifies the results and reports
36 // any errors.
RunBitexactnessTest(int sample_rate_hz,size_t num_channels,bool stream_has_voice_reference)37 void RunBitexactnessTest(int sample_rate_hz,
38                          size_t num_channels,
39                          bool stream_has_voice_reference) {
40   int sample_rate_to_use = std::min(sample_rate_hz, 16000);
41   VoiceDetection voice_detection(sample_rate_to_use,
42                                  VoiceDetection::kLowLikelihood);
43 
44   int samples_per_channel = rtc::CheckedDivExact(sample_rate_hz, 100);
45   const StreamConfig capture_config(sample_rate_hz, num_channels, false);
46   AudioBuffer capture_buffer(
47       capture_config.sample_rate_hz(), capture_config.num_channels(),
48       capture_config.sample_rate_hz(), capture_config.num_channels(),
49       capture_config.sample_rate_hz(), capture_config.num_channels());
50   test::InputAudioFile capture_file(
51       test::GetApmCaptureTestVectorFileName(sample_rate_hz));
52   std::vector<float> capture_input(samples_per_channel * num_channels);
53   bool stream_has_voice = false;
54   for (int frame_no = 0; frame_no < kNumFramesToProcess; ++frame_no) {
55     ReadFloatSamplesFromStereoFile(samples_per_channel, num_channels,
56                                    &capture_file, capture_input);
57 
58     test::CopyVectorToAudioBuffer(capture_config, capture_input,
59                                   &capture_buffer);
60 
61     stream_has_voice =
62         ProcessOneFrame(sample_rate_hz, &capture_buffer, &voice_detection);
63   }
64 
65   EXPECT_EQ(stream_has_voice_reference, stream_has_voice);
66 }
67 
68 const bool kStreamHasVoiceReference = true;
69 
70 }  // namespace
71 
TEST(VoiceDetectionBitExactnessTest,Mono8kHz)72 TEST(VoiceDetectionBitExactnessTest, Mono8kHz) {
73   RunBitexactnessTest(8000, 1, kStreamHasVoiceReference);
74 }
75 
TEST(VoiceDetectionBitExactnessTest,Mono16kHz)76 TEST(VoiceDetectionBitExactnessTest, Mono16kHz) {
77   RunBitexactnessTest(16000, 1, kStreamHasVoiceReference);
78 }
79 
TEST(VoiceDetectionBitExactnessTest,Mono32kHz)80 TEST(VoiceDetectionBitExactnessTest, Mono32kHz) {
81   RunBitexactnessTest(32000, 1, kStreamHasVoiceReference);
82 }
83 
TEST(VoiceDetectionBitExactnessTest,Mono48kHz)84 TEST(VoiceDetectionBitExactnessTest, Mono48kHz) {
85   RunBitexactnessTest(48000, 1, kStreamHasVoiceReference);
86 }
87 
TEST(VoiceDetectionBitExactnessTest,Stereo8kHz)88 TEST(VoiceDetectionBitExactnessTest, Stereo8kHz) {
89   RunBitexactnessTest(8000, 2, kStreamHasVoiceReference);
90 }
91 
TEST(VoiceDetectionBitExactnessTest,Stereo16kHz)92 TEST(VoiceDetectionBitExactnessTest, Stereo16kHz) {
93   RunBitexactnessTest(16000, 2, kStreamHasVoiceReference);
94 }
95 
TEST(VoiceDetectionBitExactnessTest,Stereo32kHz)96 TEST(VoiceDetectionBitExactnessTest, Stereo32kHz) {
97   RunBitexactnessTest(32000, 2, kStreamHasVoiceReference);
98 }
99 
TEST(VoiceDetectionBitExactnessTest,Stereo48kHz)100 TEST(VoiceDetectionBitExactnessTest, Stereo48kHz) {
101   RunBitexactnessTest(48000, 2, kStreamHasVoiceReference);
102 }
103 
104 }  // namespace webrtc
105