datautil.java

来自「It is the Speech recognition software. 」· Java 代码 · 共 530 行 · 第 1/2 页

JAVA
530
字号
    }    /**     * Returns the given double array as a string.     * The string will be in the form:     * <pre>data.length data[0] data[1] ... data[data.length-1]</pre>where     * <code>data[i]</code>.     *     * The doubles can be written as decimal, hexadecimal,     * or scientific notation. In decimal notation, it is formatted by the     * method <code>Util.formatDouble(data[i], 10, 5)</code>. Use     * the System property <code>"frontend.util.dumpformat"</code> to     * control the dump format (permitted values are "decimal",     * "hexadecimal", and "scientific".     *     * @param data the double array to dump     *     * @return a string representation of the double array     */    public static String doubleArrayToString(double[] data) {        return doubleArrayToString(data, dumpFormat);    }    /**     * Returns the given double array as a string.     * The dump will be in the form:     * <pre>data.length data[0] data[1] ... data[data.length-1]</pre>where     * <code>data[i]</code> is formatted by the method     * <code>Util.formatDouble(data[i], 10, 5)</code>.     *     * @param data the double array to dump     * @param format either HEXADECIMAL, SCIENTIFIC or DECIMAL     *     * @return a string representation of the double array     */    private static String doubleArrayToString(double[] data, int format) {        String dump = String.valueOf(data.length);        for (int i = 0; i < data.length; i++) {            if (format == DECIMAL) {                dump += (" " + formatDouble                         (data[i], decimalIntegerDigits,                          decimalFractionDigits));            } else if (format == HEXADECIMAL) {                long binary = Double.doubleToRawLongBits(data[i]);                dump += (" 0x" + Long.toHexString(binary));            } else if (format == SCIENTIFIC) {                dump += (" " + Utilities.doubleToScientificString                         (data[i], doubleScientificFractionDigits));            }        }        return dump;    }            /**     * Returns the given float array as a string. The string is of the     * form:     * <pre>data.length data[0] data[1] ... data[data.length-1]</pre>     *     * The floats can be written as decimal, hexadecimal,     * or scientific notation. In decimal notation, it is formatted by the     * method <code>Util.formatDouble(data[i], 10, 5)</code>. Use     * the System property <code>"frontend.util.dumpformat"</code> to     * control the dump format (permitted values are "decimal",     * "hexadecimal", and "scientific".     *     * @param data the float array to dump     *     * @return a string of the given float array     */    public static String floatArrayToString(float[] data) {        return floatArrayToString(data, dumpFormat);    }    /**     * Returns the given float array as a string. The string is of the     * form:     * <pre>data.length data[0] data[1] ... data[data.length-1]</pre>     *     * @param data the float array to dump     * @param format either DECIMAL, HEXADECIMAL or SCIENTIFIC     *     * @return a string of the given float array     */    private static String floatArrayToString(float[] data, int format) {        String dump = String.valueOf(data.length);        for (int i = 0; i < data.length; i++) {            if (format == DECIMAL) {                dump += (" " + formatDouble                         ((double) data[i],                          decimalIntegerDigits, decimalFractionDigits));            } else if (format == HEXADECIMAL) {                int binary = Float.floatToRawIntBits(data[i]);                dump += (" 0x" + Integer.toHexString(binary));            } else if (format == SCIENTIFIC) {                dump += (" " + Utilities.doubleToScientificString                         ((double) data[i], floatScientificFractionDigits));            }        }        return dump;    }    /**     * Returns a formatted string of the given number, with     * the given numbers of digit space for the integer and fraction parts.     * If the integer part has less than <code>integerDigits</code> digits,     * spaces will be prepended to it. If the fraction part has less than     * <code>fractionDigits</code>, spaces will be appended to it.     * Therefore, <code>formatDouble(12345.6789, 6, 6)</code> will give     * the string <pre>" 12345.6789  "</pre> (one space before 1, two spaces     * after 9).     *     * @param number the number to format     * @param integerDigits the length of the integer part     * @param fractionDigits the length of the fraction part     *     * @return a formatted number     */    public static String formatDouble(double number, int integerDigits,				      int fractionDigits) {        String formatter = "0.";	for (int i = 0; i < fractionDigits; i++) {	    formatter += "0";	}	format.applyPattern(formatter);	String formatted = format.format(number);	// pad preceding spaces before the number	int dotIndex = formatted.indexOf('.');	if (dotIndex == -1) {	    formatted += ".";	    dotIndex = formatted.length() - 1;	}	String result = "";	for (int i = dotIndex; i < integerDigits; i++) {	    result += " ";	}	result += formatted;	return result;    }    /**     * Returns the number of samples per window given the sample rate     * (in Hertz) and window size (in milliseconds).     *     * @param sampleRate the sample rate in Hertz (i.e., frequency per     *     seconds)     * @param windowSizeInMs the window size in milliseconds     *     * @return the number of samples per window     */    public static int getSamplesPerWindow(int sampleRate,                                           float windowSizeInMs) {        return (int) ( ((float) sampleRate) * windowSizeInMs / 1000);    }            /**     * Returns the number of samples in a window shift given the sample     * rate (in Hertz) and the window shift (in milliseconds).     *     * @param sampleRate the sample rate in Hertz (i.e., frequency per     *     seconds)     * @param windowShiftInMs the window shift in milliseconds     *     * @return the number of samples in a window shift     */    public static int getSamplesPerShift(int sampleRate,                                         float windowShiftInMs) {        return (int) (((float) sampleRate) * windowShiftInMs / 1000);    }    /**     * Saves the given bytes to the given binary file.     *     * @param data the bytes to save     * @param filename the binary file name     *     * @throws IOException if an I/O error occurs     */    public static void bytesToFile(byte[] data, String filename)         throws IOException {        FileOutputStream file = new FileOutputStream(filename);        file.write(data);        file.close();    }    /**     * Returns a native audio format that has the same encoding,     * endianness and sample size as the given format,     * and a sample rate that is larger than the given sample rate.     *     * @return a suitable native audio format     */    public static AudioFormat getNativeAudioFormat(AudioFormat format) {        return getNativeAudioFormat(format, null);    }    /**     * Returns a native audio format that has the same encoding,     * endianness and sample size as the given format,     * and a sample rate that is greater than or equal to the     * given sample rate.     *     * @param format the desired format     * @param mixer if non-null, use this Mixer; otherwise use     * AudioSystem     *     * @return a suitable native audio format     */    public static AudioFormat getNativeAudioFormat(AudioFormat format,                                                   Mixer mixer) {        Line.Info[] lineInfos;                if (mixer != null) {            lineInfos = mixer.getTargetLineInfo                (new Line.Info(TargetDataLine.class));        } else {            lineInfos = AudioSystem.getTargetLineInfo                (new Line.Info(TargetDataLine.class));        }                AudioFormat nativeFormat = null;        // find a usable target line        for (int i = 0; i < lineInfos.length; i++) {                        AudioFormat[] formats =                 ((TargetDataLine.Info)lineInfos[i]).getFormats();                        for (int j = 0; j < formats.length; j++) {                                // for now, just accept downsampling, not checking frame                // size/rate (encoding assumed to be PCM)                                AudioFormat thisFormat = formats[j];                if (thisFormat.getEncoding() == format.getEncoding()                    && thisFormat.isBigEndian() == format.isBigEndian()                    && thisFormat.getSampleSizeInBits() ==                     format.getSampleSizeInBits()                    && thisFormat.getSampleRate() >= format.getSampleRate()) {                    nativeFormat = thisFormat;                    break;                }            }            if (nativeFormat != null) {                //no need to look through remaining lineinfos                break;            }        }        return nativeFormat;    }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?