Ejemplo n.º 1
0
//		/// <summary>
//		/// IMPORTANT: This will crash the Lipsyncer.
//		///
//		/// Get this. I only have EnglishUS and EnglishGB recognizers for my OS
//		/// (win7 pro) but can output the phonemes of French and perhaps other
//		/// languages.
//		/// PS. My EnglishGB phonemes appear to be utterly borked.
//		/// </summary>
//		/// <param name="langid"></param>
//		void PrintPhons(int langid)
//		{
//			_phoneConverter.LanguageId = langid;
//			for (int i = 0; i != 100; ++i)
//				logfile.Log(i + " - " + _phoneConverter.IdToPhone(i));
//		}


        #region methods
        /// <summary>
        /// Sets the Recognizer and LanguageId when the Recognizers combobox
        /// selection changes.
        /// @note The LanguageId is used by both TTS and SpeechRecognition.
        /// </summary>
        /// <param name="recognizer"></param>
        /// <returns>true if the language-id is set successfully</returns>
        bool SetRecognizer(Recognizer recognizer)
        {
#if DEBUG
            logfile.Log();
            logfile.Log("SetRecognizer()");

            logfile.Log(". create (SpInprocRecognizer)_recognizer");
#endif
            _recognizer = new SpInprocRecognizer();
#if DEBUG
            logfile.Log(". (SpInprocRecognizer)_recognizer CREATED");
#endif
            _recognizer.Recognizer = (SpObjectToken)recognizer.Tok;
#if DEBUG
            logfile.Log(". recognizer.Tok.Id= " + recognizer.Tok.Id);
            logfile.Log(". recognizer.Description= " + recognizer.Tok.GetDescription());

            logfile.Log(". recognizer.Langids= " + recognizer.Langids);
#endif
            string langid = recognizer.Langids;
            int    pos    = recognizer.Langids.IndexOf(' ');
            if (pos != -1)
            {
                langid = langid.Substring(0, pos);                 // use 1st langid
            }
            // TODO: ComboBox dropdown for user to choose from if 2+ languages
            // are supported by the current Recognizer.

            int id;
            if (!Int32.TryParse(langid, out id) ||              // safety - unless the token has "n/a" Languages.
                id < 0)                                         // TODO: check id against valid SAPI language-ids
            {
                if (!FxeGeneratorF.isConsole)
                {
                    using (var d = new InfoDialog("Error", "Did not find a Language in the Recognizer's token."))
                    {
                        d.ShowDialog(FxeGeneratorF.That);
                    }
                }
                return(false);
            }

            _phoneConverter.LanguageId = id;
#if DEBUG
            logfile.Log(". _phoneConverter.LanguageId= " + _phoneConverter.LanguageId);
            logfile.Log();
#endif
            StaticData.viceroy(_phoneConverter.LanguageId);

            return(true);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Adds the user's installed SAPI-compliant SpeechRecognizers (speech
        /// recognition engines) to a specified combobox.
        /// </summary>
        /// <param name="co"></param>
        /// <returns>true if a Recognizer was found and added to the list;
        /// false if user is SoL</returns>
        internal static bool AddSpeechRecognizers(ComboBox co)
        {
#if DEBUG
            logfile.Log();
            logfile.Log("SpeechRecognizerLister.AddSpeechRecognizers()");

            logfile.Log(". create (SpInprocRecognizer)_recognizer");
#endif
            // NOTE: This is your SAPI5.4 SpeechRecognizer (aka SpeechRecognitionEngine) interface.
            // good luck!
            var sr_default = new SpInprocRecognizer();
#if DEBUG
            logfile.Log(". (SpInprocRecognizer)_recognizer CREATED");
#endif
            if (sr_default != null)
            {
#if DEBUG
                logfile.Log();
                logfile.Log("Recognizer.Id= " + sr_default.Recognizer.Id);
                logfile.Log("Recognizer.GetDescription()= " + sr_default.Recognizer.GetDescription());
                logfile.Log();
#endif
                ISpeechObjectTokens toks = sr_default.GetRecognizers();
#if DEBUG
                foreach (ISpeechObjectToken tok in toks)
                {
                    logfile.Log(". installed.Id= " + tok.Id);
                    logfile.Log(". installed.GetDescription()= " + tok.GetDescription());
                }
                logfile.Log();
#endif

                int  id_default       = -1;
                bool id_default_found = false;

                var recognizers = new List <Recognizer>();
                foreach (ISpeechObjectToken tok in toks)
                {
                    if (tok.GetDescription().Contains("Microsoft Speech Recognizer"))                     // 8.0+ TODO: other SAPI-compliant speech engines incl/ 3rd-party
                    {
                        recognizers.Add(new Recognizer(tok));

                        if (!id_default_found)
                        {
                            ++id_default;

                            if (tok.Id == sr_default.Recognizer.Id)
                            {
                                id_default_found = true;
                            }
                        }
                    }
                }

                if (recognizers.Count != 0)
                {
                    co.DataSource    = recognizers;
                    co.SelectedIndex = id_default;

                    return(true);
                }
            }
#if DEBUG
            logfile.Log(". RECOGNIZER NOT FOUND");
#endif
            return(false);
        }
    bool UseDictation;     // Declare boolean variable for storing pronunciation dictation grammar setting

    public void main()
    {
        // Reset relevant VoiceAttack text variables
        VA.SetText("~~RecognitionError", null);
        VA.SetText("~~RecognizedText", null);
        VA.SetText("~~SAPIPhonemes", null);
        VA.SetText("~~SAPIPhonemesRaw", null);
        //VA.SetText("~~FalseRecognitionFlag", null);

        // Retrieve the desired word data contained within VoiceAttack text variable
        string ProcessText = null;                     // Initialize string variable for storing the text of interest

        if (VA.GetText("~~ProcessText") != null)       // Check if user provided valid text in input variable
        {
            ProcessText = VA.GetText("~~ProcessText"); // Store text of interest held by VA text variable
        }
        else
        {
            VA.SetText("~~RecognitionError", "Error in input text string (SAPI)"); // Send error detail back to VoiceAttack as text variable
            return;                                                                // End code processing
        }

        // Retrieve path to speech grammar XML file from VoiceAttack
        GrammarPath = VA.GetText("~~GrammarFilePath");

        // Retrieve path to voice recognition input wav file from VoiceAttack
        AudioPath = VA.GetText("~~AudioFilePath");

        // Check if TTS engine is voicing the input for the speech recognition engine
        if (VA.GetBoolean("~~UserVoiceInput") == false)
        {
            //VA.WriteToLog("creating wav file");
            if (TextToWav(AudioPath, ProcessText) == false) // Create wav file with specified path that voices specified text (with text-to-speech) and check if the creation was NOT successful
            {
                return;                                     // Stop executing the code
            }
        }

        // Create speech recognizer and associated context
        SpInprocRecognizer  MyRecognizer = new SpInprocRecognizer();                              // Create new instance of SpInprocRecognizer
        SpInProcRecoContext RecoContext  = (SpInProcRecoContext)MyRecognizer.CreateRecoContext(); // Initialize the SpInProcRecoContext (in-process recognition context)

        try                                                                                       // Attempt the following code
        {
            // Open the created wav in a new FileStream
            FileStream = new SpFileStream();                                        // Create new instance of SpFileStream
            FileStream.Open(AudioPath, SpeechStreamFileMode.SSFMOpenForRead, true); // Open the specified file in the FileStream for reading with events enabled

            // Set the voice recognition input as the FileStream
            MyRecognizer.AudioInputStream = FileStream;             // This will internally "speak" the wav file for input into the voice recognition engine

            // Set up recognition event handling
            RecoContext.Recognition      += new _ISpeechRecoContextEvents_RecognitionEventHandler(RecoContext_Recognition);           // Register for successful voice recognition events
            RecoContext.FalseRecognition += new _ISpeechRecoContextEvents_FalseRecognitionEventHandler(RecoContext_FalseRecognition); // Register for failed (low confidence) voice recognition events
            if (VA.GetBoolean("~~ShowRecognitionHypothesis") == true)                                                                 // Check if user wants to show voice recognition hypothesis results
            {
                RecoContext.Hypothesis += new _ISpeechRecoContextEvents_HypothesisEventHandler(RecoContext_Hypothesis);               // Register for voice recognition hypothesis events
            }
            RecoContext.EndStream += new _ISpeechRecoContextEvents_EndStreamEventHandler(RecoContext_EndStream);                      // Register for end of file stream events

            // Set up the grammar
            grammar      = RecoContext.CreateGrammar();                     // Initialize the grammar object
            UseDictation = (bool?)VA.GetBoolean("~~UseDictation") ?? false; // Set UserDictation based on value from VoiceAttack boolean variable
            if (UseDictation == true)                                       // Check if pronunciation dictation grammar should be used with speech recognition
            {
                //grammar.DictationLoad("", SpeechLoadOption.SLOStatic); // Load blank dictation topic into the grammar
                grammar.DictationLoad("Pronunciation", SpeechLoadOption.SLOStatic);    // Load pronunciation dictation topic into the grammar so that the raw (unfiltered) phonemes may be retrieved
                grammar.DictationSetState(SpeechRuleState.SGDSActive);                 // Activate dictation grammar
            }
            else
            {
                grammar.CmdLoadFromFile(GrammarPath, SpeechLoadOption.SLODynamic);           // Load custom XML grammar file
                grammar.CmdSetRuleIdState(0, SpeechRuleState.SGDSActive);                    // Activate the loaded grammar
            }
            Application.Run();                                                               // Starts a standard application message loop on the current thread
        }
        catch                                                                                // Handle exceptions in above code
        {
            VA.SetText("~~RecognitionError", "Error during voice recognition setup (SAPI)"); // Send error detail back to VoiceAttack as text variable
            return;                                                                          // Stop executing the code
        }
        finally                                                                              // Runs whether an exception is encountered or not
        {
            MyRecognizer = null;                                                             // Set to null in preparation for garbage collection
            FileStream.Close();                                                              // Close the input FileStream
            FileStream = null;                                                               // Set to null in preparation for garbage collection

            // Close up recognition event handling
            RecoContext.Recognition      -= new _ISpeechRecoContextEvents_RecognitionEventHandler(RecoContext_Recognition);           // Unregister for successful voice recognition events
            RecoContext.FalseRecognition -= new _ISpeechRecoContextEvents_FalseRecognitionEventHandler(RecoContext_FalseRecognition); // Unregister for failed (low confidence) voice recognition events
            if (VA.GetBoolean("~~ShowRecognitionHypothesis") == true)                                                                 // Check if user wanted to show voice recognition hypothesis results
            {
                RecoContext.Hypothesis -= new _ISpeechRecoContextEvents_HypothesisEventHandler(RecoContext_Hypothesis);               // Unregister for voice recognition hypothesis events
            }
            RecoContext.EndStream -= new _ISpeechRecoContextEvents_EndStreamEventHandler(RecoContext_EndStream);                      // Unregister for end of file stream events
            RecoContext            = null;                                                                                            // Set to null in preparation for garbage collection
        }
        //VA.WriteToLog("voice recognition complete"); // Output info to event log
    }