Speech sample in VB.NET

Here's a small sample showing how to use the new Speech API from VB.NET.

I'm running VS 2005 beta 2, WinFX SDK, the Avalon & Indigo beta 1 RC, and the speech recogniser that comes with Office 2003.

Just create a new VB project and write code like I've pasted below.  Remember to add the reference to "Speech" in your project (you need to install the SDK for this to show up in the UI).  The app listens for the words "red" and "green".  When you say either of these, the form changes to that colour, and a voice tells you about it.

IMHO the trickiest part of the sample is constructing the grammar.  Here's the concept.  A grammar contains all the phrases that should be listened for.  It's expressed using a W3C XML document format called SRGS.  There are some classes in the System.Speech.GrammarBuilding namespace for building one of these documents.  Basically an SRGS document contains one or more rules.  One of these rules is the root.  The rules describe how a certain set of phrases can be built.  In this case, I started with an SrgsOneOf, meaning "choose one of the items from this list", I put the words "red" and "green" in it, put it in a rule, put the rule in the document, and made that rule the root of the document.

Imports System.Speech

Public

Class Form1

   Dim WithEvents reco As New Recognition.DesktopRecognizer

   Private Sub SetColor(ByVal color As System.Drawing.Color)

      Dim synth As New Synthesis.SpeechSynthesizer

      synth.SpeakAsync("setting the back color to " + color.ToString)

      Me.BackColor = color

   End Sub

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

      Dim gram As New GrammarBuilding.SrgsDocument

      Dim colorRule As New GrammarBuilding.SrgsRule("color")

      Dim colorsList As New GrammarBuilding.SrgsOneOf("red", "green")

      colorRule.Add(colorsList)

      gram.Rules.Add(colorRule)

      gram.Root = colorRule

      reco.LoadGrammar(New Recognition.Grammar(gram))

   End Sub

   Private Sub reco_SpeechRecognized(ByVal sender As Object, ByVal e As System.Speech.Recognition.RecognitionEventArgs) Handles reco.SpeechRecognized

      Select Case e.Result.Text

         Case "red"

            SetColor(Color.Red)

         Case "green"

            SetColor(Color.Green)

      End Select

   End Sub

End Class

NOTE: I posted this earlier today with a bug in it - I'd attached the event handler twice.