| 1 | /* DTW_and_TextGrid.cpp |
| 2 | * |
| 3 | * Copyright (C) 1993-2019 David Weenink |
| 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. 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 | /* |
| 20 | djmw 20060906 |
| 21 | djmw 20070306: Reverse x and y. Reference should always be vertical! |
| 22 | djmw 20110304 Thing_new |
| 23 | */ |
| 24 | |
| 25 | #include "DTW_and_TextGrid.h" |
| 26 | |
| 27 | // begin old prototypes for compatibility reasons with the past |
| 28 | autoTextGrid DTW_TextGrid_to_TextGrid_old (DTW me, TextGrid thee); |
| 29 | autoIntervalTier DTW_IntervalTier_to_IntervalTier_old (DTW me, IntervalTier thee); |
| 30 | autoTextTier DTW_TextTier_to_TextTier_old (DTW me, TextTier thee); |
| 31 | // end old |
| 32 | |
| 33 | /* Get times from TextGrid and substitute new time form the y-times of the DTW. */ |
| 34 | autoTextTier DTW_TextTier_to_TextTier (DTW me, TextTier thee, double precision) { |
| 35 | try { |
| 36 | if (fabs (my ymin - thy xmin) <= precision && fabs (my ymax - thy xmax) <= precision) { // map from Y to X |
| 37 | autoTextTier him = Data_copy (thee); |
| 38 | his xmin = my xmin; |
| 39 | his xmax = my xmax; |
| 40 | for (integer i = 1; i <= his points.size; i ++) { |
| 41 | const TextPoint textpoint = his points.at [i]; |
| 42 | const double time = DTW_getXTimeFromYTime (me, textpoint -> number); |
| 43 | textpoint -> number = time; |
| 44 | } |
| 45 | return him; |
| 46 | } else if (fabs (my xmin - thy xmin) <= precision && fabs (my xmax - thy xmax) <= precision) { // map from X to Y |
| 47 | autoTextTier him = Data_copy (thee); |
| 48 | his xmin = my ymin; |
| 49 | his xmax = my ymax; |
| 50 | for (integer i = 1; i <= his points.size; i ++) { |
| 51 | const TextPoint textpoint = his points.at [i]; |
| 52 | const double time = DTW_getYTimeFromXTime (me, textpoint -> number); |
| 53 | textpoint -> number = time; |
| 54 | } |
| 55 | return him; |
| 56 | } else { |
| 57 | Melder_throw (U"The domain of the TextTier and one of the domains of the DTW should be equal."); |
| 58 | } |
| 59 | } catch (MelderError) { |
| 60 | Melder_throw (U"TextTier not created from DTW & TextTier."); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | autoIntervalTier DTW_IntervalTier_to_IntervalTier (DTW me, IntervalTier thee, double precision) { |
| 65 | try { |
| 66 | //TRACE |
| 67 | trace (U"size: ", thy intervals.size); |
| 68 | trace (U"text: ", thy intervals.at [1] -> text.get()); |
| 69 | trace (U"time range: ", thy intervals.at [1] -> xmin, U" to ", thy intervals.at [1] -> xmax); |
| 70 | if (fabs (my ymin - thy xmin) <= precision && fabs (my ymax - thy xmax) <= precision) { // map from Y to X |
| 71 | autoIntervalTier him = Data_copy (thee); |
| 72 | his xmin = my xmin; |
| 73 | his xmax = my xmax; |
| 74 | TextInterval textinterval = his intervals.at [1]; |
| 75 | textinterval -> xmin = his xmin; |
| 76 | double xmax = DTW_getXTimeFromYTime (me, textinterval -> xmax); |
| 77 | textinterval -> xmax = xmax; |
| 78 | for (integer i = 2; i <= his intervals.size; i ++) { |
| 79 | textinterval = his intervals.at [i]; |
| 80 | textinterval -> xmin = xmax; |
| 81 | xmax = DTW_getXTimeFromYTime (me, textinterval -> xmax); |
| 82 | textinterval -> xmax = xmax; |
| 83 | } |
| 84 | textinterval = his intervals.at [his intervals.size]; |
| 85 | textinterval -> xmax = his xmax; |
| 86 | //Melder_assert (textinterval -> xmin < textinterval -> xmax); |
| 87 | Melder_require (textinterval -> xmin < textinterval -> xmax, |
| 88 | U"Interval runs from ", textinterval -> xmin, U" to ", textinterval -> xmax, U" seconds."); |
| 89 | return him; |
| 90 | } else if (fabs (my xmin - thy xmin) <= precision && fabs (my xmax - thy xmax) <= precision) { // map from X to Y |
| 91 | autoIntervalTier him = Data_copy (thee); |
| 92 | his xmin = my ymin; |
| 93 | his xmax = my ymax; |
| 94 | TextInterval textinterval = his intervals.at [1]; |
| 95 | textinterval -> xmin = his xmin; |
| 96 | double xmax = DTW_getYTimeFromXTime (me, textinterval -> xmax); |
| 97 | textinterval -> xmax = xmax; |
| 98 | trace (U"interval 1: from ", textinterval -> xmin, U" to ", textinterval -> xmax, U", label <<", textinterval -> text.get(), U">>"); |
| 99 | for (integer i = 2; i <= his intervals.size; i ++) { |
| 100 | textinterval = his intervals.at [i]; |
| 101 | textinterval -> xmin = xmax; |
| 102 | xmax = DTW_getYTimeFromXTime (me, textinterval -> xmax); |
| 103 | textinterval -> xmax = xmax; |
| 104 | trace (U"interval ", i, U": from ", textinterval -> xmin, U" to ", textinterval -> xmax, U", label <<", textinterval -> text.get(), U">>"); |
| 105 | } |
| 106 | textinterval = his intervals.at [his intervals.size]; |
| 107 | textinterval -> xmax = his xmax; |
| 108 | trace (U"interval ", his intervals.size, U": from ", textinterval -> xmin, U" to ", textinterval -> xmax, U", label <<", textinterval -> text.get(), U">>"); |
| 109 | //Melder_assert (textinterval -> xmin < textinterval -> xmax); |
| 110 | Melder_require (textinterval -> xmin < textinterval -> xmax, |
| 111 | U"Interval runs from ", textinterval -> xmin, U" to ", textinterval -> xmax, U" seconds."); |
| 112 | return him; |
| 113 | } else { |
| 114 | Melder_throw (U"The domain of the IntervalTier and one of the domains of the DTW should be equal."); |
| 115 | } |
| 116 | } catch (MelderError) { |
| 117 | Melder_throw (U"DTW & IntervalTier: new IntervalTier not created."); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | autoTextGrid DTW_TextGrid_to_TextGrid (DTW me, TextGrid thee, double precision) { |
| 122 | try { |
| 123 | double tmin, tmax; |
| 124 | if (fabs (my ymin - thy xmin) <= precision && fabs (my ymax - thy xmax) <= precision) { |
| 125 | tmin = my xmin; |
| 126 | tmax = my xmax; |
| 127 | } else if (fabs (my xmin - thy xmin) <= precision && fabs (my xmax - thy xmax) <= precision) { |
| 128 | tmin = my ymin; |
| 129 | tmax = my ymax; |
| 130 | } else { |
| 131 | Melder_throw (U"The domain of the TextGrid should be equal to one of the domains of the DTW."); |
| 132 | } |
| 133 | |
| 134 | autoTextGrid him = TextGrid_createWithoutTiers (tmin, tmax); |
| 135 | |
| 136 | for (integer i = 1; i <= thy tiers->size; i ++) { |
| 137 | //TRACE |
| 138 | trace (U"tier ", i); |
| 139 | const Function anyTier = thy tiers->at [i]; |
| 140 | |
| 141 | if (anyTier -> classInfo == classIntervalTier) { |
| 142 | autoIntervalTier tier = DTW_IntervalTier_to_IntervalTier (me, (IntervalTier) anyTier, precision); |
| 143 | TextGrid_addTier_copy (him.get(), tier.get()); |
| 144 | } else if (anyTier -> classInfo == classTextTier) { |
| 145 | autoTextTier tier = DTW_TextTier_to_TextTier (me, (TextTier) anyTier, precision); |
| 146 | TextGrid_addTier_copy (him.get(), tier.get()); |
| 147 | } else { |
| 148 | Melder_throw (U"Unknown tier."); |
| 149 | } |
| 150 | } |
| 151 | return him; |
| 152 | } catch (MelderError) { |
| 153 | Melder_throw (U"DTW & TextGrid: new TextGrid not created."); |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | autoTable DTW_IntervalTier_to_Table (DTW me, IntervalTier thee, double precision) { |
| 158 | try { |
| 159 | const integer numberOfIntervals = thy intervals.size; |
| 160 | const conststring32 columnNames [] = { U"tmin", U"tmax", U"label", U"dist" }; |
| 161 | autoTable him = Table_createWithColumnNames (numberOfIntervals, ARRAY_TO_STRVEC (columnNames)); |
| 162 | if (fabs (my ymin - thy xmin) <= precision && fabs (my ymax - thy xmax) <= precision) { // map from Y to X |
| 163 | integer pathIndex = 1; |
| 164 | for (integer i = 1; i <= numberOfIntervals; i ++) { |
| 165 | const TextInterval textinterval = thy intervals.at [i]; |
| 166 | const double xmin = DTW_getXTimeFromYTime (me, textinterval -> xmin); |
| 167 | const double xmax = DTW_getXTimeFromYTime (me, textinterval -> xmax); |
| 168 | integer ixmin, ixmax; |
| 169 | const integer numberOfFrames = Matrix_getWindowSamplesX (me, xmin, xmax, & ixmin, & ixmax); |
| 170 | double sumOfDistances = 0.0; |
| 171 | while (pathIndex < my pathLength && my path [pathIndex]. x < ixmax) { |
| 172 | sumOfDistances += my z [my path [pathIndex]. y] [my path [pathIndex]. x]; |
| 173 | pathIndex ++; |
| 174 | } |
| 175 | Table_setNumericValue (him.get(), i, 1, textinterval -> xmin); |
| 176 | Table_setNumericValue (him.get(), i, 2, textinterval -> xmax); |
| 177 | Table_setStringValue (him.get(), i, 3, textinterval -> text.get()); |
| 178 | Table_setNumericValue (him.get(), i, 4, sumOfDistances / numberOfFrames); |
| 179 | } |
| 180 | } else if (fabs (my xmin - thy xmin) <= precision && fabs (my xmax - thy xmax) <= precision) { // map from X to Y |
| 181 | integer pathIndex = 1; |
| 182 | for (integer i = 1; i <= numberOfIntervals; i ++) { |
| 183 | const TextInterval textinterval = thy intervals.at [i]; |
| 184 | const double ymin = DTW_getYTimeFromXTime (me, textinterval -> xmin); |
| 185 | const double ymax = DTW_getYTimeFromXTime (me, textinterval -> xmax); |
| 186 | integer iymin, iymax; |
| 187 | const integer numberOfFrames = Matrix_getWindowSamplesY (me, ymin, ymax, & iymin, & iymax); |
| 188 | double sumOfDistances = 0; |
| 189 | while (pathIndex < my pathLength && my path [pathIndex]. y < iymax) { |
| 190 | sumOfDistances += my z [my path [pathIndex]. y] [my path [pathIndex]. x]; |
| 191 | pathIndex ++; |
| 192 | } |
| 193 | Table_setNumericValue (him.get(), i, 1, textinterval -> xmin); |
| 194 | Table_setNumericValue (him.get(), i, 2, textinterval -> xmax); |
| 195 | Table_setStringValue (him.get(), i, 3, textinterval -> text.get()); |
| 196 | Table_setNumericValue (him.get(), i, 4, sumOfDistances / numberOfFrames); |
| 197 | } |
| 198 | } else { |
| 199 | Melder_throw (U"The domain of the IntervalTier and one of the domains of the DTW should be equal."); |
| 200 | } |
| 201 | return him; |
| 202 | } catch (MelderError) { |
| 203 | Melder_throw (me, U": no Table with distances created."); |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | /* Get times from TextGrid and substitute new time form the y-times of the DTW. */ |
| 208 | autoTextTier DTW_TextTier_to_TextTier_old (DTW me, TextTier thee) { |
| 209 | try { |
| 210 | Melder_require (my xmin == thy xmin && my xmax == thy xmax, |
| 211 | U"The domain of the TextTier and the DTW should be equal."); |
| 212 | autoTextTier him = Data_copy (thee); |
| 213 | his xmin = my ymin; |
| 214 | his xmax = my ymax; |
| 215 | for (integer i = 1; i <= his points.size; i ++) { |
| 216 | const TextPoint textpoint = his points.at [i]; |
| 217 | const double time = DTW_getYTimeFromXTime (me, textpoint -> number); |
| 218 | textpoint -> number = time; |
| 219 | } |
| 220 | return him; |
| 221 | } catch (MelderError) { |
| 222 | Melder_throw (U"TextTier not created."); |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | autoIntervalTier DTW_IntervalTier_to_IntervalTier_old (DTW me, IntervalTier thee) { |
| 227 | try { |
| 228 | Melder_require (my xmin == thy xmin && my xmax == thy xmax, |
| 229 | U"The domain of the IntervalTier and the DTW should be equal."); |
| 230 | |
| 231 | autoIntervalTier him = Data_copy (thee); |
| 232 | |
| 233 | his xmin = my ymin; |
| 234 | his xmax = my ymax; |
| 235 | |
| 236 | for (integer i = 1; i <= his intervals.size; i ++) { |
| 237 | const TextInterval textinterval = his intervals.at [i]; |
| 238 | const double xmin = DTW_getYTimeFromXTime (me, textinterval -> xmin); |
| 239 | textinterval -> xmin = xmin; |
| 240 | const double xmax = DTW_getYTimeFromXTime (me, textinterval -> xmax); |
| 241 | textinterval -> xmax = xmax; |
| 242 | } |
| 243 | return him; |
| 244 | } catch (MelderError) { |
| 245 | Melder_throw (U"IntervalTier not created."); |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | autoTextGrid DTW_TextGrid_to_TextGrid_old (DTW me, TextGrid thee) { |
| 250 | try { |
| 251 | autoTextGrid him = Thing_new (TextGrid); |
| 252 | Melder_require (my xmin == thy xmin && my xmax == thy xmax, |
| 253 | U"The domain of the TextGrid and the y-domain of the DTW should be equal."); |
| 254 | |
| 255 | his xmin = my ymin; |
| 256 | his xmax = my ymax; |
| 257 | his tiers = FunctionList_create (); |
| 258 | |
| 259 | for (integer i = 1; i <= thy tiers->size; i ++) { |
| 260 | const Daata anyTier = thy tiers->at [i]; |
| 261 | if (anyTier -> classInfo == classIntervalTier) { |
| 262 | autoIntervalTier tier = DTW_IntervalTier_to_IntervalTier_old (me, (IntervalTier) anyTier); |
| 263 | TextGrid_addTier_copy (him.get(), tier.get()); |
| 264 | } else if (anyTier -> classInfo == classTextTier) { |
| 265 | autoTextTier tier = DTW_TextTier_to_TextTier_old (me, (TextTier) anyTier); |
| 266 | TextGrid_addTier_copy (him.get(), tier.get()); |
| 267 | } else { |
| 268 | Melder_throw (U"Unknown tier."); |
| 269 | } |
| 270 | } |
| 271 | return him; |
| 272 | } catch (MelderError) { |
| 273 | Melder_throw (U"TextGrid not created."); |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | /* End of file DTW_and_TextGrid.cpp */ |