📄 datautil.java
字号:
/* * Copyright 1999-2002 Carnegie Mellon University. * Portions Copyright 2002 Sun Microsystems, Inc. * Portions Copyright 2002 Mitsubishi Electric Research Laboratories. * 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 edu.cmu.sphinx.frontend.util;import edu.cmu.sphinx.util.Utilities;import java.io.FileOutputStream;import java.io.IOException;import java.text.DecimalFormat;import javax.sound.sampled.AudioSystem;import javax.sound.sampled.AudioFormat;import javax.sound.sampled.Line;import javax.sound.sampled.Mixer;import javax.sound.sampled.TargetDataLine;/** * Defines utility methods for manipulating data values. */public class DataUtil { private static final int HEXADECIMAL = 1; private static final int SCIENTIFIC = 2; private static final int DECIMAL = 3; /** * DecimalFormat object to be used by all the methods. */ private static DecimalFormat format = new DecimalFormat(); private static int decimalIntegerDigits = 10; private static int decimalFractionDigits = 5; private static int floatScientificFractionDigits = 8; private static int doubleScientificFractionDigits = 8; /** * The number format to be used by *ArrayToString() methods. * The default is scientific. */ private static int dumpFormat = SCIENTIFIC; /** * Static initialization of dumpFormat */ static { String formatProperty = System.getProperty("frontend.util.dumpformat", "SCIENTIFIC"); if (formatProperty.compareToIgnoreCase("DECIMAL") == 0) { dumpFormat = DECIMAL; } else if (formatProperty.compareToIgnoreCase("HEXADECIMAL") == 0) { dumpFormat = HEXADECIMAL; } else if (formatProperty.compareToIgnoreCase("SCIENTIFIC") == 0) { dumpFormat = SCIENTIFIC; } } /** * Uninstantiable class. */ private DataUtil() {} /** * Converts a byte array into a short array. Since a byte is 8-bits, * and a short is 16-bits, the returned short array will be half in * length than the byte array. If the length of the byte array is odd, * the length of the short array will be * <code>(byteArray.length - 1)/2</code>, i.e., the last byte is * discarded. * * @param byteArray a byte array * @param offset which byte to start from * @param length how many bytes to convert * * @return a short array, or <code>null</code> if byteArray is of zero * length * * @throws java.lang.ArrayIndexOutOfBoundsException */ public static short[] byteToShortArray (byte[] byteArray, int offset, int length) throws ArrayIndexOutOfBoundsException { if (0 < length && (offset + length) <= byteArray.length) { int shortLength = length / 2; short[] shortArray = new short[shortLength]; int temp; for (int i = offset, j = 0; j < shortLength ; j++, temp = 0x00000000) { temp = (int) (byteArray[i++] << 8); temp |= (int) (0x000000FF & byteArray[i++]); shortArray[j] = (short) temp; } return shortArray; } else { throw new ArrayIndexOutOfBoundsException ("offset: " + offset + ", length: " + length + ", array length: " + byteArray.length); } } /** * Converts a big-endian byte array into an array of doubles. * Each consecutive bytes in the byte array are converted into a double, * and becomes the next element in the double array. The size of the * returned array is (length/bytesPerValue). * Currently, only 1 byte (8-bit) or 2 bytes (16-bit) * samples are supported. * * @param byteArray a byte array * @param offset which byte to start from * @param length how many bytes to convert * @param bytesPerValue the number of bytes per value * @param signedData whether the data is signed * * @return a double array, or <code>null</code> if byteArray is of zero * length * * @throws java.lang.ArrayIndexOutOfBoundsException */ public static final double[] bytesToValues(byte[] byteArray, int offset, int length, int bytesPerValue, boolean signedData) throws ArrayIndexOutOfBoundsException { if (0 < length && (offset + length) <= byteArray.length) { assert (length % bytesPerValue == 0); double[] doubleArray = new double[length/bytesPerValue]; int i = offset; for (int j = 0; j < doubleArray.length; j++) { int val = (int) byteArray[i++]; if (!signedData) { val &= 0xff; // remove the sign extension } for (int c = 1; c < bytesPerValue; c++) { int temp = (int) byteArray[i++] & 0xff; val = (val << 8) + temp; } doubleArray[j] = (double) val; } return doubleArray; } else { throw new ArrayIndexOutOfBoundsException ("offset: " + offset + ", length: " + length + ", array length: " + byteArray.length); } } /** * Converts a little-endian byte array into an array of doubles. * Each consecutive bytes of a float are converted into a double, and * becomes the next element in the double array. The number of bytes * in the double is specified as an argument. The size of * the returned array is (data.length/bytesPerValue). * * @param data a byte array * @param offset which byte to start from * @param length how many bytes to convert * @param bytesPerValue the number of bytes per value * @param signedData whether the data is signed * * @return a double array, or <code>null</code> if byteArray is of zero * length * * @throws java.lang.ArrayIndexOutOfBoundsException */ public static final double[] littleEndianBytesToValues(byte[] data, int offset, int length, int bytesPerValue, boolean signedData) throws ArrayIndexOutOfBoundsException { if (0 < length && (offset + length) <= data.length) { assert (length % bytesPerValue == 0); double[] doubleArray = new double[length/bytesPerValue]; int i = offset + bytesPerValue - 1; for (int j = 0; j < doubleArray.length; j++) { int val = (int) data[i--]; if (!signedData) { val &= 0xff; // remove the sign extension } for (int c = 1; c < bytesPerValue; c++) { int temp = (int) data[i--] & 0xff; val = (val << 8) + temp; } // advance 'i' to the last byte of the next value i += (bytesPerValue * 2); doubleArray[j] = (double) val; } return doubleArray; } else { throw new ArrayIndexOutOfBoundsException ("offset: " + offset + ", length: " + length + ", array length: " + data.length); } } /** * Convert the two bytes starting at the given offset to a short. * * @param byteArray the byte array * @param offset where to start * * @return a short * * @throws java.lang.ArrayIndexOutOfBoundsException */ public static short bytesToShort(byte[] byteArray, int offset) throws ArrayIndexOutOfBoundsException { short result = (short) ((byteArray[offset++] << 8) | (0x000000FF & byteArray[offset])); return result; } /** * Returns the string representation of the given short array. * The string will be in the form: * <pre>data.length data[0] data[1] ... data[data.length-1]</pre> * * @param data the short array to convert * * @return a string representation of the short array */ public static String shortArrayToString(short[] data) { String dump = String.valueOf(data.length); for (int i = 0; i < data.length; i++) { dump += (" " + data[i]); } return dump;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -