upstream version 1.2.2
[debian/freetts] / com / sun / speech / freetts / util / SegmentRelationUtils.java
1 /**
2  * Portions Copyright 2001 Sun Microsystems, Inc.
3  * Portions Copyright 1999-2001 Language Technologies Institute, 
4  * Carnegie Mellon University.
5  * All Rights Reserved.  Use is subject to license terms.
6  * 
7  * See the file "license.terms" for information on usage and
8  * redistribution of this file, and for a DISCLAIMER OF ALL 
9  * WARRANTIES.
10  */
11 package com.sun.speech.freetts.util;
12
13 import com.sun.speech.freetts.FeatureSet;
14 import com.sun.speech.freetts.Item;
15 import com.sun.speech.freetts.Relation;
16
17
18 /**
19  * Provides a set of utilities for the SegmentRelation. A 
20  * SegmentRelation is a Relation, but has
21  * features specific to Segments.
22  */
23 public class SegmentRelationUtils {
24
25     /**
26      * Returns the Item in the Segment Relation that corresponds to the given
27      * time.
28      *
29      * @param segmentRelation the segmentRelation of interest
30      * @param time the time
31      */
32     public static Item getItem(Relation segmentRelation, float time) {
33
34         Item lastSegment = segmentRelation.getTail();
35         
36         // if given time is closer to the front than the end, search from
37         // the front; otherwise, start search from end
38         // this might not be the best strategy though
39
40         float lastSegmentEndTime = SegmentRelationUtils.getSegmentEnd
41             (lastSegment);
42         
43         if (time < 0 || lastSegmentEndTime < time) {
44             return null;
45         } else if (lastSegmentEndTime - time > time) {
46             return SegmentRelationUtils.findFromFront(segmentRelation, time);
47         } else {
48             return SegmentRelationUtils.findFromEnd(segmentRelation, time);
49         }
50     }
51
52     /**
53      * Returns the value of the feature <code>end</code> of
54      * the given Segment Item.
55      *
56      * @param segment the Segment Item
57      *
58      * @return the <code>end</code> feature of the Segment
59      */
60     public static float getSegmentEnd(Item segment) {
61         FeatureSet segmentFeatureSet = segment.getFeatures();
62         return segmentFeatureSet.getFloat("end");
63     }
64     
65     /**
66      * Starting from the front of the given Segment Relation, finds the Item
67      * that corresponds to the given time.
68      *
69      * @param segmentRelation the Segment Relation to search
70      * @param time the time of the Segment Item
71      *
72      * @return the Segment Item
73      */
74     public static Item findFromFront(Relation segmentRelation, float time) {
75         Item item = segmentRelation.getHead();
76
77         while (item != null &&
78                time > SegmentRelationUtils.getSegmentEnd(item)) {
79             item = item.getNext();
80         }
81
82         return item;
83     }
84     
85     /**
86      * Starting from the end of the given Segment Relation, go backwards
87      * to find the Item that corresponds to the given time.
88      *
89      * @param segmentRelation the Segment Relation to search
90      * @param time the time of the Segment Item
91      *
92      * @return the Segment Item
93      */
94     public static Item findFromEnd(Relation segmentRelation, float time) {
95         Item item = segmentRelation.getTail();
96                 
97         while (item != null &&
98                SegmentRelationUtils.getSegmentEnd(item) > time) {
99             item = item.getPrevious();
100         }
101
102         if (item != segmentRelation.getTail()) {
103             item = item.getNext();
104         }
105
106         return item;
107     }
108 }