Index: chrome/browser/extensions/extension_tts_api_mac.mm |
=================================================================== |
--- chrome/browser/extensions/extension_tts_api_mac.mm (revision 62145) |
+++ chrome/browser/extensions/extension_tts_api_mac.mm (working copy) |
@@ -6,6 +6,7 @@ |
#include <string> |
+#include "base/singleton.h" |
#include "base/values.h" |
#include "chrome/browser/extensions/extension_function.h" |
@@ -13,57 +14,87 @@ |
namespace util = extension_tts_api_util; |
-static NSSpeechSynthesizer* speech_synthesizer_; |
+class ExtensionTtsPlatformImplMac : public ExtensionTtsPlatformImpl { |
+ public: |
+ virtual bool Speak( |
+ const std::string& utterance, |
+ const std::string& language, |
+ const std::string& gender, |
+ double rate, |
+ double pitch, |
+ double volume); |
-void InitializeSpeechSynthesizer() { |
- if (!speech_synthesizer_) |
- speech_synthesizer_ = [[NSSpeechSynthesizer alloc] init]; |
+ virtual bool StopSpeaking(); |
+ |
+ virtual bool IsSpeaking(); |
+ |
+ // Get the single instance of this class. |
+ static ExtensionTtsPlatformImplMac* GetInstance(); |
+ |
+ private: |
+ ExtensionTtsPlatformImplMac(); |
+ virtual ~ExtensionTtsPlatformImplMac() {} |
+ |
+ NSSpeechSynthesizer* speech_synthesizer_; |
+ |
+ friend struct DefaultSingletonTraits<ExtensionTtsPlatformImplMac>; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(ExtensionTtsPlatformImplMac); |
+}; |
+ |
+// static |
+ExtensionTtsPlatformImpl* ExtensionTtsPlatformImpl::GetInstance() { |
+ return ExtensionTtsPlatformImplMac::GetInstance(); |
} |
-bool ExtensionTtsSpeakFunction::RunImpl() { |
- InitializeSpeechSynthesizer(); |
- std::string utterance; |
- EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &utterance)); |
- DictionaryValue* speak_options = NULL; |
+bool ExtensionTtsPlatformImplMac::Speak( |
+ const std::string& utterance, |
+ const std::string& language, |
+ const std::string& gender, |
+ double rate, |
+ double pitch, |
+ double volume) { |
+ // NSSpeechSynthesizer equivalents for kGenderKey and kLanguageNameKey do |
+ // not exist and thus are not supported. |
- // Parse speech properties. |
- if (args_->GetDictionary(1, &speak_options)) { |
- std::string str_value; |
- double real_value; |
- // NSSpeechSynthesizer equivalents for kGenderKey and kLanguageNameKey do |
- // not exist and thus are not supported. |
- if (util::ReadNumberByKey(speak_options, util::kRateKey, &real_value)) { |
- // The TTS api defines rate via words per minute. |
- [speech_synthesizer_ |
- setObject:[NSNumber numberWithInt:real_value*400] |
- forProperty:NSSpeechRateProperty error:nil]; |
- } |
- if (util::ReadNumberByKey(speak_options, util::kPitchKey, &real_value)) { |
- // The TTS api allows an approximate range of 30 to 65 for speech pitch. |
- [speech_synthesizer_ |
- setObject: [NSNumber numberWithInt:(real_value*35 + 30)] |
- forProperty:NSSpeechPitchBaseProperty error:nil]; |
- } |
- if (util::ReadNumberByKey(speak_options, util::kVolumeKey, &real_value)) { |
- // The TTS api allows a range of 0.0 to 1.0 for speech volume. |
- [speech_synthesizer_ |
- setObject: [NSNumber numberWithFloat:real_value] |
- forProperty:NSSpeechVolumeProperty error:nil]; |
- } |
+ if (rate >= 0.0) { |
+ // The TTS api defines rate via words per minute. |
+ [speech_synthesizer_ |
+ setObject:[NSNumber numberWithInt:rate * 400] |
+ forProperty:NSSpeechRateProperty error:nil]; |
} |
- return |
- [speech_synthesizer_ startSpeakingString: |
- [NSString stringWithUTF8String: utterance.c_str()]]; |
+ if (pitch >= 0.0) { |
+ // The TTS api allows an approximate range of 30 to 65 for speech pitch. |
+ [speech_synthesizer_ |
+ setObject: [NSNumber numberWithInt:(pitch * 35 + 30)] |
+ forProperty:NSSpeechPitchBaseProperty error:nil]; |
+ } |
+ |
+ if (volume >= 0.0) { |
+ [speech_synthesizer_ |
+ setObject: [NSNumber numberWithFloat:volume] |
+ forProperty:NSSpeechVolumeProperty error:nil]; |
+ } |
+ |
+ return [speech_synthesizer_ startSpeakingString: |
+ [NSString stringWithUTF8String: utterance.c_str()]]; |
} |
-bool ExtensionTtsStopSpeakingFunction::RunImpl() { |
- InitializeSpeechSynthesizer(); |
+bool ExtensionTtsPlatformImplMac::StopSpeaking() { |
[speech_synthesizer_ stopSpeaking]; |
return true; |
} |
-bool ExtensionTtsIsSpeakingFunction::RunImpl() { |
- InitializeSpeechSynthesizer(); |
+bool ExtensionTtsPlatformImplMac::IsSpeaking() { |
return [speech_synthesizer_ isSpeaking]; |
} |
+ |
+ExtensionTtsPlatformImplMac::ExtensionTtsPlatformImplMac() { |
+ speech_synthesizer_ = [[NSSpeechSynthesizer alloc] init]; |
+} |
+ |
+// static |
+ExtensionTtsPlatformImplMac* ExtensionTtsPlatformImplMac::GetInstance() { |
+ return Singleton<ExtensionTtsPlatformImplMac>::get(); |
+} |