Search in sources :

Example 1 with FLACFileOutputStream

use of net.sourceforge.javaflacencoder.FLACFileOutputStream in project java-google-speech-api by goxr3plus.

the class FlacEncoder method convertWaveToFlac.

/**
 * 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) {
            // Arbitrary file size
            frameLength = 16384;
        }
        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();
    }
}
Also used : AudioInputStream(javax.sound.sampled.AudioInputStream) StreamConfiguration(net.sourceforge.javaflacencoder.StreamConfiguration) FLACFileOutputStream(net.sourceforge.javaflacencoder.FLACFileOutputStream) FLACEncoder(net.sourceforge.javaflacencoder.FLACEncoder) AudioFormat(javax.sound.sampled.AudioFormat) ByteBuffer(java.nio.ByteBuffer)

Aggregations

ByteBuffer (java.nio.ByteBuffer)1 AudioFormat (javax.sound.sampled.AudioFormat)1 AudioInputStream (javax.sound.sampled.AudioInputStream)1 FLACEncoder (net.sourceforge.javaflacencoder.FLACEncoder)1 FLACFileOutputStream (net.sourceforge.javaflacencoder.FLACFileOutputStream)1 StreamConfiguration (net.sourceforge.javaflacencoder.StreamConfiguration)1