001/**
002 * Portions Copyright 2001 Sun Microsystems, Inc.
003 * Portions Copyright 1999-2001 Language Technologies Institute, 
004 * Carnegie Mellon University.
005 * All Rights Reserved.  Use is subject to license terms.
006 * 
007 * See the file "license.terms" for information on usage and
008 * redistribution of this file, and for a DISCLAIMER OF ALL 
009 * WARRANTIES.
010 */
011package com.sun.speech.freetts.relp;
012
013import java.io.BufferedReader;
014import java.io.DataInputStream;
015import java.io.DataOutputStream;
016import java.io.IOException;
017import java.nio.ByteBuffer;
018import java.util.NoSuchElementException;
019import java.util.StringTokenizer;
020
021/**
022 * Represents the frame and residual data
023 * used by the diphone database
024 * used Residual Excited Linear Predictive synthesizer
025 */
026public class SampleSet {
027    private Sample[] samples;
028    private SampleInfo sampleInfo;
029
030    /**
031     * Reads a SampleSet from the input reader. 
032     *
033     * @param tok tokenizer that holds parameters for this SampleSet
034     * @param reader the input reader to read the data from
035     */
036    public SampleSet(StringTokenizer tok, BufferedReader reader) {
037        try {
038            int numSamples = Integer.parseInt(tok.nextToken());
039            int numChannels = Integer.parseInt(tok.nextToken());
040            int sampleRate = Integer.parseInt(tok.nextToken());
041            float coeffMin = Float.parseFloat(tok.nextToken());
042            float coeffRange = Float.parseFloat(tok.nextToken());
043            float postEmphasis = Float.parseFloat(tok.nextToken());
044            int residualFold = Integer.parseInt(tok.nextToken());
045
046            samples = new Sample[numSamples];
047            sampleInfo = new SampleInfo(sampleRate, numChannels,
048                    residualFold, coeffMin, coeffRange, postEmphasis);
049
050            for (int i = 0; i < numSamples; i++) {
051                samples[i] = new Sample(reader, numChannels);
052            }
053        } catch (NoSuchElementException nse) {
054            throw new Error("Parsing sample error " + nse.getMessage());
055        }
056    }
057
058    /**
059     * Creates a SampleSet by reading it from the given byte buffer
060     *
061     * @param bb source of the Unit data
062     *
063     * @throws IOException if an IO error occurs
064     */
065    public SampleSet(ByteBuffer bb) throws IOException {
066        int numSamples;
067        sampleInfo = new SampleInfo(bb);
068        numSamples = bb.getInt();
069        this.samples = new Sample[numSamples];
070        for (int i = 0 ; i < numSamples; i++) {
071            samples[i] = Sample.loadBinary(bb);
072        }
073    }
074
075    /**
076     * Creates a SampleSet by reading it from the given input stream
077     *
078     * @param is source of the Unit data
079     *
080     * @throws IOException if an IO error occurs
081     */
082    public SampleSet(DataInputStream is) throws IOException {
083        int numSamples;
084        sampleInfo = new SampleInfo(is);
085        numSamples = is.readInt();
086        this.samples = new Sample[numSamples];
087        for (int i = 0 ; i < numSamples; i++) {
088            samples[i] = Sample.loadBinary(is);
089        }
090    }
091
092    /**
093     * Dumps this sample set to the given stream
094     *
095     * @param os the output stream
096     *
097     * @throws IOException if an error occurs.
098     */
099    public void dumpBinary(DataOutputStream os) throws IOException {
100        sampleInfo.dumpBinary(os);
101        os.writeInt(samples.length);
102        for (int i = 0; i < samples.length; i++) {
103            samples[i].dumpBinary(os);
104        }
105    }
106
107
108    /**
109     * return the sample associated with the index
110     *
111     * @param index the index of the sample
112     *
113     * @return the sample.
114     */
115    public Sample getSample(int index) {
116        return samples[index];
117    }
118
119    /**
120     * Retrieves the info on this SampleSet
121     *
122     * @return the sample info
123     */
124    public SampleInfo getSampleInfo() {
125        return sampleInfo;
126    }
127
128
129    /**
130     * Returns the size of the unit represented
131     * by the given start and end points
132     *
133     * @param start the start of the unit
134     * @param end the end of the unit
135     *
136     * @return the size of the unit
137     */
138    public int getUnitSize(int start, int end) {
139        int size = 0;
140
141        for (int i = start; i < end; i++) {
142            size += getFrameSize(i);
143        }
144        return size;
145    }
146
147
148    /**
149     * Gets the size of the given frame
150     *
151     * @param frame the frame of interest
152     *
153     * @return the size of the frame
154     */
155    public int getFrameSize(int frame) {
156        return  samples[frame].getResidualSize();
157    }
158}
159