Link sound input to specific output device with java api

160 views Asked by At

I am trying to perform a simple task, select an input device and set the output device.

The use case is as follows, I have 3.5mm jacks and my user can select the output device (headphones or speaker) from a list.

I can play a sound on a given device (with clip), I can control the input device (mute/volume), but I haven't found any way to specify the target line, it's always the system default.

I can get the mixer

Optional<Mixer.Info> optJackInMixerInfo = Arrays.stream(AudioSystem.getMixerInfo())
        .filter(mixerInfo -> {
            // Filter based on the device name. 
        })
        .findFirst();
Mixer m = AudioSystem.getMixer(jackInMixerInfo);

// The target

Line.Info[] lineInfos = m.getTargetLineInfo();
for (Line.Info lineInfo : lineInfos) {
  m.getLine(lineInfo);
  System.out.println("ici");
}

I got only the "master volume control".

How can I select the output device ? I can be happy with changing the system default device too.

1

There are 1 answers

4
Phil Freihofner On

The naming of TargetDataLine and SourceDataLine is kind of backwards. Outputs to the local sound system for playback are directed to a SourceDataLine and inputs to Java like microphone lines use TargetDataLine. I used to know why they were named this way but it's slipped my mind at the moment.

There is a tutorial Accessing Audio System Resources with specifics.

Most computers only have a limited number of float controls available, with "master volume" being the one most likely to be implemented. You would use this to alter the volume of the output. Another tutorial in the series, Processing Audio with Controls covers this topic. For myself, I generally convert the audio stream to PCM and handle volume directly (multiply each value by a factor that ranges from 0 to 1) and then convert back to a byte stream, rather than rely on controls which may or may not be present.