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.
7 * See the file "license.terms" for information on usage and
8 * redistribution of this file, and for a DISCLAIMER OF ALL
11 package com.sun.speech.freetts.util;
13 import com.sun.speech.freetts.FeatureSet;
14 import com.sun.speech.freetts.Item;
15 import com.sun.speech.freetts.Relation;
19 * Provides a set of utilities for the SegmentRelation. A
20 * SegmentRelation is a Relation, but has
21 * features specific to Segments.
23 public class SegmentRelationUtils {
26 * Returns the Item in the Segment Relation that corresponds to the given
29 * @param segmentRelation the segmentRelation of interest
30 * @param time the time
32 public static Item getItem(Relation segmentRelation, float time) {
34 Item lastSegment = segmentRelation.getTail();
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
40 float lastSegmentEndTime = SegmentRelationUtils.getSegmentEnd
43 if (time < 0 || lastSegmentEndTime < time) {
45 } else if (lastSegmentEndTime - time > time) {
46 return SegmentRelationUtils.findFromFront(segmentRelation, time);
48 return SegmentRelationUtils.findFromEnd(segmentRelation, time);
53 * Returns the value of the feature <code>end</code> of
54 * the given Segment Item.
56 * @param segment the Segment Item
58 * @return the <code>end</code> feature of the Segment
60 public static float getSegmentEnd(Item segment) {
61 FeatureSet segmentFeatureSet = segment.getFeatures();
62 return segmentFeatureSet.getFloat("end");
66 * Starting from the front of the given Segment Relation, finds the Item
67 * that corresponds to the given time.
69 * @param segmentRelation the Segment Relation to search
70 * @param time the time of the Segment Item
72 * @return the Segment Item
74 public static Item findFromFront(Relation segmentRelation, float time) {
75 Item item = segmentRelation.getHead();
77 while (item != null &&
78 time > SegmentRelationUtils.getSegmentEnd(item)) {
79 item = item.getNext();
86 * Starting from the end of the given Segment Relation, go backwards
87 * to find the Item that corresponds to the given time.
89 * @param segmentRelation the Segment Relation to search
90 * @param time the time of the Segment Item
92 * @return the Segment Item
94 public static Item findFromEnd(Relation segmentRelation, float time) {
95 Item item = segmentRelation.getTail();
97 while (item != null &&
98 SegmentRelationUtils.getSegmentEnd(item) > time) {
99 item = item.getPrevious();
102 if (item != segmentRelation.getTail()) {
103 item = item.getNext();