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