winzenghua 发表于 2013-2-7 03:45:36

一个基于speech API5.1的通用语音识别类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SpeechLib;
using System.Windows.Forms;
namespace yincheng01@163.com
{
public class SpRecognition
{
public SpeechLib.ISpeechRecoGrammar isrg;
public SpeechLib.SpSharedRecoContextClass ssrContex = null;
public System.Windows.Forms.Control cDisplay;
public SpRecognition(string[] gh)//加载关键词
{
ssrContex = new SpSharedRecoContextClass();
isrg = ssrContex.CreateGrammar(0);
SpeechLib._ISpeechRecoContextEvents_RecognitionEventHandler recHandle =
new _ISpeechRecoContextEvents_RecognitionEventHandler(ContexRecognition);
ssrContex.Recognition += recHandle;
SpeechLib.ISpeechGrammarRule menuRule = isrg.Rules.Add("123", SpeechRuleAttributes.SRATopLevel | SpeechRuleAttributes.SRADynamic, 1);
object PropValue = "";
foreach (string ff in gh)
{
menuRule.InitialState.AddWordTransition(null, ff, " ", SpeechGrammarWordType.SGLexical, ff, 1, ref PropValue, 1.0F);
}

isrg.Rules.Commit();

isrg.CmdSetRuleState("123", SpeechRuleState.SGDSActive);

isrg.DictationLoad("", SpeechLoadOption.SLOStatic);
isrg.DictationSetState(SpeechRuleState.SGDSActive);


}
public void BeginRec(Control tbResult)
{


cDisplay = tbResult;

}
public void CloseRec()
{


ssrContex = null;
cDisplay = null;
}
private void ContexRecognition(int iIndex, object obj, SpeechLib.SpeechRecognitionType type, SpeechLib.ISpeechRecoResult result)
{
cDisplay.Text = result.PhraseInfo.GetText(0, -1, true);
}

}
}
类的调用
SpRecognition ff;
string[] dd={"尹成","华东","玉辉","};
ff = new SpRecognition(dd);
ff.BeginRec(richTextBox1);
ff.CloseRec();
页: [1]
查看完整版本: 一个基于speech API5.1的通用语音识别类