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 578
/
SpeakerRecognitionExplorer.xaml.cs
375 lines (331 loc) · 14.2 KB
/
SpeakerRecognitionExplorer.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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
//
// 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 Microsoft.CognitiveServices.Speech;
using Microsoft.CognitiveServices.Speech.Audio;
using ServiceHelpers;
using ServiceHelpers.Models;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
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.Controls.Primitives;
using Windows.UI.Xaml.Navigation;
namespace IntelligentKioskSample.Views.SpeakerRecognition
{
[KioskExperience(Id = "SpeakerRecognitionExplorer",
DisplayName = "Speaker Recognition Explorer",
Description = "Identify speakers by their unique voice characteristics using voice biometry",
ImagePath = "ms-appx:/Assets/DemoGallery/Speaker Recognition Explorer.jpg",
ExperienceType = ExperienceType.Preview | ExperienceType.Business,
TechnologiesUsed = TechnologyType.SpeechToText,
TechnologyArea = TechnologyAreaType.Speech,
DateAdded = "2020/10/07")]
public sealed partial class SpeakerRecognitionExplorer : Page, INotifyPropertyChanged
{
private static readonly string DefaultSpeechAPIRegion = "westus";
public static readonly string[] AudioExtensions = new string[] { ".wav" };
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyPropertyChanged(string name)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
}
private bool isWestUSRegion = false;
private SpeechConfig userProvidedSpeechConfig;
private SpeakerRecognitionService userProvidedSpeakerRecognitionService;
public ObservableCollection<SpeakerRecognitionViewModel> Models { get; set; } = new ObservableCollection<SpeakerRecognitionViewModel>();
private SourceType sourceType = SourceType.FromMicrophone;
public SourceType SourceType
{
get { return sourceType; }
set
{
if (sourceType != value)
{
sourceType = value;
NotifyPropertyChanged("SourceType");
}
}
}
private ProcessState processState = ProcessState.Overview;
public ProcessState ProcessState
{
get { return processState; }
set
{
if (processState != value)
{
processState = value;
NotifyPropertyChanged("ProcessState");
}
}
}
public SpeakerRecognitionExplorer()
{
this.InitializeComponent();
this.DataContext = this;
}
protected async override void OnNavigatedTo(NavigationEventArgs e)
{
this.isWestUSRegion = !string.IsNullOrEmpty(SettingsHelper.Instance.SpeechApiEndpoint) && SettingsHelper.Instance.SpeechApiEndpoint.Contains("westus.");
if (!string.IsNullOrEmpty(SettingsHelper.Instance.SpeechApiKey) && isWestUSRegion)
{
this.mainPage.IsEnabled = true;
this.userProvidedSpeechConfig = SpeechConfig.FromSubscription(SettingsHelper.Instance.SpeechApiKey, DefaultSpeechAPIRegion);
this.userProvidedSpeakerRecognitionService = new SpeakerRecognitionService(SettingsHelper.Instance.SpeechApiKey, $"https://{DefaultSpeechAPIRegion}.api.cognitive.microsoft.com");
ProcessState = ProcessState.Overview;
await LoadModelsAsync();
}
else
{
this.mainPage.IsEnabled = false;
await new MessageDialog("Missing or unsupported Speech API Key in the Settings Page. \nSpeaker Recognition is a preview service, and currently only available in the West US region.", "Missing API Keys").ShowAsync();
}
base.OnNavigatedTo(e);
}
protected override void OnNavigatedFrom(NavigationEventArgs e)
{
Pause();
base.OnNavigatedFrom(e);
}
private async void OnUploadAudioFileClick(object sender, RoutedEventArgs e)
{
StorageFile file = await Util.PickSingleFileAsync(AudioExtensions);
if (file != null)
{
await StartRecognizeFromFileAsync(file);
}
}
private async void StartRecognitionButtonClicked(object sender, RoutedEventArgs e)
{
if (Models.Any())
{
await RecognizeFromSourceAsync(SourceType.FromMicrophone);
}
else
{
await ShowMessage("No enrolled voices", "You have no enrolled voices. Please enroll a new voice.");
}
}
private async void OnFilesSamplesDropDownButtonClicked(object sender, RoutedEventArgs e)
{
if (Models.Any())
{
FlyoutBase.ShowAttachedFlyout((FrameworkElement)sender);
}
else
{
await ShowMessage("No enrolled voices", "You have no enrolled voices. Please enroll a new voice.");
}
}
private void OnSwitchInputModeButtonClicked(object sender, RoutedEventArgs e)
{
SourceType = SourceType == SourceType.FromFile ? SourceType.FromMicrophone : SourceType.FromFile;
ProcessState = ProcessState.Overview;
}
private void OnTryAgainButtonClicked(object sender, RoutedEventArgs e)
{
ProcessState = ProcessState.Overview;
}
#region Recognition
private async Task StartRecognizeFromFileAsync(StorageFile file)
{
StorageFile fileCopy = null;
try
{
this.fileSamplesFlyout.Hide();
fileCopy = await file.CopyAsync(ApplicationData.Current.TemporaryFolder, file.Name, NameCollisionOption.GenerateUniqueName);
Play(file);
await RecognizeFromSourceAsync(SourceType.FromFile, fileCopy);
}
catch (Exception ex)
{
await Util.GenericApiCallExceptionHandler(ex, "Failure recognizing from file");
}
finally
{
if (fileCopy != null)
{
await fileCopy.DeleteAsync();
}
}
}
private async Task RecognizeFromSourceAsync(SourceType sourceType, StorageFile file = null)
{
try
{
ProcessState = ProcessState.Processing;
ToggleUI(isProcessing: true);
// get all identification profiles based on current speakerRecognitionService
SpeakerProfilesResponse userProfileResponse = await this.userProvidedSpeakerRecognitionService.GetProfilesAsync(VoiceProfileType.TextIndependentIdentification);
List<SpeakerRecognitionViewModel> userIdentificationProfiles = userProfileResponse != null ? Models.Where(m => userProfileResponse.Profiles.Select(p => p.ProfileId).Contains(m.IdentificationProfileId)).ToList() : new List<SpeakerRecognitionViewModel>();
// recognize
SpeakerRecognitionResult userResult = null;
using (AudioConfig audioInput = sourceType == SourceType.FromFile ? AudioConfig.FromWavFileInput(file.Path) : AudioConfig.FromDefaultMicrophoneInput())
{
userResult = await RecognizeAsync(userProvidedSpeechConfig, audioInput, userIdentificationProfiles);
}
// display result
if (userResult == null || userResult.Reason == ResultReason.Canceled)
{
ProcessState = ProcessState.Error;
var cancellation = SpeakerRecognitionCancellationDetails.FromResult(userResult);
await ShowMessage("Speaker Identification Error", $"CANCELED: ErrorCode={cancellation.ErrorCode} ErrorDetails={cancellation.ErrorDetails}");
}
else
{
ProcessState = ProcessState.Completed;
double score = userResult?.Score ?? 0;
this.detectedVoice.Text = userResult != null ? userIdentificationProfiles.FirstOrDefault(m => string.Equals(m.IdentificationProfileId, userResult.ProfileId, StringComparison.OrdinalIgnoreCase))?.Name : string.Empty;
this.detectedVoiceProbability.Text = $"({Math.Round(score * 100)}%)";
}
}
catch (Exception ex)
{
ProcessState = ProcessState.Error;
await Util.GenericApiCallExceptionHandler(ex, "Speaker recognition error");
}
finally
{
ToggleUI(isProcessing: false);
}
}
private async Task<SpeakerRecognitionResult> RecognizeAsync(SpeechConfig speechConfig, AudioConfig audioConfig, List<SpeakerRecognitionViewModel> identificationProfiles)
{
var voiceProfiles = new List<VoiceProfile>();
foreach (SpeakerRecognitionViewModel profile in identificationProfiles)
{
voiceProfiles.Add(new VoiceProfile(profile.IdentificationProfileId));
}
using (var speakerRecognizer = new SpeakerRecognizer(speechConfig, audioConfig))
{
var model = SpeakerIdentificationModel.FromProfiles(voiceProfiles);
return await speakerRecognizer.RecognizeOnceAsync(model);
}
}
#endregion
#region Voice Models
private async Task LoadModelsAsync()
{
try
{
this.Models.Clear();
List<SpeakerRecognitionViewModel> allCustomModels = await SpeakerRecognitionDataLoader.GetCustomModelsAsync();
this.Models.AddRange(allCustomModels);
}
catch (Exception ex)
{
await Util.GenericApiCallExceptionHandler(ex, "Failure loading models");
}
}
private void OnCreateNewModelClick(object sender, RoutedEventArgs e)
{
this.speakerRecognitionModelSetupControl.OpenScenarioSetupForm(this.userProvidedSpeakerRecognitionService, this.userProvidedSpeechConfig);
}
private async void OnNewModelCreated(object sender, SpeakerRecognitionViewModel e)
{
try
{
this.Models.Add(e);
// update local file with custom models
await SpeakerRecognitionDataLoader.SaveCustomModelsToFileAsync(this.Models.Where(x => !x.IsPrebuiltModel));
}
catch (Exception ex)
{
await Util.GenericApiCallExceptionHandler(ex, "Failure creating model");
}
}
private async void OnModelDeleteButtonClick(object sender, RoutedEventArgs e)
{
var model = sender is Button button ? (SpeakerRecognitionViewModel)button.DataContext : null;
if (model != null)
{
await Util.ConfirmActionAndExecute("Delete model?", async () => { await DeleteModelAsync(model); });
}
}
private async Task DeleteModelAsync(SpeakerRecognitionViewModel model)
{
try
{
this.Models.Remove(model);
await SpeakerRecognitionDataLoader.SaveCustomModelsToFileAsync(this.Models.Where(x => !x.IsPrebuiltModel));
if (model.IdentificationProfileId != null)
{
await this.userProvidedSpeakerRecognitionService.DeleteProfileAsync(model.IdentificationProfileId, VoiceProfileType.TextIndependentIdentification);
}
}
catch (Exception ex)
{
await Util.GenericApiCallExceptionHandler(ex, "Failure deleting model");
}
}
#endregion
private void ToggleUI(bool isProcessing)
{
this.startDetectionButton.IsEnabled = !isProcessing;
if (!isProcessing)
{
Pause();
}
}
private void Play(StorageFile file)
{
if (file != null)
{
this.mediaPlayerElement.Source = MediaSource.CreateFromStorageFile(file);
this.mediaPlayerElement.MediaPlayer.Play();
}
}
private void Pause()
{
this.mediaPlayerElement.MediaPlayer.Pause();
}
private async Task ShowMessage(string title, string message)
{
await new MessageDialog(message, title).ShowAsync();
}
}
public enum ProcessState
{
Overview,
Processing,
Completed,
Error
}
}