[.Net 3.0] Speech Recognition

avalon

Member
Joined
Feb 6, 2007
Messages
7
Programming Experience
1-3
Hi Everyone,

This is my first post on this forum so please bear with me if I make some mistakes against general posting rules.

OS: Windows XP x64 Corporate Edition
Development Environment: Microsoft Visual Studio 2005 Team Edition
Framework: .Net 3.0

I've been working on getting a speechrecognition app running on my computer. I started with converting a C# application to VB first, which was not so tricky but took me about half an hour. You can see the result at the end of this post.

Now why I came to this forum is the following:
When I finally succesfully compiled the SpeechRecognition app the following error popped up:

Speech_x64Issue.PNG


Hope you guys now a way to let me play some more with this speechrecognition stuff and run this on my Windows XP 64bit version.

Thanks!

To get the following code working you need the System.Speech namespace which you should be able to find here:
C:\Program Files\Reference Assemblies\Microsoft\Framework\v3.0\System.Speech.dll
As you probably know, you'll need the .Net 3.0 Framework.

Here is the Example VB code you can use:
VB.NET:
Imports System
Imports System.Speech
Imports System.Speech.Recognition
Imports System.Threading
Imports System.Diagnostics
Imports System.Globalization


Module Module1

    Sub Main(ByVal args As String())
        EnumerateEngines()
        args = CheckArguments(args)
        Dim listener As CommandListener = New CommandListener()
        listener.SetCommands(args)
        listener.Run()
    End Sub

    Private Function CheckArguments(ByVal args As String())
        If args.Length = 0 Then
            Console.WriteLine("Setting up default words. Enter your own words via command line arguments")
            args(0) = "music"
            args(1) = "word"
        End If

        Console.Write("Now listening for...")
        Dim word As String
        For Each word In args
            Console.Write(String.Format("'{0}', ", word))
        Next
        Console.WriteLine("\r\n(Press a key to stop listening)")
        Return args
    End Function

    Private Sub EnumerateEngines()
        Console.WriteLine("The following engines are available")
        Dim config As RecognizerInfo
        For Each config In SpeechRecognitionEngine.InstalledRecognizers()
            Console.WriteLine(config.Name & " " & config.Description)
        Next
    End Sub
End Module

Class CommandListener
    Dim _speechRecogniser As SpeechRecognitionEngine

    Public Sub New()
        _speechRecogniser = New SpeechRecognitionEngine()
        _speechRecogniser.SetInputToDefaultAudioDevice()
        AddHandler _speechRecogniser.SpeechDetected, AddressOf _speechRecogniser_SpeechDetected
        AddHandler _speechRecogniser.SpeechHypothesized, AddressOf _speechRecogniser_SpeechHypothesized
        AddHandler _speechRecogniser.SpeechRecognitionRejected, AddressOf _speechRecogniser_SpeechRecognitionRejected
        AddHandler _speechRecogniser.SpeechRecognized, AddressOf _speechRecogniser_SpeechRecognized
        AddHandler _speechRecogniser.AudioStateChanged, AddressOf _speechRecogniser_AudioStateChanged
        AddHandler _speechRecogniser.AudioSignalProblemOccurred, AddressOf _speechRecogniser_AudioSignalProblemOccurred
        NotifyEvent("Loaded default engine - {0}", New Object() {_speechRecogniser.RecognizerInfo.Name})
    End Sub

    Private Sub _speechRecogniser_AudioSignalProblemOccurred(ByVal sender As Object, ByVal e As AudioSignalProblemOccurredEventArgs)
        NotifyEvent("Audio problem {0}", New Object() {e.AudioSignalProblem.ToString("G")})
    End Sub

    Private Sub _speechRecogniser_AudioStateChanged(ByVal sender As Object, ByVal e As AudioStateChangedEventArgs)
        NotifyEvent("Audio state now {0}", New Object() {e.AudioState.ToString("G")})
    End Sub

    Private Sub _speechRecogniser_SpeechRecognized(ByVal sender As Object, ByVal e As SpeechRecognizedEventArgs)
        NotifyEvent("Recognized '{0}' at {1} seconds", New Object() {e.Result.Text, e.Result.Audio.AudioPosition})
    End Sub

    Private Sub _speechRecogniser_SpeechRecognitionRejected(ByVal sender As Object, ByVal e As SpeechRecognitionRejectedEventArgs)
        NotifyEvent("Recognized '{0}' at {1} seconds", New Object() {e.Result.Text, e.Result.Audio.AudioPosition})
    End Sub

    Private Sub _speechRecogniser_SpeechHypothesized(ByVal sender As Object, ByVal e As SpeechHypothesizedEventArgs)
        NotifyEvent("Hypothesized '{0}' at seconds", New Object() {e.Result.Text})
    End Sub

    Private Sub _speechRecogniser_SpeechDetected(ByVal sender As Object, ByVal e As SpeechDetectedEventArgs)
        NotifyEvent("Detected speech at {0} seconds", New Object() {e.AudioPosition.TotalSeconds})
    End Sub

    Public Sub SetCommands(ByVal commands() As String)
        Dim grammarBuilder As GrammarBuilder = New GrammarBuilder()
        grammarBuilder.Culture = _speechRecogniser.RecognizerInfo.Culture
        grammarBuilder.Append(New Choices(commands))
        Dim grammar As Grammar = New Grammar(grammarBuilder)

        _speechRecogniser.UnloadAllGrammars()
        _speechRecogniser.LoadGrammar(grammar)
        _speechRecogniser.RecognizeAsync(RecognizeMode.Multiple)
    End Sub

    Public Sub Run()
        While Not Console.KeyAvailable
            Thread.Sleep(100)
        End While
        _speechRecogniser.Dispose()
    End Sub

    Private Shared Sub NotifyEvent(ByVal message As String, ByVal args As Object())
        Console.WriteLine(message)
    End Sub
End Class
Note: I converted this vb code from C# but I can't seem to find the guy's link anymore so if he sees this he can pm me. I didn't write this myself, just converted it
 
Back
Top