Example #1
0
        public static void PopulateLanguages(out string[] languageOptions,
                                             out int languageIndex,
                                             LanguageResult languageResult)
        {
            List <string> options = GetDefaultLanguageOptions();

            languageIndex   = 0;
            languageOptions = options.ToArray();

            // prepare the language drop down items
            if (null == languageResult)
            {
                Debug.LogError("Language result is not set!");
                return;
            }

            if (null != languageResult.languages)
            {
                foreach (Language language in languageResult.languages)
                {
                    if (!string.IsNullOrEmpty(language.display))
                    {
                        options.Add(language.display);
                    }
                    else if (!string.IsNullOrEmpty(language.name))
                    {
                        options.Add(language.name);
                    }
                }
            }

            languageOptions = options.ToArray();
        }
Example #2
0
        public static Language GetLanguage(LanguageResult languageResult,
                                           string display)
        {
            if (null == languageResult ||
                null == languageResult.languages)
            {
                Debug.LogError("Languages are not set!");
                return(null);
            }

            foreach (Language language in languageResult.languages)
            {
                if (!string.IsNullOrEmpty(language.display) &&
                    language.display.Equals(display))
                {
                    return(language);
                }
                else if (!string.IsNullOrEmpty(language.name) &&
                         language.name.Equals(display))
                {
                    return(language);
                }
            }
            return(null);
        }
Example #3
0
        public static string GetLanguageDisplay(LanguageResult languageResult,
                                                string name)
        {
            if (null == languageResult ||
                null == languageResult.languages)
            {
                Debug.LogError("Languages are not set!");
                return(null);
            }

            foreach (Language language in languageResult.languages)
            {
                if (string.IsNullOrEmpty(language.name))
                {
                    continue;
                }
                else if (language.name == name)
                {
                    if (!string.IsNullOrEmpty(language.display))
                    {
                        return(language.display);
                    }
                    else if (!string.IsNullOrEmpty(language.name))
                    {
                        return(language.name);
                    }
                }
            }
            return(null);
        }
Example #4
0
        /// <summary>
        /// Handler for changing dialects
        /// </summary>
        public static void HandleDialectChanged(string[] dialectOptions,
                                                int dialectIndex,
                                                LanguageResult languageResult,
                                                ISpeechDetectionPlugin plugin)
        {
            if (null == dialectOptions)
            {
                Debug.LogError("The dialect options are not set!");
                return;
            }

            if (null == languageResult)
            {
                Debug.LogError("The language result is not set!");
                return;
            }

            string display = dialectOptions[dialectIndex];

            Dialect dialect = null;

            if (dialectIndex > 0)
            {
                //Debug.Log(display);
                SetDefaultDialect(display);

                dialect = SpeechDetectionUtils.GetDialect(languageResult, display);
                if (null == dialect)
                {
                    Debug.LogError("Did not find specified dialect!");
                    return;
                }

                //Debug.Log(dialect.name);
                if (null != plugin)
                {
                    plugin.Abort();
                    plugin.SetLanguage(dialect.name);

                    LanguageChangedResult languageChangedResult = new LanguageChangedResult();
                    languageChangedResult._mDialect = dialect.name;
                    plugin.Invoke(languageChangedResult);
                }
            }
        }
        public void GetLanguages(Action <LanguageResult> callback)
        {
#if UNITY_WEBGL && !UNITY_EDITOR
            string jsonData = OnlyWebGL.WebGLSpeechDetectionPluginGetLanguages();
            if (string.IsNullOrEmpty(jsonData))
            {
                Debug.LogError("GetLanguages: Languages are empty!");
                callback.Invoke(null);
                return;
            }
            //Debug.Log("GetLanguages=" + jsonData);
            LanguageResult languageResult = JsonUtility.FromJson <LanguageResult>(jsonData);
            callback.Invoke(languageResult);
            return;
#else
            callback.Invoke(null);
#endif
        }
Example #6
0
        /// <summary>
        /// Handler for changing dialects
        /// </summary>
        public static void HandleDialectChanged(Dropdown dropdownDialects,
                                                LanguageResult languageResult,
                                                ISpeechDetectionPlugin plugin)
        {
            if (null == dropdownDialects)
            {
                Debug.LogError("The dropdown for dialects is not set!");
                return;
            }

            if (null == languageResult)
            {
                Debug.LogError("The language result is not set!");
                return;
            }

            string display = dropdownDialects.options[dropdownDialects.value].text;

            Dialect dialect = null;

            if (dropdownDialects.value > 0)
            {
                //Debug.Log(display);
                SetDefaultDialect(display);

                dialect = SpeechDetectionUtils.GetDialect(languageResult, display);
                if (null == dialect)
                {
                    Debug.LogError("Did not find specified dialect!");
                    return;
                }

                //Debug.Log(dialect.name);
                if (null != plugin)
                {
                    plugin.Abort();
                    plugin.SetLanguage(dialect.name);

                    LanguageChangedResult languageChangedResult = new LanguageChangedResult();
                    languageChangedResult._mDialect = dialect.name;
                    plugin.Invoke(languageChangedResult);
                }
            }
        }
Example #7
0
        public static void PopulateLanguagesDropdown(Dropdown dropdownLanguages,
                                                     LanguageResult languageResult)
        {
            if (null == dropdownLanguages)
            {
                Debug.LogError("The dropdown for languages is not set!");
                return;
            }

            if (null == languageResult)
            {
                Debug.LogError("The language result is not set!");
                return;
            }

            // prepare the language drop down items
            List <string> options = GetDefaultLanguageOptions();

            if (null != languageResult)
            {
                if (null != languageResult.languages)
                {
                    foreach (Language language in languageResult.languages)
                    {
                        if (!string.IsNullOrEmpty(language.display))
                        {
                            options.Add(language.display);
                        }
                        else if (!string.IsNullOrEmpty(language.name))
                        {
                            options.Add(language.name);
                        }
                    }
                }
            }
            SpeechDetectionUtils.PopulateDropdown(dropdownLanguages, options);
        }
Example #8
0
        public static string GetDialectDisplay(LanguageResult languageResult,
                                               string name)
        {
            if (null == languageResult ||
                null == languageResult.languages)
            {
                Debug.LogError("Languages are not set!");
                return(null);
            }

            foreach (Language language in languageResult.languages)
            {
                if (null == language.dialects)
                {
                    continue;
                }
                foreach (Dialect dialect in language.dialects)
                {
                    if (dialect.name == name)
                    {
                        if (!string.IsNullOrEmpty(dialect.display))
                        {
                            return(dialect.display);
                        }
                        else if (!string.IsNullOrEmpty(dialect.description))
                        {
                            return(dialect.description);
                        }
                        else if (!string.IsNullOrEmpty(dialect.name))
                        {
                            return(dialect.name);
                        }
                    }
                }
            }
            return(null);
        }
Example #9
0
        // Use this for initialization
        IEnumerator Start()
        {
            // get the singleton instance
            _mSpeechDetectionPlugin = SpeechDetectionUtils.GetInstance();

            // check the reference to the plugin
            if (null == _mSpeechDetectionPlugin)
            {
                Debug.LogError("Proxy Speech Detection Plugin is not set!");
                yield break;
            }

            // wait for plugin to become available
            while (!_mSpeechDetectionPlugin.IsAvailable())
            {
                yield return(null);
            }

            // no need to display a warning if the plugin is available
            SpeechDetectionUtils.SetActive(false, _mTextWaiting);

            // display the UI controls
            SpeechDetectionUtils.SetActive(true,
                                           _mDropDownDialects, _mDropDownLanguages,
                                           _mTextDictation, _mTextSummary);

            // subscribe to events
            _mSpeechDetectionPlugin.AddListenerOnDetectionResult(HandleDetectionResult);

            // Get languages from plugin,
            _mSpeechDetectionPlugin.GetLanguages((languageResult) =>
            {
                _mLanguageResult = languageResult;

                // prepare the language drop down items
                SpeechDetectionUtils.PopulateLanguagesDropdown(_mDropDownLanguages, _mLanguageResult);

                // subscribe to language change events
                if (_mDropDownLanguages)
                {
                    _mDropDownLanguages.onValueChanged.AddListener(delegate {
                        SpeechDetectionUtils.HandleLanguageChanged(_mDropDownLanguages,
                                                                   _mDropDownDialects,
                                                                   _mLanguageResult,
                                                                   _mSpeechDetectionPlugin);
                    });
                }

                // subscribe to dialect change events
                if (_mDropDownDialects)
                {
                    _mDropDownDialects.onValueChanged.AddListener(delegate {
                        SpeechDetectionUtils.HandleDialectChanged(_mDropDownDialects,
                                                                  _mLanguageResult,
                                                                  _mSpeechDetectionPlugin);
                    });
                }

                // Disabled until a language is selected
                SpeechDetectionUtils.DisableDialects(_mDropDownDialects);

                // set the default language
                SpeechDetectionUtils.RestoreLanguage(_mDropDownLanguages);

                // set the default dialect
                SpeechDetectionUtils.RestoreDialect(_mDropDownDialects);
            });
        }
Example #10
0
        public static bool HandleOnLanguageChanged(
            ref int languageIndex,
            string[] languageOptions,
            ref int dialectIndex,
            string[] dialectOptions,
            LanguageResult languageResult,
            ISpeechDetectionPlugin plugin,
            LanguageChangedResult languageChangedResult)
        {
            if (_sPausePreferences)
            {
                return(false);
            }

            if (null == languageResult)
            {
                Debug.LogError("LanguageResult is null!");
                return(false);
            }

            if (null == languageChangedResult)
            {
                Debug.LogError("LanguageChangedResult is null!");
                return(false);
            }

            bool changed = false;

            /*
             * Debug.LogFormat("GetLanguageDisplay: language={0} dialect={1}",
             *  (languageChangedResult._mLanguage == null) ? "null" : languageChangedResult._mLanguage,
             *  (languageChangedResult._mDialect == null) ? "null" : languageChangedResult._mDialect);
             */

            if (null != languageChangedResult._mLanguage)
            {
                string language = GetLanguageDisplay(languageResult, languageChangedResult._mLanguage);
                for (int index = 0; index < languageOptions.Length; ++index)
                {
                    if (languageOptions[index] == language)
                    {
                        if (languageIndex != index)
                        {
                            languageIndex = index;
                            changed       = true;
                            break;
                        }
                    }
                }
            }

            if (null != languageChangedResult._mDialect)
            {
                string dialect = GetDialectDisplay(languageResult, languageChangedResult._mDialect);
                for (int index = 0; index < dialectOptions.Length; ++index)
                {
                    if (dialectOptions[index] == dialect)
                    {
                        if (dialectIndex != index)
                        {
                            dialectIndex = index;
                            changed      = true;
                        }
                    }
                }
            }

            return(changed);
        }
Example #11
0
        /// <summary>
        /// Handler for changing languages
        /// </summary>
        public static void HandleLanguageChanged(string[] languageOptions,
                                                 int languageIndex,
                                                 out string[] dialectOptions,
                                                 ref int dialectIndex,
                                                 LanguageResult languageResult,
                                                 ISpeechDetectionPlugin plugin)
        {
            List <string> options = GetDefaultDialectOptions();

            dialectOptions = options.ToArray();
            dialectIndex   = 0;

            if (null == languageOptions)
            {
                Debug.LogError("The language options are not set!");
                return;
            }

            if (null == dialectOptions)
            {
                Debug.LogError("The dialect options are not set!");
                return;
            }

            if (null == languageResult)
            {
                Debug.LogError("The language result is not set!");
                return;
            }

            string display = languageOptions[languageIndex];

            //Debug.Log(display);
            SetDefaultLanguage(display);

            Language language = null;

            if (languageIndex > 0)
            {
                language = SpeechDetectionUtils.GetLanguage(languageResult, display);
                if (null == language)
                {
                    Debug.LogError("Did not find specified language!");
                }
                else
                {
                    LanguageChangedResult languageChangedResult = new LanguageChangedResult();
                    languageChangedResult._mLanguage = language.name;
                    plugin.Invoke(languageChangedResult);
                }
            }

            if (languageIndex > 0 &&
                null != language &&
                null != language.dialects &&
                language.dialects.Length > 0)
            {
                foreach (Dialect dialect in language.dialects)
                {
                    if (!string.IsNullOrEmpty(dialect.display))
                    {
                        options.Add(dialect.display);
                    }
                    else if (!string.IsNullOrEmpty(dialect.description))
                    {
                        options.Add(dialect.description);
                    }
                    else if (!string.IsNullOrEmpty(dialect.name))
                    {
                        options.Add(dialect.name);
                    }
                }
            }
            dialectOptions = options.ToArray();
            if (dialectOptions.Length > 1)
            {
                dialectIndex = 1;
            }

            HandleDialectChanged(dialectOptions, dialectIndex, languageResult, plugin);
        }
Example #12
0
        /// <summary>
        /// Handler for changing languages
        /// </summary>
        public static void HandleLanguageChanged(Dropdown dropdownLanguages,
                                                 Dropdown dropdownDialects,
                                                 LanguageResult languageResult,
                                                 ISpeechDetectionPlugin plugin)
        {
            if (null == dropdownLanguages)
            {
                Debug.LogError("The dropdown for languages is not set!");
                return;
            }

            if (null == dropdownDialects)
            {
                Debug.LogError("The dropdown for dialects is not set!");
                return;
            }

            if (null == languageResult)
            {
                Debug.LogError("The language result is not set!");
                return;
            }

            string display = dropdownLanguages.options[dropdownLanguages.value].text;

            //Debug.Log(display);
            SetDefaultLanguage(display);

            Language language = null;

            if (dropdownLanguages.value > 0)
            {
                language = SpeechDetectionUtils.GetLanguage(languageResult, display);
                if (null == language)
                {
                    Debug.LogError("Did not find specified language!");
                }
                else
                {
                    LanguageChangedResult languageChangedResult = new LanguageChangedResult();
                    languageChangedResult._mLanguage = language.name;
                    plugin.Invoke(languageChangedResult);
                }
            }

            List <string> options = SpeechDetectionUtils.GetDefaultDialectOptions();

            if (dropdownLanguages.value > 0 &&
                null != language &&
                null != language.dialects &&
                language.dialects.Length > 0)
            {
                foreach (Dialect dialect in language.dialects)
                {
                    if (!string.IsNullOrEmpty(dialect.display))
                    {
                        options.Add(dialect.display);
                    }
                    else if (!string.IsNullOrEmpty(dialect.description))
                    {
                        options.Add(dialect.description);
                    }
                    else if (!string.IsNullOrEmpty(dialect.name))
                    {
                        options.Add(dialect.name);
                    }
                }
            }
            SpeechDetectionUtils.PopulateDropdown(dropdownDialects, options);
            SpeechDetectionUtils.SetInteractable(options.Count > 1, dropdownDialects);
            SpeechDetectionUtils.SelectIndex(dropdownDialects, 1);

            HandleDialectChanged(dropdownDialects, languageResult, plugin);
        }
Example #13
0
        // Use this for initialization
        IEnumerator Start()
        {
            // get the singleton instance
            _mSpeechDetectionPlugin = SpeechDetectionUtils.GetInstance();

            // check the reference to the plugin
            if (null == _mSpeechDetectionPlugin)
            {
                Debug.LogError("WebGL Speech Detection Plugin is not set!");
                yield break;
            }

            // wait for plugin to become available
            while (!_mSpeechDetectionPlugin.IsAvailable())
            {
                yield return(null);
            }

            // no need to display a warning if the plugin is available
            SpeechDetectionUtils.SetActive(false, _mTextWarning);

            // make the dropdowns non-interactive
            SpeechDetectionUtils.SetInteractable(false,
                                                 _mDropDownLanguages, _mDropDownDialects);

            // show UI controls
            SpeechDetectionUtils.SetActive(true,
                                           _mDropDownDialects, _mDropDownLanguages,
                                           _mImageWords, _mTextSummary);

            // subscribe to events
            _mSpeechDetectionPlugin.AddListenerOnDetectionResult(HandleDetectionResult);

            // Get languages from plugin,
            _mSpeechDetectionPlugin.GetLanguages((languageResult) =>
            {
                _mLanguageResult = languageResult;

                // prepare the language drop down items
                SpeechDetectionUtils.PopulateLanguagesDropdown(_mDropDownLanguages, _mLanguageResult);

                // make the dropdowns interactive
                SpeechDetectionUtils.SetInteractable(true,
                                                     _mDropDownLanguages, _mDropDownDialects);

                // subscribe to language change events
                if (_mDropDownLanguages)
                {
                    _mDropDownLanguages.onValueChanged.AddListener(delegate {
                        SpeechDetectionUtils.HandleLanguageChanged(_mDropDownLanguages,
                                                                   _mDropDownDialects,
                                                                   _mLanguageResult,
                                                                   _mSpeechDetectionPlugin);
                    });
                }

                // subscribe to dialect change events
                if (_mDropDownDialects)
                {
                    _mDropDownDialects.onValueChanged.AddListener(delegate {
                        SpeechDetectionUtils.HandleDialectChanged(_mDropDownDialects,
                                                                  _mLanguageResult,
                                                                  _mSpeechDetectionPlugin);
                    });
                }

                // Disabled until a language is selected
                SpeechDetectionUtils.DisableDialects(_mDropDownDialects);

                // set the default language
                SpeechDetectionUtils.RestoreLanguage(_mDropDownLanguages);

                // set the default dialect
                SpeechDetectionUtils.RestoreDialect(_mDropDownDialects);
            });

            // get all the words in the UI
            foreach (Example02Word word in GameObject.FindObjectsOfType <Example02Word>())
            {
                Text text = word.GetComponentInChildren <Text>();
                if (text)
                {
                    _mWords[text.text.ToLower()] = word;
                }
            }
        }
Example #14
0
        /// <summary>
        /// Reinitialize the editor proxy when enabled and not compiling
        /// </summary>
        private void Update()
        {
            bool enabled = EditorProxySpeechDetectionPlugin.IsEnabled();

            if (!enabled ||
                EditorApplication.isCompiling)
            {
                _mInitialized = false;
                return;
            }

            EditorProxySpeechDetectionPlugin plugin = EditorProxySpeechDetectionPlugin.GetInstance();

            if (null != plugin)
            {
                if (!_mInitialized)
                {
                    if (!plugin.IsAvailable())
                    {
                        return;
                    }

                    //Debug.LogFormat("Update: Plugin is available");

                    // Reset the detection list
                    _mWords.Clear();
                    _mWords.Add("Say something...");

                    plugin.GetLanguages((languageResult) =>
                    {
                        _mLanguageResult = languageResult;

                        // populate the language options
                        SpeechDetectionUtils.PopulateLanguages(out _mLanguages, out _mLanguage, languageResult);

                        // Restore the default language
                        SpeechDetectionUtils.RestoreLanguage(_mLanguages, out _mLanguage);

                        // Handle setting the default language
                        SpeechDetectionUtils.PausePlayerPrefs(true);
                        SpeechDetectionUtils.HandleLanguageChanged(_mLanguages,
                                                                   _mLanguage,
                                                                   out _mDialects,
                                                                   ref _mDialect,
                                                                   _mLanguageResult,
                                                                   plugin);
                        SpeechDetectionUtils.PausePlayerPrefs(false);

                        // Restore the default dialect
                        SpeechDetectionUtils.RestoreDialect(_mDialects, out _mDialect);

                        // Update UI
                        Repaint();
                    });

                    // subscribe to events
                    plugin.AddListenerOnDetectionResult(HandleDetectionResult);

                    // get on language changed events
                    plugin.AddListenerOnLanguageChanged(HandleOnLanguageChanged);

                    //Debug.Log("Update: HandleDetectionResult subscribed");

                    _mInitialized = true;

                    Repaint();
                }
            }

            // run pending UI actions
            while (_mPendingActions.Count > 0)
            {
                Action action = _mPendingActions[0];
                _mPendingActions.RemoveAt(0);
                action.Invoke();
            }
        }
        /// <summary>
        /// Proxy languages
        /// </summary>
        /// <returns></returns>
        private IEnumerator ProxyLanguages(Action <LanguageResult> callback)
        {
            DateTime wait;

            while (true)
            {
                string url = string.Format("http://localhost:{0}/SpeechDetectionGetLanguages", _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);
                string jsonData = null;
                if (!hasError)
                {
                    jsonData = www.GetText();
                }
                www.Dispose();
                if (hasError)
                {
                    //Debug.LogError(error); //switch to status message event
                    wait = DateTime.Now + TimeSpan.FromSeconds(1);
                    while (wait > DateTime.Now)
                    {
                        yield return(null);
                    }
                    continue;
                }
                if (!string.IsNullOrEmpty(jsonData))
                {
                    //Debug.Log(jsonData);
                    LanguageResult languageResult = null;
                    try
                    {
                        languageResult = JsonUtility.FromJson <LanguageResult>(jsonData);
                        //Debug.Log(jsonData);
                    }
                    catch (Exception)
                    {
                        Debug.LogError(string.Format("Failed to decode json: {0}", jsonData));
                        hasError = true;
                    }
                    if (hasError)
                    {
                        wait = DateTime.Now + TimeSpan.FromSeconds(1);
                        while (wait > DateTime.Now)
                        {
                            yield return(null);
                        }
                        continue;
                    }
                    callback.Invoke(languageResult);
                    yield break;
                }
                wait = DateTime.Now + TimeSpan.FromSeconds(1);
                while (wait > DateTime.Now)
                {
                    yield return(null);
                }
            }
        }
        /// <summary>
        /// Reinitialize the editor proxy when enabled and not compiling
        /// </summary>
        private void Update()
        {
            bool enabled = EditorProxySpeechDetectionPlugin.IsEnabled();

            if (!enabled ||
                EditorApplication.isCompiling)
            {
                _mInitialized = false;
                return;
            }

            // handle init
            EditorProxySpeechDetectionPlugin plugin = EditorProxySpeechDetectionPlugin.GetInstance();

            if (null != plugin)
            {
                if (!_mInitialized)
                {
                    if (!plugin.IsAvailable())
                    {
                        return;
                    }

                    //Debug.LogFormat("Update: Plugin is available");

                    plugin.GetLanguages((languageResult) =>
                    {
                        _mLanguageResult = languageResult;

                        // populate the language options
                        SpeechDetectionUtils.PopulateLanguages(out _mLanguages, out _mLanguage, languageResult);

                        // Restore the default language
                        SpeechDetectionUtils.RestoreLanguage(_mLanguages, out _mLanguage);

                        // Handle setting the default language
                        SpeechDetectionUtils.PausePlayerPrefs(true);
                        SpeechDetectionUtils.HandleLanguageChanged(_mLanguages,
                                                                   _mLanguage,
                                                                   out _mDialects,
                                                                   ref _mDialect,
                                                                   _mLanguageResult,
                                                                   plugin);
                        SpeechDetectionUtils.PausePlayerPrefs(false);

                        // Restore the default dialect
                        SpeechDetectionUtils.RestoreDialect(_mDialects, out _mDialect);

                        // Update UI
                        Repaint();
                    });

                    // subscribe to events
                    plugin.AddListenerOnDetectionResult(HandleDetectionResult);

                    // get on language changed events
                    plugin.AddListenerOnLanguageChanged(HandleOnLanguageChanged);

                    //Debug.Log("Update: HandleDetectionResult subscribed");

                    _mInitialized = true;

                    Repaint();
                }
            }

            // Check timers to deactivate button after delay
            bool doRepaint = false;

            foreach (KeyValuePair <string, MethodInfo> kvp in _mEditorCommands)
            {
                DateTime timer = _mTimers[kvp.Key];
                if (timer != DateTime.MinValue &&
                    timer < DateTime.Now)
                {
                    _mTimers[kvp.Key] = DateTime.MinValue;
                    doRepaint         = true;
                }
            }

            if (doRepaint)
            {
                Repaint();
            }

            // run pending UI actions
            while (_mPendingActions.Count > 0)
            {
                Action action = _mPendingActions[0];
                _mPendingActions.RemoveAt(0);
                action.Invoke();
            }
        }