upstream version 1.2.2
[debian/freetts] / demo / freetts / HelloWorld / FreeTTSHelloWorld.java
1 /**
2  * Copyright 2003 Sun Microsystems, Inc.
3  * 
4  * See the file "license.terms" for information on usage and
5  * redistribution of this file, and for a DISCLAIMER OF ALL 
6  * WARRANTIES.
7  */
8 import com.sun.speech.freetts.Voice;
9 import com.sun.speech.freetts.VoiceManager;
10 import com.sun.speech.freetts.audio.JavaClipAudioPlayer;
11
12 /**
13  * Simple program to demonstrate the use of the FreeTTS speech
14  * synthesizer.  This simple program shows how to use FreeTTS
15  * without requiring the Java Speech API (JSAPI).
16  */
17 public class FreeTTSHelloWorld {
18
19     /**
20      * Example of how to list all the known voices.
21      */
22     public static void listAllVoices() {
23         System.out.println();
24         System.out.println("All voices available:");        
25         VoiceManager voiceManager = VoiceManager.getInstance();
26         Voice[] voices = voiceManager.getVoices();
27         for (int i = 0; i < voices.length; i++) {
28             System.out.println("    " + voices[i].getName()
29                                + " (" + voices[i].getDomain() + " domain)");
30         }
31     }
32
33     public static void main(String[] args) {
34
35         listAllVoices();
36         
37         String voiceName = (args.length > 0)
38             ? args[0]
39             : "kevin16";
40         
41         System.out.println();
42         System.out.println("Using voice: " + voiceName);
43         
44         /* The VoiceManager manages all the voices for FreeTTS.
45          */
46         VoiceManager voiceManager = VoiceManager.getInstance();
47         Voice helloVoice = voiceManager.getVoice(voiceName);
48
49         if (helloVoice == null) {
50             System.err.println(
51                 "Cannot find a voice named "
52                 + voiceName + ".  Please specify a different voice.");
53             System.exit(1);
54         }
55         
56         /* Allocates the resources for the voice.
57          */
58         helloVoice.allocate();
59         
60         /* Synthesize speech.
61          */
62         helloVoice.speak("Thank you for giving me a voice. "
63                          + "I'm so glad to say hello to this world.");
64
65         /* Clean up and leave.
66          */
67         helloVoice.deallocate();
68         System.exit(0);
69     }
70 }