Search code examples
c#speech-recognitionspeech-to-textsapi

Converting Speech-To-Text using SAPI


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


Solution

  • .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