Click here to Skip to main content
14,335,524 members
Rate this:
Please Sign up or sign in to vote.
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

Rate this:
Please Sign up or sign in to vote.

Solution 1

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() { }
}
   
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.
depak gupta 18-Mar-13 11:18am
   
Thanks sir for doing this for me ,I have also done same as you said but it is giving different from what I recorded and also not accurate please provide some advice about this.
Mike Meinz 18-Mar-13 11:44am
   
This is my first experience with speech recognition. I have read comments on other questions where programmers have tried to use the Microsoft speech recognition engine and experienced very poor results for dictation. The Microsoft speech recognition engine appears to work best if you create a grammar with just a few words (example: STOP START SLOW FAST UP DOWN LEFT RIGHT) and use it with an application that only requires recognition of those few words.

depak gupta 18-Mar-13 14:36pm
   
so,can you tell me how could I use this command so that when I say start it starts my application
Mike Meinz 18-Mar-13 17:18pm
   
The idea is you build it into your application so that your application will respond to a limited set of spoken commands.

To start your application or any other program via voice input, you would use the Speech Recognition feature built into Windows at Control Panel\Ease of Access\Speech Recognition (in Windows 8. May be found elsewhere in other Windows versions).

Again, remember that it is best when used with very limited number of words.
depak gupta 22-Mar-13 7:08am
   
sir, I have put code below but I don't know how to perform button click by command if possible check and improve it.
Mike Meinz 22-Mar-13 7:13am
   
When you determine that the correct text has been "heard", call the associated button's click event.
depak gupta 22-Mar-13 14:07pm
   
sorry here below is the code please check and tell how to implement button click by using command.
Mike Meinz 22-Mar-13 16:13pm
   
You can put those lines of code into the following and just call "doit".
private void doit()
{
record r = new record();
r.Show();
};


Or, if you absolutely must call the button click event, you need to supply parameters when you call the button's click event:
button1_Click(sender,e);

or, if that doesn' work, then this:
EventArgs e;
button1_Click(sender,e);
depak gupta 25-Mar-13 23:36pm
   
sir. I want it work with my speech command not by clicking button manually.
Mike Meinz 26-Mar-13 7:30am
   
Ot seems like you don't understand that I told you how to do exactly that. In order to clear the confusion, I have written the code for you. See the Solution 1 section labeled "Revised Solution - 26 March 2013 07:27AM EDT"
depak gupta 26-Mar-13 13:54pm
   
sir. I have put my code in "solution 3" but don't know how the error could be solved.
Mike Meinz 26-Mar-13 14:13pm
   
What error? Please provide detailed information about your problem.

Please make this change:
static void showRecord()
{
record r=new record();
r.show();
}

Also, it is considered bad manners here on CodeProject to post follow-up questions as "Solutions". You should use the Improve Question button at the top of this page and make changes to you original question.
depak gupta 27-Mar-13 10:26am
   
error is showing on r.show() and showRecord().
Mike Meinz 27-Mar-13 14:50pm
   
See revised Solution 1 above.

I put in some stub code for a class named "record" with a method named "Show". It compiled OK for me. Without seeing the exact syntax in your program and the exact error message, I am unable to guess what is causing your issue.

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100