-1

I'm new with C# and to learn, I'm watching and trying exemple from the web.

I saw this exemple:

using System;
using System.Drawing;
using System.Text.RegularExpressions;
using System.Windows.Forms;

using System.Speech.Recognition;

namespace MouseController
{
    public partial class Form1 : Form
    {
        SpeechRecognitionEngine recognitionEngine; 

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        Initialize();
    }

    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
        recognitionEngine.RecognizeAsyncStop();
    }

    private void Initialize()
    {
        recognitionEngine = new SpeechRecognitionEngine();
        recognitionEngine.SetInputToDefaultAudioDevice();
        recognitionEngine.SpeechRecognized += (s, args) =>
        {
            string line = "";
            foreach (RecognizedWordUnit word in args.Result.Words)
            {
                if (word.Confidence > 0.5f)
                    line += word.Text + " ";
            }

            string command = line.Trim();

            switch (command)
            {
                case "left":
                    MoveMouse(Cursor.Position.X - 50, Cursor.Position.Y);
                    break;
                case "right":
                    MoveMouse(Cursor.Position.X + 50, Cursor.Position.Y);
                    break;
                case "up":
                    MoveMouse(Cursor.Position.X, Cursor.Position.Y - 50);
                    break;
                case "down":
                    MoveMouse(Cursor.Position.X, Cursor.Position.Y + 50);
                    break;
            }

            txtOutput.Text += line;
            txtOutput.Text += Environment.NewLine;
        };

        recognitionEngine.UnloadAllGrammars();
        recognitionEngine.LoadGrammar(CreateGrammars());
        recognitionEngine.RecognizeAsync(RecognizeMode.Multiple);
    }

    private Grammar CreateGrammars()
    {
        Choices commandChoices = new Choices("left", "right", "up", "down");
        GrammarBuilder grammarBuilder = new GrammarBuilder();
        grammarBuilder.Append(commandChoices);
        return new Grammar(grammarBuilder);
    }

    private void MoveMouse(int x, int y)
    {
        this.Cursor = new Cursor(Cursor.Current.Handle);
        Cursor.Position = new Point(x, y);
        Cursor.Clip = new Rectangle(this.Location, this.Size);
    }
}
}

But nothing happens when I say "up", "down", "left" or "right"...

I also tried this:

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.Recognition;

namespace SpeechRecognitionExample
{
    public partial class Form1 : Form
    {
        private SpeechRecognitionEngine recognitionEngine;

        public Form1()
        {
            InitializeComponent();

            recognitionEngine = new SpeechRecognitionEngine();
            recognitionEngine.SetInputToDefaultAudioDevice();
            recognitionEngine.SpeechRecognized += (s, args) =>
            {
                foreach (RecognizedWordUnit word in args.Result.Words)
                {
                    if (word.Confidence > 0.8f)
                        txtOutput.Text += word.Text + " ";
                }
                txtOutput.Text += Environment.NewLine;
            };
            recognitionEngine.LoadGrammar(new DictationGrammar());
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            recognitionEngine.RecognizeAsync(RecognizeMode.Multiple);
        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            recognitionEngine.RecognizeAsyncStop();
        }
    }
}

And it's not working...

In both of these codes I don't get any errors from Visual Studio.

Why isn't it working?

I checked my microphone and it's working.

Widor
  • 13,003
  • 7
  • 42
  • 64
Dor Aharonson
  • 143
  • 1
  • 1
  • 9
  • 11
    Please create a minimal test-case and write an appropriately focused title. –  May 17 '12 at 13:03
  • 1
    Alternately/Additionally, sprinkle in a lot of statements to output variable values. The results may surprise your expectations. Looks like a fun script, though. – jpaugh May 17 '12 at 13:07
  • What errors are you getting. When you debug the application what is the value of `command` when you say `left`? You are asking a complex question giving us the required information. You need to tell us exactly when its failing. – Security Hound May 17 '12 at 13:30

1 Answers1

0

Set a breakpoint at the beginning of Initialize() and also on the lambda statement where it says string line = "";. Step through each line to and watch the values of the variables. If you don't get any hits on these breakpoints, then there is something wrong with the engine settings or the input device, I would guess.

Also, you note that you are concatenating multiple words on line and then looking for a single word on the switch statement. If you get multiple words like "up up", it won't match any of your case conditions.

Let us know what you find and we might be able to help you better.

Eren Ersönmez
  • 38,383
  • 7
  • 71
  • 92