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 #include "chrome/browser/speech/tts_controller.h" | 5 #include "chrome/browser/speech/tts_controller.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/float_util.h" | 10 #include "base/float_util.h" |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
110 // TtsController | 110 // TtsController |
111 // | 111 // |
112 | 112 |
113 // static | 113 // static |
114 TtsController* TtsController::GetInstance() { | 114 TtsController* TtsController::GetInstance() { |
115 return Singleton<TtsController>::get(); | 115 return Singleton<TtsController>::get(); |
116 } | 116 } |
117 | 117 |
118 TtsController::TtsController() | 118 TtsController::TtsController() |
119 : current_utterance_(NULL), | 119 : current_utterance_(NULL), |
| 120 paused_(false), |
120 platform_impl_(NULL) { | 121 platform_impl_(NULL) { |
121 } | 122 } |
122 | 123 |
123 TtsController::~TtsController() { | 124 TtsController::~TtsController() { |
124 if (current_utterance_) { | 125 if (current_utterance_) { |
125 current_utterance_->Finish(); | 126 current_utterance_->Finish(); |
126 delete current_utterance_; | 127 delete current_utterance_; |
127 } | 128 } |
128 | 129 |
129 // Clear any queued utterances too. | 130 // Clear any queued utterances too. |
130 ClearUtteranceQueue(false); // Don't sent events. | 131 ClearUtteranceQueue(false); // Don't sent events. |
131 } | 132 } |
132 | 133 |
133 void TtsController::SpeakOrEnqueue(Utterance* utterance) { | 134 void TtsController::SpeakOrEnqueue(Utterance* utterance) { |
134 if (IsSpeaking() && utterance->can_enqueue()) { | 135 // If we're paused and we get an utterance that can't be queued, |
| 136 // flush the queue but stay in the paused state. |
| 137 if (paused_ && !utterance->can_enqueue()) { |
| 138 Stop(); |
| 139 paused_ = true; |
| 140 return; |
| 141 } |
| 142 |
| 143 if (paused_ || (IsSpeaking() && utterance->can_enqueue())) { |
135 utterance_queue_.push(utterance); | 144 utterance_queue_.push(utterance); |
136 } else { | 145 } else { |
137 Stop(); | 146 Stop(); |
138 SpeakNow(utterance); | 147 SpeakNow(utterance); |
139 } | 148 } |
140 } | 149 } |
141 | 150 |
142 void TtsController::SpeakNow(Utterance* utterance) { | 151 void TtsController::SpeakNow(Utterance* utterance) { |
143 // Get all available voices and try to find a matching voice. | 152 // Get all available voices and try to find a matching voice. |
144 std::vector<VoiceData> voices; | 153 std::vector<VoiceData> voices; |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
189 utterance->OnTtsEvent(TTS_EVENT_ERROR, kInvalidCharIndex, | 198 utterance->OnTtsEvent(TTS_EVENT_ERROR, kInvalidCharIndex, |
190 GetPlatformImpl()->error()); | 199 GetPlatformImpl()->error()); |
191 delete utterance; | 200 delete utterance; |
192 return; | 201 return; |
193 } | 202 } |
194 current_utterance_ = utterance; | 203 current_utterance_ = utterance; |
195 } | 204 } |
196 } | 205 } |
197 | 206 |
198 void TtsController::Stop() { | 207 void TtsController::Stop() { |
| 208 paused_ = false; |
199 if (current_utterance_ && !current_utterance_->extension_id().empty()) { | 209 if (current_utterance_ && !current_utterance_->extension_id().empty()) { |
200 ExtensionTtsEngineStop(current_utterance_); | 210 ExtensionTtsEngineStop(current_utterance_); |
201 } else { | 211 } else { |
202 GetPlatformImpl()->clear_error(); | 212 GetPlatformImpl()->clear_error(); |
203 GetPlatformImpl()->StopSpeaking(); | 213 GetPlatformImpl()->StopSpeaking(); |
204 } | 214 } |
205 | 215 |
206 if (current_utterance_) | 216 if (current_utterance_) |
207 current_utterance_->OnTtsEvent(TTS_EVENT_INTERRUPTED, kInvalidCharIndex, | 217 current_utterance_->OnTtsEvent(TTS_EVENT_INTERRUPTED, kInvalidCharIndex, |
208 std::string()); | 218 std::string()); |
209 FinishCurrentUtterance(); | 219 FinishCurrentUtterance(); |
210 ClearUtteranceQueue(true); // Send events. | 220 ClearUtteranceQueue(true); // Send events. |
211 } | 221 } |
212 | 222 |
| 223 void TtsController::Pause() { |
| 224 paused_ = true; |
| 225 if (current_utterance_ && !current_utterance_->extension_id().empty()) { |
| 226 ExtensionTtsEnginePause(current_utterance_); |
| 227 } else if (current_utterance_) { |
| 228 GetPlatformImpl()->clear_error(); |
| 229 GetPlatformImpl()->Pause(); |
| 230 } |
| 231 } |
| 232 |
| 233 void TtsController::Resume() { |
| 234 paused_ = false; |
| 235 if (current_utterance_ && !current_utterance_->extension_id().empty()) { |
| 236 ExtensionTtsEngineResume(current_utterance_); |
| 237 } else if (current_utterance_) { |
| 238 GetPlatformImpl()->clear_error(); |
| 239 GetPlatformImpl()->Resume(); |
| 240 } else { |
| 241 SpeakNextUtterance(); |
| 242 } |
| 243 } |
| 244 |
213 void TtsController::OnTtsEvent(int utterance_id, | 245 void TtsController::OnTtsEvent(int utterance_id, |
214 TtsEventType event_type, | 246 TtsEventType event_type, |
215 int char_index, | 247 int char_index, |
216 const std::string& error_message) { | 248 const std::string& error_message) { |
217 // We may sometimes receive completion callbacks "late", after we've | 249 // We may sometimes receive completion callbacks "late", after we've |
218 // already finished the utterance (for example because another utterance | 250 // already finished the utterance (for example because another utterance |
219 // interrupted or we got a call to Stop). This is normal and we can | 251 // interrupted or we got a call to Stop). This is normal and we can |
220 // safely just ignore these events. | 252 // safely just ignore these events. |
221 if (!current_utterance_ || utterance_id != current_utterance_->id()) | 253 if (!current_utterance_ || utterance_id != current_utterance_->id()) |
222 return; | 254 return; |
(...skipping 23 matching lines...) Expand all Loading... |
246 if (current_utterance_) { | 278 if (current_utterance_) { |
247 if (!current_utterance_->finished()) | 279 if (!current_utterance_->finished()) |
248 current_utterance_->OnTtsEvent(TTS_EVENT_INTERRUPTED, kInvalidCharIndex, | 280 current_utterance_->OnTtsEvent(TTS_EVENT_INTERRUPTED, kInvalidCharIndex, |
249 std::string()); | 281 std::string()); |
250 delete current_utterance_; | 282 delete current_utterance_; |
251 current_utterance_ = NULL; | 283 current_utterance_ = NULL; |
252 } | 284 } |
253 } | 285 } |
254 | 286 |
255 void TtsController::SpeakNextUtterance() { | 287 void TtsController::SpeakNextUtterance() { |
| 288 if (paused_) |
| 289 return; |
| 290 |
256 // Start speaking the next utterance in the queue. Keep trying in case | 291 // Start speaking the next utterance in the queue. Keep trying in case |
257 // one fails but there are still more in the queue to try. | 292 // one fails but there are still more in the queue to try. |
258 while (!utterance_queue_.empty() && !current_utterance_) { | 293 while (!utterance_queue_.empty() && !current_utterance_) { |
259 Utterance* utterance = utterance_queue_.front(); | 294 Utterance* utterance = utterance_queue_.front(); |
260 utterance_queue_.pop(); | 295 utterance_queue_.pop(); |
261 SpeakNow(utterance); | 296 SpeakNow(utterance); |
262 } | 297 } |
263 } | 298 } |
264 | 299 |
265 void TtsController::RetrySpeakingQueuedUtterances() { | 300 void TtsController::RetrySpeakingQueuedUtterances() { |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
346 continue; | 381 continue; |
347 } | 382 } |
348 | 383 |
349 return static_cast<int>(i); | 384 return static_cast<int>(i); |
350 } | 385 } |
351 } | 386 } |
352 | 387 |
353 return -1; | 388 return -1; |
354 } | 389 } |
355 | 390 |
OLD | NEW |