OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_BROWSER_SPEECH_TTS_CONTROLLER_H_ | 5 #ifndef CHROME_BROWSER_SPEECH_TTS_CONTROLLER_H_ |
6 #define CHROME_BROWSER_SPEECH_TTS_CONTROLLER_H_ | 6 #define CHROME_BROWSER_SPEECH_TTS_CONTROLLER_H_ |
7 | 7 |
8 #include <queue> | 8 #include <queue> |
9 #include <set> | 9 #include <set> |
10 #include <string> | 10 #include <string> |
| 11 #include <vector> |
11 | 12 |
12 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
13 #include "base/memory/singleton.h" | 14 #include "base/memory/singleton.h" |
14 #include "googleurl/src/gurl.h" | 15 #include "googleurl/src/gurl.h" |
15 | 16 |
| 17 class Utterance; |
16 class TtsPlatformImpl; | 18 class TtsPlatformImpl; |
17 class Profile; | 19 class Profile; |
18 | 20 |
19 namespace base { | 21 namespace base { |
20 class ListValue; | |
21 class Value; | 22 class Value; |
22 } | 23 } |
23 | 24 |
24 // Events sent back from the TTS engine indicating the progress. | 25 // Events sent back from the TTS engine indicating the progress. |
25 enum TtsEventType { | 26 enum TtsEventType { |
26 TTS_EVENT_START, | 27 TTS_EVENT_START, |
27 TTS_EVENT_END, | 28 TTS_EVENT_END, |
28 TTS_EVENT_WORD, | 29 TTS_EVENT_WORD, |
29 TTS_EVENT_SENTENCE, | 30 TTS_EVENT_SENTENCE, |
30 TTS_EVENT_MARKER, | 31 TTS_EVENT_MARKER, |
31 TTS_EVENT_INTERRUPTED, | 32 TTS_EVENT_INTERRUPTED, |
32 TTS_EVENT_CANCELLED, | 33 TTS_EVENT_CANCELLED, |
33 TTS_EVENT_ERROR | 34 TTS_EVENT_ERROR |
34 }; | 35 }; |
35 | 36 |
| 37 // Returns true if this event type is one that indicates an utterance |
| 38 // is finished and can be destroyed. |
| 39 bool IsFinalTtsEventType(TtsEventType event_type); |
36 | 40 |
37 // The continuous parameters that apply to a given utterance. | 41 // The continuous parameters that apply to a given utterance. |
38 struct UtteranceContinuousParameters { | 42 struct UtteranceContinuousParameters { |
39 UtteranceContinuousParameters(); | 43 UtteranceContinuousParameters(); |
40 | 44 |
41 double rate; | 45 double rate; |
42 double pitch; | 46 double pitch; |
43 double volume; | 47 double volume; |
44 }; | 48 }; |
45 | 49 |
| 50 // Information about one voice. |
| 51 struct VoiceData { |
| 52 VoiceData(); |
| 53 ~VoiceData(); |
| 54 |
| 55 std::string name; |
| 56 std::string lang; |
| 57 std::string gender; |
| 58 std::string extension_id; |
| 59 std::vector<std::string> events; |
| 60 }; |
| 61 |
| 62 // Class that wants to receive events on utterances. |
| 63 class UtteranceEventDelegate { |
| 64 public: |
| 65 virtual ~UtteranceEventDelegate() {} |
| 66 virtual void OnTtsEvent(Utterance* utterance, |
| 67 TtsEventType event_type, |
| 68 int char_index, |
| 69 const std::string& error_message) = 0; |
| 70 }; |
46 | 71 |
47 // One speech utterance. | 72 // One speech utterance. |
48 class Utterance { | 73 class Utterance { |
49 public: | 74 public: |
50 // Construct an utterance given a profile and a completion task to call | 75 // Construct an utterance given a profile and a completion task to call |
51 // when the utterance is done speaking. Before speaking this utterance, | 76 // when the utterance is done speaking. Before speaking this utterance, |
52 // its other parameters like text, rate, pitch, etc. should all be set. | 77 // its other parameters like text, rate, pitch, etc. should all be set. |
53 explicit Utterance(Profile* profile); | 78 explicit Utterance(Profile* profile); |
54 ~Utterance(); | 79 ~Utterance(); |
55 | 80 |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
118 } | 143 } |
119 const std::set<std::string>& desired_event_types() const { | 144 const std::set<std::string>& desired_event_types() const { |
120 return desired_event_types_; | 145 return desired_event_types_; |
121 } | 146 } |
122 | 147 |
123 const std::string& extension_id() const { return extension_id_; } | 148 const std::string& extension_id() const { return extension_id_; } |
124 void set_extension_id(const std::string& extension_id) { | 149 void set_extension_id(const std::string& extension_id) { |
125 extension_id_ = extension_id; | 150 extension_id_ = extension_id; |
126 } | 151 } |
127 | 152 |
| 153 UtteranceEventDelegate* event_delegate() const { return event_delegate_; } |
| 154 void set_event_delegate(UtteranceEventDelegate* event_delegate) { |
| 155 event_delegate_ = event_delegate; |
| 156 } |
| 157 |
128 // Getters and setters for internal state. | 158 // Getters and setters for internal state. |
129 Profile* profile() const { return profile_; } | 159 Profile* profile() const { return profile_; } |
130 int id() const { return id_; } | 160 int id() const { return id_; } |
131 bool finished() const { return finished_; } | 161 bool finished() const { return finished_; } |
132 | 162 |
133 private: | 163 private: |
134 // The profile that initiated this utterance. | 164 // The profile that initiated this utterance. |
135 Profile* profile_; | 165 Profile* profile_; |
136 | 166 |
137 // The extension ID of the extension providing TTS for this utterance, or | 167 // The extension ID of the extension providing TTS for this utterance, or |
(...skipping 19 matching lines...) Expand all Loading... |
157 // receive events. | 187 // receive events. |
158 std::string src_extension_id_; | 188 std::string src_extension_id_; |
159 | 189 |
160 // The source extension's ID of this utterance, so that it can associate | 190 // The source extension's ID of this utterance, so that it can associate |
161 // events with the appropriate callback. | 191 // events with the appropriate callback. |
162 int src_id_; | 192 int src_id_; |
163 | 193 |
164 // The URL of the page where the source extension called speak. | 194 // The URL of the page where the source extension called speak. |
165 GURL src_url_; | 195 GURL src_url_; |
166 | 196 |
| 197 // The delegate to be called when an utterance event is fired. |
| 198 // Weak reference; it will be cleared after we fire a "final" event |
| 199 // (as determined by IsFinalTtsEventType). |
| 200 UtteranceEventDelegate* event_delegate_; |
| 201 |
167 // The parsed options. | 202 // The parsed options. |
168 std::string voice_name_; | 203 std::string voice_name_; |
169 std::string lang_; | 204 std::string lang_; |
170 std::string gender_; | 205 std::string gender_; |
171 UtteranceContinuousParameters continuous_parameters_; | 206 UtteranceContinuousParameters continuous_parameters_; |
172 bool can_enqueue_; | 207 bool can_enqueue_; |
173 std::set<std::string> required_event_types_; | 208 std::set<std::string> required_event_types_; |
174 std::set<std::string> desired_event_types_; | 209 std::set<std::string> desired_event_types_; |
175 | 210 |
176 // The index of the current char being spoken. | 211 // The index of the current char being spoken. |
177 int char_index_; | 212 int char_index_; |
178 | 213 |
179 // True if this utterance received an event indicating it's done. | 214 // True if this utterance received an event indicating it's done. |
180 bool finished_; | 215 bool finished_; |
181 }; | 216 }; |
182 | 217 |
183 | |
184 // Singleton class that manages text-to-speech for the TTS and TTS engine | 218 // Singleton class that manages text-to-speech for the TTS and TTS engine |
185 // extension APIs, maintaining a queue of pending utterances and keeping | 219 // extension APIs, maintaining a queue of pending utterances and keeping |
186 // track of all state. | 220 // track of all state. |
187 class TtsController { | 221 class TtsController { |
188 public: | 222 public: |
189 // Get the single instance of this class. | 223 // Get the single instance of this class. |
190 static TtsController* GetInstance(); | 224 static TtsController* GetInstance(); |
191 | 225 |
192 // Returns true if we're currently speaking an utterance. | 226 // Returns true if we're currently speaking an utterance. |
193 bool IsSpeaking(); | 227 bool IsSpeaking(); |
(...skipping 11 matching lines...) Expand all Loading... |
205 // the callback function, and in addition, completion and error events | 239 // the callback function, and in addition, completion and error events |
206 // trigger finishing the current utterance and starting the next one, if | 240 // trigger finishing the current utterance and starting the next one, if |
207 // any. | 241 // any. |
208 void OnTtsEvent(int utterance_id, | 242 void OnTtsEvent(int utterance_id, |
209 TtsEventType event_type, | 243 TtsEventType event_type, |
210 int char_index, | 244 int char_index, |
211 const std::string& error_message); | 245 const std::string& error_message); |
212 | 246 |
213 // Return a list of all available voices, including the native voice, | 247 // Return a list of all available voices, including the native voice, |
214 // if supported, and all voices registered by extensions. | 248 // if supported, and all voices registered by extensions. |
215 base::ListValue* GetVoices(Profile* profile); | 249 void GetVoices(Profile* profile, std::vector<VoiceData>* out_voices); |
216 | 250 |
217 // Called by TtsExtensionLoaderChromeOs::LoadTtsExtension when it | 251 // Called by TtsExtensionLoaderChromeOs::LoadTtsExtension when it |
218 // finishes loading the built-in TTS component extension. | 252 // finishes loading the built-in TTS component extension. |
219 void RetrySpeakingQueuedUtterances(); | 253 void RetrySpeakingQueuedUtterances(); |
220 | 254 |
221 // For unit testing. | 255 // For unit testing. |
222 void SetPlatformImpl(TtsPlatformImpl* platform_impl); | 256 void SetPlatformImpl(TtsPlatformImpl* platform_impl); |
223 int QueueSize(); | 257 int QueueSize(); |
224 | 258 |
225 protected: | 259 protected: |
(...skipping 27 matching lines...) Expand all Loading... |
253 std::queue<Utterance*> utterance_queue_; | 287 std::queue<Utterance*> utterance_queue_; |
254 | 288 |
255 // A pointer to the platform implementation of text-to-speech, for | 289 // A pointer to the platform implementation of text-to-speech, for |
256 // dependency injection. | 290 // dependency injection. |
257 TtsPlatformImpl* platform_impl_; | 291 TtsPlatformImpl* platform_impl_; |
258 | 292 |
259 DISALLOW_COPY_AND_ASSIGN(TtsController); | 293 DISALLOW_COPY_AND_ASSIGN(TtsController); |
260 }; | 294 }; |
261 | 295 |
262 #endif // CHROME_BROWSER_SPEECH_TTS_CONTROLLER_H_ | 296 #endif // CHROME_BROWSER_SPEECH_TTS_CONTROLLER_H_ |
OLD | NEW |