I need to capture sounds from line-in port, not microphone.
Although I accomplished recording from microphone, I can not accomplish capturing sounds from line-in ports or specific ports. How can I handle this problem?
I need to capture sounds from line-in port, not microphone.
Although I accomplished recording from microphone, I can not accomplish capturing sounds from line-in ports or specific ports. How can I handle this problem?
I haven't figured this either, but to you actually need
mixer.getSourceLineInfo()
as this is the view from the Mixer... really confusing !!!
A targetDataLine is a recordable input line, but to see where these lines may come from you need to look up the Ports the mixer has ie (MICROPHONE , LINE_IN, SPDIF, ) so you need to call
mixer.getSourceLineInfo()
This only gives the Ports. these can be used to, for example, control the recording volume of a LINE_IN input, BUT you cannot record directly from a PORT object.
You need to use DataLine.Info targetDataLineInfo = new DataLine.Info(TargetDataLine.class, AudioFormat);
where AudioFormat is some user defined format like
audioFormat = new AudioFormat(
Encoding.PCM_SIGNED,
sampleRate,
bitRate,
monoOrStereo,
monoOrStereo * 2, //
sampleRate,
false);
You then get the line from the preferred Mixer:
Mixer.getLine(audioFormat);
This will give you a recordable line , as you have probably already done...
What I can't figure out is, like you, how to select a Port such as LINE_IN and create a matching TargetDataLine that the Port Object can control, and I've searched and searched..
()
Anybody with a working example out there..
import javax.sound.sampled.*;
/**
*
* @author d07114915
*
* Class to get a mixer with a specified recordable audio format from a specified port
* For instance get a 44.1kHz 16bit record line for a "line in" input
*/
public class MixerMatcher {
private static final String THE_INPUT_TYPE_I_WANT = "MICROPHONE";
private static final String THE_NAME_OF_THE_MIXER_I_WANT_TO_GET_THE_INPUT_FROM = "Realtek HD Audio Input";
private static final AudioFormat af = new AudioFormat(
AudioFormat.Encoding.PCM_SIGNED,
44100.0F,
16,
2,
2 * 2,
44100.0F,
false);
private static final DataLine.Info targetDataLineInfo = new DataLine.Info(TargetDataLine.class, af);
private static final Port.Info myInputType = new Port.Info((Port.class), THE_INPUT_TYPE_I_WANT, true);
private static TargetDataLine targetDataLine = null;
public static void main(String[] args) {
Mixer portMixer = null;
Mixer targetMixer = null;
try {
for (Mixer.Info mi : AudioSystem.getMixerInfo()) {
// System.out.println("-" +mi.getName() + "-");
if (mi.getName().equals(THE_NAME_OF_THE_MIXER_I_WANT_TO_GET_THE_INPUT_FROM)) {
System.out.println("Trying to get portMixer for :" + mi.getName());
portMixer = getPortMixerInfoFor(mi);
if (portMixer != null) {
System.out.println(portMixer.getMixerInfo().toString());
targetMixer = AudioSystem.getMixer(mi);
break;
}
}
}
if (targetMixer != null) {
targetMixer.open();
targetDataLine = (TargetDataLine) targetMixer.getLine(targetDataLineInfo);
System.out.println("Got TargetDataLine from :" + targetMixer.getMixerInfo().getName());
portMixer.open();
Port port = (Port) portMixer.getLine(myInputType);
port.open();
Control[] controls = port.getControls();
System.out.println((controls.length > 0 ? "Controls for the "+ THE_INPUT_TYPE_I_WANT + " port:" : "The port has no controls."));
for (Control c : controls) {
System.out.println(c.toString());
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
//return the portMixer that corresponds to TargetMixer
private static Mixer getPortMixerInfoFor(Mixer.Info mixerInfo) {
//Check this out for interest
//http://www.java-forum.org/spiele-multimedia-programmierung/94699-java-sound-api-zuordnung-port-mixer-input-mixer.html
try {
// get the requested mixer
Mixer targetMixer = AudioSystem.getMixer(mixerInfo);
targetMixer.open();
//Check if it supports the desired format
if (targetMixer.isLineSupported(targetDataLineInfo)) {
System.out.println(mixerInfo.getName() + " supports recording @ " + af);
//now go back and start again trying to match a mixer to a port
//the only way I figured how is by matching name, because
//the port mixer name is the same as the actual mixer with "Port " in front of it
// there MUST be a better way
for (Mixer.Info portMixerInfo : AudioSystem.getMixerInfo()) {
String port_string = "Port ";
if ((port_string + mixerInfo.getName()).equals(portMixerInfo.getName())) {
System.out.println("Matched Port to Mixer:" + mixerInfo.getName());
Mixer portMixer = AudioSystem.getMixer(portMixerInfo);
portMixer.open();
//now check the mixer has the right input type eg LINE_IN
boolean lineTypeSupported = portMixer.isLineSupported((Line.Info) myInputType);
System.out.println(portMixerInfo.getName() +" does " + (lineTypeSupported? "" : "NOT") + " support " + myInputType.getName());
if (lineTypeSupported) {
portMixer.close();
targetMixer.close();
return portMixer;
}
portMixer.close();
}
}
}
targetMixer.close();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
Running this I get:
Trying to get portMixer for :Realtek HD Audio Input
Realtek HD Audio Input supports recording @ PCM_SIGNED 44100.0 Hz, 16 bit, stereo, 4 bytes/frame, little-endian
Matched Port to Mixer:Realtek HD Audio Input
Port Realtek HD Audio Input does support MICROPHONE
Port Realtek HD Audio Input, version 5.10
Got TargetDataLine from :Realtek HD Audio Input
Controls for the MICROPHONE port:
Mic Volume Control containing Select, Microphone Boost, Volume, and Balance Controls.
Changing the mixer and input type preferences to "USB Sound Device " and ""LINE_IN" respectively I get the following : (NOTE there 8 white spaces in the mixer name after the word "Device", but they won't display on this web page!)
Trying to get portMixer for :USB Sound Device
Trying to get portMixer for :USB Sound Device
USB Sound Device supports recording @ PCM_SIGNED 44100.0 Hz, 16 bit, stereo, 4 bytes/frame, little-endian
Matched Port to Mixer:USB Sound Device
Port USB Sound Device does support LINE_IN
Port USB Sound Device , version 0.16
Got TargetDataLine from :USB Sound Device
Controls for the LINE_IN port:
Line Control containing Select, Mute, Volume, and Balance Controls.
Here the USB sound card shows an in port and an out port so one of them doesn't support the LINE_IN, this probably is because it is an output so might allow to record "stereo mix" or some other output type
Hope this works and helps someone.... as the Java docs are fairly vague ... Tested on Windows, but I think Linux won't recognise the Port names such as LINE_IN, so you'll need to check what the OS ports are, and possibly a few other things like need a substring for the mixer name etc... On my Linux the Microphone is called "Capture"...
Check jsresources.org FAQ for more info
Any improvements of errors etc let me know.
d07114915
Familiarize yourself with the TargetDataLine
class. Also, from its own Javadoc:
The target data line can be obtained from a mixer by invoking the getLine method of Mixer with an appropriate DataLine.Info object.
Specifically, I'd also review Mixer.getTargetLineInfo()
, and inspect the returned output to choose the line that matches the line-in port that you're looking for.
Try this. If it works let me know, as I can't test it totally myself on my laptop.
You can then make up your own function to get the PortMixer
, the ActualMixer
and the TargateDataLine
with the specified input type and/or desired mixer.
private void testGettingInput() {
//Check this out for interest
//http://www.java-forum.org/spiele-multimedia-programmierung/94699-java-sound-api-zuordnung-port-mixer-input-mixer.html
final String newLine = System.getProperty("line.separator");
final String inputTypeString = "LINE_IN"; // or COMPACT_DISC or MICROPHONE etc ...
final Port.Info myInputType = new Port.Info((Port.class), inputTypeString, true);
final AudioFormat af = new AudioFormat(
Encoding.PCM_SIGNED,
44100.0F,
16,
2,
2 * 2,
44100.0F,
false);
final DataLine.Info targetDataLineInfo = new DataLine.Info(TargetDataLine.class, af);
TargetDataLine targetDataLine;
//Go through the System audio mixers
for (Mixer.Info mixerInfo : AudioSystem.getMixerInfo()) {
try {
Mixer targetMixer = AudioSystem.getMixer(mixerInfo);
targetMixer.open();
//Check if it supports the desired format
if (targetMixer.isLineSupported(targetDataLineInfo)) {
System.out.println(mixerInfo.getName() + " supports recording @" + af);
//now go back and start again trying to match a mixer to a port
//the only way I figured how is by matching name, because
//the port mixer name is the same as the actual mixer with "Port " in front of it
// there MUST be a better way
for (Mixer.Info mifo : AudioSystem.getMixerInfo()) {
String port_string = "Port ";
if ((port_string + mixerInfo.getName()).equals(mifo.getName())) {
System.out.println("Matched Port to Mixer:" + mixerInfo.getName());
Mixer portMixer = AudioSystem.getMixer(mifo);
portMixer.open();
portMixer.isLineSupported((Line.Info) myInputType);
//now check the mixer has the right input type eg LINE_IN
if (portMixer.isLineSupported((Line.Info) myInputType)) {
//OK we have a supported Port Type for the Mixer
//This has all matched (hopefully)
//now just get the record line
//There should be at least 1 line, usually 32 and possible unlimited
// which would be "AudioSystem.Unspecified" if we ask the mixer
//but I haven't checked any of this
targetDataLine = (TargetDataLine) targetMixer.getLine(targetDataLineInfo);
System.out.println("Got TargetDataLine from :" + targetMixer.getMixerInfo().getName());
return;
}
}
}
System.out.println(newLine);
}
targetMixer.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}