-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
50 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
#pragma once | ||
|
||
#include <voyx/Header.h> | ||
|
||
template<typename T> | ||
class CepstralPitchDetector | ||
{ | ||
|
||
public: | ||
|
||
CepstralPitchDetector(const std::pair<voyx_t, voyx_t> roi, const voyx_t samplerate) : | ||
roi(roi), | ||
samplerate(samplerate) | ||
{ | ||
} | ||
|
||
voyx_t operator()(const voyx::vector<T> cepstrum) const | ||
{ | ||
const size_t nmin = size_t(0); | ||
const size_t nmax = cepstrum.size() / 2; | ||
|
||
const size_t qmin = static_cast<size_t>(samplerate / roi.first); | ||
const size_t qmax = static_cast<size_t>(samplerate / roi.second); | ||
|
||
const size_t imin = std::clamp(std::min(qmin, qmax), nmin, nmax); | ||
const size_t imax = std::clamp(std::max(qmin, qmax), nmin, nmax); | ||
|
||
voyxassert(imin < imax); | ||
|
||
T value = std::numeric_limits<T>::lowest(); | ||
ptrdiff_t index = -1; | ||
|
||
for (size_t i = imin; i < imax; ++i) | ||
{ | ||
if (cepstrum[i] > value) | ||
{ | ||
value = cepstrum[i]; | ||
index = i; | ||
} | ||
} | ||
|
||
return index ? samplerate / index : 0; | ||
} | ||
|
||
private: | ||
|
||
const std::pair<voyx_t, voyx_t> roi; | ||
const voyx_t samplerate; | ||
|
||
}; |