| 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 <string> | 5 #include <string> |
| 6 | 6 |
| 7 #include "base/memory/scoped_nsobject.h" | 7 #include "base/memory/scoped_nsobject.h" |
| 8 #include "base/memory/singleton.h" | 8 #include "base/memory/singleton.h" |
| 9 #include "base/strings/sys_string_conversions.h" | 9 #include "base/strings/sys_string_conversions.h" |
| 10 #include "base/values.h" | 10 #include "base/values.h" |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 56 | 56 |
| 57 virtual bool Speak( | 57 virtual bool Speak( |
| 58 int utterance_id, | 58 int utterance_id, |
| 59 const std::string& utterance, | 59 const std::string& utterance, |
| 60 const std::string& lang, | 60 const std::string& lang, |
| 61 const VoiceData& voice, | 61 const VoiceData& voice, |
| 62 const UtteranceContinuousParameters& params) OVERRIDE; | 62 const UtteranceContinuousParameters& params) OVERRIDE; |
| 63 | 63 |
| 64 virtual bool StopSpeaking() OVERRIDE; | 64 virtual bool StopSpeaking() OVERRIDE; |
| 65 | 65 |
| 66 virtual void Pause() OVERRIDE; |
| 67 |
| 68 virtual void Resume() OVERRIDE; |
| 69 |
| 66 virtual bool IsSpeaking() OVERRIDE; | 70 virtual bool IsSpeaking() OVERRIDE; |
| 67 | 71 |
| 68 virtual void GetVoices(std::vector<VoiceData>* out_voices) OVERRIDE; | 72 virtual void GetVoices(std::vector<VoiceData>* out_voices) OVERRIDE; |
| 69 | 73 |
| 70 // Called by ChromeTtsDelegate when we get a callback from the | 74 // Called by ChromeTtsDelegate when we get a callback from the |
| 71 // native speech engine. | 75 // native speech engine. |
| 72 void OnSpeechEvent(NSSpeechSynthesizer* sender, | 76 void OnSpeechEvent(NSSpeechSynthesizer* sender, |
| 73 TtsEventType event_type, | 77 TtsEventType event_type, |
| 74 int char_index, | 78 int char_index, |
| 75 const std::string& error_message); | 79 const std::string& error_message); |
| 76 | 80 |
| 77 // Get the single instance of this class. | 81 // Get the single instance of this class. |
| 78 static TtsPlatformImplMac* GetInstance(); | 82 static TtsPlatformImplMac* GetInstance(); |
| 79 | 83 |
| 80 private: | 84 private: |
| 81 TtsPlatformImplMac(); | 85 TtsPlatformImplMac(); |
| 82 virtual ~TtsPlatformImplMac(); | 86 virtual ~TtsPlatformImplMac(); |
| 83 | 87 |
| 84 scoped_nsobject<SingleUseSpeechSynthesizer> speech_synthesizer_; | 88 scoped_nsobject<SingleUseSpeechSynthesizer> speech_synthesizer_; |
| 85 scoped_nsobject<ChromeTtsDelegate> delegate_; | 89 scoped_nsobject<ChromeTtsDelegate> delegate_; |
| 86 int utterance_id_; | 90 int utterance_id_; |
| 87 std::string utterance_; | 91 std::string utterance_; |
| 88 bool sent_start_event_; | 92 bool sent_start_event_; |
| 93 int last_char_index_; |
| 89 | 94 |
| 90 friend struct DefaultSingletonTraits<TtsPlatformImplMac>; | 95 friend struct DefaultSingletonTraits<TtsPlatformImplMac>; |
| 91 | 96 |
| 92 DISALLOW_COPY_AND_ASSIGN(TtsPlatformImplMac); | 97 DISALLOW_COPY_AND_ASSIGN(TtsPlatformImplMac); |
| 93 }; | 98 }; |
| 94 | 99 |
| 95 // static | 100 // static |
| 96 TtsPlatformImpl* TtsPlatformImpl::GetInstance() { | 101 TtsPlatformImpl* TtsPlatformImpl::GetInstance() { |
| 97 return TtsPlatformImplMac::GetInstance(); | 102 return TtsPlatformImplMac::GetInstance(); |
| 98 } | 103 } |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 161 } | 166 } |
| 162 | 167 |
| 163 bool TtsPlatformImplMac::StopSpeaking() { | 168 bool TtsPlatformImplMac::StopSpeaking() { |
| 164 if (speech_synthesizer_.get()) { | 169 if (speech_synthesizer_.get()) { |
| 165 [speech_synthesizer_ stopSpeaking]; | 170 [speech_synthesizer_ stopSpeaking]; |
| 166 speech_synthesizer_.reset(nil); | 171 speech_synthesizer_.reset(nil); |
| 167 } | 172 } |
| 168 return true; | 173 return true; |
| 169 } | 174 } |
| 170 | 175 |
| 176 void TtsPlatformImplMac::Pause() { |
| 177 if (speech_synthesizer_.get() && utterance_id_) { |
| 178 [speech_synthesizer_ pauseSpeakingAtBoundary:NSSpeechImmediateBoundary]; |
| 179 TtsController::GetInstance()->OnTtsEvent( |
| 180 utterance_id_, TTS_EVENT_PAUSE, last_char_index_, ""); |
| 181 } |
| 182 } |
| 183 |
| 184 void TtsPlatformImplMac::Resume() { |
| 185 if (speech_synthesizer_.get() && utterance_id_) { |
| 186 [speech_synthesizer_ continueSpeaking]; |
| 187 TtsController::GetInstance()->OnTtsEvent( |
| 188 utterance_id_, TTS_EVENT_RESUME, last_char_index_, ""); |
| 189 } |
| 190 } |
| 191 |
| 171 bool TtsPlatformImplMac::IsSpeaking() { | 192 bool TtsPlatformImplMac::IsSpeaking() { |
| 172 return [NSSpeechSynthesizer isAnyApplicationSpeaking]; | 193 return [NSSpeechSynthesizer isAnyApplicationSpeaking]; |
| 173 } | 194 } |
| 174 | 195 |
| 175 void TtsPlatformImplMac::GetVoices(std::vector<VoiceData>* outVoices) { | 196 void TtsPlatformImplMac::GetVoices(std::vector<VoiceData>* outVoices) { |
| 176 NSArray* voices = [NSSpeechSynthesizer availableVoices]; | 197 NSArray* voices = [NSSpeechSynthesizer availableVoices]; |
| 177 | 198 |
| 178 // Create a new temporary array of the available voices with | 199 // Create a new temporary array of the available voices with |
| 179 // the default voice first. | 200 // the default voice first. |
| 180 NSMutableArray* orderedVoices = | 201 NSMutableArray* orderedVoices = |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 216 else if ([gender isEqualToString:NSVoiceGenderFemale]) | 237 else if ([gender isEqualToString:NSVoiceGenderFemale]) |
| 217 data.gender = TTS_GENDER_FEMALE; | 238 data.gender = TTS_GENDER_FEMALE; |
| 218 else | 239 else |
| 219 data.gender = TTS_GENDER_NONE; | 240 data.gender = TTS_GENDER_NONE; |
| 220 data.events.insert(TTS_EVENT_START); | 241 data.events.insert(TTS_EVENT_START); |
| 221 data.events.insert(TTS_EVENT_END); | 242 data.events.insert(TTS_EVENT_END); |
| 222 data.events.insert(TTS_EVENT_WORD); | 243 data.events.insert(TTS_EVENT_WORD); |
| 223 data.events.insert(TTS_EVENT_ERROR); | 244 data.events.insert(TTS_EVENT_ERROR); |
| 224 data.events.insert(TTS_EVENT_CANCELLED); | 245 data.events.insert(TTS_EVENT_CANCELLED); |
| 225 data.events.insert(TTS_EVENT_INTERRUPTED); | 246 data.events.insert(TTS_EVENT_INTERRUPTED); |
| 247 data.events.insert(TTS_EVENT_PAUSE); |
| 248 data.events.insert(TTS_EVENT_RESUME); |
| 226 } | 249 } |
| 227 } | 250 } |
| 228 | 251 |
| 229 void TtsPlatformImplMac::OnSpeechEvent( | 252 void TtsPlatformImplMac::OnSpeechEvent( |
| 230 NSSpeechSynthesizer* sender, | 253 NSSpeechSynthesizer* sender, |
| 231 TtsEventType event_type, | 254 TtsEventType event_type, |
| 232 int char_index, | 255 int char_index, |
| 233 const std::string& error_message) { | 256 const std::string& error_message) { |
| 234 // Don't send events from an utterance that's already completed. | 257 // Don't send events from an utterance that's already completed. |
| 235 // This depends on the fact that we construct a new NSSpeechSynthesizer | 258 // This depends on the fact that we construct a new NSSpeechSynthesizer |
| 236 // each time we call Speak. | 259 // each time we call Speak. |
| 237 if (sender != speech_synthesizer_.get()) | 260 if (sender != speech_synthesizer_.get()) |
| 238 return; | 261 return; |
| 239 | 262 |
| 240 if (event_type == TTS_EVENT_END) | 263 if (event_type == TTS_EVENT_END) |
| 241 char_index = utterance_.size(); | 264 char_index = utterance_.size(); |
| 242 TtsController* controller = TtsController::GetInstance(); | 265 TtsController* controller = TtsController::GetInstance(); |
| 243 if (event_type == TTS_EVENT_WORD && !sent_start_event_) { | 266 if (event_type == TTS_EVENT_WORD && !sent_start_event_) { |
| 244 controller->OnTtsEvent( | 267 controller->OnTtsEvent( |
| 245 utterance_id_, TTS_EVENT_START, 0, ""); | 268 utterance_id_, TTS_EVENT_START, 0, ""); |
| 246 sent_start_event_ = true; | 269 sent_start_event_ = true; |
| 247 } | 270 } |
| 248 controller->OnTtsEvent( | 271 controller->OnTtsEvent( |
| 249 utterance_id_, event_type, char_index, error_message); | 272 utterance_id_, event_type, char_index, error_message); |
| 273 last_char_index_ = char_index; |
| 250 } | 274 } |
| 251 | 275 |
| 252 TtsPlatformImplMac::TtsPlatformImplMac() { | 276 TtsPlatformImplMac::TtsPlatformImplMac() { |
| 253 utterance_id_ = -1; | 277 utterance_id_ = -1; |
| 254 sent_start_event_ = true; | 278 sent_start_event_ = true; |
| 255 | 279 |
| 256 delegate_.reset([[ChromeTtsDelegate alloc] initWithPlatformImplMac:this]); | 280 delegate_.reset([[ChromeTtsDelegate alloc] initWithPlatformImplMac:this]); |
| 257 } | 281 } |
| 258 | 282 |
| 259 TtsPlatformImplMac::~TtsPlatformImplMac() { | 283 TtsPlatformImplMac::~TtsPlatformImplMac() { |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 313 didSpeak_ = true; | 337 didSpeak_ = true; |
| 314 return [super startSpeakingString:utterance_]; | 338 return [super startSpeakingString:utterance_]; |
| 315 } | 339 } |
| 316 | 340 |
| 317 - (bool)startSpeakingString:(NSString*)utterance { | 341 - (bool)startSpeakingString:(NSString*)utterance { |
| 318 CHECK(false); | 342 CHECK(false); |
| 319 return false; | 343 return false; |
| 320 } | 344 } |
| 321 | 345 |
| 322 @end | 346 @end |
| OLD | NEW |