-
Notifications
You must be signed in to change notification settings - Fork 67
/
pitch_detection.h
191 lines (162 loc) · 4.07 KB
/
pitch_detection.h
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#ifndef PITCH_DETECTION_H
#define PITCH_DETECTION_H
#include <complex>
#include <ffts/ffts.h>
#include <mlpack/core.hpp>
#include <mlpack/methods/hmm/hmm.hpp>
#include <stdexcept>
#include <vector>
/* ignore me plz */
namespace detail
{
template <typename T>
std::vector<size_t>
bin_pitches(const std::vector<std::pair<T, T>>);
mlpack::hmm::HMM<mlpack::distribution::DiscreteDistribution>
build_hmm();
void
init_pitch_bins();
} // namespace detail
/*
* The pitch namespace contains the functions:
*
* pitch::mpm(data, sample_rate)
* pitch::yin(data, sample_rate)
* pitch::pyin(data, sample_rate)
* pitch::pmpm(data, sample_rate)
*
* It will auto-allocate any buffers.
*/
namespace pitch
{
template <typename T>
T
yin(const std::vector<T> &, int);
template <typename T>
T
mpm(const std::vector<T> &, int);
/*
* pyin and pmpm emit pairs of pitch/probability
*/
template <typename T>
T
pyin(const std::vector<T> &, int);
template <typename T>
T
pmpm(const std::vector<T> &, int);
} // namespace pitch
/*
* This namespace is useful for repeated calls to pitch for the same size of
* buffer.
*
* It contains the classes Yin and Mpm which contain the allocated buffers
* and each implement a `pitch(data, sample_rate)` and
* `probablistic_pitch(data, sample_rate)` method.
*/
namespace pitch_alloc
{
enum FFTType { REAL_TO_COMPLEX, COMPLEX_TO_COMPLEX };
class BaseAlloc
{
public:
long nfft;
FFTType fft_type;
// ffts is better with floats, not doubles
std::vector<float> out_real;
std::vector<std::complex<float>> out_im;
ffts_plan_t *fft_forward;
ffts_plan_t *fft_backward;
mlpack::hmm::HMM<mlpack::distribution::DiscreteDistribution> hmm;
BaseAlloc(long audio_buffer_size)
: nfft(audio_buffer_size),
fft_type(
is_power_of_two(nfft) ? REAL_TO_COMPLEX : COMPLEX_TO_COMPLEX),
out_real(std::vector<float>(nfft)),
out_im((fft_type == REAL_TO_COMPLEX) ? (nfft / 2 + 1) : nfft)
{
if (fft_type == REAL_TO_COMPLEX) {
// For real-to-complex, output size is nfft/2 + 1
out_im.resize(nfft / 2 + 1);
fft_forward = ffts_init_1d_real(nfft, FFTS_FORWARD);
fft_backward = ffts_init_1d_real(nfft, FFTS_BACKWARD);
} else {
// For complex-to-complex, output size is nfft
out_im.resize(nfft);
fft_forward = ffts_init_1d(nfft, FFTS_FORWARD);
fft_backward = ffts_init_1d(nfft, FFTS_BACKWARD);
}
detail::init_pitch_bins();
hmm = detail::build_hmm();
clear();
}
~BaseAlloc()
{
ffts_free(fft_forward);
ffts_free(fft_backward);
}
protected:
void
clear()
{
std::fill(
out_im.begin(), out_im.end(), std::complex<float>{0.0f, 0.0f});
}
private:
// Utility function to check if a number is a power of two
static bool
is_power_of_two(long x)
{
return x && !(x & (x - 1));
}
};
/*
* Allocate the buffers for MPM for re-use.
* Intended for multiple consistently-sized audio buffers.
*
* Usage: pitch_alloc::Mpm ma(1024)
*/
template <typename T> class Mpm : public BaseAlloc
{
public:
Mpm(long audio_buffer_size) : BaseAlloc(audio_buffer_size){};
T
pitch(const std::vector<T> &, int);
T
probabilistic_pitch(const std::vector<T> &, int);
};
/*
* Allocate the buffers for YIN for re-use.
* Intended for multiple consistently-sized audio buffers.
*
* Usage: pitch_alloc::Yin ya(1024)
*/
template <typename T> class Yin : public BaseAlloc
{
public:
int yin_buffer_size;
std::vector<float> yin_buffer;
Yin(long audio_buffer_size)
: BaseAlloc(audio_buffer_size), yin_buffer_size(audio_buffer_size / 2),
yin_buffer(std::vector<float>(yin_buffer_size))
{
}
T
pitch(const std::vector<T> &, int);
T
probabilistic_pitch(const std::vector<T> &, int);
};
} // namespace pitch_alloc
namespace util
{
template <typename T>
std::pair<T, T> // the input is a float, output of FFTS
parabolic_interpolation(const std::vector<float> &, int);
template <typename T>
void
acorr_r(const std::vector<T> &, pitch_alloc::BaseAlloc *);
template <typename T>
T
pitch_from_hmm(mlpack::hmm::HMM<mlpack::distribution::DiscreteDistribution>,
const std::vector<std::pair<T, T>>);
} // namespace util
#endif /* PITCH_DETECTION_H */