Click here to Skip to main content
15,879,095 members
Please Sign up or sign in to vote.
1.00/5 (4 votes)
See more:
How can I convert speech (recorded .wav file) to a text file. I have written C# code as shown below. Please check and tell me is everything is proper or not. I have run this code and I am getting an error.


using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Speech.Recognition;
using System.Speech.AudioFormat;

namespace reco
{
    public partial class Form1 : Form
    {
        private SpeechRecognitionEngine recognitionEngine;

        public Form1()
        {
            InitializeComponent();
            try
            {
               
                recognitionEngine = new SpeechRecognitionEngine();
                DictationGrammar dg = new DictationGrammar();
                recognitionEngine.LoadGrammar(dg);
                recognitionEngine.SetInputToDefaultAudioDevice();

                recognitionEngine.SetInputToWaveFile(@"C:\Users\Deep\Music\New folder\mp3.wav");
                recognitionEngine.EndSilenceTimeout = new TimeSpan(0, 0, 2);
                recognitionEngine.Recognize();
                recognitionEngine.RecognizeAsync();
                recognitionEngine.SpeechHypothesized += new EventHandler<speechhypothesizedeventargs>(reco_SpeechHypothesized);
                recognitionEngine.SpeechRecognized += new EventHandler<speechrecognizedeventargs>(reco_SpeechRecognized);
                recognitionEngine.RecognizeCompleted += new EventHandler<recognizecompletedeventargs>(reco_RecognizeCompleted);
                recognitionEngine.AudioSignalProblemOccurred += new EventHandler<audiosignalproblemoccurredeventargs>(reco_AudioSignalProblemOccurred);
                recognitionEngine.SpeechDetected += new EventHandler<speechdetectedeventargs>(reco_SpeechDetected);
                recognitionEngine.SpeechRecognitionRejected += new EventHandler<speechrecognitionrejectedeventargs>(reco_SpeechRecognitionRejected);

                recognitionEngine.RecognizeAsync(RecognizeMode.Multiple);
            }
            catch (Exception e)
            {
                MessageBox.Show("the Error"+e);
            }
        }
        private void Form1_Load(object sender, EventArgs e)
        {

        }
        void reco_AudioSignalProblemOccurred(object sender, AudioSignalProblemOccurredEventArgs e)
        {
            richTextBox1.AppendText(e.AudioSignalProblem.ToString());
        }

        void reco_SpeechHypothesized(object sender, SpeechHypothesizedEventArgs e)
        {
            richTextBox1.AppendText(e.Result.Text);
        }

        void reco_SpeechRecognitionRejected(object sender, SpeechRecognitionRejectedEventArgs e)
        {
            richTextBox1.AppendText("Rejected!");
        }

        void reco_RecognizeCompleted(object sender, RecognizeCompletedEventArgs e)
        {
            richTextBox1.AppendText("Recognition Complete!");
        }

        void reco_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
        {
            richTextBox1.AppendText(e.Result.Text);
        }

        void reco_SpeechDetected(object sender, SpeechDetectedEventArgs e)
        {
            richTextBox1.AppendText("Speech Detected!");
        }

     
      
         
        }
    }</speechrecognitionrejectedeventargs></speechdetectedeventargs></audiosignalproblemoccurredeventargs></recognizecompletedeventargs></speechrecognizedeventargs></speechhypothesizedeventargs>
Posted
Updated 17-Mar-13 6:16am
v2
Comments
Mike Meinz 17-Mar-13 12:17pm    
Please indicate the error that you are getting and which line of code gets the error. Use the Visual Studio interactive debugger to help you.

1 solution

This version compiles. You weren't clear enough to say whether it was a compile error or a run-time error. If your problem was a compile error, this version resolves that. Because it is C# code, the upper and lower case must match. I corrected the <event args=""> clauses to use upper and lower case like this: <SpeechRecognizedEventArgs>.

using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Speech.Recognition;
using System.Speech.AudioFormat;
 
namespace reco
{
    public partial class Form1 : Form
    {
        private SpeechRecognitionEngine recognitionEngine;
 
        public Form1()
        {
            InitializeComponent();
            try
            {
               
                recognitionEngine = new SpeechRecognitionEngine();
                DictationGrammar dg = new DictationGrammar();
                dg.Name = "Dictation Grammar";
                recognitionEngine.LoadGrammar(dg);
                recognitionEngine.SetInputToDefaultAudioDevice();
                //recognitionEngine.SetInputToWaveFile(@"C:\Users\Deep\Music\New folder\mp3.wav");
                recognitionEngine.SetInputToDefaultAudioDevice();  // Gets sound from microphone
                recognitionEngine.EndSilenceTimeout = new TimeSpan(0, 0, 2);
                // Following two lines are not needed. They cause error!
                //recognitionEngine.Recognize();
                //recognitionEngine.RecognizeAsync();
                recognitionEngine.SpeechHypothesized += new EventHandler<SpeechHypothesizedEventArgs>(reco_SpeechHypothesized);
                recognitionEngine.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(reco_SpeechRecognized);
                recognitionEngine.RecognizeCompleted += new EventHandler<RecognizeCompletedEventArgs>(reco_RecognizeCompleted);
                recognitionEngine.AudioSignalProblemOccurred += new EventHandler<AudioSignalProblemOccurredEventArgs>(reco_AudioSignalProblemOccurred);
                recognitionEngine.SpeechDetected += new EventHandler<SpeechDetectedEventArgs>(reco_SpeechDetected);
                recognitionEngine.SpeechRecognitionRejected += new EventHandler<SpeechRecognitionRejectedEventArgs>(reco_SpeechRecognitionRejected);
                recognitionEngine.RecognizeAsync(RecognizeMode.Multiple);
            }
            catch (Exception e)
            {
                MessageBox.Show("Error "+e);
            }
        }
        private void Form1_Load(object sender, EventArgs e)
        {
 
        }
        void reco_AudioSignalProblemOccurred(object sender, AudioSignalProblemOccurredEventArgs e)
        {
            richTextBox1.AppendText(e.AudioSignalProblem.ToString());
        }
 
        void reco_SpeechHypothesized(object sender, SpeechHypothesizedEventArgs e)
        {
            richTextBox1.AppendText(e.Result.Text);
        }
 
        void reco_SpeechRecognitionRejected(object sender, SpeechRecognitionRejectedEventArgs e)
        {
            richTextBox1.AppendText("Rejected!");
        }
 
        void reco_RecognizeCompleted(object sender, RecognizeCompletedEventArgs e)
        {
            richTextBox1.AppendText("Recognition Complete!");
        }
 
        void reco_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
        {
            richTextBox1.AppendText(e.Result.Text);
        }
 
        void reco_SpeechDetected(object sender, SpeechDetectedEventArgs e)
        {
            richTextBox1.AppendText("Speech Detected!");
        }

             
        }
    }


Revised Solution - 27 March 2013 2:44PM EDT:
    static void speech1(object sender, SpeechRecognizedEventArgs e)
    {
        if (e.Result.Text == "record")
        {
            showRecord();
        }
        else if (e.Result.Text == "exit")
        {
            Application.Exit();
        }
    }

    private void button1_Click(object sender, EventArgs e)
    {
        showRecord();
    }
    static void showRecord()
    {
    record r=new record();
    r.show();
    }
}
public class record
{
     public  void show() { }
}
 
Share this answer
 
v7
Comments
depak gupta 17-Mar-13 13:59pm    
i'm getting run time error at ( recognitionEngine.SetInputToWaveFile(@"C:\Users\Deep\Music\New folder\mp3.wav");)line no. 30 as file not found ,is the way of writing path is proper
Mike Meinz 17-Mar-13 17:51pm    
The fullpathfilename format looks correct to me.

Is "Deep" your username?
Is there a folder named "New folder" in your "Music"?
Is there a file named "mp3.wav" in the "New folder"?
depak gupta 18-Mar-13 3:44am    
yeah ,it's all correct but same error please run it in your system and reply.
waiting for your's reply.
Mike Meinz 18-Mar-13 7:48am    
I ran on my PC replacing your username with my username. I did not have a .WAV file with speech in it so I tried both an .MP3 file and a .WMA file. Both of those caused a FormatException. I guess that the speech recognition engine can't handle MP3 or WMA formats. I took one of the Windows system .WAV sound files, moved it to the "New folder" and renamed it to your filename "mp3.wav". The program found the file OK but then threw an InvalidOperationException on execution of the RecognizeAsync method. You can't do both Sync and Async. You have to choose one. Because you set the events that are thrown when using Async mode and because you have a second RecognizeAsync at the end of your code, I commented out the following lines and got "Recognition Complete!" in the RichTextBox. Of course, because I did not have any speech in the file, there was no further text appended to the RichTextBox.

Commented out:
//recognitionEngine.Recognize();
//recognitionEngine.RecognizeAsync();

Are you sure your username is correct in the fullpathfilename?
Are you sure your file is called "mp3.wav"?
Are you sure that the folder is called "New folder"?
Are you sure you are getting a file not found error and not a format exception error?
Did you rename a sound file and change the file extension from MP3 to WAV?
Mike Meinz 18-Mar-13 8:31am    
I added the following statement after the new DictationGrammar(); statement:
dg.Name = "Dictation Grammar";

I replaced the SetInputToWaveFile statement with the following:
recognitionEngine.SetInputToDefaultAudioDevice();

It recognized speech using my headset microphone. It wasn't 100% accurate but I could see that it recognized many words that I spoke.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900