Click here to Skip to main content
15,881,172 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How to convert wav file into Text format using asp.net mvc4

Hi,

i have a wave file and want to convert that into Text format using asp.net

i tried the below code but it not understood when audio file is end.

Index


HTML
@model MvcApplication1.Models.Home
@{
    ViewBag.Title = "Home Page";
}
We suggest the following:

@using (Html.BeginForm())
{
    <input type="button" onclick="callfun()" value="Get" />
    @Html.HiddenFor(m => m.txtval)
    @Html.LabelFor(m => m.txtval)
}
 
<script>
    function callfun() {
        $.ajax({
        url: 'Home/Find',
        dataType: "json",
        type: "GET",
        contentType: 'application/json; charset=utf-8',
                    //pageNo: 2, pageSize: 20 would not be posted to the action,
                    //it would be 3 and 5 as we specified it at ajax url
                    //and user would be null
        data: {},
        async: true,
        processData: false,
        cache: false,
        success: function (data) {
                    alert(data);
                },
        error: function (xhr) {
                    alert('error');
                }
        })
    }
</script>

HomeController


C#
public ActionResult Index()
{ 
    ViewBag.Message = "Modify this template to jump-start your ASP.NET MVC application.";
     
    return View("Index");
}
 
[HttpGet]
public async Task Find()
{
    SpeechRecognitionEngine sre = new SpeechRecognitionEngine();
    Grammar gr = new DictationGrammar();
     
    sre.LoadGrammar(gr);
    //sre.LoadGrammarAsync(gr);
    sre.SetInputToWaveFile("D:\\arth_mb.wav");
    //sre.SetInputToWaveStream("D:\\arth_mb.wav");
    sre.BabbleTimeout = new TimeSpan(Int32.MaxValue);
    sre.InitialSilenceTimeout = new TimeSpan(Int32.MaxValue);
    sre.EndSilenceTimeout = new TimeSpan(100000000);
    sre.EndSilenceTimeoutAmbiguous = new TimeSpan(100000000);
     
    sre.SpeechRecognized += new EventHandler(sre_SpeechRecognized);
    sre.RecognizeCompleted += new EventHandler(sre_RecognizeCompleted);
     
    Home hm = new Home();

    while (!completed)
    {
        try
        {
            var cnt = sre.Recognize().ToString().Length;
            var recText = sre.Recognize();
            
            if (recText == null)
            {
                break;
            } 
            hm.txtval += recText.Text;
        }
        catch (Exception ex)
        {
            //handle exception
            //...
            //break;
            Response.End();
            completed = true;
        }
    }
 
    return View(hm.txtval);
    //return Json(hm.txtval, JsonRequestBehavior.AllowGet);
}

static void sre_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;
}
 
static void sre_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
    Home hm = new Home();

    if (e.Result != null && e.Result.Text != null)
    {
        Console.WriteLine(" Recognized text = {0}", e.Result.Text);
    }
    else
    {
        Console.WriteLine(" Recognized text not available.");
    }
}
Posted
v3
Comments
Leo Chapiro 30-Jan-15 1:54am    
What have you tried, dude?
Kenneth Haugland 30-Jan-15 1:59am    
I was gonna say: call ToString() :)
Marcin Kozub 30-Jan-15 2:18am    
It could work if you build some custom class and use Speech recognition inside ;)

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