This repository has been archived by the owner on Feb 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 575
/
NeuralTTS.xaml.cs
253 lines (223 loc) · 10.4 KB
/
NeuralTTS.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
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license.
//
// Microsoft Cognitive Services: http://www.microsoft.com/cognitive
//
// Microsoft Cognitive Services Github:
// https://github.com/Microsoft/Cognitive
//
// Copyright (c) Microsoft Corporation
// All rights reserved.
//
// MIT License:
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED ""AS IS"", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using Newtonsoft.Json;
using System;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Net;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Windows.Media.Core;
using Windows.Storage;
using Windows.UI.Popups;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;
namespace IntelligentKioskSample.Views.NeuralTTS
{
[KioskExperience(Id = "NeuralTTS",
DisplayName = "Neural Text To Speech",
Description = "Hear a male or female voice speak any text you provide",
ImagePath = "ms-appx:/Assets/DemoGallery/Neural Text To Speech.jpg",
ExperienceType = ExperienceType.Guided | ExperienceType.Business,
TechnologiesUsed = TechnologyType.TextToSpeech,
TechnologyArea = TechnologyAreaType.Speech,
DateAdded = "2021/06/15")]
public sealed partial class NeuralTTSPage : Page
{
private const int MaxTimeout = 6000000;
private const string SSMLUserAgent = "NeuralTTSClient";
private const string SSMLContentType = "application/ssml+xml";
private const string AudioOutputFormatName = "X-MICROSOFT-OutputFormat";
private const string AudioOutputFormatValue = "riff-16khz-16bit-mono-pcm";
private const string EndpointTemplate = "https://{0}.tts.speech.microsoft.com/cognitiveservices/v1";
private const string EndpointPattern = "wss://(.*).stt.speech.microsoft.com/speech/recognition/conversation/cognitiveservices/v1";
private const string SSMLTemplate = "<speak version='1.0' xmlns='http://www.w3.org/2001/10/synthesis' xml:lang='{0}'><voice name='{1}'>{2}</voice></speak>";
private string apiRegion = string.Empty;
private StorageFolder cacheDataFolder;
/// <summary>
/// NOTE: Neural voices
/// https://docs.microsoft.com/en-us/azure/cognitive-services/speech-service/language-support#neural-voices
/// </summary>
public ObservableCollection<VoiceInfo> AvailableVoices { get; } = new ObservableCollection<VoiceInfo>();
public ObservableCollection<CachedResult> CachedResults { get; } = new ObservableCollection<CachedResult>();
public NeuralTTSPage()
{
this.InitializeComponent();
}
protected override async void OnNavigatedTo(NavigationEventArgs e)
{
// check Speech API keys
if (!string.IsNullOrEmpty(SettingsHelper.Instance.SpeechApiKey) && !string.IsNullOrEmpty(SettingsHelper.Instance.SpeechApiEndpoint))
{
this.speakButton.IsEnabled = true;
this.apiRegion = Regex.Replace(SettingsHelper.Instance.SpeechApiEndpoint, EndpointPattern, "$1");
this.cacheDataFolder = await ApplicationData.Current.LocalFolder.CreateFolderAsync("NeuralTTSDemo\\Cache", CreationCollisionOption.OpenIfExists);
AvailableVoices.AddRange(NeuralTTSDataLoader.GetNeuralVoices());
CachedResults.AddRange(await NeuralTTSDataLoader.GetCachedResults(cacheDataFolder));
}
else
{
this.speakButton.IsEnabled = false;
ContentDialog missingApiKeyDialog = new ContentDialog
{
Title = "Missing Speech API Key",
Content = "Please enter a valid Speech API key in the Settings Page.",
PrimaryButtonText = "Open Settings",
CloseButtonText = "Close",
DefaultButton = ContentDialogButton.Primary
};
ContentDialogResult result = await missingApiKeyDialog.ShowAsync();
if (result == ContentDialogResult.Primary)
{
AppShell.Current.NavigateToPage(typeof(SettingsPage));
}
}
base.OnNavigatedTo(e);
}
private async void SpeakButtonClicked(object sender, RoutedEventArgs e)
{
if (string.IsNullOrEmpty(this.inputTextBox.Text) || voiceComboBox.SelectedValue == null)
{
await new MessageDialog("Please type some text and select a voice first.").ShowAsync();
return;
}
try
{
this.progressRing.IsActive = true;
await Speak(this.inputTextBox.Text, voiceComboBox.SelectedValue as VoiceInfo);
}
catch (Exception ex)
{
await Util.GenericApiCallExceptionHandler(ex, "Error processing your request.");
}
finally
{
this.progressRing.IsActive = false;
}
}
private async Task Speak(string text, VoiceInfo voice)
{
Authentication authentication = new Authentication(SettingsHelper.Instance.SpeechApiKey, this.apiRegion);
string tokenString = await authentication.RetrieveNewTokenAsync();
string voiceUrl = string.Format(EndpointTemplate, this.apiRegion);
WebRequest webRequest = WebRequest.Create(voiceUrl);
webRequest.ContentType = SSMLContentType;
webRequest.Headers.Add(AudioOutputFormatName, AudioOutputFormatValue);
webRequest.Headers.Add("User-Agent", SSMLUserAgent);
webRequest.Headers["Authorization"] = "Bearer " + tokenString;
webRequest.Timeout = MaxTimeout;
webRequest.Method = "POST";
string ssml = string.Format(SSMLTemplate, voice.Locale, voice.Name, text);
byte[] btBodys = Encoding.UTF8.GetBytes(ssml);
webRequest.ContentLength = btBodys.Length;
webRequest.GetRequestStream().Write(btBodys, 0, btBodys.Length);
string uniqueName = Guid.NewGuid().ToString();
StorageFile audioFile = await this.cacheDataFolder.CreateFileAsync(uniqueName + ".wav");
WebResponse httpWebResponse = await webRequest.GetResponseAsync();
using (Stream stream = httpWebResponse.GetResponseStream())
{
using (var outputStream = await audioFile.OpenAsync(FileAccessMode.ReadWrite))
{
await stream.CopyToAsync(outputStream.AsStreamForWrite());
await outputStream.FlushAsync();
}
}
StorageFile metadataFile = await this.cacheDataFolder.CreateFileAsync(uniqueName + ".json");
var cachedResult = new CachedResult { Text = text, VoiceId = voice.Id };
using (var outoutStream = await metadataFile.OpenAsync(FileAccessMode.ReadWrite))
{
string json = JsonConvert.SerializeObject(cachedResult, Formatting.Indented);
StreamWriter writer = new StreamWriter(outoutStream.AsStreamForWrite());
await writer.WriteAsync(json);
writer.Close();
}
// Add result to cache list and play it
cachedResult.AudioFilePath = audioFile.Path;
cachedResult.MetadataFilePath = metadataFile.Path;
CachedResults.Insert(0,cachedResult);
PlayAudioFile(audioFile.Path);
}
private void PlayCachedResultButtonClicked(object sender, RoutedEventArgs e)
{
var dataContext = (sender as FrameworkElement).DataContext as CachedResult;
PlayAudioFile(dataContext.AudioFilePath);
}
private void PlayAudioFile(string audioFilePath)
{
this.mediaPlayerElement.Source = MediaSource.CreateFromUri(new Uri(audioFilePath));
this.mediaPlayerElement.MediaPlayer.Play();
}
private async void DeleteCachedResultButtonClicked(object sender, RoutedEventArgs e)
{
var dataContext = (sender as FrameworkElement).DataContext as CachedResult;
await DeleteCachedResult(dataContext);
}
private async Task DeleteCachedResult(CachedResult dataContext)
{
try
{
var audioFile = await StorageFile.GetFileFromPathAsync(dataContext.AudioFilePath);
await audioFile.DeleteAsync();
}
catch { }
try
{
var metadataFile = await StorageFile.GetFileFromPathAsync(dataContext.MetadataFilePath);
await metadataFile.DeleteAsync();
}
catch { }
CachedResults.Remove(dataContext);
}
private async void ClearAllCachedResultsButtonClicked(object sender, RoutedEventArgs e)
{
await Util.ConfirmActionAndExecute("Delete all cached results?", async () =>
{
foreach (CachedResult item in CachedResults.ToArray())
{
await DeleteCachedResult(item);
}
});
}
private void OnPageLoaded(object sender, RoutedEventArgs e)
{
if (voiceComboBox.Items.Any())
{
voiceComboBox.SelectedIndex = 0;
}
}
}
}