六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 40|回复: 0

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

[复制链接]

升级  11.82%

1179

主题

1179

主题

1179

主题

榜眼

Rank: 8Rank: 8

积分
3591
 楼主| 发表于 2013-2-7 03:45:36 | 显示全部楼层 |阅读模式
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();
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表