1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/speech/tts_controller.h"
#include <string>
#include <vector>
#include "base/float_util.h"
#include "base/values.h"
#include "chrome/browser/extensions/extension_system.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/speech/extension_api/tts_engine_extension_api.h"
#include "chrome/browser/speech/extension_api/tts_extension_api.h"
#include "chrome/browser/speech/extension_api/tts_extension_api_constants.h"
#include "chrome/browser/speech/tts_platform.h"
#include "chrome/common/extensions/api/speech/tts_engine_manifest_handler.h"
#include "chrome/common/extensions/extension.h"
namespace constants = tts_extension_api_constants;
namespace {
// A value to be used to indicate that there is no char index available.
const int kInvalidCharIndex = -1;
} // namespace
bool IsFinalTtsEventType(TtsEventType event_type) {
return (event_type == TTS_EVENT_END ||
event_type == TTS_EVENT_INTERRUPTED ||
event_type == TTS_EVENT_CANCELLED ||
event_type == TTS_EVENT_ERROR);
}
//
// UtteranceContinuousParameters
//
UtteranceContinuousParameters::UtteranceContinuousParameters()
: rate(-1),
pitch(-1),
volume(-1) {}
//
// VoiceData
//
VoiceData::VoiceData() {}
VoiceData::~VoiceData() {}
//
// Utterance
//
// static
int Utterance::next_utterance_id_ = 0;
Utterance::Utterance(Profile* profile)
: profile_(profile),
id_(next_utterance_id_++),
src_id_(-1),
event_delegate_(NULL),
can_enqueue_(false),
char_index_(0),
finished_(false) {
options_.reset(new DictionaryValue());
}
Utterance::~Utterance() {
DCHECK(finished_);
}
void Utterance::OnTtsEvent(TtsEventType event_type,
int char_index,
const std::string& error_message) {
if (char_index >= 0)
char_index_ = char_index;
if (IsFinalTtsEventType(event_type))
finished_ = true;
if (event_delegate_)
event_delegate_->OnTtsEvent(this, event_type, char_index, error_message);
if (finished_)
event_delegate_ = NULL;
}
void Utterance::Finish() {
finished_ = true;
}
void Utterance::set_options(const Value* options) {
options_.reset(options->DeepCopy());
}
//
// TtsController
//
// static
TtsController* TtsController::GetInstance() {
return Singleton<TtsController>::get();
}
TtsController::TtsController()
: current_utterance_(NULL),
platform_impl_(NULL) {
}
TtsController::~TtsController() {
if (current_utterance_) {
current_utterance_->Finish();
delete current_utterance_;
}
// Clear any queued utterances too.
ClearUtteranceQueue(false); // Don't sent events.
}
void TtsController::SpeakOrEnqueue(Utterance* utterance) {
if (IsSpeaking() && utterance->can_enqueue()) {
utterance_queue_.push(utterance);
} else {
Stop();
SpeakNow(utterance);
}
}
void TtsController::SpeakNow(Utterance* utterance) {
const extensions::Extension* extension;
size_t voice_index;
if (GetMatchingExtensionVoice(utterance, &extension, &voice_index)) {
current_utterance_ = utterance;
utterance->set_extension_id(extension->id());
ExtensionTtsEngineSpeak(utterance, extension, voice_index);
const std::vector<extensions::TtsVoice>* tts_voices =
extensions::TtsVoice::GetTtsVoices(extension);
std::set<std::string> event_types;
if (tts_voices)
event_types = tts_voices->at(voice_index).event_types;
bool sends_end_event =
(event_types.find(constants::kEventTypeEnd) != event_types.end());
if (!sends_end_event) {
utterance->Finish();
delete utterance;
current_utterance_ = NULL;
SpeakNextUtterance();
}
return;
}
GetPlatformImpl()->clear_error();
bool success = GetPlatformImpl()->Speak(
utterance->id(),
utterance->text(),
utterance->lang(),
utterance->continuous_parameters());
if (!success &&
GetPlatformImpl()->LoadBuiltInTtsExtension(utterance->profile())) {
utterance_queue_.push(utterance);
return;
}
if (!success) {
utterance->OnTtsEvent(TTS_EVENT_ERROR, kInvalidCharIndex,
GetPlatformImpl()->error());
delete utterance;
return;
}
current_utterance_ = utterance;
}
void TtsController::Stop() {
if (current_utterance_ && !current_utterance_->extension_id().empty()) {
ExtensionTtsEngineStop(current_utterance_);
} else {
GetPlatformImpl()->clear_error();
GetPlatformImpl()->StopSpeaking();
}
if (current_utterance_)
current_utterance_->OnTtsEvent(TTS_EVENT_INTERRUPTED, kInvalidCharIndex,
std::string());
FinishCurrentUtterance();
ClearUtteranceQueue(true); // Send events.
}
void TtsController::OnTtsEvent(int utterance_id,
TtsEventType event_type,
int char_index,
const std::string& error_message) {
// We may sometimes receive completion callbacks "late", after we've
// already finished the utterance (for example because another utterance
// interrupted or we got a call to Stop). This is normal and we can
// safely just ignore these events.
if (!current_utterance_ || utterance_id != current_utterance_->id())
return;
current_utterance_->OnTtsEvent(event_type, char_index, error_message);
if (current_utterance_->finished()) {
FinishCurrentUtterance();
SpeakNextUtterance();
}
}
void TtsController::GetVoices(Profile* profile,
std::vector<VoiceData>* out_voices) {
TtsPlatformImpl* platform_impl = GetPlatformImpl();
if (platform_impl && platform_impl->PlatformImplAvailable()) {
out_voices->push_back(VoiceData());
VoiceData& voice = out_voices->back();
voice.name = constants::kNativeVoiceName;
voice.gender = platform_impl->gender();
// All platforms must send end events, and cancelled and interrupted
// events are generated from the controller.
DCHECK(platform_impl->SendsEvent(TTS_EVENT_END));
voice.events.push_back(constants::kEventTypeEnd);
voice.events.push_back(constants::kEventTypeCancelled);
voice.events.push_back(constants::kEventTypeInterrupted);
if (platform_impl->SendsEvent(TTS_EVENT_START))
voice.events.push_back(constants::kEventTypeStart);
if (platform_impl->SendsEvent(TTS_EVENT_WORD))
voice.events.push_back(constants::kEventTypeWord);
if (platform_impl->SendsEvent(TTS_EVENT_SENTENCE))
voice.events.push_back(constants::kEventTypeSentence);
if (platform_impl->SendsEvent(TTS_EVENT_MARKER))
voice.events.push_back(constants::kEventTypeMarker);
if (platform_impl->SendsEvent(TTS_EVENT_ERROR))
voice.events.push_back(constants::kEventTypeError);
}
GetExtensionVoices(profile, out_voices);
}
bool TtsController::IsSpeaking() {
return current_utterance_ != NULL || GetPlatformImpl()->IsSpeaking();
}
void TtsController::FinishCurrentUtterance() {
if (current_utterance_) {
if (!current_utterance_->finished())
current_utterance_->OnTtsEvent(TTS_EVENT_INTERRUPTED, kInvalidCharIndex,
std::string());
delete current_utterance_;
current_utterance_ = NULL;
}
}
void TtsController::SpeakNextUtterance() {
// Start speaking the next utterance in the queue. Keep trying in case
// one fails but there are still more in the queue to try.
while (!utterance_queue_.empty() && !current_utterance_) {
Utterance* utterance = utterance_queue_.front();
utterance_queue_.pop();
SpeakNow(utterance);
}
}
void TtsController::RetrySpeakingQueuedUtterances() {
if (current_utterance_ == NULL && !utterance_queue_.empty())
SpeakNextUtterance();
}
void TtsController::ClearUtteranceQueue(bool send_events) {
while (!utterance_queue_.empty()) {
Utterance* utterance = utterance_queue_.front();
utterance_queue_.pop();
if (send_events)
utterance->OnTtsEvent(TTS_EVENT_CANCELLED, kInvalidCharIndex,
std::string());
else
utterance->Finish();
delete utterance;
}
}
void TtsController::SetPlatformImpl(
TtsPlatformImpl* platform_impl) {
platform_impl_ = platform_impl;
}
int TtsController::QueueSize() {
return static_cast<int>(utterance_queue_.size());
}
TtsPlatformImpl* TtsController::GetPlatformImpl() {
if (!platform_impl_)
platform_impl_ = TtsPlatformImpl::GetInstance();
return platform_impl_;
}
|