File: pitch_search.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (70 lines) | stat: -rw-r--r-- 2,950 bytes parent folder | download | duplicates (2)
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
/*
 *  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/pitch_search.h"

#include <array>
#include <cstddef>

#include "rtc_base/checks.h"

namespace webrtc {
namespace rnn_vad {

PitchEstimator::PitchEstimator(const AvailableCpuFeatures& cpu_features)
    : cpu_features_(cpu_features),
      y_energy_24kHz_(kRefineNumLags24kHz, 0.f),
      pitch_buffer_12kHz_(kBufSize12kHz),
      auto_correlation_12kHz_(kNumLags12kHz) {}

PitchEstimator::~PitchEstimator() = default;

int PitchEstimator::Estimate(
    ArrayView<const float, kBufSize24kHz> pitch_buffer) {
  ArrayView<float, kBufSize12kHz> pitch_buffer_12kHz_view(
      pitch_buffer_12kHz_.data(), kBufSize12kHz);
  RTC_DCHECK_EQ(pitch_buffer_12kHz_.size(), pitch_buffer_12kHz_view.size());
  ArrayView<float, kNumLags12kHz> auto_correlation_12kHz_view(
      auto_correlation_12kHz_.data(), kNumLags12kHz);
  RTC_DCHECK_EQ(auto_correlation_12kHz_.size(),
                auto_correlation_12kHz_view.size());

  // TODO(bugs.chromium.org/10480): Use `cpu_features_` to estimate pitch.
  // Perform the initial pitch search at 12 kHz.
  Decimate2x(pitch_buffer, pitch_buffer_12kHz_view);
  auto_corr_calculator_.ComputeOnPitchBuffer(pitch_buffer_12kHz_view,
                                             auto_correlation_12kHz_view);
  CandidatePitchPeriods pitch_periods = ComputePitchPeriod12kHz(
      pitch_buffer_12kHz_view, auto_correlation_12kHz_view, cpu_features_);
  // The refinement is done using the pitch buffer that contains 24 kHz samples.
  // Therefore, adapt the inverted lags in `pitch_candidates_inv_lags` from 12
  // to 24 kHz.
  pitch_periods.best *= 2;
  pitch_periods.second_best *= 2;

  // Refine the initial pitch period estimation from 12 kHz to 48 kHz.
  // Pre-compute frame energies at 24 kHz.
  ArrayView<float, kRefineNumLags24kHz> y_energy_24kHz_view(
      y_energy_24kHz_.data(), kRefineNumLags24kHz);
  RTC_DCHECK_EQ(y_energy_24kHz_.size(), y_energy_24kHz_view.size());
  ComputeSlidingFrameSquareEnergies24kHz(pitch_buffer, y_energy_24kHz_view,
                                         cpu_features_);
  // Estimation at 48 kHz.
  const int pitch_lag_48kHz = ComputePitchPeriod48kHz(
      pitch_buffer, y_energy_24kHz_view, pitch_periods, cpu_features_);
  last_pitch_48kHz_ = ComputeExtendedPitchPeriod48kHz(
      pitch_buffer, y_energy_24kHz_view,
      /*initial_pitch_period_48kHz=*/kMaxPitch48kHz - pitch_lag_48kHz,
      last_pitch_48kHz_, cpu_features_);
  return last_pitch_48kHz_.period;
}

}  // namespace rnn_vad
}  // namespace webrtc