peah | bdbceef | 2016-03-20 16:53:32 | [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/test/audio_buffer_tools.h" |
| 15 | #include "modules/audio_processing/test/bitexactness_tools.h" |
Sam Zackrisson | 0824c6f | 2019-10-07 12:03:56 | [diff] [blame] | 16 | #include "modules/audio_processing/voice_detection.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 17 | #include "test/gtest.h" |
peah | bdbceef | 2016-03-20 16:53:32 | [diff] [blame] | 18 | |
| 19 | namespace webrtc { |
| 20 | namespace { |
| 21 | |
| 22 | const int kNumFramesToProcess = 1000; |
| 23 | |
| 24 | // Process one frame of data and produce the output. |
Sam Zackrisson | 0824c6f | 2019-10-07 12:03:56 | [diff] [blame] | 25 | bool ProcessOneFrame(int sample_rate_hz, |
peah | bdbceef | 2016-03-20 16:53:32 | [diff] [blame] | 26 | AudioBuffer* audio_buffer, |
Sam Zackrisson | 0824c6f | 2019-10-07 12:03:56 | [diff] [blame] | 27 | VoiceDetection* voice_detection) { |
peah | bdbceef | 2016-03-20 16:53:32 | [diff] [blame] | 28 | if (sample_rate_hz > AudioProcessing::kSampleRate16kHz) { |
| 29 | audio_buffer->SplitIntoFrequencyBands(); |
| 30 | } |
| 31 | |
Sam Zackrisson | 0824c6f | 2019-10-07 12:03:56 | [diff] [blame] | 32 | return voice_detection->ProcessCaptureAudio(audio_buffer); |
peah | bdbceef | 2016-03-20 16:53:32 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | // Processes a specified amount of frames, verifies the results and reports |
| 36 | // any errors. |
| 37 | void RunBitexactnessTest(int sample_rate_hz, |
| 38 | size_t num_channels, |
Sam Zackrisson | 0824c6f | 2019-10-07 12:03:56 | [diff] [blame] | 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); |
peah | bdbceef | 2016-03-20 16:53:32 | [diff] [blame] | 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( |
Per Ã…hgren | d47941e | 2019-08-22 09:51:13 | [diff] [blame] | 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()); |
peah | bdbceef | 2016-03-20 16:53:32 | [diff] [blame] | 50 | test::InputAudioFile capture_file( |
| 51 | test::GetApmCaptureTestVectorFileName(sample_rate_hz)); |
| 52 | std::vector<float> capture_input(samples_per_channel * num_channels); |
Sam Zackrisson | 0824c6f | 2019-10-07 12:03:56 | [diff] [blame] | 53 | bool stream_has_voice = false; |
peah | bdbceef | 2016-03-20 16:53:32 | [diff] [blame] | 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 | |
Sam Zackrisson | 0824c6f | 2019-10-07 12:03:56 | [diff] [blame] | 61 | stream_has_voice = |
| 62 | ProcessOneFrame(sample_rate_hz, &capture_buffer, &voice_detection); |
peah | bdbceef | 2016-03-20 16:53:32 | [diff] [blame] | 63 | } |
| 64 | |
peah | bdbceef | 2016-03-20 16:53:32 | [diff] [blame] | 65 | EXPECT_EQ(stream_has_voice_reference, stream_has_voice); |
peah | bdbceef | 2016-03-20 16:53:32 | [diff] [blame] | 66 | } |
| 67 | |
peah | bdbceef | 2016-03-20 16:53:32 | [diff] [blame] | 68 | const bool kStreamHasVoiceReference = true; |
peah | bdbceef | 2016-03-20 16:53:32 | [diff] [blame] | 69 | |
| 70 | } // namespace |
| 71 | |
| 72 | TEST(VoiceDetectionBitExactnessTest, Mono8kHz) { |
Sam Zackrisson | 0824c6f | 2019-10-07 12:03:56 | [diff] [blame] | 73 | RunBitexactnessTest(8000, 1, kStreamHasVoiceReference); |
peah | bdbceef | 2016-03-20 16:53:32 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | TEST(VoiceDetectionBitExactnessTest, Mono16kHz) { |
Sam Zackrisson | 0824c6f | 2019-10-07 12:03:56 | [diff] [blame] | 77 | RunBitexactnessTest(16000, 1, kStreamHasVoiceReference); |
peah | bdbceef | 2016-03-20 16:53:32 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | TEST(VoiceDetectionBitExactnessTest, Mono32kHz) { |
Sam Zackrisson | 0824c6f | 2019-10-07 12:03:56 | [diff] [blame] | 81 | RunBitexactnessTest(32000, 1, kStreamHasVoiceReference); |
peah | bdbceef | 2016-03-20 16:53:32 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | TEST(VoiceDetectionBitExactnessTest, Mono48kHz) { |
Sam Zackrisson | 0824c6f | 2019-10-07 12:03:56 | [diff] [blame] | 85 | RunBitexactnessTest(48000, 1, kStreamHasVoiceReference); |
peah | bdbceef | 2016-03-20 16:53:32 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | TEST(VoiceDetectionBitExactnessTest, Stereo8kHz) { |
Sam Zackrisson | 0824c6f | 2019-10-07 12:03:56 | [diff] [blame] | 89 | RunBitexactnessTest(8000, 2, kStreamHasVoiceReference); |
peah | bdbceef | 2016-03-20 16:53:32 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | TEST(VoiceDetectionBitExactnessTest, Stereo16kHz) { |
Sam Zackrisson | 0824c6f | 2019-10-07 12:03:56 | [diff] [blame] | 93 | RunBitexactnessTest(16000, 2, kStreamHasVoiceReference); |
peah | bdbceef | 2016-03-20 16:53:32 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | TEST(VoiceDetectionBitExactnessTest, Stereo32kHz) { |
Sam Zackrisson | 0824c6f | 2019-10-07 12:03:56 | [diff] [blame] | 97 | RunBitexactnessTest(32000, 2, kStreamHasVoiceReference); |
peah | bdbceef | 2016-03-20 16:53:32 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | TEST(VoiceDetectionBitExactnessTest, Stereo48kHz) { |
Sam Zackrisson | 0824c6f | 2019-10-07 12:03:56 | [diff] [blame] | 101 | RunBitexactnessTest(48000, 2, kStreamHasVoiceReference); |
peah | bdbceef | 2016-03-20 16:53:32 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | } // namespace webrtc |