📄 findsts.java
字号:
*/class STS { private int[] frame; private int numSamples; private short[] residual; /** * Create an empty STS */ public STS() { } /** * Create an sts with the given data * * @param frame frame for this sts * @param numSamples number of samples this sts will contain * @param residual the residual for this sts * */ public STS(int[] frame, int numSamples, short[] residual) { this.frame = new int[frame.length]; System.arraycopy(frame, 0, this.frame, 0, frame.length); this.numSamples = numSamples; this.residual = new short[residual.length]; System.arraycopy(residual, 0, this.residual, 0, residual.length); } /** * Get the number of samples associated with this sts * * @return the number of samples for this sts */ public int getNumSamples() { return numSamples; } /** * Get the residual associated with this sts * * @return residual associated with this sts */ public short getResidual(int i) { return residual[i]; } /** * Get the frame associated with this sts * * @return a copy of the frame associated with this sts */ public int[] getFrame() { int[] f = new int[frame.length]; System.arraycopy(frame, 0, f, 0, frame.length); return f; } /** * Get an entry out of the frame * * @param index the index into the frame * * @return the entry in the frame at offset <code>index</code> */ public int getFrameEntry(int index) { return frame[index]; }}/** * This class is for general purpose functions such as reading and * writing from files, or converting formats of numbers. */class Utility { /** * Reads the next word (text separated by whitespace) from the * given stream * * @param dis the input stream * * @return the next word * * @throws IOException on error */ public static String readWord(DataInputStream dis) throws IOException { StringBuffer sb = new StringBuffer(); char c; // skip leading whitespace do { c = readChar(dis); } while(Character.isWhitespace(c)); // read the word do { sb.append(c); c = readChar(dis); } while (!Character.isWhitespace(c)); return sb.toString(); } /** * Reads a single char from the stream * * @param dis the stream to read * @return the next character on the stream * * @throws IOException if an error occurs */ public static char readChar(DataInputStream dis) throws IOException { return (char) dis.readByte(); } /** * Reads a given number of chars from the stream * * @param dis the stream to read * @param num the number of chars to read * @return a character array containing the next <code>num<code> * in the stream * * @throws IOException if an error occurs */ public static char[] readChars(DataInputStream dis, int num) throws IOException { char[] carray = new char[num]; for (int i = 0; i < num; i++) { carray[i] = readChar(dis); } return carray; } /** * Read a float from the input stream, byte-swapping as * necessary * * @param dis the inputstream * @param isBigEndian whether or not the data being read in is in * big endian format. * * @return a floating pint value * * @throws IOException on error */ public static float readFloat(DataInputStream dis, boolean isBigEndian) throws IOException { float val; if (!isBigEndian) { val = readLittleEndianFloat(dis); } else { val = dis.readFloat(); } return val; } /** * Reads the next float from the given DataInputStream, * where the data is in little endian. * * @param dataStream the DataInputStream to read from * * @return a float */ public static float readLittleEndianFloat(DataInputStream dataStream) throws IOException { return Float.intBitsToFloat(readLittleEndianInt(dataStream)); } /** * Read an integer from the input stream, byte-swapping as * necessary * * @param dis the inputstream * @param isBigEndian whether or not the data being read in is in * big endian format. * * @return an integer value * * @throws IOException on error */ public static int readInt(DataInputStream dis, boolean isBigEndian) throws IOException { if (!isBigEndian) { return readLittleEndianInt(dis); } else { return dis.readInt(); } } /** * Reads the next little-endian integer from the given DataInputStream. * * @param dataStream the DataInputStream to read from * * @return an integer */ public static int readLittleEndianInt(DataInputStream dataStream) throws IOException { int bits = 0x00000000; for (int shift = 0; shift < 32; shift += 8) { int byteRead = (0x000000ff & dataStream.readByte()); bits |= (byteRead << shift); } return bits; } /** * Read a short from the input stream, byte-swapping as * necessary * * @param dis the inputstream * @param isBigEndian whether or not the data being read in is in * big endian format. * * @return an integer value * * @throws IOException on error */ public static short readShort(DataInputStream dis, boolean isBigEndian) throws IOException { if (!isBigEndian) { return readLittleEndianShort(dis); } else { return dis.readShort(); } } /** * Reads the next little-endian short from the given DataInputStream. * * @param dataStream the DataInputStream to read from * * @return a short */ public static short readLittleEndianShort(DataInputStream dis) throws IOException { short bits = (short)(0x0000ff & dis.readByte()); bits |= (((short)(0x0000ff & dis.readByte())) << 8); return bits; } /** * Convert a short to ulaw format * * @param sample the short to convert * * @return a short containing an unsigned 8-bit quantity * representing the ulaw */ public static short shortToUlaw(short sample) { final int[] exp_lut = {0,0,1,1,2,2,2,2,3,3,3,3,3,3,3,3, 4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4, 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5, 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5, 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6, 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6, 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6, 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6, 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7}; int sign, exponent, mantissa; short ulawbyte; final short CLIP = 32635; final short BIAS = 0x0084; /* Get the sample into sign-magnitude. */ sign = (sample >> 8) & 0x80; /* set aside the sign */ if ( sign != 0 ) { sample = (short) -sample; /* get magnitude */ } if ( sample > CLIP ) sample = CLIP; /* clip the magnitude */ /* Convert from 16 bit linear to ulaw. */ sample = (short) (sample + BIAS); exponent = exp_lut[( sample >> 7 ) & 0xFF]; mantissa = ( sample >> ( exponent + 3 ) ) & 0x0F; ulawbyte = (short) ((~ ( sign | ( exponent << 4 ) | mantissa)) & 0x00FF); if ( ulawbyte == 0 ) ulawbyte = 0x02; /* optional CCITT trap */ return ulawbyte; } /** * Convert a ulaw format to short * * @param ulawbyte a short containing an unsigned 8-but quantity * representing a ulaw * * @return the short equivalent of the ulaw */ public static short ulawToShort(short ulawbyte) { final int[] exp_lut = { 0, 132, 396, 924, 1980, 4092, 8316, 16764 }; int sign, exponent, mantissa; short sample; ulawbyte = (short) (ulawbyte & 0x00FF); ulawbyte = (short) (~ulawbyte); sign = ( ulawbyte & ((short) 0x80) ); exponent = (int) ( (ulawbyte & (short) 0x00FF) >> 4 ) & 0x07; mantissa = ulawbyte & (short) 0x0F; sample = (short) (exp_lut[exponent] + (mantissa << (exponent + 3))); if ( sign != 0 ) sample = (short) (-sample); return sample; } /** * Print a float type's internal bit representation in hex * * @param f the float to print * * @return a string containing the hex value of <code>f</code> */ public static String hex(float f) { return Integer.toHexString(Float.floatToIntBits(f)); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -