1 /* Matrix_and_Pitch.cpp
2  *
3  * Copyright (C) 1992-2005,2011,2012,2015-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 "Matrix_and_Pitch.h"
20 
21 autoMatrix Pitch_to_Matrix (Pitch me) {
22 	try {
23 		autoMatrix you = Matrix_create (my xmin, my xmax, my nx, my dx, my x1, 1.0, 1.0, 1, 1.0, 1.0);
24 		for (integer i = 1; i <= my nx; i ++) {
25 			const double value = my frames [i]. candidates [1]. frequency;
26 			your z [1] [i] =
27 				Pitch_util_frequencyIsVoiced (value, my ceiling) ? my frames [i]. candidates [1]. frequency : 0.0;
28 		}
29 		return you;
30 	} catch (MelderError) {
31 		Melder_throw (me, U": not converted to Matrix.");
32 	}
33 }
34 
35 autoPitch Matrix_to_Pitch (Matrix me) {
36 	try {
37 		autoPitch you = Pitch_create (my xmin, my xmax, my nx, my dx, my x1, 5000.0, 2);
38 		for (integer i = 1; i <= my nx; i ++) {
39 			const Pitch_Frame frame = & your frames [i];
40 			if (my z [1] [i] == 0.0) {
41 				Pitch_Frame_init (frame, 1);
42 				frame -> candidates [1]. frequency = 0.0;   // voiceless candidate always present
43 				frame -> candidates [1]. strength = 0.4;
44 			} else {
45 				Pitch_Frame_init (frame, 2);
46 				frame -> intensity = 1;
47 				frame -> candidates [1]. frequency = my z [1] [i];
48 				frame -> candidates [1]. strength = 0.9;
49 				frame -> candidates [2]. frequency = 0.0;   // voiceless candidate always present
50 				frame -> candidates [2]. strength = 0.4;
51 			}
52 		}
53 		return you;
54 	} catch (MelderError) {
55 		Melder_throw (me, U": not converted to Pitch.");
56 	}
57 }
58 
59 void Pitch_formula (Pitch me, conststring32 formula, Interpreter interpreter) {
60 	try {
61 		autoMatrix m = Matrix_create (my xmin, my xmax, my nx, my dx, my x1, 1.0, my maxnCandidates, my maxnCandidates, 1.0, 1.0);
62 		for (integer iframe = 1; iframe <= my nx; iframe ++) {
63 			const Pitch_Frame frame = & my frames [iframe];
64 			for (integer icand = 1; icand <= frame -> nCandidates; icand ++)
65 				m -> z [icand] [iframe] = frame -> candidates [icand]. frequency;
66 		}
67 		Matrix_formula (m.get(), formula, interpreter, nullptr);
68 		for (integer iframe = 1; iframe <= my nx; iframe ++) {
69 			const Pitch_Frame frame = & my frames [iframe];
70 			for (integer icand = 1; icand <= frame -> nCandidates; icand ++)
71 				frame -> candidates [icand]. frequency = m -> z [icand] [iframe];
72 		}
73 	} catch (MelderError) {
74 		Melder_throw (me, U": formula not completed.");
75 	}
76 }
77 
78 /* End of file Matrix_and_Pitch.cpp */
79