altos: Add delays to bt startup sequence
[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 com.sun.speech.freetts.audio.JavaClipAudioPlayer;
23 import java.util.concurrent.LinkedBlockingQueue;
24
25 public class AltosVoice implements Runnable {
26         VoiceManager                    voice_manager;
27         Voice                           voice;
28         LinkedBlockingQueue<String>     phrases;
29         Thread                          thread;
30         boolean                         busy;
31
32         final static String voice_name = "kevin16";
33
34         public void run() {
35                 try {
36                         for (;;) {
37                                 String s = phrases.take();
38                                 voice.speak(s);
39                                 synchronized(this) {
40                                         if (phrases.isEmpty()) {
41                                                 busy = false;
42                                                 notifyAll();
43                                         }
44                                 }
45                         }
46                 } catch (InterruptedException e) {
47                 }
48         }
49
50         public synchronized void drain() throws InterruptedException {
51                 while (busy)
52                         wait();
53         }
54
55         public void speak_always(String s) {
56                 try {
57                         if (voice != null) {
58                                 synchronized(this) {
59                                         busy = true;
60                                         phrases.put(s);
61                                 }
62                         }
63                 } catch (InterruptedException e) {
64                 }
65         }
66
67         public void speak(String s) {
68                 if (AltosPreferences.voice())
69                         speak_always(s);
70         }
71
72         public void speak(String format, Object... parameters) {
73                 speak(String.format(format, parameters));
74         }
75
76         public AltosVoice () {
77                 busy = false;
78                 voice_manager = VoiceManager.getInstance();
79                 voice = voice_manager.getVoice(voice_name);
80                 if (voice != null) {
81                         voice.allocate();
82                         phrases = new LinkedBlockingQueue<String> ();
83                         thread = new Thread(this);
84                         thread.start();
85                 } else {
86                         System.out.printf("Voice manager failed to open %s\n", voice_name);
87                         Voice[] voices = voice_manager.getVoices();
88                         System.out.printf("Available voices:\n");
89                         for (int i = 0; i < voices.length; i++) {
90                                 System.out.println("    " + voices[i].getName()
91                                                    + " (" + voices[i].getDomain() + " domain)");
92                         }
93                 }
94         }
95 }