Ejemplo n.º 1
0
        public static void Main(string[] args)
        {
            // Obtain a KinectSensor if any are available
            KinectSensor sensor = (from sensorToCheck in KinectSensor.KinectSensors where sensorToCheck.Status == KinectStatus.Connected select sensorToCheck).FirstOrDefault();
            if (sensor == null)
            {
                Console.WriteLine(
                        "No Kinect sensors are attached to this computer or none of the ones that are\n" +
                        "attached are \"Connected\".\n" +
                        "Attach the KinectSensor and restart this application.\n" +
                        "If that doesn't work run SkeletonViewer-WPF to better understand the Status of\n" +
                        "the Kinect sensors.\n\n" +
                        "Press any key to continue.\n");

                // Give a chance for user to see console output before it is dismissed
                Console.ReadKey(true);
                return;
            }

            sensor.Start();

            // Obtain the KinectAudioSource to do audio capture
            KinectAudioSource source = sensor.AudioSource;

            source.EchoCancellationMode = EchoCancellationMode.None; // No AEC for this sample
            source.AutomaticGainControlEnabled = false; // Important to turn this off for speech recognition
            System.Speech.Recognition.RecognizerInfo ri = System.Speech.Recognition.SpeechRecognitionEngine.InstalledRecognizers().FirstOrDefault();
            using (var recoEngine = new System.Speech.Recognition.SpeechRecognitionEngine(ri.Id))
            {

                // Create the question dictation grammar.
                System.Speech.Recognition.DictationGrammar customDictationGrammar = new System.Speech.Recognition.DictationGrammar();
                customDictationGrammar.Name = "Dictation";
                customDictationGrammar.Enabled = true;

                // Create a SpeechRecognitionEngine object and add the grammars to it.
                recoEngine.LoadGrammar(customDictationGrammar);

                recoEngine.SpeechRecognized += (s, sargs) => Console.Write(sargs.Result.Text);

                using (Stream s = source.Start())
                {

                    recoEngine.SetInputToAudioStream(s, new System.Speech.AudioFormat.SpeechAudioFormatInfo(System.Speech.AudioFormat.EncodingFormat.Pcm, 16000, 16, 1, 32000, 2, null));

                    Console.WriteLine("Dictating. Press ENTER to stop");

                    recoEngine.RecognizeAsync(System.Speech.Recognition.RecognizeMode.Multiple);

                    Console.ReadLine();
                    Console.WriteLine("Stopping recognizer ...");
                    recoEngine.RecognizeAsyncStop();

                }
            }

            sensor.Stop();
        }
Ejemplo n.º 2
0
        public void speechToTextInit()
        {
            //This method is used for converting speech into text
            //Creates an instance of the system.speech speech recognition engine for use with speech to text
            stt_sre = new System.Speech.Recognition.SpeechRecognitionEngine();
            //Uses dictation grammar to allow freedom of speech for entering any word detected
            System.Speech.Recognition.Grammar dictationGrammar = new System.Speech.Recognition.DictationGrammar();
            //Loads the dictation grammar into the speech recognition engine
            stt_sre.LoadGrammar(dictationGrammar);

            try
            {
                //Try catch is used here to catch any invalid operation exceptions that could occur when detecting speech to text
                stt_sre.SetInputToDefaultAudioDevice();
                //Saves result from speech recognition as a recognition result object
                System.Speech.Recognition.RecognitionResult result = stt_sre.Recognize();

                if (result != null)
                {
                    //Speech result is set to the string of the result to be used in speech to text
                    speechResult = result.Text;
                    //Removes any spaces to prevent inconsistencies
                    speechResult.Replace(",", "");
                    Console.WriteLine("The result is: " + speechResult);
                    try
                    {
                        //Used in passing results from speech to text to other classes
                        speechInputCheck(speechResult);
                    }
                    catch (NullReferenceException null_ref)
                    {
                        //Used in catching if the speechResult is null, showing that the exception has been thrown and its source
                        Console.WriteLine("NullReferenceException thrown in speech: " + null_ref.Message + null_ref.Source);
                    }
                }
            }
            catch (InvalidOperationException invalid)
            {
                Console.WriteLine("InvalidOperationException in speech: " + invalid.Message + invalid.Source);
            }
            finally
            {
                //Unloads the speech recognition engine once finished
                stt_sre.UnloadAllGrammars();
            }
        }