Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to capture sound from microphone with java sound API?

The tutorial http://download.oracle.com/javase/tutorial/sound/capturing.html does not cover how to select microphone.

I am enumerating mixers with the following code

System.out.println("Searching for microphones");
for(Mixer.Info mixerinfo : AudioSystem.getMixerInfo()) {
    mixer = AudioSystem.getMixer(mixerinfo); 

    //System.out.println(mixerinfo.toString());
    if( mixer.isLineSupported(Port.Info.MICROPHONE) ) {
        mixers.add(mixer);
        System.out.println(Integer.toString(mixers.size()) + ": " + mixerinfo.toString());
    }
}

i.e. by presense of microphone input. But next, having a mixer, I can't get line to read.

If I use mixer.getTargetLineInfo(), I receive an array of one Info, which when passing to mixer.getLine returns an object of type com.sun.media.sound.PortMixer$PortMixerPort, which is not ducumented.

If I use mixer.getTargetLines() I get an empty array.

If I create my own DataLine.Info and pass it to the mixer's getLine, I get unsupported exception.

So, what to do?

like image 773
Dims Avatar asked Jun 07 '11 05:06

Dims


People also ask

How do I access my microphone in Java?

try { line = (TargetDataLine) AudioSystem. getLine(info); line. open(format); } catch (LineUnavailableException ex) { // Handle the error ... } } }

How do you record audio in Java?

Obtain a TargetDataLine object which represents an input data line from which audio data can be captured, using the method getLineInfo(DataLine.Info) of the AudioSystem class. Open and start the target data line to begin capturing audio data. Create an AudioInputStream object to read data from the target data line.

What is Java Sound API?

"The Java Sound API is a low-level API for effecting and controlling input. and output of audio media. It provides explicit control over the capabilities. commonly required for audio input and output in a framework that promotes. extensibility and flexibility."

How is audio captured?

Acoustic analog recording is achieved by a microphone diaphragm that senses changes in atmospheric pressure caused by acoustic sound waves and records them as a mechanical representation of the sound waves on a medium such as a phonograph record (in which a stylus cuts grooves on a record).


1 Answers

Left-field suggestion.

Provide a visual rendering of each sound line, in a component along the lines of the AudioPlotPanel or a simpler RMS volume. It should not take the user too long to figure which sound line they are yodeling through. ;)

like image 133
Andrew Thompson Avatar answered Oct 03 '22 13:10

Andrew Thompson