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 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;
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;
28 * Represents the frame and residual data
29 * used by the diphone database
30 * used Residual Excited Linear Predictive synthesizer
32 public class SampleSet {
33 private Sample[] samples;
34 private SampleInfo sampleInfo;
37 * Reads a SampleSet from the input reader.
39 * @param tok tokenizer that holds parameters for this SampleSet
40 * @param reader the input reader to read the data from
42 public SampleSet(StringTokenizer tok, BufferedReader reader) {
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());
52 samples = new Sample[numSamples];
53 sampleInfo = new SampleInfo(sampleRate, numChannels,
54 residualFold, coeffMin, coeffRange, postEmphasis);
56 for (int i = 0; i < numSamples; i++) {
57 samples[i] = new Sample(reader, numChannels);
59 } catch (NoSuchElementException nse) {
60 throw new Error("Parsing sample error " + nse.getMessage());
65 * Creates a SampleSet by reading it from the given byte buffer
67 * @param bb source of the Unit data
69 * @throws IOException if an IO error occurs
71 public SampleSet(ByteBuffer bb) throws IOException {
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);
82 * Creates a SampleSet by reading it from the given input stream
84 * @param is source of the Unit data
86 * @throws IOException if an IO error occurs
88 public SampleSet(DataInputStream is) throws IOException {
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);
99 * Dumps this sample set to the given stream
101 * @param os the output stream
103 * @throws IOException if an error occurs.
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);
115 * return the sample associated with the index
117 * @param index the index of the sample
119 * @return the sample.
121 public Sample getSample(int index) {
122 return samples[index];
126 * Retrieves the info on this SampleSet
128 * @return the sample info
130 public SampleInfo getSampleInfo() {
136 * Returns the size of the unit represented
137 * by the given start and end points
139 * @param start the start of the unit
140 * @param end the end of the unit
142 * @return the size of the unit
144 public int getUnitSize(int start, int end) {
147 for (int i = start; i < end; i++) {
148 size += getFrameSize(i);
155 * Gets the size of the given frame
157 * @param frame the frame of interest
159 * @return the size of the frame
161 public int getFrameSize(int frame) {
162 return samples[frame].getResidualSize();