Mohanapriya R Mohanapriya R
Updated date Aug 19, 2026
Learn to convert speech to text in C#. The provided C# program utilizes the SpeechRecognitionEngine to recognize spoken words and display them as text.
  • 2.3k
  • 0
  • 0

Let's start by creating a simple C# program that utilizes the SpeechRecognitionEngine to convert spoken words into text. First, ensure you have the required assembly reference by adding "System.Speech" to your project.

using System;
using System.Speech.Recognition;

class SpeechToTextConverter
{
    static void Main()
    {
        SpeechRecognitionEngine recognizer = new SpeechRecognitionEngine();

        // Create a grammar for recognizing speech
        Choices choices = new Choices("Hello", "How are you", "Convert this speech to text");
        GrammarBuilder grammarBuilder = new GrammarBuilder(choices);
        Grammar grammar = new Grammar(grammarBuilder);

        recognizer.LoadGrammar(grammar);

        // Set up the event handler for recognized speech
        recognizer.SpeechRecognized += Recognizer_SpeechRecognized;

        // Start speech recognition
        recognizer.SetInputToDefaultAudioDevice();
        recognizer.RecognizeAsync();

        Console.WriteLine("Listening for speech. Press any key to exit.");
        Console.ReadKey();
    }

    private static void Recognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
    {
        Console.WriteLine($"Recognized: {e.Result.Text}");
    }
}

This program initializes a SpeechRecognitionEngine, sets up a simple grammar, and starts listening for speech. When speech is recognized, the Recognizer_SpeechRecognized event handler is triggered, displaying the recognized text.

Output:

Assuming you speak into the microphone with phrases like "Hello," "How are you," or "Convert this speech to text," the program will display the recognized text in the console.

Listening for speech. Press any key to exit.
Recognized: Hello
Recognized: How are you
Recognized: Convert this speech to text

 

Comments (0)

There are no comments. Be the first to comment!!!