| 1 | /* TextGrid_files.cpp |
| 2 | * |
| 3 | * Copyright (C) 1992-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. |
| 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 "TextGrid.h" |
| 20 | |
| 21 | static int64 Melder_readInteger (const char32 **p) { |
| 22 | char32 kar = * (*p) ++; |
| 23 | Melder_require (kar != U'\0', |
| 24 | U"Looking for an integer, but found the end of the text."); |
| 25 | Melder_require (! Melder_isVerticalSpace (kar), |
| 26 | U"Looking for an integer, but found the end of the line."); |
| 27 | const bool hasSign = ( kar == U'-' || kar == U'+' ); |
| 28 | Melder_require (hasSign || Melder_isAsciiDecimalNumber (kar), |
| 29 | U"Looking for an integer, but found “", *p, U"”."); |
| 30 | constexpr integer MAXIMUM_NUMBER_OF_DIGITS = 40; |
| 31 | char buffer [1 + MAXIMUM_NUMBER_OF_DIGITS + 1]; // include room for leading sign and trailing null |
| 32 | /* mutable increment */ integer ipos = 0; |
| 33 | buffer [ipos ++] = (char) (char8) kar; |
| 34 | if (hasSign) { |
| 35 | char32 shouldBeDigit = * (*p) ++; |
| 36 | Melder_require (Melder_isAsciiDecimalNumber (shouldBeDigit), |
| 37 | U"Looking for a digit after “", kar, U"”, but found “", *p, U"”."); |
| 38 | buffer [ipos ++] = (char) (char8) shouldBeDigit; |
| 39 | } |
| 40 | for (;;) { |
| 41 | char32 mayBeDigit = * (*p) ++; |
| 42 | if (Melder_isAsciiDecimalNumber (mayBeDigit)) { |
| 43 | Melder_require (ipos < hasSign + MAXIMUM_NUMBER_OF_DIGITS, |
| 44 | U"Looking for a normal-sized integer, but found more than ", MAXIMUM_NUMBER_OF_DIGITS, U" digits."); |
| 45 | buffer [ipos ++] = (char) (char8) mayBeDigit; |
| 46 | } else { |
| 47 | (*p) --; |
| 48 | break; |
| 49 | } |
| 50 | } |
| 51 | buffer [ipos] = '\0'; |
| 52 | return strtoll (buffer, nullptr, 10); |
| 53 | } |
| 54 | |
| 55 | static double Melder_readReal (const char32 **p) { |
| 56 | const char32 kar = * (*p) ++; |
| 57 | Melder_require (kar != U'\0', |
| 58 | U"Looking for a real number, but found the end of the text."); |
| 59 | Melder_require (! Melder_isVerticalSpace (kar), |
| 60 | U"Looking for a real number, but found the end of the line."); |
| 61 | const bool hasSign = ( kar == U'-' || kar == U'+' ); |
| 62 | Melder_require (hasSign || Melder_isAsciiDecimalNumber (kar), |
| 63 | U"Looking for a real number, but found “", *p, U"”."); |
| 64 | constexpr integer MAXIMUM_NUMBER_OF_CHARACTERS = 100; |
| 65 | char buffer [1 + MAXIMUM_NUMBER_OF_CHARACTERS + 1]; // include room for leading sign and trailing null |
| 66 | /* mutable increment */ integer ipos = 0; |
| 67 | buffer [ipos ++] = (char) (char8) kar; |
| 68 | if (hasSign) { |
| 69 | char32 shouldBeDigit = * (*p) ++; |
| 70 | Melder_require (Melder_isAsciiDecimalNumber (shouldBeDigit), |
| 71 | U"Looking for a digit after “", kar, U"”, but found “", *p, U"”."); |
| 72 | buffer [ipos ++] = (char) (char8) shouldBeDigit; |
| 73 | } |
| 74 | for (;;) { |
| 75 | char32 numberCharacter = * (*p) ++; |
| 76 | if (Melder_isAsciiDecimalNumber (numberCharacter) || numberCharacter == U'+' || numberCharacter == U'-' || |
| 77 | numberCharacter == U'e' || numberCharacter == U'E' || numberCharacter == U'.' |
| 78 | ) { |
| 79 | Melder_require (ipos < hasSign + MAXIMUM_NUMBER_OF_CHARACTERS, |
| 80 | U"Looking for a normal-sized real number, but found more than ", MAXIMUM_NUMBER_OF_CHARACTERS, U" digits."); |
| 81 | buffer [ipos ++] = (char) (char8) numberCharacter; |
| 82 | } else { |
| 83 | (*p) --; |
| 84 | break; |
| 85 | } |
| 86 | } |
| 87 | buffer [ipos] = '\0'; |
| 88 | return Melder_a8tof (buffer); |
| 89 | } |
| 90 | |
| 91 | autoTextGrid TextGrid_readFromEspsLabelFile ( |
| 92 | const MelderFile file, |
| 93 | const bool tiersArePointTiers, |
| 94 | const integer overrideNumberOfTiers |
| 95 | ) { |
| 96 | try { |
| 97 | autoMelderReadText text = MelderReadText_createFromFile (file); // going to be UTF-8-compatible |
| 98 | |
| 99 | /* |
| 100 | Cycle through all lines until encountering a line that starts with '#'. |
| 101 | */ |
| 102 | /* mutable count */ integer numberOfTiers = 1; |
| 103 | char32 separator = U';'; |
| 104 | for (;;) { |
| 105 | const conststring32 line = MelderReadText_readLine (text.get()); |
| 106 | if (! line) |
| 107 | Melder_throw (U"Missing '#' line."); |
| 108 | if (line [0] == '#') |
| 109 | break; |
| 110 | if (Melder_startsWith (line, U"nfields ")) |
| 111 | numberOfTiers = Melder_atoi (line + 8); |
| 112 | if (Melder_startsWith (line, U"separator ")) |
| 113 | separator = line [10]; |
| 114 | else if (Melder_startsWith (line, U"separator")) |
| 115 | separator = line [9]; |
| 116 | } |
| 117 | Melder_require (numberOfTiers >= 1, |
| 118 | U"The number of tiers has to be at least 1, but the file states that it should be ", numberOfTiers, U"."); |
| 119 | Melder_require (numberOfTiers <= 1'000'000'000, |
| 120 | U"The number of tiers has to be at most 1,000,000,000, but the file states that it should be ", numberOfTiers, U"."); |
| 121 | if (overrideNumberOfTiers >= 1) |
| 122 | numberOfTiers = overrideNumberOfTiers; |
| 123 | |
| 124 | /* mutable preliminary */ double globalTmin = 0.0, globalTmax = 1e-6; |
| 125 | autoTextGrid me = TextGrid_createWithoutTiers (globalTmin, globalTmax); |
| 126 | for (integer itier = 1; itier <= numberOfTiers; itier ++) { |
| 127 | autoFunction tier = tiersArePointTiers ? static_cast <autoFunction> (TextTier_create (globalTmin, globalTmax)) : |
| 128 | static_cast <autoFunction> (IntervalTier_create_raw (globalTmin, globalTmax)); |
| 129 | Thing_setName (tier.get(), Melder_integer (itier)); |
| 130 | my tiers -> addItem_move (tier.move()); |
| 131 | } |
| 132 | Melder_assert (my tiers->size > 0); |
| 133 | |
| 134 | autoMelderString label; |
| 135 | /* mutable step */ double startingTime = 0.0, endTime = 1e-6; |
| 136 | for (;;) { |
| 137 | /* mutable scan */ conststring32 line = MelderReadText_readLine (text.get()); |
| 138 | if (! line) |
| 139 | break; |
| 140 | if (line [0] == U'\0') |
| 141 | continue; // moderately normal stray empty line |
| 142 | if (line [0] == U';') { |
| 143 | /* |
| 144 | This is a continuation from the previous line; it occurs in Buckeye/s35/s3504a.words. |
| 145 | The interpretation is that this is the last tier. |
| 146 | */ |
| 147 | line ++; // step over semicolon |
| 148 | Melder_skipHorizontalSpace (& line); |
| 149 | Function lastTier = my tiers->at [my tiers->size]; |
| 150 | if (tiersArePointTiers) { |
| 151 | TextTier tier = static_cast <TextTier> (lastTier); |
| 152 | Melder_require (tier -> points.size > 0, |
| 153 | U"Stray semicolon in line ", MelderReadText_getLineNumber (text.get()), U"."); |
| 154 | TextPoint point = tier -> points.at [tier -> points.size]; |
| 155 | TextPoint_setText (point, line); |
| 156 | } else { |
| 157 | IntervalTier tier = static_cast <IntervalTier> (lastTier); |
| 158 | if (tier -> intervals.size > 0) { |
| 159 | TextInterval interval = tier -> intervals.at [tier -> intervals.size]; |
| 160 | TextInterval_setText (interval, line); |
| 161 | } |
| 162 | } |
| 163 | /* |
| 164 | The quirk in Buckeye/s35/s3504a.words also means that the second and third tiers haven't been filled. |
| 165 | Repair this very specific situation, after checking that it indeed occurs. |
| 166 | */ |
| 167 | if (my tiers->size == 4) { |
| 168 | auto getAbbreviation = [] (conststring32 fullText) -> conststring32 { |
| 169 | if (Melder_equ (fullText, U"<VOCNOISE>")) |
| 170 | return U"U"; |
| 171 | if (Melder_startsWith (fullText, U"<SIL")) |
| 172 | return U"S"; |
| 173 | if (Melder_startsWith (fullText, U"<IVER")) |
| 174 | return U"S"; |
| 175 | if (Melder_startsWith (fullText, U"<IVER")) |
| 176 | return U"S"; |
| 177 | if (Melder_equ (fullText, U"{B_TRANS}")) |
| 178 | return U"B"; |
| 179 | if (Melder_equ (fullText, U"{E_TRANS}")) |
| 180 | return U"E"; |
| 181 | if (Melder_startsWith (fullText, U"<UNKNOWN")) |
| 182 | return U"U"; |
| 183 | if (Melder_startsWith (fullText, U"<CUTOFF")) |
| 184 | return U"U"; |
| 185 | if (Melder_startsWith (fullText, U"<LAUGH")) |
| 186 | return U"U"; |
| 187 | if (Melder_startsWith (fullText, U"<EXT")) |
| 188 | return U"U"; |
| 189 | if (Melder_startsWith (fullText, U"<NOISE")) |
| 190 | return U"U"; |
| 191 | if (Melder_startsWith (fullText, U"<ERROR")) |
| 192 | return U"U"; |
| 193 | if (Melder_startsWith (fullText, U"<HES")) |
| 194 | return U"U"; |
| 195 | return U""; |
| 196 | }; |
| 197 | /* |
| 198 | Get the texts from the first three tiers. |
| 199 | */ |
| 200 | Function firstAnyTier = my tiers->at [1]; |
| 201 | Function secondAnyTier = my tiers->at [2]; |
| 202 | Function thirdAnyTier = my tiers->at [3]; |
| 203 | if (tiersArePointTiers) { |
| 204 | TextTier firstTier = static_cast <TextTier> (firstAnyTier); |
| 205 | TextTier secondTier = static_cast <TextTier> (secondAnyTier); |
| 206 | TextTier thirdTier = static_cast <TextTier> (thirdAnyTier); |
| 207 | if (firstTier->points.size > 0 && secondTier->points.size > 0 && thirdTier->points.size > 0) { |
| 208 | TextTier tier = static_cast <TextTier> (lastTier); |
| 209 | TextPoint point = tier -> points.at [tier -> points.size]; |
| 210 | const double localTime = point -> number; |
| 211 | TextPoint firstPoint = firstTier -> points.at [firstTier->points.size]; |
| 212 | TextPoint secondPoint = secondTier -> points.at [secondTier->points.size]; |
| 213 | TextPoint thirdPoint = thirdTier -> points.at [thirdTier->points.size]; |
| 214 | if ( |
| 215 | firstPoint -> number == localTime && |
| 216 | secondPoint -> number == localTime && |
| 217 | thirdPoint -> number == localTime && |
| 218 | ! Melder_equ (firstPoint -> mark.get(), U"") && // The first tier should have a text, |
| 219 | Melder_equ (secondPoint -> mark.get(), U"") && // but the second tier should be empty |
| 220 | Melder_equ (thirdPoint -> mark.get(), U"") // and the third tier should also be empty. |
| 221 | ) { |
| 222 | conststring32 abbreviation = getAbbreviation (firstPoint -> mark.get()); |
| 223 | TextPoint_setText (secondPoint, abbreviation); |
| 224 | TextPoint_setText (thirdPoint, abbreviation); |
| 225 | } |
| 226 | } |
| 227 | } else { |
| 228 | IntervalTier firstTier = static_cast <IntervalTier> (firstAnyTier); |
| 229 | IntervalTier secondTier = static_cast <IntervalTier> (secondAnyTier); |
| 230 | IntervalTier thirdTier = static_cast <IntervalTier> (thirdAnyTier); |
| 231 | if (firstTier->intervals.size > 0 && secondTier->intervals.size > 0 && thirdTier->intervals.size > 0) { |
| 232 | IntervalTier tier = static_cast <IntervalTier> (lastTier); |
| 233 | TextInterval interval = tier -> intervals.at [tier -> intervals.size]; |
| 234 | const double localTmin = interval -> xmin; |
| 235 | const double localTmax = interval -> xmax; |
| 236 | TextInterval firstInterval = firstTier -> intervals.at [firstTier->intervals.size]; |
| 237 | TextInterval secondInterval = secondTier -> intervals.at [secondTier->intervals.size]; |
| 238 | TextInterval thirdInterval = thirdTier -> intervals.at [thirdTier->intervals.size]; |
| 239 | if ( |
| 240 | firstInterval -> xmin == localTmin && |
| 241 | secondInterval -> xmin == localTmin && |
| 242 | thirdInterval -> xmin == localTmin && |
| 243 | firstInterval -> xmax == localTmax && |
| 244 | secondInterval -> xmax == localTmax && |
| 245 | thirdInterval -> xmax == localTmax && |
| 246 | ! Melder_equ (firstInterval -> text.get(), U"") && // The first tier should have a text, |
| 247 | Melder_equ (secondInterval -> text.get(), U"") && // but the second tier should be empty |
| 248 | Melder_equ (thirdInterval -> text.get(), U"") // and the third tier should also be empty. |
| 249 | ) { |
| 250 | conststring32 abbreviation = getAbbreviation (firstInterval -> text.get()); |
| 251 | TextInterval_setText (secondInterval, abbreviation); |
| 252 | TextInterval_setText (thirdInterval, abbreviation); |
| 253 | } |
| 254 | } |
| 255 | } |
| 256 | } |
| 257 | continue; |
| 258 | } |
| 259 | Melder_skipHorizontalSpace (& line); |
| 260 | endTime = Melder_readReal (& line); |
| 261 | if (endTime < 0.0) { |
| 262 | Melder_casual (U"A negative time in file ", file, U" in line ", MelderReadText_getLineNumber (text.get()), U"."); |
| 263 | continue; // don't insert any interval |
| 264 | } |
| 265 | if (endTime < startingTime) { |
| 266 | /* |
| 267 | This could be the error in Buckeye/s28/s2801a/s2301a.words. |
| 268 | */ |
| 269 | if (my tiers->size == 4 && fabs (endTime - 421.986875) < 1e-9 && fabs (startingTime - 431.847408) < 1e-9) |
| 270 | endTime += 10.0; |
| 271 | } |
| 272 | {// scope |
| 273 | const char32 shouldBeHorizontalSpace = * line ++; |
| 274 | Melder_require (Melder_isHorizontalSpace (shouldBeHorizontalSpace), |
| 275 | U"There should be a space after the time in line ", MelderReadText_getLineNumber (text.get()), U"."); |
| 276 | } |
| 277 | Melder_skipHorizontalSpace (& line); |
| 278 | const integer colour = Melder_readInteger (& line); |
| 279 | (void) colour; // ignore |
| 280 | {// scope |
| 281 | const char32 shouldBeHorizontalSpace = * line ++; |
| 282 | Melder_require (Melder_isHorizontalSpace (shouldBeHorizontalSpace), |
| 283 | U"There should be a space after the colour number in line ", MelderReadText_getLineNumber (text.get()), U"."); |
| 284 | } |
| 285 | for (integer itier = 1; itier <= numberOfTiers; itier ++) { |
| 286 | Melder_skipHorizontalSpace (& line); |
| 287 | MelderString_empty (& label); |
| 288 | for (;;) { |
| 289 | char32 kar = * line; |
| 290 | if (kar == U'\0') |
| 291 | break; |
| 292 | if (kar == separator) { |
| 293 | line ++; // consume separator |
| 294 | break; |
| 295 | } |
| 296 | MelderString_appendCharacter (& label, kar); |
| 297 | line ++; // consume kar |
| 298 | } |
| 299 | while (label.length > 0 && Melder_isHorizontalSpace (label.string [label.length - 1])) // FIXME should encapsulate |
| 300 | label.string [-- label.length] = U'\0'; // remove the space before ";" in e.g. "ih ; *" |
| 301 | if (tiersArePointTiers) { |
| 302 | TextTier tier = static_cast <TextTier> (my tiers->at [itier]); |
| 303 | TextTier_addPoint (tier, endTime, label.string); |
| 304 | } else { |
| 305 | IntervalTier tier = static_cast <IntervalTier> (my tiers->at [itier]); |
| 306 | if (startingTime == endTime) { |
| 307 | /* |
| 308 | A zero-duration interval: combine with the previous interval. |
| 309 | */ |
| 310 | if (tier -> intervals.size > 1) { |
| 311 | const TextInterval previousInterval = tier -> intervals.at [tier -> intervals.size]; |
| 312 | TextInterval_setText (previousInterval, Melder_cat (previousInterval -> text.get(), U"//", label.string)); |
| 313 | } else |
| 314 | Melder_casual (U"A zero-duration interval at the start of file ", file, U" in tier ", itier, |
| 315 | U", containing the text “", label.string, U"”."); |
| 316 | } else { |
| 317 | (void) IntervalTier_addInterval_raw (tier, startingTime, endTime, label.string); |
| 318 | } |
| 319 | } |
| 320 | } |
| 321 | startingTime = endTime; |
| 322 | } |
| 323 | for (integer itier = 1; itier <= numberOfTiers; itier ++) { |
| 324 | Function tier = my tiers->at [itier]; |
| 325 | tier -> xmax = endTime; |
| 326 | if (! tiersArePointTiers) |
| 327 | IntervalTier_haveAtLeastOneInterval (static_cast <IntervalTier> (tier)); |
| 328 | } |
| 329 | my xmax = endTime; |
| 330 | |
| 331 | TextGrid_checkInvariants_e (me.get(), true); |
| 332 | return me; |
| 333 | } catch (MelderError) { |
| 334 | Melder_throw (U"TextGrid not read from file ", file, U"."); |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | void IntervalTier_writeToXwaves (IntervalTier me, MelderFile file) { |
| 339 | try { |
| 340 | autofile f = Melder_fopen (file, "w"); |
| 341 | fprintf (f, "separator ;\nnfields 1\n#\n"); |
| 342 | for (integer iinterval = 1; iinterval <= my intervals.size; iinterval ++) { |
| 343 | TextInterval interval = my intervals.at [iinterval]; |
| 344 | fprintf (f, "\t%.6f 26\t%s\n", interval -> xmax, Melder_peek32to8 (interval -> text.get())); |
| 345 | } |
| 346 | f.close (file); |
| 347 | } catch (MelderError) { |
| 348 | Melder_throw (me, U": not written to Xwaves file ", file, U"."); |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | static void sgmlToPraat (char *text) { |
| 353 | char *sgml = text, *praat = text; |
| 354 | for (;;) { |
| 355 | if (*sgml == '\0') |
| 356 | break; |
| 357 | if (*sgml == '&') { |
| 358 | static struct { const char *sgml, *praat; } translations [] = { |
| 359 | { "auml", "\\a\"" }, { "euml", "\\e\"" }, { "iuml", "\\i\"" }, |
| 360 | { "ouml", "\\o\"" }, { "ouml", "\\o\"" }, |
| 361 | { "Auml", "\\A\"" }, { "Euml", "\\E\"" }, { "Iuml", "\\I\"" }, |
| 362 | { "Ouml", "\\O\"" }, { "Uuml", "\\U\"" }, |
| 363 | { "aacute", "\\a'" }, { "eacute", "\\e'" }, { "iacute", "\\i'" }, |
| 364 | { "oacute", "\\o'" }, { "oacute", "\\o'" }, |
| 365 | { "Aacute", "\\A'" }, { "Eacute", "\\E'" }, { "Iacute", "\\I'" }, |
| 366 | { "Oacute", "\\O'" }, { "Uacute", "\\U'" }, |
| 367 | { "agrave", "\\a`" }, { "egrave", "\\e`" }, { "igrave", "\\i`" }, |
| 368 | { "ograve", "\\o`" }, { "ograve", "\\o`" }, |
| 369 | { "Agrave", "\\A`" }, { "Egrave", "\\E`" }, { "Igrave", "\\I`" }, |
| 370 | { "Ograve", "\\O`" }, { "Ugrave", "\\U`" }, |
| 371 | { "acirc", "\\a^" }, { "ecirc", "\\e^" }, { "icirc", "\\i^" }, |
| 372 | { "ocirc", "\\o^" }, { "ocirc", "\\o^" }, |
| 373 | { "Acirc", "\\A^" }, { "Ecirc", "\\E^" }, { "Icirc", "\\I^" }, |
| 374 | { "Ocirc", "\\O^" }, { "Ucirc", "\\U^" }, |
| 375 | { nullptr, nullptr } }; |
| 376 | char sgmlCode [201]; |
| 377 | int i = 0; |
| 378 | ++ sgml; |
| 379 | for (i = 0; i < 200; i ++) { |
| 380 | char sgmlChar = sgml [i]; |
| 381 | if (sgmlChar == ';') { |
| 382 | if (i == 0) |
| 383 | Melder_throw (U"Empty SGML code."); |
| 384 | sgml += i + 1; |
| 385 | break; |
| 386 | } |
| 387 | sgmlCode [i] = sgmlChar; |
| 388 | } |
| 389 | if (i >= 200) |
| 390 | Melder_throw (U"Unfinished SGML code."); |
| 391 | sgmlCode [i] = '\0'; |
| 392 | for (i = 0; translations [i]. sgml; i ++) { |
| 393 | if (strequ (sgmlCode, translations [i]. sgml)) { |
| 394 | memcpy (praat, translations [i]. praat, strlen (translations [i]. praat)); |
| 395 | praat += strlen (translations [i]. praat); |
| 396 | break; |
| 397 | } |
| 398 | } |
| 399 | if (! translations [i]. sgml) |
| 400 | Melder_throw (U"Unknown SGML code &", Melder_peek8to32_u (sgmlCode), U";."); |
| 401 | } else { |
| 402 | * praat ++ = * sgml ++; |
| 403 | } |
| 404 | } |
| 405 | *praat = '\0'; |
| 406 | } |
| 407 | |
| 408 | autoTextGrid TextGrid_readFromChronologicalTextFile (MelderFile file) { |
| 409 | try { |
| 410 | int formatVersion = 0; |
| 411 | autoMelderReadText text = MelderReadText_createFromFile (file); |
| 412 | autostring32 tag = texgetw16 (text.get()); |
| 413 | if (! str32equ (tag.get(), U"Praat chronological TextGrid text file")) |
| 414 | Melder_throw (U"This is not a chronological TextGrid text file."); |
| 415 | autoTextGrid me = Thing_new (TextGrid); |
| 416 | my structFunction :: v1_readText (text.get(), formatVersion); |
| 417 | my tiers = FunctionList_create (); |
| 418 | integer numberOfTiers = texgeti32 (text.get()); |
| 419 | for (integer itier = 1; itier <= numberOfTiers; itier ++) { |
| 420 | autostring32 klas = texgetw16 (text.get()); |
| 421 | if (str32equ (klas.get(), U"IntervalTier")) { |
| 422 | autoIntervalTier tier = Thing_new (IntervalTier); |
| 423 | tier -> name = texgetw16 (text.get()); |
| 424 | tier -> structFunction :: v1_readText (text.get(), formatVersion); |
| 425 | my tiers -> addItem_move (tier.move()); |
| 426 | } else if (str32equ (klas.get(), U"TextTier")) { |
| 427 | autoTextTier tier = Thing_new (TextTier); |
| 428 | tier -> name = texgetw16 (text.get()); |
| 429 | tier -> structFunction :: v1_readText (text.get(), formatVersion); |
| 430 | my tiers -> addItem_move (tier.move()); |
| 431 | } else { |
| 432 | Melder_throw (U"Unknown tier class \"", klas.get(), U"\"."); |
| 433 | } |
| 434 | } |
| 435 | for (;;) { |
| 436 | integer tierNumber; |
| 437 | try { |
| 438 | tierNumber = texgeti32 (text.get()); |
| 439 | } catch (MelderError) { |
| 440 | if (str32str (Melder_getError (), U"Early end of text")) { |
| 441 | Melder_clearError (); |
| 442 | break; |
| 443 | } else { |
| 444 | throw; |
| 445 | } |
| 446 | } |
| 447 | Function anyTier = TextGrid_checkSpecifiedTierNumberWithinRange (me.get(), tierNumber); |
| 448 | if (anyTier -> classInfo == classIntervalTier) { |
| 449 | IntervalTier tier = static_cast <IntervalTier> (anyTier); |
| 450 | autoTextInterval interval = Thing_new (TextInterval); |
| 451 | interval -> v1_readText (text.get(), formatVersion); |
| 452 | tier -> intervals. addItem_move (interval.move()); // not earlier: sorting depends on contents of interval |
| 453 | } else { |
| 454 | TextTier tier = static_cast <TextTier> (anyTier); |
| 455 | autoTextPoint point = Thing_new (TextPoint); |
| 456 | point -> v1_readText (text.get(), formatVersion); |
| 457 | tier -> points. addItem_move (point.move()); // not earlier: sorting depends on contents of point |
| 458 | } |
| 459 | } |
| 460 | return me; |
| 461 | } catch (MelderError) { |
| 462 | Melder_throw (U"TextGrid not read from chronological text file ", file, U"."); |
| 463 | } |
| 464 | } |
| 465 | |
| 466 | static void writeQuotedString (MelderFile file, conststring32 string) { |
| 467 | MelderFile_writeCharacter (file, U'\"'); |
| 468 | if (string) { |
| 469 | char32 kar; |
| 470 | while ((kar = *string ++) != U'\0') { |
| 471 | MelderFile_writeCharacter (file, kar); |
| 472 | if (kar == '\"') |
| 473 | MelderFile_writeCharacter (file, kar); |
| 474 | } |
| 475 | } // BUG |
| 476 | MelderFile_writeCharacter (file, U'\"'); |
| 477 | } |
| 478 | |
| 479 | void TextGrid_writeToChronologicalTextFile (TextGrid me, MelderFile file) { |
| 480 | try { |
| 481 | Data_createTextFile (me, file, false); |
| 482 | autoMelderFile mfile = file; |
| 483 | /* |
| 484 | The "elements" (intervals and points) are sorted primarily by time and secondarily by tier. |
| 485 | */ |
| 486 | double sortingTime = -1e308; |
| 487 | integer sortingTier = 0; |
| 488 | file -> verbose = false; |
| 489 | texindent (file); |
| 490 | MelderFile_write (file, U"\"Praat chronological TextGrid text file\"\n", my xmin, U" ", my xmax, |
| 491 | U" ! Time domain.\n", my tiers->size, U" ! Number of tiers."); |
| 492 | for (integer itier = 1; itier <= my tiers->size; itier ++) { |
| 493 | Function anyTier = my tiers->at [itier]; |
| 494 | MelderFile_write (file, U"\n"); |
| 495 | writeQuotedString (file, Thing_className (anyTier)); |
| 496 | MelderFile_write (file, U" "); |
| 497 | writeQuotedString (file, anyTier -> name.get()); |
| 498 | MelderFile_write (file, U" ", anyTier -> xmin, U" ", anyTier -> xmax); |
| 499 | } |
| 500 | for (;;) { |
| 501 | double firstRemainingTime = +1e308; |
| 502 | integer firstRemainingTier = 2000000000, firstRemainingElement = 0; |
| 503 | for (integer itier = 1; itier <= my tiers->size; itier ++) { |
| 504 | Function anyTier = my tiers->at [itier]; |
| 505 | if (anyTier -> classInfo == classIntervalTier) { |
| 506 | IntervalTier tier = static_cast <IntervalTier> (anyTier); |
| 507 | for (integer ielement = 1; ielement <= tier -> intervals.size; ielement ++) { |
| 508 | TextInterval interval = tier -> intervals.at [ielement]; |
| 509 | if ((interval -> xmin > sortingTime || // sort primarily by time |
| 510 | (interval -> xmin == sortingTime && itier > sortingTier)) && // sort secondarily by tier number |
| 511 | (interval -> xmin < firstRemainingTime || // sort primarily by time |
| 512 | (interval -> xmin == firstRemainingTime && itier < firstRemainingTier))) // sort secondarily by tier number |
| 513 | { |
| 514 | firstRemainingTime = interval -> xmin; |
| 515 | firstRemainingTier = itier; |
| 516 | firstRemainingElement = ielement; |
| 517 | } |
| 518 | } |
| 519 | } else { |
| 520 | TextTier tier = static_cast <TextTier> (anyTier); |
| 521 | for (integer ielement = 1; ielement <= tier -> points.size; ielement ++) { |
| 522 | TextPoint point = tier -> points.at [ielement]; |
| 523 | if ((point -> number > sortingTime || // sort primarily by time |
| 524 | (point -> number == sortingTime && itier > sortingTier)) && // sort secondarily by tier number |
| 525 | (point -> number < firstRemainingTime || // sort primarily by time |
| 526 | (point -> number == firstRemainingTime && itier < firstRemainingTier))) // sort secondarily by tier number |
| 527 | { |
| 528 | firstRemainingTime = point -> number; |
| 529 | firstRemainingTier = itier; |
| 530 | firstRemainingElement = ielement; |
| 531 | } |
| 532 | } |
| 533 | } |
| 534 | } |
| 535 | if (firstRemainingElement == 0) { |
| 536 | break; |
| 537 | } else { |
| 538 | Function anyTier = my tiers->at [firstRemainingTier]; |
| 539 | if (anyTier -> classInfo == classIntervalTier) { |
| 540 | IntervalTier tier = static_cast <IntervalTier> (anyTier); |
| 541 | TextInterval interval = tier -> intervals.at [firstRemainingElement]; |
| 542 | if (tier -> name) MelderFile_write (file, U"\n\n! ", tier -> name.get(), U":"); |
| 543 | MelderFile_write (file, U"\n", firstRemainingTier, U" ", interval -> xmin, U" ", interval -> xmax); |
| 544 | texputw32 (file, interval -> text.get(), U"", 0,0,0,0,0); |
| 545 | } else { |
| 546 | TextTier tier = static_cast <TextTier> (anyTier); |
| 547 | TextPoint point = tier -> points.at [firstRemainingElement]; |
| 548 | if (tier -> name) MelderFile_write (file, U"\n\n! ", tier -> name.get(), U":"); |
| 549 | MelderFile_write (file, U"\n", firstRemainingTier, U" ", point -> number, U" "); |
| 550 | texputw32 (file, point -> mark.get(), U"", 0,0,0,0,0); |
| 551 | } |
| 552 | sortingTime = firstRemainingTime; |
| 553 | sortingTier = firstRemainingTier; |
| 554 | } |
| 555 | } |
| 556 | texexdent (file); |
| 557 | mfile.close (); |
| 558 | } catch (MelderError) { |
| 559 | Melder_throw (me, U": not written to chronological text file ", file, U"."); |
| 560 | } |
| 561 | } |
| 562 | |
| 563 | autoTextGrid TextGrid_readFromCgnSyntaxFile (MelderFile file) { |
| 564 | try { |
| 565 | autoTextGrid me = Thing_new (TextGrid); |
| 566 | integer sentenceNumber = 0; |
| 567 | double phraseBegin = 0.0, phraseEnd = 0.0; |
| 568 | IntervalTier sentenceTier = nullptr, phraseTier = nullptr; |
| 569 | TextInterval lastInterval = nullptr; |
| 570 | static char phrase [1000]; |
| 571 | my tiers = FunctionList_create (); |
| 572 | autoMelderFile mfile = MelderFile_open (file); |
| 573 | char *const line1 = MelderFile_readLine8 (file); |
| 574 | Melder_require (strequ (line1, "<?xml version=\"1.0\"?>"), |
| 575 | U"This is not a CGN syntax file."); |
| 576 | char *const line2 = MelderFile_readLine8 (file); |
| 577 | Melder_require (strequ (line2, "<!DOCTYPE ttext SYSTEM \"ttext.dtd\">"), |
| 578 | U"This is not a CGN syntax file."); |
| 579 | (void) MelderFile_readLine8 (file); // ignore third line |
| 580 | const integer startOfData = MelderFile_tell (file); |
| 581 | /* |
| 582 | Get duration. |
| 583 | */ |
| 584 | my xmin = 0.0; |
| 585 | char arg1 [41], arg2 [41], arg3 [41], arg4 [41], arg5 [41], arg6 [41], arg7 [201]; |
| 586 | for (;;) { |
| 587 | char *const line = MelderFile_readLine8 (file); |
| 588 | if (! line) |
| 589 | break; |
| 590 | if (strnequ (line, " <tau ref=\"", 12)) { |
| 591 | Melder_require (sscanf (line, "%40s%40s%40s%40s%40s%40s%200s", arg1, arg2, arg3, arg4, arg5, arg6, arg7) == 7, |
| 592 | U"Too few strings in tau line."); |
| 593 | my xmax = atof (arg5 + 4); |
| 594 | } |
| 595 | } |
| 596 | Melder_require (my xmax > 0.0, |
| 597 | U"Duration (", my xmax, U" seconds) should be greater than zero."); |
| 598 | /* |
| 599 | Get number and names of tiers. |
| 600 | */ |
| 601 | MelderFile_seek (file, startOfData, SEEK_SET); |
| 602 | for (;;) { |
| 603 | char *const line = MelderFile_readLine8 (file); |
| 604 | if (! line) |
| 605 | break; |
| 606 | if (strnequ (line, " <tau ref=\"", 12)) { |
| 607 | Melder_require (sscanf (line, "%40s%40s%40s%40s%40s%40s%200s", arg1, arg2, arg3, arg4, arg5, arg6, arg7) == 7, |
| 608 | U"Too few strings in tau line."); |
| 609 | const integer length_s = Melder8_length (arg3); |
| 610 | Melder_require (length_s >= 5 && strnequ (arg3, "s=\"", 3), |
| 611 | U"Missing speaker name."); |
| 612 | arg3 [length_s - 1] = '\0'; // truncate at double quote |
| 613 | char *const speakerName = arg3 + 3; // truncate leading s=" |
| 614 | /* |
| 615 | Does this speaker name occur in the tiers? |
| 616 | */ |
| 617 | /* mutable search */ integer speakerTier = 0; |
| 618 | for (integer itier = 1; itier <= my tiers->size; itier ++) { |
| 619 | IntervalTier tier = static_cast <IntervalTier> (my tiers->at [itier]); |
| 620 | if (str32equ (tier -> name.get(), Melder_peek8to32_u (speakerName))) { |
| 621 | speakerTier = itier; |
| 622 | break; |
| 623 | } |
| 624 | } |
| 625 | if (speakerTier == 0) { |
| 626 | /* |
| 627 | Create two new tiers. |
| 628 | */ |
| 629 | autoIntervalTier newSentenceTier = Thing_new (IntervalTier); |
| 630 | newSentenceTier -> xmin = 0.0; |
| 631 | newSentenceTier -> xmax = my xmax; |
| 632 | Thing_setName (newSentenceTier.get(), Melder_peek8to32_u (speakerName)); |
| 633 | sentenceTier = (IntervalTier) my tiers -> addItem_move (newSentenceTier.move()); |
| 634 | autoIntervalTier newPhraseTier = Thing_new (IntervalTier); |
| 635 | newPhraseTier -> xmin = 0.0; |
| 636 | newPhraseTier -> xmax = my xmax; |
| 637 | Thing_setName (newPhraseTier.get(), Melder_peek8to32_u (speakerName)); |
| 638 | phraseTier = (IntervalTier) my tiers -> addItem_move (newPhraseTier.move()); |
| 639 | } else { |
| 640 | sentenceTier = (IntervalTier) my tiers->at [speakerTier]; |
| 641 | phraseTier = (IntervalTier) my tiers->at [speakerTier + 1]; |
| 642 | } |
| 643 | const double tb = atof (arg4 + 4), te = atof (arg5 + 4); |
| 644 | Melder_require (te > tb, |
| 645 | U"Zero duration for sentence."); |
| 646 | /* |
| 647 | We are going to add one or two intervals to the sentence tier. |
| 648 | */ |
| 649 | if (sentenceTier -> intervals.size > 0) { |
| 650 | TextInterval latestInterval = sentenceTier -> intervals.at [sentenceTier -> intervals.size]; |
| 651 | if (tb > latestInterval -> xmax) { |
| 652 | autoTextInterval interval = TextInterval_create (latestInterval -> xmax, tb, U""); |
| 653 | sentenceTier -> intervals. addItem_move (interval.move()); |
| 654 | } else if (tb < latestInterval -> xmax) { |
| 655 | Melder_throw (U"Overlap on tier not allowed."); |
| 656 | } |
| 657 | } else { |
| 658 | if (tb > 0.0) { |
| 659 | autoTextInterval interval = TextInterval_create (0.0, tb, U""); |
| 660 | sentenceTier -> intervals. addItem_move (interval.move()); |
| 661 | } else if (tb < 0.0) { |
| 662 | Melder_throw (U"Negative times not allowed."); |
| 663 | } |
| 664 | } |
| 665 | autoTextInterval interval = TextInterval_create (tb, te, Melder_integer (++ sentenceNumber)); |
| 666 | sentenceTier -> intervals. addItem_move (interval.move()); |
| 667 | } else if (strnequ (line, " <tw ref=\"", 13)) { |
| 668 | Melder_require (sscanf (line, "%40s%40s%40s%40s%40s%40s%200s", arg1, arg2, arg3, arg4, arg5, arg6, arg7) == 7, |
| 669 | U"Too few strings in tw line."); |
| 670 | const integer length_tb = Melder8_length (arg3); |
| 671 | Melder_require (length_tb >= 6 && strnequ (arg3, "tb=\"", 4), |
| 672 | U"Missing tb."); |
| 673 | const double tb = atof (arg3 + 4); |
| 674 | const integer length_te = Melder8_length (arg4); |
| 675 | Melder_require (length_te >= 6 && strnequ (arg4, "te=\"", 4), |
| 676 | U"Missing te."); |
| 677 | const double te = atof (arg4 + 4); |
| 678 | Melder_require (te > tb, |
| 679 | U"Zero duration for phrase."); |
| 680 | if (tb == phraseBegin && te == phraseEnd) { |
| 681 | /* |
| 682 | Append a word. |
| 683 | */ |
| 684 | strcat (phrase, " "); |
| 685 | const integer length_w = Melder8_length (arg7); |
| 686 | Melder_require (length_w >= 6 && strnequ (arg7, "w=\"", 3), // BUG? no words of length 1 allowed |
| 687 | U"Missing word."); |
| 688 | arg7 [length_w - 3] = '\0'; // truncate "/> |
| 689 | strcat (phrase, arg7 + 3); |
| 690 | } else { |
| 691 | /* |
| 692 | Begin a phrase. |
| 693 | */ |
| 694 | if (lastInterval) { |
| 695 | sgmlToPraat (phrase); |
| 696 | TextInterval_setText (lastInterval, Melder_peek8to32_u (phrase)); |
| 697 | } |
| 698 | phrase [0] = '\0'; |
| 699 | const integer length_w = Melder8_length (arg7); |
| 700 | Melder_require (length_w >= 6 && strnequ (arg7, "w=\"", 3), // BUG? no words of length 1 allowed |
| 701 | U"Missing word."); |
| 702 | arg7 [length_w - 3] = '\0'; // truncate "/> |
| 703 | strcat (phrase, arg7 + 3); |
| 704 | if (phraseTier -> intervals.size > 0) { |
| 705 | TextInterval latestInterval = phraseTier -> intervals.at [phraseTier -> intervals.size]; |
| 706 | if (tb > latestInterval -> xmax) { |
| 707 | autoTextInterval interval = TextInterval_create (latestInterval -> xmax, tb, U""); |
| 708 | phraseTier -> intervals. addItem_move (interval.move()); |
| 709 | } else if (tb < latestInterval -> xmax) { |
| 710 | Melder_throw (U"Overlap on tier not allowed."); |
| 711 | } |
| 712 | } else { |
| 713 | if (tb > 0.0) { |
| 714 | autoTextInterval interval = TextInterval_create (0.0, tb, U""); |
| 715 | phraseTier -> intervals. addItem_move (interval.move()); |
| 716 | } else if (tb < 0.0) { |
| 717 | Melder_throw (U"Negative times not allowed."); |
| 718 | } |
| 719 | } |
| 720 | if (! phraseTier) |
| 721 | Melder_throw (U"Phrase outside sentence."); |
| 722 | autoTextInterval newLastInterval = TextInterval_create (tb, te, U""); |
| 723 | lastInterval = newLastInterval.get(); |
| 724 | phraseTier -> intervals. addItem_move (newLastInterval.move()); |
| 725 | phraseBegin = tb; |
| 726 | phraseEnd = te; |
| 727 | } |
| 728 | } |
| 729 | } |
| 730 | if (lastInterval) { |
| 731 | sgmlToPraat (phrase); |
| 732 | TextInterval_setText (lastInterval, Melder_peek8to32_u (phrase)); |
| 733 | } |
| 734 | for (integer itier = 1; itier <= my tiers->size; itier ++) { |
| 735 | IntervalTier tier = static_cast <IntervalTier> (my tiers->at [itier]); |
| 736 | if (tier -> intervals.size > 0) { |
| 737 | TextInterval latestInterval = tier -> intervals.at [tier -> intervals.size]; |
| 738 | if (my xmax > latestInterval -> xmax) { |
| 739 | autoTextInterval interval = TextInterval_create (latestInterval -> xmax, my xmax, U""); |
| 740 | tier -> intervals. addItem_move (interval.move()); |
| 741 | } |
| 742 | } else { |
| 743 | autoTextInterval interval = TextInterval_create (my xmin, my xmax, U""); |
| 744 | tier -> intervals. addItem_move (interval.move()); |
| 745 | } |
| 746 | } |
| 747 | mfile.close (); |
| 748 | return me; |
| 749 | } catch (MelderError) { |
| 750 | Melder_throw (U"TextGrid not read from CGN syntax file ", file, U"."); |
| 751 | } |
| 752 | } |
| 753 | |
| 754 | const struct TIMIT_key { |
| 755 | const char *timitLabel, *ipaLabel; |
| 756 | } TIMIT_toIpaTable [] = { |
| 757 | {"", ""}, |
| 758 | /* Vowels */ |
| 759 | {"iy", "i"}, /* beet: bcl b IY tcl t */ |
| 760 | {"ih", "\\ic"}, /* bit: bcl b IH tcl t */ |
| 761 | /* 20190704 wgmichener "\\ep" -> "\\ef" */ |
| 762 | {"eh", "\\ef"}, /* bet: bcl b EH tcl t */ |
| 763 | {"ey", "e"}, /* bait: bcl b EY tcl t */ |
| 764 | {"ae", "\\ae"}, /* bat: bcl b AE tcl t */ |
| 765 | {"aa", "\\as"}, /* bott: bcl b AA tcl t */ |
| 766 | {"aw", "a\\hs"}, /* bout: bcl b AW tcl t */ |
| 767 | {"ay", "a\\ic"}, /* bite: bcl b AY tcl t */ |
| 768 | {"ah", "\\vt"}, /* but: bcl b AH tcl t */ |
| 769 | {"ao", "\\ct"}, /* bought: bcl b AO tcl t */ |
| 770 | {"oy", "\\ct\\ic"}, /* boy: bcl b OY */ |
| 771 | {"ow", "o"}, /* boat: bcl b OW tcl t */ |
| 772 | {"uh", "\\hs"}, /* book: bcl b UH tcl t */ |
| 773 | {"uw", "u"}, /* boot: bcl b UW tcl t */ |
| 774 | /* fronted allophone of uw (alveolar contexts) */ |
| 775 | /* 20190704 wgmichener "\\u\"" -> "\\u\\:^" */ |
| 776 | /* 20250422 ppgb "\\u\\:^" -> "u\\:^" (as in Zue & Seneff 1988: "Transcription and alignment of the Timit database") */ |
| 777 | /* 20250426 ppgb "u\\:^" -> "\\u-" (turn Americanist into IPA (could also have been [y]) */ |
| 778 | {"ux", "\\u-"}, /* toot: tcl t UX tcl t */ |
| 779 | {"er", "\\er\\hr"}, /* bird: bcl b ER dcl d */ |
| 780 | {"ax", "\\sw"}, /* about: AX bcl b aw tcl t */ |
| 781 | {"ix", "\\i-"}, /* debit: dcl d eh bcl b IX tcl t */ |
| 782 | {"axr", "\\sr"}, /* butter: bcl ah dx AXR */ |
| 783 | /* devoiced schwa, very short */ |
| 784 | /* 20190704 wgmichener "\\sw\\ov"" -> "\\sw\\0v" */ |
| 785 | {"ax-h", "\\sw\\0v"}, /* suspect: s AX-H s pcl p eh kcl k tcl t */ |
| 786 | /* Semivowels and glides */ |
| 787 | {"l", "l"}, /* lay: L ey */ |
| 788 | /* 20190704 wgmichener "r" -> "\\rt" */ |
| 789 | {"r", "\\rt"}, /* ray: R ey */ |
| 790 | {"w", "w"}, /* way: w ey */ |
| 791 | {"y", "j"}, /* yacht: Y aa tcl t */ |
| 792 | {"hh", "h" }, /* hay: HH ey*/ |
| 793 | /* voiced allophone of h */ |
| 794 | /* 20190704 wgmichener "\\hv" -> "\\h^" */ |
| 795 | {"hv", "\\h^"}, /* ahead: ax HV eh dcl d */ |
| 796 | {"el", "l\\|v"}, /* bottle: bcl b aa tcl t EL */ |
| 797 | /* Nasals */ |
| 798 | {"m", "m"}, /* mom: M aa M */ |
| 799 | {"n", "n"}, /* noon: N uw N*/ |
| 800 | {"ng", "\\ng"}, /* sing: s ih NG */ |
| 801 | {"em", "m\\|v"}, /* bottom: b aa tcl t EM */ |
| 802 | {"en", "n\\|v"}, /* button: b ah q EN */ |
| 803 | {"eng", "\\ng\\|v"}, /* washington: w aa sh ENG tcl t ax n */ |
| 804 | /* nasal flap */ |
| 805 | /* 20190704 wgmichener "n^\\fh" -> "\\fh\\~^" */ |
| 806 | {"nx", "\\fh\\~^"}, /* winner: wih NX axr */ |
| 807 | /* Fricatives */ |
| 808 | {"s", "s"}, /* sea: S iy */ |
| 809 | {"sh", "\\sh"}, /* she: SH iy */ |
| 810 | {"z", "z"}, /* zone: Z ow n */ |
| 811 | {"zh", "\\zh"}, /* azure: ae ZH er */ |
| 812 | {"f", "f"}, /* fin: F ih n */ |
| 813 | /* 20190704 wgmichener "\\te" -> "\\tf" */ |
| 814 | {"th", "\\tf"}, /* thin: TH ih n */ |
| 815 | {"v", "v"}, /* van: v ae n */ |
| 816 | {"dh", "\\dh"}, /* then: DH en */ |
| 817 | /* Affricates */ |
| 818 | {"jh", "d\\zh"}, /* joke: DCL JH ow kcl k */ |
| 819 | {"ch", "t\\sh"}, /* choke TCL CH ow kcl k */ |
| 820 | /* Stops */ |
| 821 | {"b", "b"}, /* bee: BCL B iy */ |
| 822 | {"d", "d"}, /* day: DCL D ey */ |
| 823 | {"g", "g"}, /* gay: GCL G ey */ |
| 824 | {"p", "p"}, /* pea: PCL P iy */ |
| 825 | {"t", "t"}, /* tea: TCL T iy */ |
| 826 | {"k", "k"}, /* key: KCL K iy */ |
| 827 | /* 20140315: Added silences before the burst */ |
| 828 | /* 20190704 wgmichener "" -> "x\\cn" */ |
| 829 | {"bcl", "b\\cn"}, |
| 830 | {"dcl", "d\\cn"}, |
| 831 | {"gcl", "g\\cn"}, |
| 832 | {"pcl", "p\\cn"}, |
| 833 | {"tcl", "t\\cn"}, |
| 834 | {"kcl", "k\\cn"}, |
| 835 | /* flap */ |
| 836 | {"dx", "\\fh"}, /* muddy: m ah DX iy & dirty: dcl d er DX iy */ |
| 837 | /* glottal stop */ |
| 838 | /* 20190704 wgmichener "?" -> "\\?g" */ |
| 839 | {"q", "\\?g"}, |
| 840 | /* Others */ |
| 841 | {"pau", "(pau)"}, /* pause */ |
| 842 | {"epi", "(epi)"}, /* epenthetic silence */ |
| 843 | {"h#", "(h#)"}, /* marks start and end piece of sentence */ |
| 844 | /* the following markers occur only in the dictionary */ |
| 845 | /* 20190704 wgmichener "1" -> "\\'1" */ |
| 846 | {"1", "\\'1"}, /* primary stress marker */ |
| 847 | /* 20190704 wgmichener "2" -> "\\'2" */ |
| 848 | {"2", "\\'2"} /* secondary stress marker */ |
| 849 | }; |
| 850 | |
| 851 | constexpr integer TIMIT_NLABELS = ((integer) sizeof TIMIT_toIpaTable / (integer) sizeof TIMIT_toIpaTable [1] - 1); |
| 852 | static const char *TIMIT_DELIMITER = "h#"; |
| 853 | |
| 854 | static const char *timitLabelToIpaLabel (const char timitLabel []) { |
| 855 | for (integer i = 1; i <= TIMIT_NLABELS; i ++) |
| 856 | if (! strcmp (TIMIT_toIpaTable [i].timitLabel, timitLabel)) |
| 857 | return TIMIT_toIpaTable [i].ipaLabel; |
| 858 | return timitLabel; |
| 859 | } |
| 860 | |
| 861 | static bool isTimitPhoneticLabel (const char label []) { |
| 862 | for (integer i = 1; i <= TIMIT_NLABELS; i ++) |
| 863 | if (! strcmp (TIMIT_toIpaTable [i].timitLabel, label)) |
| 864 | return true; |
| 865 | return false; |
| 866 | } |
| 867 | |
| 868 | static bool isTimitWord (const char label []) { |
| 869 | const char *p = label; |
| 870 | for (; *p; p++) |
| 871 | if (Melder_isUpperCaseLetter (*p)) |
| 872 | return false; |
| 873 | return true; |
| 874 | } |
| 875 | |
| 876 | autoDaata TextGrid_TIMITLabelFileRecognizer (integer nread, const char *header, MelderFile file) { |
| 877 | char hkruis [3] = "h#", label1 [512], label2 [512]; |
| 878 | int length; |
| 879 | bool phnFile = false; |
| 880 | integer it [5]; |
| 881 | if (nread < 12 || sscanf (header, "%td%td%511s%n\n", & it [1], & it [2], label1, & length) != 3 || |
| 882 | it [1] < 0 || it [2] <= it [1] || sscanf (& header [length], "%td%td%511s\n", & it [3], & it [4], label2) != 3 || |
| 883 | it [4] <= it [3]) { |
| 884 | /* |
| 885 | 20120512 djmw removed the extra "it [3] < it [2]" check, because otherwise train/dr7/mdlm0/si1864.wrd cannot be read |
| 886 | */ |
| 887 | return autoDaata (); |
| 888 | } |
| 889 | if (! strcmp (label1, hkruis)) { |
| 890 | if (isTimitPhoneticLabel (label2)) |
| 891 | phnFile = true; |
| 892 | else if (! isTimitWord (label2)) |
| 893 | return autoDaata (); |
| 894 | } else if (! isTimitWord (label1) || ! isTimitWord (label2)) { |
| 895 | return autoDaata (); |
| 896 | } |
| 897 | autoTextGrid thee = TextGrid_readFromTimitLabelFile (file, phnFile); |
| 898 | return thee.move(); |
| 899 | } |
| 900 | |
| 901 | autoIntervalTier IntervalTier_readFromTimitLabelFile ( |
| 902 | const MelderFile file, |
| 903 | const bool hasPhones |
| 904 | ) { |
| 905 | try { |
| 906 | autoMelderReadText text = MelderReadText_createFromFile (file); // going to be UTF-8-compatible |
| 907 | constexpr double fixedTimitSamplingFrequency = 16000.0; // Hz |
| 908 | constexpr double fixedSamplingPeriod = 1.0 / fixedTimitSamplingFrequency; |
| 909 | autoMelderString label, swallowedWord; |
| 910 | autoIntervalTier me = IntervalTier_create_raw (+1e308, -1e308); |
| 911 | Melder_assert (my intervals.size == 0); |
| 912 | |
| 913 | /* mutable step */ integer previousStartingSample = -2, previousEndSample = -1; |
| 914 | for (;;) { |
| 915 | /* mutable scan */ conststring32 line = MelderReadText_readLine (text.get()); |
| 916 | if (! line) |
| 917 | break; |
| 918 | |
| 919 | /* |
| 920 | Read and check starting sample. |
| 921 | */ |
| 922 | const integer startingSample = Melder_readInteger (& line); |
| 923 | if (my intervals.size == 0 && startingSample != 0) { |
| 924 | Melder_require (startingSample > 0, |
| 925 | U"The annotation should not start at a negative sample number."); |
| 926 | Melder_assert (startingSample > 0); |
| 927 | (void) IntervalTier_addInterval_raw (me.get(), 0.0, startingSample * fixedSamplingPeriod, U""); |
| 928 | previousStartingSample = 0; |
| 929 | previousEndSample = startingSample; |
| 930 | } |
| 931 | Melder_require (startingSample >= previousStartingSample, |
| 932 | U"The starting sample in line ", MelderReadText_getLineNumber (text.get()) - 1, |
| 933 | U" should be at least the starting sample of the previous line, but ", |
| 934 | startingSample, U" is less than ", previousStartingSample, U"." |
| 935 | ); |
| 936 | {// scope |
| 937 | const char32 shouldBeHorizontalSpace = * line ++; |
| 938 | Melder_require (Melder_isHorizontalSpace (shouldBeHorizontalSpace), |
| 939 | U"There should be a space after the starting sample in line ", MelderReadText_getLineNumber (text.get()) - 1, U"."); |
| 940 | } |
| 941 | |
| 942 | /* |
| 943 | Read and check end sample. |
| 944 | */ |
| 945 | Melder_skipHorizontalSpace (& line); |
| 946 | const integer endSample = Melder_readInteger (& line); |
| 947 | Melder_require (endSample >= startingSample, |
| 948 | U"End sample should be at least starting sample in line ", MelderReadText_getLineNumber (text.get()), |
| 949 | U" but ", endSample, U" is less than ", startingSample, U"." |
| 950 | ); |
| 951 | Melder_require (endSample >= previousEndSample, |
| 952 | U"The end sample in line ", MelderReadText_getLineNumber (text.get()) - 1, |
| 953 | U" should be at least the end sample of the previous line, but ", |
| 954 | endSample, U" is less than ", previousEndSample, U"." |
| 955 | ); |
| 956 | {// scope |
| 957 | const char32 shouldBeHorizontalSpace = * line ++; |
| 958 | Melder_require (Melder_isHorizontalSpace (shouldBeHorizontalSpace), |
| 959 | U"There should be a space after the end sample in line ", MelderReadText_getLineNumber (text.get()) - 1, U"."); |
| 960 | } |
| 961 | |
| 962 | /* |
| 963 | Read and check label text. |
| 964 | */ |
| 965 | Melder_skipHorizontalSpace (& line); |
| 966 | MelderString_copy (& label, line); |
| 967 | if (endSample == startingSample) { |
| 968 | if (hasPhones) { |
| 969 | Melder_throw (U"Zero-duration phone found at line ", MelderReadText_getLineNumber (text.get()), U"."); |
| 970 | } else { |
| 971 | /* |
| 972 | For the following .wrd files this occurs once per file: |
| 973 | train/dr3/makr0/si1982 |
| 974 | train/dr3/mhjb0/sa2 |
| 975 | train/dr5/mmcc0/sx348 |
| 976 | train/dr6/meal0/sa2 |
| 977 | train/dr6/mesj0/si2039 |
| 978 | train/dr7/fmkc0/sx352 |
| 979 | train/dr7/mrem0/sx61 |
| 980 | train/dr7/mvrw0/sx315 |
| 981 | test/dr6/mjfc0/sx138 |
| 982 | */ |
| 983 | MelderString_copy (& swallowedWord, line); |
| 984 | continue; |
| 985 | } |
| 986 | } else { |
| 987 | /* mutable fix */ double startingTime = startingSample * fixedSamplingPeriod; |
| 988 | const double endTime = endSample * fixedSamplingPeriod; |
| 989 | if (my intervals.size > 0 && startingSample != previousEndSample) { |
| 990 | /* |
| 991 | Overlapping intervals. |
| 992 | */ |
| 993 | if (hasPhones) { |
| 994 | /* |
| 995 | Overlapping phones. Should not occur. |
| 996 | */ |
| 997 | Melder_throw (U"Overlapping phones around line ", MelderReadText_getLineNumber (text.get()), |
| 998 | U": previous end sample ", previousEndSample, U", current starting sample ", startingSample, U"."); |
| 999 | } else if (startingSample < previousEndSample){ |
| 1000 | /* |
| 1001 | Overlapping words. Put the boundary in the middle (the boundary will fall within a phone). |
| 1002 | */ |
| 1003 | const double previousEndTime = previousEndSample * fixedSamplingPeriod; |
| 1004 | startingTime = 0.5 * (startingTime + previousEndTime); |
| 1005 | TextInterval previousInterval = my intervals.at [my intervals.size]; |
| 1006 | previousInterval -> xmax = startingTime; |
| 1007 | Melder_assert (previousInterval -> xmin < previousInterval -> xmax); |
| 1008 | } else { |
| 1009 | /* |
| 1010 | A gap between words. Fill with an empty interval. |
| 1011 | */ |
| 1012 | (void) IntervalTier_addInterval_raw (me.get(), previousEndSample * fixedSamplingPeriod, startingTime, U""); |
| 1013 | } |
| 1014 | } |
| 1015 | (void) IntervalTier_addInterval_raw (me.get(), startingTime, endTime, |
| 1016 | swallowedWord.length > 0 ? Melder_cat (swallowedWord.string, U"+", line) : line); |
| 1017 | MelderString_empty (& swallowedWord); |
| 1018 | } |
| 1019 | previousStartingSample = startingSample; |
| 1020 | previousEndSample = endSample; |
| 1021 | } |
| 1022 | Melder_require (my intervals.size >= 1, |
| 1023 | U"The file should contain at least one interval, but none were found."); |
| 1024 | return me; |
| 1025 | } catch (MelderError) { |
| 1026 | Melder_throw (U"Interval tier not read from file ", file, U"."); |
| 1027 | } |
| 1028 | } |
| 1029 | |
| 1030 | autoTextGrid TextGrid_readFromTimitLabelFile ( |
| 1031 | const MelderFile file, |
| 1032 | const bool hasPhones |
| 1033 | ) { |
| 1034 | try { |
| 1035 | autoTextGrid result = TextGrid_createWithoutTiers (+1e308, -1e308); |
| 1036 | autoIntervalTier tier = IntervalTier_readFromTimitLabelFile (file, hasPhones); |
| 1037 | if (hasPhones) { |
| 1038 | autoIntervalTier ipa = Data_copy (tier.get()); |
| 1039 | for (integer iinterval = 1; iinterval <= ipa -> intervals.size; iinterval ++) { |
| 1040 | const mutableTextInterval interval = ipa -> intervals.at [iinterval]; |
| 1041 | TextInterval_setText (ipa -> intervals.at [iinterval], |
| 1042 | Melder_peek8to32_u (timitLabelToIpaLabel (Melder_peek32to8 (interval -> text.get())))); |
| 1043 | } |
| 1044 | TextGrid_addTier_move (result.get(), ipa.move()); |
| 1045 | } |
| 1046 | TextGrid_addTier_move (result.get(), tier.move()); |
| 1047 | return result; |
| 1048 | } catch (MelderError) { |
| 1049 | Melder_throw (U"TextGrid not read from file ", file, U"."); |
| 1050 | } |
| 1051 | } |
| 1052 | |
| 1053 | /* End of file TextGrid_files.cpp */ |