| 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> |
| (...skipping 256 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 267 | 267 |
| 268 // Singleton class that manages text-to-speech for the TTS and TTS engine | 268 // Singleton class that manages text-to-speech for the TTS and TTS engine |
| 269 // extension APIs, maintaining a queue of pending utterances and keeping | 269 // extension APIs, maintaining a queue of pending utterances and keeping |
| 270 // track of all state. | 270 // track of all state. |
| 271 class TtsController { | 271 class TtsController { |
| 272 public: | 272 public: |
| 273 // Get the single instance of this class. | 273 // Get the single instance of this class. |
| 274 static TtsController* GetInstance(); | 274 static TtsController* GetInstance(); |
| 275 | 275 |
| 276 // Returns true if we're currently speaking an utterance. | 276 // Returns true if we're currently speaking an utterance. |
| 277 bool IsSpeaking(); | 277 virtual bool IsSpeaking() = 0; |
| 278 | 278 |
| 279 // Speak the given utterance. If the utterance's can_enqueue flag is true | 279 // Speak the given utterance. If the utterance's can_enqueue flag is true |
| 280 // and another utterance is in progress, adds it to the end of the queue. | 280 // and another utterance is in progress, adds it to the end of the queue. |
| 281 // Otherwise, interrupts any current utterance and speaks this one | 281 // Otherwise, interrupts any current utterance and speaks this one |
| 282 // immediately. | 282 // immediately. |
| 283 void SpeakOrEnqueue(Utterance* utterance); | 283 virtual void SpeakOrEnqueue(Utterance* utterance) = 0; |
| 284 | 284 |
| 285 // Stop all utterances and flush the queue. Implies leaving pause mode | 285 // Stop all utterances and flush the queue. Implies leaving pause mode |
| 286 // as well. | 286 // as well. |
| 287 void Stop(); | 287 virtual void Stop() = 0; |
| 288 | 288 |
| 289 // Pause the speech queue. Some engines may support pausing in the middle | 289 // Pause the speech queue. Some engines may support pausing in the middle |
| 290 // of an utterance. | 290 // of an utterance. |
| 291 void Pause(); | 291 virtual void Pause() = 0; |
| 292 | 292 |
| 293 // Resume speaking. | 293 // Resume speaking. |
| 294 void Resume(); | 294 virtual void Resume() = 0; |
| 295 | 295 |
| 296 // Handle events received from the speech engine. Events are forwarded to | 296 // Handle events received from the speech engine. Events are forwarded to |
| 297 // the callback function, and in addition, completion and error events | 297 // the callback function, and in addition, completion and error events |
| 298 // trigger finishing the current utterance and starting the next one, if | 298 // trigger finishing the current utterance and starting the next one, if |
| 299 // any. | 299 // any. |
| 300 void OnTtsEvent(int utterance_id, | 300 virtual void OnTtsEvent(int utterance_id, |
| 301 TtsEventType event_type, | 301 TtsEventType event_type, |
| 302 int char_index, | 302 int char_index, |
| 303 const std::string& error_message); | 303 const std::string& error_message) = 0; |
| 304 | 304 |
| 305 // Return a list of all available voices, including the native voice, | 305 // Return a list of all available voices, including the native voice, |
| 306 // if supported, and all voices registered by extensions. | 306 // if supported, and all voices registered by extensions. |
| 307 void GetVoices(Profile* profile, std::vector<VoiceData>* out_voices); | 307 virtual void GetVoices(Profile* profile, |
| 308 std::vector<VoiceData>* out_voices) = 0; |
| 308 | 309 |
| 309 // Called by TtsExtensionLoaderChromeOs::LoadTtsExtension when it | 310 // Called by TtsExtensionLoaderChromeOs::LoadTtsExtension when it |
| 310 // finishes loading the built-in TTS component extension. | 311 // finishes loading the built-in TTS component extension. |
| 311 void RetrySpeakingQueuedUtterances(); | 312 virtual void RetrySpeakingQueuedUtterances() = 0; |
| 312 | 313 |
| 313 // Called by the extension system or platform implementation when the | 314 // Called by the extension system or platform implementation when the |
| 314 // list of voices may have changed and should be re-queried. | 315 // list of voices may have changed and should be re-queried. |
| 315 void VoicesChanged(); | 316 virtual void VoicesChanged() = 0; |
| 316 | 317 |
| 317 // Add a delegate that wants to be notified when the set of voices changes. | 318 // Add a delegate that wants to be notified when the set of voices changes. |
| 318 void AddVoicesChangedDelegate(VoicesChangedDelegate* delegate); | 319 virtual void AddVoicesChangedDelegate(VoicesChangedDelegate* delegate) = 0; |
| 319 | 320 |
| 320 // Remove delegate that wants to be notified when the set of voices changes. | 321 // Remove delegate that wants to be notified when the set of voices changes. |
| 321 void RemoveVoicesChangedDelegate(VoicesChangedDelegate* delegate); | 322 virtual void RemoveVoicesChangedDelegate(VoicesChangedDelegate* delegate) = 0; |
| 322 | 323 |
| 323 // Set the delegate that processes TTS requests with user-installed | 324 // Set the delegate that processes TTS requests with user-installed |
| 324 // extensions. | 325 // extensions. |
| 325 void SetTtsEngineDelegate(TtsEngineDelegate* delegate); | 326 virtual void SetTtsEngineDelegate(TtsEngineDelegate* delegate) = 0; |
| 326 | 327 |
| 327 // For unit testing. | 328 // For unit testing. |
| 328 void SetPlatformImpl(TtsPlatformImpl* platform_impl); | 329 virtual void SetPlatformImpl(TtsPlatformImpl* platform_impl) = 0; |
| 329 int QueueSize(); | 330 virtual int QueueSize() = 0; |
| 330 | 331 |
| 331 protected: | 332 protected: |
| 332 TtsController(); | 333 virtual ~TtsController() {} |
| 333 virtual ~TtsController(); | |
| 334 | |
| 335 private: | |
| 336 // Get the platform TTS implementation (or injected mock). | |
| 337 TtsPlatformImpl* GetPlatformImpl(); | |
| 338 | |
| 339 // Start speaking the given utterance. Will either take ownership of | |
| 340 // |utterance| or delete it if there's an error. Returns true on success. | |
| 341 void SpeakNow(Utterance* utterance); | |
| 342 | |
| 343 // Clear the utterance queue. If send_events is true, will send | |
| 344 // TTS_EVENT_CANCELLED events on each one. | |
| 345 void ClearUtteranceQueue(bool send_events); | |
| 346 | |
| 347 // Finalize and delete the current utterance. | |
| 348 void FinishCurrentUtterance(); | |
| 349 | |
| 350 // Start speaking the next utterance in the queue. | |
| 351 void SpeakNextUtterance(); | |
| 352 | |
| 353 // Given an utterance and a vector of voices, return the | |
| 354 // index of the voice that best matches the utterance. | |
| 355 int GetMatchingVoice(const Utterance* utterance, | |
| 356 std::vector<VoiceData>& voices); | |
| 357 | |
| 358 friend struct DefaultSingletonTraits<TtsController>; | |
| 359 | |
| 360 // The current utterance being spoken. | |
| 361 Utterance* current_utterance_; | |
| 362 | |
| 363 // Whether the queue is paused or not. | |
| 364 bool paused_; | |
| 365 | |
| 366 // A queue of utterances to speak after the current one finishes. | |
| 367 std::queue<Utterance*> utterance_queue_; | |
| 368 | |
| 369 // A set of delegates that want to be notified when the voices change. | |
| 370 std::set<VoicesChangedDelegate*> voices_changed_delegates_; | |
| 371 | |
| 372 // A pointer to the platform implementation of text-to-speech, for | |
| 373 // dependency injection. | |
| 374 TtsPlatformImpl* platform_impl_; | |
| 375 | |
| 376 // The delegate that processes TTS requests with user-installed extensions. | |
| 377 TtsEngineDelegate* tts_engine_delegate_; | |
| 378 | |
| 379 DISALLOW_COPY_AND_ASSIGN(TtsController); | |
| 380 }; | 334 }; |
| 381 | 335 |
| 382 #endif // CHROME_BROWSER_SPEECH_TTS_CONTROLLER_H_ | 336 #endif // CHROME_BROWSER_SPEECH_TTS_CONTROLLER_H_ |
| OLD | NEW |