-
Notifications
You must be signed in to change notification settings - Fork 7
/
SpeechSynthesis.cs
246 lines (219 loc) · 7.83 KB
/
SpeechSynthesis.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
// .NET speech synthesis library for VBScript
// Requires an assembly reference to C:\Windows\Microsoft.NET\Framework\v4.0.30319\WPF\System.Speech.dll or the equivalent.
using System.Speech.Synthesis;
using System.Runtime.InteropServices;
using System.Linq; // for Cast<object>
using System.Collections.Generic; // for List
using System.Windows.Forms; // for MessageBox
namespace VBScripting
{
/// <summary> Provide a wrapper for the .NET speech synthesizer for VBScript, for demonstration purposes. </summary>
/// <remarks> Requires an assembly reference to <code>C:\Windows\Microsoft.NET\Framework\v4.0.30319\WPF\System.Speech.dll</code> or the equivalent. </remarks>
[Guid("2650C2AB-2AF8-495F-AB4D-6C61BD463EA4")]
[ClassInterface(ClassInterfaceType.None)]
[ProgId( "VBScripting.SpeechSynthesis" )]
public class SpeechSynthesis : ISpeechSynthesis
{
private SpeechSynthesizer ss;
private List<string> installedVoices;
private List<string> voices; // installed & enabled
/// <summary> Constructor </summary>
public SpeechSynthesis()
{
this.ss = new SpeechSynthesizer();
this.voices = new List<string>(); // installed, enabled voices
this.installedVoices = new List<string>(); // installed voices
string name;
foreach (InstalledVoice voice in ss.GetInstalledVoices())
{
name = voice.VoiceInfo.Name;
this.installedVoices.Add(name);
if (voice.Enabled)
{
this.voices.Add(name);
}
}
}
/// <summary> Convert text to speech. </summary>
/// <remarks> This method is synchronous. </remarks>
public void Speak(string text)
{
if (!string.IsNullOrWhiteSpace(text))
{
this.ss.Speak(text);
}
}
/// <summary> Convert text to speech. </summary>
/// <remarks> This method is asynchronous. </remarks>
public void SpeakAsync(string text)
{
if (!string.IsNullOrWhiteSpace(text))
{
this.ss.SpeakAsync(text);
}
}
/// <summary> Pause speech synthesis. </summary>
public void Pause()
{
if (System.Speech.Synthesis.SynthesizerState.Speaking == this.ss.State)
{
this.ss.Pause();
}
}
/// <summary> Resume speech synthesis. </summary>
public void Resume()
{
if (System.Speech.Synthesis.SynthesizerState.Paused == this.ss.State)
{
this.ss.Resume();
}
}
/// <summary> Gets an array of the names of the installed, enabled voices. </summary>
/// <remarks> Each element of the array can be used to set <code>Voice</code>.</remarks>
public object Voices()
{
return this.voices.Cast<object>().ToArray(); // convert to VBScript array
}
/// <summary> Gets or sets the current voice by name. </summary>
/// <remarks> A string. One of the names from the <code>Voices</code> array. </remarks>
public string Voice
{
set
{
if (this.voices.Contains(value))
{
this.ss.SelectVoice(value);
}
else if (this.installedVoices.Contains(value))
{
string msg = string.Format(
"\"{0}\" is an installed voice but is not enabled.", value);
ShowInfoMessage(msg);
}
else
{
string msg = string.Format(
"\"{0}\" is not an installed voice.", value);
ShowInfoMessage(msg);
}
}
get
{
return this.ss.Voice.Name;
}
}
// Shows a message box with the specified string
private void ShowInfoMessage(string msg)
{
MessageBox.Show(msg,
"SpeechSynthesis class",
MessageBoxButtons.OK,
MessageBoxIcon.Information);
}
/// <summary> Disposes the SpeechSynthesis object's resources. </summary>
public void Dispose()
{
if (this.ss != null)
{
this.ss.Dispose();
}
}
/// <summary> Gets the state of the SpeechSynthesizer. </summary>
/// <remarks> Read only. Returns an integer equal to one of the <code>State</code> method return values. </remarks>
public int SynthesizerState
{
get
{
if (System.Speech.Synthesis.SynthesizerState.Ready == this.ss.State)
{
return 1;
}
else if (System.Speech.Synthesis.SynthesizerState.Paused == this.ss.State)
{
return 2;
}
else if (System.Speech.Synthesis.SynthesizerState.Speaking == this.ss.State)
{
return 3;
}
else
{
return 4;
}
}
private set { }
}
/// <summary> Gets or sets the volume. </summary>
/// <remarks> An integer from 0 to 100. </remarks>
public int Volume
{
get
{
return this.ss.Volume;
}
set
{
this.ss.Volume = value;
}
}
/// <summary> Gets an object whose properties (Ready, Paused, and Speaking) provide values useful for comparing to <code>SynthesizerState</code>. </summary>
/// <returns> a SynthersizerStateT </returns>
public SynthesizerStateT State
{
get { return new SynthesizerStateT(); }
private set { }
}
}
/// <summary> Enumerates the synthesizer states. </summary>
/// <remarks> Not intended for use in VBScript. See <code>SpeechSynthesis.State</code>. </remarks>
[Guid("2650C2AB-2DF8-495F-AB4D-6C61BD463EA4")]
public class SynthesizerStateT
{
/// <summary> Constructor </summary>
public SynthesizerStateT() { }
/// <returns> 1 </returns>
public int Ready { get { return 1; } private set { } }
/// <returns> 2 </returns>
public int Paused { get { return 2; } private set { } }
/// <returns> 3 </returns>
public int Speaking { get { return 3; } private set { } }
/// <returns> 4 </returns>
public int Unexpected { get { return 4; } private set { } }
}
/// <summary> The COM interface for <code>VBScripting.SpeechSynthesis</code> </summary>
[Guid("2650C2AB-2BF8-495F-AB4D-6C61BD463EA4")]
[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
public interface ISpeechSynthesis
{
/// <summary> </summary>
[DispId(1)]
void Speak(string text);
/// <summary> </summary>
[DispId(2)]
void SpeakAsync(string text);
/// <summary> </summary>
[DispId(3)]
void Pause();
/// <summary> </summary>
[DispId(4)]
void Resume();
/// <summary> </summary>
[DispId(5)]
object Voices(); // array
/// <summary> </summary>
[DispId(6)]
string Voice { get; set; }
/// <summary> </summary>
[DispId(7)]
void Dispose();
/// <summary> </summary>
[DispId(8)]
int SynthesizerState { get; }
/// <summary> </summary>
[DispId(9)]
SynthesizerStateT State { get; }
/// <summary> </summary>
[DispId(13)]
int Volume { get; set; }
}
}