Microsoft Speech Recognition with a C# WinForm

There aren’t many examples of working with Microsoft Speech and WinForms. I am exploring how to have a WinForm recognize speech, display what was said in one of the Form’s controls (like a TextBox or ListBox), and echo what was said in audio.

The first step is to get your system set up properly. There are several different Microsoft speech platforms. I like to work with the managed code Microsoft Speech Platform 11 — essentially Microsoft.Speech.dll — rather than the System.Speech.dll platform.

1.) Install the Speech 11 SDK to be able to develop your program. I strongly recommend the 32-bit version. 2.) Install the Speech 11 Runtime to be able to run the program once it’s been created. 3.) Install a language to understand. I used English: MSSpeech_SR_en-US_TELE.msi. 4.) Install a voice to speak. I used “Helen”: MSSpeech_TTS_en-US_Helen.msi.

Then I created a demo WinForm that has a CheckBox to toggle speech recognition on and off. If speech recognition is on, the WinForm recognizes and parrots back the commands “start” and “stop”, as text in the ListBox and also audibly. The TextBox and Button controls are not used in the demo. The entire code is at the bottom of this post.


I launched Visual Studio and created a new C# WinForm named SpeechWithWinForm. I used VS 2012 but I’m pretty sure VS 2010 will work too. I added a reference to the speech DLL which in my case was at C:\ Program Files (x86)\ Microsoft SDKs\ Speech\ v11.0\ Assembly. I removed unneeded using statements and added two using statements to bring Speech.Recognition and Speech.Synthesis into scope.

In the designer, I added a CheckBox, TextBox, Button, and ListBox. I gave the CheckBox a label of “SR on/off” then I double-clicked the CheckBox to register its CheckedChanged event handler.

There’s a ton going on in the short demo. An interesting obstacle was trying to get the demo to echo the recognized command into the ListBox control. My initial attempt was:

listBox1.Items.Add("I heard " + txt);

But this doesn’t work because it’s being called from the recognizer object which is on a different thread than the WinForm. Two equivalent ways to display text in the ListBox are:

this.Invoke(new MethodInvoker( () =>
  { listBox1.Items.Add("I heard " + txt); }));


this.Invoke( (Action)( () =>
  listBox1.Items.Add("I heard " + txt))); 

Anyway, this is my basic speech recognition and synthesis with a WinForm example. Here’s the code:

using System;
using System.Data;
using System.Drawing;
using System.Windows.Forms;

using Microsoft.Speech.Recognition;
// C:\Program Files (x86)\Microsoft SDKs\
// Speech\v11.0\Assembly\
using Microsoft.Speech.Synthesis;
using System.Globalization;

namespace SpeechWithWinForm
  public partial class Form1 : Form
    static CultureInfo ci = new CultureInfo("en-us");
    static SpeechRecognitionEngine sre =
      new SpeechRecognitionEngine(ci);
    static SpeechSynthesizer ss =
      new SpeechSynthesizer();

    public Form1()
      sre.SpeechRecognized += sre_SpeechRecognized;
      Grammar g_StartStop = GetStartStopGrammar();
      // load other patterns here

    static Grammar GetStartStopGrammar()
      Choices choicesStartStop = new Choices();
      GrammarBuilder gb_StartStop =
        new GrammarBuilder(choicesStartStop);
      Grammar g_StartStop =
        new Grammar(gb_StartStop);
      return g_StartStop;

    void sre_SpeechRecognized(object sender,
      SpeechRecognizedEventArgs e)
      string txt = e.Result.Text;
      float conf = e.Result.Confidence;

      if (conf >= 0.65)
        //listBox1.Items.Add("I heard " + txt); // NO
        this.Invoke(new MethodInvoker( () =>
          { listBox1.Items.Add("I heard " + txt); }));
        //this.Invoke( (Action)( () =>
        //listBox1.Items.Add("I think I heard " + txt)));
        ss.SpeakAsync("You said " + txt);

    private void checkBox1_CheckedChanged(object sender,
      EventArgs e) // SR on/off
      if (checkBox1.Checked == true)
      else if (checkBox1.Checked == false) // turn off
  } // Form
} // ns
This entry was posted in Machine Learning. Bookmark the permalink.