• win7语音识别开发(sapi)


    参考:http://msdn.microsoft.com/en-us/library/ee125663(v=vs.85).aspx    (sapi5.4 reference)

    http://msdn.microsoft.com/zh-cn/library/ms723634    Grammar Format Tags (SAPI 5.3)
    http://blog.csdn.net/zhubenfulovepoem/article/details/6803505  语音控制

    http://hi.baidu.com/bxybao/item/693fc8098aa36c17acdc704f  sapi5.1介绍

    开发步骤:

    1 sapi 是基于com的接口,所以应用程序开发需要遵循com调用规则

    [cpp] view plaincopy在CODE上查看代码片派生到我的代码片
     
    1. hr = ::CoInitialize(NULL);  
    2.   
    3. .........  
    4.   
    5. ::CoUninitialize();  



    2 sapi 语音识别主要接口

    (1)   语音识别引擎(ISpRecognizer)接口:用于创建语音识别引擎的实例。语音识别引擎对象有两种:独占(InProcRecognizer)的引擎和共享(SharedRecognizer)的引擎。独占的引擎对象只能由创建的应用程序使用,而共享的引擎可以

    供多个应用程序共同使用。

    (2)   语音识别上下文(ISpRecoContext)接口:主要用于发送和接收与语音识别相关的消息通知,创建语法规则对象。

    (3)   语法规则(ISpRecoGrammar)接口:定义引擎需要识别的具体内容,用于创建、载入和激活识别用的语法规则。而语法规则定义了期望识别的单词、短语和句子,通常有两种语法规则:听写语法(DictationGrammer)和命令控制语法(Command and Control Grammer)。命令控制语法主要用于识别用户在语法文件里自定义的一些特定的命令词汇和句子,这些语法规则以XML文件的格式编写,通过(ISpRecoGrammar)接口载入,并激活。

    (4)   识别结果(ISpPhrase)接口:用于获取识别的结果,包括识别的文字,识别的语法规则等。

    (5)   语音合成(ISpVoice)接口:主要功能是通过访问TTS引擎实现文本到语音的转换,从而使电脑会说话。

    [cpp] view plaincopy在CODE上查看代码片派生到我的代码片
     
    1. CComPtr<ISpRecoContext> cpRecoCtxt;   //语音识别上下文接口  
    2. CComPtr<ISpRecoGrammar> cpGrammar;    //语法规则接口  
    3. CComPtr<ISpVoice> cpVoice;            //<span style="font-family: Arial; font-size: 14px; line-height: 26px;">语音合成接口 <span style="font-family: Arial; font-size: 14px; line-height: 26px;">主要功能是通过访问TTS引擎实现文本到语音的转换,从而使电脑会说话。</span></span>  
    4. CComPtr<ISpRecognizer> cpRecognizer;  // 语音识别引擎  
    5. CComPtr<ISpAudio> m_pAudio;           // 创建进程内语音识别引擎需要的音频接口  
    [cpp] view plaincopy在CODE上查看代码片派生到我的代码片
     
    1. CComPtr<ISpRecoResult>          

                     

    3 example

    [cpp] view plaincopy在CODE上查看代码片派生到我的代码片
     
    1. // cpp_Aes.cpp : 定义控制台应用程序的入口点。  
    2. //  
    3.   
    4. #include "stdafx.h"  
    5. /* 
    6. #include "aes.h" 
    7. #include <string.h> 
    8.  
    9. using namespace std; 
    10. void AES_cbc_encrypt(const unsigned char *in, unsigned char *out, 
    11.     size_t len, const AES_KEY *key, 
    12.     unsigned char *ivec, const int enc)  
    13. int _tmain(int argc, _TCHAR* argv[]) 
    14. { 
    15.     unsigned char iv[16] ; 
    16.     strncpy((char*)iv,"0102030405060708",16); 
    17.     const  char* intext = "http://www.baidu.com"; 
    18.     AES_KEY key ={0}; 
    19.     AES_set_encrypt_key((unsigned char*)"0102030405060708",128,&key); 
    20.     unsigned char out[1024] ={0};  
    21.     unsigned char in[1024] ={0}; 
    22.     memset(in,0x0c,1024); 
    23.     memcpy(in,intext,20); 
    24.     AES_cbc_encrypt((unsigned char*)in,out,32,&key,iv,1); 
    25.  
    26.     for (int i=0; i < 32 ;i ++) 
    27.     { 
    28.         printf("%02X ", out[i]); 
    29.     } 
    30.     return 0; 
    31.  
    32.  
    33. } 
    34. */  
    35. #include <windows.h>  
    36. #include <sapi.h>  
    37. #include <stdio.h>  
    38. #include <string.h>  
    39. #include <atlbase.h>  
    40. #include "sphelper.h"  
    41. //Copyright (c) Microsoft Corporation. All rights reserved.  
    42.   
    43. inline HRESULT BlockForResult(ISpRecoContext * pRecoCtxt, ISpRecoResult ** ppResult)  
    44. {  
    45.     HRESULT hr = S_OK;  
    46.     CSpEvent event;  
    47.   
    48.     while (SUCCEEDED(hr) &&  
    49.         SUCCEEDED(hr = event.GetFrom(pRecoCtxt)) &&  
    50.         hr == S_FALSE)  
    51.     {  
    52.         hr = pRecoCtxt->WaitForNotifyEvent(INFINITE);  
    53.     }  
    54.   
    55.     *ppResult = event.RecoResult();  
    56.     if (*ppResult)  
    57.     {  
    58.         (*ppResult)->AddRef();  
    59.     }  
    60.   
    61.     return hr;  
    62. }  
    63.   
    64. const WCHAR * StopWord()  
    65. {  
    66.     const WCHAR * pchStop;  
    67.   
    68.     LANGID LangId = ::SpGetUserDefaultUILanguage();  
    69.   
    70.     switch (LangId)  
    71.     {  
    72.     case MAKELANGID(LANG_JAPANESE, SUBLANG_DEFAULT):  
    73.     //case MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT):  
    74.         pchStop = L"}42N86/0b70e50fc0ea0e70fc/05708504608a087046";;  
    75.         break;  
    76.   
    77.     default:  
    78.         pchStop = L"Stop";  
    79.         break;  
    80.     }  
    81.   
    82.     return pchStop;  
    83. }  
    84.   
    85.   
    86.   
    87. void __stdcall SPNOTIFYCALLBACK1(WPARAM wParam, LPARAM lParam)  
    88. {  
    89.     int x = 0;  
    90.   
    91.     return ;  
    92. }  
    93.   
    94. int main(int argc, char* argv[])  
    95. {  
    96.     HRESULT hr = E_FAIL;  
    97.     bool fUseTTS = true;            // turn TTS play back on or off  
    98.     bool fReplay = true;            // turn Audio replay on or off  
    99.   
    100.     // Process optional arguments  
    101.     if (argc > 1)  
    102.     {  
    103.         int i;  
    104.   
    105.         for (i = 1; i < argc; i++)  
    106.         {  
    107.             if (_stricmp(argv[i], "-noTTS") == 0)  
    108.             {  
    109.                 fUseTTS = false;  
    110.                 continue;  
    111.             }  
    112.             if (_stricmp(argv[i], "-noReplay") == 0)  
    113.             {  
    114.                 fReplay = false;  
    115.                 continue;  
    116.             }         
    117.             printf ("Usage: %s [-noTTS] [-noReplay]  ", argv[0]);  
    118.             return hr;  
    119.         }  
    120.     }  
    121.   
    122.     if (SUCCEEDED(hr = ::CoInitialize(NULL)))  
    123.     {  
    124.         {  
    125.             CComPtr<ISpRecoContext> cpRecoCtxt;  
    126.             CComPtr<ISpRecoGrammar> cpGrammar;  
    127.             CComPtr<ISpVoice> cpVoice;  
    128.             CComPtr<ISpRecognizer> cpRecognizer;  
    129.             CComPtr<ISpAudio> m_pAudio;  
    130.   
    131.             //hr = cpRecoCtxt.CoCreateInstance(CLSID_SpSharedRecoContext);  
    132.             hr = cpRecoCtxt.CoCreateInstance(CLSID_SpInProcRecoContext);  
    133.               
    134.             if(SUCCEEDED(hr))  
    135.             {  
    136.                 hr = cpRecoCtxt->GetVoice(&cpVoice);  
    137.             }  
    138.             hr = cpRecoCtxt->GetRecognizer(&cpRecognizer);  
    139.   
    140.             hr = SpCreateDefaultObjectFromCategoryId(SPCAT_AUDIOIN,&m_pAudio);  
    141.             hr = cpRecognizer->SetInput(m_pAudio,TRUE);  
    142.               
    143.             cpRecognizer->SetRecoState(SPRST_ACTIVE);  
    144.   
    145.             hr = cpRecoCtxt->SetNotifyWin32Event();  
    146.             hr = cpRecoCtxt->SetInterest(SPFEI(SPEI_RECOGNITION), SPFEI(SPEI_RECOGNITION));  
    147.             hr = cpRecoCtxt->SetAudioOptions(SPAO_RETAIN_AUDIO, NULL, NULL);  
    148.             hr = cpRecoCtxt->CreateGrammar(0, &cpGrammar);  
    149.             hr = cpGrammar->LoadCmdFromFile(L"cmd.xml",SPLO_DYNAMIC);  
    150.             int err = FAILED(hr);  
    151.             hr = cpGrammar->SetRuleState( NULL,NULL,SPRS_ACTIVE );  
    152.   
    153.             //hr = cpRecoCtxt->SetNotifyCallbackFunction(SPNOTIFYCALLBACK,)  
    154.   
    155.   
    156.             /*if (cpRecoCtxt && cpVoice && 
    157.                 SUCCEEDED(hr = cpRecoCtxt->SetNotifyWin32Event()) && 
    158.                 SUCCEEDED(hr = cpRecoCtxt->SetInterest(SPFEI(SPEI_RECOGNITION), SPFEI(SPEI_RECOGNITION))) && 
    159.                 SUCCEEDED(hr = cpRecoCtxt->SetAudioOptions(SPAO_RETAIN_AUDIO, NULL, NULL)) && 
    160.                 SUCCEEDED(hr = cpRecoCtxt->CreateGrammar(0, &cpGrammar)) && 
    161.                 SUCCEEDED(hr = cpGrammar->LoadCmdFromFile(L"cmd.xml",SPLO_DYNAMIC)) && 
    162.                 SUCCEEDED(hr = cpGrammar->SetRuleState( NULL,NULL,SPRS_ACTIVE )))*/  
    163.                 //SUCCEEDED(hr = cpRecoCtxt->CreateGrammar(0, &cpGrammar)) &&  
    164.                 //SUCCEEDED(hr = cpGrammar->LoadDictation(NULL, SPLO_STATIC)) &&  
    165.                 //SUCCEEDED(hr = cpGrammar->SetDictationState(SPRS_ACTIVE)))  
    166.             {  
    167.                 USES_CONVERSION;  
    168.   
    169.                 const WCHAR * const pchStop = StopWord();  
    170.                 CComPtr<ISpRecoResult> cpResult;  
    171.                   
    172.                   
    173.   
    174.                 printf( "I will repeat everything you say. Say " %s " to exit. ", W2A(pchStop) );  
    175.                 while(true)  
    176.                 //while (SUCCEEDED(hr = BlockForResult(cpRecoCtxt, &cpResult)))  
    177.                 {     
    178.                     //cpGrammar->SetDictationState( SPRS_INACTIVE );  
    179.                     hr = cpRecoCtxt->WaitForNotifyEvent(INFINITE);  
    180.                     cpGrammar->SetRuleState( NULL,NULL,SPRS_INACTIVE );  
    181.                     CSpDynamicString dstrText;                    
    182.                     hr = BlockForResult(cpRecoCtxt, &cpResult);  
    183.                     if (SUCCEEDED(cpResult->GetText(SP_GETWHOLEPHRASE, SP_GETWHOLEPHRASE,   
    184.                         TRUE, &dstrText, NULL)))  
    185.                     {  
    186.                         printf("I heard:  %s ", W2A(dstrText));  
    187.   
    188.                         if (fUseTTS)  
    189.                         {  
    190.                             cpVoice->Speak( L"I heard", SPF_ASYNC, NULL);  
    191.                             cpVoice->Speak( dstrText, SPF_ASYNC, NULL );  
    192.                         }  
    193.   
    194.                         if (fReplay)  
    195.                         {  
    196.                             if (fUseTTS)  
    197.                                 cpVoice->Speak( L"when you said", SPF_ASYNC, NULL);  
    198.                             else  
    199.                                 printf (" when you said... ");  
    200.                             cpResult->SpeakAudio(NULL, 0, NULL, NULL);  
    201.                         }  
    202.   
    203.                         cpResult.Release();  
    204.                     }  
    205.                     if (_wcsicmp(dstrText, pchStop) == 0)  
    206.                     {  
    207.                         break;  
    208.                     }  
    209.                     //cpGrammar->SetDictationState( SPRS_ACTIVE );  
    210.                     cpGrammar->SetRuleState( NULL,NULL,SPRS_ACTIVE );  
    211.                       
    212.                 }   
    213.             }  
    214.         }  
    215.         ::CoUninitialize();  
    216.     }  
    217.     return hr;  
    218. }  


    4 配置文件

    [html] view plaincopy在CODE上查看代码片派生到我的代码片
     
      1. <GRAMMAR>  
      2.         <DEFINE>  
      3.             <ID NAME="TheNumberFive" VAL="5"/>  
      4.         </DEFINE>  
      5.   
      6.         <!-- Note that the ID takes a number, which is actually "5" -->  
      7.         <RULE ID="TheNumberFive" TOPLEVEL="ACTIVE">  
      8.             <List>  
      9.             <P>打开灯源</P>  
      10.                         <P>关闭灯源</P>  
      11.             <P>开一号灯</P>  
      12.                         <P>开二号灯</P>  
      13.                         <P>关闭一号灯</P>  
      14.                         <P>增亮一号灯</P>  
      15.                         <P>全部关闭</P>  
      16.                         <P>打开厨房灯</P>  
      17.                           
      18.                       </List>  
      19.         </RULE>  
      20.     </GRAMMAR>  

    http://blog.csdn.net/xxq123321/article/details/17262935

  • 相关阅读:
    java 如何判断邮箱是否正确
    Android SDK Manager无法更新的解决
    洛谷P1162
    真 随笔
    初始化结构体
    Linux mkdir -p 后出现permission denied问题
    校赛F
    HDU1022
    UVa156
    HDU1060
  • 原文地址:https://www.cnblogs.com/pengkunfan/p/3523273.html
Copyright © 2020-2023  润新知