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 "extension_tts_api.h" 6 7#include <string> 8 9#include "base/memory/singleton.h" 10#include "base/values.h" 11#include "chrome/browser/extensions/extension_function.h" 12 13#import <Cocoa/Cocoa.h> 14 15namespace util = extension_tts_api_util; 16 17class ExtensionTtsPlatformImplMac : 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 ExtensionTtsPlatformImplMac* GetInstance(); 33 34 private: 35 ExtensionTtsPlatformImplMac(); 36 virtual ~ExtensionTtsPlatformImplMac() {} 37 38 NSSpeechSynthesizer* speech_synthesizer_; 39 40 friend struct DefaultSingletonTraits<ExtensionTtsPlatformImplMac>; 41 42 DISALLOW_COPY_AND_ASSIGN(ExtensionTtsPlatformImplMac); 43}; 44 45// static 46ExtensionTtsPlatformImpl* ExtensionTtsPlatformImpl::GetInstance() { 47 return ExtensionTtsPlatformImplMac::GetInstance(); 48} 49 50bool ExtensionTtsPlatformImplMac::Speak( 51 const std::string& utterance, 52 const std::string& language, 53 const std::string& gender, 54 double rate, 55 double pitch, 56 double volume) { 57 // NSSpeechSynthesizer equivalents for kGenderKey and kLanguageNameKey do 58 // not exist and thus are not supported. 59 60 if (rate >= 0.0) { 61 // The TTS api defines rate via words per minute. 62 [speech_synthesizer_ 63 setObject:[NSNumber numberWithInt:rate * 400] 64 forProperty:NSSpeechRateProperty error:nil]; 65 } 66 67 if (pitch >= 0.0) { 68 // The TTS api allows an approximate range of 30 to 65 for speech pitch. 69 [speech_synthesizer_ 70 setObject: [NSNumber numberWithInt:(pitch * 35 + 30)] 71 forProperty:NSSpeechPitchBaseProperty error:nil]; 72 } 73 74 if (volume >= 0.0) { 75 [speech_synthesizer_ 76 setObject: [NSNumber numberWithFloat:volume] 77 forProperty:NSSpeechVolumeProperty error:nil]; 78 } 79 80 return [speech_synthesizer_ startSpeakingString: 81 [NSString stringWithUTF8String: utterance.c_str()]]; 82} 83 84bool ExtensionTtsPlatformImplMac::StopSpeaking() { 85 [speech_synthesizer_ stopSpeaking]; 86 return true; 87} 88 89bool ExtensionTtsPlatformImplMac::IsSpeaking() { 90 return [speech_synthesizer_ isSpeaking]; 91} 92 93ExtensionTtsPlatformImplMac::ExtensionTtsPlatformImplMac() { 94 speech_synthesizer_ = [[NSSpeechSynthesizer alloc] init]; 95} 96 97// static 98ExtensionTtsPlatformImplMac* ExtensionTtsPlatformImplMac::GetInstance() { 99 return Singleton<ExtensionTtsPlatformImplMac>::get(); 100} 101