Label.cpp 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /* Label.cpp
  2. *
  3. * Copyright (C) 1992-2007,2011,2012,2015-2018 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. #include "Label.h"
  19. Thing_implement (Autosegment, Function, 0);
  20. void structAutosegment :: v_copy (Daata thee_Daata) {
  21. Autosegment thee = static_cast <Autosegment> (thee_Daata);
  22. Autosegment_Parent :: v_copy (thee);
  23. if (name) Thing_setName (thee, name.get());
  24. }
  25. bool structAutosegment :: v_equal (Daata thee_Daata) {
  26. Autosegment thee = static_cast <Autosegment> (thee_Daata);
  27. if (! Autosegment_Parent :: v_equal (thee)) return false;
  28. if (! our name && ! thy name) return true; // shortcut: no names
  29. if (! our name || ! thy name) return false;
  30. return str32equ (our name.get(), thy name.get());
  31. }
  32. static struct structData_Description theAutosegment_description [] = {
  33. { U"Autosegment", inheritwa, 0, sizeof (struct structAutosegment), U"Autosegment", & theClassInfo_Function, 0, nullptr, nullptr, nullptr, nullptr },
  34. { U"name", stringwa, Melder_offsetof (Autosegment, name), sizeof (char32 *), nullptr, nullptr, 0, nullptr, nullptr, nullptr, nullptr },
  35. { } };
  36. Data_Description structAutosegment :: s_description = & theAutosegment_description [0];
  37. autoAutosegment Autosegment_create (double tmin, double tmax, conststring32 label) {
  38. try {
  39. autoAutosegment me = Thing_new (Autosegment);
  40. Function_init (me.get(), tmin, tmax);
  41. if (label) {
  42. Thing_setName (me.get(), label);
  43. }
  44. return me;
  45. } catch (MelderError) {
  46. Melder_throw (U"Autosegment not created.");
  47. }
  48. }
  49. /********** class Tier **********/
  50. Thing_implement (Tier, Sorted, 0);
  51. integer Tier_timeToIndex (Tier me, double time) {
  52. for (integer i = 1; i <= my size; i ++) {
  53. Autosegment interval = my at [i];
  54. if (time >= interval -> xmin && time < interval -> xmax)
  55. return i;
  56. }
  57. return 0; // empty tier or very large time
  58. }
  59. Thing_implement (Label, Ordered, 0);
  60. void Label_addTier (Label me) {
  61. autoTier tier = Tier_create ();
  62. my addItem_move (tier.move());
  63. }
  64. void Label_suggestDomain (Label me, double *tmin, double *tmax) {
  65. *tmin = 0.0;
  66. *tmax = 0.0;
  67. for (int itier = 1; itier <= my size; itier ++) {
  68. Tier tier = my at [itier];
  69. if (tier->size > 0) {
  70. Autosegment seg = tier->at [1];
  71. if (seg -> xmin <= *tmin) {
  72. if (seg -> name && seg -> name [0])
  73. *tmin = seg -> xmin - 1.0;
  74. else
  75. *tmin = seg -> xmin;
  76. }
  77. seg = tier->at [tier->size];
  78. if (seg -> xmax >= *tmax)
  79. *tmax = seg -> xmax;
  80. }
  81. }
  82. *tmax += 1.0;
  83. }
  84. /* End of file Label.cpp */