您的当前位置:首页语音识别代码

语音识别代码

2024-08-16 来源:乌哈旅游


/ SpeechRecognitionDlg.cpp : implementation file

//

#include \"stdafx.h\"

#include \"SpeechRecognition.h\"

#include \"SpeechRecognitionDlg.h\"

#include \"math.h\"

#include \"DlgSet.h\"

#include

#include

#include \"sphelper.h\"

#pragma warning (disable:4244)

#ifdef _DEBUG

#define new DEBUG_NEW

#undef THIS_FILE

static char THIS_FILE[] = __FILE__;

#endif

CComPtr cpRecoCtxt;

CComPtr cpGrammar;

CComPtr cpVoice;

HRESULT hr = E_FAIL;

inline HRESULT BlockForResult(ISpRecoContext * pRecoCtxt, ISpRecoResult ** ppResult)

{

HRESULT hr = S_OK;

CSpEvent event;

while (SUCCEEDED(hr) &&

SUCCEEDED(hr = event.GetFrom(pRecoCtxt)) &&

hr == S_FALSE)

{

hr = pRecoCtxt->WaitForNotifyEvent(INFINITE);

}

*ppResult = event.RecoResult();

if (*ppResult)

{

(*ppResult)->AddRef();

}

return hr;

}

/////////////////////////////////////////////////////////////////////////////

// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog

{

public:

CAboutDlg();

// Dialog Data

//{{AFX_DATA(CAboutDlg)

enum { IDD = IDD_ABOUTBOX };

//}}AFX_DATA

// ClassWizard generated virtual function overrides

//{{AFX_VIRTUAL(CAboutDlg)

protected:

virtual void DoDataExchange(CDataExchange* pDX); //}}AFX_VIRTUAL

// DDX/DDV support

// Implementation

protected:

//{{AFX_MSG(CAboutDlg)

//}}AFX_MSG

DECLARE_MESSAGE_MAP()

};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)

{

//{{AFX_DATA_INIT(CAboutDlg)

//}}AFX_DATA_INIT

}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)

{

CDialog::DoDataExchange(pDX);

//{{AFX_DATA_MAP(CAboutDlg)

//}}AFX_DATA_MAP

}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)

//{{AFX_MSG_MAP(CAboutDlg)

// No message handlers

//}}AFX_MSG_MAP

END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////

// CSpeechRecognitionDlg dialog

CSpeechRecognitionDlg::CSpeechRecognitionDlg(CWnd* pParent /*=NULL*/)

: CDialog(CSpeechRecognitionDlg::IDD, pParent)

{

//{{AFX_DATA_INIT(CSpeechRecognitionDlg)

// NOTE: the ClassWizard will add member initialization here

//}}AFX_DATA_INIT

// Note that LoadIcon does not require a subsequent DestroyIcon in Win32

m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);

}

void CSpeechRecognitionDlg::DoDataExchange(CDataExchange* pDX)

{

CDialog::DoDataExchange(pDX);

//{{AFX_DATA_MAP(CSpeechRecognitionDlg)

// NOTE: the ClassWizard will add DDX and DDV calls here

//}}AFX_DATA_MAP

}

BEGIN_MESSAGE_MAP(CSpeechRecognitionDlg, CDialog)

//{{AFX_MSG_MAP(CSpeechRecognitionDlg)

ON_WM_SYSCOMMAND()

ON_WM_PAINT()

ON_WM_QUERYDRAGICON()

ON_WM_CLOSE()

ON_BN_CLICKED(IDC_BTN_INFO, OnBtnInfo)

ON_BN_CLICKED(IDC_BTN_SET, OnBtnSet)

//}}AFX_MSG_MAP

END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////

// CSpeechRecognitionDlg message handlers

BOOL CSpeechRecognitionDlg::OnInitDialog()

{

CDialog::OnInitDialog();

// Add \"About...\" menu item to system menu.

// IDM_ABOUTBOX must be in the system command range.

ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);

ASSERT(IDM_ABOUTBOX < 0xF000);

CMenu* pSysMenu = GetSystemMenu(FALSE);

if (pSysMenu != NULL)

{

CString strAboutMenu;

strAboutMenu.LoadString(IDS_ABOUTBOX);

if (!strAboutMenu.IsEmpty())

{

pSysMenu->AppendMenu(MF_SEPARATOR);

pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);

}

}

// Set the icon for this dialog. The framework does this automatically

// when the application's main window is not a dialog

SetIcon(m_hIcon, TRUE); // Set big icon

SetIcon(m_hIcon, FALSE); // Set small icon

// TODO: Add extra initialization here

//CPoint pt,pt_UP,pt_DOWN,pt_RIGHT,pt_LEFT;

flag=FALSE;

pt_Start.x=280;

pt_Start.y=130;

pt_End=pt_Start;

pt_UP.x=pt_Start.x;

pt_UP.y=20;

pt_DOWN.x=pt_Start.x;

pt_DOWN.y=240;

pt_RIGHT.x=390;

pt_RIGHT.y=pt_Start.y;

pt_LEFT.x=170;

pt_LEFT.y=pt_Start.y;

strUp=\"上边\";

strDown=\"下边\";

strRight=\"右边\";

strLeft=\"左边\";

hr = cpRecoCtxt.CoCreateInstance(CLSID_SpSharedRecoContext);

hr = cpRecoCtxt->GetVoice(&cpVoice);

hThread=CreateThread(NULL,0,RecvProc,NULL,0,NULL);

return TRUE; // return TRUE unless you set the focus to a control

}

void CSpeechRecognitionDlg::OnSysCommand(UINT nID, LPARAM lParam)

{

if ((nID & 0xFFF0) == IDM_ABOUTBOX)

{

CAboutDlg dlgAbout;

dlgAbout.DoModal();

}

else

{

CDialog::OnSysCommand(nID, lParam);

}

}

// If you add a minimize button to your dialog, you will need the code below

// to draw the icon. For MFC applications using the document/view model,

// this is automatically done for you by the framework.

void CSpeechRecognitionDlg::OnPaint()

{

if (IsIconic())

{

CPaintDC dc(this); // device context for painting

SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

// Center icon in client rectangle

int cxIcon = GetSystemMetrics(SM_CXICON);

int cyIcon = GetSystemMetrics(SM_CYICON);

CRect rect;

GetClientRect(&rect);

int x = (rect.Width() - cxIcon + 1) / 2;

int y = (rect.Height() - cyIcon + 1) / 2;

// Draw the icon

dc.DrawIcon(x, y, m_hIcon);

}

else

{

CPaintDC dc(this);

if(flag)DrawArrow(pt_Start,pt_End,20,20);

CDialog::OnPaint();

}

}

// The system calls this to obtain the cursor to display while the user drags

// the minimized window.

HCURSOR CSpeechRecognitionDlg::OnQueryDragIcon()

{

return (HCURSOR) m_hIcon;

}

void CSpeechRecognitionDlg::OnClose()

{

// TODO: Add your message handler code here and/or call default

//CloseHandle(hThread);

//CoUninitialize();

CDialog::OnClose();

}

DWORD WINAPI CSpeechRecognitionDlg::RecvProc(LPVOID lpParameter)

{

if (cpRecoCtxt && cpVoice &&

SUCCEEDED(hr = cpRecoCtxt->SetNotifyWin32Event()) &&

SUCCEEDED(hr =

cpRecoCtxt->SetInterest(SPFEI(SPEI_RECOGNITION), SPFEI(SPEI_RECOGNITION))) &&

SUCCEEDED(hr

cpRecoCtxt->SetAudioOptions(SPAO_RETAIN_AUDIO, NULL, NULL)) &&

=

SUCCEEDED(hr = cpRecoCtxt->CreateGrammar(0, &cpGrammar))

&&

SUCCEEDED(hr SPLO_STATIC)) &&

= cpGrammar->LoadDictation(NULL,

SUCCEEDED(hr = cpGrammar->SetDictationState(SPRS_ACTIVE)))

{

USES_CONVERSION;

CComPtr cpResult;

while (SUCCEEDED(hr = BlockForResult(cpRecoCtxt, &cpResult)))

{

cpGrammar->SetDictationState( SPRS_INACTIVE );

CSpDynamicString dstrText;

if SP_GETWHOLEPHRASE,

(SUCCEEDED(cpResult->GetText(SP_GETWHOLEPHRASE,

TRUE, &dstrText, NULL)))

{

CSpeechRecognitionDlg*

pDlg=(CSpeechRecognitionDlg*)AfxGetApp()->GetMainWnd();

pDlg->SetDlgItemText(IDC_EDIT1,(CString)dstrText);

pDlg->Check((CString)dstrText);

}

cpGrammar->SetDictationState( SPRS_ACTIVE );

}

}

return 0;

}

void CSpeechRecognitionDlg::DrawArrow(CPoint p1,CPoint theta,int length) //画箭头

{

p2,double

theta=3.1415926*theta/180;//转换为弧度

double Px,Py,P1x,P1y,P2x,P2y;

//以P2为原点得到向量P2P1(P)

Px=p1.x-p2.x;

Py=p1.y-p2.y;

//向量P旋转theta角得到向量P1

P1x=Px*cos(theta)-Py*sin(theta);

P1y=Px*sin(theta)+Py*cos(theta);

//向量P旋转-theta角得到向量P2

P2x=Px*cos(-theta)-Py*sin(-theta);

P2y=Px*sin(-theta)+Py*cos(-theta);

//伸缩向量至制定长度

double x1,x2;

x1=sqrt(P1x*P1x+P1y*P1y);

P1x=P1x*length/x1;

P1y=P1y*length/x1;

x2=sqrt(P2x*P2x+P2y*P2y);

P2x=P2x*length/x2;

P2y=P2y*length/x2;

//平移变量到直线的末端

P1x=P1x+p2.x;

P1y=P1y+p2.y;

P2x=P2x+p2.x;

P2y=P2y+p2.y;

CClientDC dc(this);//获取客户窗口DC

CPen pen,pen1,*oldpen;

pen.CreatePen(PS_SOLID, 2, RGB(0, 0, 0));

pen1.CreatePen(PS_SOLID, 2, RGB(0, 0, 255));

oldpen=dc.SelectObject(&pen);

dc.MoveTo(p1.x,p1.y);

dc.LineTo(p2.x,p2.y);

dc.SelectObject(&pen1);

dc.MoveTo(p2.x,p2.y);

dc.LineTo(P1x,P1y);

dc.MoveTo(p2.x,p2.y);

dc.LineTo(P2x,P2y);

dc.SelectObject(oldpen);

}

void CSpeechRecognitionDlg::Check(CString str)//判断方向

{

if(str.Compare(strUp)==0)

{

pt_End=pt_UP;

flag=TRUE;

Invalidate();

}

if(str.Compare(strDown)==0)

{

pt_End=pt_DOWN;

flag=TRUE;

Invalidate();

}

if(str.Compare(strRight)==0)

{

pt_End=pt_RIGHT;

flag=TRUE;

Invalidate();

}

if(str.Compare(strLeft)==0)

{

pt_End=pt_LEFT;

flag=TRUE;

Invalidate();

}

}

void CSpeechRecognitionDlg::OnBtnInfo()

{

// TODO: Add your control notification handler code here

CAboutDlg dlg;

dlg.DoModal();

}

void CSpeechRecognitionDlg::OnBtnSet()

{

// TODO: Add your control notification handler code here

CDlgSet dlg;

dlg.m_up=strUp;

dlg.m_down=strDown;

dlg.m_left=strLeft;

dlg.m_right=strRight;

if(IDOK==dlg.DoModal())

{

strUp=dlg.m_up;

strDown=dlg.m_down;

strLeft=dlg.m_left;

strRight=dlg.m_right;

}

}

因篇幅问题不能全部显示,请点此查看更多更全内容