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