Java Sound API on OS X with USB Sound Card

apiaudiojavamacosusb

I have a working program written in Java with the Java Sound API that is able to play multiple mp3 audio files at the same time to different USB Sound Cards in the same computer. It works fine in Linux. In Mac OS X, however, it does not.

The listing of my sound Mixer Infos is:

[0] Java Sound Audio Engine
[1] Built-in Input
[2] USB Advanced Audio Device

If I choose mixer [0] to play the file it does so successfully with the built-in sound card. If I choose mixer [2] to play the file, I get the following exception:

Exception in thread "Thread-1" java.lang.IllegalArgumentException: Line unsupported: interface SourceDataLine supporting format PCM_SIGNED 44100.0 Hz, 16 bit, stereo, 4 bytes/frame, little-endian
at com.sun.media.sound.SimpleInputDevice.getLine(SimpleInputDevice.java:142)
at javax.sound.sampled.AudioSystem.getSourceDataLine(AudioSystem.java:633)
at pt.mobbit.sensorsloader.actuators.multiaudio.AudioPlayer$AudioPlayerThread.run(AudioPlayer.java:135)
at java.lang.Thread.run(Thread.java:613)

However, if I change the Output Device in OS X's System Preferences to be the USB Sound Card, and then in my java application I choose the mixer [0], the sound comes out through the USB Sound card. This leads me to believe that the USB Sound Card can play the mp3 files in OS X as well, just not when it isn't the main output device.

Does anyone know how I can output sound to my usb sound card in OS X without it having to be the system's main output device?

Best Answer

Can you play back on the USB card with other sample rates/sample formats when it is not the default device?

My is that OS X does software mixing + sample rate + sample format conversion on the default sound card (otherwise multiple programs would not be able to simultaneously play sounds unless they all agreed on one single sample rate + sample format which would be very unpractical), but it does not do that for other sound cards. So OS X converts the output of your program to the one the USB sound card hardware needs when the USB sound card is selected as default, but there is no conversion when it is not the default, so you get an unsupported format error.

Look into the documentation of the card to see the sample rate and sample format it supports - most probably it will be like 48000 Hz, 16 bit signed little endian, and 2 or 6 or 8 channels.