Click here to Skip to main content
15,880,608 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi friends,
I was using the following code detect what the user say and change the BackColor of the form accordingly. However I am encountering run-time problems. Firstly, while I am clicking the button1 The following error comes up : 'There is no source code available for the current location.' Immediately after that the following error comes on the screen: 'At least one grammar must be loaded before doing a recognition.'
C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using System.Diagnostics;
using System.Speech;
using System.Speech.Recognition;
using System.Speech.AudioFormat;
using System.Speech.Synthesis;

namespace Trying
{
    public partial class Form1 : Form
    {
        private SpeechRecognitionEngine recognitionEngine;
        public Form1()
        {
            InitializeComponent();
            try
            {
                recognitionEngine = new SpeechRecognitionEngine();
                recognitionEngine.SetInputToDefaultAudioDevice();
                recognitionEngine.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(recognitionEngine_SpeechRecognized);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            

            
        }

        void recognitionEngine_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
        {
            try
            {
                string line = "";
                foreach (RecognizedWordUnit word in e.Result.Words)
                {
                    if (word.Confidence >= 0.5f)
                        line += word.Text + " ";
                }
                string command = Regex.Replace(line, "Change", "");
                switch (command)
                {
                    case "Red":
                        this.BackColor = Color.Red;
                        break;
                    case "Blue":
                        this.BackColor = Color.Blue;
                        break;
                    case "Green":
                        this.BackColor = Color.Green;
                        break;
                }
                txtOutput.Text += line;//txtOutput is a RichTextBox
                txtOutput.Text += Environment.NewLine;
            }
            catch (Exception ex)
            {
                MessageBox.Show("2:" + ex.Message);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                recognitionEngine.RecognizeAsync(RecognizeMode.Multiple);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private Grammar CreateGrammarObject()
        {
            try
            {
                Choices commandChoices = new Choices("Red", "Green", "Blue");
                GrammarBuilder grammarBuilder = new GrammarBuilder("Change");
                grammarBuilder.Append(commandChoices);
                Grammar g = new Grammar(grammarBuilder);
                return g;
            }
            catch (Exception ex1)
            {
                MessageBox.Show("3:" + ex1.Message);
            }
        }

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

However, none of the errors come through the catch blocks. Can anyone please help me out?
Posted

"There is no source code available for the current location ."
This happens when PDB file is missing or an old one, not updated. Compiled DLL and the respective PDB file needs to be of same timestamp. You need to make sure that the DLL last modified date and PDB last modified dates are in sync. PDB files are the one responsible that helps you in debugging.

Clean the solution and rebuild all. This should create a new dll along with PDB. Once done, you should be able to debug it.
 
Share this answer
 
Comments
Ritwesh 4-Nov-12 2:25am    
Thank you very much for the help.
Rebuilding the solution solved the problem. But my line:27 is throwing an exception:
'System.ArgumentException: Value does not fall within the expected range.
at System.Speech.Internal.SapiInterop.SapiProxy.MTAThread.Invoke2(VoidDelegate pfn)
at System.Speech.Internal.SapiInterop.SapiRecognizer.SetInput(Object input, Boolean allowFormatChanges)
at System.Speech.Recognition.RecognizerBase.SetInputToDefaultAudioDevice()
at System.Speech.Recognition.SpeechRecognitionEngine.SetInputToDefaultAudioDevice()
at Trying.Form1..ctor() in F:\Trial\Trying\Trying\Form1.cs:line 27'
Other than this when I am pressing the start button there is still the problem of 'At least one grammar must be loaded before doing a recognition.'
Please help me out.
First be sure when you add reference of System.Speech dll that it is .NET 4.0 version.
Then I think you miss a line here and may be the most important one.Just add a line..
C#
recognitionEngine.LoadGrammar(CreateGrammerObject());

in your recognitionEngine_SpeechRecognized event.
 
Share this answer
 
v3
Comments
Ritwesh 4-Nov-12 2:18am    
I have entered the line you asked me to do. I found out that my System.Speech dll is .NET 3.0 version. So what should I do now?
ridoy 4-Nov-12 6:18am    
.NET 3.0 also supports it.See this article of Code Project.
http://www.codeproject.com/Articles/380027/Csharp-Speech-to-Text
Though you not mentioned whether you got error still now,but you told above that,you found error in line 27.This usually occurs when your input audio device isn't ready for hearing your voice,so check your microphone/headphone connection with your PC carefully.

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