#include <windows.h>
#include <atlstr.h>
#include <sphelper.h>
#include <sapi.h>
#pragma comment(lib,"sapi.lib")
#define GID_CMD_GR 333333
#define WM_RECOEVENT WM_USER+1
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
TCHAR szAppName[]=TEXT("Hello");
BOOL b_initSR;
BOOL b_Cmd_Grammar;
CComPtr<ISpRecoContext>m_cpRecoCtxt; //语音识别程序接口
CComPtr<ISpRecoGrammar>m_cpCmdGramma; //识别语法
CComPtr<ISpRecognizer>m_cpRecoEngine; //语音识别引擎
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,PSTR szCmdLine,int iCmdShow)
{
HWND hwnd;
MSG msg;
WNDCLASS wndclass;
wndclass.cbClsExtra =0;
wndclass.cbWndExtra =0;
wndclass.hbrBackground =(HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.hCursor =LoadCursor(NULL,IDC_ARROW);
wndclass.hIcon =LoadIcon(NULL,IDI_APPLICATION);
wndclass.hInstance =hInstance;
wndclass.lpfnWndProc =WndProc;
wndclass.lpszClassName =szAppName;
wndclass.lpszMenuName =NULL;
wndclass.style =CS_HREDRAW|CS_VREDRAW;
if(!RegisterClass(&wndclass))
{
MessageBox(NULL,TEXT("This program requires Windows NT!"),szAppName,MB_ICONERROR);
return 0;
}
hwnd=CreateWindow(szAppName,
TEXT("Windows Speech"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL);
ShowWindow(hwnd,iCmdShow);
UpdateWindow(hwnd);
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
switch(message)
{
case WM_CREATE:
{
//初始化COM端口
::CoInitializeEx(NULL,COINIT_APARTMENTTHREADED);
//创建识别引擎COM实例为共享型
HRESULT hr=m_cpRecoEngine.CoCreateInstance(CLSID_SpSharedRecognizer);
//创建识别上下文接口
if(SUCCEEDED(hr))
{
hr=m_cpRecoEngine->CreateRecoContext(&m_cpRecoCtxt);
}
else MessageBox(hwnd,TEXT("error1"),TEXT("error"),S_OK);
//设置识别消息,使计算机时刻监听语音消息
if(SUCCEEDED(hr))
{
hr=m_cpRecoCtxt->SetNotifyWindowMessage(hwnd,WM_RECOEVENT,0,0);
}
else MessageBox(hwnd,TEXT("error2"),TEXT("error"),S_OK);
//设置我们感兴趣的事件
if(SUCCEEDED(hr))
{
ULONGLONG ullMyEvents=SPFEI(SPEI_SOUND_START)|SPFEI(SPEI_RECOGNITION)|SPFEI(SPEI_SOUND_END);
hr=m_cpRecoCtxt->SetInterest(ullMyEvents,ullMyEvents);
}
else MessageBox(hwnd,TEXT("error3"),TEXT("error"),S_OK);
//创建语法规则
b_Cmd_Grammar=TRUE;
if(FAILED(hr))
{
MessageBox(hwnd,TEXT("error4"),TEXT("error"),S_OK);
}
hr=m_cpRecoCtxt->CreateGrammar(GID_CMD_GR,&m_cpCmdGramma);
WCHAR wszXMLFile[20]=L"";
MultiByteToWideChar(CP_ACP,0,(LPCSTR)"er.xml",-1,wszXMLFile,256);
hr=m_cpCmdGramma->LoadCmdFromFile(wszXMLFile,SPLO_DYNAMIC);
if(FAILED(hr))
{
MessageBox(hwnd,TEXT("error5"),TEXT("error"),S_OK);
}
b_initSR=TRUE;
//在开始识别时,激活语法进行识别
hr=m_cpCmdGramma->SetRuleState(NULL,NULL,SPRS_ACTIVE);
return 0;
}
case WM_RECOEVENT:
{
RECT rect;
GetClientRect(hwnd,&rect);
hdc=GetDC(hwnd);
USES_CONVERSION;
CSpEvent event;
while(event.GetFrom(m_cpRecoCtxt)==S_OK)
{
switch(event.eEventId)
{
case SPEI_RECOGNITION:
{
static const WCHAR wszUnrecognized[]=L"<Unrecognized>";
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(b_Cmd_Grammar)
{
if(Recstring=="下")
{
DrawText(hdc,TEXT("hello"),-1,&rect,DT_SINGLELINE|DT_CENTER|DT_VCENTER);
}
if(Recstring=="上")
{
DrawText(hdc,TEXT("ho"),-1,&rect,DT_SINGLELINE|DT_CENTER|DT_VCENTER);
}
}
}
}
}
return TRUE;
}
case WM_PAINT:
hdc=BeginPaint(hwnd,&ps);
EndPaint(hwnd,&ps);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd,message,wParam,lParam);
}
Windows语音识别
5星 · 超过95%的资源 需积分: 50 174 浏览量
2013-01-03
22:36:39
上传
评论 8
收藏 2KB ZIP 举报
Mr-LiangYizhao
- 粉丝: 23
- 资源: 11
最新资源
- 功率拓扑快速参考指南-ti,TI官方出品
- 开关电源拓朴图表,各种电路拓扑表格
- 登录和注册 前端:vue3+iview plus +axios 后台:spring boot +mybatis
- 软件测试入门简介:从基础到实践的全面介绍
- 2024CDA Level Ⅰ 认证考试大纲
- YOLO 数据集:淋巴病灶检测(1类别,包含训练集、验证集)
- 基于AT91RM9200处理器+XC3S250E(FPGA)+AD5322采集板Cadence ORCAD(硬件原理图+PCB)
- java+vue2实现zebra打印机,js实现
- J-link烧录软件,用于hex文件烧录
- VB打开时间同步页面/Internet时间设置.vbs
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
- 1
- 2
- 3
前往页