using UnityEngine; namespace Crosstales.RTVoice { /// Wrapper of the main component from RT-Voice for MonoBehaviour-access (like "SendMessage"). [ExecuteInEditMode] [DisallowMultipleComponent] [HelpURL("https://www.crosstales.com/media/data/assets/rtvoice/api/class_crosstales_1_1_r_t_voice_1_1_live_speaker.html")] public class LiveSpeaker : MonoBehaviour { private static readonly char[] splitChar = {';'}; #region Public methods /// Speaks a text with a given wrapper -> native mode. /// Wrapper with the speech details. public void SpeakNativeLive(Model.Wrapper wrapper) { Speaker.Instance.SpeakNative(wrapper); } /// Speaks a text with a given array of arguments (native mode). /// Argument string delimited by ';': 0 = text, 1 = culture (optional), 2 = voiceName (optional), 3 = rate (optional), 4 = volume (optional), 5 = pitch (optional). public void SpeakNativeLive(string args) { if (!string.IsNullOrEmpty(args)) { SpeakNativeLive(args.Split(splitChar, System.StringSplitOptions.RemoveEmptyEntries)); } else { Debug.LogWarning("'args' is null or empty!", this); } } /// Speaks a text with a given array of arguments (native mode). /// Argument index: 0 = text, 1 = culture (optional), 2 = voiceName (optional), 3 = rate (optional), 4 = pitch (optional), 5 = volume (optional). public void SpeakNativeLive(string[] args) { if (args != null && args.Length >= 1) { string text = args[0]; string culture = null; if (args.Length >= 2) { culture = args[1]; } Model.Voice voice = null; if (args.Length >= 3) { voice = Speaker.Instance.VoiceForName(args[2]); } float rate = 1f; if (args.Length >= 4) { if (!float.TryParse(args[3], out rate)) { Debug.LogWarning("Argument 3 (= rate) is not a number: '" + args[3] + "'", this); rate = 1f; } } float pitch = 1f; if (args.Length >= 5) { if (!float.TryParse(args[4], out pitch)) { Debug.LogWarning("Argument 4 (= pitch) is not a number: '" + args[4] + "'", this); pitch = 1f; } } float volume = 1f; if (args.Length >= 6) { if (!float.TryParse(args[5], out volume)) { Debug.LogWarning("Argument 5 (= volume) is not a number: '" + args[5] + "'", this); volume = 1f; } } if (voice == null) { voice = Speaker.Instance.VoiceForCulture(culture); } SpeakNativeLive(new Model.Wrapper(text, voice, rate, pitch, volume, true)); //TODO add ForceSSML as parameter? } else { Debug.LogError("'args' is null or wrong number of arguments given!" + System.Environment.NewLine + "Please verify that you pass a string-array with at least one argument (text).", this); } } /// Speaks a text with a given wrapper. /// Wrapper with the speech details. public void SpeakLive(Model.Wrapper wrapper) { Speaker.Instance.Speak(wrapper); } /// /// Speaks a text with a given array of arguments. /// Important: you can't specify the AudioSource with this method! /// /// Argument string delimited by ';': 0 = text, 1 = culture (optional), 2 = voiceName (optional), 3 = rate (optional), 4 = volume (optional), 5 = pitch (optional). public void SpeakLive(string args) { if (!string.IsNullOrEmpty(args)) { SpeakLive(args.Split(splitChar, System.StringSplitOptions.RemoveEmptyEntries)); } else { Debug.LogWarning("'args' is null or empty!", this); } } /// /// Speaks a text with a given array of arguments. /// Important: you can't specify the AudioSource with this method! /// /// Argument index: 0 = text, 1 = culture (optional), 2 = voiceName (optional), 3 = rate (optional), 4 = pitch (optional), 5 = volume (optional). public void SpeakLive(string[] args) { if (args != null && args.Length >= 1) { string text = args[0]; string culture = null; if (args.Length >= 2) { culture = args[1]; } Model.Voice voice = null; if (args.Length >= 3) { voice = Speaker.Instance.VoiceForName(args[2]); } float rate = 1f; if (args.Length >= 4) { if (!float.TryParse(args[3], out rate)) { Debug.LogWarning("Argument 3 (= rate) is not a number: '" + args[3] + "'", this); rate = 1f; } } float pitch = 1f; if (args.Length >= 5) { if (!float.TryParse(args[4], out pitch)) { Debug.LogWarning("Argument 5 (= pitch) is not a number: '" + args[4] + "'", this); pitch = 1f; } } float volume = 1f; if (args.Length >= 6) { if (!float.TryParse(args[5], out volume)) { Debug.LogWarning("Argument 4 (= volume) is not a number: '" + args[5] + "'", this); volume = 1f; } } if (voice == null) { voice = Speaker.Instance.VoiceForCulture(culture); } SpeakLive(new Model.Wrapper(text, voice, rate, pitch, volume, null)); } else { Debug.LogError("'args' is null or wrong number of arguments given!" + System.Environment.NewLine + "Please verify that you pass a string-array with at least one argument (text).", this); } } /// Silence all active TTS-voices. public void SilenceLive() { Speaker.Instance.Silence(); } #endregion } } // © 2015-2020 crosstales LLC (https://www.crosstales.com)