3

I would like to play back 5.1 audio in java. Previously, I've worked with stereo audio and it's been pretty easy, however increasing the number of channels past 2 gives me problems immediately: java reports that there are no devices that support an AudioFormat with > 2 channels. However, using the win32 API directly in C/C++ with WAVEFORMATPCMEX, waveOutOpen(), waveOutPrepareHeader(), and waveOutWrite(), I can write to all 6 speakers (5 speakers and the sub). Following the java tutorial (http://docs.oracle.com/javase/tutorial/sound/sampled-overview.html), and iterating through the audio devices and printing out their supported behaviors, I can't find any device that states it can support > stereo audio. The code I used to iterate is (import javax.sampled.*):

Mixer.Info[] mixers = AudioSystem.getMixerInfo();
for(Mixer.Info info : mixers)
{
    System.out.println(info);
    Mixer mixer = AudioSystem.getMixer((info));
    Line.Info[] linfos = mixer.getSourceLineInfo();
    for(Line.Info linfo : linfos)
    {
        if(linfo instanceof DataLine.Info)
        {
            System.out.println("-" + linfo);
            AudioFormat[] formats = ((DataLine.Info)linfo).getFormats();
            for(AudioFormat format : formats)
            {
                System.out.println("--" + format);
            }
        }
    }
}

I've also tried the slightly-different iterating code as described here: how do I get Mixer channels layout in java. Even trying to just get a SourceDataLine directly from the AudioSystem by requesting it with a format that contains 6 channels fails with:

AudioFormat format = new AudioFormat(44100, 16, 6, true, false);
try(SourceDataLine line = AudioSystem.getSourceDataLine(format))
{
    line.open(format);
    line.start();
}
catch(LineUnavailableException lue)
{
    System.err.println("Cannot grab output device for playing audio.");
}

Exception in thread "main" java.lang.IllegalArgumentException: No line matching interface SourceDataLine supporting format PCM_SIGNED 44100.0 Hz, 16 bit, 6 channels, 12 bytes/frame, little-endian is supported.

I've tried various formats of tweaking the endianness, bytes per frame, sample rate, and PCM_SIGNED/PCM_UNSIGNED to no avail. I know that my system can support it because I can do it via the win32 API directly. Am I missing something? Is there some configuration I need to do? Has anyone had any luck with multiple-channel audio in java? Thanks!

Community
  • 1
  • 1
user450775
  • 467
  • 1
  • 5
  • 14

0 Answers0