📄 lpcresult.java
字号:
/** * Portions Copyright 2001 Sun Microsystems, Inc. * Portions Copyright 1999-2001 Language Technologies Institute, * Carnegie Mellon University. * All Rights Reserved. Use is subject to license terms. * * See the file "license.terms" for information on usage and * redistribution of this file, and for a DISCLAIMER OF ALL * WARRANTIES. */package com.sun.speech.freetts.relp;import java.io.BufferedWriter;import java.io.OutputStreamWriter;import java.io.PrintWriter;import java.io.Writer;import java.io.FileWriter;import java.io.IOException;import java.text.DecimalFormat;import javax.sound.sampled.AudioFormat;import com.sun.speech.freetts.Utterance;import com.sun.speech.freetts.FreeTTSSpeakable;import com.sun.speech.freetts.audio.AudioPlayer;import com.sun.speech.freetts.util.WaveUtils;import com.sun.speech.freetts.util.Utilities;/** * Contains the result of linear predictive coding processing. * */public class LPCResult { private static final double POST_EMPHASIS = 0.0; private int frameSize = 10; private int numberOfFrames = 0; private short[][] frames = null; private int[] times = null; private int[] sizes = null; /** * this is a normalized version of the residuals; to normalize it, * add 128 to it */ private byte[] residuals = null; private int numberOfChannels; private int sampleRate; private int residualFold; private float lpcMinimum; private float lpcRange; private final static int MAX_SAMPLE_SIZE = Utilities.getInteger("com.sun.speech.freetts.LpcResult.maxSamples", 1024).intValue(); /** * Given a residual, maps it using WaveUtils.ulawToShort() to a float. */ private final static float[] residualToFloatMap = new float[256]; static { for (short i = 0; i < residualToFloatMap.length; i++) { residualToFloatMap[i] = (float) WaveUtils.ulawToShort(i); } residualToFloatMap[128] = (float) WaveUtils.ulawToShort((short) 255); } public LPCResult() { residualFold = 1; } /** * Resets the number of frames in this LPCResult. * * @param numberOfFrames the number of frames in this LPC result */ public void resizeFrames(int numberOfFrames) { times = new int[numberOfFrames]; frames = new short[numberOfFrames][]; sizes = new int[numberOfFrames]; this.numberOfFrames = numberOfFrames; } /** * Resets the number of residuals, and initialize all of them to 255 * (which is 0 for mulaw). * * @param numberOfSamples the number of samples in this LPC result */ public void resizeResiduals(int numberOfSamples) { residuals = new byte[numberOfSamples]; } /** * A convenience method for setting the LPC values. * * @param numberOfChannels the number of channels * @param sampleRate the sample rate * @param lpcMin the LPC minimum * @param lpcRange the LPC range */ public void setValues(int numberOfChannels, int sampleRate, int residualFold, float lpcMin, float lpcRange) { this.numberOfChannels = numberOfChannels; this.sampleRate = sampleRate; this.lpcMinimum = lpcMin; this.lpcRange = lpcRange; } /** * Returns the time difference of the frame at the given position * with the frame prior to that. If the frame at the given position is * the first frame (position 0), the time of that frame is returned. * * @param frameIndex the position of the frame * * @return the time difference of the frame at the given position * with the frame prior to that */ public int getFrameShift(int frameIndex) { if (0 <= frameIndex && frameIndex < times.length) { if (frameIndex > 0) { return times[frameIndex] - times[frameIndex - 1]; } else { return times[frameIndex]; } } else { return 0; } } /** * Returns the sizes of frames in this LPC. * * @return the sizes of frames */ public int getFrameSize() { return frameSize; } /** * Returns the frame at the given index. * * @param index the index of interest * * @return the frame at the given index */ public short[] getFrame(int index) { return frames[index]; } /** * Returns the array of times. * * @return the array of times */ public int[] getTimes() { return times; } /** * Returns the number of frames in this LPCResult. * * @return the number of frames */ public int getNumberOfFrames() { return numberOfFrames; } /** * Returns the number of channels in this LPCResult. * * @return the number of channels */ public int getNumberOfChannels() { return numberOfChannels; } /** * Returns the LPC minimum. * * @return the LPC minimum */ public float getLPCMin() { return lpcMinimum; } /** * Returns the LPC range. * * @return the LPC range */ public float getLPCRange() { return lpcRange; } /** * Returns the number of samples in this LPC result * * @return the number of samples */ public int getNumberOfSamples() { if (residuals == null) { return 0; } else { return residuals.length; } } /** * Returns the sample rate. * * @return the sample rate */ public int getSampleRate() { return sampleRate; } /** * Returns the array of residuals sizes. * * @return the array of residuals sizes */ public int[] getResidualSizes() { return sizes; } /** * Returns the array of residuals. * * @return the array of residuals */ public byte[] getResiduals() { return residuals; } /** * Sets the sizes of frames in this LPC to the given size. * * @param frameSize the new frame size */ public void setFrameSize(int frameSize) { this.frameSize = frameSize; } /** * Sets the number of frames in this LPC Result. * * @param numberFrames the number of frames in this result */ public void setNumberOfFrames(int numberFrames) { this.numberOfFrames = numberFrames; } /** * Sets the frame at the given index. * * @param index the position of the frame to set * @param newFrames new frame data */ public void setFrame(int index, short[] newFrames) { frames[index] = newFrames; } /** * Sets the array of times. * * @param times the times data */ public void setTimes(int[] times) { this.times = times; } /** * Sets the number of channels. * * @param numberOfChannels the number of channels */ public void setNumberOfChannels(int numberOfChannels) { this.numberOfChannels = numberOfChannels; } /** * Sets the LPC minimum. * * @param min the LPC minimum */ public void setLPCMin(float min) { this.lpcMinimum = min; } /** * Sets the LPC range. * * @param range the LPC range */ public void setLPCRange(float range) { this.lpcRange = range; } /** * Sets the sample rate. * * @param rate the sample rate */ public void setSampleRate(int rate) { this.sampleRate = rate; } /** * Sets the array of residual sizes. * * @param sizes the new residual sizes */ public void setResidualSizes(int[] sizes) { for (int i = 0; i < this.sizes.length && i < sizes.length; i++) { this.sizes[i] = sizes[i]; } } /** * Copies the information in the given unit to the array of residuals, * starting at the given index, up until targetSize chars. * * @param source the unit that holds the information source * @param targetPosition start position in the array of residuals * @param targetSize the maximum number of characters to copy */ public void copyResiduals(byte[] source, int targetPosition, int targetSize) { int unitSize = source.length; if (unitSize < targetSize) { int targetStart = (targetSize - unitSize)/2; System.arraycopy(source, 0, residuals, targetPosition + targetStart, source.length); } else { int sourcePosition = (unitSize - targetSize)/2; System.arraycopy(source, sourcePosition, residuals, targetPosition, targetSize); } } /** * Copies the residual puse in the given unit to the array of residuals, * starting at the given index, up until targetSize chars. * * @param source the unit that holds the information source * @param targetPosition start position in the array of residuals * @param targetSize the maximum number of characters to copy */ public void copyResidualsPulse(byte[] source, int targetPosition, int targetSize) { int unitSize = source.length; short sample = (short) (source[0] + 128); if (unitSize < targetSize) { residuals[(targetSize-unitSize)/2] = WaveUtils.shortToUlaw(sample); } else { residuals[(unitSize-targetSize)/2] = WaveUtils.shortToUlaw(sample); } } /** * Given a 16 bit value (represented as an int), extract * the high eight bits and return them * * @param val the 16 bit value * * @return the high eight bits */ private final static byte hibyte(int val) { return (byte) (val >>> 8); } /** * Given a 16 bit value (represented as an int), extract * the low eight bits and return them * * @param val the 16 bit value * * @return the low eight bits */ private final static byte lobyte(int val) { return (byte) (val & 0x000000FF); } /** * Synthesize a Wave from this LPCResult * * @return the wave */ public boolean playWave(AudioPlayer player, Utterance utterance) { return playWaveSamples(player, utterance.getSpeakable(), getNumberOfSamples() * 2); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -