| 1 |
/* TextGrid_and_DurationTier.cpp |
| 2 |
* |
| 3 |
* Copyright (C) 2017-2019 David Weenink, 2017,2028,2026 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 3 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. See the GNU |
| 13 |
* 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 "TextGrid_and_DurationTier.h" |
| 20 |
#include "Thing.h" |
| 21 |
|
| 22 |
void IntervalTier_DurationTier_scaleTimes (IntervalTier me, DurationTier thee) { |
| 23 |
Melder_require (my xmin == thy xmin && my xmax == thy xmax, |
| 24 |
U"The domains of the IntervalTier and the DurationTier should be equal."); |
| 25 |
const double xmax_new = my xmin + RealTier_getArea (thee, my xmin, my xmax); |
| 26 |
for (integer i = 1; i <= my intervals.size; i ++) { |
| 27 |
const TextInterval segment = my intervals.at [i]; |
| 28 |
const double xmin = RealTier_getArea (thee, my xmin, segment -> xmin); |
| 29 |
const double xmax = RealTier_getArea (thee, my xmin, segment -> xmax); |
| 30 |
segment -> xmin = my xmin + xmin; |
| 31 |
segment -> xmax = my xmin + xmax; |
| 32 |
} |
| 33 |
my xmax = xmax_new; |
| 34 |
} |
| 35 |
|
| 36 |
void TextTier_DurationTier_scaleTimes (TextTier me, DurationTier thee) { |
| 37 |
Melder_require (my xmin == thy xmin && my xmax == thy xmax, |
| 38 |
U"The domains of the TextTier and the DurationTier should be equal."); |
| 39 |
const double xmax_new = my xmin + RealTier_getArea (thee, my xmin, my xmax); |
| 40 |
for (integer ipoint = 1; ipoint <= my points.size; ipoint ++) { |
| 41 |
const TextPoint point = my points.at [ipoint]; |
| 42 |
const double time = RealTier_getArea (thee, my xmin, point -> number); |
| 43 |
point -> number = time; |
| 44 |
} |
| 45 |
my xmax = xmax_new; |
| 46 |
} |
| 47 |
|
| 48 |
autoTextGrid TextGrid_DurationTier_scaleTimes (TextGrid me, DurationTier thee) { |
| 49 |
try { |
| 50 |
Melder_require (my xmin == thy xmin && my xmax == thy xmax, |
| 51 |
U"The domains of the TextGrid and the DurationTier should be equal."); |
| 52 |
|
| 53 |
const double xmax_new = my xmin + RealTier_getArea (thee, my xmin, my xmax); |
| 54 |
autoTextGrid him = Data_copy (me); |
| 55 |
const integer numberOfTiers = my tiers->size; |
| 56 |
for (integer itier = 1; itier <= numberOfTiers; itier ++) { |
| 57 |
Function anyTier = his tiers->at [itier]; |
| 58 |
if (anyTier -> classInfo == classIntervalTier) { |
| 59 |
const IntervalTier tier = static_cast <IntervalTier> (anyTier); |
| 60 |
IntervalTier_DurationTier_scaleTimes (tier, thee); |
| 61 |
} else { |
| 62 |
const TextTier textTier = static_cast <TextTier> (anyTier); |
| 63 |
TextTier_DurationTier_scaleTimes (textTier, thee); |
| 64 |
} |
| 65 |
} |
| 66 |
his xmax = xmax_new; |
| 67 |
return him; |
| 68 |
|
| 69 |
} catch (MelderError) { |
| 70 |
Melder_throw (me, U": no time-scaled TextGrid created."); |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
autoDurationTier TextGrid_to_DurationTier (TextGrid me, integer tierNumber, |
| 75 |
double timeScalefactor, double leftTransitionDuration, double rightTransitionDuration, kMelder_string which, conststring32 criterion) |
| 76 |
{ |
| 77 |
try { |
| 78 |
autoDurationTier him = DurationTier_create (my xmin, my xmax); |
| 79 |
const IntervalTier tier = TextGrid_checkSpecifiedTierIsIntervalTier (me, tierNumber); |
| 80 |
for (integer i = 1; i <= tier ->intervals.size; i ++) { |
| 81 |
const TextInterval segment = tier -> intervals.at [i]; |
| 82 |
if (Melder_stringMatchesCriterion (segment -> text.get(), which, criterion, true)) { |
| 83 |
const double xmin = segment -> xmin, xmax = segment -> xmax; |
| 84 |
RealTier_addPoint (him.get(), xmin, 1.0); |
| 85 |
RealTier_addPoint (him.get(), xmin + leftTransitionDuration, timeScalefactor); |
| 86 |
RealTier_addPoint (him.get(), xmax - rightTransitionDuration, timeScalefactor); |
| 87 |
RealTier_addPoint (him.get(), xmax, 1.0); |
| 88 |
} |
| 89 |
} |
| 90 |
const integer index = tier ->intervals.size; |
| 91 |
if (index == 0) |
| 92 |
RealTier_addPoint (him.get(), my xmin, 1.0); |
| 93 |
return him; |
| 94 |
} catch (MelderError) { |
| 95 |
Melder_throw (me, U":cannot create DurationTier."); |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
/* End of file TextGrid_and_DurationTier.cpp */ |