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

Unified Diff: chrome/browser/speech/tts_request_handler.cc

Issue 12589005: Implement web speech synthesis. (Closed) Base URL: http://git.chromium.org/chromium/src.git@webtts
Patch Set: Created 7 years, 9 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 side-by-side diff with in-line comments
Download patch
Index: chrome/browser/speech/tts_request_handler.cc
diff --git a/chrome/browser/speech/tts_request_handler.cc b/chrome/browser/speech/tts_request_handler.cc
new file mode 100644
index 0000000000000000000000000000000000000000..574322499788cf855d4014af4891eab6ed435088
--- /dev/null
+++ b/chrome/browser/speech/tts_request_handler.cc
@@ -0,0 +1,135 @@
+// Copyright (c) 2013 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_request_handler.h"
+
+#include "base/logging.h"
+#include "chrome/browser/profiles/profile.h"
+#include "chrome/browser/speech/tts_controller.h"
+#include "content/public/browser/browser_context.h"
+
+class TtsRequestEventHandler : public UtteranceEventDelegate {
+ public:
+ TtsRequestEventHandler(content::TtsDispatcherHost* dispatcher_host,
+ int routing_id);
+ virtual void OnTtsEvent(Utterance* utterance,
+ TtsEventType event_type,
+ int char_index,
+ const std::string& error_message) OVERRIDE;
+ private:
+ content::TtsDispatcherHost* dispatcher_host_;
+ int routing_id_;
+};
+
+TtsRequestEventHandler::TtsRequestEventHandler(
+ content::TtsDispatcherHost* dispatcher_host,
+ int routing_id)
+ : dispatcher_host_(dispatcher_host),
+ routing_id_(routing_id) {
+}
+
+void TtsRequestEventHandler::OnTtsEvent(Utterance* utterance,
+ TtsEventType event_type,
+ int char_index,
+ const std::string& error_message) {
+ switch(event_type) {
+ case TTS_EVENT_START:
+ dispatcher_host_->DidStartSpeaking(routing_id_, utterance->src_id());
+ break;
+ case TTS_EVENT_END:
+ dispatcher_host_->DidFinishSpeaking(routing_id_, utterance->src_id());
+ break;
+ case TTS_EVENT_WORD:
+ dispatcher_host_->WordBoundary(
+ routing_id_, utterance->src_id(), char_index);
+ break;
+ case TTS_EVENT_SENTENCE:
+ dispatcher_host_->SentenceBoundary(
+ routing_id_, utterance->src_id(), char_index);
+ break;
+ case TTS_EVENT_MARKER:
+ dispatcher_host_->MarkerEvent(
+ routing_id_, utterance->src_id(), char_index);
+ break;
+ case TTS_EVENT_INTERRUPTED:
+ dispatcher_host_->WasInterrupted(routing_id_, utterance->src_id());
+ break;
+ case TTS_EVENT_CANCELLED:
+ dispatcher_host_->WasCancelled(routing_id_, utterance->src_id());
+ break;
+ case TTS_EVENT_ERROR:
+ dispatcher_host_->SpeakingErrorOccurred(
+ routing_id_, utterance->src_id(), error_message);
+ break;
+ }
+
+ if (utterance->finished())
+ delete this;
+}
+
+
+TtsRequestHandler::TtsRequestHandler() {
+}
+
+TtsRequestHandler::~TtsRequestHandler() {
+}
+
+void TtsRequestHandler::OnInitializeVoiceList(
+ content::TtsDispatcherHost* dispatcher_host,
+ int routing_id) {
+ content::BrowserContext* context = dispatcher_host->GetBrowserContext();
+ Profile* profile = Profile::FromBrowserContext(context);
tommi (sloooow) - chröme 2013/03/07 13:04:46 check FromBrowserContext (here and elsewhere)
dmazzoni 2013/03/19 17:30:22 Done.
+ TtsController* tts_controller = TtsController::GetInstance();
+ std::vector<VoiceData> voices;
+ tts_controller->GetVoices(profile, &voices);
+
+ std::vector<content::TtsVoice> out_voices;
+ for (size_t i = 0; i < voices.size(); i++) {
tommi (sloooow) - chröme 2013/03/07 13:04:46 ++i
dmazzoni 2013/03/19 17:30:22 Done.
+ out_voices.push_back(content::TtsVoice());
tommi (sloooow) - chröme 2013/03/07 13:04:46 do out_voices.reset(voices.size()) outside the loo
dmazzoni 2013/03/19 17:30:22 Agreed here, no reason not to.
+ content::TtsVoice* out_voice = &out_voices.back();
+ out_voice->voice_uri = voices[i].name;
+ out_voice->name = voices[i].name;
+ out_voice->lang = voices[i].lang;
+ out_voice->local_service = true;
+ out_voice->is_default = (i == 0);
+ }
+ dispatcher_host->SendVoiceList(routing_id, out_voices);
+}
+
+void TtsRequestHandler::OnSpeak(content::TtsDispatcherHost* dispatcher_host,
+ int routing_id,
+ const content::TtsUtteranceRequest& request) {
+ content::BrowserContext* context = dispatcher_host->GetBrowserContext();
+ Profile* profile = Profile::FromBrowserContext(context);
+
+ Utterance* utterance = new Utterance(profile);
hans 2013/03/09 14:19:52 scoped_ptr? Then it would be more clear that we're
dmazzoni 2013/03/19 17:30:22 Done.
+ utterance->set_src_id(request.id);
+ utterance->set_text(request.text);
+ utterance->set_lang(request.lang);
+ utterance->set_voice_name(request.voice);
+ utterance->set_can_enqueue(true);
+
+ UtteranceContinuousParameters params;
+ params.rate = request.rate;
+ params.pitch = request.pitch;
+ params.volume = request.volume;
+ utterance->set_continuous_parameters(params);
+
+ utterance->set_event_delegate(
+ new TtsRequestEventHandler(dispatcher_host, routing_id));
+
+ TtsController::GetInstance()->SpeakOrEnqueue(utterance);
+}
+
+void TtsRequestHandler::OnPause(content::TtsDispatcherHost* dispatcher_host) {
+ // Not supported by TtsController yet.
tommi (sloooow) - chröme 2013/03/07 13:04:46 NOTIMPLEMENTED() or TODO?
dmazzoni 2013/03/19 17:30:22 Done.
+}
+
+void TtsRequestHandler::OnResume(content::TtsDispatcherHost* dispatcher_host) {
+ // Not supported by TtsController yet.
+}
+
+void TtsRequestHandler::OnCancel(content::TtsDispatcherHost* dispatcher_host) {
+ TtsController::GetInstance()->Stop();
+}

Powered by Google App Engine
This is Rietveld 408576698