Tuesday, February 4, 2014

Make Voice Calculator in c#

To Make it simple our calculation here is the voice calculator  c# code try your self and enjoy


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.Speech.Synthesis;
using System.Speech.Recognition;

namespace Add_speak
{
    public partial class Form1 : Form
    {
        SpeechSynthesizer sSyn = new SpeechSynthesizer();
        PromptBuilder pb = new PromptBuilder();
        public Form1()
        {
            InitializeComponent();
        }

        SpeechRecognitionEngine sReg = new SpeechRecognitionEngine();

        private void button1_Click(object sender, EventArgs e)
        {

        }
     

        private void Form1_Load(object sender, EventArgs e)
        {
         
         
            Choices cList = new Choices();
            cList.Add(new string[] { "next","add","1", "2", "3", "4", "5", "6", "7", "8", "9" });
            Grammar gr = new Grammar(new GrammarBuilder(cList));

            try
            {
                sReg.RequestRecognizerUpdate();
                sReg.LoadGrammar(gr);
                sReg.SpeechRecognized +=new EventHandler<SpeechRecognizedEventArgs>(sReg_SpeechRecognized);
                sReg.SetInputToDefaultAudioDevice();
                sReg.RecognizeAsync(RecognizeMode.Multiple);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }

     
        }
        bool ba = false;
        void sReg_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
        {
     
         
            if (e.Result.Text.ToString() == "next")
            {
                this.ActiveControl = textBox2;
                ba = true;
               // textBox2.Text = e.Result.Text.ToString();
            }
         
            else if (e.Result.Text.ToString() == "add")
            {

                int a = Convert.ToInt32(textBox1.Text);
                int b = Convert.ToInt32(textBox2.Text);

                label3.Text = (a + b).ToString();
            }
            else if (ba)
            {
                textBox2.Text += e.Result.Text.ToString();
            }
         

            else
            {
                textBox1.Text += e.Result.Text.ToString();
            }
         
        }

    }
}