1 /* IntensityTier.cpp
2  *
3  * Copyright (C) 1992-2005,2007,2008,2010-2012,2015-2018,2020,2021 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 "IntensityTier.h"
20 
21 Thing_implement (IntensityTier, RealTier, 0);
22 
IntensityTier_create(double tmin,double tmax)23 autoIntensityTier IntensityTier_create (double tmin, double tmax) {
24 	try {
25 		autoIntensityTier me = Thing_new (IntensityTier);
26 		RealTier_init (me.get(), tmin, tmax);
27 		return me;
28 	} catch (MelderError) {
29 		Melder_throw (U"IntensityTier not created.");
30 	}
31 }
32 
IntensityTier_draw(IntensityTier me,Graphics g,double tmin,double tmax,double ymin,double ymax,conststring32 method,bool garnish)33 void IntensityTier_draw (IntensityTier me, Graphics g, double tmin, double tmax,
34 	double ymin, double ymax, conststring32 method, bool garnish)
35 {
36 	RealTier_draw (me, g, tmin, tmax, ymin, ymax, garnish, method, U"Intensity (dB)");
37 }
38 
PointProcess_upto_IntensityTier(PointProcess me,double intensity)39 autoIntensityTier PointProcess_upto_IntensityTier (PointProcess me, double intensity) {
40 	try {
41 		autoIntensityTier thee = PointProcess_upto_RealTier (me, intensity, classIntensityTier).static_cast_move<structIntensityTier>();
42 		return thee;
43 	} catch (MelderError) {
44 		Melder_throw (me, U": not converted to IntensityTier.");
45 	}
46 }
47 
Intensity_downto_IntensityTier(Intensity me)48 autoIntensityTier Intensity_downto_IntensityTier (Intensity me) {
49 	try {
50 		autoIntensityTier thee = Vector_to_RealTier (me, 1, classIntensityTier).static_cast_move<structIntensityTier>();
51 		return thee;
52 	} catch (MelderError) {
53 		Melder_throw (me, U": not converted to IntensityTier.");
54 	}
55 }
56 
Intensity_to_IntensityTier_peaks(Intensity me)57 autoIntensityTier Intensity_to_IntensityTier_peaks (Intensity me) {
58 	try {
59 		autoIntensityTier thee = Vector_to_RealTier_peaks (me, 1, classIntensityTier).static_cast_move<structIntensityTier>();
60 		return thee;
61 	} catch (MelderError) {
62 		Melder_throw (me, U": peaks not converted to IntensityTier.");
63 	}
64 }
65 
Intensity_to_IntensityTier_valleys(Intensity me)66 autoIntensityTier Intensity_to_IntensityTier_valleys (Intensity me) {
67 	try {
68 		autoIntensityTier thee = Vector_to_RealTier_valleys (me, 1, classIntensityTier).static_cast_move<structIntensityTier>();
69 		return thee;
70 	} catch (MelderError) {
71 		Melder_throw (me, U": valleys not converted to IntensityTier.");
72 	}
73 }
74 
Intensity_PointProcess_to_IntensityTier(Intensity me,PointProcess pp)75 autoIntensityTier Intensity_PointProcess_to_IntensityTier (Intensity me, PointProcess pp) {
76 	try {
77 		autoIntensityTier temp = Intensity_downto_IntensityTier (me);
78 		autoIntensityTier thee = IntensityTier_PointProcess_to_IntensityTier (temp.get(), pp);
79 		return thee;
80 	} catch (MelderError) {
81 		Melder_throw (me, U" & ", pp, U": not converted to IntensityTier.");
82 	}
83 }
84 
IntensityTier_PointProcess_to_IntensityTier(IntensityTier me,PointProcess pp)85 autoIntensityTier IntensityTier_PointProcess_to_IntensityTier (IntensityTier me, PointProcess pp) {
86 	try {
87 		if (my points.size == 0)
88 			Melder_throw (U"No intensity points.");
89 		autoIntensityTier thee = IntensityTier_create (pp -> xmin, pp -> xmax);
90 		RealTier_PointProcess_into_RealTier (me, pp, thee.get());
91 		return thee;
92 	} catch (MelderError) {
93 		Melder_throw (me, U" & ", pp, U": not converted to IntensityTier.");
94 	}
95 }
96 
IntensityTier_downto_TableOfReal(IntensityTier me)97 autoTableOfReal IntensityTier_downto_TableOfReal (IntensityTier me) {
98 	return RealTier_downto_TableOfReal (me, U"Time (s)", U"Intensity (dB)");
99 }
100 
Sound_IntensityTier_multiply_inplace(Sound me,IntensityTier intensity)101 void Sound_IntensityTier_multiply_inplace (Sound me, IntensityTier intensity) {
102 	if (intensity -> points.size == 0) return;
103 	for (integer isamp = 1; isamp <= my nx; isamp ++) {
104 		double t = my x1 + (isamp - 1) * my dx;
105 		double factor = pow (10, RealTier_getValueAtTime (intensity, t) / 20);
106 		for (integer channel = 1; channel <= my ny; channel ++) {
107 			my z [channel] [isamp] *= factor;
108 		}
109 	}
110 }
111 
Sound_IntensityTier_multiply(Sound me,IntensityTier intensity,int scale)112 autoSound Sound_IntensityTier_multiply (Sound me, IntensityTier intensity, int scale) {
113 	try {
114 		autoSound thee = Data_copy (me);
115 		Sound_IntensityTier_multiply_inplace (thee.get(), intensity);
116 		if (scale) Vector_scale (thee.get(), 0.9);
117 		return thee;
118 	} catch (MelderError) {
119 		Melder_throw (me, U": not multiplied with ", intensity, U".");
120 	}
121 }
122 
RealTier_to_IntensityTier(RealTier me)123 autoIntensityTier RealTier_to_IntensityTier (RealTier me) {
124 	try {
125 		autoIntensityTier thee = Thing_new (IntensityTier);
126 		my structRealTier :: v_copy (thee.get());
127 		return thee;
128 	} catch (MelderError) {
129 		Melder_throw (me, U": not converted to IntensityTier.");
130 	}
131 }
132 
133 /* End of file IntensityTier.cpp */
134