, 3 min read
Java Sound under Linux
I’m having fun with Java Sound under Linux, though my troubles are probably not Linux specific. My setup is that I have a sound card plus a USB microphone. Both are correctly configured and I have the two devices appear as /dev/dsp and /dev/dsp1. Using kamix, I initially made sure that the alsa mixer was configured properly so that you can only record on /dev/dsp1 and only ouput on /dev/dsp. However, I recently changed it so that /dev/dsp can also serve as a recording device (the gain is non-zero). Tools like rawrec work just fine, though, in order to record with the default alsa recording tool, I need to specify my recording device:
/usr/bin/arecord -t wav test.wav -D hw:1 -f S16_LE
Tools like mhwaveedit also work fine, as long as I specify a quantization of 16 bits and hw:1 as the device. Don’t ask me what hw stands for! I just know that hw:1 points to the second audio device whereas hw:0 probably points to the first one. These people have done a bit too much C programming: please count starting at 1 people! Not everyone knows that the first device ought to be labelled 0.
Anyhow. As it turns out, recent versions of Java (Java 1.4 and up) have decent support for audio builtin. If you want it to work with applets, you need to create a file called .java.policy in your user directory. The following content is probably highly insecure and dangerous, but it will get you going:
grant{
permission java.io.FilePermission "<
permission javax.sound.sampled.AudioPermission "record";
permission javax.sound.sampled.AudioPermission "play";
permission java.util.PropertyPermission "*", "read";
};
Notice that after you change this file, you should restart your browser to make absolutely sure the changes are registered!
If you are using Firefox, you can type “about:plugins” in the URL bar to see which Java your machine uses. You can also check the file “~/.mozilla/firefox/pluginreg.dat”. To test your setup, here’s a nice applet:
Or, if you are more ambitious, you can try the webhuddle applet though it is more complex to get it started.
Unfortunately, I can’t record using these applets after fiddling a bit, tt tells me “Line matching interface TargetDataLine supporting format …, not supported” (great English!).
I still wanted to make sure I could, indeed, record under Java and that the problem was indeed in the applets! For such purposes, you can download some cool software at the Java Sound FAQ. For example, with my current setup, I’m able to record audio using a Java command line tool:
java AudioRecorder -f S16_LE -c 1 -r 8000 -t wav -M "default [plughw:1,0]" file.wav
It took me some doing to figure out what my USB Microsoft was called (which is “default [plughw:1,0]”). Here’s what the tool told me my my devices were:
>java AudioRecorder -l
Available Mixers:
V8237 [plughw:0,0]
V8237 [plughw:0,1]
default [plughw:1,0]
Java Sound Audio Engine
Port V8237 [hw:0]
Port default [hw:1]
I knew V8237 refers to my builtin sound card so my USB microphone had to be called “default”. I’m hoping smart software would choose the device whose name start with “default” as the most sensible default…? But even if it selects some other input device, shouldn’t it at least record noise?