77using Windows . Foundation ;
88using Windows . Media . SpeechSynthesis ;
99using Windows . Storage . Streams ;
10+ using System . Linq ;
1011using System . Threading . Tasks ;
1112#endif
1213
1314namespace HoloToolkit . Unity
1415{
16+ /// <summary>
17+ /// The well-know voices that can be used by <see cref="TextToSpeechManager"/>.
18+ /// </summary>
19+ public enum TextToSpeechVoice
20+ {
21+ /// <summary>
22+ /// The default system voice.
23+ /// </summary>
24+ Default ,
25+
26+ /// <summary>
27+ /// Microsoft David Mobile
28+ /// </summary>
29+ David ,
30+
31+ /// <summary>
32+ /// Microsoft Mark Mobile
33+ /// </summary>
34+ Mark ,
35+
36+ /// <summary>
37+ /// Microsoft Zira Mobile
38+ /// </summary>
39+ Zira ,
40+ }
41+
1542 /// <summary>
1643 /// Enables text to speech using the Windows 10 <see cref="SpeechSynthesizer"/> class.
1744 /// </summary>
@@ -29,9 +56,13 @@ public class TextToSpeechManager : MonoBehaviour
2956 [ Tooltip ( "The audio source where speech will be played." ) ]
3057 public AudioSource audioSource ;
3158
59+ [ Tooltip ( "The voice that will be used to generate speech." ) ]
60+ public TextToSpeechVoice voice ;
61+
3262 // Member Variables
3363 #if WINDOWS_UWP
3464 private SpeechSynthesizer synthesizer ;
65+ private VoiceInformation voiceInfo ;
3566 #endif
3667
3768 // Static Helper Methods
@@ -205,6 +236,30 @@ private void PlaySpeech(string text, Func<IAsyncOperation<SpeechSynthesisStream>
205236 // This is good since it frees up Unity to keep running anyway.
206237 Task . Run ( async ( ) =>
207238 {
239+ // Change voice?
240+ if ( voice != TextToSpeechVoice . Default )
241+ {
242+ // Get name
243+ var voiceName = Enum . GetName ( typeof ( TextToSpeechVoice ) , voice ) ;
244+
245+ // See if it's never been found or is changing
246+ if ( ( voiceInfo == null ) || ( ! voiceInfo . DisplayName . Contains ( voiceName ) ) )
247+ {
248+ // Search for voice info
249+ voiceInfo = SpeechSynthesizer . AllVoices . Where ( v => v . DisplayName . Contains ( voiceName ) ) . FirstOrDefault ( ) ;
250+
251+ // If found, select
252+ if ( voiceInfo != null )
253+ {
254+ synthesizer . Voice = voiceInfo ;
255+ }
256+ else
257+ {
258+ Debug . LogErrorFormat ( "TTS voice {0} could not be found." , voiceName ) ;
259+ }
260+ }
261+ }
262+
208263 // Speak and get stream
209264 var speechStream = await speakFunc ( ) ;
210265
0 commit comments