一个Nuance 的语音识别的例子

  1. #include "stdafx.h"    
  2. #include "GetToken.h"    
  3.    
  4.    
  5. #ifdef _DEBUG    
  6. #define new DEBUG_NEW    
  7. #undef THIS_FILE    
  8. static char THIS_FILE[] = __FILE__;   
  9. #endif    
  10.    
  11. /////////////////////////////////////////////////////////////////////////////    
  12. // The one and only application object    
  13.    
  14. //CWinApp theApp;    
  15. //using namespace std;    
  16.    
  17. #ifdef __UNICODE__    
  18. #undef __UNICODE__    
  19. #endif    
  20.    
  21. //#define __UNICODE__    
  22.    
  23. #ifdef __UNICODE__    
  24. #define dictName "dict.uni"    
  25. #define alpha "freepy.uni"    
  26.    
  27. #else//gb2312    
  28. #define dictName "dict.gb"    
  29. #define alpha "freepy.gb"    
  30. #endif    
  31.    
  32. const int  maxlength = 256;   
  33. #define dictSize 0x10000    
  34. const char pyIndex[]  = "pyindex";   
  35.    
  36.    
  37. BOOL ISHanzi(char *src)   
  38. {   
  39.     int asc=*src;   
  40.     if(asc<0)  asc+=256;   
  41.     if(asc>127)    
  42.         return TRUE;   
  43.     return FALSE;      
  44. }   
  45.    
  46. char * GetPhoneticByWideString(WCHAR * str,const WORD * dict,const char * pyTable)   
  47. {   
  48.     char phonetic[10];   
  49.     char * widestr = (char*)str;   
  50.     char *returnvalue=NULL;   
  51.     BOOL hz;   
  52.     hz=FALSE;   
  53.     while (*widestr != 0)   
  54.     {   
  55.         memset(phonetic,0,10);   
  56.         bool bEnglish = true;   
  57.         if(*widestr <= 'Z' && *widestr >='A')   
  58.         {   
  59.             if(hz)   
  60.             {   
  61.                 *phonetic=' ';     
  62.                 *(phonetic+1) = *widestr + 'a' - 'A';   
  63.                 *(phonetic+2) = NULL;   
  64.             }else   
  65.             {   
  66.                 *phonetic = *widestr + 'a' - 'A';   
  67.                 *(phonetic+1) = NULL;   
  68.             }   
  69.             hz=FALSE;   
  70.             widestr ++;   
  71.         }   
  72.         else if(*widestr <= 'z' && *widestr >='a' || *widestr == ' ')   
  73.         {   
  74.             if(hz&&*widestr != ' ')   
  75.             {   
  76.                 *(phonetic)=' ';   
  77.                 *(phonetic+1) = *widestr;   
  78.                 *(phonetic+2) = NULL;   
  79.             }   
  80.             else   
  81.             {   
  82.                 *phonetic = *widestr;   
  83.                 *(phonetic+1) = NULL;   
  84.             }   
  85.             hz=FALSE;   
  86.             widestr ++;   
  87.         }   
  88.         else if(*widestr >= '0' && *widestr <='9')   
  89.         {   
  90. /*          *phonetic = *widestr;  
  91.             *(phonetic+1) = NULL;         
  92.             hz=FALSE;  
  93.             widestr ++;  
  94. */   
  95.    
  96.             GetDigits(*widestr,phonetic);   
  97.             hz=TRUE;   
  98.             bEnglish = false;   
  99.             widestr++;   
  100.         }   
  101.    
  102.         else if(ISHanzi(widestr))   
  103.         {   
  104.             if (-1 == GetPhoneticByHanZi(*(WCHAR*)widestr,dict,pyTable,phonetic))   
  105.                 return NULL;   
  106.             widestr ++;   
  107.             if(!((*widestr <= 'Z' && *widestr >='A') || (*widestr <= 'a' && *widestr >='a') || *widestr == ' '))   
  108.                 widestr ++;   
  109.             hz=TRUE;   
  110.             bEnglish = false;   
  111.         }else   
  112.         {   
  113.             widestr ++;   
  114.             continue;   
  115.         }   
  116.         if (!returnvalue)   
  117.         {   
  118.             int i = strlen(phonetic);   
  119.             returnvalue = strdup(phonetic);   
  120.         }   
  121.         else   
  122.         {   
  123.             if(!bEnglish && returnvalue[strlen(returnvalue)-1] != ' ')   
  124.             {   
  125.                 returnvalue  = (char*)realloc(returnvalue,strlen(returnvalue)+strlen(phonetic)+2);   
  126.                 strcat(returnvalue," ");   
  127.             }   
  128.             else   
  129.             {   
  130.                 returnvalue  = (char*)realloc(returnvalue,strlen(returnvalue)+strlen(phonetic)+1);   
  131.             }   
  132.             strcat(returnvalue,phonetic);   
  133.         }   
  134.            
  135.     }   
  136.     return returnvalue;   
  137. }   
  138.    
  139. void LoadDictAndPYTable(WORD* pDict,char** pPyTable)   
  140. {   
  141.     CFile dictFile(dictName,CFile::modeRead|CFile::typeBinary);   
  142.     //for (int i = 0;i<DICTSIZE;I++){ (a if a="wch" (m<lstr){ while tabfile='fopen("table.txt","r+b");' tabfile; * FILE destfile='fopen("big.txt","w+b");' destfile; BigSource="Big5Source;" char* GBString="outString;" 输出字符串 char[lstr]; outString="new" lstr="bigStrlen+1;" m="0;" long lstr; b; char unsigned a; Big5Source="new" else } NULL); NULL, bigStrlen, Big5Source, -1, WidecharStr, 0, WideCharToMultiByte(936, MultiByteToWideChar(950,0,SourceText,bigStrlen,WidecharStr,bigStrlen); { (strstr(SourceText,?big5?)!="NULL)" bigStrlen+1); memset(Big5Source, (bigStrlen+1)*sizeof(WCHAR)); memset(WidecharStr, String WideChar WCHAR[bigStrlen+1]; WidecharStr="new" WCHAR Big5字符串 char[bigStrlen+1]; LPSTR 源字符串长度 bigStrlen="strlen(SourceText);" *SourceText) Big2GB(char ?);} strcpy(returnvalue,?ling2 (digits="='1')" strcpy(returnvalue,?jiu3 strcpy(returnvalue,?ba1 strcpy(returnvalue,?qi1 strcpy(returnvalue,?liu4 strcpy(returnvalue,?wu3 strcpy(returnvalue,?si4 strcpy(returnvalue,?san1 strcpy(returnvalue,?er4 strcpy(returnvalue,?yi1 returnvalue) digits,char* GetDigits(char void count; return count++; *(lpStrArr+count*wMaxArrSize+i-cursor+1)="_T('/0');" _tcsncpy(lpStrArr+count*wMaxArrSize,lpBuf+cursor,i-cursor+1); ){ ?) *(lpBuf+i)!="_T('" && wBufLen-1 if(i="=" cursor="0,count=0,wBufLen;" *(lpStrArr+count*wMaxArrSize+i-cursor)="_T('/0');" _tcsncpy(lpStrArr+count*wMaxArrSize,lpBuf+cursor,i-cursor); *(lpBuf+i-1)!="_T('" if(i!="0" _T(?/t?)) *(lpBuf+i)="=" || _T(? if(*(lpBuf+i)="=" (i="0;i<wBufLen;i++){" for wBufLen="strlen(lpBuf);" WORD i; int wMaxArrSize) lpStrArr,WORD lpBuf,LPTSTR String2Array(LPTSTR 1; phonetic[len+1]="0;" phonetic[len]="shengdiao+0x30;" len="strlen(phonetic);" strcpy(phonetic,pyTable+index-shengdiao); 8; % shengdiao="index" -1; (!index) ); !="NULL" pyTable ASSERT(phonetic!="NULL" phonetic) *pyTable,char* index,const GetPYByIndex(int 返回只为* *根据索引值获得汉字的发音,为上函数的逆函数 find/n?); printf(?not break; 415) (count++="=" +="8;" index="(char*)" while(strcmp(index,phonetic)!="0)" count="0;" pyTable; pyTable) void* phonetic,const GetIndexByPY(char* 顺序查找,可以修改为这般查找* +音调 返回为索引植="音标序号*8" pyTable为存储音标的表, *phonetic为要查的音标,如ang, GetPYByIndex(dict[a],pyTable,phonetic); ; short phonetic pyTable,char* dict,const wch,const GetPhoneticByHanZi(WCHAR +音调phonetic* 返回为该汉字的拼音代码="音标序号*8" pyTable为字典数组的起始地址, *从一个汉字取得数组下标 pyFile.Read(*pPyTable,4096); pyFile(pyIndex,CFile::modeRead|CFile::typeBinary); CFile *pPyTable="(char*)malloc(4096);" (!*pPyTable) TRACE(dictName); dictFile.ReadHuge(pDict,dictSize*2);>=0x81 && a <=0xfe)    
  143.       {   
  144.           b=*(Big5Source+1);   
  145.           if (b<0x7e) b=b - 0x40;   
  146.             if (b>=0x80) b = b - 0x80 + 0x7e - 0x40 + 1;    
  147.           fseek(tabfile, 2 * ((a -0x81) * (0xfe - 0x80 + 1 + 0x7e - 0x40 + 1 ) + b), SEEK_SET);   
  148.           fread(& a,1,1,tabfile);    
  149.             fread(& b,1,1,tabfile);   
  150.           fwrite(&a,1,1,destfile);   
  151.           fwrite(&b,1,1,destfile);   
  152.             *outString=a;   
  153.           *(outString+1)=b;   
  154.           outString+=2;   
  155.           Big5Source+=2;   
  156.           m+=2;   
  157.       }   
  158.       else   
  159.       {   
  160.           fwrite(&a,1,1,destfile);   
  161.           *outString=a;   
  162.             outString++;   
  163.           Big5Source++;   
  164.           m++;   
  165.       }   
  166.               
  167.     }   
  168.      
  169.     fclose(destfile);   
  170.     fclose(tabfile);   
  171.     Big5Source=BigSource;   
  172.     outString[lstr-1]='/0';   
  173.     delete[] Big5Source;   
  174.     delete[] WidecharStr;   
  175.      
  176.     return GBString;   
  177.      
  178.  }   

 

  1. #include "stdafx.h"    
  2. #include "GrammarThread.h"    
  3. #include "GetToken.h"    
  4. #include "HandleDataThread.h"    
  5. #include "process.h"    
  6. #ifdef _DEBUG    
  7. #define new DEBUG_NEW    
  8. #undef THIS_FILE    
  9. static char THIS_FILE[] = __FILE__;   
  10. #endif    
  11. #define MAX_STRING_PARAM_LEN 300     
  12. /////////////////////////////////////////////////////////////////////////////    
  13. // CGrammarThread    
  14. using namespace std;   
  15. extern WORD dictArray[0X10000];   
  16. extern char* pyTable;   
  17. void ShowMsg(CString msg);   
  18.    
  19.    
  20.    
  21. CGrammarThread::CGrammarThread()   
  22. {   
  23.    
  24. }   
  25.    
  26. CGrammarThread::CGrammarThread(NuanceConfig &config,   
  27.                DispatcherInterface & d,   
  28.                DBDescriptor * db_desc,CHandleDataThread* _p):   m_pConfig(&config), m_pDispatcher(&d), m_pRcEngine(NULL),// m_kb(NULL),    
  29.                m_pDynagram_Db_Desc(db_desc)   
  30. {   
  31.     m_parent=_p;   
  32.     NuanceStatus   status;     
  33.     for(int i=0;i<MAX_SLOT_CNT;I++) m_pRcEngine- { CGrammarThread::~CGrammarThread() } -------- NotifiableAdapter from inherited WarnForUnhandledNotifications(); m_uRecord_ID="0;" m_uRecognition_ID="0;" m_specdtmf="0;" ; m_termno="#" m_iddtime="0;" m_firsttime="0;" m_dtmflen="40;" m_dtmfreason="0;" m_dtmfcnt="0;" memset(m_dtmfbuf,0,200); m_uPlay_Prompt_ID="0;" m_playreason="0;" h_recordevent="CreateEvent(NULL,FALSE,TRUE,NULL);" h_parameterevent="CreateEvent(NULL,FALSE,TRUE,NULL);" h_recognevent="CreateEvent(NULL,FALSE,TRUE,NULL);" h_dtmfevent="CreateEvent(NULL,FALSE,TRUE,NULL);" h_playevent="CreateEvent(NULL,FALSE,TRUE,NULL);" h_insertevent="CreateEvent(NULL,FALSE,FALSE,NULL);" h_closedbevent="CreateEvent(NULL,FALSE,FALSE,NULL);" h_delevent="CreateEvent(NULL,FALSE,FALSE,NULL);" h_queryevent="CreateEvent(NULL,FALSE,FALSE,NULL);" h_newevent="CreateEvent(NULL,FALSE,FALSE,NULL);" return; !="status)" (NUANCE_OK if status="NLGetSlotNamesFromConfig(m_pConfig,slotname);" m_pRcEngine) (NULL="=" status); *this, *m_pDispatcher, RCEngine(m_pConfig, m_pRcEngine="new" NLFreeValue(slotname); NLFreeValue(slotstr); strcpy(slotnamelist[i],slot); slotnamelist[i]="NULL;" NLGetStringFromValue(slotstr,slot,200); for(i="0;i<slen;i++)" slotstr="NLNewValue(&status);" NLValue* slotname="NLNewValue(&status);" slot[200]; char slen="0;" int>Abort(NullID);   
  34.     if (m_pDynagram_Db_Desc) {   
  35.         DeleteDBDescriptor(m_pDynagram_Db_Desc);   
  36.     }   
  37.     m_pDynagram_Db_Desc = NULL;   
  38.     delete m_pRcEngine;   
  39.     m_pRcEngine = NULL;   
  40.     CloseHandle(h_newevent);   
  41.     CloseHandle(h_queryevent);   
  42.     CloseHandle(h_delevent);   
  43.     CloseHandle(h_closedbevent);   
  44.     CloseHandle(h_insertevent);   
  45.     CloseHandle(h_playevent);   
  46.     CloseHandle(h_dtmfevent);   
  47.     CloseHandle(h_recognevent);   
  48.     CloseHandle(h_parameterevent);   
  49.     CloseHandle(h_recordevent);   
  50.     for(int i=0;slotnamelist[i]!=NULL;i++)   
  51.     {   
  52.         free(slotnamelist[i]);         
  53.     }   
  54.    
  55. }   
  56.    
  57. void NUANCE_MEMBER_FUNCTION   
  58. CGrammarThread::HandleCallTransferFailed(CallTransferFailedNotification const & n)   
  59. {   
  60. }    
  61.    
  62. void NUANCE_MEMBER_FUNCTION    
  63. CGrammarThread::HandleNuanceDBRecordDeleted(NuanceDBRecordDeletedNotification const & n)   
  64. {   
  65.     NuanceStatus status = n.GetStatus();   
  66.     if (NUANCE_OK != status) {   
  67.     }else   
  68.     {   
  69.         CString msg;   
  70.         msg="记录删除";   
  71.         ShowMsg(msg);   
  72.     }   
  73.     SetEvent(h_delevent);   
  74. }    
  75.    
  76. void NUANCE_MEMBER_FUNCTION   
  77. CGrammarThread::HandleNuanceDBRecordModified(NuanceDBRecordModifiedNotification const & n)   
  78. {   
  79.     CString msg;   
  80.     NuanceStatus status = n.GetStatus();   
  81.     if (NUANCE_OK != status) {   
  82.         msg.Format("Dynamic Grammar Modified 失败:%s",n.GetErrorString());   
  83.         ShowMsg(msg);   
  84.            
  85.     }else   
  86.     {   
  87.         msg="Dynamic Grammar Modified";   
  88.         ShowMsg(msg);   
  89.     }   
  90.     SetEvent(h_newevent);   
  91. }    
  92.    
  93. void NUANCE_MEMBER_FUNCTION   
  94. CGrammarThread::HandleNuanceDBRecordCreated(NuanceDBRecordCreatedNotification const & n)   
  95. {   
  96.     CString msg;   
  97.     NuanceStatus status = n.GetStatus();   
  98.     if (NUANCE_OK != status) {   
  99.         msg.Format("Dynamic Grammar Create 失败:%s",n.GetErrorString());   
  100.         ShowMsg(msg);   
  101.     }else   
  102.     {   
  103.         msg="Dynamic Grammar Create";   
  104.         ShowMsg(msg);   
  105.     }   
  106.     SetEvent(h_newevent);   
  107. }   
  108.    
  109. void NUANCE_MEMBER_FUNCTION    
  110. CGrammarThread::HandleConferenceCallStarted(ConferenceCallStartedNotification const & n)   
  111. {   
  112. }   
  113.    
  114. void NUANCE_MEMBER_FUNCTION    
  115. CGrammarThread::HandleConferenceEnded(ConferenceEndedNotification const & n)   
  116. {   
  117. }   
  118.    
  119. void NUANCE_MEMBER_FUNCTION    
  120. CGrammarThread::HandleDTMFTones(DTMFTonesNotification const & dn)   
  121. {   
  122.        
  123.     char const *m_cNum = dn.GetTones();    
  124.     printf("have gotten dtmf :%c/r/n",*m_cNum);   
  125.     if(m_uRecord_ID>0)   
  126.     {   
  127.         if(m_termdtmf!='a'&&m_termdtmf!='A')   
  128.         {   
  129.             if(*m_cNum==m_termdtmf)   
  130.                 StopRecord();   
  131.         }else   
  132.         {   
  133.             StopRecord();   
  134.         }   
  135.     }   
  136.     if(m_dtmfstate==1)   
  137.     {   
  138.         if(m_uRecognition_ID>0)   
  139.             StopRecognize(8);   
  140.            
  141.         StopPrompt();   
  142.         if(*m_cNum==m_termno)   
  143.         {   
  144.             m_dtmfreason=1;   
  145.             m_dtmfstate=0;   
  146.             return;   
  147.         }   
  148.         memcpy(m_dtmfbuf+m_dtmfcnt,m_cNum,strlen(m_cNum));   
  149.         m_dtmfcnt+=strlen(m_cNum);   
  150.         time(&m_dt);   
  151.         if(m_dtmfcnt==1&&*m_cNum==m_specdtmf)   
  152.         {   
  153.             m_dtmfreason=1;   
  154.             m_dtmfstate=0;   
  155.         }else if(m_dtmfcnt==m_dtmflen)   
  156.         {   
  157.             m_dtmfreason=1;   
  158.             m_dtmfstate=0;   
  159.         }   
  160.     }   
  161.        
  162. }    
  163.    
  164. void NUANCE_MEMBER_FUNCTION   
  165. CGrammarThread::HandleAcknowledgment (AcknowledgmentNotification const & an)   
  166. {   
  167.     CString strfname;   
  168.     strfname.Format("%s",an.GetRequestTypeAsString(an.GetRequestType()));   
  169.    
  170.     NuanceStatus status = an.GetStatus();   
  171.     if (NUANCE_OK != status) {   
  172.    
  173.         switch(an.GetRequestType())   
  174.         {   
  175.         case AcknowledgmentNotification::RequestType::StartRecording:   
  176.             m_recordreason=2;   
  177.             m_uRecord_ID=0;   
  178.             SetEvent(h_recordevent);   
  179.             break;   
  180.         case AcknowledgmentNotification::RequestType::RecognizeUtterance:   
  181.             m_uRecognition_ID=0;   
  182.             SetEvent(h_recognevent);   
  183.             break;   
  184.         case AcknowledgmentNotification::RequestType::SetParameter:   
  185.             SetEvent(h_parameterevent);   
  186.             break;   
  187.         case AcknowledgmentNotification::RequestType::PlayPrompts:   
  188.             if(m_dtmfstate==1) time(&m_ft);   
  189.             m_uPlay_Prompt_ID=0;   
  190.             m_parent->RemoveAllWav();   
  191.             ShowMsg("***playback Stop for error");   
  192.             SetEvent(h_playevent);     
  193.             break;   
  194.         case AcknowledgmentNotification::RequestType::DeleteRecord:   
  195.             SetEvent(h_delevent);   
  196.             break;   
  197.         case AcknowledgmentNotification::RequestType::CloseDatabase:   
  198.             SetEvent(h_closedbevent);   
  199.             break;   
  200.         case AcknowledgmentNotification::RequestType::NewDynamicGrammar:   
  201.         case AcknowledgmentNotification::RequestType::NewDynamicGrammarFromGSL:   
  202.         case AcknowledgmentNotification::RequestType::NewDynamicGrammarFromPhraseList:   
  203.         case AcknowledgmentNotification::RequestType::AddPhraseListToDynamicGrammar:   
  204.         case AcknowledgmentNotification::RequestType::AddPhraseToDynamicGrammar:       
  205.         case AcknowledgmentNotification::RequestType::RemovePhraseFromDynamicGrammar:      
  206.             SetEvent(h_newevent);   
  207.             break;   
  208.         case AcknowledgmentNotification::RequestType::InsertDynamicGrammar:   
  209.             SetEvent(h_insertevent);   
  210.             break;   
  211.         case AcknowledgmentNotification::RequestType::QueryDynamicGrammarContents:   
  212.         case AcknowledgmentNotification::RequestType::QueryRecordExists:   
  213.             SetEvent(h_queryevent);   
  214.             break;             
  215.         }   
  216.     }   
  217. }   
  218.    
  219. void NUANCE_MEMBER_FUNCTION   
  220. CGrammarThread::HandleInitializationCompleted (InitializationCompletedNotification const & icn)   
  221. {   
  222.     StateDynagramOpenDB();   
  223. }   
  224.    
  225. void NUANCE_MEMBER_FUNCTION   
  226. CGrammarThread::HandlePlaybackStopped (PlaybackStoppedNotification const & pcn)   
  227. {   
  228.    
  229.     PlaybackStoppedNotification::Reason reason = pcn.GetReason();   
  230.     switch (reason)    
  231.     {   
  232.         //完全播放结束    
  233.     case PlaybackStoppedNotification::PLAYBACK_COMPLETED:   
  234.         {   
  235.             m_playreason=1;   
  236.         }   
  237.         break;         
  238.         //若是调用了 Abort 函数 或 挂断电话    
  239.     case PlaybackStoppedNotification::PLAYBACK_ABORTED:   
  240.         {   
  241.             m_playreason=2;   
  242.         }   
  243.         break;   
  244.            
  245.     case PlaybackStoppedNotification::HANG_UP:   
  246.         {              
  247.             m_playreason=3;   
  248.         }   
  249.         break;   
  250.     default:   
  251.         {   
  252.             m_playreason=4;   
  253.         }   
  254.     }//end of case    
  255.     if(m_dtmfstate==1) time(&m_ft);   
  256.     m_uPlay_Prompt_ID=0;   
  257.     m_parent->RemoveAllWav();   
  258.     ShowMsg("***playback Stop");   
  259.     SetEvent(h_playevent);     
  260. }   
  261.    
  262. void NUANCE_MEMBER_FUNCTION   
  263. CGrammarThread::HandleRecordingStopped (RecordingStoppedNotification const & rsn)   
  264. {   
  265.     m_uRecord_ID=0;   
  266.     SetEvent(h_recordevent);   
  267. }   
  268.    
  269. void NUANCE_MEMBER_FUNCTION   
  270. CGrammarThread::HandleStartOfSpeech (StartOfSpeechNotification const & sosn)   
  271. {   
  272.     printf("^^^^^^^^^HandleStartOfSpeech/r/n");   
  273.     if(m_dtmfstate==1)  StopDtmf(8);   
  274. }   
  275.    
  276. void NUANCE_MEMBER_FUNCTION   
  277. CGrammarThread::HandleEndOfSpeech (EndOfSpeechNotification const & eosn)   
  278. {   
  279. }   
  280.    
  281. void NUANCE_MEMBER_FUNCTION   
  282. CGrammarThread::HandlePartialResult (PartialResultNotification const & prn)   
  283. {   
  284. }   
  285.    
  286. void NUANCE_MEMBER_FUNCTION   
  287. CGrammarThread::HandleRecognitionStopped (RecognitionStoppedNotification const & rsn)   
  288. {   
  289.     //取的识别 ID    
  290.     unsigned id = rsn.GetUtteranceID();   
  291.        
  292.     //判断识别结束原因    
  293.     int iReason = rsn.GetReason();    
  294.     switch (iReason)    
  295.     {   
  296.         //识别顺利完成    
  297.         case RecognitionStoppedNotification::COMPLETED:   
  298.         {   
  299.             RecResult const * rr = rsn.GetRecResult();     
  300.             RecognizeNotify((RecResult*)rr);                            
  301.         }   
  302.         break;   
  303.            
  304.         //挂断电话    
  305.         case RecognitionStoppedNotification::HANG_UP:   
  306.         {   
  307.                
  308.         }   
  309.         break;   
  310.            
  311.         //调用 Abort    
  312.         case RecognitionStoppedNotification::ABORTED:   
  313.         {   
  314.             ShowMsg("00000000000000000");   
  315.         }   
  316.         break;   
  317.            
  318.     default:   
  319.         {              
  320.             ShowMsg("111111111111111111111");   
  321.         }   
  322.     }   
  323.     ShowMsg("Recognize Stop");   
  324.     m_uRecognition_ID=0;   
  325.     SetEvent(h_recognevent);   
  326.    
  327. }   
  328.    
  329. void NUANCE_MEMBER_FUNCTION   
  330. CGrammarThread::HandleIncomingCall (IncomingCallNotification const & icn)   
  331. {   
  332.     ShowMsg("Incoming call--------------");   
  333.     strcpy(m_parent->m_callingID,icn.GetCallerID());   
  334.     strcpy(m_parent->m_calledID,icn.GetDID());   
  335.     unsigned id = icn.GetCallID();     
  336.     m_uAnswer_Call_ID=id;          
  337.     m_pRcEngine->AnswerCall(m_uAnswer_Call_ID);   
  338. }   
  339.    
  340. void NUANCE_MEMBER_FUNCTION   
  341. CGrammarThread::HandleOutgoingCallConnected (OutgoingCallConnectedNotification const & occn)   
  342. {   
  343. }   
  344.    
  345. void NUANCE_MEMBER_FUNCTION   
  346. CGrammarThread::HandleCallAnswered (CallAnsweredNotification const & can)   
  347. {   
  348.     ShowMsg("Call Answered--------------");   
  349.     m_parent->m_callstate=1;   
  350.     m_parent->PostThreadMessage(WM_CALL_CONNECT,0,0);   
  351. }   
  352.    
  353. void NUANCE_MEMBER_FUNCTION   
  354. CGrammarThread::HandleCallEnded (CallEndedNotification const & cen)   
  355. {   
  356.     ShowMsg("Call Ended--------------");   
  357.     m_parent->m_callstate=2;   
  358.     StopAll();   
  359.     m_parent->PostThreadMessage(WM_CALL_DISCONNECT,0,0);   
  360. }   
  361.    
  362. void NUANCE_MEMBER_FUNCTION   
  363. CGrammarThread::HandleNuanceDBOpened (NuanceDBOpenedNotification const & ndbon)   
  364. {   
  365.     // check status    
  366.     NuanceStatus status = ndbon.GetStatus();   
  367.     if (NUANCE_OK != status) {   
  368.         CString msg="OpenDB 失败";   
  369.         ShowMsg(msg);   
  370.         m_pRcEngine->Abort(NullID);   
  371.         return;   
  372.     }   
  373.        
  374.     unsigned id = ndbon.GetID();   
  375.        
  376.     if (m_uDynagram_Db_ID != id) {   
  377.         CString msg="OpenDB ID 失败";   
  378.         ShowMsg(msg);   
  379.         m_pRcEngine->Abort(NullID);   
  380.         return;   
  381.     }   
  382.     CString msg="Open DB 成功";   
  383.     ShowMsg(msg);   
  384.        
  385. }   
  386.    
  387. void NUANCE_MEMBER_FUNCTION   
  388. CGrammarThread::HandleNuanceDBClosed (NuanceDBClosedNotification const & ndbcn)   
  389. {   
  390.     NuanceStatus status = ndbcn.GetStatus();   
  391.     if (NUANCE_OK != status) {   
  392.         CString msg="CloseDB 失败";   
  393.         ShowMsg(msg);   
  394.    
  395.     }else   
  396.     {   
  397.         CString msg="CloseDB 成功";   
  398.         ShowMsg(msg);          
  399.     }   
  400.     SetEvent(h_closedbevent);   
  401. }   
  402.    
  403. void NUANCE_MEMBER_FUNCTION   
  404. CGrammarThread::HandleNuanceDBRecordExists (NuanceDBRecordExistsNotification const & ndben)   
  405. {   
  406.     NuanceStatus status = ndben.GetStatus();   
  407.     if (NUANCE_OK != status) {   
  408.         CString msg="Grammar 记录不存在";   
  409.         ShowMsg(msg);      
  410.         r_exist=false;   
  411.     }else   
  412.     {   
  413.         CString msg="Grammar 记录存在";   
  414.         ShowMsg(msg);      
  415.    
  416.         r_exist=true;   
  417.     }   
  418.     SetEvent(h_queryevent);   
  419.    
  420. }   
  421.    
  422. void NUANCE_MEMBER_FUNCTION   
  423. CGrammarThread::HandleDynamicGrammarInserted (DynamicGrammarInsertedNotification const & dgin)   
  424. {   
  425.     NuanceStatus status = dgin.GetStatus();   
  426.     if (NUANCE_OK != status) {   
  427.         ShowMsg("Grammar insert 失败");   
  428.     }else   
  429.     {   
  430.         ShowMsg("Grammar insert 成功");   
  431.     }   
  432.     SetEvent(h_insertevent);   
  433. }   
  434.    
  435. void NUANCE_MEMBER_FUNCTION   
  436. CGrammarThread::HandleParameterSet (ParameterSetNotification const & psn)   
  437. {   
  438.     SetEvent(h_parameterevent);   
  439. }   
  440.    
  441. void NUANCE_MEMBER_FUNCTION   
  442. CGrammarThread::HandleParameterGotten (ParameterGottenNotification const & pgn)   
  443. {   
  444. }   
  445.    
  446. void NUANCE_MEMBER_FUNCTION   
  447. CGrammarThread::HandleException (ExceptionNotification const & en)   
  448. {   
  449. }   
  450.    
  451. void   
  452. CGrammarThread::   
  453. StateDynagramOpenDB (void)   
  454. {   
  455.     m_uDynagram_Db_ID = m_pRcEngine->GetUniqueID();   
  456.     m_pRcEngine->OpenDynamicGrammarDatabase(m_pDynagram_Db_Desc, m_uDynagram_Db_ID);   
  457. }   
  458.    
  459.    
  460. int CGrammarThread::NewDynaGrammar(char *key,char *text,bool overwrite)   
  461. {   
  462.        
  463.     bool t_ret=false;   
  464.     PhraseList* m_pPhrase_List;   
  465.     m_pPhrase_List=NewPhraseList();   
  466.    
  467.     char* token = strtok(text, "|");   
  468.     while( token != NULL )   
  469.     {      
  470.         CString tmpstr;   
  471.         tmpstr.Format("%s",token);   
  472.         bool ret=CreatePhraseList(m_pPhrase_List,(char*)(LPCTSTR)tmpstr);   
  473.         if(ret) t_ret=ret;   
  474.         token = strtok( NULL, "|" );   
  475.     }   
  476.    
  477.     if(!t_ret)   
  478.     {   
  479.         SetEvent(h_newevent);   
  480.         DeletePhraseList(m_pPhrase_List);   
  481.         return 1;   
  482.     }   
  483.        
  484.     unsigned m_uDynagram_New_ID = m_pRcEngine->GetUniqueID();   
  485.     if(overwrite)   
  486.     {   
  487.         ResetEvent(h_newevent);   
  488.         m_pRcEngine->NewDynamicGrammar(m_uDynagram_Db_ID,key,   
  489.             m_uDynagram_New_ID,m_pPhrase_List,NULL,1);   
  490.     }else   
  491.     {   
  492.         if(QueryRecord(key))   
  493.         {   
  494.             ResetEvent(h_newevent);   
  495.             m_pRcEngine->AddPhraseListToDynamicGrammar(m_uDynagram_Db_ID,key,   
  496.                 m_uDynagram_New_ID,m_pPhrase_List,NULL);   
  497.         }   
  498.         else   
  499.         {   
  500.             ResetEvent(h_newevent);   
  501.             m_pRcEngine->NewDynamicGrammar(m_uDynagram_Db_ID,key,   
  502.             m_uDynagram_New_ID,m_pPhrase_List,NULL,1);   
  503.         }   
  504.                
  505.     }   
  506.        
  507.     DeletePhraseList(m_pPhrase_List);   
  508.     WaitForSingleObject(h_newevent,INFINITE);   
  509.     return 0;   
  510. }   
  511.    
  512. CString ParseGrammar(CString src)   
  513. {   
  514.     CString result="";   
  515.     CString tmp="";   
  516.     char ch;   
  517.     bool haso=false;   
  518.     for(int i=0;i<SRC.GETLENGTH();I++) { } ; slotname="" char int if(i="=0)" breakif(!mrun) i++; slotname.TrimRight(); slotname.TrimLeft(); ntext.TrimRight(); ntext.TrimLeft(); ntext="" ptext.TrimRight(); ptext.TrimLeft(); ptext="" stext.TrimRight(); stext.TrimLeft(); stext="" pos1='srctext.Find("*",0);' pos2="0;" ttext="" }else mrun="true;" if(pos1<0) CString while(1) i="0;" bool srctext.Format(?%s?,text); srctext; status; NuanceStatus *text) *p_list, CGrammarThread::CreatePhraseList(PhraseList result; return ?); result.TrimRight(? tmp="" free(phFood); result+="tmp;" ?,phFood); tmp.Format(?%s if(phFood) pyTable); *phFood="GetPhoneticByWideString((WCHAR*)(char*)(LPCTSTR)tmp,dictArray," tmp.Format(??(%s) if(haso) ) if(tmp!="" tmp+="ch;" haso="true;" if(ch="='[')" ch="src.GetAt(i);">3) break;   
  519.     }   
  520.     if(stext==""return false;   
  521.     bool srcz=false;   
  522.     if (ntext.Left(2)=="c:"||ntext.Left(2)=="C:")   
  523.     {   
  524.         ntext=ntext.Right(ntext.GetLength()-2);   
  525.         srcz=true;   
  526.     }   
  527.     if(ntext=="")   
  528.     {   
  529.    
  530.         char *phFood =GetPhoneticByWideString((WCHAR*)(char*)(LPCTSTR)stext,dictArray,   
  531.             pyTable);   
  532.         if(!phFood)   
  533.         {   
  534.             return false;   
  535.         }   
  536.         ntext.Format("%s",phFood);   
  537.         free(phFood);      
  538.     }else   
  539.     {   
  540.         if(srcz)   
  541.         {   
  542.             ntext=ParseGrammar(ntext);   
  543.             srcz=false;   
  544.         }   
  545.     }   
  546.        
  547.     if(ptext=="")   
  548.     {   
  549.         ptext=ntext;   
  550.         ptext.Replace(" ","");   
  551.     }   
  552.     CString ctmp=ntext;   
  553.     ntext="("+ctmp+")";   
  554.     ctmp=stext;   
  555.     if(slotname=="")   
  556.     {   
  557.         slotname="peoplename";   
  558.     }   
  559.     stext="{<" + slotname+" /""+ctmp+"/">}";   
  560.     status = PhraseListAdd(   
  561.         p_list,         // phrase list pointer    
  562.         ptext,          // used to reference phrase in db    
  563.         ntext,  // actual text to pronounce    
  564.         stext,             // nl entry    
  565.         1.0                     // probability    
  566.         );         
  567.     if(status == NUANCE_OK)   
  568.         return true;       
  569.     return false;   
  570. }   
  571.    
  572.    
  573. bool CGrammarThread::QueryRecord(char *key)   
  574. {   
  575.     ResetEvent(h_queryevent);   
  576.     r_exist=false;   
  577.     unsigned m_uDynagram_Query_ID = m_pRcEngine->GetUniqueID();   
  578.     m_pRcEngine->QueryRecordExists(m_uDynagram_Db_ID,key,m_uDynagram_Query_ID);   
  579.     WaitForSingleObject(h_queryevent,INFINITE);   
  580.     return r_exist;   
  581. }   
  582.    
  583. void CGrammarThread::RemovePhrase(char *key, char *phraseid)   
  584. {   
  585.     char* token = strtok(phraseid, "|");   
  586.     while( token != NULL )   
  587.     {   
  588.         if(QueryDGrmContents(key,token))   
  589.         {   
  590.             ResetEvent(h_newevent);   
  591.             unsigned m_uDynagram_Remove_ID = m_pRcEngine->GetUniqueID();   
  592.             m_pRcEngine->RemovePhraseFromDynamicGrammar(m_uDynagram_Db_ID,key,m_uDynagram_Remove_ID,token);   
  593.             WaitForSingleObject(h_newevent,INFINITE);          
  594.         }   
  595.         token = strtok( NULL, "|" );           
  596.     }   
  597. }   
  598.    
  599. bool CGrammarThread::QueryDGrmContents(char *key, char *phraseid)   
  600. {   
  601.     ResetEvent(h_queryevent);   
  602.     r_exist=false;   
  603.     unsigned m_uDynagram_Query_ID = m_pRcEngine->GetUniqueID();   
  604.     m_pRcEngine->QueryDynamicGrammarContents(m_uDynagram_Db_ID,key,m_uDynagram_Query_ID,phraseid);   
  605.     WaitForSingleObject(h_queryevent,INFINITE);   
  606.     return r_exist;   
  607. }   
  608.    
  609. void NUANCE_MEMBER_FUNCTION   
  610. CGrammarThread::HandleNuanceDBRecordContents(NuanceDBRecordContentsNotification const & n)   
  611. {   
  612.     NuanceStatus status = n.GetStatus();   
  613.     if (NUANCE_OK != status) {   
  614.         ShowMsg("Grammar中内容不存在");   
  615.         r_exist=false;   
  616.     }else   
  617.     {   
  618.            
  619.         char* str=(char*)n.GetPhraseString();   
  620.         if(strlen(str)>0)   
  621.         {   
  622.             //--避免CString 的长度限制,所以没用ShowMsg    
  623.             cout<<"Grammar中内容存在:"<<ENDL; { } }else m_uDynagram_Del_ID="m_pRcEngine-" unsigned ResetEvent(h_delevent); *key) CGrammarThread::DeleteGrm(char void SetEvent(h_queryevent); r_exist="true;" ShowMsg(?Grammar中内容不存在?); cout<<str<<endl;>GetUniqueID();   
  624.     m_pRcEngine->DeleteRecord(m_uDynagram_Db_ID,key,m_uDynagram_Del_ID);   
  625.     WaitForSingleObject(h_delevent,INFINITE);   
  626. }   
  627.    
  628. void CGrammarThread::CloseDB()   
  629. {   
  630.     ResetEvent(h_closedbevent);    
  631.     m_pRcEngine->CloseDatabase(m_uDynagram_Db_ID);   
  632.     WaitForSingleObject(h_closedbevent,INFINITE);      
  633. }   
  634.    
  635. void CGrammarThread::InsertGrammer(char *key, char *lable,bool tp)   
  636. {   
  637.     ResetEvent(h_insertevent);   
  638.     unsigned m_uDynagram_Insert_ID = m_pRcEngine->GetUniqueID();   
  639.     m_pRcEngine->InsertDynamicGrammar(m_uDynagram_Db_ID,key,m_uDynagram_Insert_ID,lable,((tp)?DURATION_CALL:DURATION_PERMANENT));   
  640.     WaitForSingleObject(h_insertevent,INFINITE);   
  641. }   
  642.    
  643. BYTE CGrammarThread::StatePrompt(bool _wait)   
  644. {   
  645.     /*for(int i=0;i<M_PARENT->m_currentprompt;i++)  
  646.     {   ShowMsg("******");  
  647.         ShowMsg(m_parent->m_playlist[i]);  
  648.     }  
  649.     */   
  650.     if(m_parent->m_currentprompt<=0)   
  651.     {   
  652.         if(m_dtmfstate==1) time(&m_ft);   
  653.         SetEvent(h_playevent);   
  654.         return 1;   
  655.     }   
  656.     m_playreason=0;   
  657.     ResetEvent(h_playevent);   
  658.     m_uPlay_Prompt_ID= m_pRcEngine->GetUniqueID();   
  659.     m_pRcEngine->PlayPrompts(m_parent->m_playlist,m_uPlay_Prompt_ID);   
  660.     if(_wait) WaitForSingleObject(h_playevent,INFINITE);   
  661.     return m_playreason;   
  662. }   
  663.    
  664. void DtmfThread(LPVOID lp)   
  665. {      
  666.     CGrammarThread* p=(CGrammarThread*)lp;   
  667.     while(p->m_dtmfstate)   
  668.     {   
  669.         if(p->m_firsttime>0&&p->m_dtmfcnt==0&&p->m_uPlay_Prompt_ID==0)   
  670.         {   
  671.             time_t nt;   
  672.             time(&nt);   
  673.             if(nt-p->m_ft>=p->m_firsttime)   
  674.             {   
  675.                 p->m_dtmfreason=1;   
  676.                 p->m_dtmfstate=0;   
  677.                 break;   
  678.             }   
  679.         }   
  680.         if(p->m_iddtime>0&&p->m_dtmfcnt>0)   
  681.         {   
  682.             time_t nt;   
  683.             time(&nt);   
  684.             if(nt-p->m_dt>=p->m_iddtime)   
  685.             {   
  686.                 p->m_dtmfreason=1;   
  687.                 p->m_dtmfstate=0;   
  688.                 break;   
  689.             }   
  690.         }   
  691.         Sleep(1);   
  692.     }   
  693.     p->StopPrompt();   
  694.     SetEvent(p->h_dtmfevent);   
  695.    
  696. }   
  697. BYTE CGrammarThread::RunDtmf(int dtmflen, int firsttime,int iddtime, char termno,char specdtmf)   
  698. {   
  699.        
  700.     ResetEvent(h_dtmfevent);   
  701.     memset(m_dtmfbuf,0,40);   
  702.     m_dtmfcnt=0;   
  703.     m_dtmfreason=0;   
  704.    
  705.     m_dtmflen=dtmflen;   
  706.     m_firsttime=firsttime;   
  707.     m_iddtime=iddtime;   
  708.     m_termno=termno;   
  709.     m_specdtmf=specdtmf;   
  710.     time(&m_dt);   
  711.     m_dtmfstate=1;   
  712.     StatePrompt(false);   
  713.     _beginthread(DtmfThread,0,this);   
  714.     WaitForSingleObject(h_dtmfevent,INFINITE);   
  715.     WaitForSingleObject(h_playevent,INFINITE);   
  716.     return m_dtmfreason;       
  717. }   
  718.    
  719. void CGrammarThread::StopDtmf(BYTE _r)   
  720. {   
  721.     m_dtmfreason=_r;       
  722.     m_dtmfstate=0;   
  723.        
  724. }   
  725.    
  726. void CGrammarThread::StopPrompt()   
  727. {   
  728.     if(m_uPlay_Prompt_ID>0)   
  729.     {   
  730.         m_pRcEngine->AbortPlayback(m_uPlay_Prompt_ID);   
  731.     }else   
  732.     {   
  733.         SetEvent(h_playevent);   
  734.     }   
  735.    
  736. }   
  737.    
  738. BYTE CGrammarThread::RecognizeUttr(bool hotword,WORD nbest, WORD nospeechtimeout,   
  739.                                    WORD toomuchms,WORD tooslowms, WORD rejthreshold,   
  740.                                    char* resultbuf,char *pText, char *_grammar,    
  741.                                    bool _overwrite, char *key,int dtmflen,   
  742.                                    int iddtime,char termno,char specdtmf)   
  743. {   
  744.    
  745.     BYTE ret=0;   
  746.     m_resultbuf=resultbuf;   
  747.     int m_nbest=nbest;   
  748.     double m_nospeechtimeout=nospeechtimeout*1.00;   
  749.     int m_toomuchms=toomuchms;   
  750.     int m_tooslowms=tooslowms;   
  751.     int m_rejthreshold=rejthreshold;   
  752.    
  753.     SetParameters("rec.NumNBest",&m_nbest,0);   
  754.     SetParameters("client.NoSpeechTimeoutSecs",&m_nospeechtimeout,1);   
  755.     SetParameters("rec.ConfidenceRejectionThreshold",&m_rejthreshold,0);   
  756.        
  757.     char glable[150];   
  758.     char f_grammar[150];   
  759.     char gkey[150];   
  760.        
  761.     strcpy(glable,"DynaPeople0");   
  762.     if(key) strcpy(gkey,key);   
  763.     else    sprintf(gkey,"nasr%d",m_parent->m_index);   
  764.    
  765.     if(_grammar) strcpy(f_grammar,_grammar);   
  766.     else    strcpy(f_grammar,".PEOPLE0");   
  767.    
  768.                
  769.     if(!_grammar)   
  770.     {   
  771.         if(strlen(pText)>0)   
  772.         {              
  773.             int ret=NewDynaGrammar(gkey,pText,_overwrite);   
  774.             if(ret!=0)   
  775.             {   
  776.                 return 2;      
  777.             }   
  778.         }          
  779.         InsertGrammer(gkey,glable);                    
  780.     }   
  781.     ResetEvent(h_recognevent);   
  782.     m_uRecognition_ID=m_pRcEngine->GetUniqueID();   
  783.     m_pRcEngine->RecognizeUtterance(f_grammar,m_uRecognition_ID);   
  784.     if(dtmflen<=0)   
  785.     {   
  786.         StatePrompt(false);                            
  787.         WaitForSingleObject(h_playevent,INFINITE);   
  788.         WaitForSingleObject(h_recognevent,INFINITE);   
  789.     }   
  790.     else   
  791.     {   
  792.         ret=RunDtmf(dtmflen, 0,iddtime, termno, specdtmf);   
  793.         WaitForSingleObject(h_recognevent,INFINITE);   
  794.         if(ret!=8)   
  795.         {   
  796.             strcpy(resultbuf,m_dtmfbuf);   
  797.         }   
  798.     }   
  799.     return 1;   
  800. }   
  801.    
  802. void CGrammarThread::SetParameters(char *pname, void *value, BYTE vtype)   
  803. {   
  804.     ResetEvent(h_parameterevent);   
  805.     m_uParameter_ID=m_pRcEngine->GetUniqueID();   
  806.     switch(vtype)   
  807.     {   
  808.     case 0://INT    
  809.         m_pRcEngine->SetParameter(pname,*((int*)value),m_uParameter_ID);   
  810.         break;   
  811.     case 1://double    
  812.         m_pRcEngine->SetParameter(pname,*((double*)value),m_uParameter_ID);   
  813.         break;   
  814.     case 2://char*    
  815.         m_pRcEngine->SetParameter(pname,*((char**)value),m_uParameter_ID);   
  816.         break;     
  817.     }   
  818.     WaitForSingleObject(h_parameterevent,INFINITE);   
  819. }   
  820.    
  821.    
  822. HRESULT CGrammarThread::RecognizeNotify(RecResult *rr)   
  823. {   
  824.     //判断结果是否合法,记录在  rec_result_type 中    
  825.     //jb 2001.12.07    
  826.     NuanceStatus   status;   
  827.     RecResultType  rec_result_type;   
  828.        
  829.     status = RecResultGetType(rr,&rec_result_type);   
  830.     if (NUANCE_OK != status)    
  831.     {   
  832.         strcpy(m_resultbuf,"error");   
  833.         return 0;   
  834.     }   
  835.        
  836.     //若是 不说话超时,则重播刚才的 Prompt    
  837.     if(rec_result_type == NO_SPEECH_TIMEOUT)   
  838.     {          
  839.         m_resultbuf[0]=0;          
  840.         return 0;   
  841.     }   
  842.    
  843.     //若识别不成功    
  844.     if(rec_result_type != RECOGNITION)   
  845.     {   
  846.         strcpy(m_resultbuf,"nomatch");   
  847.         return 0;   
  848.     }   
  849.    
  850.        
  851.     // 得到结果的个数    
  852.     int num_results;   
  853.     status = RecResultNumAnswers(rr, &num_results);   
  854.                      // nlresult for natural language interpretion    
  855.     m_resultbuf[0]=0;   
  856.     // 取得每一个结果    
  857.     for (int index = 0; index < num_results; ++index)    
  858.     {   
  859.         NLResult *            m_pNl_Result=NULL;       
  860.         m_pNl_Result = NLInitializeResult(&status);   
  861.         char result_buf[MAX_STRING_PARAM_LEN];   
  862.         int  confidence_score;                //识别结果的确定性分数    
  863.    
  864.         // 得到结果的 string 型    
  865.         status = RecResultString(rr, index, result_buf, sizeof(result_buf));   
  866.         if (NUANCE_OK != status)   
  867.         {   
  868.             strcpy(m_resultbuf,"error");   
  869.             return 0;              
  870.         }   
  871.         // 得到结果的 Confidence    
  872.         status = RecResultOverallConfidence(rr, index, &confidence_score);   
  873.         if (NUANCE_OK != status)    
  874.         {               
  875.             strcpy(m_resultbuf,"error");   
  876.             return 0;              
  877.         }   
  878.                
  879.            
  880.         status = RecResultNLResult(rr, index, m_pNl_Result);   
  881.    
  882.         // 得到 NL Result 的个数    
  883.         int num_nl_results =   
  884.                 NLGetNumberOfInterpretations(m_pNl_Result, &status);   
  885.         if (NUANCE_OK != status)   
  886.         {   
  887.             strcpy(m_resultbuf,"error");   
  888.             return 0;   
  889.                
  890.         }   
  891.    
  892.         for (int nl_index = 0; nl_index < num_nl_results; nl_index++)    
  893.         {   
  894.             //激活当前的 NL Result    
  895.             status = NLMakeIthInterpretationActive(m_pNl_Result,   
  896.                                                    nl_index);   
  897.             if (NUANCE_OK != status)    
  898.             {   
  899.                 strcpy(m_resultbuf,"error");   
  900.                 return 0;                  
  901.             }   
  902.                    
  903.             char nlvalue[300];   
  904.             for(int i=0;slotnamelist[i]!=NULL;i++)   
  905.             {   
  906.                 status = NLGetSlotValueAsString(m_pNl_Result,slotnamelist[i],nlvalue,300);     
  907.                 if(nlvalue[0]!=0)   
  908.                 {                  
  909.                     strcat(m_resultbuf,nlvalue);   
  910.                     strcat(m_resultbuf,";");   
  911.                 }   
  912.             }   
  913.                            
  914.         }   
  915.         NLFreeResult(m_pNl_Result);        
  916.      }   
  917.     m_resultbuf[strlen(m_resultbuf)-1]=0;   
  918.    
  919.     return 0;   
  920. }   
  921.    
  922. void CGrammarThread::StopRecognize(BYTE r)   
  923. {   
  924.     if(m_uRecognition_ID>0)   
  925.     {   
  926.         m_pRcEngine->Abort(m_uRecognition_ID);   
  927.     }else   
  928.     {   
  929.         SetEvent(h_recognevent);   
  930.     }   
  931.    
  932. }   
  933.    
  934.    
  935. void CGrammarThread::StopAll()   
  936. {   
  937.     StopPrompt();   
  938.     StopRecord();   
  939.     StopRecognize(0);   
  940.     if(m_dtmfstate==1) StopDtmf(0);    
  941. }   
  942.    
  943. void CheckRecordTime(LPVOID lp)   
  944. {   
  945.     CGrammarThread* p=(CGrammarThread*)lp;   
  946.     time_t ft;   
  947.     time(&ft);   
  948.     while(p->m_uRecord_ID>0)   
  949.     {   
  950.         time_t nt;   
  951.         time(&nt);   
  952.         if(nt-ft>=p->m_maxtime)   
  953.         {      
  954.             p->StopRecord();   
  955.             break;   
  956.         }   
  957.         Sleep(1);   
  958.     }   
  959. }   
  960.    
  961. BYTE CGrammarThread::StateRecord(char* filename,BOOL beep,int maxtime,int stime,CString dtmf,int type,CString bgwav,int mtype)   
  962. {   
  963.     m_recordreason=1;   
  964.     m_maxtime=maxtime;   
  965.     m_termdtmf=dtmf.GetAt(0);   
  966.     ResetEvent(h_recordevent);   
  967.     m_uRecord_ID=m_pRcEngine->GetUniqueID();   
  968.     //m_pRcEngine->RecordUtterance();        
  969.     m_pRcEngine->StartRecording(filename,m_uRecord_ID);   
  970.     if(m_maxtime>0)   
  971.     {   
  972.         _beginthread(CheckRecordTime,0,this);   
  973.     }   
  974.     WaitForSingleObject(h_recordevent,INFINITE);   
  975.     return m_recordreason;   
  976. }   
  977.    
  978. void CGrammarThread::StopRecord()   
  979. {   
  980.     if(m_uRecord_ID>0)   
  981.     {   
  982.         m_pRcEngine->StopRecording(m_uRecord_ID);   
  983.     }else   
  984.     {   
  985.         SetEvent(h_recordevent);   
  986.     }   
  987. }   
  988.    
  989. void CGrammarThread::StateDisconnect()   
  990. {   
  991.     m_pRcEngine->Hangup();   
  992. }   
  993.    
  994. void CGrammarThread::StateTransfer(char *dest, char *src)   
  995. {   
  996.     m_pRcEngine->TransferCall(dest,m_uAnswer_Call_ID);   
  997.     Sleep(1000);   
  998.     StateDisconnect();   
  999. }   
  1000. #include "stdafx.h"    
  1001. #include "HandleDataThread.h"    
  1002. #include "process.h"    
  1003.    
  1004.    
  1005. #include "datastr.h"    
  1006. #ifdef _DEBUG    
  1007. #define new DEBUG_NEW    
  1008. #undef THIS_FILE    
  1009. static char THIS_FILE[] = __FILE__;   
  1010. #endif    
  1011. using namespace std;   
  1012. char m_tempwavdir[250];   
  1013.    
  1014. /////////////////////////////////////////////////////////////////////////////    
  1015. // CHandleDataThread    
  1016. void ShowMsg(CString msg);   
  1017. void DispatherThread(void* lp);   
  1018. CString ParseParString(char* src,char*pname,bool last=false);   
  1019. char ParseParChar(char* src,char*pname);   
  1020. int ParseParInt(char* src,char*pname);   
  1021. BOOL ParseParBool(char* src,char*pname);   
  1022. extern CONFIG_PARAM config_params[MAX_HANDLE_THREADS];   
  1023. extern HANDLE m_endevents [MAX_HANDLE_THREADS];   
  1024. IMPLEMENT_DYNCREATE(CHandleDataThread, CWinThread)   
  1025. BOOL MyPostThreadMessage(DWORD id,UINT tmsg,WPARAM wParam,LPARAM lParam);   
  1026. BOOL MyPostThreadMessage(DWORD id,UINT tmsg,WPARAM wParam,LPARAM lParam)   
  1027. {   
  1028.     if(!(::PostThreadMessage(id,tmsg,wParam,lParam)))   
  1029.     {   
  1030.         NOTIFY_MESSAGE* ps=(NOTIFY_MESSAGE*)lParam;   
  1031.         delete ps;   
  1032.         return FALSE;   
  1033.     }   
  1034.     return TRUE;   
  1035. }   
  1036.    
  1037. char ParseParChar(char* src,char*pname)   
  1038. {   
  1039.     CString ret=ParseParString(src,pname);   
  1040.     if(ret.GetLength()>0)   
  1041.         return (char)ret.GetAt(0);   
  1042.     return 0;   
  1043. }   
  1044.    
  1045. BOOL ParseParBool(char* src,char*pname)   
  1046. {   
  1047.     CString ret=ParseParString(src,pname);   
  1048.     ret.TrimLeft();   
  1049.     ret.TrimRight();   
  1050.        
  1051.     if(ret.CompareNoCase("true")==0)   
  1052.         return TRUE;   
  1053.     return FALSE;   
  1054. }   
  1055.    
  1056.    
  1057. CString ParseParString(char* src,char*pname,bool last)   
  1058. {   
  1059.     CString ret="";   
  1060.     CString strsrc="";   
  1061.     strsrc.Format("%s",src);   
  1062.     int pstart=0;   
  1063.     int pos=0;         
  1064.     while(pstart<STRSRC.GETLENGTH()) if(ret.GetLength() ret="strsrc.Mid(pstart+strlen(pname)+1);" CString { src,char*pname) ParseParInt(charint } ret; return pstart="pos+2;" breakelse if(last) if(tpname.CompareNoCase(CString(pname)+?:?)="=0)" tpname="tmpName.Left(strlen(pname)+1);" ) if(tmpName!="" tmpName="strsrc.Mid(pstart,pos-pstart);" pos='strsrc.Find("@@",pstart);' if(pos="=-1)">0)   
  1065.     {   
  1066.         return atoi(ret);   
  1067.     }   
  1068.     return 0;   
  1069. }   
  1070. DWORD execmdline(char* cmdline)   
  1071. {   
  1072.     DWORD Result=100;   
  1073.     STARTUPINFO  StartupInfo={0};   
  1074.     PROCESS_INFORMATION ProcessInfo;   
  1075.        
  1076.     StartupInfo.cb = sizeof(StartupInfo);   
  1077.     StartupInfo.dwFlags = STARTF_USESHOWWINDOW;   
  1078.     StartupInfo.wShowWindow = SW_HIDE;   
  1079.        
  1080.     if(CreateProcess(NULL,cmdline,   
  1081.         NULL,NULL,FALSE,   
  1082.         0,NULL,NULL,   
  1083.         &StartupInfo,&ProcessInfo   
  1084.         ))   
  1085.     {   
  1086.         WaitForSingleObject(ProcessInfo.hProcess,INFINITE);     
  1087.         GetExitCodeProcess(ProcessInfo.hProcess,&Result);     
  1088.            
  1089.     }   
  1090.     return Result;   
  1091. }   
  1092.    
  1093. CHandleDataThread::CHandleDataThread()   
  1094. {   
  1095.    
  1096. }   
  1097.    
  1098. CHandleDataThread::CHandleDataThread(int _index)   
  1099. {   
  1100.     m_callstate=0;   
  1101.     m_currentprompt=0;   
  1102.     for(int i=0;i<MAX_PROMPT;I++) { int } return p_rc- CHandleDataThread::ExitInstance() TRUE; CHandleDataThread::InitInstance() BOOL CHandleDataThread::~CHandleDataThread() _beginthread(DispatherThread,0,this); m_index="_index;" m_eevent="CreateEvent(NULL,FALSE,FALSE,NULL);" sprintf(m_desc,?%d?,_index); m_playlist[i]="0;">CloseDB();   
  1103.     delete p_rc;   
  1104.     WaitForSingleObject(m_eevent,INFINITE);   
  1105.     CloseHandle(m_eevent);   
  1106.     SetEvent(m_endevents[m_index]);   
  1107.     return CWinThread::ExitInstance();   
  1108. }   
  1109.    
  1110. BEGIN_MESSAGE_MAP(CHandleDataThread, CWinThread)   
  1111.     //{{AFX_MSG_MAP(CHandleDataThread)    
  1112.         // NOTE - the ClassWizard will add and remove mapping macros here.    
  1113.     //}}AFX_MSG_MAP    
  1114.     ON_THREAD_MESSAGE(IVR_NOTIFY,OnXmlMsgEvent)   
  1115.     ON_THREAD_MESSAGE(WM_HANDLE_PACKAGE,HandleData)    
  1116.     ON_THREAD_MESSAGE(WM_CALL_CONNECT,OnCallConnect)   
  1117.     ON_THREAD_MESSAGE(WM_CALL_DISCONNECT,OnCallDisConnect)   
  1118.        
  1119. END_MESSAGE_MAP()   
  1120.    
  1121. /////////////////////////////////////////////////////////////////////////////    
  1122. // CHandleDataThread message handlers    
  1123. void CHandleDataThread::HandleData(WPARAM wPa,LPARAM lPa)   
  1124. {   
  1125.    
  1126. }   
  1127. void   CHandleDataThread::OnXmlMsgEvent(WPARAM wPa,LPARAM lPa)   
  1128. {   
  1129.     CString Msgstring;   
  1130.     NOTIFY_MESSAGE* p=(NOTIFY_MESSAGE*)lPa;   
  1131.     if(m_callstate==0||m_callstate==2)   
  1132.     {   
  1133.         if(p->command!=DEBUG_MESSAGE)   
  1134.         {          
  1135.             delete p;   
  1136.             TerminalBrowser();   
  1137.             return;   
  1138.         }else   
  1139.         {   
  1140.             ShowMsg(p->szData);   
  1141.             delete p;   
  1142.             TerminalBrowser();   
  1143.             return;   
  1144.         }   
  1145.     }   
  1146.     switch(p->command)   
  1147.     {   
  1148.     case DEBUG_MESSAGE:        
  1149.         {          
  1150.             ShowMsg(p->szData);             
  1151.             CString msgx=p->szData;   
  1152.             delete p;   
  1153.             if(msgx=="vxml thread quit...")   
  1154.             {   
  1155.                 p_rc->StateDisconnect();   
  1156.             }   
  1157.         }   
  1158.         break;   
  1159.            
  1160.     case OUTPUT_AUDIO:   
  1161.         {   
  1162.             Msgstring.Format("OUTPUT_AUDIO:%s",p->szData);   
  1163.             ShowMsg((char*)(LPCTSTR)Msgstring);   
  1164.             m_playlist[m_currentprompt]=(char*)malloc(strlen(p->szData)+1);   
  1165.             strcpy(m_playlist[m_currentprompt],p->szData);   
  1166.             m_currentprompt++;   
  1167.             delete p;   
  1168.             NOTIFY_MESSAGE* ps=new NOTIFY_MESSAGE;   
  1169.             ps->command=ACTION_COMPLETE;   
  1170.             MyPostThreadMessage(m_browserThreadId,IVR_NOTIFY,0,LPARAM(ps));   
  1171.         }   
  1172.         break;   
  1173.     case EDIT_WAV:     
  1174.         Msgstring.Format("EDIT_WAV:%s",p->szData);   
  1175.         ShowMsg((char*)(LPCTSTR)Msgstring);   
  1176.            
  1177.         break;   
  1178.     case ACTION_CHAT:   
  1179.         Msgstring.Format("ACTON_CHAT:%s",p->szData);   
  1180.         ShowMsg((char*)(LPCTSTR)Msgstring);   
  1181.            
  1182.         break;   
  1183.     case WAV_INFO:     
  1184.         Msgstring.Format("WAV_INFO:%s",p->szData);   
  1185.         ShowMsg((char*)(LPCTSTR)Msgstring);   
  1186.         break;   
  1187.    
  1188.     case ACTION_EXEC:   
  1189.         {   
  1190.             Msgstring.Format("ACTION_EXEC:%s",p->szData);   
  1191.             ShowMsg((char*)(LPCTSTR)Msgstring);   
  1192.             CString cmd=ParseParString(p->szData,"cmd");   
  1193.             CString params=ParseParString(p->szData,"params");   
  1194.                
  1195.             delete p;   
  1196.             cmd=cmd + " " + params;   
  1197.             int ret=(int)execmdline((char*)(LPCTSTR)cmd);   
  1198.    
  1199.             CString dStr;   
  1200.             dStr.Format("%d",ret);     
  1201.             NOTIFY_MESSAGE* ps=new NOTIFY_MESSAGE;   
  1202.             ps->command=ACTION_COMPLETE;   
  1203.             ps->szData=new char[dStr.GetLength()+1];   
  1204.             strcpy(ps->szData,dStr);   
  1205.             MyPostThreadMessage(m_browserThreadId,IVR_NOTIFY,0,LPARAM(ps));   
  1206.         }   
  1207.         break;   
  1208.     case ACTION_CVTWAVE:   
  1209.         {   
  1210.             Msgstring.Format("ACTION_CVTWAVE:%s",p->szData);   
  1211.             ShowMsg((char*)(LPCTSTR)Msgstring);   
  1212.             CString action=ParseParString(p->szData,"action");   
  1213.             CString src=ParseParString(p->szData,"src");   
  1214.             CString dest=ParseParString(p->szData,"dest");   
  1215.             delete p;   
  1216.             CString dStr="error";   
  1217.             action.MakeLower();   
  1218.            
  1219.             FILE* fp=NULL;   
  1220.             if((fp=fopen((char*)(LPCTSTR)src,"r"))!=NULL)   
  1221.             {   
  1222.                 fclose(fp);   
  1223.                 if(action=="p8k16b2a8k8b")   
  1224.                 {   
  1225.                 }          
  1226.                 else if(action=="a8k8b2p8k16b")   
  1227.                 {   
  1228.                 }          
  1229.                 dStr="success";   
  1230.             }   
  1231.             NOTIFY_MESSAGE* ps=new NOTIFY_MESSAGE;   
  1232.             ps->command=ACTION_COMPLETE;   
  1233.             ps->szData=new char[dStr.GetLength()+1];   
  1234.             strcpy(ps->szData,dStr);   
  1235.             MyPostThreadMessage(m_browserThreadId,IVR_NOTIFY,0,LPARAM(ps));   
  1236.    
  1237.         }   
  1238.         break;   
  1239.     case OUTPUT_CATWAV:    
  1240.         Msgstring.Format("OUTPUT_CATWAV:%s",p->szData);   
  1241.         ShowMsg((char*)(LPCTSTR)Msgstring);   
  1242.            
  1243.         break;   
  1244.     case OUTPUT_DISCONNECT:        
  1245.         ShowMsg("OUTPUT_DISCONNECT");   
  1246.         delete p;   
  1247.         PlayPrompt(true);   
  1248.         p_rc->StateDisconnect();   
  1249.         break;   
  1250.     case INPUT_DTMF_M:   
  1251.         {   
  1252.             Msgstring.Format("INPUT_DTMF_M:%s",p->szData);   
  1253.             ShowMsg((char*)(LPCTSTR)Msgstring);   
  1254.             Handle_DTMF_M(p);                  
  1255.         }   
  1256.            
  1257.         break;   
  1258.     case INPUT_ENROLL:   
  1259.         {   
  1260.             Msgstring.Format("INPUT_ENROLL:%s",p->szData);   
  1261.             ShowMsg((char*)(LPCTSTR)Msgstring);   
  1262.         }   
  1263.         break;   
  1264.     case COPY_DYNAGRAMMAR:   
  1265.         {   
  1266.             Msgstring.Format("COPY_DYNAGRAMMAR:%s",p->szData);   
  1267.             ShowMsg((char*)(LPCTSTR)Msgstring);    
  1268.         }   
  1269.         break;   
  1270.     case ACTION_DYNAGRAMMAR:   
  1271.         {   
  1272.             Msgstring.Format("ACTION_DYNAGRAMMAR:%s",p->szData);   
  1273.             ShowMsg((char*)(LPCTSTR)Msgstring);   
  1274.         }   
  1275.         break;   
  1276.     case INPUT_SPOKEN_RECORD:   
  1277.         {          
  1278.             Msgstring.Format("INPUT_SPOKEN_RECORD:%s",p->szData);   
  1279.             ShowMsg((char*)(LPCTSTR)Msgstring);    
  1280.             Hand_INPUT_SPOKEN_RECORD(p);   
  1281.         }   
  1282.         break;   
  1283.    
  1284.     case START_PROMPT:         
  1285.         {   
  1286.             delete p;   
  1287.             ShowMsg("START_PROMPT");   
  1288.             PlayPrompt(true);   
  1289.             NOTIFY_MESSAGE* ps=new NOTIFY_MESSAGE;   
  1290.             ps->command=ACTION_COMPLETE;   
  1291.             MyPostThreadMessage(m_browserThreadId,IVR_NOTIFY,0,LPARAM(ps));                        
  1292.         }   
  1293.         break;   
  1294.    
  1295.     case OUTPUT_DIALOUT:   
  1296.         Msgstring.Format("OUTPUT_DIALOUT:%s",p->szData);   
  1297.         ShowMsg((char*)(LPCTSTR)Msgstring);   
  1298.            
  1299.         break;   
  1300.     case OUTPUT_TRANSFER:   
  1301.         Msgstring.Format("OUTPUT_TRANSFER:%s",p->szData);   
  1302.         ShowMsg((char*)(LPCTSTR)Msgstring);   
  1303.         Hand_TRANSFER(p);   
  1304.         break;   
  1305.     case SEND_SMS:   
  1306.         Msgstring.Format("SEND_SMS:%s",p->szData);   
  1307.         ShowMsg((char*)(LPCTSTR)Msgstring);   
  1308.            
  1309.         break;   
  1310.     case EMAIL_SMTP:   
  1311.         Msgstring.Format("EMAIL_SMTP:%s",p->szData);   
  1312.         ShowMsg((char*)(LPCTSTR)Msgstring);        
  1313.         break;   
  1314.     case TTS_FILE:   
  1315.         Msgstring.Format("TTS:%s",p->szData);   
  1316.         ShowMsg((char*)(LPCTSTR)Msgstring);        
  1317.         break;   
  1318.     case INPUT_SR_M:   
  1319.         {   
  1320.             Msgstring.Format("INPUT_SR_M:%s",p->szData);   
  1321.             ShowMsg((char*)(LPCTSTR)Msgstring);        
  1322.             Handle_INPUT_SR_M(p);   
  1323.         }   
  1324.         break;   
  1325.     case INPUT_SRDTMF_M:   
  1326.         {          
  1327.             Msgstring.Format("INPUT_SRDTMF_M:%s",p->szData);   
  1328.             ShowMsg((char*)(LPCTSTR)Msgstring);    
  1329.             Hand_INPUT_SRDTMF_M(p);   
  1330.         }   
  1331.         break;   
  1332.     default:   
  1333.         delete p;   
  1334.         break;   
  1335.     }   
  1336. }   
  1337. void DispatherThread(void* lp)   
  1338. {   
  1339.     CHandleDataThread* p=(CHandleDataThread*)lp;   
  1340.     p->InitRc();   
  1341. }   
  1342. bool CHandleDataThread::InitRc()   
  1343. {   
  1344.     CString msg;   
  1345.     NuanceStatus status;   
  1346.     NuanceConfig * config =   
  1347.         NuanceConfigBuildFromCommandLine(&(config_params[m_index].argc), config_params[m_index].argv, 1, &status);   
  1348.     if ( (NULL == config) || (NUANCE_OK != status) ) {   
  1349.    
  1350.         msg.Format("T%d: NuanceConfigBuildFromCommandLine 失败",m_index);   
  1351.         ShowMsg(msg);   
  1352.         return 0;   
  1353.     }   
  1354.     int silent_flag = 1;   
  1355.     DBDescriptor * dynagram_db_desc =   
  1356.         NewDBDescriptorFromCommandLine(&(config_params[m_index].argc), config_params[m_index].argv, "", &silent_flag);   
  1357.     Dispatcher * d = new Dispatcher(config, status);   
  1358.     if (NUANCE_OK != status) {   
  1359.         msg.Format("T%d: Dispatcher create 失败",m_index);   
  1360.         ShowMsg(msg);   
  1361.         delete d;   
  1362.         NuanceConfigFree(config);   
  1363.         return 0;   
  1364.     }   
  1365.     p_rc=new CGrammarThread(*config,*d,dynagram_db_desc,this);   
  1366.     msg.Format("T%d: Dispatcher listening......",m_index);   
  1367.     ShowMsg(msg);   
  1368.     SetEvent(m_endevents[m_index]);   
  1369.     d->Dispatch();   
  1370.     delete d;   
  1371.     d = NULL;   
  1372.     NuanceConfigFree(config);   
  1373.     config = NULL;   
  1374.     msg.Format("T%d: Dispatcher Close......",m_index);   
  1375.     ShowMsg(msg);   
  1376.     msg.Empty();   
  1377.     SetEvent(m_eevent);   
  1378.     return true;   
  1379. }   
  1380.    
  1381. void sss(LPVOID lp)   
  1382. {   
  1383.     CHandleDataThread* p=(CHandleDataThread*)lp;   
  1384.     Sleep(10000);   
  1385.     p->p_rc->StopPrompt();   
  1386.    
  1387. }   
  1388. void CHandleDataThread::PlayPrompt(bool _wait)   
  1389. {      
  1390.     p_rc->StatePrompt(_wait);   
  1391. }   
  1392.    
  1393. void CHandleDataThread::CreateBrowser()   
  1394. {   
  1395.     CreateBrowserThread(m_homepage,m_nThreadID,&m_browserThreadId,m_callingID,m_calledID,m_desc,m_tempwavdir);   
  1396. }   
  1397. void  CHandleDataThread::OnCallConnect(WPARAM wPa,LPARAM lPa)   
  1398. {   
  1399.     RemoveAllWav();   
  1400.     CreateBrowser();   
  1401. }   
  1402.    
  1403. void CHandleDataThread::RemoveAllWav()   
  1404. {   
  1405.     for(int i=0;i<M_CURRENTPROMPT;I++) { int } m_playlist[i]="NULL;" len="ParseParInt(p-" *p) CHandleDataThread::Handle_DTMF_M(NOTIFY_MESSAGE void m_currentprompt="0;" ; free(m_playlist[i])>szData,"len");   
  1406.     int finalsilence=ParseParInt(p->szData,"finalsilence");   
  1407.     int firsttimeout=ParseParInt(p->szData,"firsttime");   
  1408.     char termdtmf=ParseParChar(p->szData,"termdtmf");   
  1409.     char sdtmf=ParseParChar(p->szData,"sdtmf");   
  1410.        
  1411.     if(finalsilence==0) finalsilence=4;   
  1412.     if(len==0) len=40;   
  1413.     delete p;   
  1414.     BYTE ret=p_rc->RunDtmf(len,firsttimeout,finalsilence,termdtmf,sdtmf);   
  1415.     if(ret==1)   
  1416.     {   
  1417.         ShowMsg(p_rc->m_dtmfbuf);   
  1418.         NOTIFY_MESSAGE* ps=new NOTIFY_MESSAGE;   
  1419.         ps->command=ACTION_COMPLETE;   
  1420.         ps->szData=new char[p_rc->m_dtmfcnt+1];   
  1421.         strcpy(ps->szData,p_rc->m_dtmfbuf);   
  1422.         MyPostThreadMessage(m_browserThreadId,IVR_NOTIFY,0,LPARAM(ps));   
  1423.     }   
  1424. }   
  1425.    
  1426. void CHandleDataThread::Handle_INPUT_SR_M(NOTIFY_MESSAGE *p)   
  1427. {   
  1428.     CString gkey=ParseParString(p->szData,"gkey");   
  1429.     BOOL _overwrite=ParseParBool(p->szData,"gover");   
  1430.     CString grammar=ParseParString(p->szData,"grammar");   
  1431.     WORD nbest=(WORD)ParseParInt(p->szData,"nbest");   
  1432.     if(nbest==0) nbest=1;   
  1433.        
  1434.     WORD nospeechtimeout=(WORD)ParseParInt(p->szData,"nospeechtimeout");   
  1435.     if(nospeechtimeout==0) nospeechtimeout=7;   
  1436.        
  1437.     WORD toomuchms=(WORD)ParseParInt(p->szData,"toomuchms");   
  1438.     if(toomuchms==0) toomuchms=60;   
  1439.        
  1440.     WORD tooslowms=(WORD)ParseParInt(p->szData,"tooslowms");   
  1441.     if(tooslowms==0) tooslowms=0;   
  1442.        
  1443.     WORD rejthreshold=(WORD)ParseParInt(p->szData,"rejthreshold");   
  1444.     if(rejthreshold==0) rejthreshold=38;   
  1445.        
  1446.     BOOL hot=(WORD)ParseParBool(p->szData,"hotword");   
  1447.     bool hotword=false;   
  1448.     if(hot) hotword=true;   
  1449.        
  1450.        
  1451.     delete p;   
  1452.     char r[100];   
  1453.     memset(r,0,100);       
  1454.     strcpy(r,"error");   
  1455.     BYTE ret;   
  1456.     if(grammar.Left(1)==".")   
  1457.         ret=p_rc->RecognizeUttr(hotword,nbest,  nospeechtimeout, toomuchms, tooslowms,  rejthreshold,r,NULL,(char*)(LPCTSTR)grammar,(_overwrite?true:false),((gkey.GetLength()>0)?((char*)(LPCTSTR)gkey):NULL),0,10,'#');   
  1458.     else   
  1459.         ret=p_rc->RecognizeUttr(hotword,nbest,  nospeechtimeout, toomuchms, tooslowms,  rejthreshold,r,(char*)(LPCTSTR)grammar,NULL,(_overwrite?true:false),((gkey.GetLength()>0)?((char*)(LPCTSTR)gkey):NULL),0,10,'#');   
  1460.     ShowMsg(r);   
  1461.     NOTIFY_MESSAGE* ps=new NOTIFY_MESSAGE;   
  1462.     ps->command=ACTION_COMPLETE;   
  1463.     ps->szData=new char[strlen(r)+1];   
  1464.     strcpy(ps->szData,r);   
  1465.     MyPostThreadMessage(m_browserThreadId,IVR_NOTIFY,0,LPARAM(ps));    
  1466. }   
  1467.    
  1468.    
  1469.    
  1470. void CHandleDataThread::Hand_INPUT_SRDTMF_M(NOTIFY_MESSAGE *p)   
  1471. {   
  1472.     CString gkey=ParseParString(p->szData,"gkey");   
  1473.     BOOL _overwrite=ParseParBool(p->szData,"gover");   
  1474.     CString grammar=ParseParString(p->szData,"grammar");   
  1475.     int len=ParseParInt(p->szData,"len");   
  1476.     int finalsilence=ParseParInt(p->szData,"finalsilence");   
  1477.     int firsttimeout=ParseParInt(p->szData,"firsttime");   
  1478.     char termdtmf=ParseParChar(p->szData,"termdtmf");   
  1479.     char sdtmf=ParseParChar(p->szData,"sdtmf");   
  1480.     if(finalsilence==0) finalsilence=4;   
  1481.     if(len==0) len=40;   
  1482.     WORD nbest=(WORD)ParseParInt(p->szData,"nbest");   
  1483.     if(nbest==0) nbest=1;   
  1484.        
  1485.     WORD nospeechtimeout=(WORD)ParseParInt(p->szData,"nospeechtimeout");   
  1486.     if(nospeechtimeout==0) nospeechtimeout=7;   
  1487.        
  1488.     WORD toomuchms=(WORD)ParseParInt(p->szData,"toomuchms");   
  1489.     if(toomuchms==0) toomuchms=60;   
  1490.        
  1491.     WORD tooslowms=(WORD)ParseParInt(p->szData,"tooslowms");   
  1492.     if(tooslowms==0) tooslowms=0;   
  1493.        
  1494.     WORD rejthreshold=(WORD)ParseParInt(p->szData,"rejthreshold");   
  1495.     if(rejthreshold==0) rejthreshold=38;   
  1496.        
  1497.     BOOL hot=(WORD)ParseParBool(p->szData,"hotword");   
  1498.     bool hotword=false;   
  1499.     if(hot) hotword=true;   
  1500.        
  1501.     delete p;   
  1502.     char r[100];   
  1503.     memset(r,0,100);       
  1504.     strcpy(r,"error");   
  1505.     BYTE ret;   
  1506.     if(grammar.Left(1)==".")   
  1507.         ret=p_rc->RecognizeUttr(hotword,nbest,  nospeechtimeout, toomuchms, tooslowms,  rejthreshold,r,NULL,(char*)(LPCTSTR)grammar,(_overwrite?true:false),((gkey.GetLength()>0)?((char*)(LPCTSTR)gkey):NULL),len,finalsilence,termdtmf,sdtmf);   
  1508.     else   
  1509.         ret=p_rc->RecognizeUttr(hotword,nbest,  nospeechtimeout, toomuchms, tooslowms,  rejthreshold,r,(char*)(LPCTSTR)grammar,NULL,(_overwrite?true:false),((gkey.GetLength()>0)?((char*)(LPCTSTR)gkey):NULL),len,finalsilence,termdtmf,sdtmf);   
  1510.        
  1511.     ShowMsg(r);   
  1512.    
  1513.     NOTIFY_MESSAGE* ps=new NOTIFY_MESSAGE;   
  1514.     ps->command=ACTION_COMPLETE;   
  1515.     ps->szData=new char[strlen(r)+1];   
  1516.     strcpy(ps->szData,r);   
  1517.     MyPostThreadMessage(m_browserThreadId,IVR_NOTIFY,0,LPARAM(ps));    
  1518. }   
  1519.        
  1520. void CHandleDataThread::OnCallDisConnect(WPARAM wPa,LPARAM lPa)   
  1521. {   
  1522.     printf("disconnected/r/n");   
  1523.     TerminalBrowser();   
  1524. }   
  1525. void CHandleDataThread::Hand_INPUT_SPOKEN_RECORD(NOTIFY_MESSAGE *p)   
  1526. {   
  1527.     int maxtime=ParseParInt(p->szData,"maxtime");   
  1528.     int iddtime=ParseParInt(p->szData,"finalsilence");   
  1529.     CString dtmf=ParseParString(p->szData,"dtmfterm");   
  1530.     BOOL beep=ParseParBool(p->szData,"beep");   
  1531.     CString type=ParseParString(p->szData,"type");   
  1532.     CString bgmusic=ParseParString(p->szData,"bgmusic");   
  1533.     int     mixtype=ParseParInt(p->szData,"mixtype");   
  1534.     int rtype=0;   
  1535.        
  1536.     if(dtmf=="") dtmf="a";   
  1537.     if(type=="audio/vox") rtype=1;   
  1538.     delete p;   
  1539.     PlayPrompt(true);   
  1540.     CString filename=GenuniqueRecFileName(rtype);   
  1541.     BYTE ret=p_rc->StateRecord((char*)(LPCTSTR)filename,beep,maxtime,iddtime,dtmf,rtype,bgmusic,mixtype);   
  1542.     if(ret!=1) filename="";   
  1543.     NOTIFY_MESSAGE* ps=new NOTIFY_MESSAGE;   
  1544.     ps->command=ACTION_COMPLETE;   
  1545.     ps->szData=new char[filename.GetLength()+1];   
  1546.     strcpy(ps->szData,filename);   
  1547.     MyPostThreadMessage(m_browserThreadId,IVR_NOTIFY,0,LPARAM(ps));    
  1548. }   
  1549.    
  1550. CString CHandleDataThread::GenuniqueRecFileName(int type)   
  1551. {   
  1552.     int index=0;   
  1553.     CString _filter="";   
  1554.     if(type==0) _filter="wav";   
  1555.     else    _filter="vox";   
  1556.        
  1557.     time_t _t=0;   
  1558.     time(&_t);   
  1559.     CString _newfile;   
  1560.     CString _tmpdir("e://app");   
  1561.     _tmpdir.TrimRight("//");   
  1562.     _newfile.Format("%s//rec%d%ld.%s",_tmpdir,m_index,_t,_filter);   
  1563.     CFileFind file;   
  1564.     while(file.FindFile(_newfile))   
  1565.     {   
  1566.         index+=1;   
  1567.         _newfile.Format("%s//rec%d%ld%d.%s",_tmpdir,m_index,_t,index,_filter);   
  1568.         file.Close ();   
  1569.     }   
  1570.     return _newfile;   
  1571. }   
  1572.    
  1573. void CHandleDataThread::Hand_TRANSFER(NOTIFY_MESSAGE *p)   
  1574. {   
  1575.        
  1576.     CString dest=ParseParString(p->szData,"dest");   
  1577.     BOOL bridge=ParseParBool(p->szData,"bridge");       
  1578.     int  chindex=ParseParInt(p->szData,"chindex");   
  1579.     int  delay=ParseParInt(p->szData,"delay");      
  1580.     CString src=ParseParString(p->szData,"src");   
  1581.    
  1582.     delete p;   
  1583.    
  1584.     if(!bridge)   
  1585.     {   
  1586.        
  1587.     }else   
  1588.     {          
  1589.            
  1590.     }   
  1591.     p_rc->StateTransfer((char*)(LPCTSTR)dest,(char*)(LPCTSTR)src);   
  1592. }   
  1593.    
  1594. void CHandleDataThread::TerminalBrowser()   
  1595. {   
  1596.     if(m_browserThreadId!=0)   
  1597.     {   
  1598.         ::PostThreadMessage(m_browserThreadId,WM_QUIT,0,0);   
  1599.         m_browserThreadId=0;   
  1600.     }   
  1601. }  

需要源码的留下Email

你可能感兴趣的:(例子)