C#基于Microsoft Speech SDK Version 5.1 的开发

本文介绍如何使用 ISpeechRecoGrammar 和 ISpeechGrammarRule 接口来管理语音识别引擎中的单词和短语,包括加载语法、设置规则状态和添加规则等操作。
部署运行你感兴趣的模型镜像


//Microsoft Speech SDK Version 5.1
#endregion
private SpeechLib.SpSharedRecoContext objRecoContext = null;

#region ISpeechRecoGrammar说明
//The ISpeechRecoGrammar automation interface enables applications to manage the words and phrases for the SR engine.
//为语音识别引擎自动管理应用程序单词和短语
/*
属性
Id Property Returns the Id assigned to the grammar when it was created.
RecoContext Property Returns the RecoContext object that created this grammar.
Rules Property Returns the collection of grammar rules in the RecoGrammar.
State Property Gets and sets the operational status of the speech grammar.

方法
CmdLoadFromFile Method Loads a command and control grammar from the specified file.
CmdLoadFromMemory Method Loads a compiled speech grammar from memory.
CmdLoadFromObject Method Loads a speech grammar from a COM object.
CmdLoadFromProprietaryGrammar Method Loads a proprietary speech grammar.
CmdLoadFromResource Method Loads a command and control grammar from a Win32 resource.
CmdSetRuleIdState Method Activates or deactivates a rule by its rule ID.
CmdSetRuleState Method Activates or deactivates a rule by its rule name.
DictationLoad Method Loads a dictation topic into the grammar.
DictationSetState Method Sets the dictation topic state.
DictationUnload Method Unloads the active dictation topic from the grammar.
IsPronounceable Method Determines if a word has a pronunciation.
Reset Method Clears all grammar rules and resets the grammar's language to NewLanguage.
SetTextSelection Method Sets the range of text selection information in a word sequence data buffer.
SetWordSequenceData Method Defines a word sequence data buffer for use by the SR engine.
*/
#endregion
private SpeechLib.ISpeechRecoGrammar grammar = null;

#region ISpeechGrammarRule说明
//The ISpeechGrammarRule automation interface defines the properties and methods of a speech grammar rule
//自动定义语音属性和方法的接口
/*
属性
Attributes Property Returns information about the attributes of a speech grammar rule.
Id Property Specifies the ID of the speech grammar rule.
InitialState Property Specifies the initial state of the speech grammar rule.
Name Property Specifies the name of the speech grammar rule.

方法
AddResource Method Adds a string to a speech rule.
AddState Method Adds a state to a speech rule.
Clear Method Clears a rule, leaving only its initial state.
*/
#endregion
private SpeechLib.ISpeechGrammarRule menuRule = null;

#endregion

private void cmdEnable_Click(object sender, System.EventArgs e)
{

// 得到一个RecoContext实例.
objRecoContext = new SpeechLib.SpSharedRecoContext();
// 指派一个事件给Hypothesis Event(中间层暂定的识别,即,初级的,临时的识别).
objRecoContext.Hypothesis += new _ISpeechRecoContextEvents_HypothesisEventHandler(Hypo_Event);
// 指派一个事件给语音识别.
objRecoContext.Recognition += new _ISpeechRecoContextEvents_RecognitionEventHandler(Reco_Event);
//创建grammer实例.
grammar = objRecoContext.CreateGrammar(0);

label3.Text = "Speak Out one of the follwoing.\r\n1. New 2. Open 3. Close 4. Exit\r\n5. Cut 6. Copy 7. Paste 8. Delete";

//激活菜单命令.
menuRule = grammar.Rules.Add("MenuCommands",SpeechRuleAttributes.SRATopLevel|SpeechRuleAttributes.SRADynamic,1);

object PropValue = "";
menuRule.InitialState.AddWordTransition(null,"New"," ",SpeechGrammarWordType.SGLexical,"New", 1, ref PropValue, 1.0F );
menuRule.InitialState.AddWordTransition(null,"Open"," ",SpeechGrammarWordType.SGLexical,"Open", 2, ref PropValue, 1.0F );
menuRule.InitialState.AddWordTransition(null,"Close"," ",SpeechGrammarWordType.SGLexical,"Close",3, ref PropValue, 1.0F );
menuRule.InitialState.AddWordTransition(null,"Exit"," ",SpeechGrammarWordType.SGLexical,"Exit",4, ref PropValue, 1.0F );
menuRule.InitialState.AddWordTransition(null,"Cut"," ",SpeechGrammarWordType.SGLexical,"Cut",5, ref PropValue, 1.0F );
menuRule.InitialState.AddWordTransition(null,"Copy"," ",SpeechGrammarWordType.SGLexical,"Copy",6, ref PropValue, 1.0F );
menuRule.InitialState.AddWordTransition(null,"Paste"," ",SpeechGrammarWordType.SGLexical,"Paste",7, ref PropValue, 1.0F );
menuRule.InitialState.AddWordTransition(null,"Delete"," ",SpeechGrammarWordType.SGLexical,"Delete",8, ref PropValue, 1.0F );

grammar.Rules.Commit();
grammar.CmdSetRuleState("MenuCommands", SpeechRuleState.SGDSActive);
}

#region 初级和正式的语音识别事件处理

private void Reco_Event(int StreamNumber, object StreamPosition,SpeechRecognitionType RecognitionType,ISpeechRecoResult Result)
{
txtReco.Text = Result.PhraseInfo.GetText(0, -1, true);
}

private void Hypo_Event(int StreamNumber, object StreamPosition, ISpeechRecoResult Result)
{
txtHyp.Text = Result.PhraseInfo.GetText(0, -1, true);

}

#endregion

#region 清空控件和使关闭语音识别

private void cmdDiable_Click(object sender, System.EventArgs e)
{
ClearControls(label3, objRecoContext);
}

#endregion

#region 自定义函数

/// <summary>
/// 清空数据
/// </summary>
/// <param name="args">需要清空的变量及控件</param>
public void ClearControls(params object[] args)
{
foreach (object obj in args)
{
if (obj is TextBox)
{
TextBox t = (TextBox)obj;
t.Text = "";
}
if (obj is ListBox)
{
ListBox l = (ListBox)obj;
l.Items.Clear();
}
if (obj is Label)
{
Label l = (Label)obj;
l.Text = "";
}
if (obj is SpeechLib.SpSharedRecoContext)
{
SpeechLib.SpSharedRecoContext s = (SpeechLib.SpSharedRecoContext)obj;
s = null;
}
}
}

#endregion
}
}

您可能感兴趣的与本文相关的镜像

Qwen-Image

Qwen-Image

图片生成
Qwen

Qwen-Image是阿里云通义千问团队于2025年8月发布的亿参数图像生成基础模型,其最大亮点是强大的复杂文本渲染和精确图像编辑能力,能够生成包含多行、段落级中英文文本的高保真图像

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值