OpenTracker

An Open Architecture for Reconfigurable Tracking based on XML | Contact

SpeechModule.cxx

Go to the documentation of this file.
00001 /* ========================================================================
00002  * Copyright (c) 2006,
00003  * Institute for Computer Graphics and Vision
00004  * Graz University of Technology
00005  * All rights reserved.
00006  *
00007  * Redistribution and use in source and binary forms, with or without
00008  * modification, are permitted provided that the following conditions are
00009  * met:
00010  *
00011  * Redistributions of source code must retain the above copyright notice,
00012  * this list of conditions and the following disclaimer.
00013  *
00014  * Redistributions in binary form must reproduce the above copyright
00015  * notice, this list of conditions and the following disclaimer in the
00016  * documentation and/or other materials provided with the distribution.
00017  *
00018  * Neither the name of the Graz University of Technology nor the names of
00019  * its contributors may be used to endorse or promote products derived from
00020  * this software without specific prior written permission.
00021  *
00022  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
00023  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
00024  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
00025  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
00026  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00027  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00028  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00029  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00030  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00031  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00032  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00033  * ========================================================================
00034  * PROJECT: OpenTracker
00035  * ======================================================================== */
00042 /* ======================================================================= */
00043 
00044 
00045 // this will remove the warning 4786
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     // Destructor methode
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     // This methode is called to construct a new Node.
00082     Node* SpeechModule::createNode(const std::string& name, StringTable& attributes)
00083     {
00084         if(name.compare("SpeechRecoSource") == 0)
00085         {
00086             assert(initialized == 1);   // must be initialized
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             // Search for the speechset in the config tree, and register the appropriate commands
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         // store the config node tree for later use
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         // create the sr core component
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         // get the standard system voice
00166         m_Voice = new SpeechVoiceModule;
00167         assert(m_Voice);
00168 
00169 
00170         //m_Voice->Speak("The SR system is initialized!");
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     // pushes events into the tracker tree.
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                 //printf("SR: SpeechModule::pushEvent\n");
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         // try to get the speech set via the SpeechSetId
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         // retrieve the command via the CommandId from the speechset
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 } // namespace ot
00245 
00246 
00247 #else
00248 #pragma message(">>> OT_NO_SPEECH_SUPPORT")
00249 #endif // OT_NO_SPEECH_SUPPORT
00250 
00251 /* 
00252  * ------------------------------------------------------------
00253  *   End of SpeechModule.cxx
00254  * ------------------------------------------------------------
00255  *   Automatic Emacs configuration follows.
00256  *   Local Variables:
00257  *   mode:c++
00258  *   c-basic-offset: 4
00259  *   eval: (c-set-offset 'substatement-open 0)
00260  *   eval: (c-set-offset 'case-label '+)
00261  *   eval: (c-set-offset 'statement 'c-lineup-runin-statements)
00262  *   eval: (setq indent-tabs-mode nil)
00263  *   End:
00264  * ------------------------------------------------------------ 
00265  */

copyright (c) 2006 Graz University of Technology