Exemple #1
0
        /// <summary>
        /// Handler for speech detection events
        /// </summary>
        /// <param name="detectionResult"></param>
        /// <returns></returns>
        bool HandleDetectionResult(DetectionResult detectionResult)
        {
            if (null == detectionResult)
            {
                return(false);
            }
            SpeechRecognitionResult[] results = detectionResult.results;
            if (null == results)
            {
                return(false);
            }
            foreach (SpeechRecognitionResult result in results)
            {
                SpeechRecognitionAlternative[] alternatives = result.alternatives;
                if (null == alternatives)
                {
                    continue;
                }
                foreach (SpeechRecognitionAlternative alternative in alternatives)
                {
                    if (string.IsNullOrEmpty(alternative.transcript))
                    {
                        continue;
                    }
                    if (result.isFinal)
                    {
                        _mWords.Add(string.Format("[FINAL] \"{0}\" Confidence={1}",
                                                  alternative.transcript,
                                                  alternative.confidence));
                    }
                    else
                    {
                        _mWords.Add(string.Format("\"{0}\" Confidence={1}",
                                                  alternative.transcript,
                                                  alternative.confidence));
                    }
                }
            }
            while (_mWords.Count > 15)
            {
                _mWords.RemoveAt(0);
            }

            if (_mTextDictation)
            {
                if (_mStringBuilder.Length > 0)
                {
                    _mStringBuilder.Remove(0, _mStringBuilder.Length);
                }
                foreach (string text in _mWords)
                {
                    _mStringBuilder.AppendLine(text);
                }
                _mTextDictation.text = _mStringBuilder.ToString();
            }

            // dictation doesn't need to handle the event
            return(false);
        }
Exemple #2
0
 /// <summary>
 /// Invoke all the listeners until event is handled
 /// </summary>
 /// <param name="detectionResult"></param>
 public void Invoke(DetectionResult detectionResult)
 {
     foreach (DelegateHandleDetectionResult listener in _sOnDetectionResults)
     {
         if (listener.Invoke(detectionResult))
         {
             return;
         }
     }
 }
Exemple #3
0
        /// <summary>
        /// Handler for speech detection events
        /// </summary>
        /// <param name="detectionResult"></param>
        /// <returns></returns>
        bool HandleDetectionResult(DetectionResult detectionResult)
        {
            bool enabled = EditorProxySpeechDetectionPlugin.IsEnabled();

            if (!enabled)
            {
                return(false);
            }

            //Debug.Log("Example06PanelDictation: HandleDetectionResult:");
            if (null == detectionResult)
            {
                return(false);
            }
            SpeechRecognitionResult[] results = detectionResult.results;
            if (null == results)
            {
                return(false);
            }
            foreach (SpeechRecognitionResult result in results)
            {
                SpeechRecognitionAlternative[] alternatives = result.alternatives;
                if (null == alternatives)
                {
                    continue;
                }
                foreach (SpeechRecognitionAlternative alternative in alternatives)
                {
                    if (string.IsNullOrEmpty(alternative.transcript))
                    {
                        continue;
                    }
                    if (result.isFinal)
                    {
                        _mWords.Add(string.Format("[FINAL] \"{0}\" Confidence={1}",
                                                  alternative.transcript,
                                                  alternative.confidence));
                    }
                    else
                    {
                        _mWords.Add(string.Format("\"{0}\" Confidence={1}",
                                                  alternative.transcript,
                                                  alternative.confidence));
                    }
                }
            }
            while (_mWords.Count > 15)
            {
                _mWords.RemoveAt(0);
            }
            Repaint();

            // dictation doesn't need to handle the event
            return(false);
        }
Exemple #4
0
        /// <summary>
        /// Handler for speech detection events
        /// </summary>
        /// <param name="detectionResult"></param>
        /// <returns>Return true if the result was handled</returns>
        bool HandleDetectionResult(DetectionResult detectionResult)
        {
            if (null == detectionResult)
            {
                return(false);
            }
            SpeechRecognitionResult[] results = detectionResult.results;
            if (null == results)
            {
                return(false);
            }
            bool doAbort = false;

            foreach (SpeechRecognitionResult result in results)
            {
                SpeechRecognitionAlternative[] alternatives = result.alternatives;
                if (null == alternatives)
                {
                    continue;
                }
                foreach (SpeechRecognitionAlternative alternative in alternatives)
                {
                    if (string.IsNullOrEmpty(alternative.transcript))
                    {
                        continue;
                    }
                    string lower = alternative.transcript.ToLower();
                    Debug.LogFormat("Detected: {0}", lower);
                    foreach (KeyValuePair <string, Example02Word> kvp in _mWords)
                    {
                        if (lower.Contains(kvp.Key))
                        {
                            kvp.Value.Highlight();
                            doAbort = true;
                            break;
                        }
                    }
                }
                if (doAbort)
                {
                    break;
                }
            }

            // abort detection on match for faster matching on words instead of complete sentences
            if (doAbort)
            {
                _mSpeechDetectionPlugin.Abort();
                return(true);
            }

            return(false);
        }
        /// <summary>
        /// Proxy speech detection results
        /// </summary>
        /// <returns></returns>
        protected IEnumerator GetResult()
        {
            DateTime wait;

            //Debug.Log("ProxySpeechDetectionPlugin: GetResult:");
            while (true)
            {
                string url = string.Format("http://localhost:{0}/SpeechDetectionGetResult", _mPort);
                IWWW   www = CreateWWW(url);
                while (null == www.GetError() &&
                       !www.IsDone())
                {
#if UNITY_2018_1_OR_NEWER
                    yield return(www.SendWebRequest());
#else
                    yield return(null);
#endif
                }

                string error    = www.GetError();
                bool   hasError = !string.IsNullOrEmpty(error);
                if (!hasError)
                {
                    string jsonData = www.GetText();
                    if (!string.IsNullOrEmpty(jsonData))
                    {
                        //Debug.LogFormat("ProxySpeechDetectionPlugin: GetResult: jsondata={0}", jsonData);
                        DetectionResult detectionResult = JsonUtility.FromJson <DetectionResult>(jsonData);
                        Invoke(detectionResult);
                    }
                }
                www.Dispose();
                if (hasError)
                {
                    //Debug.LogError(error); // change to status message
                    wait = DateTime.Now + TimeSpan.FromSeconds(1);
                    while (wait > DateTime.Now)
                    {
                        yield return(null);
                    }
                    continue;
                }
                wait = DateTime.Now + TimeSpan.FromSeconds(0.1f);
                while (wait > DateTime.Now)
                {
                    yield return(null);
                }
            }
        }
 void FixedUpdate()
 {
     if (OnlyWebGL.WebGLSpeechDetectionPluginGetNumberOfResults() > 0)
     {
         string jsonData = OnlyWebGL.WebGLSpeechDetectionPluginGetResult();
         if (string.IsNullOrEmpty(jsonData))
         {
             return;
         }
         DetectionResult detectionResult = JsonUtility.FromJson <DetectionResult>(jsonData);
         if (null != detectionResult)
         {
             Invoke(detectionResult);
         }
     }
 }
Exemple #7
0
        /// <summary>
        /// Handler for speech detection events
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="args"></param>
        bool HandleDetectionResult(DetectionResult detectionResult)
        {
            if (null == detectionResult)
            {
                return(false);
            }
            SpeechRecognitionResult[] results = detectionResult.results;
            if (null == results)
            {
                return(false);
            }
            foreach (SpeechRecognitionResult result in results)
            {
                SpeechRecognitionAlternative[] alternatives = result.alternatives;
                if (null == alternatives)
                {
                    continue;
                }
                foreach (SpeechRecognitionAlternative alternative in alternatives)
                {
                    if (string.IsNullOrEmpty(alternative.transcript))
                    {
                        continue;
                    }
                    if (result.isFinal)
                    {
                        Debug.Log(string.Format("[FINAL] \"{0}\" Confidence={1}",
                                                alternative.transcript,
                                                alternative.confidence));
                    }
                    else
                    {
                        Debug.Log(string.Format("\"{0}\" Confidence={1}",
                                                alternative.transcript,
                                                alternative.confidence));
                    }
                }
            }

            // dictation doesn't need to handle the event
            return(false);
        }
        /// <summary>
        /// Handler for speech detection events
        /// </summary>
        /// <param name="args"></param>
        bool HandleDetectionResult(DetectionResult detectionResult)
        {
            bool enabled = EditorProxySpeechDetectionPlugin.IsEnabled();

            if (!enabled)
            {
                return(false);
            }

            //Debug.Log("Example06PanelDictation: HandleDetectionResult:");
            if (null == detectionResult)
            {
                return(false);
            }
            SpeechRecognitionResult[] results = detectionResult.results;
            if (null == results)
            {
                return(false);
            }
            bool doAbort = false;

            foreach (SpeechRecognitionResult result in results)
            {
                if (doAbort)
                {
                    break;
                }
                SpeechRecognitionAlternative[] alternatives = result.alternatives;
                if (null == alternatives)
                {
                    continue;
                }
                foreach (SpeechRecognitionAlternative alternative in alternatives)
                {
                    if (string.IsNullOrEmpty(alternative.transcript))
                    {
                        continue;
                    }
                    string lower = alternative.transcript.ToLower();
                    SetTextStatus(lower);
                    if (RunEditorCommands(lower))
                    {
                        doAbort = true;
                        break;
                    }
                }
            }

            if (doAbort)
            {
                EditorProxySpeechDetectionPlugin plugin = EditorProxySpeechDetectionPlugin.GetInstance();
                if (null != plugin)
                {
                    plugin.Abort();
                }

                return(true);
            }

            return(false);
        }