Debian Codegraph @

code/apis/SpeechManager.cpp

raw
1 /*
2 Copyright (C) 2009-2010 wxLauncher Team
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 */
18
19 #include <wx/wx.h>
20 #include "generated/configure_launcher.h"
21 #if USE_SPEECH
22 #include <sapi.h>
23 #endif
24
25 #include <vector>
26
27 #include "apis/SpeechManager.h"
28
29 #include "global/MemoryDebugging.h"
30
31 using namespace SpeechMan;
32
33 #if USE_SPEECH
34 // wxLauncher objects
35 bool isInitialized = false;
36
37 class VoiceData {
38 public:
39 VoiceData(const wxString& voiceName, ISpObjectToken* voice);
40 VoiceData::VoiceData(const VoiceData& vd);
41 VoiceData& VoiceData::operator=(const VoiceData& vd);
42 const wxString& GetVoiceName() const { return this->voiceName; }
43 ISpObjectToken* GetVoice() const { return this->voice; }
44 private:
45 VoiceData();
46 wxString voiceName;
47 ISpObjectToken* voice;
48 };
49
50 VoiceData::VoiceData(const wxString& voiceName, ISpObjectToken* voice)
51 : voiceName(voiceName), voice(voice) {
52 }
53
54 VoiceData::VoiceData(const VoiceData& vd)
55 : voiceName(vd.voiceName), voice(vd.voice) {
56 }
57
58 VoiceData& VoiceData::operator=(const VoiceData& vd) {
59 if (this == &vd) {
60 return *this;
61 }
62
63 this->voiceName = vd.voiceName;
64 this->voice = vd.voice;
65 return *this;
66 }
67
68 std::vector<VoiceData> voices;
69
70 // COM objects
71 ISpVoice * comVoice = NULL;
72
73 void enumerateObjectToken(ISpObjectToken * token) {
74 HRESULT valuesres = S_OK;
75 HRESULT datares = S_OK;
76 wxLogDebug(wxT_2("Enumerating token:"));
77 ULONG valueIndex = 0;
78 LPWSTR value = NULL;
79 LPWSTR valuedata = NULL;
80 do {
81 valuesres = token->EnumValues(valueIndex, &value);
82 if ( valuesres == S_OK ) {
83 datares = token->GetStringValue(value, &valuedata);
84 if ( datares == S_OK ) {
85 wxLogDebug(wxT_2(" %s=%s"), wxString(value, wxMBConvUTF16()).c_str(),
86 wxString(valuedata, wxMBConvUTF16()).c_str());
87 CoTaskMemFree(valuedata);
88 valuedata = NULL;
89 } else {
90 wxLogDebug(wxT_2(" %s= (NONE)"), wxString(value, wxMBConvUTF16()).c_str());
91 }
92 CoTaskMemFree(value);
93 value = NULL;
94 }
95 valueIndex++;
96 } while( SUCCEEDED(valuesres) );
97 }
98
99 bool EnumerateVoices() {
100 HRESULT comResult = S_OK;
101 ISpObjectTokenCategory * comTokenCategory = NULL;
102 IEnumSpObjectTokens * comVoices = NULL;
103 ULONG comVoicesCount = 0;
104
105 // Init speech api
106 comResult = ::CoCreateInstance(
107 CLSID_SpVoice, NULL, CLSCTX_INPROC_SERVER, IID_ISpVoice, (LPVOID*)&comVoice);
108 wxCHECK_MSG( SUCCEEDED(comResult), false, _T("Unable to instantiate speech API"));
109
110 // Generate enumeration of voices
111 comResult = ::CoCreateInstance(CLSID_SpObjectTokenCategory, NULL,
112 CLSCTX_INPROC_SERVER, IID_ISpObjectTokenCategory, (LPVOID*)&comTokenCategory);
113 wxCHECK_MSG( SUCCEEDED(comResult), false, _T("Unable to instantiate a TokenCategory"));
114
115 comResult = comTokenCategory->SetId(SPCAT_VOICES, false);
116 wxCHECK_MSG( SUCCEEDED(comResult), false,
117 _T("Unable to to set location to find the installed voices.")
118 _T("Likely the key that I am looking for does not exist and thus ")
119 _T("there are no installed voices on this system."));
120
121 comResult = comTokenCategory->EnumTokens(NULL, NULL, &comVoices);
122 wxCHECK_MSG( SUCCEEDED(comResult), false,
123 _T("Unable to enumerate the installed voices. Check that this system")
124 _T(" has voices installed."));
125
126 comResult = comVoices->GetCount(&comVoicesCount);
127 wxCHECK_MSG( SUCCEEDED(comResult), false,
128 _T("Unable get a count of the installed voices."));
129
130 while( comVoicesCount > 0 ) {
131 ISpObjectToken * comAVoice = NULL;
132 comVoices->Next(1, &comAVoice, NULL); // retrieve just one
133 LPWSTR id = NULL;
134 comAVoice->GetStringValue(NULL, &id);
135 size_t idlength = wcslen(id);
136 wxLogDebug(_T(" Got string of length %ld:"), idlength);
137 for(size_t i = 0; i < idlength; i++) {
138 wxLogDebug(_T(" %04X"), id[i]);
139 }
140 voices.push_back(VoiceData(wxString(id, wxMBConvUTF16(), wcslen(id)), comAVoice));
141
142 #ifdef __WXDEBUG__
143 enumerateObjectToken(comAVoice);
144 #endif
145 comAVoice->Release();
146 comVoicesCount--;
147 }
148
149 comTokenCategory->Release();
150
151 return true;
152 }
153
154 #endif // USE_SPEECH
155
156 bool SpeechMan::Initialize() {
157 #if USE_SPEECH
158 ::CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
159
160 wxCHECK_MSG(EnumerateVoices(), false, _T("Cannot enumerate voices"));
161
162 isInitialized = true;
163 return true;
164 #else
165 return false;
166 #endif
167 }
168
169 bool SpeechMan::DeInitialize() {
170 #if USE_SPEECH
171 isInitialized = false;
172 ::CoUninitialize();
173 return true;
174 #else
175 return false;
176 #endif
177 }
178 /** Returns true if Speech Support was compiled in. Returns false if speech
179 support was not compiled in. */
180 bool SpeechMan::WasBuiltIn() {
181 #if USE_SPEECH
182 return true;
183 #else
184 return false;
185 #endif
186 }
187
188 bool SpeechMan::IsInitialized() {
189 #if USE_SPEECH
190 return isInitialized;
191 #else
192 return false;
193 #endif
194 }
195
196 wxArrayString SpeechMan::EnumVoices() {
197 wxArrayString arr;
198 #if USE_SPEECH
199 for (std::vector<VoiceData>::const_iterator it = voices.begin();
200 it != voices.end(); ++it) {
201 arr.Add(it->GetVoiceName());
202 }
203 #endif
204 return arr; // reference counted copy
205 }
206
207 void SpeechMan::Speak(wxString what) {
208 #if USE_SPEECH
209 wxCHECK_RET( isInitialized, _T("Speak called but SpeechMan not initialized."));
210 wxCHECK_RET( comVoice != NULL, _T("Speak called but comVoice is null"));
211 wxWritableWCharBuffer buffer(what.wchar_str());
212 LPCWSTR str = buffer.operator const wchar_t *();
213
214 HRESULT result = comVoice->Speak(str, SPF_IS_NOT_XML, NULL);
215 wxCHECK_RET( SUCCEEDED(result), _T("Failed to speak"));
216 #else
217 wxFAIL_MSG(_T("Speak called but USE_SPEECH not defined"));
218 #endif
219 }
220
221 #if USE_SPEECH
222 void SpeechMan::SetVoice(size_t i) {
223 wxCHECK_RET( isInitialized, _T("SetVoice called but SpeechMan not initialized."));
224 wxCHECK_RET( comVoice != NULL, _T("SetVoice called but comVoice is null"));
225 wxCHECK_RET( i < voices.size(), _T("i is larger than the number of voices"));
226
227 wxLogDebug(_T("Selected voice: %s"), voices[i].GetVoiceName().c_str());
228
229 HRESULT result = comVoice->SetVoice(voices[i].GetVoice());
230 wxCHECK_RET( SUCCEEDED(result), _T("Error in setting voice"));
231 #else
232 void SpeechMan::SetVoice(size_t) {
233 wxFAIL_MSG(_T("SetVoice called but USE_SPEECH not defined"));
234 #endif
235 }
236
237 /** Returns the currently set voice. Useful mostly get get the system default
238 voice if there is no value for the voice set by the profile, or the FS2_open
239 registry entries. */
240 int SpeechMan::GetVoice() {
241 #if USE_SPEECH
242 wxCHECK_MSG( isInitialized, -1, _T("GetVoice called but SpeechMan not initialized."));
243 wxCHECK_MSG( comVoice != NULL, -1, _T("GetVoice called but comVoice is null"));
244 ISpObjectToken * voice = NULL;
245
246 HRESULT result = comVoice->GetVoice(&voice);
247 wxCHECK_MSG( SUCCEEDED(result), -1, _T("Error in getting voice"));
248
249 LPWSTR name = NULL;
250 result = voice->GetStringValue(NULL, &name);
251 wxCHECK_MSG( SUCCEEDED(result), -1, _T("Error in getting voice name"));
252
253 wxString voiceName(name, wxMBConvUTF16());
254 int index = EnumVoices().Index(voiceName.c_str());
255 wxCHECK_MSG( index != wxNOT_FOUND, -1,
256 wxString::Format(_T("Could not find voice (%s) in database"), voiceName));
257
258 return index;
259 #else
260 wxFAIL_MSG(_T("GetVoice called but USE_SPEECH not defined"));
261 return -1;
262 #endif
263 }
264
265 int SpeechMan::GetVolume() {
266 #if USE_SPEECH
267 wxCHECK_MSG( isInitialized, -1, _T("GetVolume called but SpeechMan not initialized."));
268 wxCHECK_MSG( comVoice != NULL, -1, _T("GetVolume called but comVoice is null"));
269
270 USHORT volume = 100;
271 HRESULT result = comVoice->GetVolume(&volume);
272 wxCHECK_MSG( SUCCEEDED(result), -1, _T("Unable to retrieve volume"));
273
274 return volume;
275 #else
276 wxFAIL_MSG(_T("GetVolume called but USE_SPEECH not defined"));
277 return 0;
278 #endif
279 }
280
281 #if USE_SPEECH
282 void SpeechMan::SetVolume(int volume) {
283 wxCHECK_RET( isInitialized, _T("SetVolume called but SpeechMan not initialized."));
284 wxCHECK_RET( comVoice != NULL, _T("SetVolume called but comVoice is null"));
285 wxCHECK_RET( volume >= 0, _T("Volume is less than zero"));
286 wxCHECK_RET( volume <= 100, _T("Volume is greater than 100"));
287
288 /* volume must be within 0-100 which is well within USHORT's range.*/
289 USHORT v = static_cast<USHORT>(volume);
290 HRESULT result = comVoice->SetVolume(v);
291 wxCHECK_RET( SUCCEEDED(result), _T("Unable to set volume"));
292 #else
293 void SpeechMan::SetVolume(int) {
294 wxFAIL_MSG(_T("SetVolume called but USE_SPEECH not defined"));
295 #endif
296 }
297
298