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

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

Powered by Google App Engine
This is Rietveld 408576698