get Audio Format - Java javax.sound.sampled

Java examples for javax.sound.sampled:Audio

Description

get Audio Format

Demo Code


import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.ShortBuffer;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.TargetDataLine;

public class Main{
    public static AudioFormat getAudioFormat(AudioFormat.Encoding encoding,
            float sampleRate, int sampleSizeInBits, int channels,
            boolean bigEndian, int frameSizeInBytes)
            throws AudioUtil.Exception {

        AudioFormat desiredFormat = new AudioFormat(encoding, sampleRate,
                sampleSizeInBits, channels, frameSizeInBytes, sampleRate,
                !bigEndian);//from w  w w.  j  a  v a2s .  co  m
        AudioFormat[] formats = AudioSystem.getTargetFormats(encoding,
                desiredFormat);
        AudioFormat sourceFormat = null;
        for (AudioFormat format : formats) {
            if (format.isBigEndian() == bigEndian
                    && format.getEncoding() == encoding
                    && format.getSampleSizeInBits() == sampleSizeInBits
                    && format.getChannels() == channels
                    && format.getSampleRate() == sampleRate) {
                sourceFormat = format;
                break;
            }
        }
        if (sourceFormat == null) {
            throw new AudioUtil.Exception(
                    new UnsupportedAudioFormatException(String.format(
                            "The format \"%s\" is unsupported",
                            desiredFormat)));
        }
        return sourceFormat;
    }
}

Related Tutorials