none
Speech recognition alert!! NEED help converting code from console to winforms application

    Question

  • Hello everyone!

    I am doing a speech recognition application where a user will enter the path to .wav file and the program will start the speech recognition process on said .wav file. this goes all fine and dandy in the .NET console application but doesn't work completely in the winforms. If someone can shed some light into my problems that would be great!!

    here's my .NET code for the SR:

    class Program 
        { 
            static bool completed; 
            static string globePath; 
            static List<string> transList = new List<string>(); 
            static string[] transArray; 
            static List<TimeSpan> posList = new List<TimeSpan>(); 
            static TimeSpan[] posArray; 
            static List<int> indices = new List<int>();
             
            static void Main(string[] args) 
            { 
                try 
                { 
                    // Initialize an in-process speech recognition engine. 
                    speechRecognition(); 
                } 
                catch (Exception ex) 
                { 
                    Console.WriteLine(ex.Message, "error"); 
                    speechRecognition(); 
                } 
    
                //End the programm 
                Console.WriteLine(); 
                Console.WriteLine("Press any key to exit..."); 
                Console.ReadKey(); 
            }
    
            //Implementation of speech recognition process 
            static void speechRecognition() 
            { 
                using (SpeechRecognitionEngine recognizer = 
                 new SpeechRecognitionEngine()) 
                { 
                    // Create and load a grammar. 
                    Grammar dictation = new DictationGrammar(); 
                    dictation.Name = "Dictation Grammar"; 
                    recognizer.LoadGrammar(dictation);
    
                    //get the path to the audio file 
                    Console.WriteLine("Enter Path of WAV file; e.g.(c:\\te...\\*.wav):");
    
                    globePath = Console.ReadLine();
    
                    // Configure the input to the recognizer. 
                    recognizer.SetInputToWaveFile(@globePath); 
    
                    // Attach event handlers for the results of recognition. 
                    recognizer.SpeechRecognized += 
                      new EventHandler<SpeechRecognizedEventArgs>(SpeechRecognizedHandler);
    
                    recognizer.SpeechRecognized += 
                      new EventHandler<SpeechRecognizedEventArgs>(recognizer_SpeechRecognized);
    
                    recognizer.RecognizeCompleted += 
                      new EventHandler<RecognizeCompletedEventArgs>(recognizer_RecognizeCompleted); 
    
                    // Perform recognition on the entire file. 
                    Console.WriteLine("Starting asynchronous recognition..."); 
                    completed = false; 
                    recognizer.RecognizeAsync(RecognizeMode.Multiple);
    
                    // Keep the console window open. 
                    while (!completed) 
                    {
                        Thread.Sleep(333); 
                    } 
                    //store the transcribe audio in an array 
                    transArray = transList.ToArray();
                    //store the timespan of the audio in an array
                    posArray = posList.ToArray();
                    Console.WriteLine("Done.");
                }
            }
    
            // Handle the SpeechRecognized event.
            static void recognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
            {
                if (e.Result != null && e.Result.Text != null)
                {
                    //store the transcribed audio in txt
                    string txt = e.Result.Text;
                    //add the txt into the list
                    transList.Add(txt);
                    //print the transcribed text
                    Console.WriteLine("  Recognized text =  {0} ", txt);
                }
                else
                {
                    Console.WriteLine("  Recognized text not available.");
                }
            }
    
            // Handle the SpeechRecognized event. 
            static void SpeechRecognizedHandler(object sender, SpeechRecognizedEventArgs e)
            {
                if (e.Result == null) return;
    
                //get the position of the recognized audio
                if (e.Result.Audio != null)
                {
                    RecognizedAudio audio = e.Result.Audio;
                    posList.Add(audio.AudioPosition);
                }
            }
    
            // Handle the RecognizeCompleted event.
            static void recognizer_RecognizeCompleted(object sender, RecognizeCompletedEventArgs e)
            {
                if (e.Error != null)
                {
                    Console.WriteLine("  Error encountered, {0}: {1}",
                    e.Error.GetType().Name, e.Error.Message);
                }
                if (e.Cancelled)
                {
                    Console.WriteLine("  Operation cancelled.");
                }
                if (e.InputStreamEnded)
                {
                    Console.WriteLine("  End of stream encountered.");
                }
                completed = true;
            }
    }

    and here's my winforms application code:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using System.IO;
    using System.Speech.Recognition;
    using System.Speech.AudioFormat;
    using System.Threading;
    using System.Diagnostics;
    using System.Media;
    using NAudio;
    using NAudio.Wave;
    using NAudio.Wave.SampleProviders;
    
    namespace Phrase_Finder_Interface
    {
        public partial class Form1 : Form
        {
            
            static bool completed;
            static List<string> transList = new List<string>();
            static string[] transArray;
            static List<TimeSpan> posList = new List<TimeSpan>();
            static TimeSpan[] posArray;
            static List<int> indices = new List<int>();
    
            public Form1()
            {
                InitializeComponent();
            }
            
    
            private void Form1_Load(object sender, EventArgs e)
            {
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                OpenFileDialog open = new OpenFileDialog();
                open.Filter = "wave file (*.wav) | *.wav;";
                if (open.ShowDialog() != DialogResult.OK)
                {
                    return;
                }else
                {
                speechRecognition(open.FileName);
                }
            }
            
            
     //Implementation of speech recognition process
            public static string[] speechRecognition(string globePath)
            {
                using (SpeechRecognitionEngine recognizer =
                 new SpeechRecognitionEngine())
                {
                    // Create and load a grammar.
                    Grammar dictation = new DictationGrammar();
                    dictation.Name = "Dictation Grammar";
                    recognizer.LoadGrammar(dictation);
                    
                    //get the path to the audio file
                   // Console.WriteLine("enter name of WAV file:");
                   // globePath = "C:\\temp\\" + Console.ReadLine();
                   
                    // Configure the input to the recognizer.
                    recognizer.SetInputToWaveFile(@globePath);
                    
                    // Attach event handlers for the results of recognition. 
                    recognizer.SpeechRecognized +=        
                      new EventHandler<SpeechRecognizedEventArgs>(SpeechRecognizedHandler);
    
                    recognizer.SpeechRecognized +=
                      new EventHandler<SpeechRecognizedEventArgs>(recognizer_SpeechRecognized);
    
                    while()
                    recognizer.Recognize();
    
                    recognizer.RecognizeCompleted +=
                      new EventHandler<RecognizeCompletedEventArgs>(recognizer_RecognizeCompleted);
    
                    // Perform recognition on the entire file.
                    // Console.WriteLine("Starting asynchronous recognition...");
                    completed = false;
                    recognizer.RecognizeAsync(RecognizeMode.Multiple);
                    // Keep the console window open.
                    while (!completed)
                    {
                        Thread.Sleep(333);
                        //   Console.ReadLine();
                    }
    
                    //store the transcribe audio in an array
                    transArray = transList.ToArray();
    
                    //store the timespan of the audio in an array
                    posArray = posList.ToArray();
    
                    // Console.WriteLine("Done.");
                    return transArray;
                }
    
            }
    
            // Handle the SpeechRecognized event.
            static void recognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
            {
                if (e.Result != null && e.Result.Text != null)
                {
                    //store the transcribed audio in txt
                    string txt = e.Result.Text;
    
                    //add the txt into the list
                    transList.Add(txt);
    
                    //print the transcribed text
                    // Console.WriteLine("  Recognized text =  {0} ", txt);
                }
                else
                {
                    // Console.WriteLine("  Recognized text not available.");
                }
            }
            
            // Handle the SpeechRecognized event. 
            static void SpeechRecognizedHandler(object sender, SpeechRecognizedEventArgs e)
            {
                if (e.Result == null) return;
                //get the position of the recognized audio
                if (e.Result.Audio != null)
                {
                    RecognizedAudio audio = e.Result.Audio;
                    posList.Add(audio.AudioPosition);
                }
            }
    
            // Handle the RecognizeCompleted event.
            static void recognizer_RecognizeCompleted(object sender, RecognizeCompletedEventArgs e)
            {
                if (e.Error != null)
                {
                    // Console.WriteLine("  Error encountered, {0}: {1}",
                    //e.Error.GetType().Name, e.Error.Message);
                }
                if (e.Cancelled)
                {
                   // Console.WriteLine("  Operation cancelled.");
                }
                if (e.InputStreamEnded)
                {
                   // Console.WriteLine("  End of stream encountered.");
                }
                completed = true;
            }
        }
    }
    


    Thanks again for the help.

    Wednesday, April 5, 2017 9:33 AM

All replies

  • ... but doesn't work completely in the winforms.

    What *exactly* is that supposed to mean? Are we expected to guess what
    "doesn't work"? The more time and effort you take to describe your
    problems, the more time and effort others will likely expend trying
    to help.

    - Wayne

    Wednesday, April 5, 2017 9:54 AM
  • Hi ms.A,

    Could you try debugging your code to check what is happening. Also debug at the following line in your code see if it loading the file correctly in this function public static string[] speechRecognition(string globePath).

     recognizer.SetInputToWaveFile(@globePath);

    I think file might not be loading correctly. Also if could please provide more information what you meant by "doesn't work in WinForm", will help us to give you appropriate solution.


    Thanks,
    Sabah Shariq

    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered "Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]

    Wednesday, April 5, 2017 2:17 PM
    Moderator
  •  the console application takes the entire .wav file and using the event handlers for the speech recognition to transcribe the audio into text. if the audio has  some sort of breaks in between the words it will start the speech recognition for the next part. the code is storing these texts in a list. when converting the code into a Windows Forms Application, it enters into the loop:

     // Keep the console window open. 
                    while (!completed) 
                    {
                        Thread.Sleep(333); 
                    } 

    and stays there. I tried adding this bit:

    recognizer.Recognize();

    but it only managed to get the first audio to the break point and not the entire .wav file.

    note: the audio break point is every time the speaker stops talking for a second or so.

    thanks again for the help.

    Thursday, April 6, 2017 8:54 AM