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

Side by Side 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: Fresh update 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 unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright (c) 2013 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.
4
5 #include "chrome/browser/speech/tts_request_handler.h"
6
7 #include "base/logging.h"
8 #include "chrome/browser/profiles/profile.h"
9 #include "chrome/browser/speech/tts_controller.h"
10 #include "content/public/browser/browser_context.h"
11
12 class TtsRequestEventHandler : public UtteranceEventDelegate {
13 public:
14 explicit TtsRequestEventHandler(content::TtsDispatcherHost* dispatcher_host);
15 virtual void OnTtsEvent(Utterance* utterance,
16 TtsEventType event_type,
17 int char_index,
18 const std::string& error_message) OVERRIDE;
19 private:
20 content::TtsDispatcherHost* dispatcher_host_;
21 };
22
23 TtsRequestEventHandler::TtsRequestEventHandler(
24 content::TtsDispatcherHost* dispatcher_host)
25 : dispatcher_host_(dispatcher_host) {
26 }
27
28 void TtsRequestEventHandler::OnTtsEvent(Utterance* utterance,
29 TtsEventType event_type,
30 int char_index,
31 const std::string& error_message) {
32 switch (event_type) {
33 case TTS_EVENT_START:
34 dispatcher_host_->DidStartSpeaking(utterance->src_id());
35 break;
36 case TTS_EVENT_END:
37 dispatcher_host_->DidFinishSpeaking(utterance->src_id());
38 break;
39 case TTS_EVENT_WORD:
40 dispatcher_host_->WordBoundary(
41 utterance->src_id(), char_index);
42 break;
43 case TTS_EVENT_SENTENCE:
44 dispatcher_host_->SentenceBoundary(
45 utterance->src_id(), char_index);
46 break;
47 case TTS_EVENT_MARKER:
48 dispatcher_host_->MarkerEvent(
49 utterance->src_id(), char_index);
50 break;
51 case TTS_EVENT_INTERRUPTED:
52 dispatcher_host_->WasInterrupted(utterance->src_id());
53 break;
54 case TTS_EVENT_CANCELLED:
55 dispatcher_host_->WasCancelled(utterance->src_id());
56 break;
57 case TTS_EVENT_ERROR:
58 dispatcher_host_->SpeakingErrorOccurred(
59 utterance->src_id(), error_message);
60 break;
61 }
62
63 if (utterance->finished())
64 delete this;
65 }
66
67
68 TtsRequestHandler::TtsRequestHandler() {
69 }
70
71 TtsRequestHandler::~TtsRequestHandler() {
72 }
73
74 void TtsRequestHandler::OnInitializeVoiceList(
75 content::TtsDispatcherHost* dispatcher_host) {
76 content::BrowserContext* context = dispatcher_host->GetBrowserContext();
77 Profile* profile = Profile::FromBrowserContext(context);
78 CHECK(profile);
79 TtsController* tts_controller = TtsController::GetInstance();
80 std::vector<VoiceData> voices;
81 tts_controller->GetVoices(profile, &voices);
82
83 std::vector<content::TtsVoice> out_voices;
84 out_voices.resize(voices.size());
85 for (size_t i = 0; i < voices.size(); ++i) {
86 content::TtsVoice& out_voice = out_voices[i];
87 out_voice.voice_uri = voices[i].name;
88 out_voice.name = voices[i].name;
89 out_voice.lang = voices[i].lang;
90 out_voice.local_service = true;
91 out_voice.is_default = (i == 0);
92 }
93 dispatcher_host->SendVoiceList(out_voices);
94 }
95
96 void TtsRequestHandler::OnSpeak(content::TtsDispatcherHost* dispatcher_host,
97 const content::TtsUtteranceRequest& request) {
98 content::BrowserContext* context = dispatcher_host->GetBrowserContext();
99 Profile* profile = Profile::FromBrowserContext(context);
100 CHECK(profile);
101
102 scoped_ptr<Utterance> utterance(new Utterance(profile));
103 utterance->set_src_id(request.id);
104 utterance->set_text(request.text);
105 utterance->set_lang(request.lang);
106 utterance->set_voice_name(request.voice);
107 utterance->set_can_enqueue(true);
108
109 UtteranceContinuousParameters params;
110 params.rate = request.rate;
111 params.pitch = request.pitch;
112 params.volume = request.volume;
113 utterance->set_continuous_parameters(params);
114
115 utterance->set_event_delegate(new TtsRequestEventHandler(dispatcher_host));
116
117 TtsController::GetInstance()->SpeakOrEnqueue(utterance.release());
118 }
119
120 void TtsRequestHandler::OnPause(content::TtsDispatcherHost* dispatcher_host) {
121 // TODO(dmazzoni): Not supported by TtsController yet.
122 }
123
124 void TtsRequestHandler::OnResume(content::TtsDispatcherHost* dispatcher_host) {
125 // TODO(dmazzoni): Not supported by TtsController yet.
126 }
127
128 void TtsRequestHandler::OnCancel(content::TtsDispatcherHost* dispatcher_host) {
129 TtsController::GetInstance()->Stop();
130 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698