Click here to Skip to main content
14,830,769 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
C#
 Am enhancing en existing web application which makes the visually impaired people, to be used based on the Text to Speech & Speech Recognition



Here is the code, please do the needful.



C#
using System.Speech;
using System.Speech.Synthesis;
using System.Threading;
using System.Speech.Recognition;



 SpeechSynthesizer ss = new SpeechSynthesizer();
    PromptBuilder pb = new PromptBuilder();
    SpeechRecognitionEngine sre = new SpeechRecognitionEngine(); 
    SpeechRecognizer sr = new SpeechRecognizer();

   protected void btn_Spk_Click(object sender, EventArgs e)    
    {
        string optiona, optionb, optionc;
        Choices clist = new Choices();
        foreach (GridViewRow row in gvtemperament.Rows)
        {
            RadioButton rblist1 = ((RadioButton)row.FindControl("rdTemp_Opt1"));
            RadioButton rblist2 = ((RadioButton)row.FindControl("rdTemp_Opt2"));
            RadioButton rblist3 = ((RadioButton)row.FindControl("rdTemp_Opt3"));
            Label lbl1 = (Label)row.FindControl("lbl_desc");
            if (row.Cells[0].Text == "1")
            {
                ss.SelectVoiceByHints(VoiceGender.Female);
                ss.Volume = 100;
                ss.Rate = -3;
                pb.ClearContent();
                pb.AppendText("Please answer all the Questions and you starts here!");
                ss.Speak(pb);
                Thread.Sleep(1500);

                pb.ClearContent();
                pb.AppendText(lbl1.Text);
                ss.Speak(pb);
                Thread.Sleep(1500);

                ss.SelectVoiceByHints(VoiceGender.Male);
                pb.ClearContent();
                pb.AppendText("Options are ");
                ss.Speak(pb);

                optiona = rblist1.Text;
                optionb = rblist2.Text;
                optionc = "Not Sure";

                
                pb.ClearContent();
                pb.AppendText(optiona);
                ss.Speak(pb);
                Thread.Sleep(700);

                pb.ClearContent();
                pb.AppendText(optionb);
                ss.Speak(pb);
                Thread.Sleep(700);

                pb.ClearContent();
                pb.AppendText(optionc);
                ss.Speak(pb);

                Thread.Sleep(150);

                clist.Add(new string[] { "Yes", "No", "Not Sure" });
                Grammar gr = new Grammar(new GrammarBuilder(clist));
                try
                {
                    sre.RequestRecognizerUpdate();
                    sre.LoadGrammar(gr);
                    sre.SpeechRecognized += sre_SpeechRecognized;
                    sre.SetInputToDefaultAudioDevice();
                    sre.RecognizeAsync(RecognizeMode.Multiple);
                }
                catch (Exception ex)
                {
                     this.ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script language=\"javascript\">" + "alert('Error');</script>");
                }
            }
            else if (row.Cells[0].Text == "2")
            {
                ss.SelectVoiceByHints(VoiceGender.Female);
                ss.Volume = 100;
                ss.Rate = -3;

                pb.ClearContent();
                pb.AppendText(lbl1.Text);
                ss.Speak(pb);
                Thread.Sleep(1500);

                ss.SelectVoiceByHints(VoiceGender.Male);
                pb.ClearContent();
                pb.AppendText("Options are ");
                ss.Speak(pb);

                optiona = rblist1.Text;
                optionb = rblist2.Text;
                optionc = "Not Sure";

 
                pb.ClearContent();
                pb.AppendText(optiona);
                ss.Speak(pb);
                Thread.Sleep(700);

                pb.ClearContent();
                pb.AppendText(optionb);
                ss.Speak(pb);
                Thread.Sleep(700);
 
                pb.ClearContent();
                pb.AppendText(optionc);
                ss.Speak(pb);

                Thread.Sleep(150);
                clist.Add(new string[] { "Yes", "No", "Not Sure" });
                Grammar gr = new Grammar(new GrammarBuilder(clist));
                try
                {
                    sre.RequestRecognizerUpdate();
                    sre.LoadGrammar(gr);
                    sre.SpeechRecognized += sre_SpeechRecognized;
                    sre.SetInputToDefaultAudioDevice();
                    sre.RecognizeAsync(RecognizeMode.Multiple);
                }
                catch (Exception ex)
                {
                     this.ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script language=\"javascript\">" + "alert('Error');</script>");
                }
            }
            else if (row.Cells[0].Text == "3")
            {
                ss.SelectVoiceByHints(VoiceGender.Female);
                ss.Volume = 100;
                ss.Rate = -3;

                pb.ClearContent();
                pb.AppendText(lbl1.Text);
                ss.Speak(pb);
                Thread.Sleep(1500);

                ss.SelectVoiceByHints(VoiceGender.Male);
                pb.ClearContent();
                pb.AppendText("Options are ");
                ss.Speak(pb);

                optiona = rblist1.Text;
                optionb = rblist2.Text;
                optionc = "Not Sure";
                                                           
                pb.ClearContent();
                pb.AppendText(optiona);
                ss.Speak(pb);
                Thread.Sleep(700);

                pb.ClearContent();
                pb.AppendText(optionb);
                ss.Speak(pb);
                Thread.Sleep(700);

                pb.ClearContent();
                pb.AppendText(optionc);
                ss.Speak(pb);
                Thread.Sleep(150);
                clist.Add(new string[] { "Yes", "No", "Not Sure" });
                Grammar gr = new Grammar(new GrammarBuilder(clist));
                try
                {
                    sre.RequestRecognizerUpdate();
                    sre.LoadGrammar(gr);
                    sre.SpeechRecognized += sre_SpeechRecognized;
                    sre.SetInputToDefaultAudioDevice();
                    sre.RecognizeAsync(RecognizeMode.Multiple);
                }
                catch (Exception ex)
                {
                      this.ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script language=\"javascript\">" + "alert('Error');</script>");
                }
            }
            else if (row.Cells[0].Text == "4")
            {
                ss.SelectVoiceByHints(VoiceGender.Female);
                ss.Volume = 100;
                ss.Rate = -3;

                pb.ClearContent();
                pb.AppendText(lbl1.Text);
                ss.Speak(pb);
                Thread.Sleep(1500);

                ss.SelectVoiceByHints(VoiceGender.Male);
                pb.ClearContent();
                pb.AppendText("Options are ");
                ss.Speak(pb);

                optiona = rblist1.Text;
                optionb = rblist2.Text;
                optionc = "Not Sure";
                                     
                pb.ClearContent();
                pb.AppendText(optiona);
                ss.Speak(pb);
                Thread.Sleep(700);

                pb.ClearContent();
                pb.AppendText(optionb);
                ss.Speak(pb);
                Thread.Sleep(700);

                pb.ClearContent();
                pb.AppendText(optionc);
                ss.Speak(pb);
                Thread.Sleep(150);
                clist.Add(new string[] { "Yes", "No", "Not Sure" });
                Grammar gr = new Grammar(new GrammarBuilder(clist));
                try
                {
                    sre.RequestRecognizerUpdate();
                    sre.LoadGrammar(gr);
                    sre.SpeechRecognized += sre_SpeechRecognized;
                    sre.SetInputToDefaultAudioDevice();
                    sre.RecognizeAsync(RecognizeMode.Multiple);
                }
                catch (Exception ex)
                {
                    this.ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script language=\"javascript\">" + "alert('Error');</script>");
                }
            }
            else if (row.Cells[0].Text == "5")
            {
                ss.SelectVoiceByHints(VoiceGender.Female);
                ss.Volume = 100;
                ss.Rate = -3;
                pb.ClearContent();
                pb.AppendText(lbl1.Text);
                ss.Speak(pb);
                Thread.Sleep(1500);

                ss.SelectVoiceByHints(VoiceGender.Male);
                pb.ClearContent();
                pb.AppendText("Options are ");
                ss.Speak(pb);

                optiona = rblist1.Text;
                optionb = rblist2.Text;
                optionc = "Not Sure";

                pb.ClearContent();
                pb.AppendText(optiona);
                ss.Speak(pb);
                Thread.Sleep(700);

                pb.ClearContent();
                pb.AppendText(optionb);
                ss.Speak(pb);
                Thread.Sleep(700);

                pb.ClearContent();
                pb.AppendText(optionc);
                ss.Speak(pb);
                Thread.Sleep(150);
                clist.Add(new string[] { "Yes", "No", "Not Sure" });
                Grammar gr = new Grammar(new GrammarBuilder(clist));
                try
                {
                    sre.RequestRecognizerUpdate();
                    sre.LoadGrammar(gr);
                    sre.SpeechRecognized += sre_SpeechRecognized;
                    sre.SetInputToDefaultAudioDevice();
                    sre.RecognizeAsync(RecognizeMode.Multiple);
                }
                catch (Exception ex)
                {
                     this.ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script language=\"javascript\">" + "alert('Error');</script>");
                }
            }
            else if (row.Cells[0].Text == "6")
            {
                ss.SelectVoiceByHints(VoiceGender.Female);
                ss.Volume = 100;
                ss.Rate = -3;
                pb.ClearContent();
                pb.AppendText(lbl1.Text);
                ss.Speak(pb);
                Thread.Sleep(1500);

                ss.SelectVoiceByHints(VoiceGender.Male);
                pb.ClearContent();
                pb.AppendText("Options are ");
                ss.Speak(pb);

                optiona = rblist1.Text;
                optionb = rblist2.Text;
                optionc = "Not Sure";

                pb.ClearContent();
                pb.AppendText(optiona);
                ss.Speak(pb);
                Thread.Sleep(700);

                pb.ClearContent();
                pb.AppendText(optionb);
                ss.Speak(pb);
                Thread.Sleep(700);

                pb.ClearContent();
                pb.AppendText(optionc);
                ss.Speak(pb);
                Thread.Sleep(150);
                clist.Add(new string[] { "Yes", "No", "Not Sure" });
                Grammar gr = new Grammar(new GrammarBuilder(clist));
                try
                {
                    sre.RequestRecognizerUpdate();
                    sre.LoadGrammar(gr);
                    sre.SpeechRecognized += sre_SpeechRecognized;
                    sre.SetInputToDefaultAudioDevice();
                    sre.RecognizeAsync(RecognizeMode.Multiple);
                }
                catch (Exception ex)
                {
                     this.ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script language=\"javascript\">" + "alert('Error');</script>");
                }
            }
            else if (row.Cells[0].Text == "7")
            {
                ss.SelectVoiceByHints(VoiceGender.Female);
                ss.Volume = 100;
                ss.Rate = -3;
                pb.ClearContent();
                pb.AppendText(lbl1.Text);
                ss.Speak(pb);
                Thread.Sleep(1500);

                ss.SelectVoiceByHints(VoiceGender.Male);
                pb.ClearContent();
                pb.AppendText("Options are ");
                ss.Speak(pb);

                optiona = rblist1.Text;
                optionb = rblist2.Text;
                optionc = "Not Sure";

                pb.ClearContent();
                pb.AppendText(optiona);
                ss.Speak(pb);
                Thread.Sleep(700);

                pb.ClearContent();
                pb.AppendText(optionb);
                ss.Speak(pb);
                Thread.Sleep(700);

                pb.ClearContent();
                pb.AppendText(optionc);
                ss.Speak(pb);
                Thread.Sleep(150);
                clist.Add(new string[] { "Yes", "No", "Not Sure" });
                Grammar gr = new Grammar(new GrammarBuilder(clist));
                try
                {
                    sre.RequestRecognizerUpdate();
                    sre.LoadGrammar(gr);
                    sre.SpeechRecognized += sre_SpeechRecognized;
                    sre.SetInputToDefaultAudioDevice();
                    sre.RecognizeAsync(RecognizeMode.Multiple);
                }
                catch (Exception ex)
                {
                     this.ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script language=\"javascript\">" + "alert('Error');</script>");
                }
            }
            else if (row.Cells[0].Text == "8")
            {
                ss.SelectVoiceByHints(VoiceGender.Female);
                ss.Volume = 100;
                ss.Rate = -3;
                pb.ClearContent();
                pb.AppendText(lbl1.Text);
                ss.Speak(pb);
                Thread.Sleep(1500);

                ss.SelectVoiceByHints(VoiceGender.Male);
                pb.ClearContent();
                pb.AppendText("Options are ");
                ss.Speak(pb);

                optiona = rblist1.Text;
                optionb = rblist2.Text;
                optionc = "Not Sure";

                pb.ClearContent();
                pb.AppendText(optiona);
                ss.Speak(pb);
                Thread.Sleep(700);

                pb.ClearContent();
                pb.AppendText(optionb);
                ss.Speak(pb);
                Thread.Sleep(700);

                pb.ClearContent();
                pb.AppendText(optionc);
                ss.Speak(pb);
                Thread.Sleep(150);
                clist.Add(new string[] { "Yes", "No", "Not Sure" });
                Grammar gr = new Grammar(new GrammarBuilder(clist));
                try
                {
                    sre.RequestRecognizerUpdate();
                    sre.LoadGrammar(gr);
                    sre.SpeechRecognized += sre_SpeechRecognized;
                    sre.SetInputToDefaultAudioDevice();
                    sre.RecognizeAsync(RecognizeMode.Multiple);
                }
                catch (Exception ex)
                {
                      this.ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script language=\"javascript\">" + "alert('Error');</script>");
                }
            }
            else if (row.Cells[0].Text == "9")
            {
                ss.SelectVoiceByHints(VoiceGender.Female);
                ss.Volume = 100;
                ss.Rate = -3;
                pb.ClearContent();
                pb.AppendText(lbl1.Text);
                ss.Speak(pb);
                Thread.Sleep(1500);

                ss.SelectVoiceByHints(VoiceGender.Male);
                pb.ClearContent();
                pb.AppendText("Options are ");
                ss.Speak(pb);

                optiona = rblist1.Text;
                optionb = rblist2.Text;
                optionc = "Not Sure";

                pb.ClearContent();
                pb.AppendText(optiona);
                ss.Speak(pb);
                Thread.Sleep(700);

                pb.ClearContent();
                pb.AppendText(optionb);
                ss.Speak(pb);
                Thread.Sleep(700);

                pb.ClearContent();
                pb.AppendText(optionc);
                ss.Speak(pb);
                Thread.Sleep(150);
                clist.Add(new string[] { "Yes", "No", "Not Sure" });
                Grammar gr = new Grammar(new GrammarBuilder(clist));
                try
                {
                    sre.RequestRecognizerUpdate();
                    sre.LoadGrammar(gr);
                    sre.SpeechRecognized += sre_SpeechRecognized;
                    sre.SetInputToDefaultAudioDevice();
                    sre.RecognizeAsync(RecognizeMode.Multiple);
                }
                catch (Exception ex)
                {
                     this.ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script language=\"javascript\">" + "alert('Error');</script>");
                }
            }
            else if (row.Cells[0].Text == "10")
            {
                ss.SelectVoiceByHints(VoiceGender.Female);
                ss.Volume = 100;
                ss.Rate = -3;
                pb.ClearContent();
                pb.AppendText(lbl1.Text);
                ss.Speak(pb);
                Thread.Sleep(1500);

                ss.SelectVoiceByHints(VoiceGender.Male);
                pb.ClearContent();
                pb.AppendText("Options are ");
                ss.Speak(pb);

                optiona = rblist1.Text;
                optionb = rblist2.Text;
                optionc = "Not Sure";

                pb.ClearContent();
                pb.AppendText(optiona);
                ss.Speak(pb);
                Thread.Sleep(700);

                pb.ClearContent();
                pb.AppendText(optionb);
                ss.Speak(pb);
                Thread.Sleep(700);

                pb.ClearContent();
                pb.AppendText(optionc);
                ss.Speak(pb);
                Thread.Sleep(150);
                clist.Add(new string[] { "Yes", "No", "Not Sure" });
                Grammar gr = new Grammar(new GrammarBuilder(clist));
                try
                {
                    sre.RequestRecognizerUpdate();
                    sre.LoadGrammar(gr);
                    sre.SpeechRecognized += sre_SpeechRecognized;
                    sre.SetInputToDefaultAudioDevice();
                    sre.RecognizeAsync(RecognizeMode.Multiple);
                }
                catch (Exception ex)
                {
                    this.ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script language=\"javascript\">" + "alert('Error');</script>");
                }
            }
        }
    }

  void sre_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
  {
        foreach (GridViewRow row in gvtemperament.Rows)
        {
            RadioButton rblist1 = ((RadioButton)row.FindControl("rdTemp_Opt1"));
            RadioButton rblist2 = ((RadioButton)row.FindControl("rdTemp_Opt2"));
            RadioButton rblist3 = ((RadioButton)row.FindControl("rdTemp_Opt3"));
            Label lbl1 = (Label)row.FindControl("lbl_desc");

            if (row.Cells[0].Text == "1")
            {
                if (e.Result.Text == "Yes")
                {                     
                    rblist1.Checked = true;
                    rblist1.Text = "Narendran";
                }
                else if (e.Result.Text == "No")
                {
                    rblist2.Checked = true;
                        rblist1.Text = "Naren";
                }
                else //if (e.Result.Text == "Not Sure")
                {
                    rblist3.Checked = true;
                    rblist1.Text = "Chk";
                }
            }
            else if (row.Cells[0].Text == "2")
            {
                if (e.Result.Text == "Yes")
                {
                    rblist1.Checked = true;
                    rblist1.Text = "Round2";
                }
                else if (e.Result.Text == "No")
                {
                    rblist2.Checked = true;
                    rblist2.Text = "Round2";
                }
                else //if (e.Result.Text == "Not Sure")
                {
                    rblist3.Checked = true; 
                    rblist3.Text = "Round2";
                }
            }

            else if (row.Cells[0].Text == "3")
            {
                if (e.Result.Text == "Yes")
                {
                    rblist1.Checked = true;
                }
                else if (e.Result.Text == "No")
                {
                    rblist2.Checked = true;
                }
                else //if (e.Result.Text == "Not Sure")
                {
                    rblist3.Checked = true;
                }
            }

            else if (row.Cells[0].Text == "4")
            {
                if (e.Result.Text == "Yes")
                {
                    rblist1.Checked = true;
                }
                else if (e.Result.Text == "No")
                {
                    rblist2.Checked = true;
                }
                else //if (e.Result.Text == "Not Sure")
                {
                    rblist3.Checked = true;
                }
            }

            else if (row.Cells[0].Text == "5")
            {
                if (e.Result.Text == "Yes")
                {
                    rblist1.Checked = true;
                }
                else if (e.Result.Text == "No")
                {
                    rblist2.Checked = true;
                }
                else //if (e.Result.Text == "Not Sure")
                {
                    rblist3.Checked = true;
                }
            }

            else if (row.Cells[0].Text == "6")
            {
                if (e.Result.Text == "Yes")
                {
                    rblist1.Checked = true;
                }
                else if (e.Result.Text == "No")
                {
                    rblist2.Checked = true;
                }
                else //if (e.Result.Text == "Not Sure")
                {
                    rblist3.Checked = true;
                }
            }
            else if (row.Cells[0].Text == "7")
            {
                if (e.Result.Text == "Yes")
                {
                    rblist1.Checked = true;
                }
                else if (e.Result.Text == "No")
                {
                    rblist2.Checked = true;
                }
                else //if (e.Result.Text == "Not Sure")
                {
                    rblist3.Checked = true;
                }
            }

            else if (row.Cells[0].Text == "8")
            {
                if (e.Result.Text == "Yes")
                {
                    rblist1.Checked = true;
                }
                else if (e.Result.Text == "No")
                {
                    rblist2.Checked = true;
                }
                else //if (e.Result.Text == "Not Sure")
                {
                    rblist3.Checked = true;
                }
            }

            else if (row.Cells[0].Text == "9")
            {
                if (e.Result.Text == "Yes")
                {
                    rblist1.Checked = true;
                }
                else if (e.Result.Text == "No")
                {
                    rblist2.Checked = true;
                }
                else //if (e.Result.Text == "Not Sure")
                {
                    rblist3.Checked = true;
                }
            }

            else if (row.Cells[0].Text == "10")
            {
                if (e.Result.Text == "Yes")
                {
                    rblist1.Checked = true;
                }
                else if (e.Result.Text == "No")
                {
                    rblist2.Checked = true;
                }
                else //if (e.Result.Text == "Not Sure")
                {
                    rblist3.Checked = true;
                }
            }
            else
            {

            }
        }
    }  


Please do the needful.
Narendran Namachivayam.

What I have tried:

The same code is working fine in windows application, but not in web application.
If I put break point, it's coming to the point but the code is not getting execute.
Posted
Updated 2-Aug-16 0:50am

Your code is running ON THE SERVER.

If your server has speakers, the text will be read out on the server, to surprise and annoy your IT administrators.

If your server has a microphone, the only text it's going to recognise will be the IT administrators swearing at you for writing code that reads things out on the server. :)

There is a Javascript speech API[^] which is currently being developed.
   
Comments
N4Narendran 26-Jul-16 0:03am
   
Hi Richard,

Thank you for your valuable reply. The thing is not in Server and with IT administrators, am coding in my local PC everything is working is fine but the functionalities are not getting execute inside my condition after the speech got recognized.


Narendran Namachivayam.
Richard Deeming 26-Jul-16 7:30am
   
The code is still running on the server. You might be able to get it working when you test locally in Visual Studio, but as soon as you deploy the site to a real server, it will stop working.
Please chk my updation on the same & do the needful

Speech Recognition is not working in Web application | The ASP.NET Forums[^]
   
Thank U.



My code is working fine, now the thing is I need the same to be implemented for Tamil & Hindi Languages, am already having the texts in DB and bound in Gridview too, but couldn't read through SpeechSynthesizer.

Do I need to have the voice for those languages?
   

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