altosui: remove un-used import
[fw/altos] / altosui / AltosVoice.java
1 /*
2  * Copyright © 2010 Keith Packard <keithp@keithp.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; version 2 of the License.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
16  */
17
18 package altosui;
19
20 import com.sun.speech.freetts.Voice;
21 import com.sun.speech.freetts.VoiceManager;
22 import java.util.concurrent.LinkedBlockingQueue;
23
24 public class AltosVoice implements Runnable {
25         VoiceManager                    voice_manager;
26         Voice                           voice;
27         LinkedBlockingQueue<String>     phrases;
28         Thread                          thread;
29         boolean                         busy;
30
31         final static String voice_name = "kevin16";
32
33         public void run() {
34                 try {
35                         for (;;) {
36                                 String s = phrases.take();
37                                 voice.speak(s);
38                                 synchronized(this) {
39                                         if (phrases.isEmpty()) {
40                                                 busy = false;
41                                                 notifyAll();
42                                         }
43                                 }
44                         }
45                 } catch (InterruptedException e) {
46                 }
47         }
48
49         public synchronized void drain() throws InterruptedException {
50                 while (busy)
51                         wait();
52         }
53
54         public void speak_always(String s) {
55                 try {
56                         if (voice != null) {
57                                 synchronized(this) {
58                                         busy = true;
59                                         phrases.put(s);
60                                 }
61                         }
62                 } catch (InterruptedException e) {
63                 }
64         }
65
66         public void speak(String s) {
67                 if (AltosUIPreferences.voice())
68                         speak_always(s);
69         }
70
71         public void speak(String format, Object... parameters) {
72                 speak(String.format(format, parameters));
73         }
74
75         public AltosVoice () {
76                 busy = false;
77                 voice_manager = VoiceManager.getInstance();
78                 voice = voice_manager.getVoice(voice_name);
79                 if (voice != null) {
80                         voice.allocate();
81                         phrases = new LinkedBlockingQueue<String> ();
82                         thread = new Thread(this);
83                         thread.start();
84                 } else {
85                         System.out.printf("Voice manager failed to open %s\n", voice_name);
86                         Voice[] voices = voice_manager.getVoices();
87                         System.out.printf("Available voices:\n");
88                         for (int i = 0; i < voices.length; i++) {
89                                 System.out.println("    " + voices[i].getName()
90                                                    + " (" + voices[i].getDomain() + " domain)");
91                         }
92                 }
93         }
94 }