Guest User

audioformats

a guest
Oct 21st, 2021
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.87 KB | None | 0 0
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import java.io.*;
  5. import java.util.Vector;
  6.  
  7. import javax.sound.sampled.*;
  8.  
  9. public class AudioFormats{
  10.  
  11.     public static void main(String[] args) throws UnsupportedAudioFileException, IOException, LineUnavailableException, InterruptedException{
  12.         String mixerName = System.getProperty("audio.mixer");
  13.        
  14.         getFormats();
  15.  
  16.        
  17.  
  18.         System.out.println("\n\n\n\n");
  19.         File inputFile = new File(args[0]);
  20.         AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(inputFile);
  21.         System.out.println("Playing " + args[0]);
  22.         //SourceDataLine sourceLine = AudioSystem.getSourceDataLine(audioInputStream.getFormat());
  23.         AudioFormat format2 = audioInputStream.getFormat();
  24.         DataLine.Info   info = new DataLine.Info(SourceDataLine.class, format2);
  25.        
  26.        
  27.         System.out.print("Source Data Lines: ");
  28.         for(AudioFormat format : info.getFormats())
  29.         {
  30.             boolean isSupport = AudioSystem.isLineSupported(new DataLine.Info(SourceDataLine.class, format));
  31.             System.out.println("    "+format);
  32.             System.out.printf("      encoding:           %s\n", format.getEncoding());
  33.             System.out.printf("      channels:           %d\n", format.getChannels());
  34.             System.out.printf(format.getFrameRate()==-1?"":"      frame rate [1/s]:   %s\n", format.getFrameRate());
  35.             System.out.printf("      frame size [bytes]: %d\n", format.getFrameSize());
  36.             System.out.printf(format.getSampleRate()==-1?"":"      sample rate [1/s]:  %s\n", format.getSampleRate());
  37.             System.out.printf("      sample size [bit]:  %d\n", format.getSampleSizeInBits());
  38.             System.out.printf("      big endian:         %b\n", format.isBigEndian());
  39.             System.out.printf("      line supported:         %b\n", isSupport);
  40.         }
  41.        
  42.         info = new DataLine.Info(Clip.class, format2);
  43.         System.out.print("\n\n\nClip Data Lines: ");
  44.         for(AudioFormat format : info.getFormats())
  45.         {
  46.             boolean isSupport = AudioSystem.isLineSupported(new DataLine.Info(Clip.class, format));
  47.             System.out.println("    "+format);
  48.             System.out.printf("      encoding:           %s\n", format.getEncoding());
  49.             System.out.printf("      channels:           %d\n", format.getChannels());
  50.             System.out.printf(format.getFrameRate()==-1?"":"      frame rate [1/s]:   %s\n", format.getFrameRate());
  51.             System.out.printf("      frame size [bytes]: %d\n", format.getFrameSize());
  52.             System.out.printf(format.getSampleRate()==-1?"":"      sample rate [1/s]:  %s\n", format.getSampleRate());
  53.             System.out.printf("      sample size [bit]:  %d\n", format.getSampleSizeInBits());
  54.             System.out.printf("      big endian:         %b\n", format.isBigEndian());
  55.             System.out.printf("      line supported:         %b\n", isSupport);
  56.         }
  57.         info = new DataLine.Info(TargetDataLine.class, format2);
  58.         System.out.print("\n\n\nTarget Data Lines: ");
  59.         for(AudioFormat format : info.getFormats())
  60.         {
  61.             boolean isSupport = AudioSystem.isLineSupported(new DataLine.Info(TargetDataLine.class, format));
  62.             System.out.println("    "+format);
  63.             System.out.printf("      encoding:           %s\n", format.getEncoding());
  64.             System.out.printf("      channels:           %d\n", format.getChannels());
  65.             System.out.printf(format.getFrameRate()==-1?"":"      frame rate [1/s]:   %s\n", format.getFrameRate());
  66.             System.out.printf("      frame size [bytes]: %d\n", format.getFrameSize());
  67.             System.out.printf(format.getSampleRate()==-1?"":"      sample rate [1/s]:  %s\n", format.getSampleRate());
  68.             System.out.printf("      sample size [bit]:  %d\n", format.getSampleSizeInBits());
  69.             System.out.printf("      big endian:         %b\n", format.isBigEndian());
  70.             System.out.printf("      line supported:         %b\n", isSupport);
  71.         }
  72.        
  73.        
  74.        
  75.         try {
  76.         info = new DataLine.Info(SourceDataLine.class, format2);
  77.         SourceDataLine sourceLine = (SourceDataLine) AudioSystem.getLine(info);
  78.         sourceLine.open(format2);
  79.         sourceLine.start();
  80.         sourceLine.drain();
  81.         byte[] data = new byte[(int)inputFile.length()];
  82.         int size = audioInputStream.read(data);
  83.         int writeCount =sourceLine.write(data, 0, size);
  84.         Thread.sleep(2000);
  85.         sourceLine.stop();;
  86.         sourceLine.close();
  87.         } catch (Exception e)
  88.         {
  89.             e.printStackTrace();
  90.         }
  91.        
  92.         try {
  93.             Thread.sleep(500);
  94.             Clip clip = AudioSystem.getClip();
  95.             audioInputStream.close();
  96.             audioInputStream = AudioSystem.getAudioInputStream(new File(args[0]).getAbsoluteFile());
  97.             // open audioInputStream to the clip
  98.             clip.open(audioInputStream);
  99.            
  100.             clip.start();
  101.             Thread.sleep(2000);
  102.             clip.close();
  103.            
  104.         }catch (Exception e) {
  105.             e.printStackTrace();
  106.         } finally {
  107.            
  108.         }
  109.  
  110.  
  111.     }
  112.     public static Vector<AudioFormat> getSupportedFormats(Class<?> dataLineClass) {
  113.         /*
  114.          * These define our criteria when searching for formats supported
  115.          * by Mixers on the system.
  116.          */
  117.         float sampleRates[] = { (float) 8000.0, (float) 16000.0, (float) 44100.0 };
  118.         int channels[] = { 1, 2 };
  119.         int bytesPerSample[] = { 2 };
  120.  
  121.         AudioFormat format;
  122.         DataLine.Info lineInfo;
  123.  
  124.         //      SystemAudioProfile profile = new SystemAudioProfile(); // Used for allocating MixerDetails below.
  125.         Vector<AudioFormat> formats = new Vector<AudioFormat>();
  126.  
  127.         for (Mixer.Info mixerInfo : AudioSystem.getMixerInfo()) {
  128.             for (int a = 0; a < sampleRates.length; a++) {
  129.                 for (int b = 0; b < channels.length; b++) {
  130.                     for (int c = 0; c < bytesPerSample.length; c++) {
  131.                         format = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,
  132.                                 sampleRates[a], 8 * bytesPerSample[c], channels[b], bytesPerSample[c],
  133.                                 sampleRates[a], false);
  134.                         lineInfo = new DataLine.Info(dataLineClass, format);
  135.                         //if (AudioSystem.isLineSupported(lineInfo)) {
  136.                             /*
  137.                              * TODO: To perform an exhaustive search on supported lines, we should open
  138.                              * TODO: each Mixer and get the supported lines. Do this if this approach
  139.                              * TODO: doesn't give decent results. For the moment, we just work with whatever
  140.                              * TODO: the unopened mixers tell us.
  141.                              */
  142.                             //if (AudioSystem.getMixer(mixerInfo).isLineSupported(lineInfo)) {
  143.                                 formats.add(format);
  144.                             //}
  145.                         //}
  146.                     }
  147.                 }
  148.             }
  149.         }
  150.         return formats;
  151.     }
  152.  
  153.     public static void getFormats() throws LineUnavailableException{
  154.         String mixerName = System.getProperty("audio.mixer");      
  155.         Mixer mixer = AudioSystem.getMixer(getMixerInfo(mixerName));
  156.         try {
  157.             mixer.open();
  158.         } catch (LineUnavailableException e) {
  159.             // TODO Auto-generated catch block
  160.             e.printStackTrace();
  161.         }
  162.  
  163.         Mixer.Info[] mixInfo = AudioSystem.getMixerInfo();
  164.  
  165.         for(Mixer.Info mixinf  :mixInfo)
  166.         {
  167.             System.out.printf("Supported Lines of default mixer (%s):\n\n", mixinf.getName());
  168.             System.out.println("\nMixer ClassName: " + mixinf.getClass().getName());
  169.             System.out.println("Mixer Desc: "+ mixinf.getDescription());
  170.             System.out.println("Vendor: " +mixinf.getVendor());
  171.             System.out.println();
  172.         }
  173.  
  174.  
  175.         Mixer.Info[] mixerInfos = AudioSystem.getMixerInfo();
  176.         for (Mixer.Info info: mixerInfos){
  177.             Mixer m = AudioSystem.getMixer(info);
  178.             Line.Info[] lineInfos = m.getSourceLineInfo();
  179.             for (Line.Info lineInfo:lineInfos){
  180.                 System.out.println (info.getName()+"\t---\t"+lineInfo);
  181.                 Line line = m.getLine(lineInfo);
  182.                 System.out.println("\t-----\t"+line);
  183.             }
  184.             lineInfos = m.getTargetLineInfo();
  185.             for (Line.Info lineInfo:lineInfos){
  186.                 System.out.println (m+"\t---\t"+lineInfo);
  187.                 Line line = m.getLine(lineInfo);
  188.                 System.out.println("\t-----\t"+line);
  189.             }
  190.  
  191.         }
  192.         Line.Info [] lines = mixer.getSourceLineInfo();
  193.         if (lines !=null)
  194.         {
  195.             if (lines.length > 0)
  196.             {
  197.                 System.out.println("Mixer Num Source Info Lines: " + lines.length);
  198.             }else {
  199.                 System.out.println("Mixer Has 0 Source Info Lines: ");
  200.             }
  201.         } else {
  202.             System.out.println("Mixer.getSourceLines is null");
  203.         }
  204.  
  205.         lines = mixer.getTargetLineInfo();
  206.  
  207.         if (lines !=null)
  208.         {
  209.             if (lines.length > 0)
  210.             {
  211.                 System.out.println("Mixer Num Target Info Lines: " + lines.length);
  212.             }else {
  213.                 System.out.println("Mixer Has 0 Target Info Lines: ");
  214.             }
  215.         } else {
  216.             System.out.println("Mixer.getTargetLines is null");
  217.         }
  218.  
  219.         for(Line.Info info : mixer.getSourceLineInfo()) {
  220.             if(SourceDataLine.class.isAssignableFrom(info.getLineClass())) {
  221.                 SourceDataLine.Info info2 = (SourceDataLine.Info) info;
  222.                 System.out.println(info2);
  223.                 System.out.printf("  max buffer size: \t%d\n", info2.getMaxBufferSize());
  224.                 System.out.printf("  min buffer size: \t%d\n", info2.getMinBufferSize());
  225.                 AudioFormat[] formats = info2.getFormats();
  226.                 System.out.println("  Supported Audio formats: ");
  227.                 for(AudioFormat format : formats) {
  228.                     boolean isSupport = AudioSystem.isLineSupported(new DataLine.Info(SourceDataLine.class, format));
  229.                     System.out.println("    "+format);
  230.                     System.out.printf("      encoding:           %s\n", format.getEncoding());
  231.                     System.out.printf("      channels:           %d\n", format.getChannels());
  232.                     System.out.printf(format.getFrameRate()==-1?"":"      frame rate [1/s]:   %s\n", format.getFrameRate());
  233.                     System.out.printf("      frame size [bytes]: %d\n", format.getFrameSize());
  234.                     System.out.printf(format.getSampleRate()==-1?"":"      sample rate [1/s]:  %s\n", format.getSampleRate());
  235.                     System.out.printf("      sample size [bit]:  %d\n", format.getSampleSizeInBits());
  236.                     System.out.printf("      big endian:         %b\n", format.isBigEndian());
  237.                     System.out.printf("      line supported:         %b\n", isSupport);
  238.  
  239.  
  240.                 }
  241.                 System.out.println();
  242.             } else {
  243.                 System.out.println(info.toString());
  244.             }
  245.             System.out.println();
  246.         }
  247.  
  248.         mixer.close();
  249.  
  250.     }
  251.  
  252.     public static Mixer.Info getMixerInfo(final String name) {
  253.         for (final Mixer.Info info : AudioSystem.getMixerInfo()) {
  254.             System.out.println("Found audio mixer: " + (Object)info.getName());
  255.             if (info.getName().equalsIgnoreCase(name)) {
  256.                 return info;
  257.             }
  258.         }
  259.         return null;
  260.     }
  261. }
Advertisement
Add Comment
Please, Sign In to add comment