Friday, February 11, 2011

Java Audio, FreeTTS "LINE UNAVAILABLE" Error

A program based on FreeTTS, the free text-to-speech engine for Java, was getting occasional errors
"LINE UNAVAILABLE: Format is ..."
The reason was later found in the KDE desktop: Its configuration panel has options to keep the audio device open for some time after playing beeps or other desktop sounds. The default seemed to be rather high at 60 seconds. But even after changing that to only 2 seconds, the FreeTTS library still needs to compete with some occasional other users of the audio devices.
Turns out there is no Java Exception or other mechanism to detect this error that occurs inside the FreeTTS library. All you get is the message on System.out, so there is no good way to react programatically.

Workaround: Configure the FreeTTS audio player to attempt accessing the audio device more than once until it succeeds. In this example, a short delay of 0.1 seconds is used to not miss an opportunity to grab the audio device; we keep trying for 30 seconds:
System.setProperty("com.sun.speech.freetts.audio.AudioPlayer.openFailDelayMs", "100");
System.setProperty("com.sun.speech.freetts.audio.AudioPlayer.totalOpenFailDelayMs", "30000");
If the audio device is permanently used by another program, there is of course no way to get access. Under Linux, this command will display the ID of the process that is currently holding the audio device, so you can then try to get rid of the offending program:
  /sbin/fuser /dev/dsp