get Current Preferred Audio Format - Java javax.sound.sampled

Java examples for javax.sound.sampled:Audio

Description

get Current Preferred 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 void main(String[] argv) throws Exception{
        System.out.println(getCurrentPreferredAudioFormat());
    }/*  www.ja  v a  2 s  .  co  m*/
    private final static boolean DEBUG = true;
    public static AudioFormat getCurrentPreferredAudioFormat()
            throws AudioUtil.Exception {
        AudioFormat.Encoding encoding = AudioFormat.Encoding.PCM_SIGNED;
        float sampleRate = 20000;
        int sampleSizeInBits = 16;
        int channels = 1;
        boolean bigEndian = false;
        AudioFormat sourceFormat = AudioUtil.getAudioFormat(encoding,
                sampleRate, sampleSizeInBits, channels, bigEndian,
                sampleSizeInBits / 8);
        if (DEBUG) {
            System.out
                    .println(String.format("%d %d %d %s %s",
                            (int) sourceFormat.getSampleRate(),
                            (int) sourceFormat.getSampleSizeInBits(),
                            (int) sourceFormat.getChannels(),
                            (String) sourceFormat.getEncoding().toString(),
                            sourceFormat.isBigEndian() ? "big endian"
                                    : "little endian"));
        }
        return sourceFormat;
    }
    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);
        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