Example #1
0
        private IEnumerator getVoices()
        {
#if !UNITY_EDITOR
            try
            {
                System.Collections.Generic.List <Model.Voice> voices = new System.Collections.Generic.List <Model.Voice>(70);
                string[] myStringVoices = ttsHandler.Voices;
                string   name;

                foreach (string voice in myStringVoices)
                {
                    string[] currentVoiceData = voice.Split(';');
                    name = currentVoiceData[0];
                    Model.Voice newVoice = new Model.Voice(name, "UWP voice: " + voice, Util.Helper.WSAVoiceNameToGender(name), "unknown", currentVoiceData[1]);
                    voices.Add(newVoice);
                }

                cachedVoices = voices.OrderBy(s => s.Name).ToList();

                if (Util.Constants.DEV_DEBUG)
                {
                    Debug.Log("Voices read: " + cachedVoices.CTDump());
                }
            }
            catch (System.Exception ex)
            {
                string errorMessage = "Could not get any voices!" + System.Environment.NewLine + ex;
                Debug.LogError(errorMessage);
                onErrorInfo(null, errorMessage);
            }
#endif
            yield return(null);

            onVoicesReady();
        }
Example #2
0
        private IEnumerator getVoices()
        {
#if UNITY_ANDROID || UNITY_EDITOR
            yield return(null);

            if (!isInitialized)
            {
                do
                {
                    yield return(wfs);
                } while (!(isInitialized = TtsHandler.CallStatic <bool>("isInitalized")));
            }

            try
            {
                string[] myStringVoices = TtsHandler.CallStatic <string[]>("GetVoices");

                System.Collections.Generic.List <Model.Voice> voices = new System.Collections.Generic.List <Model.Voice>(300);

                foreach (string voice in myStringVoices)
                {
                    string[] currentVoiceData = voice.Split(';');

                    Model.Enum.Gender gender = Model.Enum.Gender.UNKNOWN;

                    if (currentVoiceData[0].CTContains("#male"))
                    {
                        gender = Model.Enum.Gender.MALE;
                    }
                    else if (currentVoiceData[0].CTContains("#female"))
                    {
                        gender = Model.Enum.Gender.FEMALE;
                    }

                    Model.Voice newVoice = new Model.Voice(currentVoiceData[0], "Android voice: " + voice, gender, "unknown", currentVoiceData[1]);
                    voices.Add(newVoice);
                }

                cachedVoices = voices.OrderBy(s => s.Name).ToList();

                if (Util.Constants.DEV_DEBUG)
                {
                    Debug.Log("Voices read: " + cachedVoices.CTDump());
                }

                //onVoicesReady();
            }
            catch (System.Exception ex)
            {
                string errorMessage = "Could not get any voices!" + System.Environment.NewLine + ex;
                Debug.LogError(errorMessage);
                onErrorInfo(null, errorMessage);
            }
#else
            yield return(null);
#endif

            onVoicesReady();
        }
        /// <summary>Speak the text.</summary>
        public void Speak()
        {
            Silence();

            Model.Voice voice = Speaker.VoiceForName(RTVoiceName);

            if (voice == null)
            {
                voice = Speaker.VoiceForCulture(Culture);
            }

            string path = null;

            if (GenerateAudioFile && !string.IsNullOrEmpty(FilePath))
            {
                if (FileInsideAssets)
                {
                    path = Util.Helper.ValidatePath(Application.dataPath + @"/" + FilePath);
                }
                else
                {
                    path = Util.Helper.ValidatePath(FilePath);
                }

                //                if (!System.IO.Directory.Exists(path))
                //                {
                //                    System.IO.Directory.CreateDirectory(path);
                //                }

                path += FileName;
            }

            if (Util.Helper.isEditorMode)
            {
#if UNITY_EDITOR
                Speaker.SpeakNativeInEditor(Text, voice, Rate, Pitch, Volume);
                if (GenerateAudioFile)
                {
                    Speaker.GenerateInEditor(Text, voice, Rate, Pitch, Volume, path);
                }
#endif
            }
            else
            {
                if (Mode == Model.Enum.SpeakMode.Speak)
                {
                    uid = Speaker.Speak(Text, Source, voice, true, Rate, Pitch, Volume, path);
                }
                else
                {
                    uid = Speaker.SpeakNative(Text, voice, Rate, Pitch, Volume);
                }
            }
        }
Example #4
0
        private SpObjectToken getSapiVoice(Model.Voice voice)
        {
            if (voice != null)
            {
                foreach (SpObjectToken sapiVoice in availableVoices)
                {
                    if (sapiVoice.Id.Equals(voice.Identifier))
                    {
                        return(sapiVoice);
                    }
                }
            }

            return(null);
        }
Example #5
0
        /*
         *      /// <summary>Bridge to the native tts system</summary>
         *      /// <param name="id">Identifier of the voice to speak.</param>
         *      /// <param name="text">Text to speak.</param>
         *      /// <param name="rate">Speech rate of the speaker in percent (default: 1, optional).</param>
         *      /// <param name="pitch">Pitch of the speech in percent (default: 1, optional).</param>
         *      /// <param name="volume">Volume of the speaker in percent (default: 1, optional).</param>
         *      [System.Runtime.InteropServices.DllImport("__Internal")]
         *      extern static public void Speak(string id, string text, float rate = 1f, float pitch = 1f, float volume = 1f);
         */
#endif

        /// <summary>Receives all voices</summary>
        /// <param name="voicesText">All voices as text string.</param>
        public static void SetVoices(string voicesText)
        {
#if (UNITY_IOS || UNITY_EDITOR) && !UNITY_WEBPLAYER
            string[] voices = voicesText.Split(new char[] { ',' }, System.StringSplitOptions.RemoveEmptyEntries);

            if (voices.Length % 2 == 0)
            //if (voices.Length % 3 == 0)
            {
                //string id;
                string      name;
                string      culture;
                Model.Voice newVoice;

                cachedVoices.Clear();

                for (int ii = 0; ii < voices.Length; ii += 2)
                //for (int ii = 0; ii < voices.Length; ii += 3)
                {
                    name    = voices[ii];
                    culture = voices[ii + 1];
                    //id = voices[ii];
                    //name = voices[ii + 1];
                    //culture = voices[ii + 2];
                    name     = voices[ii];
                    culture  = voices[ii + 1];
                    newVoice = new Model.Voice(name, "iOS voice: " + name + " " + culture, culture);
                    //newVoice = new Model.Voice(id, name, "iOS voice: " + name + " " + culture, culture);

                    cachedVoices.Add(newVoice);
                }

                if (Util.Constants.DEV_DEBUG)
                {
                    Debug.Log("Voices read: " + cachedVoices.CTDump());
                }

                //onVoicesReady();
            }
            else
            {
                Debug.LogWarning("Voice-string contains wrong number of elements!");
            }
#endif

            onVoicesReady();
        }
Example #6
0
        private IEnumerator getVoices()
        {
#if (UNITY_ANDROID || UNITY_EDITOR) && !UNITY_WEBPLAYER
            yield return(null);

            if (!isInitialized)
            {
                do
                {
                    yield return(wfs);
                } while (!(isInitialized = TtsHandler.CallStatic <bool>("isInitalized")));
            }

            try
            {
                string[] myStringVoices = TtsHandler.CallStatic <string[]>("GetVoices");

                cachedVoices.Clear();

                foreach (string voice in myStringVoices)
                {
                    string[]    currentVoiceData = voice.Split(';');
                    Model.Voice newVoice         = new Model.Voice(currentVoiceData[0], "Android voice: " + voice, currentVoiceData[1]);
                    cachedVoices.Add(newVoice);
                }

                if (Util.Constants.DEV_DEBUG)
                {
                    Debug.Log("Voices read: " + cachedVoices.CTDump());
                }

                //onVoicesReady();
            }
            catch (System.Exception ex)
            {
                string errorMessage = "Could not get any voices!" + System.Environment.NewLine + ex;
                Debug.LogError(errorMessage);
                onErrorInfo(null, errorMessage);
            }
#else
            yield return(null);
#endif

            onVoicesReady();
        }
Example #7
0
        /// <summary>Receives all voices</summary>
        /// <param name="voicesText">All voices as text string.</param>
        public static void SetVoices(string voicesText)
        {
#if UNITY_IOS || UNITY_EDITOR
            string[] voices = voicesText.Split(new char[] { ',' }, System.StringSplitOptions.RemoveEmptyEntries);

            //if (voices.Length % 2 == 0)
            if (voices.Length % 3 == 0)
            {
                string      name;
                string      culture;
                Model.Voice newVoice;

                System.Collections.Generic.List <Model.Voice> voicesList = new System.Collections.Generic.List <Model.Voice>(60);

                //for (int ii = 0; ii < voices.Length; ii += 2)
                for (int ii = 0; ii < voices.Length; ii += 3)
                {
                    //name = voices[ii];
                    //culture = voices[ii + 1];
                    //newVoice = new Model.Voice(name, "iOS voice: " + name + " " + culture, Util.Helper.AppleVoiceNameToGender(name), "unknown", culture);

                    name     = voices[ii + 1];
                    culture  = voices[ii + 2];
                    newVoice = new Model.Voice(name, "iOS voice: " + name + " " + culture, Util.Helper.AppleVoiceNameToGender(name), "unknown", culture, voices[ii], "Apple");

                    voicesList.Add(newVoice);
                }

                cachediOSVoices = voicesList.OrderBy(s => s.Name).ToList();

                if (Util.Constants.DEV_DEBUG)
                {
                    Debug.Log("Voices read: " + cachediOSVoices.CTDump());
                }

                //onVoicesReady();
            }
            else
            {
                Debug.LogWarning("Voice-string contains wrong number of elements!");
            }
#endif

            onVoicesReady();
        }
        private IEnumerator playMe(Model.Sequence seq)
        {
            yield return(new WaitForSeconds(Delay));

            Model.Voice voice = Speaker.VoiceForName(seq.RTVoiceName);

            if (voice == null)
            {
                voice = Speaker.VoiceForCulture(Culture);
            }

            if (seq.Mode == Model.Enum.SpeakMode.Speak)
            {
                uidCurrentSpeaker = Speaker.Speak(seq.Text, seq.Source, voice, true, seq.Rate, seq.Pitch, seq.Volume);
            }
            else
            {
                uidCurrentSpeaker = Speaker.SpeakNative(seq.Text, voice, seq.Rate, seq.Pitch, seq.Volume);
            }
        }
        private IEnumerator initalizeVoices()
        {
            if (AllVoices)
            {
                foreach (Model.Voice voice in Speaker.Voices)
                {
                    activeUid = Speaker.SpeakNative(text, voice, 3, 1, 0);

                    do
                    {
                        yield return(null);
                    } while (!activeUid.Equals(completedUid));
                }
            }
            else
            {
                foreach (string voiceName in VoiceNames)
                {
                    if (!string.IsNullOrEmpty(voiceName))
                    {
                        if (Speaker.isVoiceForNameAvailable(voiceName))
                        {
                            Model.Voice voice = Speaker.VoiceForName(voiceName);

                            activeUid = Speaker.SpeakNative(text, voice, 3, 1, 0);

                            do
                            {
                                yield return(null);
                            } while (!activeUid.Equals(completedUid));
                        }
                    }
                }
            }

            if (DestroyWhenFinished)
            {
                Destroy(gameObject);
            }
        }
Example #10
0
        /// <summary>Receives all voices</summary>
        /// <param name="voicesText">All voices as text string.</param>
        public static void SetVoices(string voicesText)
        {
            string[] voices = voicesText.Split(new[] { ',' }, System.StringSplitOptions.RemoveEmptyEntries);

            if (voices.Length % 3 == 0)
            {
                System.Collections.Generic.List <Model.Voice> voicesList =
                    new System.Collections.Generic.List <Model.Voice>(60);

                //for (int ii = 0; ii < voices.Length; ii += 2)
                for (int ii = 0; ii < voices.Length; ii += 3)
                {
                    var name     = voices[ii + 1];
                    var culture  = voices[ii + 2];
                    var newVoice = new Model.Voice(name, "iOS voice: " + name + " " + culture,
                                                   Util.Helper.AppleVoiceNameToGender(name), "unknown", culture, voices[ii], "Apple");

                    voicesList.Add(newVoice);
                }

                cachediOSVoices = voicesList.OrderBy(s => s.Name).ToList();

                if (Util.Constants.DEV_DEBUG)
                {
                    Debug.Log("Voices read: " + cachediOSVoices.CTDump());
                }
            }
            else
            {
                Debug.LogWarning("Voice-string contains wrong number of elements!");
            }

            onVoicesReady();

            //NativeMethods.FreeMemory();
        }
        private void getVoicesInEditor()
        {
            System.Collections.Generic.List <string[]> serverVoicesResponse = new System.Collections.Generic.List <string[]>();

            if (!Util.Helper.isInternetAvailable)
            {
                string errorMessage = "Internet is not available - can't use MaryTTS right now!";
                Debug.LogError(errorMessage);
            }
            else
            {
                using (WWW www = new WWW(uri + "/voices", rawData, headers))
                {
                    float time = Time.realtimeSinceStartup;

                    do
                    {
                        // waiting...

#if !UNITY_WSA && !UNITY_WEBGL
                        System.Threading.Thread.Sleep(50);
#endif
                    } while (Time.realtimeSinceStartup - time < 5 && !www.isDone);

                    if (string.IsNullOrEmpty(www.error) && Time.realtimeSinceStartup - time < 5)
                    {
                        string[] rawVoices = www.text.Split('\n');
                        foreach (string rawVoice in rawVoices)
                        {
                            try
                            {
                                if (!string.IsNullOrEmpty(rawVoice))
                                {
                                    string[] newVoice =
                                    {
                                        rawVoice.Split(' ') [0],
                                        rawVoice.Split(' ') [1],
                                        rawVoice.Split(' ') [2]
                                    };
                                    serverVoicesResponse.Add(newVoice);
                                }
                            }
                            catch (System.Exception ex)
                            {
                                Debug.LogWarning("Problem preparing voice: " + rawVoice + " - " + ex);
                            }
                        }

                        cachedVoices.Clear();

                        foreach (string[] voice in serverVoicesResponse)
                        {
                            Model.Voice newVoice = new Model.Voice(voice[0], "MaryTTS voice: " + voice[0], voice[2], "unknown", voice[1]);
                            cachedVoices.Add(newVoice);
                        }

                        if (Util.Constants.DEV_DEBUG)
                        {
                            Debug.Log("Voices read: " + cachedVoices.CTDump());
                        }
                    }
                    else
                    {
                        string errorMessage = null;

                        if (!string.IsNullOrEmpty(www.error))
                        {
                            errorMessage = "Could not get the voices: " + www.error;
                        }
                        else
                        {
                            errorMessage = "Could not get the voices, MaryTTS had a timeout after 5 seconds. This indicates a very slow network connection or a bigger problem.";
                        }

                        Debug.LogError(errorMessage);
                    }
                }
            }
        }
        private IEnumerator getVoices()
        {
            System.Collections.Generic.List <string[]> serverVoicesResponse = new System.Collections.Generic.List <string[]>();

            if (!Util.Helper.isInternetAvailable)
            {
                string errorMessage = "Internet is not available - can't use MaryTTS right now!";
                Debug.LogError(errorMessage);
                onErrorInfo(null, errorMessage);
            }
            else
            {
                using (WWW www = new WWW(uri + "/voices", rawData, headers))
                {
                    do
                    {
                        yield return(www);
                    } while (!www.isDone);

                    if (string.IsNullOrEmpty(www.error))
                    {
                        string[] rawVoices = www.text.Split('\n');
                        foreach (string rawVoice in rawVoices)
                        {
                            try
                            {
                                if (!string.IsNullOrEmpty(rawVoice))
                                {
                                    string[] newVoice =
                                    {
                                        rawVoice.Split(' ') [0],
                                        rawVoice.Split(' ') [1],
                                        rawVoice.Split(' ') [2]
                                    };
                                    serverVoicesResponse.Add(newVoice);
                                }
                            }
                            catch (System.Exception ex)
                            {
                                Debug.LogWarning("Problem preparing voice: " + rawVoice + " - " + ex);
                            }
                        }

                        cachedVoices.Clear();

                        foreach (string[] voice in serverVoicesResponse)
                        {
                            Model.Voice newVoice = new Model.Voice(voice[0], "MaryTTS voice: " + voice[0], voice[2], "unknown", voice[1]);
                            cachedVoices.Add(newVoice);
                        }

                        if (Util.Constants.DEV_DEBUG)
                        {
                            Debug.Log("Voices read: " + cachedVoices.CTDump());
                        }

                        //onVoicesReady();
                    }
                    else
                    {
                        string errorMessage = "Could not get the voices: " + www.error;

                        Debug.LogError(errorMessage);
                        onErrorInfo(null, errorMessage);
                    }
                }

                onVoicesReady();
            }
        }
Example #13
0
        /// <summary>Speaks a text with an optional index.</summary>
        /// <param name="index">Index of the text (default: -1 (random), optional).</param>
        /// <returns>UID of the speaker.</returns>
        public string SpeakText(int index = -1)
        {
            string[] texts = new string[TextFiles.Length];

            string file_path = Application.persistentDataPath + "/ocrtext.txt";

            string result = null;

            for (int ii = 0; ii < TextFiles.Length; ii++)
            {
                if (TextFiles[ii] != null)
                {
                    texts[ii] = TextFiles[ii].text;
                }
                else
                {
                    texts[ii] = string.Empty;
                }
            }

            StreamReader sr = System.IO.File.OpenText(file_path);

            texts[0] = sr.ReadToEnd();

            Debug.Log("text[0]=" + texts[0]);

            Model.Voice voice = Speaker.VoiceForName(RTVoiceName);

            if (voice == null)
            {
                voice = Speaker.VoiceForCulture(Culture);
            }

            if (texts.Length > 0)
            {
                if (index < 0)
                {
                    if (Util.Helper.isEditorMode)
                    {
#if UNITY_EDITOR
                        Speaker.SpeakNativeInEditor(texts[rnd.Next(texts.Length)], voice, Rate, Pitch, Volume);
#endif
                    }
                    else
                    {
                        if (Mode == Model.Enum.SpeakMode.Speak)
                        {
                            result = Speaker.Speak(texts[rnd.Next(texts.Length)], Source, voice, true, Rate, Pitch, Volume);
                        }
                        else
                        {
                            result = Speaker.SpeakNative(texts[rnd.Next(texts.Length)], voice, Rate, Pitch, Volume);
                        }
                    }
                }
                else
                {
                    if (index < texts.Length)
                    {
                        if (Util.Helper.isEditorMode)
                        {
#if UNITY_EDITOR
                            Speaker.SpeakNativeInEditor(texts[index], voice, Rate, Pitch, Volume);
#endif
                        }
                        else
                        {
                            if (Mode == Model.Enum.SpeakMode.Speak)
                            {
                                result = Speaker.Speak(texts[index], Source, voice, true, Rate, Pitch, Volume);
                            }
                            else
                            {
                                result = Speaker.SpeakNative(texts[index], voice, Rate, Pitch, Volume);
                            }
                        }
                    }
                    else
                    {
                        Debug.LogWarning("Text file index is out of bounds: " + index + " - maximal index is: " + (texts.Length - 1));
                    }
                }
            }
            else
            {
                Debug.LogError("No text files added - speak cancelled!");
            }

            return(result);
        }
Example #14
0
        /// <summary>Speaks a text with an optional index.</summary>
        /// <param name="index">Index of the text (default: -1 (random), optional).</param>
        /// <returns>UID of the speaker.</returns>
        public string SpeakText(int index = -1)
        {
            string[] texts  = new string[TextFiles.Length];
            string   result = null;

            for (int ii = 0; ii < TextFiles.Length; ii++)
            {
                if (TextFiles[ii] != null)
                {
                    texts[ii] = TextFiles[ii].text;
                }
                else
                {
                    texts[ii] = string.Empty;
                }
            }

            Model.Voice voice = Speaker.VoiceForName(RTVoiceName);

            if (voice == null)
            {
                voice = Speaker.VoiceForCulture(Culture);
            }

            if (texts.Length > 0)
            {
                if (index < 0)
                {
                    if (Util.Helper.isEditorMode)
                    {
#if UNITY_EDITOR
                        Speaker.SpeakNativeInEditor(texts[rnd.Next(texts.Length)], voice, Rate, Pitch, Volume);
#endif
                    }
                    else
                    {
                        if (Mode == Model.Enum.SpeakMode.Speak)
                        {
                            result = Speaker.Speak(texts[rnd.Next(texts.Length)], Source, voice, true, Rate, Pitch, Volume);
                        }
                        else
                        {
                            result = Speaker.SpeakNative(texts[rnd.Next(texts.Length)], voice, Rate, Pitch, Volume);
                        }
                    }
                }
                else
                {
                    if (index < texts.Length)
                    {
                        if (Util.Helper.isEditorMode)
                        {
#if UNITY_EDITOR
                            Speaker.SpeakNativeInEditor(texts[index], voice, Rate, Pitch, Volume);
#endif
                        }
                        else
                        {
                            if (Mode == Model.Enum.SpeakMode.Speak)
                            {
                                result = Speaker.Speak(texts[index], Source, voice, true, Rate, Pitch, Volume);
                            }
                            else
                            {
                                result = Speaker.SpeakNative(texts[index], voice, Rate, Pitch, Volume);
                            }
                        }
                    }
                    else
                    {
                        Debug.LogWarning("Text file index is out of bounds: " + index + " - maximal index is: " + (texts.Length - 1));
                    }
                }
            }
            else
            {
                Debug.LogError("No text files added - speak cancelled!");
            }

            return(result);
        }