Monday, April 30, 2012

Speech Recognition using Interop.SpeechLib.dll with C#


using System;
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.Drawing.Drawing2D;
using System.Runtime.InteropServices;
using System.Threading;
using SpeechLib;
using MediaPlayer;
using WMPLib;
using System.Diagnostics;



 private SpeechLib.SpSharedRecoContext objRecoContext;
        private SpeechLib.ISpeechRecoGrammar grammar = null;
        private SpeechLib.ISpeechGrammarRule menuRule = null;
        SpVoice spo = new SpVoice();
        bool saved = false;
        bool enable_speach = false;
        bool speach_diabletime_out = false;



     private void Form1_Load(object sender, EventArgs e)
        {
         
            try
            {
                objRecoContext = new SpSharedRecoContext();
           
                spo.Speak("Welcome", SpeechVoiceSpeakFlags.SVSFlagsAsync);
                spo.WaitUntilDone(1000);
           
                enable_speach = false;
                objRecoContext.Recognition += new _ISpeechRecoContextEvents_RecognitionEventHandler(Reco_Event);
           
            }
            catch (Exception exp)
            {

                MessageBox.Show("Install speech Before Statring Akku", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
                Application.Exit();
            }

       
           
   
        }




 string result = "";

        private void Reco_Event(int StreamNumber, object StreamPosition, SpeechRecognitionType RecognitionType, ISpeechRecoResult Result)
        {


            result = Result.PhraseInfo.GetText(0, -1, true);
       
            if (result == Properties.Settings.Default.start)
            {
             
                System.Diagnostics.Process p = new System.Diagnostics.Process();
                //p.StartInfo.FileName = @"C:\Program Files\Mozilla Firefox\firefox.exe";
                p.StartInfo.FileName = @"C:\Program Files\Internet Explorer\IEXPLORE.EXE";
                p.Start();
                p.StartInfo.FileName = @"C:\\Windows";
                p.Start();
                //System.Diagnostics.Process("C:\\Windows");

            }
            if (result == Properties.Settings.Default.explorer)
            {
             
                System.Diagnostics.Process p = new System.Diagnostics.Process();
                p.StartInfo.FileName = @"C:\Program Files\Internet Explorer\IEXPLORE.EXE";
                p.Start();

             
            }
            if (result == Properties.Settings.Default.player)
            {
                result = "";
                System.Diagnostics.Process p = new System.Diagnostics.Process();
                p.StartInfo.FileName = @"C:\Program Files\Windows Media Player\wmplayer.exe" ;
                p.Start();
                System.Diagnostics.Process.Start(@"D:\virtulamouse\virtulamouse\01 - Kya Mujhe Pyaar Hai (Kay Kay).mp3");

            }
            if (result == Properties.Settings.Default.notepad)
            {
                result = "";
                System.Diagnostics.Process p = new System.Diagnostics.Process();
                p.StartInfo.FileName = @"notepad.exe";
                p.Start();
                //System.Diagnostics.Process.Start(@"%SystemRoot%\system32\notepad.exe");

            }
            if (result == Properties.Settings.Default.keyboard)
            {
                result = "";
                System.Diagnostics.Process p = new System.Diagnostics.Process();
                p.StartInfo.FileName = @"osk.exe";
                p.Start();
                //System.Diagnostics.Process.Start(@"%SystemRoot%\system32\notepad.exe");

            }
            serialPort1.Close();

            //System.Windows.Forms.SendKeys.SendWait("This text was entered ");
            //Thread.Sleep(1000);
            //System.Windows.Forms.SendKeys.SendWait("{TAB}");
            //Thread.Sleep(1000);
            //System.Windows.Forms.SendKeys.SendWait("using the System.Windows.");
            //Thread.Sleep(1000);

        }

        private void btnenablespeech_Click(object sender, EventArgs e)
        {
            try
            {
                grammar = objRecoContext.CreateGrammar(0);
                menuRule = grammar.Rules.Add("MenuCommands", SpeechRuleAttributes.SRATopLevel | SpeechRuleAttributes.SRADynamic, 1);
                object PropValue = "";

            menuRule.InitialState.AddWordTransition(null, Properties.Settings.Default.start, " ", SpeechGrammarWordType.SGLexical, Properties.Settings.Default.start, 2, ref PropValue, 1.0F);
            menuRule.InitialState.AddWordTransition(null, Properties.Settings.Default.explorer, " ", SpeechGrammarWordType.SGLexical, Properties.Settings.Default.explorer, 3, ref PropValue, 1.0F);
            menuRule.InitialState.AddWordTransition(null, Properties.Settings.Default.player, " ", SpeechGrammarWordType.SGLexical, Properties.Settings.Default.player, 4, ref PropValue, 1.0F);
            menuRule.InitialState.AddWordTransition(null, Properties.Settings.Default.notepad, " ", SpeechGrammarWordType.SGLexical, Properties.Settings.Default.notepad, 5, ref PropValue, 1.0F);
            menuRule.InitialState.AddWordTransition(null, Properties.Settings.Default.keyboard, " ", SpeechGrammarWordType.SGLexical, Properties.Settings.Default.keyboard, 5, ref PropValue, 1.0F);
         
            //menuRule.InitialState.AddWordTransition(null, Properties.Settings.Default.start, " ", SpeechGrammarWordType.SGLexical, Properties.Settings.Default.start, 2, ref PropValue, 1.0F);
            //menuRule.InitialState.AddWordTransition(null, Properties.Settings.Default.explorer, " ", SpeechGrammarWordType.SGLexical, Properties.Settings.Default.explorer, 3, ref PropValue, 1.0F);
            //menuRule.InitialState.AddWordTransition(null, Properties.Settings.Default.player, " ", SpeechGrammarWordType.SGLexical, Properties.Settings.Default.player, 4, ref PropValue, 1.0F);
            //menuRule.InitialState.AddWordTransition(null, Properties.Settings.Default.notepad, " ", SpeechGrammarWordType.SGLexical, Properties.Settings.Default.notepad, 5, ref PropValue, 1.0F);
            //menuRule.InitialState.AddWordTransition(null, Properties.Settings.Default.keyboard, " ", SpeechGrammarWordType.SGLexical, Properties.Settings.Default.keyboard, 5, ref PropValue, 1.0F);
                grammar.Rules.Commit();
                grammar.CmdSetRuleState("MenuCommands", SpeechRuleState.SGDSActive);
            }
            catch (Exception exp)
            {

                MessageBox.Show(exp.Message);
                if (serialPort1.IsOpen == true)
                {
                    serialPort1.Close();
                }
                return;
            }

            spo.Speak("start", SpeechVoiceSpeakFlags.SVSFlagsAsync);
            spo.WaitUntilDone(1000);
            if (button2.Text == "Enable Speech")
            {
                button2.Text = "Disable Speech";
            }
            else if (button2.Text == "Disable Speech")
            {
                objRecoContext.EndStream += new _ISpeechRecoContextEvents_EndStreamEventHandler(wavRecoContext_EndStream);
                button2.Text = "Enable Speech";
            }
        }

  private void wavRecoContext_EndStream(int StreamNumber, object StreamPosition, bool f)
        {

            // ' Disable dictation

            grammar.DictationSetState(SpeechRuleState.SGDSInactive);

        }

11 comments:

Anonymous said...

Getting error in this area
How can i resolve thi error

using MediaPlayer;
using WMPLib;

Hanitha said...

Getting error in this area

using MediaPlayer;
using WMPLib;

Hanitha said...

getting error in this area

using MediaPlayer;
using WMPLib;

Hanitha said...

how can i add mediaplayer dll
Could u please suggest me.....

Hanitha said...

how can i add mediaplayer dll
Could u please suggest me.....

Hanitha said...

could u please show frontend also

sunichand2020 said...

hi Hanitha,

Please remove

using MediaPlayer;
using WMPLib;

try again

Hanitha said...

else if (button2.Text == "Disable Speech")
{
objRecoContext.EndStream += new _ISpeechRecoContextEvents_EndStreamEventHandler(wavRecoContext_EndStream);
button2.Text = "Enable Speech";
}
i got error in this area
the error is the name wavRecoContext_EndStream does not exist in the current context

sunichand2020 said...

hi hanitha,

private void wavRecoContext_EndStream(int StreamNumber, object StreamPosition, bool f)
{

// ' Disable dictation

grammar.DictationSetState(SpeechRuleState.SGDSInactive);

}

Hanitha said...

What is the input and expected output

sunichand2020 said...

hi hanitha,

If you speech anything like start or explorer then will do action like open firefox or internet explorer.try again

Using Authorization with Swagger in ASP.NET Core

 Create Solution like below LoginModel.cs using System.ComponentModel.DataAnnotations; namespace UsingAuthorizationWithSwagger.Models {     ...