Alessio Bazzica c36f8623c0 Reland "RNN VAD: pitch search optimizations (part 1)"
This reverts commit 1b6b958a4aa574b7852fe62efe5d4f96ce085d8b.

Reason for revert: Bug fix

Original change's description:
> Revert "RNN VAD: pitch search optimizations (part 1)"
>
> This reverts commit 9da3e177fd5c2236cc15fea0ee8933e1dd0d8f6d.
>
> Reason for revert: bug in ComputePitchPeriod48kHz()
>
> Original change's description:
> > RNN VAD: pitch search optimizations (part 1)
> >
> > TL;DR this CL improves efficiency and includes several code
> > readability improvements mainly triggered by the comments to
> > patch set #10.
> >
> > Highlights:
> > - Split `FindBestPitchPeriods()` into 12 and 24 kHz versions
> >   to hard-code the input size and simplify the 24 kHz version
> > - Loop in `ComputePitchPeriod48kHz()` (new name for
> >   `RefinePitchPeriod48kHz()`) removed since the lags for which
> >   we need to compute the auto correlation are a few
> > - `ComputePitchGainThreshold()` was only used in unit tests; it's been
> >   moved into the anon ns and the test removed
> >
> > This CL makes `ComputePitchPeriod48kHz()` is about 10% faster (measured
> > with https://webrtc-review.googlesource.com/c/src/+/191320/4/modules/audio_processing/agc2/rnn_vad/pitch_search_internal_unittest.cc).
> > The realtime factor has improved by about +14%.
> >
> > Benchmarked as follows:
> > ```
> > out/release/modules_unittests \
> >   --gtest_filter=*RnnVadTest.DISABLED_RnnVadPerformance* \
> >   --gtest_also_run_disabled_tests --logs
> > ```
> >
> > Results:
> >
> >       | baseline             | this CL
> > ------+----------------------+------------------------
> > run 1 | 24.0231 +/- 0.591016 | 23.568 +/- 0.990788
> >       | 370.06x              | 377.207x
> > ------+----------------------+------------------------
> > run 2 | 24.0485 +/- 0.957498 | 23.3714 +/- 0.857523
> >       | 369.67x              | 380.379x
> > ------+----------------------+------------------------
> > run 2 | 25.4091 +/- 2.6123   | 23.709 +/- 1.04477
> >       | 349.875x             | 374.963x
> >
> > Bug: webrtc:10480
> > Change-Id: I9a3e9164b2442114b928de506c92a547c273882f
> > Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/191320
> > Reviewed-by: Per Åhgren <peah@webrtc.org>
> > Commit-Queue: Alessio Bazzica <alessiob@webrtc.org>
> > Cr-Commit-Position: refs/heads/master@{#32568}
>
> TBR=alessiob@webrtc.org,peah@webrtc.org
>
> No-Presubmit: true
> No-Tree-Checks: true
> No-Try: true
> Bug: webrtc:10480
> Change-Id: I2a91f4f29566f872a7dfa220b31c6c625ed075db
> Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/192660
> Commit-Queue: Alessio Bazzica <alessiob@webrtc.org>
> Reviewed-by: Alessio Bazzica <alessiob@webrtc.org>
> Cr-Commit-Position: refs/heads/master@{#32581}

TBR=alessiob@webrtc.org,peah@webrtc.org

# Not skipping CQ checks because this is a reland.

Bug: webrtc:10480
Change-Id: I66e3e8d73ebc04a437c01a0396cd5613c42a8cf5
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/192780
Reviewed-by: Alessio Bazzica <alessiob@webrtc.org>
Reviewed-by: Per Åhgren <peah@webrtc.org>
Commit-Queue: Alessio Bazzica <alessiob@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#32585}
2020-11-11 12:43:45 +00:00

90 lines
3.5 KiB
C++

/*
* Copyright (c) 2018 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/agc2/rnn_vad/features_extraction.h"
#include <array>
#include "modules/audio_processing/agc2/rnn_vad/lp_residual.h"
#include "rtc_base/checks.h"
namespace webrtc {
namespace rnn_vad {
namespace {
// Generated via "B, A = scipy.signal.butter(2, 30/12000, btype='highpass')"
const BiQuadFilter::BiQuadCoefficients kHpfConfig24k = {
{0.99446179f, -1.98892358f, 0.99446179f},
{-1.98889291f, 0.98895425f}};
} // namespace
FeaturesExtractor::FeaturesExtractor()
: use_high_pass_filter_(false),
pitch_buf_24kHz_(),
pitch_buf_24kHz_view_(pitch_buf_24kHz_.GetBufferView()),
lp_residual_(kBufSize24kHz),
lp_residual_view_(lp_residual_.data(), kBufSize24kHz),
pitch_estimator_(),
reference_frame_view_(pitch_buf_24kHz_.GetMostRecentValuesView()) {
RTC_DCHECK_EQ(kBufSize24kHz, lp_residual_.size());
hpf_.Initialize(kHpfConfig24k);
Reset();
}
FeaturesExtractor::~FeaturesExtractor() = default;
void FeaturesExtractor::Reset() {
pitch_buf_24kHz_.Reset();
spectral_features_extractor_.Reset();
if (use_high_pass_filter_)
hpf_.Reset();
}
bool FeaturesExtractor::CheckSilenceComputeFeatures(
rtc::ArrayView<const float, kFrameSize10ms24kHz> samples,
rtc::ArrayView<float, kFeatureVectorSize> feature_vector) {
// Pre-processing.
if (use_high_pass_filter_) {
std::array<float, kFrameSize10ms24kHz> samples_filtered;
hpf_.Process(samples, samples_filtered);
// Feed buffer with the pre-processed version of |samples|.
pitch_buf_24kHz_.Push(samples_filtered);
} else {
// Feed buffer with |samples|.
pitch_buf_24kHz_.Push(samples);
}
// Extract the LP residual.
float lpc_coeffs[kNumLpcCoefficients];
ComputeAndPostProcessLpcCoefficients(pitch_buf_24kHz_view_, lpc_coeffs);
ComputeLpResidual(lpc_coeffs, pitch_buf_24kHz_view_, lp_residual_view_);
// Estimate pitch on the LP-residual and write the normalized pitch period
// into the output vector (normalization based on training data stats).
pitch_period_48kHz_ = pitch_estimator_.Estimate(lp_residual_view_);
feature_vector[kFeatureVectorSize - 2] = 0.01f * (pitch_period_48kHz_ - 300);
// Extract lagged frames (according to the estimated pitch period).
RTC_DCHECK_LE(pitch_period_48kHz_ / 2, kMaxPitch24kHz);
auto lagged_frame = pitch_buf_24kHz_view_.subview(
kMaxPitch24kHz - pitch_period_48kHz_ / 2, kFrameSize20ms24kHz);
// Analyze reference and lagged frames checking if silence has been detected
// and write the feature vector.
return spectral_features_extractor_.CheckSilenceComputeFeatures(
reference_frame_view_, {lagged_frame.data(), kFrameSize20ms24kHz},
{feature_vector.data() + kNumLowerBands, kNumBands - kNumLowerBands},
{feature_vector.data(), kNumLowerBands},
{feature_vector.data() + kNumBands, kNumLowerBands},
{feature_vector.data() + kNumBands + kNumLowerBands, kNumLowerBands},
{feature_vector.data() + kNumBands + 2 * kNumLowerBands, kNumLowerBands},
&feature_vector[kFeatureVectorSize - 1]);
}
} // namespace rnn_vad
} // namespace webrtc