Skip to content

Converting or Transcribing audio to text using C# and .NET System.Speech

Recently, I had a project where I needed to convert some audio to text. It took a bit more googling than I was used to in order to find the code, so I went ahead and whipped up a project that demonstrates its usage, so people can more easily find it.

This code uses the .NET System.Speech namespace and demonstrates how to transcribe audio using either a microphone or a previously created .wav file using C#.

The code can be divided into 2 main parts:

Step 1: Configuring the SpeechRecognitionEngine

_speechRecognitionEngine = new SpeechRecognitionEngine();
_speechRecognitionEngine.SetInputToDefaultAudioDevice();
_dictationGrammar = new DictationGrammar();
_speechRecognitionEngine.LoadGrammar(_dictationGrammar);
_speechRecognitionEngine.RecognizeAsync(RecognizeMode.Multiple);

At this point, your object is ready to start transcribing audio from the microphone. You need to handle some events though, in order to actually get access to the results.

Step 2: Handling the SpeechRecognitionEngine Events

_speechRecognitionEngine.SpeechRecognized -= new EventHandler(SpeechRecognized);
_speechRecognitionEngine.SpeechHypothesized -= new EventHandler(SpeechHypothesizing);_speechRecognitionEngine.SpeechRecognized += new EventHandler(SpeechRecognized);
_speechRecognitionEngine.SpeechHypothesized += new EventHandler(SpeechHypothesizing);

private void SpeechHypothesizing(object sender, SpeechHypothesizedEventArgs e)
{
///real-time results from the engine
     string realTimeResults = e.Result.Text;
}

private void SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
///final answer from the engine
     string finalAnswer = e.Result.Text;
}

That’s it. If you want to use a pre-recorded .wav file instead of a microphone, you would use _speechRecognitionEngine.SetInputToWaveFile(pathToTargetWavFile); instead of _speechRecognitionEngine.SetInputToDefaultAudioDevice();.

There are a bunch of different options in these classes and they are worth exploring in more detail. This covers the bare essentials for a prototype.

25 thoughts on “Converting or Transcribing audio to text using C# and .NET System.Speech”

  1. Awesome! I was looking for exactly this and having the code project with it made it even sweeter! Have you planned on doing anything more with the speech recognition? I’m just starting out using it and C# for that matter (coming from Java) for home automation. Would be interested in any other projects related to speech recognition. Thanks again!

    1. Hey Nicholas,

      I really do not have any more plans for working with speech recognition. I found the Microsoft solution and a Google solution. I would like to get my hands on the Dragon NaturallySpeaking SDK and see how good that is, but I have no plans in the immediate future for working on it.

      Good luck with your home automation and thanks for responding to my blog!

      1. hi Micheal, thanks for help, nowadays i am working on Speech Recognition project , in which i have to convert video files into text, here i have tried to convert audio file( .mp3 ) into text through your code but its not properly working , so please do u tell me the code so i can proceed trough my work. please reply as soon as possible.

        1. i am working on Speech Recognition project , in which i have to convert video or audio files into text,if you done this workplz send me the code or project
          email ::kanlalmuhammad@yahoo.com

      2. hey admin can u plz do speech recognition in hindi which read as a text file in hindi……plz do it if u can…

    2. Hey can you please send me the code , i have also looking for this audio to text conversion in C# visual studio.. i have tried above code. but i getting through so many errors, so please help, will b very grateful to you

      1. am working on Speech Recognition project , in which i have to convert video or audio files into text,if you done this workplz send me the code or project
        email ::kanlalmuhammad@yahoo.com
        if u done thiswork send it to me

  2. Like the article. Do you know if its possible to have a voice recognition system which identifies a certain word or words when someone speaks it or them and then converts it to text with a time when the word was spoken? Can it be incorporated into an ipad or tablet? If I have a sales person and I want them to emphasise the brand name for example, it will display as text every time they mentioned the brand name.

  3. hey it is awesome but there are some problem with this code
    1.Not converting long audio file(more than 1 min) in proper format
    2.converted txt not match with audio voice
    Please give me some solutions as soon as possible
    thanks

  4. Where could I read to actually build a command line application that would take microphone input or a wav file and provide me with a cout of the text in visual studio 2010

  5. I want to get involved in tts and stt functioning appl production. Because in special environment of which deaf person recognize the message and normal person’s environment requires such app in order to communicate each other. Well, sometimes I thought only text can solve the problem. But the trend is going to where apple understand and transfer the sound or text into counter format of message ie text, sound. If normal person like to speak out then the apple should be adjusted according to its trend. Likewise if deaf person like to send text message then appl shoud do so. Well all of these things depend on the this era’s trend. Anyway I want to get some help in terms of compiler and SDK. I am in need of detailed link and kind explanation as I am novice to this STT, TTS area. God bless you all!!! What if I download from this link? visualstudio

  6. hi,
    i wish to know about building the grammer. how many words can be added in the grammer so that it works fine ? Also i am getting the accuracy of the recognized words close to 30 % ! is there a way to increase this accuracy ?
    any help is appreciated.
    thank you

  7. CAN YOU PLEASE TELL ME IF THIS CODE ENABLES THE SPEECH OR AUDIO TO BE CONVERTED INTO TEXT IN ANY TEXT FIELD OR JUST FOR A TEXT BOX IN THE IDE??

  8. CAN YOU PLEASE TELLVME IF THIS CODE IS ABLE TO CONVERT THE VOICE (SPEECH OR SOUND) INTO TEXT IN ANY TEXT FIELD SUCH AS, MS WORD, MS POWERPOINT, NOTPAD ETC, OR I HAVE TO CREAT A TEXT FIELD LIKE A TEXT BOX??

  9. I am working on a project in visual studio with C# where I need to covert speech into text. I am getting codes for window based application but I need for web based application. Kindly Help.

  10. Hi I’m working on a project and need audio converter in real time text, ie, that is to capture the package of rtp and converter in wav and use a tool, thinking of a level of sales ie 50 calls, you can do using your example?

Leave a Reply

Your email address will not be published. Required fields are marked *