예제 #1
0
파일: Program.cs 프로젝트: rdodgen/Ezri
        static void Main(string[] args)
        {
            AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);

            voice = new Voice();

            commandProcessor = ConfigureCommands().CreateCommandProcessor();
            commandProcessor.CommandRecognized += sound.NotifyRecognizedCommandAsync;
            commandProcessor.CommandRejected += sound.NotifyUnrecognizedCommandAsync;

            Console.WriteLine("Attached PIR-1 devices:");
            foreach (var pir in PIRDriver.Instance.QueryAttachedDevices())
                Console.WriteLine("\t{0}", pir);

            ConfigureLightShow();
            Console.WriteLine("Configured LightShow");

            var recognizer = GetKinectRecognizer();
            using (var sensor = GetKinectSensor())
            {
                /* Skeleton-based beam control is disabled due to an OOM issue when long running.
                var beamController = new SkeletonBasedBeamControl();
                beamController.AttentionGestureDetected += delegate(SkeletonBasedBeamControl controller)
                {
                    sound.NotifyAttentionGestureRecognized();
                };
                beamController.Start(sensor);
                */

                sensor.Start();
                var source = sensor.AudioSource;

                source.AutomaticGainControlEnabled = false;
                source.EchoCancellationMode = EchoCancellationMode.None;
                source.NoiseSuppression = true;

                Console.WriteLine("Using: {0}", recognizer.Name);

                using (Stream s = source.Start())
                {
                    SpeechRecognitionEngine sre = null;
                    var sreLock = new object();

                    EventHandler<SpeechDetectedEventArgs> SreSpeechDetected = delegate(object sender, SpeechDetectedEventArgs dea) { SpeechDetected(source, dea); };

                    Action startRecognizer = delegate()
                    {
                        SpeechRecognitionEngine oldSre = null;

                        lock (sreLock)
                        {
                            if (sre != null)
                            {
                                oldSre = sre;
                            }
                            sre = new SpeechRecognitionEngine(recognizer.Id);
                            sre.UpdateRecognizerSetting("AdaptationOn", 1);
                            sre.UpdateRecognizerSetting("PersistedBackgroundAdaptation", 1);
                            sre.LoadGrammar(commandProcessor.CreateGrammar());

                            sre.SpeechDetected += SreSpeechDetected;
                            sre.SpeechHypothesized += SreSpeechHypothesized;
                            sre.SpeechRecognitionRejected += SreSpeechRecognitionRejected;
                            sre.AudioSignalProblemOccurred += SreAudioSignalProblemOccurred;

                            sre.EndSilenceTimeoutAmbiguous = TimeSpan.FromMilliseconds(AmbiguousSilenceTimeout);
                            sre.EndSilenceTimeout = TimeSpan.FromMilliseconds(UnambiguousSilenceTimeout);

                            sre.SpeechRecognized += delegate(object sender, SpeechRecognizedEventArgs r)
                            {
                                Console.WriteLine("Handling text {0} in command processor", r.Result.Text);
                                try
                                {
                                    commandProcessor.ProcessSpeech(r.Result);
                                }
                                catch (Exception ex)
                                {
                                    Console.WriteLine("Command handler failed: " + ex.ToString());
                                    voice.SpeakAsync("Failed to execute command. Sorry!");
                                }
                            };

                            sre.SetInputToAudioStream(s,
                                                      new SpeechAudioFormatInfo(
                                                          EncodingFormat.Pcm, 16000, 16, 1,
                                                          32000, 2, null));
                            sre.RecognizeAsync(RecognizeMode.Multiple);
                            Trace.TraceInformation("New recognizer started");

                            if (oldSre != null)
                            {
                                oldSre.RecognizeAsyncStop();

                                oldSre.SpeechDetected -= SreSpeechDetected;
                                oldSre.SpeechHypothesized -= SreSpeechHypothesized;
                                oldSre.SpeechRecognitionRejected -= SreSpeechRecognitionRejected;
                                oldSre.AudioSignalProblemOccurred -= SreAudioSignalProblemOccurred;

                                oldSre.Dispose();
                                Trace.TraceInformation("Old recognizer disposed");
                            }
                        }
                    };

                    var recognizerRecycleTimer = new System.Timers.Timer()
                    {
                        AutoReset = false,
                        Enabled = false,
                        Interval = RecognizerRecyleTime.TotalMilliseconds,
                    };
                    recognizerRecycleTimer.Elapsed += (sender, elapsedEventArgs) =>
                    {
                        Trace.TraceInformation("Recycling recognizer...");
                        startRecognizer();
                        recognizerRecycleTimer.Start();
                        Trace.TraceInformation("Done recycling recognizer.");
                    };

                    startRecognizer();
                    Console.WriteLine("Ready.");
                    voice.SpeakAsync("Ez-ree is now online.");

                    recognizerRecycleTimer.Start();

                    Console.ReadLine();
                    Console.WriteLine("Stopping recognizer ...");
                    // TODO: poison flag so the recycle timer doesn't get in the way
                    lock (sreLock)
                    {
                        sre.RecognizeAsyncStop();
                        sre.Dispose();
                    }
                    // beamController.Stop();
                }
            }
        }