Search code examples
c#.netspeech-recognitiontext-to-speech

System.InvalidOperationException "The language for the grammar does not match the language of the speech recognizer"


I am currently recreating a tutorial project that I saw o recently but I am facing certain problems. Firstly I am using Windows 7 Home Premium and my OS is Turkish.

Because of this, System.Speech only works when I do text to speech, otherwise it throws an exception saying no speech recognizer installed. I checked this from the control panel as well and it says speech recognition is not available in this language. Since I do not have the ultimate version of windows I can't use language packs to change the language totally.

After a bit of research in stackoverflow I found out that installing micosoft speech platform and changing System.Speech to Microsoft.Speech works. So I followed the instructions on this web site(http://msdn.microsoft.com/en-us/library/hh362873.aspx) and installed the components including the en-US language pack.

I changed my code to reflect the changes and now I am getting different errors on different lines. Here is my code :

using System;
using System.Globalization;
using Microsoft.Speech.Recognition;
using Microsoft.Speech.Synthesis;
using System.Windows.Forms;

namespace SpeechRecognitionTest
{
    public partial class Form1 : Form
    {
        private SpeechRecognitionEngine _speechRecognitionEngine = new SpeechRecognitionEngine(new CultureInfo("en-US"));
        private SpeechSynthesizer _speechSynthesizer = new SpeechSynthesizer();
        private PromptBuilder _promptBuilder = new PromptBuilder();

        public Form1()
        {
            InitializeComponent();
        }

        private void btnSpeakText_Click(object sender, EventArgs e)
        {
            _promptBuilder.ClearContent();
            _promptBuilder.AppendText(txtSpeech.Text);
            _speechSynthesizer.SetOutputToDefaultAudioDevice();
            _speechSynthesizer.Speak(_promptBuilder);
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            btnStart.Enabled = false;
            btnEnd.Enabled = true;
            var choicesList = new Choices();
            choicesList.Add(new string[]{"hello","yes"});
            var grammar = new Grammar(new GrammarBuilder(choicesList));
            _speechRecognitionEngine.RequestRecognizerUpdate();
            _speechRecognitionEngine.LoadGrammar(grammar);
            _speechRecognitionEngine.SpeechRecognized += _speechRecognitionEngine_SpeechRecognized;
            _speechRecognitionEngine.SetInputToDefaultAudioDevice();
            _speechRecognitionEngine.RecognizeAsync(RecognizeMode.Multiple);
        }

        void _speechRecognitionEngine_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
        {
            MessageBox.Show(e.Result.Text);
        }

        private void btnEnd_Click(object sender, EventArgs e)
        {
            btnStart.Enabled = true;
            btnEnd.Enabled = false;
        }

    }
}

Firstly, when I try text to speech I get the error "An unhandled exception of type 'System.IO.FileNotFoundException' occurred in Microsoft.Speech.dll" on this line:

_speechSynthesizer.Speak(_promptBuilder);

Secondly when I try to do voice recognition I get the following exception:

An unhandled exception of type 'System.InvalidOperationException' occurred in Microsoft.Speech.dll

Additional information: The language for the grammar does not match the language of the speech recognizer.

On the line:

_speechRecognitionEngine.LoadGrammar(grammar);

I did search the internet and found mixed responses to this problem. Some could use System.Speech without a problem since they had english language installed, some non-english OS owners solved the problem through Microsoft.Speech. But there is no definitive answer on this. I am currently out of options and would really like if someone can explain what's wrong or even if I can run this code on my machine because of native OS language.


Solution

  • The exception in the synthesis engine is caused by the synthesis engine trying to find a default voice and failing. If you explicitly specify a voice (using SelectVoiceByHints or GetInstalledVoices(CultureInfo)), synthesis will succeed.

    Second, GrammarBuilder objects have a Culture property (that defaults to the current UI culture). You will need to set it to the Culture of the recognizer before recognitions will work.