| 1 |
/* Pitch_to_PitchTier.cpp |
| 2 |
* |
| 3 |
* Copyright (C) 1992-2005,2007,2009-2012,2014-2020 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 "Pitch_to_PitchTier.h" |
| 20 |
|
| 21 |
autoPitchTier Pitch_to_PitchTier (Pitch me) { |
| 22 |
try { |
| 23 |
autoPitchTier you = PitchTier_create (my xmin, my xmax); |
| 24 |
for (integer i = 1; i <= my nx; i ++) { |
| 25 |
const double frequency = my frames [i]. candidates [1]. frequency; |
| 26 |
/* |
| 27 |
Count only voiced frames. |
| 28 |
*/ |
| 29 |
if (Pitch_util_frequencyIsVoiced (frequency, my ceiling)) { |
| 30 |
const double time = Sampled_indexToX (me, i); |
| 31 |
RealTier_addPoint (you.get(), time, frequency); |
| 32 |
} |
| 33 |
} |
| 34 |
return you; |
| 35 |
} catch (MelderError) { |
| 36 |
Melder_throw (me, U": not converted to PitchTier."); |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
static void Pitch_line (Pitch me, Graphics graphics, double tmin, double fleft, double tmax, double fright, |
| 41 |
int nonPeriodicLineType) |
| 42 |
{ |
| 43 |
/* |
| 44 |
f = fleft + (t - tmin) * (fright - fleft) / (tmax - tmin); |
| 45 |
*/ |
| 46 |
const int lineType = Graphics_inqLineType (graphics); |
| 47 |
const double lineWidth = Graphics_inqLineWidth (graphics); |
| 48 |
const double slope = (fright - fleft) / (tmax - tmin); |
| 49 |
const integer imin = Melder_clippedLeft (1_integer, Sampled_xToNearestIndex (me, tmin)); |
| 50 |
const integer imax = Melder_clippedRight (Sampled_xToNearestIndex (me, tmax), my nx); |
| 51 |
for (integer i = imin; i <= imax; i ++) { |
| 52 |
if (! Pitch_isVoiced_i (me, i)) { |
| 53 |
if (nonPeriodicLineType == 2) |
| 54 |
continue; |
| 55 |
Graphics_setLineType (graphics, Graphics_DOTTED); |
| 56 |
Graphics_setLineWidth (graphics, 0.67 * lineWidth); |
| 57 |
} else if (nonPeriodicLineType != 2) { |
| 58 |
Graphics_setLineWidth (graphics, 2 * lineWidth); |
| 59 |
} |
| 60 |
double tleft = Sampled_indexToX (me, i) - 0.5 * my dx; |
| 61 |
double tright = tleft + my dx; |
| 62 |
Melder_clipLeft (tmin, & tleft); // has to be ordered after the previous line! |
| 63 |
Melder_clipRight (& tright, tmax); |
| 64 |
Graphics_line (graphics, |
| 65 |
tleft, fleft + (tleft - tmin) * slope, |
| 66 |
tright, fleft + (tright - tmin) * slope |
| 67 |
); |
| 68 |
Graphics_setLineType (graphics, lineType); |
| 69 |
Graphics_setLineWidth (graphics, lineWidth); |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
void PitchTier_Pitch_draw (PitchTier me, Pitch uv, Graphics graphics, |
| 74 |
double tmin, double tmax, double fmin, double fmax, int nonPeriodicLineType, bool garnish, conststring32 method) |
| 75 |
{ |
| 76 |
if (nonPeriodicLineType == 0) { |
| 77 |
PitchTier_draw (me, graphics, tmin, tmax, fmin, fmax, garnish, method); |
| 78 |
return; |
| 79 |
} |
| 80 |
Function_unidirectionalAutowindow (me, & tmin, & tmax); |
| 81 |
Graphics_setWindow (graphics, tmin, tmax, fmin, fmax); |
| 82 |
Graphics_setInner (graphics); |
| 83 |
const integer imin = AnyTier_timeToHighIndex (me->asAnyTier(), tmin); |
| 84 |
const integer imax = AnyTier_timeToLowIndex (me->asAnyTier(), tmax); |
| 85 |
if (my points.size == 0) { |
| 86 |
} else if (imax < imin) { |
| 87 |
const double fleft = RealTier_getValueAtTime (me, tmin); |
| 88 |
const double fright = RealTier_getValueAtTime (me, tmax); |
| 89 |
Pitch_line (uv, graphics, tmin, fleft, tmax, fright, nonPeriodicLineType); |
| 90 |
} else for (integer i = imin; i <= imax; i ++) { |
| 91 |
const RealPoint point = my points.at [i]; |
| 92 |
const double time = point -> number, frequency = point -> value; |
| 93 |
Graphics_speckle (graphics, time, frequency); |
| 94 |
if (i == 1) |
| 95 |
Pitch_line (uv, graphics, tmin, frequency, time, frequency, nonPeriodicLineType); |
| 96 |
else if (i == imin) |
| 97 |
Pitch_line (uv, graphics, time, frequency, tmin, RealTier_getValueAtTime (me, tmin), nonPeriodicLineType); |
| 98 |
if (i == my points.size) |
| 99 |
Pitch_line (uv, graphics, time, frequency, tmax, frequency, nonPeriodicLineType); |
| 100 |
else if (i == imax) |
| 101 |
Pitch_line (uv, graphics, time, frequency, tmax, RealTier_getValueAtTime (me, tmax), nonPeriodicLineType); |
| 102 |
else { |
| 103 |
RealPoint pointRight = my points.at [i + 1]; |
| 104 |
Pitch_line (uv, graphics, time, frequency, pointRight -> number, pointRight -> value, nonPeriodicLineType); |
| 105 |
} |
| 106 |
} |
| 107 |
Graphics_unsetInner (graphics); |
| 108 |
if (garnish) { |
| 109 |
Graphics_drawInnerBox (graphics); |
| 110 |
Graphics_textBottom (graphics, true, U"Time (s)"); |
| 111 |
Graphics_marksBottom (graphics, 2, true, true, false); |
| 112 |
Graphics_marksLeft (graphics, 2, true, true, false); |
| 113 |
Graphics_textLeft (graphics, true, U"Frequency (Hz)"); |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
autoPitch Pitch_PitchTier_to_Pitch (Pitch me, PitchTier tier) { |
| 118 |
try { |
| 119 |
if (tier -> points.size == 0) |
| 120 |
Melder_throw (U"No pitch points."); |
| 121 |
autoPitch you = Data_copy (me); |
| 122 |
for (integer iframe = 1; iframe <= my nx; iframe ++) { |
| 123 |
const Pitch_Frame frame = & your frames [iframe]; |
| 124 |
const Pitch_Candidate candidate = & frame -> candidates [1]; |
| 125 |
if (Pitch_util_frequencyIsVoiced (candidate -> frequency, my ceiling)) |
| 126 |
candidate -> frequency = RealTier_getValueAtTime (tier, Sampled_indexToX (me, iframe)); |
| 127 |
candidate -> strength = 0.9; |
| 128 |
frame -> candidates. resize (frame -> nCandidates = 1); |
| 129 |
} |
| 130 |
return you; |
| 131 |
} catch (MelderError) { |
| 132 |
Melder_throw (me, U" & ", tier, U": not converted to Pitch."); |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
/* End of file Pitch_to_PitchTier.cpp */ |