TextGrid_and_DurationTier.cpp 4.0 KB

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