C # development based on Microsoft Speech SDK Version 5.1

Source: Internet
Author: User


// Microsoft Speech SDK Version 5.1
# Endregion
Private SpeechLib. SpSharedRecoContext objRecoContext = null;

# Region ISpeechRecoGrammar description
// The ISpeechRecoGrammar automation interface enables applications to manage the words and phrases for the SR engine.
// Automatically manage application words and phrases for the speech recognition engine
/*
Attribute
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.

Method
Uploloadfromfile 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 description
// The ISpeechGrammarRule automation interface defines the properties and methods of a speech grammar rule
// API for automatically defining speech attributes and Methods
/*
Attribute
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.

Method
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)
{

// Obtain a RecoContext instance.
ObjRecoContext = new SpeechLib. SpSharedRecoContext ();
// Assign an Event to Hypothesis Event (tentative middle layer identification, that is, preliminary, temporary identification ).
ObjRecoContext. Hypothesis + = new _ ISpeechRecoContextEvents_HypothesisEventHandler (Hypo_Event );
// Assign an event to speech recognition.
ObjRecoContext. Recognition + = new _ ISpeechRecoContextEvents_RecognitionEventHandler (Reco_Event );
// Create a grammer instance.
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 ";

// Activate the menu command.
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 primary and formal speech recognition event processing

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 clear controls and enable and disable Speech Recognition

Private void upload diable_click (object sender, System. EventArgs e)
{
ClearControls (label3, objRecoContext );
}

# Endregion

# Region custom functions

/// <Summary>
/// Clear data
/// </Summary>
/// <Param name = "args"> variables and controls to be cleared </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
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.