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
3 // found in the LICENSE file.
5 #ifndef CHROME_BROWSER_SPEECH_TTS_CONTROLLER_H_
6 #define CHROME_BROWSER_SPEECH_TTS_CONTROLLER_H_
13 #include "base/memory/scoped_ptr.h"
14 #include "base/memory/singleton.h"
15 #include "base/memory/weak_ptr.h"
19 class TtsPlatformImpl
;
26 // Events sent back from the TTS engine indicating the progress.
33 TTS_EVENT_INTERRUPTED
,
46 // Returns true if this event type is one that indicates an utterance
47 // is finished and can be destroyed.
48 bool IsFinalTtsEventType(TtsEventType event_type
);
50 // The continuous parameters that apply to a given utterance.
51 struct UtteranceContinuousParameters
{
52 UtteranceContinuousParameters();
59 // Information about one voice.
67 std::string extension_id
;
68 std::set
<TtsEventType
> events
;
70 // If true, the synthesis engine is a remote network resource.
71 // It may be higher latency and may incur bandwidth costs.
74 // If true, this is implemented by this platform's subclass of
75 // TtsPlatformImpl. If false, this is implemented by an extension.
77 std::string native_voice_identifier
;
80 // Class that wants to receive events on utterances.
81 class UtteranceEventDelegate
{
83 virtual ~UtteranceEventDelegate() {}
84 virtual void OnTtsEvent(Utterance
* utterance
,
85 TtsEventType event_type
,
87 const std::string
& error_message
) = 0;
90 // Class that wants to be notified when the set of
91 // voices has changed.
92 class VoicesChangedDelegate
{
94 virtual ~VoicesChangedDelegate() {}
95 virtual void OnVoicesChanged() = 0;
98 // One speech utterance.
101 // Construct an utterance given a profile and a completion task to call
102 // when the utterance is done speaking. Before speaking this utterance,
103 // its other parameters like text, rate, pitch, etc. should all be set.
104 explicit Utterance(Profile
* profile
);
105 virtual ~Utterance();
107 // Sends an event to the delegate. If the event type is TTS_EVENT_END
108 // or TTS_EVENT_ERROR, deletes the utterance. If |char_index| is -1,
109 // uses the last good value.
110 void OnTtsEvent(TtsEventType event_type
,
112 const std::string
& error_message
);
114 // Finish an utterance without sending an event to the delegate.
117 // Getters and setters for the text to speak and other speech options.
118 void set_text(const std::string
& text
) { text_
= text
; }
119 const std::string
& text() const { return text_
; }
121 void set_options(const base::Value
* options
);
122 const base::Value
* options() const { return options_
.get(); }
124 void set_src_extension_id(const std::string
& src_extension_id
) {
125 src_extension_id_
= src_extension_id
;
127 const std::string
& src_extension_id() { return src_extension_id_
; }
129 void set_src_id(int src_id
) { src_id_
= src_id
; }
130 int src_id() { return src_id_
; }
132 void set_src_url(const GURL
& src_url
) { src_url_
= src_url
; }
133 const GURL
& src_url() { return src_url_
; }
135 void set_voice_name(const std::string
& voice_name
) {
136 voice_name_
= voice_name
;
138 const std::string
& voice_name() const { return voice_name_
; }
140 void set_lang(const std::string
& lang
) {
143 const std::string
& lang() const { return lang_
; }
145 void set_gender(TtsGenderType gender
) {
148 TtsGenderType
gender() const { return gender_
; }
150 void set_continuous_parameters(const UtteranceContinuousParameters
& params
) {
151 continuous_parameters_
= params
;
153 const UtteranceContinuousParameters
& continuous_parameters() {
154 return continuous_parameters_
;
157 void set_can_enqueue(bool can_enqueue
) { can_enqueue_
= can_enqueue
; }
158 bool can_enqueue() const { return can_enqueue_
; }
160 void set_required_event_types(const std::set
<TtsEventType
>& types
) {
161 required_event_types_
= types
;
163 const std::set
<TtsEventType
>& required_event_types() const {
164 return required_event_types_
;
167 void set_desired_event_types(const std::set
<TtsEventType
>& types
) {
168 desired_event_types_
= types
;
170 const std::set
<TtsEventType
>& desired_event_types() const {
171 return desired_event_types_
;
174 const std::string
& extension_id() const { return extension_id_
; }
175 void set_extension_id(const std::string
& extension_id
) {
176 extension_id_
= extension_id
;
179 UtteranceEventDelegate
* event_delegate() const {
180 return event_delegate_
.get();
182 void set_event_delegate(
183 base::WeakPtr
<UtteranceEventDelegate
> event_delegate
) {
184 event_delegate_
= event_delegate
;
187 // Getters and setters for internal state.
188 Profile
* profile() const { return profile_
; }
189 int id() const { return id_
; }
190 bool finished() const { return finished_
; }
193 void set_finished_for_testing(bool finished
) { finished_
= finished
; }
196 // The profile that initiated this utterance.
199 // The extension ID of the extension providing TTS for this utterance, or
200 // empty if native TTS is being used.
201 std::string extension_id_
;
203 // The unique ID of this utterance, used to associate callback functions
207 // The id of the next utterance, so we can associate requests with
209 static int next_utterance_id_
;
211 // The text to speak.
214 // The full options arg passed to tts.speak, which may include fields
215 // other than the ones we explicitly parse, below.
216 scoped_ptr
<base::Value
> options_
;
218 // The extension ID of the extension that called speak() and should
220 std::string src_extension_id_
;
222 // The source extension's ID of this utterance, so that it can associate
223 // events with the appropriate callback.
226 // The URL of the page where the source extension called speak.
229 // The delegate to be called when an utterance event is fired.
230 base::WeakPtr
<UtteranceEventDelegate
> event_delegate_
;
232 // The parsed options.
233 std::string voice_name_
;
235 TtsGenderType gender_
;
236 UtteranceContinuousParameters continuous_parameters_
;
238 std::set
<TtsEventType
> required_event_types_
;
239 std::set
<TtsEventType
> desired_event_types_
;
241 // The index of the current char being spoken.
244 // True if this utterance received an event indicating it's done.
248 // Singleton class that manages text-to-speech for the TTS and TTS engine
249 // extension APIs, maintaining a queue of pending utterances and keeping
250 // track of all state.
251 class TtsController
{
253 // Get the single instance of this class.
254 static TtsController
* GetInstance();
256 // Returns true if we're currently speaking an utterance.
259 // Speak the given utterance. If the utterance's can_enqueue flag is true
260 // and another utterance is in progress, adds it to the end of the queue.
261 // Otherwise, interrupts any current utterance and speaks this one
263 void SpeakOrEnqueue(Utterance
* utterance
);
265 // Stop all utterances and flush the queue. Implies leaving pause mode
269 // Pause the speech queue. Some engines may support pausing in the middle
276 // Handle events received from the speech engine. Events are forwarded to
277 // the callback function, and in addition, completion and error events
278 // trigger finishing the current utterance and starting the next one, if
280 void OnTtsEvent(int utterance_id
,
281 TtsEventType event_type
,
283 const std::string
& error_message
);
285 // Return a list of all available voices, including the native voice,
286 // if supported, and all voices registered by extensions.
287 void GetVoices(Profile
* profile
, std::vector
<VoiceData
>* out_voices
);
289 // Called by the extension system or platform implementation when the
290 // list of voices may have changed and should be re-queried.
291 void VoicesChanged();
293 // Add a delegate that wants to be notified when the set of voices changes.
294 void AddVoicesChangedDelegate(VoicesChangedDelegate
* delegate
);
296 // Remove delegate that wants to be notified when the set of voices changes.
297 void RemoveVoicesChangedDelegate(VoicesChangedDelegate
* delegate
);
300 void SetPlatformImpl(TtsPlatformImpl
* platform_impl
);
305 virtual ~TtsController();
308 // Get the platform TTS implementation (or injected mock).
309 TtsPlatformImpl
* GetPlatformImpl();
311 // Start speaking the given utterance. Will either take ownership of
312 // |utterance| or delete it if there's an error. Returns true on success.
313 void SpeakNow(Utterance
* utterance
);
315 // Clear the utterance queue. If send_events is true, will send
316 // TTS_EVENT_CANCELLED events on each one.
317 void ClearUtteranceQueue(bool send_events
);
319 // Finalize and delete the current utterance.
320 void FinishCurrentUtterance();
322 // Start speaking the next utterance in the queue.
323 void SpeakNextUtterance();
325 // Given an utterance and a vector of voices, return the
326 // index of the voice that best matches the utterance.
327 int GetMatchingVoice(const Utterance
* utterance
,
328 std::vector
<VoiceData
>& voices
);
330 friend struct DefaultSingletonTraits
<TtsController
>;
332 // The current utterance being spoken.
333 Utterance
* current_utterance_
;
335 // Whether the queue is paused or not.
338 // A queue of utterances to speak after the current one finishes.
339 std::queue
<Utterance
*> utterance_queue_
;
341 // A set of delegates that want to be notified when the voices change.
342 std::set
<VoicesChangedDelegate
*> voices_changed_delegates_
;
344 // A pointer to the platform implementation of text-to-speech, for
345 // dependency injection.
346 TtsPlatformImpl
* platform_impl_
;
348 DISALLOW_COPY_AND_ASSIGN(TtsController
);
351 #endif // CHROME_BROWSER_SPEECH_TTS_CONTROLLER_H_