Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(997)

Side by Side Diff: chrome/browser/speech/tts_controller.cc

Issue 15108002: Add Pause and Resume to Web TTS & Extension TTS APIs (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix typo Created 7 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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 (paused_ || (IsSpeaking() && utterance->can_enqueue())) {
David Tseng 2013/05/13 17:49:54 I don't remember why isSpeaking is needed here. It
dmazzoni 2013/05/13 19:34:38 Are you thinking about the change we made on Mac w
dmazzoni 2013/05/14 16:50:27 Got it. That won't happen because IsSpeaking check
135 utterance_queue_.push(utterance); 136 utterance_queue_.push(utterance);
136 } else { 137 } else {
David Tseng 2013/05/13 17:49:54 What happens if we're currently paused and get a n
dmazzoni 2013/05/14 16:50:27 Done.
137 Stop(); 138 Stop();
138 SpeakNow(utterance); 139 SpeakNow(utterance);
139 } 140 }
140 } 141 }
141 142
142 void TtsController::SpeakNow(Utterance* utterance) { 143 void TtsController::SpeakNow(Utterance* utterance) {
143 // Get all available voices and try to find a matching voice. 144 // Get all available voices and try to find a matching voice.
144 std::vector<VoiceData> voices; 145 std::vector<VoiceData> voices;
145 GetVoices(utterance->profile(), &voices); 146 GetVoices(utterance->profile(), &voices);
146 int index = GetMatchingVoice(utterance, voices); 147 int index = GetMatchingVoice(utterance, voices);
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
189 utterance->OnTtsEvent(TTS_EVENT_ERROR, kInvalidCharIndex, 190 utterance->OnTtsEvent(TTS_EVENT_ERROR, kInvalidCharIndex,
190 GetPlatformImpl()->error()); 191 GetPlatformImpl()->error());
191 delete utterance; 192 delete utterance;
192 return; 193 return;
193 } 194 }
194 current_utterance_ = utterance; 195 current_utterance_ = utterance;
195 } 196 }
196 } 197 }
197 198
198 void TtsController::Stop() { 199 void TtsController::Stop() {
200 paused_ = false;
199 if (current_utterance_ && !current_utterance_->extension_id().empty()) { 201 if (current_utterance_ && !current_utterance_->extension_id().empty()) {
200 ExtensionTtsEngineStop(current_utterance_); 202 ExtensionTtsEngineStop(current_utterance_);
201 } else { 203 } else {
202 GetPlatformImpl()->clear_error(); 204 GetPlatformImpl()->clear_error();
203 GetPlatformImpl()->StopSpeaking(); 205 GetPlatformImpl()->StopSpeaking();
204 } 206 }
205 207
206 if (current_utterance_) 208 if (current_utterance_)
207 current_utterance_->OnTtsEvent(TTS_EVENT_INTERRUPTED, kInvalidCharIndex, 209 current_utterance_->OnTtsEvent(TTS_EVENT_INTERRUPTED, kInvalidCharIndex,
208 std::string()); 210 std::string());
209 FinishCurrentUtterance(); 211 FinishCurrentUtterance();
210 ClearUtteranceQueue(true); // Send events. 212 ClearUtteranceQueue(true); // Send events.
211 } 213 }
212 214
215 void TtsController::Pause() {
216 paused_ = true;
217 if (current_utterance_ && !current_utterance_->extension_id().empty()) {
218 ExtensionTtsEnginePause(current_utterance_);
219 } else if (current_utterance_) {
220 GetPlatformImpl()->clear_error();
221 GetPlatformImpl()->Pause();
222 }
223 }
224
225 void TtsController::Resume() {
226 paused_ = false;
227 if (current_utterance_ && !current_utterance_->extension_id().empty()) {
228 ExtensionTtsEngineResume(current_utterance_);
229 } else if (current_utterance_) {
230 GetPlatformImpl()->clear_error();
231 GetPlatformImpl()->Resume();
232 } else {
233 SpeakNextUtterance();
234 }
235 }
236
213 void TtsController::OnTtsEvent(int utterance_id, 237 void TtsController::OnTtsEvent(int utterance_id,
214 TtsEventType event_type, 238 TtsEventType event_type,
215 int char_index, 239 int char_index,
216 const std::string& error_message) { 240 const std::string& error_message) {
217 // We may sometimes receive completion callbacks "late", after we've 241 // We may sometimes receive completion callbacks "late", after we've
218 // already finished the utterance (for example because another utterance 242 // already finished the utterance (for example because another utterance
219 // interrupted or we got a call to Stop). This is normal and we can 243 // interrupted or we got a call to Stop). This is normal and we can
220 // safely just ignore these events. 244 // safely just ignore these events.
221 if (!current_utterance_ || utterance_id != current_utterance_->id()) 245 if (!current_utterance_ || utterance_id != current_utterance_->id())
222 return; 246 return;
(...skipping 23 matching lines...) Expand all
246 if (current_utterance_) { 270 if (current_utterance_) {
247 if (!current_utterance_->finished()) 271 if (!current_utterance_->finished())
248 current_utterance_->OnTtsEvent(TTS_EVENT_INTERRUPTED, kInvalidCharIndex, 272 current_utterance_->OnTtsEvent(TTS_EVENT_INTERRUPTED, kInvalidCharIndex,
249 std::string()); 273 std::string());
250 delete current_utterance_; 274 delete current_utterance_;
251 current_utterance_ = NULL; 275 current_utterance_ = NULL;
252 } 276 }
253 } 277 }
254 278
255 void TtsController::SpeakNextUtterance() { 279 void TtsController::SpeakNextUtterance() {
280 if (paused_)
281 return;
282
256 // Start speaking the next utterance in the queue. Keep trying in case 283 // 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. 284 // one fails but there are still more in the queue to try.
258 while (!utterance_queue_.empty() && !current_utterance_) { 285 while (!utterance_queue_.empty() && !current_utterance_) {
259 Utterance* utterance = utterance_queue_.front(); 286 Utterance* utterance = utterance_queue_.front();
260 utterance_queue_.pop(); 287 utterance_queue_.pop();
261 SpeakNow(utterance); 288 SpeakNow(utterance);
262 } 289 }
263 } 290 }
264 291
265 void TtsController::RetrySpeakingQueuedUtterances() { 292 void TtsController::RetrySpeakingQueuedUtterances() {
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after
346 continue; 373 continue;
347 } 374 }
348 375
349 return static_cast<int>(i); 376 return static_cast<int>(i);
350 } 377 }
351 } 378 }
352 379
353 return -1; 380 return -1;
354 } 381 }
355 382
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698