55 lines
1.4 KiB
C++
55 lines
1.4 KiB
C++
#include "audio/voice_activity_detector.h"
|
|
|
|
#include <array>
|
|
#include <cassert>
|
|
#include <iostream>
|
|
|
|
namespace {
|
|
|
|
using namespace borla;
|
|
|
|
void TestHysteresis()
|
|
{
|
|
VoiceActivityDetector detector(
|
|
VoiceActivityConfig{500, 2, 3});
|
|
std::array<int16_t, 960> loud{};
|
|
std::array<int16_t, 960> quiet{};
|
|
loud.fill(1000);
|
|
|
|
assert(detector.Process(loud.data(), loud.size()) ==
|
|
VoiceActivityResult::kSilence);
|
|
assert(detector.Process(loud.data(), loud.size()) ==
|
|
VoiceActivityResult::kSpeechStarted);
|
|
assert(detector.speech_active());
|
|
assert(detector.Process(quiet.data(), quiet.size()) ==
|
|
VoiceActivityResult::kSpeech);
|
|
assert(detector.Process(quiet.data(), quiet.size()) ==
|
|
VoiceActivityResult::kSpeech);
|
|
assert(detector.Process(quiet.data(), quiet.size()) ==
|
|
VoiceActivityResult::kSpeechEnded);
|
|
assert(!detector.speech_active());
|
|
}
|
|
|
|
void TestNoiseDoesNotStartSpeech()
|
|
{
|
|
VoiceActivityDetector detector(
|
|
VoiceActivityConfig{500, 2, 3});
|
|
std::array<int16_t, 960> noise{};
|
|
noise.fill(300);
|
|
for (int index = 0; index < 20; ++index) {
|
|
assert(detector.Process(noise.data(), noise.size()) ==
|
|
VoiceActivityResult::kSilence);
|
|
}
|
|
assert(detector.LastRms() == 300);
|
|
}
|
|
|
|
} // namespace
|
|
|
|
int main()
|
|
{
|
|
TestHysteresis();
|
|
TestNoiseDoesNotStartSpeech();
|
|
std::cout << "voice activity detector tests passed\n";
|
|
return 0;
|
|
}
|