1 /* Sound_to_Harmonicity.cpp
2  *
3  * Copyright (C) 1992-2011,2015,2016,2017,2019 Paul Boersma
4  *
5  * This code is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or (at
8  * your option) any later version.
9  *
10  * This code is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13  * See the GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this work. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #include "Sound_to_Pitch.h"
20 #include "Sound_to_Harmonicity.h"
21 
Sound_to_Harmonicity_ac(Sound me,double dt,double minimumPitch,double silenceThreshold,double periodsPerWindow)22 autoHarmonicity Sound_to_Harmonicity_ac (Sound me, double dt, double minimumPitch,
23 	double silenceThreshold, double periodsPerWindow)
24 {
25 	try {
26 		autoPitch pitch = Sound_to_Pitch_any (me, dt, minimumPitch, periodsPerWindow, 15, 1,
27 				silenceThreshold, 0.0, 0.0, 0.0, 0.0, 0.5 / my dx);
28 		autoHarmonicity thee = Harmonicity_create (my xmin, my xmax, pitch -> nx,
29 				pitch -> dx, pitch -> x1);
30 		for (integer i = 1; i <= thy nx; i ++) {
31 			if (pitch -> frames [i]. candidates [1]. frequency == 0.0) {
32 				thy z [1] [i] = -200.0;
33 			} else {
34 				const double r = pitch -> frames [i]. candidates [1]. strength;
35 				thy z [1] [i] = ( r <= 1e-15 ? -150.0 : r > 1.0 - 1e-15 ? 150.0 : 10.0 * log10 (r / (1.0 - r)) );
36 			}
37 		}
38 		return thee;
39 	} catch (MelderError) {
40 		Melder_throw (me, U": harmonicity analysis (ac) not performed.");
41 	}
42 }
43 
Sound_to_Harmonicity_cc(Sound me,double dt,double minimumPitch,double silenceThreshold,double periodsPerWindow)44 autoHarmonicity Sound_to_Harmonicity_cc (Sound me, double dt, double minimumPitch,
45 	double silenceThreshold, double periodsPerWindow)
46 {
47 	try {
48 		autoPitch pitch = Sound_to_Pitch_any (me, dt, minimumPitch, periodsPerWindow, 15, 3,
49 				silenceThreshold, 0.0, 0.0, 0.0, 0.0, 0.5 / my dx);
50 		autoHarmonicity thee = Harmonicity_create (my xmin, my xmax, pitch -> nx,
51 				pitch -> dx, pitch -> x1);
52 		for (integer i = 1; i <= thy nx; i ++) {
53 			if (pitch -> frames [i]. candidates [1]. frequency == 0.0) {
54 				thy z [1] [i] = -200.0;
55 			} else {
56 				double r = pitch -> frames [i]. candidates [1]. strength;
57 				thy z [1] [i] = ( r <= 1e-15 ? -150.0 : r > 1.0 - 1e-15 ? 150.0 : 10.0 * log10 (r / (1.0 - r)) );
58 			}
59 		}
60 		return thee;
61 	} catch (MelderError) {
62 		Melder_throw (me, U": harmonicity analysis (cc) not performed.");
63 	}
64 }
65 
66 /* End of file Sound_to_Harmonicity.cpp */
67