SpeechModule.cxx
Go to the documentation of this file.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 <stdio.h>
00049 #include <iostream>
00050
00051 #include "SpeechModule.h"
00052 #include "SpeechSource.h"
00053 #include "SpeechSet.h"
00054 #include "SpeechCore.h"
00055
00056
00057 #ifndef OT_NO_SPEECH_SUPPORT
00058
00059
00060 namespace ot {
00061
00062
00063 SpeechModule::~SpeechModule()
00064 {
00065 m_Nodes.clear();
00066
00067 if(m_Voice)
00068 {
00069 delete(m_Voice);
00070 m_Voice = 0;
00071 }
00072 if(m_SpeechCore)
00073 {
00074 delete(m_SpeechCore);
00075 m_SpeechCore = 0;
00076 }
00077 }
00078
00079
00080
00081
00082 Node* SpeechModule::createNode(const std::string& name, StringTable& attributes)
00083 {
00084 if(name.compare("SpeechRecoSource") == 0)
00085 {
00086 assert(initialized == 1);
00087
00088 std::string SpeechSetName, SpeechSetId;
00089 SpeechSetId = attributes.get("set");
00090 SpeechSetName = attributes.get("name");
00091
00092 printf("SR: Create SpeechSource, SpeechSetName = '%s', SpeechSetId = '%s'\n", SpeechSetName.c_str(), SpeechSetId.c_str());
00093
00094 SpeechSetBase *speechset = m_SpeechCore->GetSpeechSet(SpeechSetName.c_str());
00095 assert(speechset);
00096
00097 SpeechSource *source = new SpeechSource(this, speechset);
00098 assert(source);
00099
00100
00101
00102 for(unsigned int i = 0; i < m_ConfigTree->countChildren(); ++i)
00103 {
00104 ConfigNode *SpeechSetNode = (ConfigNode*)m_ConfigTree->getChild(i);
00105 std::string SpeechSetId2;
00106 SpeechSetId2 = SpeechSetNode->getAttributes().get("id");
00107
00108 if(!SpeechSetId.compare(SpeechSetId2))
00109 {
00110 for(unsigned int j = 0; j < SpeechSetNode->countChildren(); ++j)
00111 {
00112 ConfigNode *CommandNode = (ConfigNode*)SpeechSetNode->getChild(j);
00113 int CommandId;
00114 std::string CommandName;
00115 CommandNode->getAttributes().get("id", &CommandId);
00116 CommandName = CommandNode->getAttributes().get("name");
00117 float CommandWeight = 1.0f;
00118 if(CommandNode->getAttributes().get("weight").size() > 0)
00119 CommandWeight = (float)atof(CommandNode->getAttributes().get("weight").c_str());
00120
00121 printf("SR: Register CommandId = %i, Command = '%s', Weight = %.2f\n", CommandId, CommandName.c_str(), CommandWeight);
00122 speechset->AddCommand(CommandName.c_str(), CommandId, CommandWeight);
00123 }
00124 }
00125 }
00126
00127 m_Nodes.push_back(source);
00128 return(source);
00129 }
00130 return(NULL);
00131 }
00132
00133
00134 void SpeechModule::init(StringTable& attributes, ConfigNode *localTree)
00135 {
00136 Module::init( attributes, localTree );
00137
00138
00139 m_ConfigTree = localTree;
00140
00141 std::string Language;
00142 Language = localTree->getAttributes().get("language");
00143
00144 printf("SR: Language = '%s'\n", Language.c_str());
00145
00146
00147
00148 #ifdef USE_SAPISPEECH
00149 m_SpeechCore = new CSpeechCore;
00150 #else
00151 m_SpeechCore = new SpeechCoreBase;
00152 #endif
00153 assert(m_SpeechCore);
00154
00155 if(!Language.compare("english"))
00156 {
00157 m_SpeechCore->Init();
00158 }
00159 else
00160 {
00161 printf("SR: ERROR: Invalid Language: '%s'\n", Language.c_str());
00162 m_SpeechCore->Init();
00163 }
00164
00165
00166 m_Voice = new SpeechVoiceModule;
00167 assert(m_Voice);
00168
00169
00170
00171 }
00172
00173
00174 void SpeechModule::close()
00175 {
00176 if(m_Voice)
00177 {
00178 delete(m_Voice);
00179 m_Voice = 0;
00180 }
00181 if(m_SpeechCore)
00182 {
00183 delete(m_SpeechCore);
00184 m_SpeechCore = 0;
00185 }
00186 }
00187
00188
00189
00190 void SpeechModule::pushEvent()
00191 {
00192 if(m_SpeechCore)
00193 {
00194 assert(m_SpeechCore);
00195 assert(m_Voice);
00196
00197 if(m_Push2Nodes.size() > 0)
00198 {
00199 for(unsigned int i = 0; i < m_Push2Nodes.size(); ++i)
00200 {
00201 SpeechSource *source = (SpeechSource*)m_Push2Nodes[i];
00202 source->push2();
00203 }
00204 m_Push2Nodes.clear();
00205 }
00206 else if(m_SpeechCore->ProcessRecognitionPoll())
00207 {
00208
00209
00210 for(NodeVector::iterator it = m_Nodes.begin(); it != m_Nodes.end(); it++)
00211 {
00212 SpeechSource *source = (SpeechSource*)*it;
00213 if(source->push())
00214 {
00215 m_Push2Nodes.push_back(source);
00216 }
00217 }
00218 }
00219 }
00220 }
00221
00222
00223 bool SpeechModule::GetCommand(DWORD p_CommandId, DWORD p_SpeechSetId, std::string &p_Command)
00224 {
00225
00226 SpeechSetBase *set = m_SpeechCore->GetSpeechSet(p_SpeechSetId);
00227 if(!set)
00228 {
00229 printf("SR: ERROR: Invalid SpeechSet Id\n");
00230 return(false);
00231 }
00232
00233
00234 return(set->GetCommand(p_CommandId, p_Command));
00235 }
00236
00237
00238 void SpeechModule::Speak(const char *p_Sentence, bool p_Async)
00239 {
00240 assert(m_Voice);
00241 m_Voice->Speak(p_Sentence, p_Async);
00242 }
00243
00244 }
00245
00246
00247 #else
00248 #pragma message(">>> OT_NO_SPEECH_SUPPORT")
00249 #endif // OT_NO_SPEECH_SUPPORT
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265