/
MainPage.xaml.cs
352 lines (304 loc) · 14 KB
/
MainPage.xaml.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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using Microsoft.Phone.Controls;
using Windows.ApplicationModel;
using Windows.Phone.Speech.Recognition;
using Windows.Phone.Speech.Synthesis;
using Windows.Phone.Speech.VoiceCommands;
namespace Speech
{
public partial class MainPage : PhoneApplicationPage
{
// Start SpeechSynthesizer
private SpeechSynthesizer _speechSynthesizer = new SpeechSynthesizer();
// Get all available voices
private IEnumerable<VoiceInformation> _availableVoices = InstalledVoices.All.ToList();
// Get installed languages from available voices
private IEnumerable<CultureInfo> InstalledLanguages
{
get
{
return _availableVoices.Select(voice => new CultureInfo(voice.Language)).Distinct().ToList();
}
}
// Constructor
public MainPage()
{
InitializeComponent();
_speechSynthesizer.BookmarkReached += _speechSynthesizer_BookmarkReached;
// Set the DataContext of the Languages ListBox for data binding
LanguagesListBox.DataContext = InstalledLanguages;
RegisterVoiceCommands();
}
void _speechSynthesizer_BookmarkReached(SpeechSynthesizer sender, SpeechBookmarkReachedEventArgs args)
{
Dispatcher.BeginInvoke(() => MessageBox.Show(args.Bookmark + " Reached"));
}
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
base.OnNavigatedTo(e);
// Set the selectedIndex to trigger the SelectionChanged event
LanguagesListBox.SelectedIndex = 0;
// Check if launched by a voice command
bool containsKey = NavigationContext.QueryString.ContainsKey("voiceCommandName");
if (containsKey)
{
string s = NavigationContext.QueryString["voiceCommandName"];
switch (s)
{
case "SetVoiceCommand":
string action = string.Empty;
string voice = NavigationContext.QueryString["voice"];
if (NavigationContext.QueryString.ContainsKey("action"))
{
action = NavigationContext.QueryString["action"];
}
SetVoiceFromCommand(voice, action);
break;
default:
MessageBox.Show("uh oh");
break;
}
}
}
private async void RegisterVoiceCommands()
{
await VoiceCommandService.InstallCommandSetsFromFileAsync(new Uri("ms-appx:///VoiceCommands.xml", UriKind.Absolute));
UpdatePhraseList("EnglishCommands", "voice");
MessageBox.Show("Voices PhraseList Updated.");
}
private async void UpdatePhraseList(string commandSetName, string phraseListName)
{
// Create List to hold the voice names
List<string> voiceNames = new List<string>();
// Loop through the installed voices
foreach (VoiceInformation voice in _availableVoices)
{
// Take the DisplayName of the voice and split it at
// the spaces. Take the second item in the results
// and assign it to a new string.
string voiceName = voice.DisplayName.Split(' ')[1];
// add voiceName to the list of VoiceNames
voiceNames.Add(voiceName);
}
// lookup the CommandSet we want to update
VoiceCommandSet commandSet = VoiceCommandService.InstalledCommandSets[commandSetName];
// Update commandSet with the new phrase list.
await commandSet.UpdatePhraseListAsync(phraseListName, voiceNames);
}
private async void SetVoiceFromCommand(string voice, string action)
{
foreach (var voiceInformation in _availableVoices)
{
if (voiceInformation.DisplayName.Split(' ')[1].ToLower() == voice.ToLower())
{
var voiceInfo = voiceInformation;
var language = InstalledLanguages.Single(l => l.Name == voiceInfo.Language);
LanguagesListBox.SelectedItem = language;
VoicesListBox.SelectedItem = voiceInfo;
await _speechSynthesizer.SpeakTextAsync("Voice set to " + voice);
if (action == "read")
{
await SayText();
}
break;
}
}
}
private async void SayItButtonClick(object sender, RoutedEventArgs e)
{
await SayText();
}
private async Task SayText()
{
try // error handling
{
// Disable the ListBoxes to prevent exceptions
VoicesListBox.IsEnabled = false;
LanguagesListBox.IsEnabled = false;
// Stop playing any Synthesized speech
_speechSynthesizer.CancelAll();
// Check that string is not null or empty
if (!string.IsNullOrEmpty(PhraseTextBox.Text))
{
// Call SpeakTextAsync and await
await _speechSynthesizer.SpeakTextAsync(PhraseTextBox.Text);
MessageBox.Show("Text read: '" + PhraseTextBox.Text + "'");
}
else // if string is null or empty, display error message
{
MessageBox.Show("Phrase text is required.",
"Error",
MessageBoxButton.OK);
}
// Re-enable the ListBoxes
VoicesListBox.IsEnabled = true;
LanguagesListBox.IsEnabled = true;
}
catch (Exception ex) // Catch exception and call function to handle it.
{
HandleSpeechSynthesisError(ex);
}
}
private async void SaySsml_Click(object sender, RoutedEventArgs e)
{
try // error handling
{
// Disable the ListBoxes to prevent exceptions
VoicesListBox.IsEnabled = false;
LanguagesListBox.IsEnabled = false;
// Stop playing any Synthesized speech
_speechSynthesizer.CancelAll();
// Check that string is not null or empty
if (!string.IsNullOrEmpty(PhraseTextBox.Text))
{
// Create SSML string
var SsmlString = "<speak version=\"1.0\" xmlns=\"http://www.w3.org/2001/10/synthesis\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xml:lang=\""+ _speechSynthesizer.GetVoice().Language +"\">";
SsmlString += "<s>" + PhraseTextBox.Text + "</s></speak>";
// Call SpeakTextAsync and await
await _speechSynthesizer.SpeakSsmlAsync(SsmlString);
MessageBox.Show("SSML Text read: '" + SsmlString + "'");
}
else // if string is null or empty, display error message
{
MessageBox.Show("Phrase text is required.",
"Error",
MessageBoxButton.OK);
}
// Re-enable the ListBoxes
VoicesListBox.IsEnabled = true;
LanguagesListBox.IsEnabled = true;
}
catch (Exception ex) // Catch exception and call function to handle it.
{
HandleSpeechSynthesisError(ex);
}
}
// Speaks the content of a standalone SSML file.
private async void SpeakSsmlFromFile_Click(object sender, RoutedEventArgs e)
{
try // error handling
{
// Disable the ListBoxes to prevent exceptions
VoicesListBox.IsEnabled = false;
LanguagesListBox.IsEnabled = false;
// Stop playing any Synthesized speech
_speechSynthesizer.CancelAll();
// Set the path to the SSML-compliant XML file.
string path = Package.Current.InstalledLocation.Path + "\\SSMLExample.xml";
Uri fileToRead = new Uri(path, UriKind.Absolute);
// Speak the SSML prompt.
await _speechSynthesizer.SpeakSsmlFromUriAsync(fileToRead);
MessageBox.Show("SSML file read.");
// Re-enable the ListBoxes
VoicesListBox.IsEnabled = true;
LanguagesListBox.IsEnabled = true;
}
catch (Exception ex) // Catch exception and call function to handle it.
{
HandleSpeechSynthesisError(ex);
}
}
private static void HandleSpeechSynthesisError(Exception ex)
{
// convert the HResult code to an int for comparison to returned
const uint AbortedCallHResult = 0x80045508; // SPERR_SYSTEM_CALL_INTERRUPTED
const uint GenericInternalHResult = 0x800455A0; // SPERR_WINRT_INTERNAL_ERROR
const uint AlreadyInLexiconHResult = 0x800455A1; // SPERR_WINRT_ALREADY_IN_LEX
const uint NotInLexiconHResult = 0x800455A2; // SPERR_WINRT_NOT_IN_LEX
const uint UnsupportedPhonemeHResult = 0x800455B5; // SPERR_WINRT_UNSUPPORTED_PHONEME
const uint PhonemeConversionHResult = 0x800455B6; // SPERR_WINRT_PHONEME_CONVERSION
const uint InvalidLexiconHResult = 0x800455B8; // SPERR_WINRT_LEX_INVALID_DATA
const uint UnsupportedLanguageHResult = 0x800455BC; // SPERR_WINRT_UNSUPPORTED_LANG
const uint StringTooLongHResult = 0x800455BD; // SPERR_WINRT_STRING_TOO_LONG
const uint StringEmptyHResult = 0x800455BE; // SPERR_WINRT_STRING_EMPTY
const uint NoMoreItemsHResult = 0x800455BF; // SPERR_WINRT_NO_MORE_ITEMS
if ((uint) ex.HResult == AbortedCallHResult)
{
MessageBox.Show(ex.Message, "SPERR_SYSTEM_CALL_INTERRUPTED", MessageBoxButton.OK);
}
else if ((uint) ex.HResult == GenericInternalHResult)
{
MessageBox.Show(ex.Message, "SPERR_WINRT_INTERNAL_ERROR", MessageBoxButton.OK);
}
else if ((uint) ex.HResult == AlreadyInLexiconHResult)
{
MessageBox.Show(ex.Message, "SPERR_WINRT_ALREADY_IN_LEX", MessageBoxButton.OK);
}
else if ((uint) ex.HResult == NotInLexiconHResult)
{
MessageBox.Show(ex.Message, "SPERR_WINRT_NOT_IN_LEX", MessageBoxButton.OK);
}
else if ((uint) ex.HResult == UnsupportedPhonemeHResult)
{
MessageBox.Show(ex.Message, "SPERR_WINRT_UNSUPPORTED_PHONEME", MessageBoxButton.OK);
}
else if ((uint) ex.HResult == PhonemeConversionHResult)
{
MessageBox.Show(ex.Message, "SPERR_WINRT_PHONEME_CONVERSION", MessageBoxButton.OK);
}
else if ((uint) ex.HResult == InvalidLexiconHResult)
{
MessageBox.Show(ex.Message, "SPERR_WINRT_LEX_INVALID_DATA", MessageBoxButton.OK);
}
else if ((uint) ex.HResult == UnsupportedLanguageHResult)
{
MessageBox.Show(ex.Message, "SPERR_WINRT_UNSUPPORTED_LANG", MessageBoxButton.OK);
}
else if ((uint) ex.HResult == StringTooLongHResult)
{
MessageBox.Show(ex.Message, "SPERR_WINRT_STRING_TOO_LONG", MessageBoxButton.OK);
}
else if ((uint) ex.HResult == StringEmptyHResult)
{
MessageBox.Show(ex.Message, "SPERR_WINRT_STRING_EMPTY", MessageBoxButton.OK);
}
else if ((uint) ex.HResult == NoMoreItemsHResult)
{
MessageBox.Show(ex.Message, "SPERR_WINRT_NO_MORE_ITEMS", MessageBoxButton.OK);
}
else
{
MessageBox.Show(ex.Message, ex.GetType().ToString(), MessageBoxButton.OK);
}
}
#region Language and Voice
private void LanguagesListBox_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
{
// First we cast the sender from an object to a then cast its
// selected item to CultureInfo then update the Voices ListBox.
UpdateVoicesListBox((CultureInfo)((ListBox)sender).SelectedItem);
}
private void UpdateVoicesListBox(CultureInfo info)
{
// Get only the voices that match then set the datacontext for databinding.
VoicesListBox.DataContext = _availableVoices.Where(v => v.Language == info.Name).OrderBy(v => v.Gender);
// Set voice to first in list
SetVoice(VoicesListBox.Items[0]);
}
private void VoicesListBox_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
{
SetVoice(sender);
}
private void SetVoice(object sender)
{
// Determine if sender is VoiceInformation
if (sender is VoiceInformation)
{
_speechSynthesizer.SetVoice((VoiceInformation)sender);
}
else
{ // If the sender is NOT VoiceInformation, it is a ListBox and cast its SelectedItem to VoiceInformation
_speechSynthesizer.SetVoice((VoiceInformation)((ListBox)sender).SelectedItem);
}
}
#endregion Language and Voice
}
}