Click here to Skip to main content
15,850,214 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Dear Friends,


I hope all are doing well. I need one help, How to convert audio file to text in c#.
Posted
Updated 24-Mar-23 17:23pm

//If you need to convert a mp3 to a wav you will need to Nuget NAudio.

public async Task<string> ConvertSpeech()
{
SpeechRecognitionEngine sre = new SpeechRecognitionEngine();
Grammar gr = new DictationGrammar();
sre.LoadGrammar(gr);

string InputAudioFilePath = ""; // @"c:\logs\views3files.mp3";
string OutputAudioFilePath = @"C:\Logs\views3Files.wav";
if (InputAudioFilePath != String.Empty)
{
using (Mp3FileReader reader = new Mp3FileReader(InputAudioFilePath))
{
WaveFileWriter.CreateWaveFile(OutputAudioFilePath, reader);
}
}
sre.SetInputToWaveFile(@"C:\Logs\views3files.wav");
sre.BabbleTimeout = new TimeSpan(Int32.MaxValue);
sre.InitialSilenceTimeout = new TimeSpan(Int32.MaxValue);
sre.EndSilenceTimeout = new TimeSpan(100000000);
sre.EndSilenceTimeoutAmbiguous = new TimeSpan(100000000);
StringBuilder sb = new StringBuilder();
while (true)
{
try
{
var recText = sre.Recognize();
if (recText == null)
{
break;
}
sb.Append(recText.Text);
}
catch (Exception ex)
{
//handle exception
//...

break;
}
}
System.IO.File.WriteAllText(@"c:\logs\audio.txt", sb.ToString());
return sb.ToString();
}
 
Share this answer
 
Comments
Dave Kreskowiak 25-Mar-23 11:20am    
This is no different from the example that was posted seven years ago.
C#
SpeechRecognitionEngine sre = new SpeechRecognitionEngine();
Grammar gr = new DictationGrammar();
sre.LoadGrammar(gr);
sre.SetInputToWaveFile("C:\\Users\\Soham Dasgupta\\Downloads\\Podcasts\\Engadget_Podcast_353.wav");
sre.BabbleTimeout = new TimeSpan(Int32.MaxValue);
sre.InitialSilenceTimeout = new TimeSpan(Int32.MaxValue);
sre.EndSilenceTimeout = new TimeSpan(100000000);
sre.EndSilenceTimeoutAmbiguous = new TimeSpan(100000000); 

StringBuilder sb = new StringBuilder();
while (true)
{
    try
    {
        var recText = sre.Recognize();
        if (recText == null)
        {               
            break;
        }

        sb.Append(recText.Text);
    }
    catch (Exception ex)
    {   
        //handle exception      
        //...

        break;
    }
}
return sb.ToString();
 
Share this answer
 
 
Share this answer
 
Comments
[no name] 9-Dec-15 2:14am    
Dear DanielBrownAU,

First Thank you so much. I tried it read only .wav formats. It will read all format audio files ?
DanielBrownAU 9-Dec-15 17:01pm    
I beleive so yes, but its been awhile since I used thoese API's
 
Share this answer
 
Comments
[no name] 9-Dec-15 1:42am    
Dear Mehdi Gholam,

First Thank you so much. I tried it read only .wav formats. It will read all format audio files ?

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