upstream version 1.2.2
[debian/freetts] / com / sun / speech / freetts / relp / UnitConcatenator.java
1 /**
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.
6  * 
7  * See the file "license.terms" for information on usage and
8  * redistribution of this file, and for a DISCLAIMER OF ALL 
9  * WARRANTIES.
10  */
11 package com.sun.speech.freetts.relp;
12
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;
22
23 /**
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.
28  *
29  * @see LPCResult
30  */
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);
38
39
40     /**
41      * Concatenate the Units in the given Utterance to the target_lpc
42      * result.
43      *
44      * @param utterance the Utterance to do concatenation
45      *
46      * @see LPCResult
47      *
48      * @throws ProcessException if an error occurs while processing
49      *     the utterance
50      */
51     public void processUtterance(Utterance utterance) throws ProcessException {
52         float uIndex = 0, m;
53         int pmI = 0, targetResidualPosition = 0, nearestPM, 
54             unitPart, targetStart = 0, targetEnd, residualSize, numberFrames;
55         Relation unitRelation = utterance.getRelation(Relation.UNIT);
56
57         SampleInfo sampleInfo;
58         
59
60         int addResidualMethod = ADD_RESIDUAL;
61         
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;
68             }
69         }
70
71         sampleInfo = (SampleInfo) utterance.getObject(SampleInfo.UTT_NAME);
72         if (sampleInfo == null) {
73             throw new IllegalStateException
74                 ("UnitConcatenator: SampleInfo does not exist");
75         }
76
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());
83
84         // create the array of final residual sizes
85         int[] targetTimes = lpcResult.getTimes();
86         int[] residualSizes = lpcResult.getResidualSizes();
87
88         int samplesSize = 0;
89         if (lpcResult.getNumberOfFrames() > 0) {
90                 samplesSize = targetTimes[lpcResult.getNumberOfFrames() - 1];
91         }
92         lpcResult.resizeResiduals(samplesSize);
93         
94         for (Item unitItem = unitRelation.getHead(); unitItem != null;
95              unitItem = unitItem.getNext()) {
96             FeatureSet featureSet = unitItem.getFeatures();
97
98             String unitName = featureSet.getString("name");
99             targetEnd = featureSet.getInt("target_end");
100             Unit unit = (Unit) featureSet.getObject("unit");
101             int unitSize = unit.getSize();
102
103             uIndex = 0;
104             m = (float)unitSize/(float)(targetEnd - targetStart);
105             numberFrames = lpcResult.getNumberOfFrames();
106             
107             // for all the pitchmarks that are required
108             for (; (pmI < numberFrames) &&
109                      (targetTimes[pmI] <= targetEnd); pmI++) {
110
111                 Sample sample = unit.getNearestSample(uIndex);
112                 
113                 // Get LPC coefficients by copying
114                 lpcResult.setFrame(pmI, sample.getFrameData());
115
116                 // Get residual by copying
117                 residualSize = lpcResult.getFrameShift(pmI);
118                 
119                 residualSizes[pmI] = residualSize;
120                 byte[] residualData = sample.getResidualData();
121
122                 if (addResidualMethod == ADD_RESIDUAL_PULSE) {
123                     lpcResult.copyResidualsPulse
124                         (residualData, targetResidualPosition, residualSize);
125                 } else {
126                     lpcResult.copyResiduals
127                         (residualData, targetResidualPosition, residualSize);
128                 }
129                 
130                 targetResidualPosition += residualSize;
131                 uIndex += ((float) residualSize * m);
132             }
133             targetStart = targetEnd;
134         }
135         lpcResult.setNumberOfFrames(pmI);
136
137         if (outputLPC) {
138             lpcResult.dump();
139         }
140     }
141
142     /**
143      * Converts this object to a string
144      * @return the string form of this object.
145      */
146     public String toString() {
147         return "UnitConcatenator";
148     }
149 }
150