/
VoiceInfo.cs
288 lines (248 loc) · 8.24 KB
/
VoiceInfo.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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Diagnostics;
using System.Globalization;
using System.Speech.AudioFormat;
using System.Speech.Internal;
using System.Speech.Internal.ObjectTokens;
using System.Speech.Internal.Synthesis;
namespace System.Speech.Synthesis
{
[DebuggerDisplay("{(_name != null ? \"'\" + _name + \"' \" : \"\") + (_culture != null ? \" '\" + _culture.ToString () + \"' \" : \"\") + (_gender != VoiceGender.NotSet ? \" '\" + _gender.ToString () + \"' \" : \"\") + (_age != VoiceAge.NotSet ? \" '\" + _age.ToString () + \"' \" : \"\") + (_variant > 0 ? \" \" + _variant.ToString () : \"\")}")]
[Serializable]
public class VoiceInfo
{
#region Constructors
internal VoiceInfo(string name)
{
Helpers.ThrowIfEmptyOrNull(name, nameof(name));
_name = name;
}
internal VoiceInfo(CultureInfo culture)
{
// Fails if no culture is provided
Helpers.ThrowIfNull(culture, nameof(culture));
if (culture.Equals(CultureInfo.InvariantCulture))
{
throw new ArgumentException(SR.Get(SRID.InvariantCultureInfo), nameof(culture));
}
_culture = culture;
}
internal VoiceInfo(ObjectToken token)
{
_registryKeyPath = token._sKeyId;
// Retrieve the token name
_id = token.Name;
// Retrieve default display name
_description = token.Description;
// Get other attributes
_name = token.TokenName();
SsmlParserHelpers.TryConvertAge(token.Age.ToLowerInvariant(), out _age);
SsmlParserHelpers.TryConvertGender(token.Gender.ToLowerInvariant(), out _gender);
string langId;
if (token.Attributes.TryGetString("Language", out langId))
{
_culture = SapiAttributeParser.GetCultureInfoFromLanguageString(langId);
}
string assemblyName;
if (token.TryGetString("Assembly", out assemblyName))
{
_assemblyName = assemblyName;
}
string clsid;
if (token.TryGetString("CLSID", out clsid))
{
_clsid = clsid;
}
if (token.Attributes != null)
{
// Enum all values and add to custom table
Dictionary<string, string> attrs = new();
foreach (string keyName in token.Attributes.GetValueNames())
{
string attributeValue;
if (token.Attributes.TryGetString(keyName, out attributeValue))
{
attrs.Add(keyName, attributeValue);
}
}
_attributes = new ReadOnlyDictionary<string, string>(attrs);
}
string audioFormats;
if (token.Attributes != null && token.Attributes.TryGetString("AudioFormats", out audioFormats))
{
_audioFormats = new ReadOnlyCollection<SpeechAudioFormatInfo>(SapiAttributeParser.GetAudioFormatsFromString(audioFormats));
}
else
{
_audioFormats = new ReadOnlyCollection<SpeechAudioFormatInfo>(new List<SpeechAudioFormatInfo>());
}
}
internal VoiceInfo(VoiceGender gender)
{
_gender = gender;
}
internal VoiceInfo(VoiceGender gender, VoiceAge age)
{
_gender = gender;
_age = age;
}
internal VoiceInfo(VoiceGender gender, VoiceAge age, int voiceAlternate)
{
if (voiceAlternate < 0)
{
throw new ArgumentOutOfRangeException(nameof(voiceAlternate), SR.Get(SRID.PromptBuilderInvalidVariant));
}
_gender = gender;
_age = age;
_variant = voiceAlternate + 1;
}
#endregion
#region public Methods
/// <summary>
/// Tests whether two AutomationIdentifier objects are equivalent
/// </summary>
public override bool Equals(object obj)
{
#pragma warning disable 6506
VoiceInfo voice = obj as VoiceInfo;
return voice != null
&& _name == voice._name
&& (_age == voice._age || _age == VoiceAge.NotSet || voice._age == VoiceAge.NotSet)
&& (_gender == voice._gender || _gender == VoiceGender.NotSet || voice._gender == VoiceGender.NotSet)
&& (_culture == null || voice._culture == null || _culture.Equals(voice._culture));
#pragma warning restore 6506
}
/// <summary>
/// Overrides Object.GetHashCode()
/// </summary>
public override int GetHashCode()
{
return _name.GetHashCode();
}
#endregion
#region public Properties
public VoiceGender Gender
{
get
{
return _gender;
}
}
public VoiceAge Age
{
get
{
return _age;
}
}
public string Name
{
get
{
return _name;
}
}
/// <summary>
///
/// Return a copy of the internal Language set. This disable client
/// applications to modify the internal languages list.
/// </summary>
public CultureInfo Culture
{
get
{
return _culture;
}
}
public string Id
{
get
{
return _id;
}
}
public string Description
{
get
{
return _description ?? string.Empty;
}
}
[EditorBrowsable(EditorBrowsableState.Advanced)]
public ReadOnlyCollection<SpeechAudioFormatInfo> SupportedAudioFormats
{
get
{
return _audioFormats;
}
}
[EditorBrowsable(EditorBrowsableState.Advanced)]
public IDictionary<string, string> AdditionalInfo => _attributes ??= new ReadOnlyDictionary<string, string>(new Dictionary<string, string>(0));
#endregion
#region Internal Methods
internal static bool ValidateGender(VoiceGender gender)
{
return gender == VoiceGender.Female || gender == VoiceGender.Male || gender == VoiceGender.Neutral || gender == VoiceGender.NotSet;
}
internal static bool ValidateAge(VoiceAge age)
{
return age == VoiceAge.Adult || age == VoiceAge.Child || age == VoiceAge.NotSet || age == VoiceAge.Senior || age == VoiceAge.Teen;
}
#endregion
#region Internal Property
internal int Variant
{
get
{
return _variant;
}
}
internal string AssemblyName
{
get
{
return _assemblyName;
}
}
internal string Clsid
{
get
{
return _clsid;
}
}
internal string RegistryKeyPath
{
get
{
return _registryKeyPath;
}
}
#endregion
#region Private Fields
private string _name;
private CultureInfo _culture;
private VoiceGender _gender;
private VoiceAge _age;
private int _variant = -1;
[NonSerialized]
private string _id;
[NonSerialized]
private string _registryKeyPath;
[NonSerialized]
private string _assemblyName;
[NonSerialized]
private string _clsid;
[NonSerialized]
private string _description;
[NonSerialized]
private ReadOnlyDictionary<string, string> _attributes;
[NonSerialized]
private ReadOnlyCollection<SpeechAudioFormatInfo> _audioFormats;
#endregion
}
}