C#如何开发语音识别,最好有例子

C#如何开发语音识别,最好有例子,第1张

语音识别小程序,调用了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

}

}

}

我们这学期刚学的凌阳61板,语音程序有A2000,S480,S2403种算法,我先给个A2000的,还需要的话发消息给我,一起学习啦!

#include "A2000.h"

#define SPEECH_1 0

#define DAC1 1

#define DAC2 2

#define Ramp_UpDn_Off 0

#define Ramp_UpDn_On 3

#define Manual 0

#define Auto 1

#define Full 1

#define Empty 2

#define Mode 1

//===============================================================================================

/颤橘/ 函数: main()

// 描述:主函数

//===============================================================================================

main()

{

extern long RES_WW_24K_SA,RES_WW_24K_EA //定义语音资源的首末地址标号

long int Addr //定义地址变量

int Ret = 0 //定义获取语音数据变量并初始化

if(Mode == 1) //采用自动方式播放

{

SACM_A2000_Initial(1)//自动方式播放初始化

SACM_A2000_Play(SPEECH_1,DAC1,Ramp_UpDn_On) //销键定义语音索引号、播放通道、允许亏洞巧音量增/减调节

while(1){

F_ClearWatchdog()

SACM_A2000_ServiceLoop()} //获取语音数据并将其填入解码队列

}

if(Mode == 0)//采用非自动方式播放

{

Addr=RES_WW_24K_SA//送入语音队列的首址

SACM_A2000_Initial(0) //非自动方式播放的初始化

SACM_A2000_InitDecoder(DAC1) //开始对A2000的语音数据以非自动方式解码

while(SACM_A2000_TestQueue()!=Full) //测试并获取语音队列的状态

{

Ret=SP_GetResource(Addr) //从资源文件里获取一个字型语音数据

SACM_A2000_FillQueue(Ret) //获取语音编码数据并填入语音队列等候解码

Addr++

}

while(1)

{

if(SACM_A2000_TestQueue()!=Full)

{

Ret =SP_GetResource(Addr)

SACM_A2000_FillQueue(Ret)

Addr++

}

if(Addr<RES_WW_24K_EA ) //如果该段语音未播完,即未到达末地址时

SACM_A2000_Decoder() //获取资源并进行解码,再通过中断服务子程序送入DAC通道播放

else

SACM_A2000_Stop() //否则,停止播放

F_ClearWatchdog()//清看门狗

}

}

}

首先你要引用 DotNetSpeech

然后

创建SpeechRecognitionEngine 对象

下面的伍源代码可以让此亏系统获取腔扒态语音并且识别

SRE = new SpeechRecognitionEngine(new System.Globalization.CultureInfo("zh-CN"))//中文

SRE.LoadGrammar(new DictationGrammar())//所有语言

SRE.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(SRE_SpeechRecognized)//系统接受后的事件

SRE.SetInputToDefaultAudioDevice()//设置默认输入设备

微软MSDN里面有详细的介绍,望采纳。


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/12448380.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-25
下一篇 2023-05-25

发表评论

登录后才能评论

评论列表(0条)

保存