| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 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 | 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 "chrome/browser/extensions/extension_tts_api.h" | 5 #include "chrome/browser/extensions/extension_tts_api.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/float_util.h" |
| 9 #include "base/values.h" | 10 #include "base/values.h" |
| 10 #include "base/string_number_conversions.h" | 11 #include "base/string_number_conversions.h" |
| 11 | 12 |
| 12 #include "chrome/browser/chromeos/cros/cros_library.h" | |
| 13 #include "chrome/browser/chromeos/cros/speech_synthesis_library.h" | |
| 14 | |
| 15 namespace util = extension_tts_api_util; | 13 namespace util = extension_tts_api_util; |
| 16 | 14 |
| 17 using base::DoubleToString; | 15 using base::DoubleToString; |
| 18 | 16 |
| 19 namespace { | 17 namespace { |
| 20 const char kCrosLibraryNotLoadedError[] = | 18 const char kCrosLibraryNotLoadedError[] = |
| 21 "Cros shared library not loaded."; | 19 "Cros shared library not loaded."; |
| 22 }; | 20 }; |
| 23 | 21 |
| 24 bool ExtensionTtsSpeakFunction::RunImpl() { | 22 bool ExtensionTtsSpeakFunction::RunImpl() { |
| 25 std::string utterance; | 23 std::string utterance; |
| 26 std::string options = ""; | 24 std::string language; |
| 25 std::string gender; |
| 26 double rate = -1.0; |
| 27 double pitch = -1.0; |
| 28 double volume = -1.0; |
| 29 |
| 27 DictionaryValue* speak_options = NULL; | 30 DictionaryValue* speak_options = NULL; |
| 28 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &utterance)); | 31 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &utterance)); |
| 32 |
| 29 if (args_->GetDictionary(1, &speak_options)) { | 33 if (args_->GetDictionary(1, &speak_options)) { |
| 30 std::string str_value; | 34 if (speak_options->HasKey(util::kLanguageNameKey)) { |
| 31 double real_value; | 35 speak_options->GetString(util::kLanguageNameKey, &language); |
| 32 if (speak_options->HasKey(util::kLanguageNameKey) && | |
| 33 speak_options->GetString(util::kLanguageNameKey, &str_value)) { | |
| 34 util::AppendSpeakOption( | |
| 35 std::string(util::kNameKey), str_value, &options); | |
| 36 } | 36 } |
| 37 if (speak_options->HasKey(util::kGenderKey) && | 37 |
| 38 speak_options->GetString(util::kGenderKey, &str_value)) { | 38 if (speak_options->HasKey(util::kGenderKey)) { |
| 39 util::AppendSpeakOption( | 39 speak_options->GetString(util::kGenderKey, &gender); |
| 40 std::string(util::kGenderKey), str_value, &options); | |
| 41 } | 40 } |
| 42 if (util::ReadNumberByKey(speak_options, util::kRateKey, &real_value)) { | 41 |
| 43 // The TTS service allows a range of 0 to 5 for speech rate. | 42 if (util::ReadNumberByKey(speak_options, util::kRateKey, &rate)) { |
| 44 util::AppendSpeakOption(std::string(util::kRateKey), | 43 if (!base::IsFinite(rate) || rate < 0.0 || rate > 1.0) { |
| 45 DoubleToString(real_value * 5), &options); | 44 rate = -1.0; |
| 45 } |
| 46 } | 46 } |
| 47 if (util::ReadNumberByKey(speak_options, util::kPitchKey, &real_value)) { | 47 |
| 48 // The TTS service allows a range of 0 to 2 for speech pitch. | 48 if (util::ReadNumberByKey(speak_options, util::kPitchKey, &pitch)) { |
| 49 util::AppendSpeakOption(std::string(util::kPitchKey), | 49 if (!base::IsFinite(pitch) || pitch < 0.0 || pitch > 1.0) { |
| 50 DoubleToString(real_value * 2), &options); | 50 pitch = -1.0; |
| 51 } |
| 51 } | 52 } |
| 52 if (util::ReadNumberByKey(speak_options, util::kVolumeKey, &real_value)) { | 53 |
| 53 // The TTS service allows a range of 0 to 5 for speech volume. | 54 if (util::ReadNumberByKey(speak_options, util::kVolumeKey, &volume)) { |
| 54 util::AppendSpeakOption(std::string(util::kVolumeKey), | 55 if (!base::IsFinite(volume) || volume < 0.0 || volume > 1.0) { |
| 55 DoubleToString(real_value * 5), &options); | 56 volume = -1.0; |
| 57 } |
| 56 } | 58 } |
| 57 } | 59 } |
| 58 if (chromeos::CrosLibrary::Get()->EnsureLoaded()) { | 60 |
| 59 if (!options.empty()) { | 61 ExtensionTtsPlatformImpl* impl = ExtensionTtsPlatformImpl::GetInstance(); |
| 60 chromeos::CrosLibrary::Get()->GetSpeechSynthesisLibrary()-> | 62 impl->clear_error(); |
| 61 SetSpeakProperties(options.c_str()); | 63 return impl->Speak(utterance, language, gender, rate, pitch, volume); |
| 62 } | |
| 63 bool ret = chromeos::CrosLibrary::Get()->GetSpeechSynthesisLibrary()-> | |
| 64 Speak(utterance.c_str()); | |
| 65 result_.reset(); | |
| 66 return ret; | |
| 67 } | |
| 68 error_ = kCrosLibraryNotLoadedError; | |
| 69 return false; | |
| 70 } | 64 } |
| 71 | 65 |
| 72 bool ExtensionTtsStopSpeakingFunction::RunImpl() { | 66 bool ExtensionTtsStopSpeakingFunction::RunImpl() { |
| 73 if (chromeos::CrosLibrary::Get()->EnsureLoaded()) { | 67 ExtensionTtsPlatformImpl* impl = ExtensionTtsPlatformImpl::GetInstance(); |
| 74 return chromeos::CrosLibrary::Get()->GetSpeechSynthesisLibrary()-> | 68 impl->clear_error(); |
| 75 StopSpeaking(); | 69 return impl->StopSpeaking(); |
| 76 } | |
| 77 error_ = kCrosLibraryNotLoadedError; | |
| 78 return false; | |
| 79 } | 70 } |
| 80 | 71 |
| 81 bool ExtensionTtsIsSpeakingFunction::RunImpl() { | 72 bool ExtensionTtsIsSpeakingFunction::RunImpl() { |
| 82 if (chromeos::CrosLibrary::Get()->EnsureLoaded()) { | 73 ExtensionTtsPlatformImpl* impl = ExtensionTtsPlatformImpl::GetInstance(); |
| 83 result_.reset(Value::CreateBooleanValue( | 74 impl->clear_error(); |
| 84 chromeos::CrosLibrary::Get()->GetSpeechSynthesisLibrary()-> | 75 return impl->IsSpeaking(); |
| 85 IsSpeaking())); | |
| 86 return true; | |
| 87 } | |
| 88 error_ = kCrosLibraryNotLoadedError; | |
| 89 return false; | |
| 90 } | 76 } |
| OLD | NEW |