00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00042
00043
00044
00045
00046 #include "../tool/disable4786.h"
00047
00048 #include "SpeechSet.h"
00049 #include "SpeechCore.h"
00050
00051
00052 #ifndef OT_NO_SPEECH_SUPPORT
00053
00054
00055 namespace ot {
00056
00057 DWORD SpeechSetBase::GetId()
00058 {
00059 assert(m_SpeechCore);
00060
00061 return(m_Id);
00062 }
00063
00064
00065 const char* SpeechSetBase::GetName()
00066 {
00067 assert(m_SpeechCore);
00068
00069 return(m_Name.c_str());
00070 }
00071
00072
00073 bool SpeechSetBase::IsCommandRegistered(const char *p_Command)
00074 {
00075 for(unsigned int i = 0; i < m_RegisteredCommands.size(); ++i)
00076 {
00077 if(!strcmp(m_RegisteredCommands[i].m_Command.c_str(), p_Command))
00078 return(true);
00079 }
00080 return(false);
00081 }
00082
00083
00084 bool SpeechSetBase::IsCommandIdRegistered(DWORD p_CommandId)
00085 {
00086 for(unsigned int i = 0; i < m_RegisteredCommands.size(); ++i)
00087 {
00088 if(m_RegisteredCommands[i].m_CommandId == p_CommandId)
00089 return(true);
00090 }
00091 return(false);
00092 }
00093
00094
00095 long SpeechSetBase::GetCommandId(const char *p_Command)
00096 {
00097 for(unsigned int i = 0; i < m_RegisteredCommands.size(); ++i)
00098 {
00099 if(!strcmp(m_RegisteredCommands[i].m_Command.c_str(), p_Command))
00100 return(m_RegisteredCommands[i].m_CommandId);
00101 }
00102 return(-1);
00103 }
00104
00105
00106 bool SpeechSetBase::GetCommand(DWORD p_CommandId, std::string &p_Command)
00107 {
00108 for(unsigned int i = 0; i < m_RegisteredCommands.size(); ++i)
00109 {
00110 if(m_RegisteredCommands[i].m_CommandId == p_CommandId)
00111 {
00112 p_Command = m_RegisteredCommands[i].m_Command;
00113 return(true);
00114 }
00115 }
00116 p_Command = "";
00117 return(false);
00118 }
00119
00120
00121 void SpeechSetBase::AddCommand(const char *p_Command, DWORD p_CommandId, float p_Weight)
00122 {
00123 assert(m_SpeechCore);
00124
00125
00126 if(IsCommandRegistered(p_Command))
00127 return;
00128
00129 if((int)p_CommandId == -1)
00130 p_CommandId = m_RegisteredCommands.size() + 1;
00131
00132 SSpeechCommand command;
00133 command.m_CommandId = p_CommandId;
00134 command.m_Command = p_Command;
00135 command.m_Seperator = " ";
00136 command.m_Weight = p_Weight;
00137 m_RegisteredCommands.push_back(command);
00138 }
00139
00140
00141 void SpeechSetBase::RemoveCommand(const char *p_Command)
00142 {
00143 for(std::vector<SSpeechCommand>::iterator i = m_RegisteredCommands.begin(); i < m_RegisteredCommands.end(); ++i)
00144 {
00145 if(!strcmp(i->m_Command.c_str(), p_Command))
00146 {
00147 m_RegisteredCommands.erase(i);
00148 return;
00149 }
00150 }
00151 }
00152
00153
00154 void SpeechSetBase::RemoveCommand(DWORD p_CommandId)
00155 {
00156 for(std::vector<SSpeechCommand>::iterator i = m_RegisteredCommands.begin(); i < m_RegisteredCommands.end(); ++i)
00157 {
00158 if(i->m_CommandId == p_CommandId)
00159 {
00160 m_RegisteredCommands.erase(i);
00161 return;
00162 }
00163 }
00164 }
00165
00166 void SpeechSetBase::Activate()
00167 {
00168 if(IsActive())
00169 return;
00170 m_Active = true;
00171 }
00172
00173 void SpeechSetBase::Deactivate()
00174 {
00175 if(!IsActive())
00176 return;
00177 m_Active = false;
00178 }
00179
00180 bool SpeechSetBase::IsActive()
00181 {
00182 return(m_Active);
00183 }
00184
00185 #ifdef USE_SAPISPEECH
00186
00187
00188 CSpeechSet::CSpeechSet(const char *p_Name, DWORD p_Id, CSpeechCore *p_SpeechCore)
00189 : SpeechSetBase( p_Name, p_Id, p_SpeechCore)
00190 {
00191 Initialize();
00192 m_Name = p_Name;
00193 m_Id = p_Id;
00194 m_SpeechCore = p_SpeechCore;
00195 }
00196
00197 void CSpeechSet::Initialize()
00198 {
00199 m_SpeechCore = 0;
00200 m_Active = true;
00201 }
00202
00203
00204 void CSpeechSet::Destroy()
00205 {
00206 if(m_SpeechCore)
00207 {
00208 HRESULT hr = S_OK;
00209 SPSTATEHANDLE hEventTravel;
00210 std::wstring wName;
00211
00212 CSpeechCore::StrToWide(GetName(), wName);
00213
00214
00215 hr = m_SpeechCore->m_CmdGrammar->GetRule(&wName[0], m_Id, SPRAF_TopLevel | SPRAF_Active, TRUE, &hEventTravel);
00216 if(SUCCEEDED(hr))
00217 m_SpeechCore->m_CmdGrammar->ClearRule(hEventTravel);
00218 }
00219
00220 m_SpeechCore = 0;
00221 m_Active = false;
00222 }
00223
00224
00225 DWORD CSpeechSet::GetId()
00226 {
00227 assert(m_SpeechCore);
00228
00229 return(m_Id);
00230 }
00231
00232
00233 const char* CSpeechSet::GetName()
00234 {
00235 assert(m_SpeechCore);
00236
00237 return(m_Name.c_str());
00238 }
00239
00240
00241 bool CSpeechSet::IsCommandRegistered(const char *p_Command)
00242 {
00243 for(unsigned int i = 0; i < m_RegisteredCommands.size(); ++i)
00244 {
00245 if(!strcmp(m_RegisteredCommands[i].m_Command.c_str(), p_Command))
00246 return(true);
00247 }
00248 return(false);
00249 }
00250
00251
00252 bool CSpeechSet::IsCommandIdRegistered(DWORD p_CommandId)
00253 {
00254 for(unsigned int i = 0; i < m_RegisteredCommands.size(); ++i)
00255 {
00256 if(m_RegisteredCommands[i].m_CommandId == p_CommandId)
00257 return(true);
00258 }
00259 return(false);
00260 }
00261
00262
00263 long CSpeechSet::GetCommandId(const char *p_Command)
00264 {
00265 for(unsigned int i = 0; i < m_RegisteredCommands.size(); ++i)
00266 {
00267 if(!strcmp(m_RegisteredCommands[i].m_Command.c_str(), p_Command))
00268 return(m_RegisteredCommands[i].m_CommandId);
00269 }
00270 return(-1);
00271 }
00272
00273
00274 bool CSpeechSet::GetCommand(DWORD p_CommandId, std::string &p_Command)
00275 {
00276 for(unsigned int i = 0; i < m_RegisteredCommands.size(); ++i)
00277 {
00278 if(m_RegisteredCommands[i].m_CommandId == p_CommandId)
00279 {
00280 p_Command = m_RegisteredCommands[i].m_Command;
00281 return(true);
00282 }
00283 }
00284 p_Command = "";
00285 return(false);
00286 }
00287
00288
00289 void CSpeechSet::AddCommand(const char *p_Command, DWORD p_CommandId, float p_Weight)
00290 {
00291 assert(m_SpeechCore);
00292
00293 HRESULT hr = S_OK;
00294 SPSTATEHANDLE hEventTravel;
00295 std::wstring wName, wCommand, wSeperator;
00296
00297
00298 if(IsCommandRegistered(p_Command))
00299 return;
00300
00301 CSpeechCore::StrToWide(GetName(), wName);
00302 CSpeechCore::StrToWide(p_Command, wCommand);
00303 CSpeechCore::StrToWide(" ", wSeperator);
00304
00305
00306 hr = m_SpeechCore->m_CmdGrammar->GetRule(&wName[0], m_Id, SPRAF_TopLevel | SPRAF_Active, TRUE, &hEventTravel);
00307 if(FAILED(hr))
00308 {
00309 Destroy();
00310 throw CSpeechException("Unable to get Rule");
00311 }
00312
00313
00314 hr = m_SpeechCore->m_CmdGrammar->AddWordTransition(hEventTravel, NULL, &wCommand[0], &wSeperator[0], SPWT_LEXICAL, p_Weight, NULL);
00315 if(FAILED(hr))
00316 {
00317 Destroy();
00318 throw CSpeechException("Unable to add Word");
00319 }
00320
00321
00322 hr = m_SpeechCore->m_CmdGrammar->Commit(0);
00323 if (FAILED(hr))
00324 {
00325 Destroy();
00326 throw CSpeechException("Unable to Commit");
00327 }
00328
00329
00330 hr = m_SpeechCore->m_CmdGrammar->SetRuleState(&wName[0], NULL, SPRS_ACTIVE);
00331 if (FAILED(hr))
00332 {
00333 Destroy();
00334 throw CSpeechException("Unable to activate rule");
00335 }
00336
00337 if(p_CommandId == -1)
00338 p_CommandId = m_RegisteredCommands.size() + 1;
00339
00340 SSpeechCommand command;
00341 command.m_CommandId = p_CommandId;
00342 command.m_Command = p_Command;
00343 command.m_Seperator = " ";
00344 command.m_Weight = p_Weight;
00345 m_RegisteredCommands.push_back(command);
00346 }
00347
00348
00349 void CSpeechSet::RemoveCommand(const char *p_Command)
00350 {
00351 for(std::vector<SSpeechCommand>::iterator i = m_RegisteredCommands.begin(); i < m_RegisteredCommands.end(); ++i)
00352 {
00353 if(!strcmp(i->m_Command.c_str(), p_Command))
00354 {
00355 m_RegisteredCommands.erase(i);
00356 RebuildRule();
00357 return;
00358 }
00359 }
00360 }
00361
00362
00363 void CSpeechSet::RemoveCommand(DWORD p_CommandId)
00364 {
00365 for(std::vector<SSpeechCommand>::iterator i = m_RegisteredCommands.begin(); i < m_RegisteredCommands.end(); ++i)
00366 {
00367 if(i->m_CommandId == p_CommandId)
00368 {
00369 m_RegisteredCommands.erase(i);
00370 RebuildRule();
00371 return;
00372 }
00373 }
00374 }
00375
00376
00377 void CSpeechSet::RebuildRule()
00378 {
00379 HRESULT hr = S_OK;
00380 SPSTATEHANDLE hEventTravel;
00381 std::wstring wName, wCommand, wSeperator;
00382
00383 CSpeechCore::StrToWide(GetName(), wName);
00384
00385
00386 hr = m_SpeechCore->m_CmdGrammar->GetRule(&wName[0], m_Id, SPRAF_TopLevel | SPRAF_Active, TRUE, &hEventTravel);
00387 if(FAILED(hr))
00388 {
00389 Destroy();
00390 throw CSpeechException("Unable to get Rule");
00391 }
00392
00393
00394 hr = m_SpeechCore->m_CmdGrammar->ClearRule(hEventTravel);
00395 if(FAILED(hr))
00396 {
00397 Destroy();
00398 throw CSpeechException("Unable to clear Rule");
00399 }
00400
00401 for(unsigned int i = 0; i < m_RegisteredCommands.size(); ++i)
00402 {
00403 CSpeechCore::StrToWide(m_RegisteredCommands[i].m_Command.c_str(), wCommand);
00404 CSpeechCore::StrToWide(m_RegisteredCommands[i].m_Seperator.c_str(), wSeperator);
00405
00406
00407 hr = m_SpeechCore->m_CmdGrammar->AddWordTransition(hEventTravel, NULL, &wCommand[0], &wSeperator[0], SPWT_LEXICAL, m_RegisteredCommands[i].m_Weight, NULL);
00408 if(FAILED(hr))
00409 {
00410 Destroy();
00411 throw CSpeechException("Unable to add Word");
00412 }
00413 }
00414
00415
00416 hr = m_SpeechCore->m_CmdGrammar->Commit(0);
00417 if (FAILED(hr))
00418 {
00419 Destroy();
00420 throw CSpeechException("Unable to Commit");
00421 }
00422
00423 if(IsActive())
00424 Activate();
00425 else
00426 Deactivate();
00427 }
00428
00429
00430 void CSpeechSet::Recognize(const char *p_String)
00431 {
00432 m_RecogizedCommands.push(p_String);
00433 }
00434
00435
00436 bool CSpeechSet::IsReco()
00437 {
00438 assert(m_SpeechCore);
00439 return(m_RecogizedCommands.size() > 0);
00440 }
00441
00442
00443 bool CSpeechSet::GetReco(std::string &p_Result)
00444 {
00445 assert(m_SpeechCore);
00446
00447 if(m_RecogizedCommands.size() > 0)
00448 {
00449 p_Result = m_RecogizedCommands.front();
00450 m_RecogizedCommands.pop();
00451 return(true);
00452 }
00453
00454 return(false);
00455 }
00456
00457
00458 void CSpeechSet::Activate()
00459 {
00460 if(IsActive())
00461 return;
00462
00463 HRESULT hr = S_OK;
00464 std::wstring wName;
00465 CSpeechCore::StrToWide(GetName(), wName);
00466
00467
00468 hr = m_SpeechCore->m_CmdGrammar->SetRuleState(&wName[0], NULL, SPRS_ACTIVE);
00469 if (FAILED(hr))
00470 {
00471 Destroy();
00472 throw CSpeechException("Unable to activate rule");
00473 }
00474
00475 m_Active = true;
00476 }
00477
00478
00479 void CSpeechSet::Deactivate()
00480 {
00481 if(!IsActive())
00482 return;
00483
00484 HRESULT hr = S_OK;
00485 std::wstring wName;
00486 CSpeechCore::StrToWide(GetName(), wName);
00487
00488
00489 hr = m_SpeechCore->m_CmdGrammar->SetRuleState(&wName[0], NULL, SPRS_INACTIVE);
00490 if (FAILED(hr))
00491 {
00492 Destroy();
00493 throw CSpeechException("Unable to deactivate rule");
00494 }
00495
00496 m_Active = false;
00497 }
00498
00499
00500 bool CSpeechSet::IsActive()
00501 {
00502 return(m_Active);
00503 }
00504
00505
00506 #endif //ifdef USE_SAPISPEECH
00507
00508
00509 }
00510
00511
00512 #else
00513 #pragma message(">>> OT_NO_SPEECH_SUPPORT")
00514 #endif // OT_NO_SPEECH_SUPPORT
00515
00516
00517
00518
00519
00520
00521
00522
00523
00524
00525
00526
00527
00528
00529
00530