📄 clusterunitdatabase.java
字号:
/** * Loads a binary file from the input stream. * * @param is the input stream to read the database from * * @throws IOException if there is trouble opening the DB * */ private void loadBinary(InputStream is) throws IOException { // we get better performance if we can map the file in // 1.0 seconds vs. 1.75 seconds, but we can't // always guarantee that we can do that. if (is instanceof FileInputStream) { FileInputStream fis = (FileInputStream) is; FileChannel fc = fis.getChannel(); MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, (int) fc.size()); bb.load(); loadBinary(bb); is.close(); } else { loadBinary(new DataInputStream(is)); } } /** * Loads the database from the given byte buffer. * * @param bb the byte buffer to load the db from * * @throws IOException if there is trouble opening the DB */ private void loadBinary(ByteBuffer bb) throws IOException { if (bb.getInt() != MAGIC) { throw new Error("Bad magic in db"); } if (bb.getInt() != VERSION) { throw new Error("Bad VERSION in db"); } continuityWeight = bb.getInt(); optimalCoupling = bb.getInt(); extendSelections = bb.getInt(); joinMethod = bb.getInt(); joinWeightShift = bb.getInt(); int weightLength = bb.getInt(); joinWeights = new int[weightLength]; for (int i = 0; i < joinWeights.length; i++) { joinWeights[i] = bb.getInt(); } int unitsLength = bb.getInt(); units = new DatabaseClusterUnit[unitsLength]; for (int i = 0; i < units.length; i++) { units[i] = new DatabaseClusterUnit(bb); } int unitTypesLength = bb.getInt(); unitTypes = new UnitType[unitTypesLength]; for (int i = 0; i < unitTypes.length; i++) { unitTypes[i] = new UnitType(bb); } sts = new SampleSet(bb); mcep = new SampleSet(bb); int numCarts = bb.getInt(); cartMap = new HashMap(); for (int i = 0; i < numCarts; i++) { String name = Utilities.getString(bb); CART cart = CARTImpl.loadBinary(bb); cartMap.put(name, cart); if (defaultCart == null) { defaultCart = cart; } } } /** * Loads the database from the given input stream. * * @param is the input stream to load the db from * * @throws IOException if there is trouble opening the DB */ private void loadBinary(DataInputStream is) throws IOException { if (is.readInt() != MAGIC) { throw new Error("Bad magic in db"); } if (is.readInt() != VERSION) { throw new Error("Bad VERSION in db"); } continuityWeight = is.readInt(); optimalCoupling = is.readInt(); extendSelections = is.readInt(); joinMethod = is.readInt(); joinWeightShift = is.readInt(); int weightLength = is.readInt(); joinWeights = new int[weightLength]; for (int i = 0; i < joinWeights.length; i++) { joinWeights[i] = is.readInt(); } int unitsLength = is.readInt(); units = new DatabaseClusterUnit[unitsLength]; for (int i = 0; i < units.length; i++) { units[i] = new DatabaseClusterUnit(is); } int unitTypesLength = is.readInt(); unitTypes = new UnitType[unitTypesLength]; for (int i = 0; i < unitTypes.length; i++) { unitTypes[i] = new UnitType(is); } sts = new SampleSet(is); mcep = new SampleSet(is); int numCarts = is.readInt(); cartMap = new HashMap(); for (int i = 0; i < numCarts; i++) { String name = Utilities.getString(is); CART cart = CARTImpl.loadBinary(is); cartMap.put(name, cart); if (defaultCart == null) { defaultCart = cart; } } } /** * Dumps a binary form of the database. * * @param path the path to dump the file to */ void dumpBinary(String path) { try { FileOutputStream fos = new FileOutputStream(path); DataOutputStream os = new DataOutputStream(new BufferedOutputStream(fos)); os.writeInt(MAGIC); os.writeInt(VERSION); os.writeInt(continuityWeight); os.writeInt(optimalCoupling); os.writeInt(extendSelections); os.writeInt(joinMethod); os.writeInt(joinWeightShift); os.writeInt(joinWeights.length); for (int i = 0; i < joinWeights.length; i++) { os.writeInt(joinWeights[i]); } os.writeInt(units.length); for (int i = 0; i < units.length; i++) { units[i].dumpBinary(os); } os.writeInt(unitTypes.length); for (int i = 0; i < unitTypes.length; i++) { unitTypes[i].dumpBinary(os); } sts.dumpBinary(os); mcep.dumpBinary(os); os.writeInt(cartMap.size()); for (Iterator i = cartMap.keySet().iterator(); i.hasNext();) { String name = (String) i.next(); CART cart = (CART) cartMap.get(name); Utilities.outString(os, name); cart.dumpBinary(os); } os.close(); // note that we are not currently saving the state // of the default cart } catch (FileNotFoundException fe) { throw new Error("Can't dump binary database " + fe.getMessage()); } catch (IOException ioe) { throw new Error("Can't write binary database " + ioe.getMessage()); } } /** * Determines if two databases are identical. * * @param other the database to compare this one to * * @return true if the databases are identical */ public boolean compare(ClusterUnitDatabase other) { System.out.println("Warning: Compare not implemented yet"); return false; } /** * Manipulates a ClusterUnitDatabase. * * <p> * <b> Usage </b> * <p> * <code> java com.sun.speech.freetts.clunits.ClusterUnitDatabase * [options]</code> * <p> * <b> Options </b> * <p> * <ul> * <li> <code> -src path </code> provides a directory * path to the source text for the database * <li> <code> -dest path </code> provides a directory * for where to place the resulting binaries * <li> <code> -generate_binary [filename]</code> reads * in the text version of the database and generates * the binary version of the database. * <li> <code> -compare </code> Loads the text and * binary versions of the database and compares them to * see if they are equivalent. * <li> <code> -showTimes </code> shows timings for any * loading, comparing or dumping operation * </ul> * */ public static void main(String[] args) { boolean showTimes = false; String srcPath = "."; String destPath = "."; try { if (args.length > 0) { BulkTimer timer = new BulkTimer(); timer.start(); for (int i = 0 ; i < args.length; i++) { if (args[i].equals("-src")) { srcPath = args[++i]; } else if (args[i].equals("-dest")) { destPath = args[++i]; } else if (args[i].equals("-generate_binary")) { String name = "clunits.txt"; if (i + 1 < args.length) { String nameArg = args[++i]; if (!nameArg.startsWith("-")) { name = nameArg; } } int suffixPos = name.lastIndexOf(".txt"); String binaryName = "clunits.bin"; if (suffixPos != -1) { binaryName = name.substring(0, suffixPos) + ".bin"; } System.out.println("Loading " + name); timer.start("load_text"); ClusterUnitDatabase udb = new ClusterUnitDatabase( new URL("file:" + srcPath + "/" + name), false); timer.stop("load_text"); System.out.println("Dumping " + binaryName); timer.start("dump_binary"); udb.dumpBinary(destPath + "/" + binaryName); timer.stop("dump_binary"); } else if (args[i].equals("-compare")) { timer.start("load_text"); ClusterUnitDatabase udb = new ClusterUnitDatabase( new URL("file:./cmu_time_awb.txt"), false); timer.stop("load_text"); timer.start("load_binary"); ClusterUnitDatabase budb = new ClusterUnitDatabase( new URL("file:./cmu_time_awb.bin"), true); timer.stop("load_binary"); timer.start("compare"); if (udb.compare(budb)) { System.out.println("other compare ok"); } else { System.out.println("other compare different"); } timer.stop("compare"); } else if (args[i].equals("-showtimes")) { showTimes = true; } else { System.out.println("Unknown option " + args[i]); } } timer.stop(); if (showTimes) { timer.show("ClusterUnitDatabase"); } } else { System.out.println("Options: "); System.out.println(" -src path"); System.out.println(" -dest path"); System.out.println(" -compare"); System.out.println(" -generate_binary"); System.out.println(" -showTimes"); } } catch (IOException ioe) { System.err.println(ioe); } } /** * Represents a unit for the cluster database. */ class DatabaseClusterUnit { int type; int phone; int start; int end; int prev; int next; /** * Constructs a unit. * * @param type the name of the unit * @param phone the name of the unit * @param start the starting frame * @param end the ending frame * @param prev the previous index * @param next the next index */ DatabaseClusterUnit(int type, int phone, int start, int end, int prev, int next) { this.type = type; this.phone = phone; this.start = start; this.end = end; this.prev = prev; this.next = next; } /** * Creates a unit by reading it from the given byte buffer. * * @param bb source of the DatabaseClusterUnit data * * @throws IOException if an IO error occurs */ DatabaseClusterUnit(ByteBuffer bb) throws IOException { this.type = bb.getInt(); this.phone = bb.getInt(); this.start = bb.getInt(); this.end = bb.getInt(); this.prev = bb.getInt(); this.next = bb.getInt(); } /** * Creates a unit by reading it from the given input stream. * * @param is source of the DatabaseClusterUnit data * * @throws IOException if an IO error occurs */ DatabaseClusterUnit(DataInputStream is) throws IOException { this.type = is.readInt(); this.phone = is.readInt(); this.start = is.readInt(); this.end = is.readInt(); this.prev = is.readInt(); this.next = is.readInt(); } /** * Returns the name of the unit. * * @return the name */ String getName() { return unitTypes[type].getName(); } /** * Dumps this unit to the given output stream. * * @param os the output stream * * @throws IOException if an error occurs. */ void dumpBinary(DataOutputStream os) throws IOException { os.writeInt(type); os.writeInt(phone); os.writeInt(start); os.writeInt(end); os.writeInt(prev); os.writeInt(next); } } /** * Displays an error message * * @param s the error message */ private void error(String s) { System.out.println("ClusterUnitDatabase Error: " + s); }}/** * Represents a unit type in the system */class UnitType { private String name; private int start; private int count; /** * Constructs a UnitType from the given parameters * * @param name the name of the type * @param start the starting index for this type * @param count the number of elements for this type */ UnitType(String name, int start, int count) { this.name = name; this.start = start; this.count = count; } /** * Creates a unit type by reading it from the given input stream. * * @param is source of the UnitType data * * @throws IOException if an IO error occurs */ UnitType(DataInputStream is) throws IOException { this.name = Utilities.getString(is); this.start = is.readInt(); this.count = is.readInt(); } /** * Creates a unit type by reading it from the given byte buffer. * * @param bb source of the UnitType data * * @throws IOException if an IO error occurs */ UnitType(ByteBuffer bb) throws IOException { this.name = Utilities.getString(bb); this.start = bb.getInt(); this.count = bb.getInt(); } /** * Gets the name for this unit type * * @return the name for the type */ String getName() { return name; } /** * Gets the start index for this type * * @return the start index */ int getStart() { return start; } /** * Gets the count for this type * * @return the count for this type */ int getCount() { return count; } /** * Dumps this unit to the given output stream. * * @param os the output stream * * @throws IOException if an error occurs. */ void dumpBinary(DataOutputStream os) throws IOException { Utilities.outString(os, name); os.writeInt(start); os.writeInt(count); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -