21 memcpy(
signal_data_.get(), signal,
sizeof(
float) * size);
28 static constexpr float kDcDeltaErrorMultiplier = 0.015f;
30 int waves =
size_ / period - 1;
33 for (
int w = 0; w < waves; ++w) {
34 float total_from = 0.0f;
35 float total_to = 0.0f;
36 for (
int i = 0; i < points; ++i) {
37 float first_position = w * period + i * period / points;
38 float second_position = (w + 1) * period + i * period / points;
40 int first_index = first_position;
41 float first_t = first_position - first_index;
45 total_from += first_value;
47 int second_index = second_position;
48 float second_t = second_position - second_index;
52 total_to += second_value;
54 float delta = first_value - second_value;
55 error += delta * delta;
58 float total_diff = total_from - total_to;
59 error += total_diff * total_diff * kDcDeltaErrorMultiplier;
68 constexpr float kMinLength = 300.0f;
70 float max_length = std::min<float>(
size_ / 2.0f, max_period);
72 float best_error = INT_MAX;
73 float match = kMinLength;
75 for (
float length = kMinLength; length < max_length; length += 1.0f) {
77 if (error < best_error) {
83 float best_match = match;
84 for (
float length = match - 1.0f; length <= match + 1.0f; length += 0.1f) {
86 if (error < best_error) {
float findYinPeriod(int max_period)
Searches for a period using a YIN-like algorithm, up to a specified maximum period.
Definition pitch_detector.cpp:65
float matchPeriod(int max_period)
High-level method to find the best matching period using the YIN approach.
Definition pitch_detector.cpp:95
int size_
Number of samples in the loaded signal.
Definition pitch_detector.h:86
void loadSignal(const float *signal, int size)
Loads a signal into the PitchDetector for analysis.
Definition pitch_detector.cpp:17
static constexpr int kNumPoints
A fixed number of points used in the period error computation.
Definition pitch_detector.h:25
std::unique_ptr< float[]> signal_data_
Buffer holding the loaded signal samples.
Definition pitch_detector.h:87
float getPeriodError(float period)
Computes the error metric for a given period length.
Definition pitch_detector.cpp:24
PitchDetector()
Constructs a PitchDetector with no loaded signal.
Definition pitch_detector.cpp:12
#define VITAL_ASSERT(x)
Definition common.h:11
force_inline poly_float interpolate(poly_float from, poly_float to, mono_float t)
Performs a linear interpolation between two poly_floats using a scalar t in [0..1].
Definition poly_utils.h:182