1

According to my research, Java's sound api does not play well with OsX. It has a hard time determining the active input, so it generally defaults to the first system input.

My solution is to iterate through an array of input ports, recording a couple milliseconds of audio and comparing those pieces. Whichever one has the greatest amplitude, I'll use as my input.

My question is, what would the best method be for generating an array of all input ports available to Java?

tylerdavis
  • 185
  • 4
  • 13
  • For an example of showing the available lines, see the [MediaTypes source](http://stackoverflow.com/questions/5304001/javasound-mixer-with-both-ports-and-datalines/5337619#5337619). That might give you some ideas on how to delve into them. – Andrew Thompson Jun 21 '11 at 18:38

2 Answers2

3

You can list all of the available Mixer objects using the following:

Mixer.Info[] mixers = AudioSystem.getMixerInfo();
for (Mixer.Info mixerInfo : mixers){
    System.out.println(mixerInfo);
}

On my system, a Mac, this is the result:

Java Sound Audio Engine, version 1.0
Built-in Input, version Unknown Version
Built-in Microphone, version Unknown Version

Edit

Here's how to extract a list of valid Target lines that you can get audio input from:

Mixer.Info[] mixers = AudioSystem.getMixerInfo();
List<Line.Info> availableLines = new ArrayList<Line.Info>();
for (Mixer.Info mixerInfo : mixers){
    System.out.println("Found Mixer: " + mixerInfo);

    Mixer m = AudioSystem.getMixer(mixerInfo);

    Line.Info[] lines = m.getTargetLineInfo();

    for (Line.Info li : lines){
        System.out.println("Found target line: " + li);
        try {
            m.open();
            availableLines.add(li);                  
        } catch (LineUnavailableException e){
            System.out.println("Line unavailable.");
        }
    }  
}

System.out.println("Available lines: " + availableLines);

Once you have the Line.Info object, you can get the TargetDataLine associated with the Line.Info object by calling AudioSystem.getLine() and using that Line.Info as a parameter.

Dan
  • 7,286
  • 6
  • 49
  • 114
  • These are the mixers, not the actual ports. – tylerdavis Jun 21 '11 at 20:50
  • Each Mixer has a set of associated Lines that you can access using the getTargetLineInfo() or getSourceLineInfo() methods. So you can access the Microphone line (or port) through the mixer with the name "Built-in Microphone." – David Rueter Jun 21 '11 at 21:49
0

Basics on how to determine what resources are available can be found here: Accessing Audio System Resources

I found this section to be the most helpful in terms of example code, in the Java Sound tutorials: http://download.oracle.com/javase/tutorial/sound/converters.html

Phil Freihofner
  • 7,645
  • 1
  • 20
  • 41