reco.cpp

来自「英语学习的软件说明」· C++ 代码 · 共 126 行

CPP
126
字号
#include "stdafx.h"
#include <sphelper.h>
#include "Reco.h"

CRecoApp::CRecoApp()
{
	
};

BOOL CRecoApp::InitInstance()
{
  CoInitialize(NULL);
  return TRUE;
}

CRecoApp RecoApp;

CRecoApp::InitSAPI( HWND hWnd )
{
    HRESULT hr = S_OK;
    CComPtr<ISpAudio> cpAudio;

    while ( 1 )
    {
        hr = g_cpEngine.CoCreateInstance(CLSID_SpSharedRecognizer);
        if ( FAILED( hr ) )
        {
            break;
        }
       
        hr = g_cpEngine->CreateRecoContext( &g_cpRecoCtxt );
        if ( FAILED( hr ) )
        {
            break;
        }

        hr = g_cpRecoCtxt->SetNotifyWindowMessage( hWnd, WM_RECOEVENT, 0, 0 );
        if ( FAILED( hr ) )
        {
            break;
        }

        hr = g_cpRecoCtxt->SetInterest( SPFEI(SPEI_RECOGNITION), SPFEI(SPEI_RECOGNITION) );
        if ( FAILED( hr ) )
        {
            break;
        }

        hr = g_cpRecoCtxt->CreateGrammar(NULL, &g_cpCmdGrammar);
        if (FAILED(hr))
        {
            break;
        }
        hr = g_cpCmdGrammar->LoadCmdFromFile(L"reco.xml",SPLO_DYNAMIC);
        if ( FAILED( hr ) )
        {
            break;
        }

        hr = g_cpCmdGrammar->SetRuleState(NULL, NULL, SPRS_ACTIVE );
        if ( FAILED( hr ) )
        {
            break;
        }

        break;
    }

    if ( FAILED( hr ) )
    {	
        CleanupSAPI();
    }
    return ( hr );
}

CRecoApp::CleanupSAPI( void )
{
    if ( g_cpCmdGrammar )
    {
        g_cpCmdGrammar.Release();
    }
    if ( g_cpRecoCtxt )
    {
        g_cpRecoCtxt->SetNotifySink(NULL);
        g_cpRecoCtxt.Release();
    }
	if ( g_cpEngine )
	{
		g_cpEngine.Release();
	}
}

CRecoApp::ProcessRecoEvent( HWND hWnd )
{
    CSpEvent event;

    while (event.GetFrom(g_cpRecoCtxt) == S_OK)
    {
        if (event.eEventId == SPEI_RECOGNITION)
		{
			ISpPhrase *pPhrase;
			SPPHRASE *pElements;
			ULONG resultid;

            pPhrase = event.RecoResult();
			pPhrase->GetPhrase(&pElements);
			resultid = pElements->Rule.ulId;
			PostMessage(hWnd,WM_RECOGNIZED,WPARAM(resultid),0);
        }
    }
}

CRecoApp::traincmd(HWND hWnd)
{
	g_cpEngine->DisplayUI(hWnd, NULL, SPDUI_UserTraining, NULL, 0);
}

CRecoApp::miccmd(HWND hWnd)
{
	g_cpEngine->DisplayUI(hWnd, NULL, SPDUI_MicTraining, NULL, 0);
}

CRecoApp::recocmd(HWND hWnd)
{
	g_cpEngine->DisplayUI(hWnd, NULL, SPDUI_RecoProfileProperties, NULL, 0);
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?