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.relp;
13 import com.sun.speech.freetts.Unit;
14 import com.sun.speech.freetts.FeatureSet;
15 import com.sun.speech.freetts.Item;
16 import com.sun.speech.freetts.relp.LPCResult;
17 import com.sun.speech.freetts.UtteranceProcessor;
18 import com.sun.speech.freetts.Utterance;
19 import com.sun.speech.freetts.Relation;
20 import com.sun.speech.freetts.ProcessException;
21 import com.sun.speech.freetts.util.Utilities;
24 * Concatenates the Units in the given Utterance to the target_lpc
25 * result. This class is an UtteranceProcessor. It defines a method
26 * <code> processUtterance </code> that helps populate the
27 * target_lpcres relation.
31 public class UnitConcatenator implements UtteranceProcessor {
32 static private final int ADD_RESIDUAL_PULSE = 1;
33 static private final int ADD_RESIDUAL_WINDOWED = 2;
34 static private final int ADD_RESIDUAL = 3;
35 public final static String PROP_OUTPUT_LPC =
36 "com.sun.speech.freetts.outputLPC";
37 private boolean outputLPC = Utilities.getBoolean(PROP_OUTPUT_LPC);
41 * Concatenate the Units in the given Utterance to the target_lpc
44 * @param utterance the Utterance to do concatenation
48 * @throws ProcessException if an error occurs while processing
51 public void processUtterance(Utterance utterance) throws ProcessException {
53 int pmI = 0, targetResidualPosition = 0, nearestPM,
54 unitPart, targetStart = 0, targetEnd, residualSize, numberFrames;
55 Relation unitRelation = utterance.getRelation(Relation.UNIT);
57 SampleInfo sampleInfo;
60 int addResidualMethod = ADD_RESIDUAL;
62 String residualType = utterance.getString("residual_type");
63 if (residualType != null) {
64 if (residualType.equals("pulse")) {
65 addResidualMethod = ADD_RESIDUAL_PULSE;
66 } else if (residualType.equals("windowed")) {
67 addResidualMethod = ADD_RESIDUAL_WINDOWED;
71 sampleInfo = (SampleInfo) utterance.getObject(SampleInfo.UTT_NAME);
72 if (sampleInfo == null) {
73 throw new IllegalStateException
74 ("UnitConcatenator: SampleInfo does not exist");
77 LPCResult lpcResult = (LPCResult) utterance.getObject("target_lpcres");
78 lpcResult.setValues(sampleInfo.getNumberOfChannels(),
79 sampleInfo.getSampleRate(),
80 sampleInfo.getResidualFold(),
81 sampleInfo.getCoeffMin(),
82 sampleInfo.getCoeffRange());
84 // create the array of final residual sizes
85 int[] targetTimes = lpcResult.getTimes();
86 int[] residualSizes = lpcResult.getResidualSizes();
89 if (lpcResult.getNumberOfFrames() > 0) {
90 samplesSize = targetTimes[lpcResult.getNumberOfFrames() - 1];
92 lpcResult.resizeResiduals(samplesSize);
94 for (Item unitItem = unitRelation.getHead(); unitItem != null;
95 unitItem = unitItem.getNext()) {
96 FeatureSet featureSet = unitItem.getFeatures();
98 String unitName = featureSet.getString("name");
99 targetEnd = featureSet.getInt("target_end");
100 Unit unit = (Unit) featureSet.getObject("unit");
101 int unitSize = unit.getSize();
104 m = (float)unitSize/(float)(targetEnd - targetStart);
105 numberFrames = lpcResult.getNumberOfFrames();
107 // for all the pitchmarks that are required
108 for (; (pmI < numberFrames) &&
109 (targetTimes[pmI] <= targetEnd); pmI++) {
111 Sample sample = unit.getNearestSample(uIndex);
113 // Get LPC coefficients by copying
114 lpcResult.setFrame(pmI, sample.getFrameData());
116 // Get residual by copying
117 residualSize = lpcResult.getFrameShift(pmI);
119 residualSizes[pmI] = residualSize;
120 byte[] residualData = sample.getResidualData();
122 if (addResidualMethod == ADD_RESIDUAL_PULSE) {
123 lpcResult.copyResidualsPulse
124 (residualData, targetResidualPosition, residualSize);
126 lpcResult.copyResiduals
127 (residualData, targetResidualPosition, residualSize);
130 targetResidualPosition += residualSize;
131 uIndex += ((float) residualSize * m);
133 targetStart = targetEnd;
135 lpcResult.setNumberOfFrames(pmI);
143 * Converts this object to a string
144 * @return the string form of this object.
146 public String toString() {
147 return "UnitConcatenator";