1 /* Pitch_to_Sound.cpp
2  *
3  * Copyright (C) 1992-2011,2015,2016,2017 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 /*
20  * pb 2002/07/16 GPL
21  * pb 2005/02/09 Pitch_to_Sound_sine
22  * pb 2007/02/25 changed default sampling frequency to 44100 Hz
23  * pb 2008/01/19 double
24  * pb 2011/06/05 C++
25  */
26 
27 #include "Pitch_to_PointProcess.h"
28 #include "PointProcess_and_Sound.h"
29 #include "Pitch_to_Sound.h"
30 #include "PitchTier_to_Sound.h"
31 #include "Pitch_to_PitchTier.h"
32 
Pitch_to_Sound(Pitch me,double tmin,double tmax,bool hum)33 autoSound Pitch_to_Sound (Pitch me, double tmin, double tmax, bool hum) {
34 	static double formant [1 + 6] { 0.0, 600.0, 1400.0, 2400.0, 3400.0, 4500.0, 5500.0 };
35 	static double bandwidth [1 + 6] { 0.0, 50.0, 100.0, 200.0, 300.0, 400.0, 500.0 };
36 	try {
37 		autoPointProcess point = Pitch_to_PointProcess (me);
38 		autoSound sound = PointProcess_to_Sound_pulseTrain (point.get(), 44100.0, 0.7, 0.05, 30);
39 		if (hum) {
40 			Sound_filterWithFormants (sound.get(), tmin, tmax, 6, formant, bandwidth);
41 		}
42 		return sound;
43 	} catch (MelderError) {
44 		Melder_throw (me, U": not converted to Sound.");
45 	}
46 }
47 
Pitch_play(Pitch me,double tmin,double tmax)48 void Pitch_play (Pitch me, double tmin, double tmax) {
49 	try {
50 		Function_unidirectionalAutowindow (me, & tmin, & tmax);
51 		autoSound sound = Pitch_to_Sound (me, tmin, tmax, false);
52 		Sound_playPart (sound.get(), tmin, tmax, nullptr, nullptr);
53 	} catch (MelderError) {
54 		Melder_throw (me, U": not played.");
55 	}
56 }
57 
Pitch_hum(Pitch me,double tmin,double tmax)58 void Pitch_hum (Pitch me, double tmin, double tmax) {
59 	try {
60 		Function_unidirectionalAutowindow (me, & tmin, & tmax);
61 		autoSound sound = Pitch_to_Sound (me, tmin, tmax, true);
62 		Sound_playPart (sound.get(), tmin, tmax, nullptr, nullptr);
63 	} catch (MelderError) {
64 		Melder_throw (me, U": not played.");
65 	}
66 }
67 
Pitch_to_Sound_sine(Pitch me,double tmin,double tmax,double samplingFrequency,bool roundToNearestZeroCrossings)68 autoSound Pitch_to_Sound_sine (Pitch me, double tmin, double tmax, double samplingFrequency, bool roundToNearestZeroCrossings) {
69 	try {
70 		autoPitchTier tier = Pitch_to_PitchTier (me);
71 		autoSound sound = PitchTier_to_Sound_sine (tier.get(), tmin, tmax, samplingFrequency);
72 		integer iframe = 1;
73 		double unvoicedMin = my xmin;
74 		double unvoicedMax = my x1 + (iframe - 1.5) * my dx;
75 		for (;;) {
76 			while (! Pitch_isVoiced_i (me, iframe)) {
77 				unvoicedMax = my x1 + (iframe - 0.5) * my dx;
78 				if (++ iframe > my nx) break;
79 			}
80 			if (unvoicedMax > unvoicedMin) {
81 				Sound_setZero (sound.get(), unvoicedMin, unvoicedMax, roundToNearestZeroCrossings);
82 			}
83 			if (iframe > my nx) break;
84 			while (Pitch_isVoiced_i (me, iframe)) {
85 				unvoicedMin = my x1 + (iframe - 0.5) * my dx;
86 				if (++ iframe > my nx) break;
87 			}
88 			if (iframe > my nx) break;
89 		}
90 		unvoicedMax = my xmax;
91 		if (unvoicedMax > unvoicedMin) {
92 			Sound_setZero (sound.get(), unvoicedMin, unvoicedMax, roundToNearestZeroCrossings);
93 		}
94 		return sound;
95 	} catch (MelderError) {
96 		Melder_throw (me, U": not converted to Sound (sine).");
97 	}
98 }
99 
100 /* End of file Pitch_to_Sound.cpp */
101