其实都是一样的,都是调用微软系统里面的一个COM组件,当然斗游你得先安装语音识别引擎,这一点只针对XP系统,如果你是vista或者seven系统,只要不是精简的,就有这个SAPI。在控制面板里面可以找到。
具体的代码思想好像是先初始化引擎,加入常用词语作为语法库,然如码后会优先从你自定义的语法库中找,找不到,就会到share那个库,那个库就是所有单词短语的集合,那一般就不准了。所以预先知道你要识别的内容很关键!
你可以到codeproject里面搜索speech recogintion,国内资料很少渣销哪。那个网站上源码很多的。
语音识别小程序,调用了windows的识别组件。精简了一些代码,算是比较简单易懂的一个语音识别类。开发测试环境win7,VS2008。如果有其它环境中的,欢迎补充纯历。
SRecognition.cs
using System
using System.Speech.Recognition
using System.Globalization
using System.Windows.Forms
namespace NingTao
{
public class SRecognition
{
public SpeechRecognitionEngine recognizer = null//语音识别引擎
public DictationGrammar dictationGrammar = null//自然语法
public System.Windows.Forms.Control cDisplay//显示控件
public SRecognition(string[] fg) //创建关键词语列表
{
CultureInfo myCIintl = new CultureInfo("游亏zh-CN")
foreach (RecognizerInfo config in SpeechRecognitionEngine.InstalledRecognizers())//获取所有语音引擎
{
if (config.Culture.Equals(myCIintl) &&config.Id == "MS-2052-80-DESK")
{
recognizer = new SpeechRecognitionEngine(config)
break
}//选择识别引擎
}
if (recognizer != null)
{
InitializeSpeechRecognitionEngine(fg)//初始化语音识别引擎
dictationGrammar = new DictationGrammar()
}
else
{
MessageBox.Show("创建语音识别失败")
}
}
private void InitializeSpeechRecognitionEngine(string[] fg)
{
recognizer.SetInputToDefaultAudioDevice()//选择默认的音频输入设备
Grammar customGrammar = CreateCustomGrammar(fg)
//根据关键字数组建立语法
recognizer.UnloadAllGrammars()
recognizer.LoadGrammar(customGrammar)
//加载语法
recognizer.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(recognizer_SpeechRecognized)
//recognizer.SpeechHypothesized += new EventHandler <SpeechHypothesizedEventArgs>(recognizer_SpeechHypothesized)
}
public void BeginRec(Control tbResult)//关联窗口控件
{
TurnSpeechRecognitionOn()
TurnDictationOn()
cDisplay = tbResult
}
public void over()//停止语音识别引擎
{
TurnSpeechRecognitionOff()
}
public virtual Grammar CreateCustomGrammar(string[] fg) //创造做磨搜自定义语法
{
GrammarBuilder grammarBuilder = new GrammarBuilder()
grammarBuilder.Append(new Choices(fg))
return new Grammar(grammarBuilder)
}
private void TurnSpeechRecognitionOn()//启动语音识别函数
{
if (recognizer != null)
{
recognizer.RecognizeAsync(RecognizeMode.Multiple)
//识别模式为连续识别
}
else
{
MessageBox.Show("创建语音识别失败")
}
}
private void TurnSpeechRecognitionOff()//关闭语音识别函数
{
if (recognizer != null)
{
recognizer.RecognizeAsyncStop()
TurnDictationOff()
}
else
{
MessageBox.Show("创建语音识别失败")
}
}
private void recognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
//识别出结果完成的动作,通常把识别结果传给某一个控件
string text = e.Result.Text
cDisplay.Text += text
}
private void TurnDictationOn()
{
if (recognizer != null)
{
recognizer.LoadGrammar(dictationGrammar)
//加载自然语法
}
else
{
MessageBox.Show("创建语音识别失败")
}
}
private void TurnDictationOff()
{
if (dictationGrammar != null)
{
recognizer.UnloadGrammar(dictationGrammar)
//卸载自然语法
}
else
{
MessageBox.Show("创建语音识别失败")
}
}
}
}
form调用,其中2个按钮(开始,停止),1个文本框(识别结果)
using System
using System.Windows.Forms
namespace NingTao
{
public partial class Form1 : Form
{
private SRecognition sr
public Form1()
{
InitializeComponent()
string[] fg = { "东方", "西方", "南方", "北方" }
sr = new SRecognition(fg)
button2.Enabled = false
}
private void button1_Click(object sender, EventArgs e)
{
sr.BeginRec(textBox1)
button1.Enabled = false
button2.Enabled = true
}
private void button2_Click(object sender, EventArgs e)
{
sr.over()
button1.Enabled = true
button2.Enabled = false
}
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)