当前位置: 首页>>代码示例>>Java>>正文


Java FLACFileOutputStream类代码示例

本文整理汇总了Java中net.sourceforge.javaflacencoder.FLACFileOutputStream的典型用法代码示例。如果您正苦于以下问题:Java FLACFileOutputStream类的具体用法?Java FLACFileOutputStream怎么用?Java FLACFileOutputStream使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


FLACFileOutputStream类属于net.sourceforge.javaflacencoder包,在下文中一共展示了FLACFileOutputStream类的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: convertWaveToFlac

import net.sourceforge.javaflacencoder.FLACFileOutputStream; //导入依赖的package包/类
/**
 * Converts a wave file to a FLAC file(in order to POST the data to Google and retrieve a response) <br>
 * Sample Rate is 8000 by default
 *
 * @param inputFile  Input wave file
 * @param outputFile Output FLAC file
 */
public void convertWaveToFlac(File inputFile, File outputFile) {


    StreamConfiguration streamConfiguration = new StreamConfiguration();
    streamConfiguration.setSampleRate(8000);
    streamConfiguration.setBitsPerSample(16);
    streamConfiguration.setChannelCount(1);


    try {
        AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(inputFile);
        AudioFormat format = audioInputStream.getFormat();

        int frameSize = format.getFrameSize();

        FLACEncoder flacEncoder = new FLACEncoder();
        FLACFileOutputStream flacOutputStream = new FLACFileOutputStream(outputFile);

        flacEncoder.setStreamConfiguration(streamConfiguration);
        flacEncoder.setOutputStream(flacOutputStream);

        flacEncoder.openFLACStream();

        int frameLength = (int) audioInputStream.getFrameLength();
        if(frameLength <= AudioSystem.NOT_SPECIFIED){
        	frameLength = 16384;//Arbitrary file size
        }
        int[] sampleData = new int[frameLength];
        byte[] samplesIn = new byte[frameSize];

        int i = 0;

        while (audioInputStream.read(samplesIn, 0, frameSize) != -1) {
            if (frameSize != 1) {
                ByteBuffer bb = ByteBuffer.wrap(samplesIn);
                bb.order(ByteOrder.LITTLE_ENDIAN);
                short shortVal = bb.getShort();
                sampleData[i] = shortVal;
            } else {
                sampleData[i] = samplesIn[0];
            }

            i++;
        }

        sampleData = truncateNullData(sampleData, i);
        
        flacEncoder.addSamples(sampleData, i);
        flacEncoder.encodeSamples(i, false);
        flacEncoder.encodeSamples(flacEncoder.samplesAvailableToEncode(), true);

        audioInputStream.close();
        flacOutputStream.close();

    } catch (Exception ex) {
        ex.printStackTrace();
    }
}
 
开发者ID:goxr3plus,项目名称:java-google-speech-api,代码行数:66,代码来源:FlacEncoder.java

示例2: convertWaveToFlac

import net.sourceforge.javaflacencoder.FLACFileOutputStream; //导入依赖的package包/类
/**
 * Converts a wave file to a FLAC file(in order to POST the data to Google and retrieve a response) <br>
 * Sample Rate is 8000 by default
 *
 * @param inputFile  Input wave file
 * @param outputFile Output FLAC file
 */
public void convertWaveToFlac(File inputFile, File outputFile) {
    StreamConfiguration streamConfiguration = new StreamConfiguration();
    streamConfiguration.setSampleRate(8000);
    streamConfiguration.setBitsPerSample(16);
    streamConfiguration.setChannelCount(1);


    try {
        AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(inputFile);
        AudioFormat format = audioInputStream.getFormat();

        int frameSize = format.getFrameSize();

        FLACEncoder flacEncoder = new FLACEncoder();
        FLACFileOutputStream flacOutputStream = new FLACFileOutputStream(outputFile);

        flacEncoder.setStreamConfiguration(streamConfiguration);
        flacEncoder.setOutputStream(flacOutputStream);

        flacEncoder.openFLACStream();

        int frameLength = (int) audioInputStream.getFrameLength();
        if(frameLength <= AudioSystem.NOT_SPECIFIED){
        	frameLength = 16384;//Arbitrary file size
        }
        int[] sampleData = new int[frameLength];
        byte[] samplesIn = new byte[frameSize];

        int i = 0;

        while (audioInputStream.read(samplesIn, 0, frameSize) != -1) {
            if (frameSize != 1) {
                ByteBuffer bb = ByteBuffer.wrap(samplesIn);
                bb.order(ByteOrder.LITTLE_ENDIAN);
                short shortVal = bb.getShort();
                sampleData[i] = shortVal;
            } else {
                sampleData[i] = samplesIn[0];
            }

            i++;
        }

        sampleData = truncateNullData(sampleData, i);
        
        flacEncoder.addSamples(sampleData, i);
        flacEncoder.encodeSamples(i, false);
        flacEncoder.encodeSamples(flacEncoder.samplesAvailableToEncode(), true);

        audioInputStream.close();
        flacOutputStream.close();

    } catch (Exception ex) {
        ex.printStackTrace();
    }
}
 
开发者ID:OpenASR,项目名称:idear,代码行数:64,代码来源:FlacEncoder.java


注:本文中的net.sourceforge.javaflacencoder.FLACFileOutputStream类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。