一个基于c#3.0的开发基于2000/XP/2003下语音识别的通用类

C#语音识别系统实现
本文介绍了一个基于C#的语音识别系统的实现方法,通过使用SpeechLib库进行语音输入识别,并详细展示了如何加载语法文件、处理识别结果及事件监听等关键步骤。

废话少说看代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SpeechLib;
using System.Windows.Forms;
using System.Collections;


namespace yincheng01@163.com
{
public class speechxp
{


private SpeechLib.SpSharedRecoContext objRecoContext;
private SpeechLib.ISpeechRecoGrammar grammar;
private SpeechLib.ISpeechGrammarRule rule = null;
private SpeechLib.ISpeechGrammarRuleState state;
private SpeechLib.ISpeechPhraseElement PhraseElem;
public System.Windows.Forms.Control cDisplay;

public speechxp(string FileName, Control cDisplay)
{



}

private void initSAPI()
{
try
{
objRecoContext = new SpeechLib.SpSharedRecoContext();
objRecoContext.AudioLevel += new _ISpeechRecoContextEvents_AudioLevelEventHandler(RecoContext_VUMeter);//设置声音事件的响应函数
objRecoContext.Recognition += new _ISpeechRecoContextEvents_RecognitionEventHandler(RecoContext_Recognition);//设置成功识别事件的响应函数
objRecoContext.EventInterests = SpeechLib.SpeechRecoEvents.SRERecognition | SpeechLib.SpeechRecoEvents.SREAudioLevel;//设置响应事件

//create grammar interface with ID = 0
grammar = objRecoContext.CreateGrammar(1);//语法的实例
}
catch (Exception ex)
{
MessageBox.Show("Exception \n" + ex.ToString(), "Error - initSAPI");
}
}
private void SAPIGrammarFromFile(string FileName)
{
try
{

grammar.CmdLoadFromFile(FileName, SpeechLib.SpeechLoadOption.SLODynamic);
grammar.CmdSetRuleIdState(0, SpeechRuleState.SGDSActive);

}
catch
{
MessageBox.Show("载入语法文件失败 " + FileName + "\n", "Error - SAPIGrammarFromFile");
}
}
public void RecoContext_Recognition(int StreamNumber, object StreamPosition, SpeechRecognitionType RecognitionType, ISpeechRecoResult e)
{

int i;
ArrayList reco = new ArrayList();
string phrase = e.PhraseInfo.GetText(0, -1, true);

cDisplay.Text = e.PhraseInfo.GetText(0, -1, true);

foreach (SpeechLib.ISpeechPhraseElement PhraseElem in e.PhraseInfo.Elements)
{
/// listBox1.Items.Add(PhraseElem.Pronunciation);添加语音指令

}

i = e.PhraseInfo.Elements.Count;
for (int h = 0; h < i; h++)
{
}

}

public void RecoContext_VUMeter(int StreamNumber, object StreamPosition, int e)
{
int hg = e;
}
}
}

类的调用

hhh = new SpRecognition(file); 定义好的语法XML文件
hhh.BeginRec(textBox10); 关联到显示识别结果的控件


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值