java / pulseaudio question

Knute Johnson ubuntu at knutejohnson.com
Fri Sep 21 01:15:13 UTC 2012


Richard:

It works fine on my Oracle (used to be Sun) JDK 7 installations under XP 
and Linux and on JDK 6 under XP (didn't have a 6 on Linux to try)  So 
maybe it will work if you put a Sun JDK on your system.  When I look at 
my Linux installation I have ALSA and that's what Java is using I think. 
  So it could still be something to do with PulseAudio but I would 
certainly try loading a Sun JDK to test it.  I'm including some Java 
code that I use to look at audio files to see if they will play under Java.

import java.io.*;
import javax.sound.sampled.*;

public class Play {
     public static void main(String[] args) {
         class MyLineListener implements LineListener {
             public void update(LineEvent le) {
                 LineEvent.Type type = le.getType();
                 System.out.println(type);
             }
         };

         try {
             System.out.println("Audio File Format: " +
              AudioSystem.getAudioFileFormat(new File(args[0])));
             AudioInputStream fis =
              AudioSystem.getAudioInputStream(new File(args[0]));
             System.out.println("Input AudioFormat: " + fis.getFormat());
             AudioInputStream ais = AudioSystem.getAudioInputStream(
              AudioFormat.Encoding.PCM_SIGNED,fis);
             AudioFormat af = ais.getFormat();
             System.out.println("Converted AudioFormat: " + af.toString());

             int frameRate = (int)af.getFrameRate();
             System.out.println("Frame Rate: " + frameRate);
             int frameSize = af.getFrameSize();
             System.out.println("Frame Size: " + frameSize);

             SourceDataLine line = AudioSystem.getSourceDataLine(af);
             line.addLineListener(new MyLineListener());

             line.open(af);
             int bufSize = line.getBufferSize();
             System.out.println("Buffer Size: " + bufSize);

             line.start();

             byte[] data = new byte[bufSize];
             int bytesRead;

             while ((bytesRead = ais.read(data,0,data.length)) != -1)
                 line.write(data,0,bytesRead);

             line.drain();
             line.stop();
             line.close();
         } catch (Exception e) {
             System.out.println(e);
         }
     }
}

And this Sun program will list the audio lines, ports and controls. 
Sorry about the formatting, Sun's fault.

/*
  * Copyright (c) 2003 Sun Microsystems, Inc. All Rights Reserved.
  *
  * Sun grants you ("Licensee") a non-exclusive, royalty free, license to
  use, * modify and redistribute this software in source and binary code
  form, * provided that i) this copyright notice and license appear on all
  copies of * the software; and ii) Licensee does not utilize the software
  in a manner * which is disparaging to Sun. * * This software is provided
  "AS IS," without a warranty of any kind. ALL * EXPRESS OR IMPLIED
  CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY * IMPLIED
  WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR *
  NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE
  * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING,
  MODIFYING * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT
  WILL SUN OR ITS * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR
  DATA, OR FOR DIRECT, * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR
  PUNITIVE DAMAGES, HOWEVER * CAUSED AND REGARDLESS OF THE THEORY OF
  LIABILITY, ARISING OUT OF THE USE OF * OR INABILITY TO USE SOFTWARE, EVEN
  IF SUN HAS BEEN ADVISED OF THE * POSSIBILITY OF SUCH DAMAGES. * * This
  software is not designed or intended for use in on-line control of *
  aircraft, air traffic, aircraft navigation or aircraft communications; or
  in * the design, construction, operation or maintenance of any nuclear *
  facility. Licensee represents and warrants that it will not use or *
  redistribute the Software for such purposes. */

import javax.sound.sampled.*;

public class ListPorts {
  public static void main(String[] args) {
   Mixer.Info[] aInfos = AudioSystem.getMixerInfo();
   for (int i = 0; i < aInfos.length; i++) {
    try {
     Mixer mixer = AudioSystem.getMixer(aInfos[i]);
     mixer.open();
     try {
      System.out.println(aInfos[i]);
      printPorts(mixer, mixer.getSourceLineInfo());
      printPorts(mixer, mixer.getTargetLineInfo());
     } finally {
      mixer.close();
     }
    } catch (Exception e) {
     e.printStackTrace();
    }
   }
   if (aInfos.length == 0) {
    System.out.println("[No mixers available]");
   }
   System.exit(0);
  }

  public static void printPorts(Mixer mixer, Line.Info[] infos) {
   for (int i = 0; i<infos.length; i++) {
    try {
     if (infos[i] instanceof Port.Info) {
      Port.Info info = (Port.Info) infos[i];
      System.out.println("  Port "+info);
      Port port = (Port) mixer.getLine(info);
      port.open();
      try {
       printControls(port.getControls());
      } finally {
       port.close();
      }
     }
    } catch (Exception e) {
     e.printStackTrace();
    }
   }
  }

  public static void printControls(Control[] controls) {
   for (int i = 0; i<controls.length; i++) {
    printControl("    ", "Controls["+i+"]: ", controls[i]);
   }
   if (controls.length == 0) {
    System.out.println("    [no controls]");
   }

  }

  static boolean balanceTested = false;

  public static void printControl(String indent, String id, Control
  control) {
   if (control instanceof BooleanControl) {
    BooleanControl ctrl = (BooleanControl) control;
    System.out.println(indent+id+"BooleanControl: "+ctrl);
    //try {
    //    Thread.sleep(500);
    //    ctrl.setValue(!ctrl.getValue());
    //    Thread.sleep(500);
    //    ctrl.setValue(!ctrl.getValue());
    //} catch (Exception e) {}
   }
   else if (control instanceof CompoundControl) {
    CompoundControl ctrl = (CompoundControl) control;
    Control[] ctrls = ctrl.getMemberControls();
    System.out.println(indent+id+"CompoundControl: "+control);
    for (int i=0; i<ctrls.length; i++) {
     printControl(indent+"  ", "MemberControls["+i+"]: ", ctrls[i]);
    }
   }
   else if (control instanceof EnumControl) {
    EnumControl ctrl = (EnumControl) control;
    Object[] values = ctrl.getValues();
    Object value = ctrl.getValue();
    System.out.println(indent+id+"EnumControl: "+control);
    for (int i=0; i<values.length; i++) {
     if (values[i] instanceof Control) {
      printControl(indent+"  ", "Values["+i+"]:"+
       ((values[i]==value)?"*":""), (Control) values[i]);
     } else {
      System.out.println(indent+"  Values["+i+"]:"+
       ((values[i]==value)?"*":"")+values[i]);
     }
    }
   }
   else if (control instanceof FloatControl) {
    FloatControl ctrl = (FloatControl) control;
    System.out.println(indent+id+"FloatControl: "+ctrl);
    //try {
    //    Thread.sleep(500);
    //    float x = ctrl.getValue();
    //    ctrl.setValue((float) (Math.random()*(ctrl.getMaximum() -
    //   ctrl.getMinimum()) + ctrl.getMinimum())); //    Thread.sleep(1000);
    //    ctrl.setValue(x); //} catch (Exception e) {} //if 
(ctrl.getType() ==
    //   FloatControl.Type.BALANCE && !balanceTested) {
    //    balanceTested = true;
    //    for (float y = -1.0f; y<=1.0f; y+=0.02) { // 
ctrl.setValue(y);
    //        System.out.println("  Set to "+y);
    //   System.out.println("     res"+ctrl.getValue()); //    } //}
   } else {
    System.out.println(indent+id+"Control: "+control);
   }
  }
}


-- 

Knute Johnson




More information about the ubuntu-users mailing list