當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。