Home | History | Annotate | Download | only in extensions
      1 // Copyright (c) 2011 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/memory/singleton.h"
      8 #include "base/string_number_conversions.h"
      9 #include "chrome/browser/chromeos/cros/cros_library.h"
     10 #include "chrome/browser/chromeos/cros/speech_synthesis_library.h"
     11 
     12 namespace util = extension_tts_api_util;
     13 
     14 using base::DoubleToString;
     15 
     16 namespace {
     17 const char kCrosLibraryNotLoadedError[] = "Cros shared library not loaded.";
     18 };
     19 
     20 class ExtensionTtsPlatformImplChromeOs : public ExtensionTtsPlatformImpl {
     21  public:
     22   virtual bool Speak(
     23       const std::string& utterance,
     24       const std::string& locale,
     25       const std::string& gender,
     26       double rate,
     27       double pitch,
     28       double volume);
     29 
     30   virtual bool StopSpeaking();
     31 
     32   virtual bool IsSpeaking();
     33 
     34   // Get the single instance of this class.
     35   static ExtensionTtsPlatformImplChromeOs* GetInstance();
     36 
     37  private:
     38   ExtensionTtsPlatformImplChromeOs() {}
     39   virtual ~ExtensionTtsPlatformImplChromeOs() {}
     40 
     41   void AppendSpeakOption(std::string key,
     42                          std::string value,
     43                          std::string* options);
     44 
     45   friend struct DefaultSingletonTraits<ExtensionTtsPlatformImplChromeOs>;
     46 
     47   DISALLOW_COPY_AND_ASSIGN(ExtensionTtsPlatformImplChromeOs);
     48 };
     49 
     50 // static
     51 ExtensionTtsPlatformImpl* ExtensionTtsPlatformImpl::GetInstance() {
     52   return ExtensionTtsPlatformImplChromeOs::GetInstance();
     53 }
     54 
     55 bool ExtensionTtsPlatformImplChromeOs::Speak(
     56     const std::string& utterance,
     57     const std::string& locale,
     58     const std::string& gender,
     59     double rate,
     60     double pitch,
     61     double volume) {
     62   chromeos::CrosLibrary* cros_library = chromeos::CrosLibrary::Get();
     63   if (!cros_library->EnsureLoaded()) {
     64     set_error(kCrosLibraryNotLoadedError);
     65     return false;
     66   }
     67 
     68   std::string options;
     69 
     70   if (!locale.empty()) {
     71     AppendSpeakOption(
     72         chromeos::SpeechSynthesisLibrary::kSpeechPropertyLocale,
     73         locale,
     74         &options);
     75   }
     76 
     77   if (!gender.empty()) {
     78     AppendSpeakOption(
     79         chromeos::SpeechSynthesisLibrary::kSpeechPropertyGender,
     80         gender,
     81         &options);
     82   }
     83 
     84   if (rate >= 0.0) {
     85     AppendSpeakOption(
     86         chromeos::SpeechSynthesisLibrary::kSpeechPropertyRate,
     87         DoubleToString(rate * 5),
     88         &options);
     89   }
     90 
     91   if (pitch >= 0.0) {
     92     // The TTS service allows a range of 0 to 2 for speech pitch.
     93     AppendSpeakOption(
     94         chromeos::SpeechSynthesisLibrary::kSpeechPropertyPitch,
     95         DoubleToString(pitch * 2),
     96         &options);
     97   }
     98 
     99   if (volume >= 0.0) {
    100     // The TTS service allows a range of 0 to 5 for speech volume.
    101     AppendSpeakOption(
    102         chromeos::SpeechSynthesisLibrary::kSpeechPropertyVolume,
    103         DoubleToString(volume * 5),
    104         &options);
    105   }
    106 
    107   if (!options.empty()) {
    108     cros_library->GetSpeechSynthesisLibrary()->SetSpeakProperties(
    109         options.c_str());
    110   }
    111 
    112   return cros_library->GetSpeechSynthesisLibrary()->Speak(utterance.c_str());
    113 }
    114 
    115 bool ExtensionTtsPlatformImplChromeOs::StopSpeaking() {
    116   if (chromeos::CrosLibrary::Get()->EnsureLoaded()) {
    117     return chromeos::CrosLibrary::Get()->GetSpeechSynthesisLibrary()->
    118         StopSpeaking();
    119   }
    120 
    121   set_error(kCrosLibraryNotLoadedError);
    122   return false;
    123 }
    124 
    125 bool ExtensionTtsPlatformImplChromeOs::IsSpeaking() {
    126   if (chromeos::CrosLibrary::Get()->EnsureLoaded()) {
    127     return chromeos::CrosLibrary::Get()->GetSpeechSynthesisLibrary()->
    128         IsSpeaking();
    129   }
    130 
    131   set_error(kCrosLibraryNotLoadedError);
    132   return false;
    133 }
    134 
    135 void ExtensionTtsPlatformImplChromeOs::AppendSpeakOption(
    136     std::string key,
    137     std::string value,
    138     std::string* options) {
    139   *options +=
    140       key +
    141       chromeos::SpeechSynthesisLibrary::kSpeechPropertyEquals +
    142       value +
    143       chromeos::SpeechSynthesisLibrary::kSpeechPropertyDelimiter;
    144 }
    145 
    146 // static
    147 ExtensionTtsPlatformImplChromeOs*
    148 ExtensionTtsPlatformImplChromeOs::GetInstance() {
    149   return Singleton<ExtensionTtsPlatformImplChromeOs>::get();
    150 }
    151