Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(255)

Side by Side Diff: chrome/browser/extensions/extension_tts_api_mac.mm

Issue 3640001: Refactored TTS extension code so that the platform-specific TTS... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 10 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2010 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 "extension_tts_api.h" 5 #include "extension_tts_api.h"
6 6
7 #include <string> 7 #include <string>
8 8
9 #include "base/singleton.h"
9 #include "base/values.h" 10 #include "base/values.h"
10 #include "chrome/browser/extensions/extension_function.h" 11 #include "chrome/browser/extensions/extension_function.h"
11 12
12 #import <Cocoa/Cocoa.h> 13 #import <Cocoa/Cocoa.h>
13 14
14 namespace util = extension_tts_api_util; 15 namespace util = extension_tts_api_util;
15 16
16 static NSSpeechSynthesizer* speech_synthesizer_; 17 class 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);
17 26
18 void InitializeSpeechSynthesizer() { 27 virtual bool StopSpeaking();
19 if (!speech_synthesizer_) 28
20 speech_synthesizer_ = [[NSSpeechSynthesizer alloc] init]; 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
46 ExtensionTtsPlatformImpl* ExtensionTtsPlatformImpl::GetInstance() {
47 return ExtensionTtsPlatformImplMac::GetInstance();
21 } 48 }
22 49
23 bool ExtensionTtsSpeakFunction::RunImpl() { 50 bool ExtensionTtsPlatformImplMac::Speak(
24 InitializeSpeechSynthesizer(); 51 const std::string& utterance,
25 std::string utterance; 52 const std::string& language,
26 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &utterance)); 53 const std::string& gender,
27 DictionaryValue* speak_options = NULL; 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.
28 59
29 // Parse speech properties. 60 if (rate >= 0.0) {
30 if (args_->GetDictionary(1, &speak_options)) { 61 // The TTS api defines rate via words per minute.
31 std::string str_value; 62 [speech_synthesizer_
32 double real_value; 63 setObject:[NSNumber numberWithInt:rate * 400]
33 // NSSpeechSynthesizer equivalents for kGenderKey and kLanguageNameKey do 64 forProperty:NSSpeechRateProperty error:nil];
34 // not exist and thus are not supported.
35 if (util::ReadNumberByKey(speak_options, util::kRateKey, &real_value)) {
36 // The TTS api defines rate via words per minute.
37 [speech_synthesizer_
38 setObject:[NSNumber numberWithInt:real_value*400]
39 forProperty:NSSpeechRateProperty error:nil];
40 }
41 if (util::ReadNumberByKey(speak_options, util::kPitchKey, &real_value)) {
42 // The TTS api allows an approximate range of 30 to 65 for speech pitch.
43 [speech_synthesizer_
44 setObject: [NSNumber numberWithInt:(real_value*35 + 30)]
45 forProperty:NSSpeechPitchBaseProperty error:nil];
46 }
47 if (util::ReadNumberByKey(speak_options, util::kVolumeKey, &real_value)) {
48 // The TTS api allows a range of 0.0 to 1.0 for speech volume.
49 [speech_synthesizer_
50 setObject: [NSNumber numberWithFloat:real_value]
51 forProperty:NSSpeechVolumeProperty error:nil];
52 }
53 } 65 }
54 66
55 return 67 if (pitch >= 0.0) {
56 [speech_synthesizer_ startSpeakingString: 68 // The TTS api allows an approximate range of 30 to 65 for speech pitch.
57 [NSString stringWithUTF8String: utterance.c_str()]]; 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()]];
58 } 82 }
59 83
60 bool ExtensionTtsStopSpeakingFunction::RunImpl() { 84 bool ExtensionTtsPlatformImplMac::StopSpeaking() {
61 InitializeSpeechSynthesizer();
62 [speech_synthesizer_ stopSpeaking]; 85 [speech_synthesizer_ stopSpeaking];
63 return true; 86 return true;
64 } 87 }
65 88
66 bool ExtensionTtsIsSpeakingFunction::RunImpl() { 89 bool ExtensionTtsPlatformImplMac::IsSpeaking() {
67 InitializeSpeechSynthesizer();
68 return [speech_synthesizer_ isSpeaking]; 90 return [speech_synthesizer_ isSpeaking];
69 } 91 }
92
93 ExtensionTtsPlatformImplMac::ExtensionTtsPlatformImplMac() {
94 speech_synthesizer_ = [[NSSpeechSynthesizer alloc] init];
95 }
96
97 // static
98 ExtensionTtsPlatformImplMac* ExtensionTtsPlatformImplMac::GetInstance() {
99 return Singleton<ExtensionTtsPlatformImplMac>::get();
100 }
OLDNEW
« no previous file with comments | « chrome/browser/extensions/extension_tts_api_linux.cc ('k') | chrome/browser/extensions/extension_tts_api_win.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698