Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.*;
- import java.io.*;
- import java.util.Vector;
- import javax.sound.sampled.*;
- public class AudioFormats{
- public static void main(String[] args) throws UnsupportedAudioFileException, IOException, LineUnavailableException, InterruptedException{
- String mixerName = System.getProperty("audio.mixer");
- getFormats();
- System.out.println("\n\n\n\n");
- File inputFile = new File(args[0]);
- AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(inputFile);
- System.out.println("Playing " + args[0]);
- //SourceDataLine sourceLine = AudioSystem.getSourceDataLine(audioInputStream.getFormat());
- AudioFormat format2 = audioInputStream.getFormat();
- DataLine.Info info = new DataLine.Info(SourceDataLine.class, format2);
- System.out.print("Source Data Lines: ");
- for(AudioFormat format : info.getFormats())
- {
- boolean isSupport = AudioSystem.isLineSupported(new DataLine.Info(SourceDataLine.class, format));
- System.out.println(" "+format);
- System.out.printf(" encoding: %s\n", format.getEncoding());
- System.out.printf(" channels: %d\n", format.getChannels());
- System.out.printf(format.getFrameRate()==-1?"":" frame rate [1/s]: %s\n", format.getFrameRate());
- System.out.printf(" frame size [bytes]: %d\n", format.getFrameSize());
- System.out.printf(format.getSampleRate()==-1?"":" sample rate [1/s]: %s\n", format.getSampleRate());
- System.out.printf(" sample size [bit]: %d\n", format.getSampleSizeInBits());
- System.out.printf(" big endian: %b\n", format.isBigEndian());
- System.out.printf(" line supported: %b\n", isSupport);
- }
- info = new DataLine.Info(Clip.class, format2);
- System.out.print("\n\n\nClip Data Lines: ");
- for(AudioFormat format : info.getFormats())
- {
- boolean isSupport = AudioSystem.isLineSupported(new DataLine.Info(Clip.class, format));
- System.out.println(" "+format);
- System.out.printf(" encoding: %s\n", format.getEncoding());
- System.out.printf(" channels: %d\n", format.getChannels());
- System.out.printf(format.getFrameRate()==-1?"":" frame rate [1/s]: %s\n", format.getFrameRate());
- System.out.printf(" frame size [bytes]: %d\n", format.getFrameSize());
- System.out.printf(format.getSampleRate()==-1?"":" sample rate [1/s]: %s\n", format.getSampleRate());
- System.out.printf(" sample size [bit]: %d\n", format.getSampleSizeInBits());
- System.out.printf(" big endian: %b\n", format.isBigEndian());
- System.out.printf(" line supported: %b\n", isSupport);
- }
- info = new DataLine.Info(TargetDataLine.class, format2);
- System.out.print("\n\n\nTarget Data Lines: ");
- for(AudioFormat format : info.getFormats())
- {
- boolean isSupport = AudioSystem.isLineSupported(new DataLine.Info(TargetDataLine.class, format));
- System.out.println(" "+format);
- System.out.printf(" encoding: %s\n", format.getEncoding());
- System.out.printf(" channels: %d\n", format.getChannels());
- System.out.printf(format.getFrameRate()==-1?"":" frame rate [1/s]: %s\n", format.getFrameRate());
- System.out.printf(" frame size [bytes]: %d\n", format.getFrameSize());
- System.out.printf(format.getSampleRate()==-1?"":" sample rate [1/s]: %s\n", format.getSampleRate());
- System.out.printf(" sample size [bit]: %d\n", format.getSampleSizeInBits());
- System.out.printf(" big endian: %b\n", format.isBigEndian());
- System.out.printf(" line supported: %b\n", isSupport);
- }
- try {
- info = new DataLine.Info(SourceDataLine.class, format2);
- SourceDataLine sourceLine = (SourceDataLine) AudioSystem.getLine(info);
- sourceLine.open(format2);
- sourceLine.start();
- sourceLine.drain();
- byte[] data = new byte[(int)inputFile.length()];
- int size = audioInputStream.read(data);
- int writeCount =sourceLine.write(data, 0, size);
- Thread.sleep(2000);
- sourceLine.stop();;
- sourceLine.close();
- } catch (Exception e)
- {
- e.printStackTrace();
- }
- try {
- Thread.sleep(500);
- Clip clip = AudioSystem.getClip();
- audioInputStream.close();
- audioInputStream = AudioSystem.getAudioInputStream(new File(args[0]).getAbsoluteFile());
- // open audioInputStream to the clip
- clip.open(audioInputStream);
- clip.start();
- Thread.sleep(2000);
- clip.close();
- }catch (Exception e) {
- e.printStackTrace();
- } finally {
- }
- }
- public static Vector<AudioFormat> getSupportedFormats(Class<?> dataLineClass) {
- /*
- * These define our criteria when searching for formats supported
- * by Mixers on the system.
- */
- float sampleRates[] = { (float) 8000.0, (float) 16000.0, (float) 44100.0 };
- int channels[] = { 1, 2 };
- int bytesPerSample[] = { 2 };
- AudioFormat format;
- DataLine.Info lineInfo;
- // SystemAudioProfile profile = new SystemAudioProfile(); // Used for allocating MixerDetails below.
- Vector<AudioFormat> formats = new Vector<AudioFormat>();
- for (Mixer.Info mixerInfo : AudioSystem.getMixerInfo()) {
- for (int a = 0; a < sampleRates.length; a++) {
- for (int b = 0; b < channels.length; b++) {
- for (int c = 0; c < bytesPerSample.length; c++) {
- format = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,
- sampleRates[a], 8 * bytesPerSample[c], channels[b], bytesPerSample[c],
- sampleRates[a], false);
- lineInfo = new DataLine.Info(dataLineClass, format);
- //if (AudioSystem.isLineSupported(lineInfo)) {
- /*
- * TODO: To perform an exhaustive search on supported lines, we should open
- * TODO: each Mixer and get the supported lines. Do this if this approach
- * TODO: doesn't give decent results. For the moment, we just work with whatever
- * TODO: the unopened mixers tell us.
- */
- //if (AudioSystem.getMixer(mixerInfo).isLineSupported(lineInfo)) {
- formats.add(format);
- //}
- //}
- }
- }
- }
- }
- return formats;
- }
- public static void getFormats() throws LineUnavailableException{
- String mixerName = System.getProperty("audio.mixer");
- Mixer mixer = AudioSystem.getMixer(getMixerInfo(mixerName));
- try {
- mixer.open();
- } catch (LineUnavailableException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- Mixer.Info[] mixInfo = AudioSystem.getMixerInfo();
- for(Mixer.Info mixinf :mixInfo)
- {
- System.out.printf("Supported Lines of default mixer (%s):\n\n", mixinf.getName());
- System.out.println("\nMixer ClassName: " + mixinf.getClass().getName());
- System.out.println("Mixer Desc: "+ mixinf.getDescription());
- System.out.println("Vendor: " +mixinf.getVendor());
- System.out.println();
- }
- Mixer.Info[] mixerInfos = AudioSystem.getMixerInfo();
- for (Mixer.Info info: mixerInfos){
- Mixer m = AudioSystem.getMixer(info);
- Line.Info[] lineInfos = m.getSourceLineInfo();
- for (Line.Info lineInfo:lineInfos){
- System.out.println (info.getName()+"\t---\t"+lineInfo);
- Line line = m.getLine(lineInfo);
- System.out.println("\t-----\t"+line);
- }
- lineInfos = m.getTargetLineInfo();
- for (Line.Info lineInfo:lineInfos){
- System.out.println (m+"\t---\t"+lineInfo);
- Line line = m.getLine(lineInfo);
- System.out.println("\t-----\t"+line);
- }
- }
- Line.Info [] lines = mixer.getSourceLineInfo();
- if (lines !=null)
- {
- if (lines.length > 0)
- {
- System.out.println("Mixer Num Source Info Lines: " + lines.length);
- }else {
- System.out.println("Mixer Has 0 Source Info Lines: ");
- }
- } else {
- System.out.println("Mixer.getSourceLines is null");
- }
- lines = mixer.getTargetLineInfo();
- if (lines !=null)
- {
- if (lines.length > 0)
- {
- System.out.println("Mixer Num Target Info Lines: " + lines.length);
- }else {
- System.out.println("Mixer Has 0 Target Info Lines: ");
- }
- } else {
- System.out.println("Mixer.getTargetLines is null");
- }
- for(Line.Info info : mixer.getSourceLineInfo()) {
- if(SourceDataLine.class.isAssignableFrom(info.getLineClass())) {
- SourceDataLine.Info info2 = (SourceDataLine.Info) info;
- System.out.println(info2);
- System.out.printf(" max buffer size: \t%d\n", info2.getMaxBufferSize());
- System.out.printf(" min buffer size: \t%d\n", info2.getMinBufferSize());
- AudioFormat[] formats = info2.getFormats();
- System.out.println(" Supported Audio formats: ");
- for(AudioFormat format : formats) {
- boolean isSupport = AudioSystem.isLineSupported(new DataLine.Info(SourceDataLine.class, format));
- System.out.println(" "+format);
- System.out.printf(" encoding: %s\n", format.getEncoding());
- System.out.printf(" channels: %d\n", format.getChannels());
- System.out.printf(format.getFrameRate()==-1?"":" frame rate [1/s]: %s\n", format.getFrameRate());
- System.out.printf(" frame size [bytes]: %d\n", format.getFrameSize());
- System.out.printf(format.getSampleRate()==-1?"":" sample rate [1/s]: %s\n", format.getSampleRate());
- System.out.printf(" sample size [bit]: %d\n", format.getSampleSizeInBits());
- System.out.printf(" big endian: %b\n", format.isBigEndian());
- System.out.printf(" line supported: %b\n", isSupport);
- }
- System.out.println();
- } else {
- System.out.println(info.toString());
- }
- System.out.println();
- }
- mixer.close();
- }
- public static Mixer.Info getMixerInfo(final String name) {
- for (final Mixer.Info info : AudioSystem.getMixerInfo()) {
- System.out.println("Found audio mixer: " + (Object)info.getName());
- if (info.getName().equalsIgnoreCase(name)) {
- return info;
- }
- }
- return null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment