1 import java.io.BufferedReader;
3 import java.io.FileInputStream;
4 import java.io.FileOutputStream;
5 import java.io.InputStreamReader;
6 import java.io.IOException;
7 import java.io.PrintStream;
9 import java.util.ArrayList;
10 import java.util.HashMap;
11 import java.util.Iterator;
12 import java.util.TreeSet;
15 * Database containing both the Catalog and the Track Files.
17 public class UnitDatabase {
19 static float lpcRange;
21 static float mcepRange;
23 UnitCatalog unitCatalog;
28 * Creates a new UnitDatabase.
30 * @param unitCatalog the unit unitCatalog
31 * @param sts Track data from individual sts files, indexed by filename
32 * @param mcep Track data from individual mcep files, indexed by filename
34 public UnitDatabase(UnitCatalog unitCatalog, HashMap sts, HashMap mcep) {
35 this.unitCatalog = unitCatalog;
41 * Dumps Catalog to stdout.
43 void dumpUnitCatalog(PrintStream out) {
44 /* Sort the keys (which are the unit types)
46 Iterator keys = new TreeSet(unitCatalog.keySet()).iterator();
50 while (keys.hasNext()) {
51 String key = (String) keys.next();
52 ArrayList units = (ArrayList) unitCatalog.get(key);
53 out.println("UNIT_TYPE " + key
55 + " " + units.size());
56 currentIndex += units.size();
60 public void dumpUnitCatalog(String filename) throws IOException {
61 PrintStream out = new PrintStream(new FileOutputStream(filename));
67 * Gets the LPC metadata
69 static void getLPCParams() throws IOException {
70 BufferedReader reader =
72 new InputStreamReader(
73 new FileInputStream("lpc/lpc.params")));
74 String line = reader.readLine();
75 while (line != null) {
76 if (line.startsWith("LPC_MIN=")) {
77 lpcMin = Float.parseFloat(line.substring(8));
78 } else if (line.startsWith("LPC_RANGE=")) {
79 lpcRange = Float.parseFloat(line.substring(10));
81 line = reader.readLine();
87 * Gets the MCEP metadata
89 static void getMCEPParams() throws IOException {
90 BufferedReader reader =
92 new InputStreamReader(
93 new FileInputStream("mcep/mcep.params")));
94 String line = reader.readLine();
95 while (line != null) {
96 if (line.startsWith("MCEP_MIN=")) {
97 mcepMin = Float.parseFloat(line.substring(9));
98 } else if (line.startsWith("MCEP_RANGE=")) {
99 mcepRange = Float.parseFloat(line.substring(11));
101 line = reader.readLine();
107 * Dumps the sts and mcep data.
109 void dumpVoiceData(PrintStream stsOut, PrintStream mcepOut)
112 int numLPCChannels = 0;
113 int numMCEPChannels = 0;
115 /* Sort the keys (which are the filenames)
117 Iterator keys = new TreeSet(sts.keySet()).iterator();
120 while (keys.hasNext()) {
121 String filename = (String) keys.next();
122 Track track = (Track) sts.get(filename);
123 sampleRate = track.sampleRate;
124 numLPCChannels = track.numChannels;
125 numFrames += track.numFrames;
127 track = (Track) mcep.get(filename);
128 numMCEPChannels = track.numChannels;
131 /* [[[WDW FIXME: has hardcoded data.]]]
133 stsOut.println("STS STS " + numFrames
134 + " " + numLPCChannels
136 + " " + lpcMin + " " + lpcRange
137 + " 0.000000 1"); /* postEmph and residualFold */
139 mcepOut.println("STS MCEP " + numFrames
140 + " " + numMCEPChannels
142 + " " + mcepMin + " " + mcepRange
143 + " 0.000000 1"); /* postEmph and residualFold */
145 keys = new TreeSet(sts.keySet()).iterator();
146 int currentIndex = 0;
147 while (keys.hasNext()) {
148 String filename = (String) keys.next();
150 Track track = (Track) sts.get(filename);
151 track.startIndex = currentIndex;
152 track.dumpData(stsOut);
154 track = (Track) mcep.get(filename);
155 track.startIndex = currentIndex;
156 track.dumpData(mcepOut);
158 currentIndex += track.numFrames;
163 * Dumps the unit index.
165 public void dumpUnitIndex(PrintStream unitIndexOut,
167 PrintStream mcepOut) throws IOException {
169 System.out.println(" Dumping STS and MCEP tracks");
170 dumpVoiceData(stsOut, mcepOut);
172 System.out.println(" Dumping unit index");
174 /* Sort the keys (which are the unit_types)
176 Iterator keys = new TreeSet(unitCatalog.keySet()).iterator();
178 int unitTypeIndex = 0;
179 int phoneNumber = 0; /* just to guarantee some difference */
181 while (keys.hasNext()) {
182 String unitType = (String) keys.next();
183 ArrayList units = (ArrayList) unitCatalog.get(unitType);
185 for (int i = 0; i < units.size(); i++) {
186 Unit unit = (Unit) units.get(i);
187 Track track = (Track) sts.get(unit.filename);
188 int startIndex = track.findTrackFrameIndex(unit.start);
189 int endIndex = track.findTrackFrameIndex(unit.end);
190 unitIndexOut.println(
191 "UNITS " + unitTypeIndex
193 + " " + (startIndex + track.startIndex)
194 + " " + (endIndex + track.startIndex)
196 + ((unit.previous != null) ? unit.previous.index : 65535)
198 + ((unit.next != null) ? unit.next.index : 65535));
203 + ((unit.previous != null)
204 ? unit.previous.toString()
210 + ((unit.next != null)
211 ? unit.next.toString()
220 public void dumpUnitIndex(String unitIndexFilename,
222 String mcepFilename) throws IOException {
223 PrintStream unitIndexOut = new PrintStream(
224 new FileOutputStream(unitIndexFilename));
225 PrintStream stsOut = new PrintStream(
226 new FileOutputStream(stsFilename));
227 PrintStream mcepOut = new PrintStream(
228 new FileOutputStream(mcepFilename));
230 dumpUnitIndex(unitIndexOut, stsOut, mcepOut);
232 unitIndexOut.close();
238 * Testing. args[0] = *.catalog file
239 * args[1..n] = * files (no path or suffix - the code
240 * will add sts/file.sts and
243 static public void main(String[] args) {
245 System.out.println("Reading " + args[0]);
246 UnitCatalog unitCatalog = new UnitCatalog(args[0]);
248 /* Store the TrackFile in the sts and mcep HashMaps
249 * indexed by the filename.
251 System.out.println("Reading STS and MCEP files");
255 HashMap sts = new HashMap();
256 HashMap mcep = new HashMap();
257 for (int i = 1; i < args.length; i++) {
260 new Track("sts/" + args[i] + ".sts",
264 new Track("mcep/" + args[i] + ".mcep.txt",
270 UnitDatabase database = new UnitDatabase(unitCatalog, sts, mcep);
272 System.out.println("Creating FreeTTS/unit_catalog");
273 database.dumpUnitCatalog("FreeTTS/unit_catalog.txt");
276 "Creating FreeTTS/unit_index.txt, FreeTTS/sts.txt, and "
277 + "FreeTTS/mcep.txt");
279 database.dumpUnitIndex("FreeTTS/unit_index.txt",
283 System.out.println("Done!");
284 } catch (Exception e) {