1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
|
/*
* Copyright (c) 2014 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#include "modules/audio_processing/transient/transient_suppressor.h"
#include <optional>
#include <vector>
#include "modules/audio_processing/transient/common.h"
#include "modules/audio_processing/transient/transient_suppressor_impl.h"
#include "test/gtest.h"
namespace webrtc {
namespace {
constexpr int kMono = 1;
// Returns the index of the first non-zero sample in `samples` or an unspecified
// value if no value is zero.
std::optional<int> FindFirstNonZeroSample(const std::vector<float>& samples) {
for (size_t i = 0; i < samples.size(); ++i) {
if (samples[i] != 0.0f) {
return i;
}
}
return std::nullopt;
}
} // namespace
class TransientSuppressorVadModeParametrization
: public ::testing::TestWithParam<TransientSuppressor::VadMode> {};
TEST_P(TransientSuppressorVadModeParametrization,
TypingDetectionLogicWorksAsExpectedForMono) {
TransientSuppressorImpl ts(GetParam(), ts::kSampleRate16kHz,
ts::kSampleRate16kHz, kMono);
// Each key-press enables detection.
EXPECT_FALSE(ts.detection_enabled_);
ts.UpdateKeypress(true);
EXPECT_TRUE(ts.detection_enabled_);
// It takes four seconds without any key-press to disable the detection
for (int time_ms = 0; time_ms < 3990; time_ms += ts::kChunkSizeMs) {
ts.UpdateKeypress(false);
EXPECT_TRUE(ts.detection_enabled_);
}
ts.UpdateKeypress(false);
EXPECT_FALSE(ts.detection_enabled_);
// Key-presses that are more than a second apart from each other don't enable
// suppression.
for (int i = 0; i < 100; ++i) {
EXPECT_FALSE(ts.suppression_enabled_);
ts.UpdateKeypress(true);
EXPECT_TRUE(ts.detection_enabled_);
EXPECT_FALSE(ts.suppression_enabled_);
for (int time_ms = 0; time_ms < 990; time_ms += ts::kChunkSizeMs) {
ts.UpdateKeypress(false);
EXPECT_TRUE(ts.detection_enabled_);
EXPECT_FALSE(ts.suppression_enabled_);
}
ts.UpdateKeypress(false);
}
// Two consecutive key-presses is enough to enable the suppression.
ts.UpdateKeypress(true);
EXPECT_FALSE(ts.suppression_enabled_);
ts.UpdateKeypress(true);
EXPECT_TRUE(ts.suppression_enabled_);
// Key-presses that are less than a second apart from each other don't disable
// detection nor suppression.
for (int i = 0; i < 100; ++i) {
for (int time_ms = 0; time_ms < 1000; time_ms += ts::kChunkSizeMs) {
ts.UpdateKeypress(false);
EXPECT_TRUE(ts.detection_enabled_);
EXPECT_TRUE(ts.suppression_enabled_);
}
ts.UpdateKeypress(true);
EXPECT_TRUE(ts.detection_enabled_);
EXPECT_TRUE(ts.suppression_enabled_);
}
// It takes four seconds without any key-press to disable the detection and
// suppression.
for (int time_ms = 0; time_ms < 3990; time_ms += ts::kChunkSizeMs) {
ts.UpdateKeypress(false);
EXPECT_TRUE(ts.detection_enabled_);
EXPECT_TRUE(ts.suppression_enabled_);
}
for (int time_ms = 0; time_ms < 1000; time_ms += ts::kChunkSizeMs) {
ts.UpdateKeypress(false);
EXPECT_FALSE(ts.detection_enabled_);
EXPECT_FALSE(ts.suppression_enabled_);
}
}
INSTANTIATE_TEST_SUITE_P(
TransientSuppressorImplTest,
TransientSuppressorVadModeParametrization,
::testing::Values(TransientSuppressor::VadMode::kDefault,
TransientSuppressor::VadMode::kRnnVad,
TransientSuppressor::VadMode::kNoVad));
class TransientSuppressorSampleRateParametrization
: public ::testing::TestWithParam<int> {};
// Checks that voice probability and processed audio data are temporally aligned
// after `Suppress()` is called.
TEST_P(TransientSuppressorSampleRateParametrization,
CheckAudioAndVoiceProbabilityTemporallyAligned) {
const int sample_rate_hz = GetParam();
TransientSuppressorImpl ts(TransientSuppressor::VadMode::kDefault,
sample_rate_hz,
/*detection_rate_hz=*/sample_rate_hz, kMono);
const int frame_size = sample_rate_hz * ts::kChunkSizeMs / 1000;
std::vector<float> frame(frame_size);
constexpr int kMaxAttempts = 3;
for (int i = 0; i < kMaxAttempts; ++i) {
SCOPED_TRACE(i);
// Call `Suppress()` on frames of non-zero audio samples.
std::fill(frame.begin(), frame.end(), 1000.0f);
float delayed_voice_probability = ts.Suppress(
frame.data(), frame.size(), kMono, /*detection_data=*/nullptr,
/*detection_length=*/frame_size, /*reference_data=*/nullptr,
/*reference_length=*/frame_size, /*voice_probability=*/1.0f,
/*key_pressed=*/false);
// Detect the algorithmic delay of `TransientSuppressorImpl`.
std::optional<int> frame_delay = FindFirstNonZeroSample(frame);
// Check that the delayed voice probability is delayed according to the
// measured delay.
if (frame_delay.has_value()) {
if (*frame_delay == 0) {
// When the delay is a multiple integer of the frame duration,
// `Suppress()` returns a copy of a previously observed voice
// probability value.
EXPECT_EQ(delayed_voice_probability, 1.0f);
} else {
// Instead, when the delay is fractional, `Suppress()` returns an
// interpolated value. Since the exact value depends on the
// interpolation method, we only check that the delayed voice
// probability is not zero as it must converge towards the previoulsy
// observed value.
EXPECT_GT(delayed_voice_probability, 0.0f);
}
break;
} else {
// The algorithmic delay is longer than the duration of a single frame.
// Until the delay is detected, the delayed voice probability is zero.
EXPECT_EQ(delayed_voice_probability, 0.0f);
}
}
}
INSTANTIATE_TEST_SUITE_P(TransientSuppressorImplTest,
TransientSuppressorSampleRateParametrization,
::testing::Values(ts::kSampleRate8kHz,
ts::kSampleRate16kHz,
ts::kSampleRate32kHz,
ts::kSampleRate48kHz));
} // namespace webrtc
|