Exemple #1
0
        public static void Recognize(string phrase, Action <SpeechRecognizedEventArgs> action)
        {
            // If this is the first phrase to listen for
            if (recognitions.Count == 0)
            {
                // Do some setup.
                stream = KinectAudioStreamManager.Get();

                recognizer = GetKinectRecognizer();

                // Something went wrong
                if (recognizer == null)
                {
                    KinectAudioStreamManager.Release(ref stream);
                    throw new InvalidOperationException("Unable to find a Kinect Speech Recognizer");
                }

                try
                {
                    engine = new SpeechRecognitionEngine(recognizer.Id);
                    engine.SpeechRecognized += SpeechRecognized;

                    engine.SetInputToAudioStream(stream,
                                                 new SpeechAudioFormatInfo(
                                                     EncodingFormat.Pcm, 16000, 16, 1,
                                                     32000, 2, null));

                    //engine.RecognizeAsync();
                }
                catch (Exception e)
                {
                    KinectAudioStreamManager.Release(ref stream);
                    // rethrow
                    throw e;
                }
            }

            // Add our phrase
            if (recognitions.ContainsKey(phrase))
            {
                // Add this action to the list
                recognitions[phrase].Add(action);
            }
            else
            {
                // Create a new list with only this action in it
                var recognizers = new List <Action <SpeechRecognizedEventArgs> >();
                recognizers.Add(action);

                recognitions.Add(phrase, recognizers);
            }

            ResetGrammar();
        }
Exemple #2
0
        public static void CancelRecognize(string phrase, Action <SpeechRecognizedEventArgs> action)
        {
            if (!recognitions.ContainsKey(phrase))
            {
                throw new InvalidOperationException("The given phrase has no registered handlers.");
            }

            var handlers = recognitions[phrase];

            if (!handlers.Contains(action))
            {
                throw new InvalidOperationException("The given handler is not registered with the given phrase.");
            }

            // Remove the handler
            handlers.Remove(action);

            // If we have no more handlers, remove the phrase
            if (handlers.Count == 0)
            {
                recognitions.Remove(phrase);
            }

            // We have nothing else to listen for
            if (recognitions.Count == 0)
            {
                engine.RecognizeAsyncCancel();
                engine.SpeechRecognized -= SpeechRecognized;
                engine = null;
                KinectAudioStreamManager.Release(ref stream);
            }
            else
            {
                ResetGrammar();
            }
        }