-
Notifications
You must be signed in to change notification settings - Fork 18
/
pitch_smoother.c
34 lines (34 loc) · 908 Bytes
/
pitch_smoother.c
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
#include "pitch_smoother.h"
float SmoothPitch(PitchSmoother* s, float semitones) {
float lastpitch=s->lastpitch;
if(lastpitch==0 || *s->p_pitchsmooth==0) {
s->lastpitch=semitones;
return semitones;
}
float diff=semitones-lastpitch;
float divisor=(*s->p_pitchsmooth)*s->periods_per_second;
float maxdiff=0.04;
if(divisor>1 && fabs(diff)>maxdiff) {
float toadd=diff/divisor;
if(fabs(s->momentum)<fabs(toadd)){
s->momentum=toadd;
s->lastpitch+=toadd;
} else if(fabs(s->momentum)>fabs(diff)) {
s->momentum=0;
s->lastpitch=semitones;
} else {
s->lastpitch+=s->momentum;
}
}else {
s->momentum=0;
s->lastpitch=semitones;
}
return s->lastpitch;
}
void ResetPitchSmoother(PitchSmoother* s) {
s->momentum=0;
s->lastpitch=0;
}
void InitializePitchSmoother(PitchSmoother* s, unsigned long N, int noverlap, float fs) {
s->periods_per_second=(float)(fs*noverlap)/(float)(N);
}