| 1 |
/* PitchTier_to_PointProcess.cpp |
| 2 |
* |
| 3 |
* Copyright (C) 1992-2005,2011,2012,2015-2017,2019,2023,2024 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 "PitchTier_to_PointProcess.h" |
| 20 |
#include "Pitch_to_PitchTier.h" |
| 21 |
|
| 22 |
autoPointProcess PitchTier_to_PointProcess (PitchTier me) { |
| 23 |
try { |
| 24 |
autoPointProcess thee = PointProcess_create (my xmin, my xmax, 1000); |
| 25 |
double area = 0.5; // imagine an event half a period before the beginning |
| 26 |
integer size = my points.size; |
| 27 |
if (size == 0) |
| 28 |
return thee; |
| 29 |
for (integer interval = 0; interval <= size; interval ++) { |
| 30 |
const double t1 = ( interval == 0 ? my xmin : my points.at [interval] -> number ); |
| 31 |
Melder_require (isdefined (t1), |
| 32 |
U"Time of point ", interval, U" not defined."); |
| 33 |
const double t2 = ( interval == size ? my xmax : my points.at [interval + 1] -> number ); |
| 34 |
Melder_require (isdefined (t2), |
| 35 |
U"Time of point ", interval + 1, U" not defined."); |
| 36 |
const double f1 = my points.at [interval == 0 ? 1 : interval] -> value; |
| 37 |
Melder_require (isdefined (f1), |
| 38 |
U"Pitch of point ", interval == 0 ? 1 : interval, U" not defined."); |
| 39 |
const double f2 = my points.at [interval == size ? size : interval + 1] -> value; |
| 40 |
Melder_require (isdefined (f2), |
| 41 |
U"Pitch of point ", interval == size ? size : interval + 1, U" not defined."); |
| 42 |
area += (t2 - t1) * 0.5 * (f1 + f2); |
| 43 |
while (area >= 1.0) { |
| 44 |
const double slope = (f2 - f1) / (t2 - t1); |
| 45 |
area -= 1.0; |
| 46 |
const double discriminant = f2 * f2 - 2.0 * area * slope; |
| 47 |
PointProcess_addPoint (thee.get(), t2 - 2.0 * area / (f2 + NUMsqrt_0 (discriminant))); |
| 48 |
} |
| 49 |
} |
| 50 |
return thee; |
| 51 |
} catch (MelderError) { |
| 52 |
Melder_throw (me, U": not converted to PointProcess."); |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
autoPointProcess PitchTier_Pitch_to_PointProcess (PitchTier me, Pitch vuv) { |
| 57 |
try { |
| 58 |
autoPointProcess fullPoint = PitchTier_to_PointProcess (me); |
| 59 |
autoPointProcess thee = PointProcess_create (my xmin, my xmax, fullPoint -> nt); |
| 60 |
/* |
| 61 |
Copy only voiced parts to result. |
| 62 |
*/ |
| 63 |
for (integer i = 1; i <= fullPoint -> nt; i ++) { |
| 64 |
const double t = fullPoint -> t [i]; |
| 65 |
if (Pitch_isVoiced_t (vuv, t)) |
| 66 |
PointProcess_addPoint (thee.get(), t); |
| 67 |
} |
| 68 |
return thee; |
| 69 |
} catch (MelderError) { |
| 70 |
Melder_throw (me, U" & ", vuv, U": not converted to PointProcess."); |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
static bool PointProcess_isVoiced_t (PointProcess me, const double t, const double maximumPeriod) { |
| 75 |
const integer imid = PointProcess_getNearestIndex (me, t); |
| 76 |
if (imid == 0) |
| 77 |
return false; |
| 78 |
const double tmid = my t [imid]; |
| 79 |
const bool leftVoiced = ( imid > 1 && tmid - my t [imid - 1] <= maximumPeriod ); |
| 80 |
const bool rightVoiced = ( imid < my nt && my t [imid + 1] - tmid <= maximumPeriod ); |
| 81 |
if (leftVoiced && t <= tmid || rightVoiced && t >= tmid) |
| 82 |
return true; |
| 83 |
if (leftVoiced && t < 1.5 * tmid - 0.5 * my t [imid - 1]) |
| 84 |
return true; |
| 85 |
if (rightVoiced && t > 1.5 * tmid - 0.5 * my t [imid + 1]) |
| 86 |
return true; |
| 87 |
return false; |
| 88 |
} |
| 89 |
|
| 90 |
autoPointProcess PitchTier_Point_to_PointProcess (PitchTier me, PointProcess vuv, const double maximumPeriod) { |
| 91 |
try { |
| 92 |
autoPointProcess fullPoint = PitchTier_to_PointProcess (me); |
| 93 |
autoPointProcess thee = PointProcess_create (my xmin, my xmax, fullPoint -> nt); |
| 94 |
/* |
| 95 |
Copy only voiced parts to result. |
| 96 |
*/ |
| 97 |
for (integer i = 1; i <= fullPoint -> nt; i ++) { |
| 98 |
const double t = fullPoint -> t [i]; |
| 99 |
if (PointProcess_isVoiced_t (vuv, t, maximumPeriod)) |
| 100 |
PointProcess_addPoint (thee.get(), t); |
| 101 |
} |
| 102 |
return thee; |
| 103 |
} catch (MelderError) { |
| 104 |
Melder_throw (me, U" & ", vuv, U": not converted to PointProcess."); |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
autoPitchTier PointProcess_to_PitchTier (PointProcess me, const double maximumPeriod) { |
| 109 |
try { |
| 110 |
autoPitchTier thee = PitchTier_create (my xmin, my xmax); |
| 111 |
for (integer i = 1; i < my nt; i ++) { |
| 112 |
const double interval = my t [i + 1] - my t [i]; |
| 113 |
if (interval <= maximumPeriod) |
| 114 |
RealTier_addPoint (thee.get(), my t [i] + 0.5 * interval, 1.0 / interval); |
| 115 |
} |
| 116 |
return thee; |
| 117 |
} catch (MelderError) { |
| 118 |
Melder_throw (me, U": not converted to PitchTier."); |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
autoPitchTier Pitch_PointProcess_to_PitchTier (Pitch me, PointProcess pp) { |
| 123 |
try { |
| 124 |
autoPitchTier temp = Pitch_to_PitchTier (me); |
| 125 |
autoPitchTier thee = PitchTier_PointProcess_to_PitchTier (temp.get(), pp); |
| 126 |
return thee; |
| 127 |
} catch (MelderError) { |
| 128 |
Melder_throw (me, U" & ", pp, U": not converted to PitchTier."); |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
autoPitchTier PitchTier_PointProcess_to_PitchTier (PitchTier me, PointProcess pp) { |
| 133 |
try { |
| 134 |
Melder_require (my points.size > 0, |
| 135 |
U"No pitch points."); |
| 136 |
autoPitchTier thee = PitchTier_create (pp -> xmin, pp -> xmax); |
| 137 |
for (integer i = 1; i <= pp -> nt; i ++) { |
| 138 |
const double time = pp -> t [i]; |
| 139 |
const double value = RealTier_getValueAtTime (me, time); |
| 140 |
RealTier_addPoint (thee.get(), time, value); |
| 141 |
} |
| 142 |
return thee; |
| 143 |
} catch (MelderError) { |
| 144 |
Melder_throw (me, U" & ", pp, U": not converted to PitchTier."); |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
autoTableOfReal PitchTier_downto_TableOfReal (const constPitchTier me, const bool useSemitones) { |
| 149 |
try { |
| 150 |
autoTableOfReal thee = RealTier_downto_TableOfReal (me, U"Time", U"F0"); |
| 151 |
if (useSemitones) |
| 152 |
for (integer i = 1; i <= thy numberOfRows; i ++) |
| 153 |
thy data [i] [2] = NUMhertzToSemitones (thy data [i] [2]); |
| 154 |
return thee; |
| 155 |
} catch (MelderError) { |
| 156 |
Melder_throw (me, U": not converted to TableOfReal."); |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
/* End of file PitchTier_to_PointProcess.cpp */ |