Click here to Skip to main content
15,889,335 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
When I try to load the grammar for the speech recognition I got this error:
SAPI does not implement phonetic alphabet selection. I use the default speech DLL for the recognition.

And...I know it's a noob question but...how to recognize my voice automatically and then put the text into a textbox?

My code:
VB
Imports System.Speech
Imports System.Xml
Imports System.IO
Public Class Form1
    Dim choices As New Speech.Recognition.Choices
    Dim builder As New Speech.Recognition.GrammarBuilder
    Dim engine As New Speech.Recognition.SpeechRecognitionEngine

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If IO.File.Exists("C:\Test\grammar.xml") Then
            IO.File.Delete("C:\Test\grammar.xml")
        End If
        choices.Add("red")
        builder.Append(choices)
        Speech.Recognition.GrammarBuilder.Add(choices, builder)
        Dim doc As New Speech.Recognition.SrgsGrammar.SrgsDocument(builder)
        Dim writer As Xml.XmlWriter = XmlWriter.Create("C:\Test\grammar.xml")
        doc.WriteSrgs(writer)
        writer.Close()
        AddHandler engine.LoadGrammarCompleted, AddressOf Me.HandleSpeechRecognized
    End Sub

    Public Sub HandleSpeechRecognized(ByVal sender As Object, _
           ByVal e As System.EventArgs)
        MsgBox("Grammar loaded") 'I just use it to see if the grammar is loaded...big fail
    End Sub

    Private Sub wait(ByVal interval As Integer)
        Dim sw As New Stopwatch
        sw.Start()
        Do While sw.ElapsedMilliseconds < interval
            Application.DoEvents()
        Loop
        sw.Stop()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim grammar As New Speech.Recognition.Grammar("C:\Test\grammar.xml")
        engine.LoadGrammar(grammar) 'there's the error
        engine.SetInputToDefaultAudioDevice()

        engine.RecognizeAsync() 'that's my fail to recognize...
        wait(3000)
        engine.RecognizeAsyncStop()
    End Sub

End Class

Infinite thanks for anyone who can help me!
Posted
Updated 2-Jul-12 7:42am
v5
Comments
Sergey Alexandrovich Kryukov 3-Jul-12 0:33am    
Please don't re-post; use "Improve question" and the page or you original question.
--SA

1 solution

:WTF: You wired up the LoadGrammarCompleted event (which you don't need and wiredup to the WRONG handler BTW), but not the SpeechRecognized event?? How are you going to get notified that the engine heard something??

When you finally get the event handler written and wired up, you can get the Text of the recognition Result out of the event args you are passed in the handler.

VB
AddHandler engine.SpeechRecognized, AddressOf SpeechRecognizedHandler

You can delete the other AddHandler line and put the above in it's place. You're not calling LoadGrammarAsync so the LoadGrammarCompleted event won't do you any good.

Then you can easily get the text of the recognized speech...
VB
Private Sub SpeechRecognizedHandler(ByVal sender As Object, ByVal e As SpeechRecognizedEventArgs)
        Console.WriteLine("Recognized: " & e.Result.Text)
    End Sub
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900