A while back, Bryan posted a program that renders a spoken version of the classic "99 bottles of beer on the wall" using FreeTTS 1.1. Well, Bryan was using lower level (non-JSAPI) FreeTTS API and things have changed in this API a bit with the release of FreeTTS 1.2, so I thought I'd update the source so it will work with 1.2. This little code snippet, when coupled with FreeTTS will indeed sing the whole song, without ever getting tired.

import com.sun.speech.freetts.Voice;
import com.sun.speech.freetts.VoiceManager;

public class BottlesOfBeer  {

    public static void main(String[] args) {

        VoiceManager voiceManager = VoiceManager.getInstance();
        Voice voice = voiceManager.getVoice("kevin16");

        if (voice == null) {
            System.err.println( "Cannot find kevin16");
            System.exit(1);
        }

        voice.allocate();
        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 around, " +
                    (i - 1) + getBottle(i - 1) + "of beer on the wall.";
            voice.speak(verse);
        }
        voice.speak("Thank you everyone.  You've been a great audience.  " +
                "I'll be here all week!");

        /* Clean up and leave.
         */
        voice.deallocate();
        System.exit(0);
    }

    public static String getBottle(int numberOfBottles) {
        return numberOfBottles != 1 ? " bottles" : " bottle";
    }
}

I figured with all those kids heading out to camp this summer with their laptops, they can just fire this up on those bus rides instead of having to sing it themselves.

Oh, and be sure to check out www.99-bottles-of-beer.net for 621 variations of this program in just about every programming language ever written. A most valuable resource.

Comments:

thx for the demo code. quite clean. i'll u/d myjxta2 in kind. couldn't be simplier.

Posted by gonzo on July 25, 2004 at 11:02 PM EDT #

[Trackback] just upgraded to freetts 1.2b2 in order to incorporate the ultra-lean and cool code example as posted by plamere . couldn't be easier and i was able to remove earlier implementation kruft in the process. less is more. i like that! I'll spin out a ne...

Posted by GonzoMoFo on July 26, 2004 at 04:30 AM EDT #

mmmm ....beer

Posted by Foo on July 29, 2004 at 07:48 PM EDT #

a test comment

Posted by fum on July 29, 2004 at 08:28 PM EDT #

Should not your deallocate be in a finally block? I realize this is just a simple demo, but for the newbie that walks by; it wouldn't hurt to remind him/her of such things.

Posted by Zorkerman on September 18, 2004 at 10:32 AM EDT #

Post a Comment:
Comments are closed for this entry.

This blog copyright 2010 by plamere