コード例 #1
0
        /// <summary>
        /// Speech recognized handler
        /// </summary>
        /// <param name="recognized"></param>
        private void SpeechRecognizedHandler(sr.SpeechRecognized recognized)
        {
            TimeSpan sinceTalk = DateTime.Now - Talker.lastSpoken;

            if (sinceTalk.TotalSeconds < SpeechRecognizerTalkerBlackoutSec)
            {
                Tracer.Trace("SpeechRecognizedHandler in blackout at " + sinceTalk.TotalSeconds + " sec");
                return;
            }

            int    angle            = Direction.to180fromRad(-recognized.Body.Angle);
            string commandText      = recognized.Body.Text;
            string commandSemantics = recognized.Body.Semantics.ValueString;
            double confidence       = Math.Round(recognized.Body.Confidence, 3); // 0 to 1, usually around 0.97 for successfully recognized commands

            //Tracer.Trace("****************************************  SpeechRecognizedHandler  **************************************** ");
            //Tracer.Trace("speech '" + commandText + "'=>'" + commandSemantics + "' at " + angle + " degrees,  confidence " + confidence);


            // find the handler based on semantics:
            SpeechRecognizerDictionaryItem srdi = (from di in speechRecognizerDictionary
                                                   where di.semantics == commandSemantics
                                                   select di).FirstOrDefault();


            // usually Confidence is 0.95-0.99
            if (srdi != null && recognized.Body.Confidence > srdi.minimumRequiredConfidence)
            {
                VoiceCommandState state = _state.VoiceCommandState;
                state.TimeStamp         = DateTime.Now;
                state.Text              = commandText;
                state.Semantics         = commandSemantics;
                state.ConfidencePercent = (int)Math.Round(confidence * 100.0f);
                state.Direction         = angle;

                LogHistory(1, "speech '" + commandText + "'=>'" + commandSemantics + "' at " + angle + " deg,   " + confidence);

                // now call the handler:
                srdi.handler(srdi, angle);
            }
            else if (confidence > 0.5d)
            {
                LogHistory(3, "rejected '" + commandText + "'=>'" + commandSemantics + "' at " + angle + " deg,   " + confidence);

                Talker.Say(10, "Homm");     // not enough confidence
            }

            //setPan(angle);
            //setTilt(0);
        }