OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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/values.h" | 9 #include "base/values.h" |
| 10 #include "base/string_number_conversions.h" |
10 | 11 |
11 #include "chrome/browser/chromeos/cros/cros_library.h" | 12 #include "chrome/browser/chromeos/cros/cros_library.h" |
12 #include "chrome/browser/chromeos/cros/speech_synthesis_library.h" | 13 #include "chrome/browser/chromeos/cros/speech_synthesis_library.h" |
13 | 14 |
| 15 using base::DoubleToString; |
| 16 |
| 17 const char kNameKey[] = "name"; |
| 18 const char kLanguageNameKey[] = "languageName"; |
| 19 const char kGenderKey[] = "gender"; |
| 20 const char kRateKey[] = "rate"; |
| 21 const char kPitchKey[] = "pitch"; |
| 22 const char kVolumeKey[] = "volume"; |
| 23 const char kEqualStr[] = "="; |
| 24 const char kDelimiter[] = ";"; |
| 25 |
14 namespace { | 26 namespace { |
15 const char kCrosLibraryNotLoadedError[] = | 27 const char kCrosLibraryNotLoadedError[] = |
16 "Cros shared library not loaded."; | 28 "Cros shared library not loaded."; |
| 29 |
| 30 bool ReadNumberByKey(DictionaryValue* dict, const char* key, |
| 31 double* ret_value) { |
| 32 Value* value; |
| 33 dict->Get(key, &value); |
| 34 if (value->IsType(Value::TYPE_INTEGER)) { |
| 35 int int_value; |
| 36 if (!dict->GetInteger(key, &int_value)) |
| 37 return false; |
| 38 *ret_value = int_value; |
| 39 } else if (value->IsType(Value::TYPE_REAL)) { |
| 40 if (!dict->GetReal(key, ret_value)) |
| 41 return false; |
| 42 } else { |
| 43 return false; |
| 44 } |
| 45 return true; |
| 46 } |
| 47 |
| 48 void AppendSpeakOption(std::string key, std::string value, |
| 49 std::string* options) { |
| 50 *options += key + kEqualStr + value + kDelimiter; |
| 51 } |
17 }; | 52 }; |
18 | 53 |
19 bool ExtensionTtsSpeakFunction::RunImpl() { | 54 bool ExtensionTtsSpeakFunction::RunImpl() { |
20 std::string utterance; | 55 std::string utterance; |
| 56 std::string options = ""; |
| 57 DictionaryValue* speak_options = NULL; |
21 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &utterance)); | 58 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &utterance)); |
| 59 if (args_->GetDictionary(1, &speak_options)) { |
| 60 std::string str_value; |
| 61 double real_value; |
| 62 if (speak_options->HasKey(kLanguageNameKey) && |
| 63 speak_options->GetString(kLanguageNameKey, &str_value)) { |
| 64 AppendSpeakOption(std::string(kNameKey), str_value, &options); |
| 65 } |
| 66 if (speak_options->HasKey(kGenderKey) && |
| 67 speak_options->GetString(kGenderKey, &str_value)) { |
| 68 AppendSpeakOption(std::string(kGenderKey), str_value, &options); |
| 69 } |
| 70 if (ReadNumberByKey(speak_options, kRateKey, &real_value)) |
| 71 // The TTS service allows a range of 0 to 5 for speech rate. |
| 72 AppendSpeakOption(std::string(kRateKey), |
| 73 DoubleToString(real_value * 5), &options); |
| 74 if (ReadNumberByKey(speak_options, kPitchKey, &real_value)) |
| 75 // The TTS service allows a range of 0 to 2 for speech pitch. |
| 76 AppendSpeakOption(std::string(kPitchKey), |
| 77 DoubleToString(real_value * 2), &options); |
| 78 if (ReadNumberByKey(speak_options, kVolumeKey, &real_value)) |
| 79 // The TTS service allows a range of 0 to 5 for speech volume. |
| 80 AppendSpeakOption(std::string(kVolumeKey), |
| 81 DoubleToString(real_value * 5), &options); |
| 82 } |
22 if (chromeos::CrosLibrary::Get()->EnsureLoaded()) { | 83 if (chromeos::CrosLibrary::Get()->EnsureLoaded()) { |
| 84 if (!options.empty()) { |
| 85 chromeos::CrosLibrary::Get()->GetSpeechSynthesisLibrary()-> |
| 86 SetSpeakProperties(options.c_str()); |
| 87 } |
23 bool ret = chromeos::CrosLibrary::Get()->GetSpeechSynthesisLibrary()-> | 88 bool ret = chromeos::CrosLibrary::Get()->GetSpeechSynthesisLibrary()-> |
24 Speak(utterance.c_str()); | 89 Speak(utterance.c_str()); |
25 result_.reset(); | 90 result_.reset(); |
26 return ret; | 91 return ret; |
27 } | 92 } |
28 error_ = kCrosLibraryNotLoadedError; | 93 error_ = kCrosLibraryNotLoadedError; |
29 return false; | 94 return false; |
30 } | 95 } |
31 | 96 |
32 bool ExtensionTtsStopSpeakingFunction::RunImpl() { | 97 bool ExtensionTtsStopSpeakingFunction::RunImpl() { |
33 if (chromeos::CrosLibrary::Get()->EnsureLoaded()) { | 98 if (chromeos::CrosLibrary::Get()->EnsureLoaded()) { |
34 return chromeos::CrosLibrary::Get()->GetSpeechSynthesisLibrary()-> | 99 return chromeos::CrosLibrary::Get()->GetSpeechSynthesisLibrary()-> |
35 StopSpeaking(); | 100 StopSpeaking(); |
36 } | 101 } |
37 error_ = kCrosLibraryNotLoadedError; | 102 error_ = kCrosLibraryNotLoadedError; |
38 return false; | 103 return false; |
39 } | 104 } |
40 | 105 |
41 bool ExtensionTtsIsSpeakingFunction::RunImpl() { | 106 bool ExtensionTtsIsSpeakingFunction::RunImpl() { |
42 if (chromeos::CrosLibrary::Get()->EnsureLoaded()) { | 107 if (chromeos::CrosLibrary::Get()->EnsureLoaded()) { |
43 result_.reset(Value::CreateBooleanValue( | 108 result_.reset(Value::CreateBooleanValue( |
44 chromeos::CrosLibrary::Get()->GetSpeechSynthesisLibrary()-> | 109 chromeos::CrosLibrary::Get()->GetSpeechSynthesisLibrary()-> |
45 IsSpeaking())); | 110 IsSpeaking())); |
46 return true; | 111 return true; |
47 } | 112 } |
48 error_ = kCrosLibraryNotLoadedError; | 113 error_ = kCrosLibraryNotLoadedError; |
49 return false; | 114 return false; |
50 } | 115 } |
OLD | NEW |