Monday, March 17, 2003

I read an article on Slashdot about a contest which would have programmers write an interesting program printing the lyrics to "99 bottles of beer on the wall". This inspired me to play with the Java Speech API. I wrote a quick program to recite the lyrics. It's really nothing more than a glorified Hello World program, but it let me play with the Speech API:

public class BottlesOfBeer {
public static void main(String[] args) {
Voice voice = null;
Class voiceClass;
try {
voiceClass = Class.forName("");
voice = (Voice) voiceClass.newInstance();
} catch (ClassNotFoundException e) {
} catch (InstantiationException e) {
} catch (IllegalAccessException e) {
if (voice instanceof MbrolaVoice) {
try {
(new MbrolaVoiceValidator((MbrolaVoice) voice)).validate();
} catch (ValidationException ve) {
throw new IllegalStateException("Problem starting MBROLA voice");
voice.setLexicon(new CMULexicon());
voice.setAudioPlayer(new JavaClipAudioPlayer());
for (int i = 99; i > 0; i--) {
String verse = i + getBottle(i) + " of beer on the wall. " +
i + getBottle(i) + " of beer. " +
"Take one down. Pass it arround. " +
(i - 1) + getBottle(i - 1) + "of beer on the wall.";
voice.speak("Thank you everyone. You've been a great audience. " +
"I'll be here all week!");
public static String getBottle(int numberOfBottles) {
return numberOfBottles != 1 ? " bottles" : " bottle";

For those of you who don't know, the Java Speech API doesn't have a reference implementation. It only defines the api. I used an open source implementation called FreeTTS.

By sharing this code, I've disqualified my self from the contest, but since the code is judged on compactness, obfuscation and originality, I don't think I had much of a shot anyway.

By the way... when did obfuscation become a cherished trait in a programmer?



Post a Comment

Subscribe to Post Comments [Atom]

<< Home