Here is a complete example of a C# Windows Forms application that implements speech-to-text using the System.Speech.Recognition namespace:
using System;
using System.Windows.Forms;
using System.Speech.Recognition;
namespace SpeechToTextExample
{
public partial class Form1 : Form
{
private SpeechRecognitionEngine recognitionEngine;
public Form1()
{
InitializeComponent();
// Create a new instance of the SpeechRecognitionEngine
recognitionEngine = new SpeechRecognitionEngine(new System.Globalization.CultureInfo(“en-US”));
// Load a grammar file
Grammar grammar = new Grammar(new GrammarBuilder(new Choices(File.ReadAllLines(@”commands.txt”))));
recognitionEngine.LoadGrammar(grammar);
// Subscribe to the SpeechRecognized event
recognitionEngine.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(recognitionEngine_SpeechRecognized);
}
private void startButton_Click(object sender, EventArgs e)
{
// Start listening for spoken input
recognitionEngine.RecognizeAsync(RecognizeMode.Multiple);
}
private void stopButton_Click(object sender, EventArgs e)
{
// Stop listening for spoken input
recognitionEngine.RecognizeAsyncStop();
}
void recognitionEngine_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
// Display the recognized text in the text box
textBox1.Text += e.Result.Text + Environment.NewLine;
}