How to Create Speech to Text in wpf c# application

Nandini Nammi 41 Reputation points
2022-09-23T06:53:52.967+00:00

I need an application which take live voice speech and convert into text in wpf C# application

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,367 questions
Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,670 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,232 questions
{count} votes

Accepted answer
  1. Hui Liu-MSFT 38,191 Reputation points Microsoft Vendor
    2022-09-27T06:50:04.557+00:00

    As Castorix31 said, you could use SpeechRecognitionEngine. Here is an example, you can try to refer to it.

    Add a reference to System.Speech to the project.

    MainWindow.xaml:

      <Grid>  
            <Grid>  
                <Grid.RowDefinitions>  
                    <RowDefinition Height="30"/>  
                    <RowDefinition Height="30"/>  
                    <RowDefinition Height="*"/>  
                </Grid.RowDefinitions>  
                <Grid.ColumnDefinitions>  
                    <ColumnDefinition Width="120"/>  
                    <ColumnDefinition Width="120"/>  
                    <ColumnDefinition Width="120"/>  
                    <ColumnDefinition Width="*"/>  
                </Grid.ColumnDefinitions>  
      
                <TextBlock Name="tHypothesized"  Grid.Column="0" Foreground="Green" >Hypothesized</TextBlock>  
                <TextBlock Name="tRecognized"  Grid.Column="1" Foreground="Green" >Recognized</TextBlock>  
                <Button Name="btnStart"  Grid.Column="3" Content="Start" Click="btnStart_Click" Width="80" IsEnabled="False"></Button>  
                <Label Name="lStatus" Grid.Row="1" Grid.Column="0" FontSize="10" Foreground="Red">Status:</Label>  
                <TextBlock Name="stop" Grid.Row="1" Grid.Column="3" FontSize="10">Speak "End Dictate" to stop.</TextBlock>  
                <TextBox Name="TextBox1"  Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="4"  TextWrapping="Wrap" />  
            </Grid>  
        </Grid>  
    

    MainWinodw.xmal.cs:

    using System;  
    using System.Linq;  
    using System.Windows;  
    using System.Speech.Recognition;  
    using System.Threading;  
    using System.Speech.Synthesis;  
      
    namespace SpeechToTextDemo  
    {  
        public partial class MainWindow : Window  
        {  
            private enum State  
            {  
                Idle = 0,  
                Accepting = 1,  
                Off = 2,  
            }  
      
            private State RecogState = State.Off;  
            private SpeechRecognitionEngine srecog;  
            private SpeechSynthesizer synth = null;          
            private int Hypothesized = 0;  
            private int Recognized = 0;  
            public MainWindow()  
            {  
                InitializeComponent();  
                this.Loaded += new RoutedEventHandler(MainWindow_Loaded);  
            }  
            private void MainWindow_Loaded(object sender, RoutedEventArgs e)  
            {  
                InitializeRecognizerSynthesizer();  
      
                if (SelectInputDevice())  
                {  
                    LoadDictationGrammar();  
                    btnStart.IsEnabled = true;  
                    ReadAloud("Speech Engine Ready for Input");  
                }  
            }  
      
            private void InitializeRecognizerSynthesizer()  
            {  
                var selectedRecognizer = (from o in SpeechRecognitionEngine.InstalledRecognizers()  
                                          where o.Culture.Equals(Thread.CurrentThread.CurrentCulture)  
                                          select o).FirstOrDefault();  
                srecog = new SpeechRecognitionEngine(selectedRecognizer);  
                srecog.AudioStateChanged+=new EventHandler<AudioStateChangedEventArgs>(recognizer_AudioStateChanged);  
                srecog.SpeechHypothesized += new EventHandler<SpeechHypothesizedEventArgs>(recognizer_SpeechHypothesized);  
                srecog.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(recognizer_SpeechRecognized);  
      
                synth = new SpeechSynthesizer();  
            }  
      
            private bool SelectInputDevice()  
            {  
                bool proceedLoading = true;  
                if (IsOscompatible())  
                {  
                    try  
                    {  
                        srecog.SetInputToDefaultAudioDevice();  
                    }  
                    catch  
                    {  
                        proceedLoading = false;   
                    }  
                }  
                else  
                    ThreadPool.QueueUserWorkItem(InitSpeechRecogniser);  
                return proceedLoading;  
            }  
      
            private bool IsOscompatible()  
            {  
                OperatingSystem osInfo = Environment.OSVersion;  
                if (osInfo.Version > new Version("6.0"))  
                    return true;  
                else  
                    return false;  
            }  
      
            private void InitSpeechRecogniser(object o)  
            {  
                srecog.SetInputToDefaultAudioDevice();  
            }  
      
            private void LoadDictationGrammar()  
            {  
                GrammarBuilder grammarBuilder = new GrammarBuilder();  
                grammarBuilder.Append(new Choices("End Dictate"));  
                Grammar commandGrammar = new Grammar(grammarBuilder);  
                commandGrammar.Name = "main command grammar";  
                srecog.LoadGrammar(commandGrammar);  
      
                DictationGrammar dictationGrammar = new DictationGrammar();  
                dictationGrammar.Name = "dictation";  
                srecog.LoadGrammar(dictationGrammar);  
            }  
      
            private void recognizer_AudioStateChanged(object sender, AudioStateChangedEventArgs e)  
            {  
                switch (e.AudioState)  
                {  
                    case AudioState.Speech:  
                       lStatus.Content = "Listening";  
                        break;  
                    case AudioState.Silence:  
                        lStatus.Content = "Idle";  
                        break;  
                    case AudioState.Stopped:  
                        lStatus.Content = "Stopped";  
                        break;  
                }  
            }  
      
            private void recognizer_SpeechHypothesized(object sender, SpeechHypothesizedEventArgs e)  
            {  
                Hypothesized++;  
                tHypothesized.Text = "Hypothesized: " + Hypothesized.ToString();  
            }  
            private void recognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)  
            {  
                Recognized++;  
                tRecognized.Text = "Recognized: " + Recognized.ToString();  
      
                if (RecogState == State.Off)  
                    return;  
                float accuracy = (float)e.Result.Confidence;  
                string phrase = e.Result.Text;  
                {  
                    if (phrase == "End Dictate")  
                    {  
                        RecogState = State.Off;  
                        srecog.RecognizeAsyncStop();  
                        ReadAloud("Dictation Ended");  
                        return;  
                    }  
                    TextBox1.AppendText(" " + e.Result.Text);  
                }  
            }   
      
            public void ReadAloud(string speakText)  
            {  
                try  
                {  
                    srecog.RecognizeAsyncCancel();  
                    synth.SpeakAsync(speakText);  
                }  
                catch { }  
            }  
      
      
            private void btnStart_Click(object sender, RoutedEventArgs e)  
            {  
                switch (RecogState)  
                {  
                    case State.Off:  
                        RecogState = State.Accepting;  
                        btnStart.Content = "Stop";  
                        srecog.RecognizeAsync(RecognizeMode.Multiple);  
                        break;  
                    case State.Accepting:  
                        RecogState = State.Off;  
                        btnStart.Content = "Start";  
                        srecog.RecognizeAsyncStop();  
                        break;  
                }  
            }  
        }  
    }  
    

    ----------------------------------------------------------------------------

    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

4 additional answers

Sort by: Most helpful
  1. Olaf Helper 40,741 Reputation points
    2022-09-23T07:02:45.507+00:00

    Speech to Text
    speech and convert into text

    Which one now, Speech to text or text to speech?
    Search on internet, you will find for both examples & solutions.

    0 comments No comments

  2. Castorix31 81,636 Reputation points
    2022-09-23T07:47:07.043+00:00

    There should be SpeechRecognitionEngine,
    but I cannot make it work on my french OS (although I configured Speech Recognition in Control Panel)

    0 comments No comments

  3. Fatima Shahid 0 Reputation points Student Ambassador
    2023-08-02T08:47:03.1033333+00:00

    Open Visual Studio and create a new Console Application project.

    Add a reference to the System. Speech assembly. Right-click on the project in Solution Explorer, select Add Reference, and then choose System. Speech from the list of assemblies.

    In the Program.cs file

    0 comments No comments

  4. Fatima Shahid 0 Reputation points Student Ambassador
    2023-08-02T08:49:18.63+00:00
    Create WPF window as below
    
    <Window x:Class="Speech_to_Text.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="Speech to Text" Height="300" Width="525">
        <Grid>
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="*"/>
                    <RowDefinition Height="30"/>
                    <RowDefinition Height="25"/>
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="120"/>
                    <ColumnDefinition Width="120"/>
                    <ColumnDefinition Width="120"/>
                    <ColumnDefinition Width="*"/>
                </Grid.ColumnDefinitions>
                <TextBox Name="TextBox1"  Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="4"  TextWrapping="Wrap" />
                <Label Name="LabelHypothesized" Grid.Row="1" Grid.Column="0" Foreground="Green" >Hypothesized</Label>
                <Label Name="LabelRecognized" Grid.Row="1" Grid.Column="1" Foreground="Green" >Recognized</Label>
                <Button Name="ButtonStart" Grid.Row="1" Grid.Column="3" Content="Start" Click="ButtonStart_Click" Width="80" IsEnabled="False"></Button>
                <Label Name="LabelStatus" Grid.Row="2" Grid.Column="0" FontSize="10" Foreground="Red">Status:</Label>
                <Label Name="Label1" Grid.Row="2" Grid.Column="3" FontSize="10">Speak "End Dictate" to stop.</Label>
            </Grid>
        </Grid>
    </Window>
    
    0 comments No comments