3

Is there anyway or anyone know how to do STT using Microsoft's Speech Recognition API for Windows Form Application?

Xeon
  • 246
  • 3
  • 15

1 Answers1

5

.NET contains an assembly for speech recognition. You'll need to add the reference to

System.Speech

And add the namespace with

using System.Speech.Recognition;

Following code will analyze your speech and add the text to an textbox:

private void startRecognition()
    {
        SpeechRecognitionEngine recognizer = new SpeechRecognitionEngine(); //default culture
        //SpeechRecognitionEngine recognizer = new SpeechRecognitionEngine(new CultureInfo("de-DE"));
        //With specified culture | Could cause an CultureNotFoundException
        Grammar dictationGrammar = new DictationGrammar();
        recognizer.LoadGrammar(dictationGrammar);
        try
        {
            recognizer.SetInputToDefaultAudioDevice();
            RecognitionResult result = recognizer.Recognize();
            if(result != null)
                result_textBox.Text += result.Text + "\r\n"; 
        }
        catch (InvalidOperationException exception)
        {
            MessageBox.Show(exception.Message,exception.Source);
        }
        finally
        {
            recognizer.UnloadAllGrammars();
        }                
    }

To change the times which timeout the recognition, change following properties:

    recognizer.InitialSilenceTimeout = TimeSpan.FromSeconds(3);
    recognizer.BabbleTimeout = TimeSpan.FromSeconds(2);
    recognizer.EndSilenceTimeout = TimeSpan.FromSeconds(1);
    recognizer.EndSilenceTimeoutAmbiguous = TimeSpan.FromSeconds(1.5);

Sources:

http://msdn.microsoft.com/en-us/magazine/cc163663.aspx | http://msdn.microsoft.com/en-us/library/system.speech.recognition.speechrecognitionengine.aspx

Daniel Abou Chleih
  • 2,440
  • 2
  • 19
  • 31
  • You're welcome, just found something to improve. If we start recognition and just won't speak, result will be null and adding the Text of result to the textBox will result in a NullReferenceException. To avoid that: `if(result != null)result_textBox.Text += result.Text + "\r\n";` – Daniel Abou Chleih Oct 13 '13 at 13:34