Skip to content

Commit f496558

Browse files
author
Jared Bienz
committed
Added dropdown for voice selection to TextToSpeechManager.
1 parent a0b6193 commit f496558

File tree

2 files changed

+75
-283
lines changed

2 files changed

+75
-283
lines changed

Assets/HoloToolkit/Utilities/Scripts/TextToSpeechManager.cs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,38 @@
77
using Windows.Foundation;
88
using Windows.Media.SpeechSynthesis;
99
using Windows.Storage.Streams;
10+
using System.Linq;
1011
using System.Threading.Tasks;
1112
#endif
1213

1314
namespace 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

Comments
 (0)