📄 clusterunitselector.java
字号:
clunitDB.getMcep().getSampleInfo().getNumberOfChannels()) + Math.abs( clunitDB.getSts().getFrameSize(a) - clunitDB.getSts().getFrameSize(b)) * clunitDB.getContinuityWeight(); if (dist < best_val) { best_val = dist; best_u0 = u0_st + i; best_u1_p = u1_p_st + i; } } // u0Move is the new end for u0 // u1Move is the new start for u1 cost.u0Move = clunitDB.getStart(u0) + best_u0; cost.u1Move = clunitDB.getStart(u1_p) + best_u1_p; cost.cost = 30000 + best_val; return cost; } /** * Returns the distance between the successive potential * frames. * * @param u0 the first unit to try * @param u1 the second unit to try * * @return the distance between the two units */ int getOptimalCoupleFrame(int u0, int u1) { int a, b; if (clunitDB.getPrevUnit(u1) == u0) { return 0; // consecutive units win } if (clunitDB.getNextUnit(u0) != ClusterUnitDatabase.CLUNIT_NONE) { a = clunitDB.getEnd(u0); } else { // don't want to do this but it's all that is left to do a = clunitDB.getEnd(u0) - 1; // if num frames < 1 this is bad } b = clunitDB.getStart(u1); return getFrameDistance(a, b, clunitDB.getJoinWeights(), clunitDB.getMcep().getSampleInfo().getNumberOfChannels()) + Math.abs( clunitDB.getSts().getFrameSize(a) - clunitDB.getSts().getFrameSize(b)) * clunitDB.getContinuityWeight(); } /** * Get the 'distance' between the frames a and b. * * @param a first frame * @param b second frame * @param joinWeights the weights used in comparison * @param order number of compares * * @return the distance between the frames */ public int getFrameDistance(int a, int b, int[] joinWeights,int order) { if (DEBUG) { debug(" gfd a " + a + " b " + b + " or " + order); } int r, i; short[] bv = clunitDB.getMcep().getSample(b).getFrameData(); short[] av = clunitDB.getMcep().getSample(a).getFrameData(); for (r = 0, i = 0; i < order; i++) { int diff = av[i] - bv[i]; r += Math.abs(diff) * joinWeights[i] / 65536; } return r; } } /** * Represents a point in the Viterbi path. A point corresponds to an item, * e.g. a Segment. * Each ViterbiPoint knows * about its next ViterbiPoint, i.e. they can form a queue. */ static class ViterbiPoint { Item item = null; // TODO: remove the numStates attribute from ViterbiPoint, as this is only statePaths.length int numStates = 0; int numPaths = 0; ViterbiCandidate cands = null; ViterbiPath paths = null; ViterbiPath[] statePaths = null; ViterbiPoint next = null; /** * Creates a ViterbiPoint for the given item. A typical item of choice is a Segment item. * * @param item the item of interest */ public ViterbiPoint(Item item) { this.item = item; } /** * Initialize the path array to the given size. * * @param size the size of the path array */ public void initPathArray(int size) { if (DEBUG) { debug("init_path_array: " + size); } numStates = size; statePaths = new ViterbiPath[size]; } /** * Initializes the dynamic path array. The path array will have * as many ViterbiPath members as there are candidates in the * queue starting with candidate. * Side effect on parameter: This will set the pos member of the * candidates in the queue starting with candidate to the position * in the queue. * * @param candidate the first candidate of interest */ public void initDynamicPathArray(ViterbiCandidate candidate) { int i = 0; for (ViterbiCandidate cc = candidate; cc != null; i++, cc = cc.next) { cc.pos = i; } if (DEBUG) { debug("init_dynamic_ path_array: " + i); } initPathArray(i); } public String toString() { return " pnt: " + numStates + " paths " + numPaths; } } /** * Represents a candidate for the Viterbi algorthm. * Each candidate knows about its next candidate, i.e. they can form * a queue. */ static class ViterbiCandidate { int score = 0; Object value = null; int ival = 0; int pos = 0; Item item = null; ViterbiCandidate next = null; /** * Sets the object for this candidate. * * @param obj the object */ void set(Object obj) { value = obj; } /** * Sets the integer value for this candidate. This can be used for saving * the unit index of the candidate unit represented by this ViterbiCandidate. * * @param ival the integer value */ void setInt(int ival) { this.ival = ival; set(new Integer(ival)); } /** * Converts this object to a string. * * @return the string form of this object */ public String toString() { return "VC: Score " + score + " ival " + ival + " Pos " + pos; } } /** * Describes a Viterbi path. */ static class ViterbiPath { int score = 0; int state = 0; ViterbiCandidate candidate = null; private FeatureSet f = null; ViterbiPath from = null; ViterbiPath next = null; /** * Sets a feature with the given name to the given value. * * @param name the name of the feature * @param value the new value for the feature */ void setFeature(String name, Object value) { if (f == null) { f = new FeatureSetImpl(); } f.setObject(name, value); } /** * Retrieves a feature. * * @param name the name of the feature * * @return the feature */ Object getFeature(String name) { Object value = null; if (f != null) { value = f.getObject(name); } return value; } /** * Determines if the feature with the given name * exsists. * * @param name the feature to look for * * @return <code>true</code> if the feature is present; * otherwise <code>false</code>. */ boolean isPresent(String name) { if (f == null) { return false; } else { return getFeature(name) != null; } } /** * Converts this object to a string. * * @return the string form of this object */ public String toString() { return "ViterbiPath score " + score + " state " + state; } } /** * Prints debug messages. * * @param s the debug message */ static void debug(String s) { if (DEBUG) { System.out.println("cludebug: " + s); } }}/** * Information returned from getOptimalCoupling. */class Cost { int cost = 0; int u0Move = -1; int u1Move = -1;}/** * A Cluster Unit. */class ClusterUnit implements com.sun.speech.freetts.Unit { private final static boolean DEBUG = false; private ClusterUnitDatabase db; private String name; private int start; private int end; /** * Contructs a cluster unit given. * * @param db the database * @param name the unitName * @param start the start * @param end the end */ public ClusterUnit(ClusterUnitDatabase db, String name, int start,int end) { this.db = db; this.start = start; this.end = end; this.name = name; } /** * Returns the start. * * @return the start */ public int getStart() { return start; } /** * Returns the end. * * @return the end */ public int getEnd() { return end; } /** * Returns the name of this Unit. * * @return the name of this unit */ public String getName() { return name; } /** * returns the size of the unit. * * @return the size of the unit */ public int getSize() { return db.getSts().getUnitSize(start, end); } /** * Retrieves the nearest sample. * * @param index the ideal index * * @return the nearest Sample */ public Sample getNearestSample(float index) { int i, iSize = 0, nSize; SampleSet sts = db.getSts(); // loop through all the Samples in this unit for (i = start; i < end; i++) { Sample sample = sts.getSample(i); nSize = iSize + sample.getResidualSize(); if (Math.abs(index - (float) iSize) < Math.abs(index - (float) nSize)) { return sample; } iSize = nSize; } return sts.getSample(end - 1); } /** * gets the string name for the unit. * * @return string rep of this object. */ public String toString() { return getName(); } /** * Dumps this unit. */ public void dump() { } /** * Prints debugging statements. * * @param s the debugging message */ private void debug(String s) { if (DEBUG) { System.out.println("Clunit debug: " + s); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -