OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2010 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/extensions/extension_tts_api.h" |
| 6 |
| 7 #include "base/singleton.h" |
| 8 #include "chrome/browser/chromeos/cros/cros_library.h" |
| 9 #include "chrome/browser/chromeos/cros/speech_synthesis_library.h" |
| 10 |
| 11 namespace util = extension_tts_api_util; |
| 12 |
| 13 namespace { |
| 14 const char kCrosLibraryNotLoadedError[] = "Cros shared library not loaded."; |
| 15 }; |
| 16 |
| 17 class ExtensionTtsPlatformImplChromeOs : public ExtensionTtsPlatformImpl { |
| 18 public: |
| 19 virtual bool Speak( |
| 20 const std::string& utterance, |
| 21 const std::string& language, |
| 22 const std::string& gender, |
| 23 double rate, |
| 24 double pitch, |
| 25 double volume); |
| 26 |
| 27 virtual bool StopSpeaking(); |
| 28 |
| 29 virtual bool IsSpeaking(); |
| 30 |
| 31 // Get the single instance of this class. |
| 32 static ExtensionTtsPlatformImplChromeOs* GetInstance(); |
| 33 |
| 34 private: |
| 35 ExtensionTtsPlatformImplChromeOs() {} |
| 36 virtual ~ExtensionTtsPlatformImplChromeOs() {} |
| 37 |
| 38 friend struct DefaultSingletonTraits<ExtensionTtsPlatformImplChromeOs>; |
| 39 |
| 40 DISALLOW_COPY_AND_ASSIGN(ExtensionTtsPlatformImplChromeOs); |
| 41 }; |
| 42 |
| 43 // static |
| 44 ExtensionTtsPlatformImpl* ExtensionTtsPlatformImpl::GetInstance() { |
| 45 return ExtensionTtsPlatformImplChromeOs::GetInstance(); |
| 46 } |
| 47 |
| 48 bool ExtensionTtsPlatformImplChromeOs::Speak( |
| 49 const std::string& utterance, |
| 50 const std::string& language, |
| 51 const std::string& gender, |
| 52 double rate, |
| 53 double pitch, |
| 54 double volume) { |
| 55 chromeos::CrosLibrary* cros_library = chromeos::CrosLibrary::Get(); |
| 56 if (!cros_library->EnsureLoaded()) { |
| 57 set_error(kCrosLibraryNotLoadedError); |
| 58 return false; |
| 59 } |
| 60 |
| 61 std::string options; |
| 62 |
| 63 if (!language.empty()) { |
| 64 util::AppendSpeakOption( |
| 65 std::string(util::kNameKey), language, &options); |
| 66 } |
| 67 |
| 68 if (!gender.empty()) { |
| 69 util::AppendSpeakOption( |
| 70 std::string(util::kGenderKey), gender, &options); |
| 71 } |
| 72 |
| 73 if (rate >= 0.0) { |
| 74 util::AppendSpeakOption( |
| 75 std::string(util::kRateKey), DoubleToString(rate * 5), &options); |
| 76 } |
| 77 |
| 78 if (pitch >= 0.0) { |
| 79 // The TTS service allows a range of 0 to 2 for speech pitch. |
| 80 util::AppendSpeakOption( |
| 81 std::string(util::kPitchKey), DoubleToString(pitch * 2), &options); |
| 82 } |
| 83 |
| 84 if (volume >= 0.0) { |
| 85 // The TTS service allows a range of 0 to 5 for speech volume. |
| 86 util::AppendSpeakOption( |
| 87 std::string(util::kVolumeKey), DoubleToString(volume * 5), &options); |
| 88 } |
| 89 |
| 90 if (!options.empty()) { |
| 91 cros_library->GetSpeechSynthesisLibrary()->SetSpeakProperties( |
| 92 options.c_str()); |
| 93 } |
| 94 |
| 95 return cros_library->GetSpeechSynthesisLibrary()->Speak(utterance.c_str()); |
| 96 } |
| 97 |
| 98 bool ExtensionTtsPlatformImplChromeOs::StopSpeaking() { |
| 99 if (chromeos::CrosLibrary::Get()->EnsureLoaded()) { |
| 100 return chromeos::CrosLibrary::Get()->GetSpeechSynthesisLibrary()-> |
| 101 StopSpeaking(); |
| 102 } |
| 103 |
| 104 set_error(kCrosLibraryNotLoadedError); |
| 105 return false; |
| 106 } |
| 107 |
| 108 bool ExtensionTtsPlatformImplChromeOs::IsSpeaking() { |
| 109 if (chromeos::CrosLibrary::Get()->EnsureLoaded()) { |
| 110 return chromeos::CrosLibrary::Get()->GetSpeechSynthesisLibrary()-> |
| 111 IsSpeaking(); |
| 112 } |
| 113 |
| 114 set_error(kCrosLibraryNotLoadedError); |
| 115 return false; |
| 116 } |
| 117 |
| 118 // static |
| 119 ExtensionTtsPlatformImplChromeOs* |
| 120 ExtensionTtsPlatformImplChromeOs::GetInstance() { |
| 121 return Singleton<ExtensionTtsPlatformImplChromeOs>::get(); |
| 122 } |
OLD | NEW |