1 /*
2 * Copyright (c) 2018 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
11 #include "modules/audio_processing/agc2/rnn_vad/pitch_search.h"
12
13 #include <array>
14 #include <cstddef>
15
16 #include "rtc_base/checks.h"
17
18 namespace webrtc {
19 namespace rnn_vad {
20
PitchEstimator()21 PitchEstimator::PitchEstimator()
22 : pitch_buf_decimated_(kBufSize12kHz),
23 pitch_buf_decimated_view_(pitch_buf_decimated_.data(), kBufSize12kHz),
24 auto_corr_(kNumInvertedLags12kHz),
25 auto_corr_view_(auto_corr_.data(), kNumInvertedLags12kHz) {
26 RTC_DCHECK_EQ(kBufSize12kHz, pitch_buf_decimated_.size());
27 RTC_DCHECK_EQ(kNumInvertedLags12kHz, auto_corr_view_.size());
28 }
29
30 PitchEstimator::~PitchEstimator() = default;
31
Estimate(rtc::ArrayView<const float,kBufSize24kHz> pitch_buf)32 PitchInfo PitchEstimator::Estimate(
33 rtc::ArrayView<const float, kBufSize24kHz> pitch_buf) {
34 // Perform the initial pitch search at 12 kHz.
35 Decimate2x(pitch_buf, pitch_buf_decimated_view_);
36 auto_corr_calculator_.ComputeOnPitchBuffer(pitch_buf_decimated_view_,
37 auto_corr_view_);
38 std::array<size_t, 2> pitch_candidates_inv_lags = FindBestPitchPeriods(
39 auto_corr_view_, pitch_buf_decimated_view_, kMaxPitch12kHz);
40 // Refine the pitch period estimation.
41 // The refinement is done using the pitch buffer that contains 24 kHz samples.
42 // Therefore, adapt the inverted lags in |pitch_candidates_inv_lags| from 12
43 // to 24 kHz.
44 pitch_candidates_inv_lags[0] *= 2;
45 pitch_candidates_inv_lags[1] *= 2;
46 size_t pitch_inv_lag_48kHz =
47 RefinePitchPeriod48kHz(pitch_buf, pitch_candidates_inv_lags);
48 // Look for stronger harmonics to find the final pitch period and its gain.
49 RTC_DCHECK_LT(pitch_inv_lag_48kHz, kMaxPitch48kHz);
50 last_pitch_48kHz_ = CheckLowerPitchPeriodsAndComputePitchGain(
51 pitch_buf, kMaxPitch48kHz - pitch_inv_lag_48kHz, last_pitch_48kHz_);
52 return last_pitch_48kHz_;
53 }
54
55 } // namespace rnn_vad
56 } // namespace webrtc
57