Saturday 2 May 2015

Voice Recognition - Visual Studio

Let us study voice recognition using visual studio.

  • Open visual studio > New Project > Visual C# > Windows Form Application > OK
  • Now a new project is started
  • Go to solution explorer > right click reference > add reference > in .NET tab select “System.Speech” > OK
  • Now Design the form as shown in the picture and name it as Below
  • Right click on the form design > view code > copy the code as shown in video below.


Form Design :-




Source code :-

using System;
using System.Windows.Forms;
using System.Speech;
using System.Speech.Synthesis;
using System.Speech.Recognition;
using System.Diagnostics;

namespace voice_recognition // your project name. be care while copying.
{
    public partial class Form1 : Form
    {
        SpeechSynthesizer speechsynth = new SpeechSynthesizer();
        SpeechRecognitionEngine receng = new SpeechRecognitionEngine();
        Choices choice = new Choices();

        public Form1()
        {
            InitializeComponent();
        }

        private void startbtn_Click(object sender, EventArgs e)
        {
            startbtn.Enabled = false;
            stopbtn.Enabled = true;
            choice.Add(new string[] { "hello", "how are you", "what is the current time", "open blog", "thank you", "close" });
            Grammar gr = new Grammar(new GrammarBuilder(choice));
            try
            {
                receng.RequestRecognizerUpdate();
                receng.LoadGrammar(gr);
                receng.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(receng_SpeechRecognized);
                receng.SetInputToDefaultAudioDevice();
                receng.RecognizeAsync(RecognizeMode.Multiple);

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error");
            }
        }

        void receng_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
        {
            switch (e.Result.Text.ToString())
            {
                case "hello":
                    speechsynth.SpeakAsync("hai user");
                    break;
                case "how are you":
                    speechsynth.SpeakAsync("iam fine. what about you");
                    break;
                case "what is the current time":
                    speechsynth.SpeakAsync("right now it is " + DateTime.Now.ToLongTimeString());
                    break;
                case "thank you":
                    speechsynth.SpeakAsync("well. same to you");
                    break;
                case "open blog":
                    Process.Start("chrome", "http://homotrick.blogspot.in");
                    break;
                case "close":
                    speechsynth.Speak("see you later. bye bye");
                    Application.Exit();
                    break;

            }
            list1.Items.Add(e.Result.Text.ToString());
        }

        private void stopbtn_Click(object sender, EventArgs e)
        {
            receng.RecognizeAsyncStop();
            startbtn.Enabled = true;
            stopbtn.Enabled = false;
        }
    }
}


Video :-



Keyboard Short cuts with in a Form - C#

Here we teach you how to make an windows form application with some keyboard short cuts that works with in that form only.
  • Open visual studio > Make a new windows form application in C# with name "form keybord shortcut" > OK
  • Then design the form as shown in the picture below.

Form Design :-





Source code :-


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;

namespace form_keybord_shortcut   //your project name. be care while copying.
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            listBox1.Items.Add(keyData.ToString());
            if (keyData.ToString() == "F1, Control, Alt")
                button1_Click(null, null);
            return base.ProcessCmdKey(ref msg, keyData);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            label1.Text = "Pressed";
        }
    }
}


Video :-