upstream version 1.2.2
[debian/freetts] / com / sun / speech / freetts / diphone / AliasDiphone.java
1 /*\r
2  * Copyright (C) 2005 DFKI GmbH. All rights reserved.\r
3  */\r
4 package com.sun.speech.freetts.diphone;\r
5 \r
6 import java.io.DataOutputStream;\r
7 import java.io.IOException;\r
8 import java.nio.ByteBuffer;\r
9 \r
10 import com.sun.speech.freetts.relp.Sample;\r
11 \r
12 /**\r
13  * Represent an alias diphone which is just another name for an\r
14  * already-existing original diphone. This can be used just like a\r
15  * "real" diphone. \r
16  * @author Marc Schröder\r
17  *\r
18  */\r
19 public class AliasDiphone extends Diphone\r
20 {\r
21     private String originalName;\r
22     private Diphone original;\r
23     /**\r
24      * @param name The alias name to use for the existing diphone\r
25      * @param originalName the original name of the diphone.\r
26      */\r
27     public AliasDiphone(String name, String originalName)\r
28     {\r
29         super(name);\r
30         this.originalName = originalName;\r
31     }\r
32 \r
33     /**\r
34      * Get the name of the original name that this alias points to.\r
35      */\r
36     public String getOriginalName()\r
37     {\r
38         return originalName;\r
39     }\r
40     \r
41     /**\r
42      * Associate the actual diphone object of the original with this alias.\r
43      * @param original a diphone object whose getName() must return the same\r
44      * as our getOriginalName().\r
45      * @throws IllegalArgumentException if the diphone to be registered as the\r
46      * original has a name which is different from this AliasDiphone's original name\r
47      * as returned by getOriginalName().\r
48      */\r
49     public void setOriginalDiphone(Diphone original)\r
50     {\r
51         if (!originalName.equals(original.getName())) {\r
52             throw new IllegalArgumentException("The diphone to register ("+original.getName()\r
53                     +") does not match the original name ("+originalName+")");\r
54         }\r
55         this.original = original;\r
56     }\r
57     \r
58     /**\r
59      * Returns the samples associated with this diphone.\r
60      *\r
61      * @return the samples associated with this diphone\r
62      */\r
63     public Sample[] getSamples() {\r
64     return original.getSamples();\r
65     }\r
66 \r
67     /**\r
68      * Returns a particular sample.\r
69      *\r
70      * @param which which sample to return\r
71      *\r
72      * @return the desired sample\r
73      */\r
74     public Sample getSamples(int which) {\r
75     return original.getSamples(which);\r
76     }\r
77 \r
78     /**\r
79      * Returns the midpoint index. the midpoint index is the sample\r
80      * that divides the diphone into the first and second parts.\r
81      *\r
82      * @return the midpoint index.\r
83      */\r
84     public int getMidPoint() {\r
85     return original.getMidPoint();\r
86     }\r
87     \r
88     /**\r
89      * Returns the midpoint index. the midpoint index is the sample\r
90      * that divides the diphone into the first and second parts.\r
91      *\r
92      * @return the midpoint index.\r
93      */\r
94     public int getPbPositionMillis() {\r
95     return original.getPbPositionMillis();\r
96     }\r
97 \r
98     /**\r
99      * Returns the sample that is closest to uIndex.\r
100      *\r
101      * @param uIndex the desired index\r
102      * @param unitPart do we want the first have (1) or the second\r
103      *      half (2)\r
104      *\r
105      * @return the sample nearest to the given index in the given\r
106      *      part\r
107      */ \r
108     public Sample nearestSample(float uIndex, int unitPart) {\r
109         return original.nearestSample(uIndex, unitPart);\r
110     }\r
111 \r
112     /**\r
113      * Returns the total number of residuals in the given part for this\r
114      * diphone.\r
115      *\r
116      * @param unitPart indicates which part is of interest (1 or 2)\r
117      *\r
118      * @return the number of residuals in the specified part\r
119      */\r
120     public int getUnitSize(int unitPart) {\r
121         return original.getUnitSize(unitPart);\r
122     }\r
123 \r
124     /**\r
125      * dumps out this Diphone.\r
126      */\r
127     public void dump() {\r
128     System.out.println("AliasDiphone: " + getName() + " aliased to " + original.getName());\r
129     }\r
130 \r
131     /**\r
132      * Dumps the diphone to the given channel.\r
133      *\r
134      * @param bb the ByteBuffer to write to\r
135      *\r
136      * @throws IOException if IO error occurs\r
137      */\r
138     public void dumpBinary(ByteBuffer bb) throws IOException {\r
139         char[] nameArray = (getName() + "        ").toCharArray();\r
140         char[] origNameArray = (original.getName() + "        ").toCharArray();\r
141 \r
142         bb.putInt(ALIAS_MAGIC);\r
143         for (int i = 0; i < NAME_LENGTH; i++) {\r
144             bb.putChar(nameArray[i]);\r
145         }\r
146         for (int i = 0; i < NAME_LENGTH; i++) {\r
147             bb.putChar(origNameArray[i]);\r
148         }\r
149     }\r
150 \r
151     /**\r
152      * Dumps the diphone to the given channel.\r
153      *\r
154      * @param os the DataOutputStream to write to\r
155      *\r
156      * @throws IOException if IO error occurs\r
157      */\r
158     public void dumpBinary(DataOutputStream os) throws IOException {\r
159         char[] nameArray = (getName() + "        ").toCharArray();\r
160         char[] origNameArray = (original.getName() + "        ").toCharArray();\r
161 \r
162         os.writeInt(ALIAS_MAGIC);\r
163         for (int i = 0; i < NAME_LENGTH; i++) {\r
164             os.writeChar(nameArray[i]);\r
165         }\r
166         for (int i = 0; i < NAME_LENGTH; i++) {\r
167             os.writeChar(origNameArray[i]);\r
168         }\r
169     }\r
170 \r
171     /**\r
172      * Determines if the two diphones are equivalent. This is for\r
173      * testing databases. This is not the same as "equals".\r
174      * For aliased diphones, this will return true if the compare() method\r
175      * of the original diphone returns true, even if the name used in this alias \r
176      * is different.\r
177      *\r
178      * @param other the diphone to compare this one to\r
179      *\r
180      * @return <code>true</code> if the diphones match; otherwise\r
181      *      <code>false</code> \r
182      */\r
183     boolean compare(Diphone other) {\r
184         return original.compare(other);\r
185     }\r
186 \r
187 }\r