需要配置:vs/VC++6.0 博主安装的是VS2013
一、下载开发包
官网地址:http://www.microsoft.com/en-us/download/details.aspx?id=10121
下载三个关键的程序:SpeechSDK51.exe 、 msttss22L.exe、SpeechSDK51LangPack.exe
二、安装SDK
解压SpeechSDK51.exe 、SpeechSDK51LangPack.exe,分别安装,记住SpeechSDK51.exe安装目录,博主安装在了默认路径下;安装msttss22L.exe
三、配置环境(以VS为例)
在应用SDK的开发前当然得需要对工程环境进行配置,配置的过程如下:
属性–配置属性–C/C++–常规–附加包含目录,添加一项C:\Program Files\Microsoft Speech SDK 5.1\Include到目录中去。
再选择"库目录"项,添加一项C:\Program Files\Microsoft Speech SDK 5.1\Lib\i386到目录中去.
四、新建一个Win32 console Application工程
添加一个cpp源文件
一个简单的语音播报程序:
#include //语音头文件
#include //C++头文件,用来提示错误信息
int main()
{
::CoInitialize(NULL);//初始化语音环境
ISpVoice * pSpVoice = NULL;//初始化语音变量
if (FAILED(CoCreateInstance(CLSID_SpVoice, NULL,CLSCTX_INPROC_SERVER, IID_ISpVoice, (void **)&pSpVoice)))
//给语音变量创建环境,相当于创建语音变量,FAILED是个宏定义,就是来判断CoCreateInstance这个函数又没有成功创建语音变量,下面是不成功的提示信息。
{
std::cout << "Failed to create instance of ISpVoice!" << std::endl;
return -1;
}
pSpVoice->Speak(L"Hello World!", SPF_DEFAULT, NULL);//执行语音变量的Speek函数,这个函数用来读文字。
pSpVoice->Release(); //释放语音变量
::CoUninitialize();//释放语音环境
return 0;
}
出现问题:error C4996: ‘GetVersionExW': 被声明为已否决
解决方法:
方法一. Project Properties > Configuration Properties > C/C++ > General > SDL checks关掉
方法二. #pragma warning(disable: 4996)
方法三. /wd 4996
五、语音识别代码
语音识别接口可分为文字转语音和语音转文字
1、文字转语音
需要添加的头文件:
#include
#pragma comment(lib,"sapi.lib")
函数:
void CBodyBasics::MSSSpeak(LPCTSTR speakContent)
{
ISpVoice *pVoice = NULL;
if (FAILED(::CoInitialize(NULL)))
MessageBox(NULL, (LPCWSTR)L"COM接口初始化失败!", (LPCWSTR)L"提示", MB_ICONWARNING | MB_CANCELTRYCONTINUE | MB_DEFBUTTON2);
HRESULT hr = CoCreateInstance(CLSID_SpVoice, NULL, CLSCTX_ALL, IID_ISpVoice, (void**)&pVoice);
if (SUCCEEDED(hr))
{
pVoice->SetVolume((USHORT)100);
pVoice->SetRate(2);
hr = pVoice->Speak(speakContent, 0, NULL);
pVoice->Release();
pVoice = NULL;
}
::CoUninitialize();
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
2、语音转文字
需要实时监控麦克风,涉及到windows的消息机制。
(1)首先设置工程属性:
属性–配置属性–C/C++–预处理器–预处理器定义:_WIN32_DCOM;
(2)需要添加的头文件:
#include
#pragma comment(lib,"sapi.lib")
#include
#include
#pragma once
const int WM_RECORD = WM_USER + 100;
(3)在程序的.h头文件中定义变量
CComPtrm_cpRecoEngine;
CComPtrm_cpRecoCtxt;
CComPtrm_cpCmdGrammar;
CComPtrm_cpInputStream;
CComPtrm_cpToken;
CComPtrm_cpAudio;
ULONGLONG ullGrammerID;
(4)创建语音识别初始化函数(程序刚开始执行的时候调用,例如文末示例代码中,将此初始化函数放在对话框初始化消息WM_INITDIALOG的响应代码里)
void CBodyBasics::MSSListen()
{
if (FAILED(::CoInitialize(NULL)))
MessageBox(NULL, (LPCWSTR)L"COM接口初始化失败!", (LPCWSTR)L"提示", MB_ICONWARNING | MB_CANCELTRYCONTINUE | MB_DEFBUTTON2);
HRESULT hr = m_cpRecoEngine.CoCreateInstance(CLSID_SpSharedRecognizer);
if (SUCCEEDED(hr))
{
hr = m_cpRecoEngine->CreateRecoContext(&m_cpRecoCtxt);
hr = m_cpRecoCtxt->SetNotifyWindowMessage(m_hWnd, WM_RECORD, 0, 0);
const ULONGLONG ullInterest = SPFEI(SPEI_SOUND_START) | SPFEI(SPEI_SOUND_END) | SPFEI(SPEI_RECOGNITION);
hr = m_cpRecoCtxt->SetInterest(ullInterest, ullInterest);
hr = SpCreateDefaultObjectFromCategoryId(SPCAT_AUDIOIN, &m_cpAudio);
m_cpRecoEngine->SetInput(m_cpAudio, true);
ullGrammerID = 1000;
hr = m_cpRecoCtxt->CreateGrammar(ullGrammerID, &m_cpCmdGrammar);
WCHAR wszXMLFile[20] = L"";
MultiByteToWideChar(CP_ACP, 0, (LPCSTR)"CmdCtrl.xml", -1, wszXMLFile, 256);
hr = m_cpCmdGrammar->LoadCmdFromFile(wszXMLFile, SPLO_DYNAMIC);
hr = m_cpCmdGrammar->SetRuleState(NULL, NULL, SPRS_ACTIVE);
hr = m_cpRecoEngine->SetRecoState(SPRST_ACTIVE);
}
else
{
MessageBox(NULL, (LPCWSTR)L"语音识别引擎启动出错!", (LPCWSTR)L"警告", MB_OK);
exit(0);
}
::CoUninitialize();
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
(5)定义消息处理函数
需要和其他的消息处理代码放在一起,如本文代码中,放在文末示例代码的DlgProc()函数尾部。本文整个其他的代码块都可以直接照搬,只需要更改如下的消息反应模块即可
USES_CONVERSION;
CSpEvent event;
if (m_cpRecoCtxt)
{
while (event.GetFrom(m_cpRecoCtxt) == S_OK){
switch (event.eEventId)
{
case SPEI_RECOGNITION:
{
m_bGotReco = TRUE;
static const WCHAR wszUnrecognized[] = L"";
CSpDynamicString dstrText;
if (FAILED(event.RecoResult()->GetText(SP_GETWHOLEPHRASE, SP_GETWHOLEPHRASE, TRUE, &dstrText, NULL)))
{
dstrText = wszUnrecognized;
}
BSTR SRout;
dstrText.CopyToBSTR(&SRout);
CString Recstring;
Recstring.Empty();
Recstring = SRout;
if (Recstring == "发短信")
{
MSSSpeak(LPCTSTR(_T("好,马上发短信!")));
}
else if (Recstring == "李雷")
{
MSSSpeak(LPCTSTR(_T("好久没看见他了,真是 long time no see")));
}
}
break;
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
(6)修改语法文件
修改CmdCtrl.xml文件,可以提高某些词汇的识别度,对里面的词识别效果会很好多,如人名等。(此外,单独运行exe时也需要将此文件和exe放在同一文件夹内,不放也不会报错,只是语法文件里的词汇识别效果变差)
<GRAMMAR LANGID="804">
<DEFINE>
<ID NAME="VID_SubName1" VAL="4001"/>
<ID NAME="VID_SubName2" VAL="4002"/>
<ID NAME="VID_SubName3" VAL="4003"/>
<ID NAME="VID_SubName4" VAL="4004"/>
<ID NAME="VID_SubName5" VAL="4005"/>
<ID NAME="VID_SubName6" VAL="4006"/>
<ID NAME="VID_SubName7" VAL="4007"/>
<ID NAME="VID_SubName8" VAL="4008"/>
<ID NAME="VID_SubName9" VAL="4009"/>
<ID NAME="VID_SubNameRule" VAL="3001"/>
<ID NAME="VID_TopLevelRule" VAL="3000"/>
DEFINE>
<RULE ID="VID_TopLevelRule" TOPLEVEL="ACTIVE">
<O>
<L>
<P>我要P>
<P>运行P>
<P>执行P>
L>
O>
<RULEREF REFID="VID_SubNameRule" />
RULE>
<RULE ID="VID_SubNameRule" >
<L PROPID="VID_SubNameRule">
<P VAL="VID_SubName1">发短信P>
<P VAL="VID_SubName2">是的P>
<P VAL="VID_SubName3">好的P>
<P VAL="VID_SubName4">不用P>
<P VAL="VID_SubName5">李雷P>
<P VAL="VID_SubName6">韩梅梅P>
<P VAL="VID_SubName7">中文界面P>
<P VAL="VID_SubName8">英文界面P>
<P VAL="VID_SubName9">EnglishP>
L>
RULE>
GRAMMAR>
参考资料:
http://jingyan.baidu.com/article/fb48e8be58421c6e622e14af.html
http://blog.csdn.net/pamchen/article/details/7856207
http://blog.csdn.net/michaelliang12/article/details/51317531