forked from ericlaw1979/babysmash
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathSpeech.cs
76 lines (73 loc) · 2.44 KB
/
Speech.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
using BabySmash.Properties;
using System;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Speech.Synthesis;
using System.Threading;
using System.Windows.Forms;
namespace BabySmash
{
public static class Speech
{
public static void Speak(string s)
{
if (Settings.Default.Sounds != "None")
{
ThreadedSpeak ts = new ThreadedSpeak(s);
ts.Speak();
}
}
private class ThreadedSpeak
{
private string Word = null;
SpeechSynthesizer SpeechSynth = new SpeechSynthesizer();
public ThreadedSpeak(string Word)
{
this.Word = Word;
CultureInfo keyboardLanguage = InputLanguage.CurrentInputLanguage.Culture;
InstalledVoice neededVoice = this.SpeechSynth.GetInstalledVoices(keyboardLanguage).FirstOrDefault();
if (neededVoice == null)
{
//http://superuser.com/questions/590779/how-to-install-more-voices-to-windows-speech
//https://msdn.microsoft.com/en-us/library/windows.media.speechsynthesis.speechsynthesizer.voice.aspx
//http://stackoverflow.com/questions/34776593/speechsynthesizer-selectvoice-fails-with-no-matching-voice-is-installed-or-th
this.Word = "Unsupported Language";
}
else if (!neededVoice.Enabled)
{
this.Word = "Voice Disabled";
}
else
{
try
{
this.SpeechSynth.SelectVoice(neededVoice.VoiceInfo.Name);
}
catch (Exception ex)
{
Debug.Assert(false, ex.ToString());
}
}
SpeechSynth.Rate = -1;
SpeechSynth.Volume = 100;
}
public void Speak()
{
Thread oThread = new Thread(new ThreadStart(this.Start));
oThread.Start();
}
private void Start()
{
try
{
SpeechSynth.Speak(Word);
}
catch (Exception e)
{
Trace.WriteLine(e.ToString());
}
}
}
}
}