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 #include "chrome/browser/speech/tts_controller.h"
10 #include "base/float_util.h"
11 #include "base/json/json_writer.h"
12 #include "base/values.h"
13 #include "chrome/browser/extensions/event_router.h"
14 #include "chrome/browser/extensions/extension_system.h"
15 #include "chrome/browser/profiles/profile.h"
16 #include "chrome/browser/speech/extension_api/tts_engine_extension_api.h"
17 #include "chrome/browser/speech/extension_api/tts_extension_api.h"
18 #include "chrome/browser/speech/extension_api/tts_extension_api_constants.h"
19 #include "chrome/browser/speech/tts_platform.h"
20 #include "chrome/common/extensions/api/speech/tts_engine_manifest_handler.h"
21 #include "chrome/common/extensions/extension.h"
23 namespace constants
= tts_extension_api_constants
;
27 // A value to be used to indicate that there is no char index available.
28 const int kInvalidCharIndex
= -1;
31 const char kOnEvent
[] = "tts.onEvent";
32 }; // namespace events
34 std::string
TtsEventTypeToString(TtsEventType event_type
) {
37 return constants::kEventTypeStart
;
39 return constants::kEventTypeEnd
;
41 return constants::kEventTypeWord
;
42 case TTS_EVENT_SENTENCE
:
43 return constants::kEventTypeSentence
;
44 case TTS_EVENT_MARKER
:
45 return constants::kEventTypeMarker
;
46 case TTS_EVENT_INTERRUPTED
:
47 return constants::kEventTypeInterrupted
;
48 case TTS_EVENT_CANCELLED
:
49 return constants::kEventTypeCancelled
;
51 return constants::kEventTypeError
;
61 // UtteranceContinuousParameters
65 UtteranceContinuousParameters::UtteranceContinuousParameters()
76 int Utterance::next_utterance_id_
= 0;
78 Utterance::Utterance(Profile
* profile
)
80 id_(next_utterance_id_
++),
85 options_
.reset(new DictionaryValue());
88 Utterance::~Utterance() {
92 void Utterance::OnTtsEvent(TtsEventType event_type
,
94 const std::string
& error_message
) {
95 std::string event_type_string
= TtsEventTypeToString(event_type
);
97 char_index_
= char_index
;
98 if (event_type
== TTS_EVENT_END
||
99 event_type
== TTS_EVENT_INTERRUPTED
||
100 event_type
== TTS_EVENT_CANCELLED
||
101 event_type
== TTS_EVENT_ERROR
) {
104 if (desired_event_types_
.size() > 0 &&
105 desired_event_types_
.find(event_type_string
) ==
106 desired_event_types_
.end()) {
113 DictionaryValue
* details
= new DictionaryValue();
114 if (char_index
!= kInvalidCharIndex
)
115 details
->SetInteger(constants::kCharIndexKey
, char_index
);
116 details
->SetString(constants::kEventTypeKey
, event_type_string
);
117 if (event_type
== TTS_EVENT_ERROR
) {
118 details
->SetString(constants::kErrorMessageKey
, error_message
);
120 details
->SetInteger(constants::kSrcIdKey
, src_id_
);
121 details
->SetBoolean(constants::kIsFinalEventKey
, finished_
);
123 scoped_ptr
<ListValue
> arguments(new ListValue());
124 arguments
->Set(0, details
);
126 scoped_ptr
<extensions::Event
> event(new extensions::Event(
127 events::kOnEvent
, arguments
.Pass()));
128 event
->restrict_to_profile
= profile_
;
129 event
->event_url
= src_url_
;
130 extensions::ExtensionSystem::Get(profile_
)->event_router()->
131 DispatchEventToExtension(src_extension_id_
, event
.Pass());
134 void Utterance::Finish() {
138 void Utterance::set_options(const Value
* options
) {
139 options_
.reset(options
->DeepCopy());
147 TtsController
* TtsController::GetInstance() {
148 return Singleton
<TtsController
>::get();
151 TtsController::TtsController()
152 : current_utterance_(NULL
),
153 platform_impl_(NULL
) {
156 TtsController::~TtsController() {
157 if (current_utterance_
) {
158 current_utterance_
->Finish();
159 delete current_utterance_
;
162 // Clear any queued utterances too.
163 ClearUtteranceQueue(false); // Don't sent events.
166 void TtsController::SpeakOrEnqueue(Utterance
* utterance
) {
167 if (IsSpeaking() && utterance
->can_enqueue()) {
168 utterance_queue_
.push(utterance
);
175 void TtsController::SpeakNow(Utterance
* utterance
) {
176 const extensions::Extension
* extension
;
178 if (GetMatchingExtensionVoice(utterance
, &extension
, &voice_index
)) {
179 current_utterance_
= utterance
;
180 utterance
->set_extension_id(extension
->id());
182 ExtensionTtsEngineSpeak(utterance
, extension
, voice_index
);
184 const std::vector
<extensions::TtsVoice
>* tts_voices
=
185 extensions::TtsVoice::GetTtsVoices(extension
);
186 std::set
<std::string
> event_types
;
188 event_types
= tts_voices
->at(voice_index
).event_types
;
189 bool sends_end_event
=
190 (event_types
.find(constants::kEventTypeEnd
) != event_types
.end());
191 if (!sends_end_event
) {
194 current_utterance_
= NULL
;
195 SpeakNextUtterance();
200 GetPlatformImpl()->clear_error();
201 bool success
= GetPlatformImpl()->Speak(
205 utterance
->continuous_parameters());
208 GetPlatformImpl()->LoadBuiltInTtsExtension(utterance
->profile())) {
209 utterance_queue_
.push(utterance
);
214 utterance
->OnTtsEvent(TTS_EVENT_ERROR
, kInvalidCharIndex
,
215 GetPlatformImpl()->error());
219 current_utterance_
= utterance
;
222 void TtsController::Stop() {
223 if (current_utterance_
&& !current_utterance_
->extension_id().empty()) {
224 ExtensionTtsEngineStop(current_utterance_
);
226 GetPlatformImpl()->clear_error();
227 GetPlatformImpl()->StopSpeaking();
230 if (current_utterance_
)
231 current_utterance_
->OnTtsEvent(TTS_EVENT_INTERRUPTED
, kInvalidCharIndex
,
233 FinishCurrentUtterance();
234 ClearUtteranceQueue(true); // Send events.
237 void TtsController::OnTtsEvent(int utterance_id
,
238 TtsEventType event_type
,
240 const std::string
& error_message
) {
241 // We may sometimes receive completion callbacks "late", after we've
242 // already finished the utterance (for example because another utterance
243 // interrupted or we got a call to Stop). This is normal and we can
244 // safely just ignore these events.
245 if (!current_utterance_
|| utterance_id
!= current_utterance_
->id())
248 current_utterance_
->OnTtsEvent(event_type
, char_index
, error_message
);
249 if (current_utterance_
->finished()) {
250 FinishCurrentUtterance();
251 SpeakNextUtterance();
255 ListValue
* TtsController::GetVoices(Profile
* profile
) {
256 ListValue
* result_voices
= new ListValue();
257 TtsPlatformImpl
* platform_impl
= GetPlatformImpl();
258 if (platform_impl
&& platform_impl
->PlatformImplAvailable()) {
259 DictionaryValue
* result_voice
= new DictionaryValue();
260 result_voice
->SetString(
261 constants::kVoiceNameKey
, constants::kNativeVoiceName
);
262 if (!platform_impl
->gender().empty())
263 result_voice
->SetString(constants::kGenderKey
, platform_impl
->gender());
264 ListValue
* event_types
= new ListValue();
266 // All platforms must send end events, and cancelled and interrupted
267 // events are generated from the controller.
268 DCHECK(platform_impl
->SendsEvent(TTS_EVENT_END
));
269 event_types
->Append(Value::CreateStringValue(constants::kEventTypeEnd
));
270 event_types
->Append(Value::CreateStringValue(
271 constants::kEventTypeCancelled
));
272 event_types
->Append(Value::CreateStringValue(
273 constants::kEventTypeInterrupted
));
275 if (platform_impl
->SendsEvent(TTS_EVENT_START
))
276 event_types
->Append(Value::CreateStringValue(constants::kEventTypeStart
));
277 if (platform_impl
->SendsEvent(TTS_EVENT_WORD
))
278 event_types
->Append(Value::CreateStringValue(constants::kEventTypeWord
));
279 if (platform_impl
->SendsEvent(TTS_EVENT_SENTENCE
))
280 event_types
->Append(Value::CreateStringValue(
281 constants::kEventTypeSentence
));
282 if (platform_impl
->SendsEvent(TTS_EVENT_MARKER
))
283 event_types
->Append(Value::CreateStringValue(
284 constants::kEventTypeMarker
));
285 if (platform_impl
->SendsEvent(TTS_EVENT_ERROR
))
286 event_types
->Append(Value::CreateStringValue(
287 constants::kEventTypeError
));
288 result_voice
->Set(constants::kEventTypesKey
, event_types
);
289 result_voices
->Append(result_voice
);
292 GetExtensionVoices(profile
, result_voices
);
294 return result_voices
;
297 bool TtsController::IsSpeaking() {
298 return current_utterance_
!= NULL
|| GetPlatformImpl()->IsSpeaking();
301 void TtsController::FinishCurrentUtterance() {
302 if (current_utterance_
) {
303 if (!current_utterance_
->finished())
304 current_utterance_
->OnTtsEvent(TTS_EVENT_INTERRUPTED
, kInvalidCharIndex
,
306 delete current_utterance_
;
307 current_utterance_
= NULL
;
311 void TtsController::SpeakNextUtterance() {
312 // Start speaking the next utterance in the queue. Keep trying in case
313 // one fails but there are still more in the queue to try.
314 while (!utterance_queue_
.empty() && !current_utterance_
) {
315 Utterance
* utterance
= utterance_queue_
.front();
316 utterance_queue_
.pop();
321 void TtsController::RetrySpeakingQueuedUtterances() {
322 if (current_utterance_
== NULL
&& !utterance_queue_
.empty())
323 SpeakNextUtterance();
326 void TtsController::ClearUtteranceQueue(bool send_events
) {
327 while (!utterance_queue_
.empty()) {
328 Utterance
* utterance
= utterance_queue_
.front();
329 utterance_queue_
.pop();
331 utterance
->OnTtsEvent(TTS_EVENT_CANCELLED
, kInvalidCharIndex
,
339 void TtsController::SetPlatformImpl(
340 TtsPlatformImpl
* platform_impl
) {
341 platform_impl_
= platform_impl
;
344 int TtsController::QueueSize() {
345 return static_cast<int>(utterance_queue_
.size());
348 TtsPlatformImpl
* TtsController::GetPlatformImpl() {
350 platform_impl_
= TtsPlatformImpl::GetInstance();
351 return platform_impl_
;