-
Notifications
You must be signed in to change notification settings - Fork 24
/
pitch.c
88 lines (71 loc) · 2.04 KB
/
pitch.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
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
#include <stdlib.h>
#include <string.h>
#include <sndfile.h>
#include <math.h>
#include "file.h"
#include <xtract/libxtract.h>
extern void pitch_init(t_loop *loop, int samplerate) {
// loop->win_s = 1024;
// loop->hop_s = loop->win_s/4;
loop->win_s = loop->chunksz;
loop->hop_s = loop->chunksz;
loop->samplerate = samplerate;
loop->channels = 1;
xtract_init_fft(loop->chunksz, XTRACT_SPECTRUM);
loop->initialised = 1;
}
void pitch_destruct(t_loop *loop) {
}
extern float *pitch_calc(t_loop *loop) {
float rmsAmplitude = 0;
double pitch = -1;
static float result[3];
result[0] = -1;
result[1] = -1;
result[2] = -1;
if (loop->initialised == 0) {
return(NULL);
}
int j = (loop->now - loop->chunksz) % loop->frames;
for (int i = 0; i < loop->chunksz; i++){
rmsAmplitude += sqrt(loop->items[j]*loop->items[j]);
loop->in[i] = (double) loop->items[j];
++j;
if (j >= loop->frames) {
j = 0;
}
}
rmsAmplitude /= loop->chunksz;
if( rmsAmplitude > 0.005 ){
double flux = -1;
double centroid = -1;
double param[4];
double spectrum[loop->chunksz];
xtract[XTRACT_WAVELET_F0](loop->in, loop->chunksz, &loop->samplerate, &pitch);
param[0] = (double) loop->samplerate / (double)loop->chunksz;
param[1] = XTRACT_MAGNITUDE_SPECTRUM;
param[2] = 0.f;
param[3] = 0.f;
xtract[XTRACT_SPECTRUM](loop->in, loop->chunksz, ¶m[0], spectrum);
xtract_spectral_centroid(spectrum, loop->chunksz, NULL, ¢roid);
// order
param[0] = 1;
// type
param[1] = XTRACT_POSITIVE_SLOPE;
xtract[XTRACT_FLUX](loop->in, loop->chunksz, ¶m, &flux);
printf("pitches: %f %f %f %f\n", pitch, flux, centroid, rmsAmplitude);
result[0] = pitch;
result[1] = flux;
result[2] = centroid;
}
return(result);
}
/*
extern int *segment_get_onsets(t_sample *sample) {
int *result;
pitch_init(sample->info->channels);
result = pitch_process(sample, sample->items, sample->info->frames);
pitch_destruct();
return(result);
}
*/