edit: I am now using Jack (Jack Audio Connection Kit). See answer below.
I have a soundcard on my Raspberry Pi with 8 output channels (four stereo channels), an Octosound card. What I want to do is select one of the channels to route sound to. With this code I print info of the sound card:
mixers = AudioSystem.getMixerInfo();
for (Mixer.Info mixerInfo : mixers) {
logger.debug("\n");
logger.debug("Found Mixer: " + mixerInfo);
Mixer m = AudioSystem.getMixer(mixerInfo);
Line.Info[] sourceLines = m.getSourceLineInfo();
for (Line.Info li : sourceLines) {
logger.debug("Found source line: " + li + " " + li.getClass());
if (li instanceof Port.Info) {
Port.Info portInfo = (Port.Info) li;
logger.debug("port found " + portInfo.getName() + " is source " + portInfo.isSource());
sourceDataLines.add(portInfo);
}
}
Line.Info[] targetLines = m.getTargetLineInfo();
for (Line.Info li : targetLines) {
logger.debug("Found target line: " + li + " " + li.getClass());
outputLines.add(li);
if (li instanceof Port.Info) {
Port.Info portInfo = (Port.Info) li;
logger.debug("port found " + portInfo.getName() + " is source " + portInfo.isSource());
outputPorts.add(portInfo);
}
}
}
private void lineClose(int soundPort) throws LineUnavailableException {
Port.Info lineInfo = outputPorts.get(soundPort);
Line line = (Port) AudioSystem.getLine(lineInfo);
line.close();
}
private void lineOpen(int l) throws LineUnavailableException {
for (int i = 0; i < outputPorts.size(); i++) {
Port.Info lineInfo = outputPorts.get(i);
Line line = (Port) AudioSystem.getLine(lineInfo);
if (l == i) {
line.open();
} else {
line.close();
}
}
}
This is the output I get:
Found Mixer: audioinjectoroc [default], version 4.9.41-v7+
Found source line: interface SourceDataLine supporting 84 audio formats, and buffers of at least 32 bytes class com.sun.media.sound.DirectAudioDevice$DirectDLI
Found source line: interface Clip supporting 84 audio formats, and buffers of at least 32 bytes class com.sun.media.sound.DirectAudioDevice$DirectDLI
Found target line: interface TargetDataLine supporting 84 audio formats, and buffers of at least 32 bytes class com.sun.media.sound.DirectAudioDevice$DirectDLI
Found Mixer: audioinjectoroc [plughw:0,0], version 4.9.41-v7+
Found source line: interface SourceDataLine supporting 96 audio formats, and buffers of at least 32 bytes class com.sun.media.sound.DirectAudioDevice$DirectDLI
Found source line: interface Clip supporting 96 audio formats, and buffers of at least 32 bytes class com.sun.media.sound.DirectAudioDevice$DirectDLI
Found target line: interface TargetDataLine supporting 96 audio formats, and buffers of at least 32 bytes class com.sun.media.sound.DirectAudioDevice$DirectDLI
Found Mixer: Port audioinjectoroc [hw:0], version 4.9.41-v7+
Found source line: ADC1 source port class com.sun.media.sound.PortMixer$PortInfo
port found ADC1 is source true
Found source line: ADC2 source port class com.sun.media.sound.PortMixer$PortInfo
port found ADC2 is source true
Found source line: ADC3 source port class com.sun.media.sound.PortMixer$PortInfo
port found ADC3 is source true
Found target line: DAC1 target port class com.sun.media.sound.PortMixer$PortInfo
port found DAC1 is source false
Found target line: DAC2 target port class com.sun.media.sound.PortMixer$PortInfo
port found DAC2 is source false
Found target line: DAC3 target port class com.sun.media.sound.PortMixer$PortInfo
port found DAC3 is source false
Found target line: DAC4 target port class com.sun.media.sound.PortMixer$PortInfo
port found DAC4 is source false
Now this is the code I use to output sound from a wav file:
String path = soundDirectory + soundUrl;
InputStream is = new FileInputStream(path);
BufferedInputStream bis = new BufferedInputStream(is);
AudioInputStream inputStream = AudioSystem.getAudioInputStream(bis);
AudioFormat format = inputStream.getFormat();
Mixer.Info mi = mixers[0];
SourceDataLine sourceDataLine = (SourceDataLine) AudioSystem.getSourceDataLine(format,mi);
sourceDataLine.open(format);
sourceDataLine.start();
byte[] buf = new byte[1024];
int bytesRead;
while ((bytesRead = inputStream.read(buf)) != -1){
sourceDataLine.write(buf, 0, bytesRead);
}
inputStream.close();
sourceDataLine.drain();
sourceDataLine.stop();
sourceDataLine.close();
lineClose(soundPort);
I have tried a number of things, but in all cases, sound comes out of all ouputs.