コード例 #1
0
        public bool Initialize()
        {
            try
            {
                if (!string.IsNullOrEmpty(_grammarFile) && File.Exists(_grammarFile))
                {
                    _speechEngine = new SpeechRecognitionEngine();

                    Console.WriteLine("Speech Engine created");

                    // Load the grammar file
                    var g = new Grammar(_grammarFile);
                    _speechEngine.LoadGrammar(g);

                    Console.WriteLine("Loaded Grammar File : {0}", _grammarFile);

                    // Subscribe to events
                    _speechEngine.SpeechRecognized += SpeechRecognized;
                    _speechEngine.SpeechRecognitionRejected += SpeechRejected;

                    _speechEngine.SetInputToDefaultAudioDevice();
                    _speechEngine.RecognizeAsync(RecognizeMode.Multiple);

                    if (_voiceCommandPublisher != null)
                    {
                        _voiceCommandPublisher.Initialize();
                    }

                    _speechCommand = new SpeechCommand(_voiceCommandPublisher);
                    _speechCommandTask = Task.Factory.StartNew(() => _speechCommand.Run(100));

                    Console.WriteLine("Started recognizing");
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex.Message);
                Log.Error(ex.StackTrace);
            }
            return false;
        }
コード例 #2
0
        public bool Initialize()
        {
            try
            {
                if (_kinectSensor == null && !string.IsNullOrEmpty(_grammarFile) && System.IO.File.Exists(_grammarFile))
                {
                    _kinectSensor = KinectSensor.GetDefault();
                    if (_kinectSensor != null)
                    {
                        // open the sensor
                        _kinectSensor.Open();

                        Console.WriteLine("Sensor Opened");

                        // grab the audio stream
                        IReadOnlyList<AudioBeam> audioBeamList = _kinectSensor.AudioSource.AudioBeams;
                        System.IO.Stream audioStream = audioBeamList[0].OpenInputStream();

                        // create the convert stream
                        _convertStream = new KinectAudioStream(audioStream);

                        Console.WriteLine("Stream created");

                        var ri = TryGetKinectRecognizer();

                        if (ri != null)
                        {
                            Console.WriteLine("Kinect recognizer exists");
                            // Create instance of the speech engine
                            _speechEngine = new SpeechRecognitionEngine(ri.Id);

                            Console.WriteLine("Speech Engine created");

                            // Load the grammar file
                            var g = new Grammar(_grammarFile);
                            _speechEngine.LoadGrammar(g);

                            Console.WriteLine("Loaded Grammar File : {0}", _grammarFile);

                            // Subscribe to events
                            _speechEngine.SpeechRecognized += SpeechRecognized;
                            _speechEngine.SpeechRecognitionRejected += SpeechRejected;

                            // let the convertStream know speech is going active
                            _convertStream.SpeechActive = true;

                            // For long recognition sessions (a few hours or more), it may be beneficial to turn off adaptation of the acoustic model. 
                            // This will prevent recognition accuracy from degrading over time.
                            ////speechEngine.UpdateRecognizerSetting("AdaptationOn", 0);

                            _speechEngine.SetInputToAudioStream(
                                _convertStream,
                                new SpeechAudioFormatInfo(EncodingFormat.Pcm, 16000, 16, 1, 32000, 2, null));
                            _speechEngine.RecognizeAsync(RecognizeMode.Multiple);

                            if (_voiceCommandPublisher != null)
                            {
                                _voiceCommandPublisher.Initialize();
                            }

                            _speechCommand = new SpeechCommand(_voiceCommandPublisher);
                            _speechCommandTask = Task.Factory.StartNew(() => _speechCommand.Run(200));

                            Console.WriteLine("Started recognizing");
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex.Message);
                Log.Error(ex.StackTrace);
            }
            return false;
        }