void ChangeVoice(TextToSpeechVoice voice) { // Change voice? if (voice != TextToSpeechVoice.Default) { // Get name var voiceName = Enum.GetName(typeof(TextToSpeechVoice), voice); // See if it's never been found or is changing if ((voiceInfo == null) || (!voiceInfo.DisplayName.Contains(voiceName))) { // Search for voice info voiceInfo = SpeechSynthesizer.AllVoices.Where(v => v.DisplayName.Contains(voiceName)).FirstOrDefault(); // If found, select if (voiceInfo != null) { synthesizer.Voice = voiceInfo; } else { Debug.LogErrorFormat("TTS voice {0} could not be found.", voiceName); } } } }
/// <summary> /// Executes a function that generates a speech stream and then converts and plays it in Unity. /// </summary> /// <param name="text"> /// A raw text version of what's being spoken for use in debug messages when speech isn't supported. /// </param> /// <param name="speakFunc"> /// The actual function that will be executed to generate speech. /// </param> void PlaySpeech( string text, TextToSpeechVoice voice, Func <string, IAsyncOperation <SpeechSynthesisStream> > speakFunc) { // Make sure there's something to speak if (speakFunc == null) { throw new ArgumentNullException(nameof(speakFunc)); } if (synthesizer != null) { base.AddWorkItem( new SpeechEntry() { Text = text, Voice = voice, SpeechGenerator = speakFunc } ); } else { Debug.LogErrorFormat("Speech not initialized. \"{0}\"", text); } }
private void SpeekText(TextToSpeechVoice voice, string text) { TextToSpeech.Voice = voice; TextToSpeech.StartSpeaking(text); }
public Task SayText(string text, TextToSpeechVoice voice, SpeechRate rate, SpeechVolume volume) { PromptStyle style = new PromptStyle(); switch (rate) { case SpeechRate.ExtraFast: style.Rate = PromptRate.ExtraFast; break; case SpeechRate.Fast: style.Rate = PromptRate.Fast; break; case SpeechRate.Medium: style.Rate = PromptRate.Medium; break; case SpeechRate.Slow: style.Rate = PromptRate.Slow; break; case SpeechRate.ExtraSlow: style.Rate = PromptRate.ExtraSlow; break; } switch (volume) { case SpeechVolume.Default: style.Volume = PromptVolume.Default; break; case SpeechVolume.ExtraLoud: style.Volume = PromptVolume.ExtraLoud; break; case SpeechVolume.Loud: style.Volume = PromptVolume.Loud; break; case SpeechVolume.Medium: style.Volume = PromptVolume.Medium; break; case SpeechVolume.Soft: style.Volume = PromptVolume.Soft; break; case SpeechVolume.ExtraSoft: style.Volume = PromptVolume.ExtraSoft; break; } PromptBuilder prompt = new PromptBuilder(); prompt.StartStyle(style); prompt.AppendText(text); prompt.EndStyle(); Task.Run(async() => { try { using (SpeechSynthesizer synthesizer = new SpeechSynthesizer()) { if (voice == null) { voice = this.GetInstalledVoices().FirstOrDefault(); } if (voice != null) { synthesizer.SelectVoice(voice.Name); } Prompt promptAsync = synthesizer.SpeakAsync(prompt); while (!promptAsync.IsCompleted) { await Task.Delay(1000); } } } catch (Exception ex) { Logger.Log(ex); } }); return(Task.FromResult(0)); }