upstream version 1.2.2
[debian/freetts] / com / sun / speech / freetts / relp / SampleSet.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 java.io.BufferedReader;
14 import java.io.IOException;
15 import java.io.DataOutputStream;
16 import java.io.DataInputStream;
17 import java.util.StringTokenizer;
18 import java.util.NoSuchElementException;
19
20 import java.nio.channels.WritableByteChannel;
21 import java.nio.channels.ReadableByteChannel;
22 import java.nio.ByteBuffer;
23 import java.nio.CharBuffer;
24 import java.nio.ShortBuffer;
25 import java.io.IOException;
26
27 /**
28  * Represents the frame and residual data
29  * used by the diphone database
30  * used Residual Excited Linear Predictive synthesizer
31  */
32 public class SampleSet {
33     private Sample[] samples;
34     private SampleInfo sampleInfo;
35
36     /**
37      * Reads a SampleSet from the input reader. 
38      *
39      * @param tok tokenizer that holds parameters for this SampleSet
40      * @param reader the input reader to read the data from
41      */
42     public SampleSet(StringTokenizer tok, BufferedReader reader) {
43         try {
44             int numSamples = Integer.parseInt(tok.nextToken());
45             int numChannels = Integer.parseInt(tok.nextToken());
46             int sampleRate = Integer.parseInt(tok.nextToken());
47             float coeffMin = Float.parseFloat(tok.nextToken());
48             float coeffRange = Float.parseFloat(tok.nextToken());
49             float postEmphasis = Float.parseFloat(tok.nextToken());
50             int residualFold = Integer.parseInt(tok.nextToken());
51
52             samples = new Sample[numSamples];
53             sampleInfo = new SampleInfo(sampleRate, numChannels,
54                     residualFold, coeffMin, coeffRange, postEmphasis);
55
56             for (int i = 0; i < numSamples; i++) {
57                 samples[i] = new Sample(reader, numChannels);
58             }
59         } catch (NoSuchElementException nse) {
60             throw new Error("Parsing sample error " + nse.getMessage());
61         }
62     }
63
64     /**
65      * Creates a SampleSet by reading it from the given byte buffer
66      *
67      * @param bb source of the Unit data
68      *
69      * @throws IOException if an IO error occurs
70      */
71     public SampleSet(ByteBuffer bb) throws IOException {
72         int numSamples;
73         sampleInfo = new SampleInfo(bb);
74         numSamples = bb.getInt();
75         this.samples = new Sample[numSamples];
76         for (int i = 0 ; i < numSamples; i++) {
77             samples[i] = Sample.loadBinary(bb);
78         }
79     }
80
81     /**
82      * Creates a SampleSet by reading it from the given input stream
83      *
84      * @param is source of the Unit data
85      *
86      * @throws IOException if an IO error occurs
87      */
88     public SampleSet(DataInputStream is) throws IOException {
89         int numSamples;
90         sampleInfo = new SampleInfo(is);
91         numSamples = is.readInt();
92         this.samples = new Sample[numSamples];
93         for (int i = 0 ; i < numSamples; i++) {
94             samples[i] = Sample.loadBinary(is);
95         }
96     }
97
98     /**
99      * Dumps this sample set to the given stream
100      *
101      * @param os the output stream
102      *
103      * @throws IOException if an error occurs.
104      */
105     public void dumpBinary(DataOutputStream os) throws IOException {
106         sampleInfo.dumpBinary(os);
107         os.writeInt(samples.length);
108         for (int i = 0; i < samples.length; i++) {
109             samples[i].dumpBinary(os);
110         }
111     }
112
113
114     /**
115      * return the sample associated with the index
116      *
117      * @param index the index of the sample
118      *
119      * @return the sample.
120      */
121     public Sample getSample(int index) {
122         return samples[index];
123     }
124
125     /**
126      * Retrieves the info on this SampleSet
127      *
128      * @return the sample info
129      */
130     public SampleInfo getSampleInfo() {
131         return sampleInfo;
132     }
133
134
135     /**
136      * Returns the size of the unit represented
137      * by the given start and end points
138      *
139      * @param start the start of the unit
140      * @param end the end of the unit
141      *
142      * @return the size of the unit
143      */
144     public int getUnitSize(int start, int end) {
145         int size = 0;
146
147         for (int i = start; i < end; i++) {
148             size += getFrameSize(i);
149         }
150         return size;
151     }
152
153
154     /**
155      * Gets the size of the given frame
156      *
157      * @param frame the frame of interest
158      *
159      * @return the size of the frame
160      */
161     public int getFrameSize(int frame) {
162         return  samples[frame].getResidualSize();
163     }
164 }
165