blob: 9a52fa60a0546c11c0c7a8eeba757219ec9bb620 [file] [log] [blame]
peahbdbceef2016-03-20 16:53:321/*
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/test/audio_buffer_tools.h"
15#include "modules/audio_processing/test/bitexactness_tools.h"
Sam Zackrisson0824c6f2019-10-07 12:03:5616#include "modules/audio_processing/voice_detection.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3117#include "test/gtest.h"
peahbdbceef2016-03-20 16:53:3218
19namespace webrtc {
20namespace {
21
22const int kNumFramesToProcess = 1000;
23
24// Process one frame of data and produce the output.
Sam Zackrisson0824c6f2019-10-07 12:03:5625bool ProcessOneFrame(int sample_rate_hz,
peahbdbceef2016-03-20 16:53:3226 AudioBuffer* audio_buffer,
Sam Zackrisson0824c6f2019-10-07 12:03:5627 VoiceDetection* voice_detection) {
peahbdbceef2016-03-20 16:53:3228 if (sample_rate_hz > AudioProcessing::kSampleRate16kHz) {
29 audio_buffer->SplitIntoFrequencyBands();
30 }
31
Sam Zackrisson0824c6f2019-10-07 12:03:5632 return voice_detection->ProcessCaptureAudio(audio_buffer);
peahbdbceef2016-03-20 16:53:3233}
34
35// Processes a specified amount of frames, verifies the results and reports
36// any errors.
37void RunBitexactnessTest(int sample_rate_hz,
38 size_t num_channels,
Sam Zackrisson0824c6f2019-10-07 12:03:5639 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);
peahbdbceef2016-03-20 16:53:3243
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(
Per Ã…hgrend47941e2019-08-22 09:51:1347 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());
peahbdbceef2016-03-20 16:53:3250 test::InputAudioFile capture_file(
51 test::GetApmCaptureTestVectorFileName(sample_rate_hz));
52 std::vector<float> capture_input(samples_per_channel * num_channels);
Sam Zackrisson0824c6f2019-10-07 12:03:5653 bool stream_has_voice = false;
peahbdbceef2016-03-20 16:53:3254 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
Sam Zackrisson0824c6f2019-10-07 12:03:5661 stream_has_voice =
62 ProcessOneFrame(sample_rate_hz, &capture_buffer, &voice_detection);
peahbdbceef2016-03-20 16:53:3263 }
64
peahbdbceef2016-03-20 16:53:3265 EXPECT_EQ(stream_has_voice_reference, stream_has_voice);
peahbdbceef2016-03-20 16:53:3266}
67
peahbdbceef2016-03-20 16:53:3268const bool kStreamHasVoiceReference = true;
peahbdbceef2016-03-20 16:53:3269
70} // namespace
71
72TEST(VoiceDetectionBitExactnessTest, Mono8kHz) {
Sam Zackrisson0824c6f2019-10-07 12:03:5673 RunBitexactnessTest(8000, 1, kStreamHasVoiceReference);
peahbdbceef2016-03-20 16:53:3274}
75
76TEST(VoiceDetectionBitExactnessTest, Mono16kHz) {
Sam Zackrisson0824c6f2019-10-07 12:03:5677 RunBitexactnessTest(16000, 1, kStreamHasVoiceReference);
peahbdbceef2016-03-20 16:53:3278}
79
80TEST(VoiceDetectionBitExactnessTest, Mono32kHz) {
Sam Zackrisson0824c6f2019-10-07 12:03:5681 RunBitexactnessTest(32000, 1, kStreamHasVoiceReference);
peahbdbceef2016-03-20 16:53:3282}
83
84TEST(VoiceDetectionBitExactnessTest, Mono48kHz) {
Sam Zackrisson0824c6f2019-10-07 12:03:5685 RunBitexactnessTest(48000, 1, kStreamHasVoiceReference);
peahbdbceef2016-03-20 16:53:3286}
87
88TEST(VoiceDetectionBitExactnessTest, Stereo8kHz) {
Sam Zackrisson0824c6f2019-10-07 12:03:5689 RunBitexactnessTest(8000, 2, kStreamHasVoiceReference);
peahbdbceef2016-03-20 16:53:3290}
91
92TEST(VoiceDetectionBitExactnessTest, Stereo16kHz) {
Sam Zackrisson0824c6f2019-10-07 12:03:5693 RunBitexactnessTest(16000, 2, kStreamHasVoiceReference);
peahbdbceef2016-03-20 16:53:3294}
95
96TEST(VoiceDetectionBitExactnessTest, Stereo32kHz) {
Sam Zackrisson0824c6f2019-10-07 12:03:5697 RunBitexactnessTest(32000, 2, kStreamHasVoiceReference);
peahbdbceef2016-03-20 16:53:3298}
99
100TEST(VoiceDetectionBitExactnessTest, Stereo48kHz) {
Sam Zackrisson0824c6f2019-10-07 12:03:56101 RunBitexactnessTest(48000, 2, kStreamHasVoiceReference);
peahbdbceef2016-03-20 16:53:32102}
103
104} // namespace webrtc