School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 306,987 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,966 people online right now. Registration is fast and FREE... Join Now!




SAPI 5.3

 
Reply to this topicStart new topic

> SAPI 5.3

ccubed
Group Icon



post 15 Jun, 2009 - 12:59 PM
Post #1


Okay, so from what I gather, the biggest difference between the old SAPI 5.1 and SAPI 5.3 is that SAPI 5.3 actually takes less code to do some of the same things that SAPI 5.1 does. This is just a simple example that runs notepad and says it is before doing so.

One thing to remember is that the name of speech lib is changed again. Now you have to do this: Project -> Add Reference -> .NET -> System.Speech

CODE


imports system.speech

Public Class Form1

Public WithEvents recognizer As New System.Speech.Recognition.SpeechRecognitionEngine
Dim gram As New System.Speech.Recognition.DictationGrammar()
Public synth As New System.Speech.Synthesis.SpeechSynthesizer
Dim cmd as String

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

     recognizer.LoadGrammar(gram)
     recognizer.SetInputToDefaultAudioDevice()
     recognizer.RecognizeAsync()

End Sub

Private Sub GotSpeech( ByVal sender As System.Object, ByVal phrase As System.Speech.Recognition.SpeechRecognizedEventArgs ) Handles recognizer.SpeechRecognized

cmd = phrase.Result.Text

If cmd.IndexOf("Run") <> 0 or cmd.IndexOf("run") <> 0 Then

            If cmd.Split(" ")(1) = "Notepad" Or cmd.Split(" ")(1) = "notepad" Then

                 synth.Speak("Running Notepad.")
                 Shell("notepad.exe", AppWinStyle.NormalFocus, False)

            End If

End If

End Sub

End Class




Now some explanation.

cmd is the String version of what the speech recognition engine determined was what was said. This is a simple example that simply allows someone to say run notepad and it will run. You can essentially adapt this to any purpose in an app.

Also, i check both "Run" and "run" because I'm not sure at any point whether the recognizer will capitalize the first letter or not.

gram is a DictationGrammar object. As it turns out, there are two grammar objects in SAPI 5.3. As you've probably already guessed, DictationGrammar is for spoken language and the normal grammar object is for simple grammar rules.

So basically, Load the grammar, set audio to the default input device and then start an Async recognize call. They have a blocking recognize call and one thing to note here is that it will turn itself off after each sucessful recognized phrase. Meaning that you have to recall Async recognize at the end of the GotSpeech sub if you want to keep picking up voice commands. As it stands, this will pick up one command and no more because it stops recognition.

One neat thing about the synthesis engine is that you can specify things like ".NET" to be said as "dot net" by Anna. Also, as it stands, the periods aren't said and so are pretty much useless. It acts just like the Vista Recognition engine also so if you spell out a word, Anna will spell it out too.

Anyways, hope it helps someone out there. If you want to know more, well, MSDN is your friend.
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!

Umaid123
*



post 31 Jul, 2009 - 11:01 PM
Post #2
Hi
I am unable to run this code because an exception occured as

An unhandled exception of type 'System.ArgumentException' occurred in System.Speech.dll

Additional information: Value does not fall within the expected range.


Although the code is showing no errors on runtime.
I am also figuring out how to load grammar via xml in continuos dictation mode to use my own custom words to SAPI grammar. Any assistance in this regard will be highly appreciated.

My code for continuous grammar dictation
CODE


Dim WithEvents context As SpSharedRecoContext
    Dim grammer As ISpeechRecoGrammar
    Dim CharCount As Integer
    Dim priority As Integer

Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click

        
        'First check to see if reco has been loaded before. If not lets load it.
        If (context Is Nothing) Then
            Debug.Print("Initializing SAPI reco context object...")
            context = New SpSharedRecoContextClass
            grammer = context.CreateGrammar(1)
            grammer.CmdLoadFromFile("test3.xml", SpeechLoadOption.SLOStatic)
            grammer.DictationLoad()
            'grammer.CmdSetRuleIdState(1, SpeechRuleState.SGDSActive)
        End If

        Label8.Text = "Recognition Started"                  'Change the Label to let the user know whats up
        grammer.DictationSetState(SpeechRuleState.SGDSActive)   'Turns on the Recognition. This is Vitally important

        
        Button4.Enabled = False
        Button6.Enabled = True

    End Sub

    Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
        Dim choices As New Speech.Recognition.Choices()
        choices.Add(New Speech.Recognition.GrammarBuilder(New Speech.Recognition.SemanticResultValue("product", "<product/>")))
        Dim builder As New Speech.Recognition.GrammarBuilder()
        builder.Append(New Speech.Recognition.SemanticResultKey("options", choices.ToGrammarBuilder()))
        Speech.Recognition.LoadGrammarCompletedEventArgs(grammer = New Speech.Recognition.Grammar(builder) {Name as Constants.GrammarNameLanguage})
        grammer.Priority = priority
        context.LoadGrammar(grammer)

    End Sub


    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
        grammer.DictationSetState(SpeechRuleState.SGDSInactive)         'Turns off the Recognition.
        Label8.Text = "Recognition Stopped"


        Button4.Enabled = True
        Button6.Enabled = False
    End Sub
  
    Private Sub OnHypo(ByVal StreamNumber As Integer, ByVal StreamPosition As Object, ByVal Result As ISpeechRecoResult) Handles context.Hypothesis

        Button6.Enabled = False
        'The button will re-enable in the OnReco Event


        If Label8.Text <> "Receiving" Then
            Label8.Text = "Receiving"
        End If
    End Sub

    Private Sub OnReco(ByVal StreamNumber As Integer, ByVal StreamPosition As Object, ByVal RecognitionType As SpeechRecognitionType, ByVal Result As ISpeechRecoResult) Handles context.Recognition

        Dim recoResult As String = Result.PhraseInfo.GetText 'Create a new string, and assign the recognized text to it.


        TextBox1.SelectionStart = CharCount
        TextBox1.SelectedText = recoResult & " "
        CharCount = CharCount + 1 + Len(recoResult)


        SendKeys.Send(recoResult & " ")

        Label8.Text = "Finished Dictating"
        Button2.Enabled = True

    End Sub



Go to the top of the page
+Quote Post


Fast ReplyReply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 


Lo-Fi Version Time is now: 11/21/09 05:37AM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month