Mohanapriya R Mohanapriya R
Updated date Feb 08, 2024
In this article, we will learn to convert speech to text in C#. The provided C# program utilizes the SpeechRecognitionEngine to recognize spoken words and display them as text.

Introduction:

Speech-to-text conversion is a fascinating technology that allows computers to understand spoken language and convert it into readable text. In this article, we will explore how to implement speech-to-text conversion in C# using the SpeechRecognitionEngine class in the System.Speech.Recognition namespace.

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

Conclusion:

In conclusion, we have explored a simple way to convert speech to text using C# and the SpeechRecognitionEngine. This technology opens up exciting possibilities, from creating voice-controlled applications to making systems more accessible.

Comments (0)

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