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
/
Copy pathSpeechToTextExplorer.xaml.cs
661 lines (587 loc) · 28.6 KB
/
SpeechToTextExplorer.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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
//
// 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 IntelligentKioskSample.Controls;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Windows.Media.Core;
using Windows.Storage;
using Windows.UI;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Navigation;
namespace IntelligentKioskSample.Views.SpeechToText
{
[KioskExperience(Id = "SpeechToTextExplorer",
DisplayName = "Speech to Text Explorer",
Description = "Transcribe speech from live audio in 10 languages",
ImagePath = "ms-appx:/Assets/DemoGallery/Speech to Text Explorer.jpg",
ExperienceType = ExperienceType.Guided | ExperienceType.Business,
TechnologiesUsed = TechnologyType.SpeechToText,
TechnologyArea = TechnologyAreaType.Speech,
DateAdded = "2019/05/23")]
public sealed partial class SpeechToTextExplorer : Page, INotifyPropertyChanged
{
public static readonly int RecognitionMaxTimeDelayInSeconds = 5;
public static readonly int RecognitionTimeLimitInSeconds = 30;
private static readonly List<AudioSampleViewModel> audioSamples = new List<AudioSampleViewModel>()
{
new AudioSampleViewModel() { Name = "Sample 1 (English / US)", FileName = "ms-appx:///Assets/AudioSamples/Baryonyx.wav", LanguageCode = "en-US" },
new AudioSampleViewModel() { Name = "Sample 2 (English / US)", FileName = "ms-appx:///Assets/AudioSamples/ClassQX_F.wav", LanguageCode = "en-US" },
new AudioSampleViewModel() { Name = "Sample 3 (English / US)", FileName = "ms-appx:///Assets/AudioSamples/Fare4Denver_M.wav", LanguageCode = "en-US" },
new AudioSampleViewModel() { Name = "Sample 1 (German / DE)", FileName = "ms-appx:///Assets/AudioSamples/de-de-1.wav", LanguageCode = "de-DE" },
new AudioSampleViewModel() { Name = "Sample 2 (German / DE)", FileName = "ms-appx:///Assets/AudioSamples/de-de-2.wav", LanguageCode = "de-DE" },
new AudioSampleViewModel() { Name = "Sample 1 (French / FR)", FileName = "ms-appx:///Assets/AudioSamples/fr-fr-1.wav", LanguageCode = "fr-FR" },
new AudioSampleViewModel() { Name = "Sample 1 (Italian / IT)", FileName = "ms-appx:///Assets/AudioSamples/it-it-1.wav", LanguageCode = "it-IT" },
new AudioSampleViewModel() { Name = "Sample 2 (Italian / IT)", FileName = "ms-appx:///Assets/AudioSamples/it-it-2.wav", LanguageCode = "it-IT" },
new AudioSampleViewModel() { Name = "Sample 1 (Spanish / ES)", FileName = "ms-appx:///Assets/AudioSamples/es-es-1.wav", LanguageCode = "es-ES" },
new AudioSampleViewModel() { Name = "Sample 2 (Spanish / ES)", FileName = "ms-appx:///Assets/AudioSamples/es-es-2.wav", LanguageCode = "es-ES" },
new AudioSampleViewModel() { Name = "Sample 1 (Chinese / CN)", FileName = "ms-appx:///Assets/AudioSamples/zh-cn-1.wav", LanguageCode = "zh-CN" },
new AudioSampleViewModel() { Name = "Sample 2 (Chinese / CN)", FileName = "ms-appx:///Assets/AudioSamples/zh-cn-2.wav", LanguageCode = "zh-CN" },
new AudioSampleViewModel() { Name = "Sample 1 (Russian / RU)", FileName = "ms-appx:///Assets/AudioSamples/ru-ru-1.wav", LanguageCode = "ru-RU" },
new AudioSampleViewModel() { Name = "Sample 2 (Russian / RU)", FileName = "ms-appx:///Assets/AudioSamples/ru-ru-2.wav", LanguageCode = "ru-RU" }
};
private static readonly List<SupportedLanguage> supportedLanguages = new List<SupportedLanguage>()
{
new SupportedLanguage() { Name = "English (US)", Code = "en-US", TranslationCode = "en" },
new SupportedLanguage() { Name = "Arabic (EG)", Code = "ar-EG", TranslationCode = "ar" },
new SupportedLanguage() { Name = "Chinese (Mandarin)", Code = "zh-CN", TranslationCode = "zh-Hans" },
new SupportedLanguage() { Name = "French (FR)", Code = "fr-FR", TranslationCode = "fr" },
new SupportedLanguage() { Name = "German", Code = "de-DE", TranslationCode = "de" },
new SupportedLanguage() { Name = "Italian", Code = "it-IT", TranslationCode = "it" },
new SupportedLanguage() { Name = "Japanese", Code = "ja-JP", TranslationCode = "ja" },
new SupportedLanguage() { Name = "Portuguese (BR)", Code = "pt-BR", TranslationCode = "pt" },
new SupportedLanguage() { Name = "Russian", Code = "ru-RU", TranslationCode = "ru" },
new SupportedLanguage() { Name = "Spanish (ES)", Code = "es-ES", TranslationCode = "es" }
};
private DispatcherTimer timer;
private DispatcherTimer recordingTimer;
private int recordingSeconds = 0;
private bool isAzureSpeechEndpoint = false;
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyPropertyChanged(string name)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
}
private bool isProcessing = false;
public bool IsProcessing
{
get { return isProcessing; }
set
{
if (isProcessing != value)
{
isProcessing = value;
NotifyPropertyChanged("IsProcessing");
}
}
}
private SpeechExplorerState speechExplorerState = SpeechExplorerState.Initial;
private SpeechExplorerState oldSpeechExplorerState = SpeechExplorerState.Initial;
public SpeechExplorerState SpeechExplorerState
{
get { return speechExplorerState; }
set
{
if (speechExplorerState != value)
{
oldSpeechExplorerState = SpeechExplorerState;
speechExplorerState = value;
NotifyPropertyChanged("SpeechExplorerState");
}
}
}
private SupportedLanguage currentInputLanguage = supportedLanguages.FirstOrDefault(x => x.Code == "en-US");
public SupportedLanguage CurrentInputLanguage
{
get { return currentInputLanguage; }
set
{
if (currentInputLanguage != value)
{
currentInputLanguage = value;
NotifyPropertyChanged("CurrentInputLanguage");
CurrentInputLanguageChanged();
}
}
}
private SupportedLanguage firstTranslationLanguage;
public SupportedLanguage FirstTranslationLanguage
{
get { return firstTranslationLanguage; }
set
{
if (firstTranslationLanguage != value)
{
firstTranslationLanguage = value;
NotifyPropertyChanged("FirstTranslationLanguage");
TranslationRecognitionData outputData = new TranslationRecognitionData()
{
FirstTranslationText = this.speechToTextWithTranslation.GetFirstTranslatedText()
};
TranslationLanguagesChanged(outputData);
}
}
}
private SupportedLanguage secondTranslationLanguage;
public SupportedLanguage SecondTranslationLanguage
{
get { return secondTranslationLanguage; }
set
{
if (secondTranslationLanguage != value)
{
secondTranslationLanguage = value;
NotifyPropertyChanged("SecondTranslationLanguage");
TranslationRecognitionData outputData = new TranslationRecognitionData()
{
SecondTranslationText = this.speechToTextWithTranslation.GetSecondTranslatedText()
};
TranslationLanguagesChanged(outputData);
}
}
}
public ObservableCollection<SupportedLanguage> InputLanguagesCollection { get; set; } = new ObservableCollection<SupportedLanguage>(supportedLanguages);
public ObservableCollection<SupportedLanguage> TranslationLanguagesCollection { get; set; } = new ObservableCollection<SupportedLanguage>();
public ObservableCollection<AudioSampleViewModel> AudioSampleCollection { get; set; } = new ObservableCollection<AudioSampleViewModel>();
public SpeechToTextExplorer()
{
this.InitializeComponent();
this.DataContext = this;
TranslationLanguagesCollection.AddRange(supportedLanguages.Where(x => x.Code != CurrentInputLanguage?.Code));
AudioSampleCollection.AddRange(audioSamples.Where(x => x.LanguageCode == CurrentInputLanguage?.Code));
}
protected async override void OnNavigatedTo(NavigationEventArgs e)
{
// check Speech API keys
if (!string.IsNullOrEmpty(SettingsHelper.Instance.SpeechApiKey) && !string.IsNullOrEmpty(SettingsHelper.Instance.SpeechApiEndpoint))
{
this.isAzureSpeechEndpoint = IsAzureSpeechEndpoint(SettingsHelper.Instance.SpeechApiEndpoint);
await Initialize();
}
else
{
this.mainPage.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));
}
}
}
private async Task Initialize()
{
// Prompt the user for permission to access the microphone.
bool permissionGained = await AudioCapturePermissions.RequestMicrophonePermission();
if (permissionGained)
{
this.mainPage.IsEnabled = true;
this.notificationControl.Visibility = Visibility.Visible;
this.targetLanguagesListView.IsEnabled = this.isAzureSpeechEndpoint;
timer = new DispatcherTimer();
timer.Tick += AutoStopRecognitionHandlerTimerTick;
timer.Interval = new TimeSpan(0, 0, RecognitionTimeLimitInSeconds);
recordingTimer = new DispatcherTimer();
recordingTimer.Tick += RecordingTimerTickHandler;
recordingTimer.Interval = new TimeSpan(0, 0, 1);
this.speechToTextView.ShowNotificationEventHandler += OnShowNotification;
this.speechToTextWithTranslation.ShowNotificationEventHandler += OnShowNotification;
this.speechToTextWithTranslation.Closed += (s, args) => {
SpeechExplorerState = oldSpeechExplorerState != SpeechExplorerState.SpeechToTextWithTranslation ? oldSpeechExplorerState : SpeechExplorerState.SpeechToText;
};
}
else
{
this.mainPage.IsEnabled = false;
this.notificationControl.Visibility = Visibility.Collapsed;
ContentDialog deleteFileDialog = new ContentDialog
{
Title = "Intelligent Kiosk can't access the microphone",
Content = "To let kiosk use this device's microphone, go to Windows Settings -> Apps and turn on microphone permissions for Intelligent Kiosk.",
PrimaryButtonText = "Open Settings",
CloseButtonText = "Close",
DefaultButton = ContentDialogButton.Primary
};
ContentDialogResult result = await deleteFileDialog.ShowAsync();
if (result == ContentDialogResult.Primary)
{
await Windows.System.Launcher.LaunchUriAsync(new Uri("ms-settings:privacy-microphone"));
}
}
}
protected async override void OnNavigatedFrom(NavigationEventArgs e)
{
this.speechToTextView.ShowNotificationEventHandler -= OnShowNotification;
this.speechToTextWithTranslation.ShowNotificationEventHandler -= OnShowNotification;
await StopRecognitionAsync();
base.OnNavigatedFrom(e);
}
private async void AudioSampleCollection_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
AudioSampleViewModel selectedAudioSample = this.audioSampleListView.SelectedValue as AudioSampleViewModel;
if (selectedAudioSample == null || string.IsNullOrEmpty(selectedAudioSample.FileName))
{
return;
}
try
{
IsProcessing = true;
var properties = new Dictionary<string, string>();
this.startDictateButton.IsEnabled = false;
this.stopDictateButton.IsEnabled = false;
recordingSeconds = 0;
this.recordingTimePanel.Visibility = Visibility.Collapsed;
this.audioSampleFlyout.Hide();
var audioFileUri = new Uri(selectedAudioSample.FileName);
StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(audioFileUri);
if (file != null)
{
Play(audioFileUri);
switch (SpeechExplorerState)
{
case SpeechExplorerState.Initial:
case SpeechExplorerState.SpeechToText:
SpeechExplorerState = SpeechExplorerState.SpeechToText;
await this.speechToTextView.SpeechRecognitionFromFileAsync(file);
break;
case SpeechExplorerState.SpeechToTextWithTranslation:
await this.speechToTextWithTranslation.SpeechRecognitionFromFileAsync(file);
break;
}
}
}
catch (Exception ex)
{
await Util.GenericApiCallExceptionHandler(ex, "Audio sample recognition error");
}
finally
{
IsProcessing = false;
this.recordingTimePanel.Visibility = Visibility.Visible;
this.startDictateButton.IsEnabled = true;
this.stopDictateButton.IsEnabled = true;
this.audioSampleListView.SelectedItem = null;
}
}
private void CurrentInputLanguageChanged()
{
// update the text boxes
RecognitionData inputData;
switch (SpeechExplorerState)
{
case SpeechExplorerState.Initial:
case SpeechExplorerState.SpeechToText:
inputData = this.speechToTextView.GetRecognizedData();
if (!string.IsNullOrEmpty(inputData?.Text) && !Regex.IsMatch(inputData.Text, @"[()]"))
{
inputData.Text = inputData.Language != null ? $"({inputData.Language.Name}) {inputData.Text}" : inputData.Text;
inputData.HighlightStyle = true;
this.speechToTextView.SetRecognitionData(inputData);
}
break;
case SpeechExplorerState.SpeechToTextWithTranslation:
inputData = this.speechToTextWithTranslation.GetRecognizedData();
if (!string.IsNullOrEmpty(inputData?.Text) && !Regex.IsMatch(inputData.Text, @"[()]"))
{
inputData.Text = inputData.Language != null ? $"({inputData.Language.Name}) {inputData.Text}" : inputData.Text;
inputData.HighlightStyle = true;
this.speechToTextWithTranslation.SetRecognitionData(inputData, null);
}
break;
}
// update audio samples
AudioSampleCollection.Clear();
AudioSampleCollection.AddRange(audioSamples.Where(x => x.LanguageCode == CurrentInputLanguage?.Code));
// update translation languages
string firstLangCode = FirstTranslationLanguage?.Code;
string secondLangCode = SecondTranslationLanguage?.Code;
TranslationLanguagesCollection.Clear();
TranslationLanguagesCollection.AddRange(supportedLanguages.Where(x => x.Code != CurrentInputLanguage?.Code));
FirstTranslationLanguage = CurrentInputLanguage?.Code == firstLangCode
? TranslationLanguagesCollection.FirstOrDefault() : TranslationLanguagesCollection.FirstOrDefault(x => x.Code == firstLangCode);
SecondTranslationLanguage = CurrentInputLanguage?.Code == secondLangCode
? TranslationLanguagesCollection.LastOrDefault() : TranslationLanguagesCollection.FirstOrDefault(x => x.Code == secondLangCode);
}
private void TranslationLanguagesChanged(TranslationRecognitionData outputData)
{
switch (SpeechExplorerState)
{
case SpeechExplorerState.Initial:
case SpeechExplorerState.SpeechToText:
this.speechToTextWithTranslation.SetRecognitionData(this.speechToTextView.GetRecognizedData(), null);
SpeechExplorerState = SpeechExplorerState.SpeechToTextWithTranslation;
break;
case SpeechExplorerState.SpeechToTextWithTranslation:
if (!string.IsNullOrEmpty(outputData?.FirstTranslationText?.Text))
{
string text = outputData.FirstTranslationText.Text;
string language = outputData.FirstTranslationText.Language?.Name;
if (!Regex.IsMatch(text, @"[()]"))
{
outputData.FirstTranslationText.Text = language != null ? $"({language}) {text}" : string.Empty;
outputData.FirstTranslationText.HighlightStyle = true;
}
}
if (!string.IsNullOrEmpty(outputData?.SecondTranslationText?.Text))
{
string text = outputData.SecondTranslationText.Text;
string language = outputData.SecondTranslationText.Language?.Name;
if (!Regex.IsMatch(text, @"[()]"))
{
outputData.SecondTranslationText.Text = language != null ? $"({language}) {text}" : string.Empty;
outputData.SecondTranslationText.HighlightStyle = true;
}
}
this.speechToTextWithTranslation.SetRecognitionData(null, outputData);
break;
}
this.targetLanguagesListView.IsEnabled = FirstTranslationLanguage == null || SecondTranslationLanguage == null;
}
private async void StartDictateButtonClicked(object sender, RoutedEventArgs e)
{
await StartRecognitionAsync();
}
private async void StopDictateButtonClicked(object sender, RoutedEventArgs e)
{
await StopRecognitionAsync();
}
private async Task StartRecognitionAsync()
{
IsProcessing = true;
switch (SpeechExplorerState)
{
case SpeechExplorerState.Initial:
case SpeechExplorerState.SpeechToText:
SpeechExplorerState = SpeechExplorerState.SpeechToText;
await this.speechToTextView.StartSpeechRecognitionAsync();
break;
case SpeechExplorerState.SpeechToTextWithTranslation:
await this.speechToTextWithTranslation.StartSpeechRecognitionAsync();
break;
}
recordingSeconds = 0;
recordingTimer?.Start();
timer?.Start();
}
private async Task StopRecognitionAsync()
{
if (!IsProcessing)
{
return;
}
try
{
IsProcessing = false;
timer?.Stop();
recordingTimer?.Stop();
switch (SpeechExplorerState)
{
case SpeechExplorerState.Initial:
case SpeechExplorerState.SpeechToText:
SpeechExplorerState = SpeechExplorerState.SpeechToText;
await this.speechToTextView.StopSpeechRecognitionAsync();
break;
case SpeechExplorerState.SpeechToTextWithTranslation:
await this.speechToTextWithTranslation.StopSpeechRecognitionAsync();
break;
}
}
catch (Exception ex)
{
await Util.GenericApiCallExceptionHandler(ex, "Stop speech recognition error");
}
}
private async void AutoStopRecognitionHandlerTimerTick(object sender, object e)
{
ShowNotificationAlert(prms: new NotificationParams { Message = "Your recording session ended after reaching the time limit." });
await StopRecognitionAsync();
}
private void RecordingTimerTickHandler(object sender, object e)
{
if (recordingSeconds <= RecognitionTimeLimitInSeconds)
{
recordingSeconds += 1;
this.recordingSecondsTextBlock.Text = recordingSeconds < 10 ? $"00:0{recordingSeconds}" : $"00:{recordingSeconds}";
}
}
private void Play(Uri fileUri)
{
this.mediaPlayerElement.Source = MediaSource.CreateFromUri(fileUri);
this.mediaPlayerElement.MediaPlayer.Play();
}
private void OnMainGridTapped(object sender, Windows.UI.Xaml.Input.TappedRoutedEventArgs e)
{
var tappedElement = e.OriginalSource as FrameworkElement;
if (tappedElement == this.notificationControl)
{
ShowNotificationAlert();
}
else
{
HideNotificationAlert();
}
}
private void OnShowNotification(object sender, NotificationViewModel notificationViewModel)
{
ShowNotificationAlert(notificationViewModel.NotificationType, notificationViewModel.IsFileProcessing, notificationViewModel.NotificationParams);
}
private async void ShowNotificationAlert(NotificationType notificationType = NotificationType.Warning, bool isFileProcessing = false, NotificationParams prms = null)
{
if (notificationType == NotificationType.Error && !isFileProcessing)
{
await this.StopRecognitionAsync();
}
if (prms != null)
{
this.notificationControl.ShowNotification(prms);
}
}
private void HideNotificationAlert()
{
this.notificationControl.HideNotification();
}
private void TargetLanguagesListView_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (this.targetLanguagesListView.SelectedItem is SupportedLanguage supportedLanguage)
{
if (FirstTranslationLanguage == null)
{
FirstTranslationLanguage = supportedLanguage;
SpeechExplorerState = SpeechExplorerState.SpeechToTextWithTranslation;
this.speechToTextWithTranslation.UpdateTranslationView();
}
else if (SecondTranslationLanguage == null)
{
SecondTranslationLanguage = supportedLanguage;
SpeechExplorerState = SpeechExplorerState.SpeechToTextWithTranslation;
this.speechToTextWithTranslation.UpdateTranslationView();
}
}
this.targetLanguagesListView.SelectedItem = null;
this.translationFlyout.Hide();
}
private void TranslationFlyout_Opened(object sender, object e)
{
if (!this.isAzureSpeechEndpoint)
{
ShowNotificationAlert(prms: new NotificationParams
{
BackgroundColor = Colors.OrangeRed,
Message = "Speech Service container only supports Speech-to-text feature. Click here for details: ",
Link = new CustomHyperlink(new Uri("https://aka.ms/speechcontainer"))
});
}
}
private bool IsAzureSpeechEndpoint(string endpoint)
{
bool isUri = !string.IsNullOrEmpty(endpoint) && Uri.IsWellFormedUriString(endpoint, UriKind.Absolute);
if (isUri)
{
string host = new Uri(endpoint).Host;
return !string.IsNullOrEmpty(host) && host.Contains(@".speech.microsoft.com");
}
return false;
}
}
public enum SpeechExplorerState
{
Initial,
SpeechToText,
SpeechToTextWithTranslation
}
public enum NotificationType
{
Error,
Warning
}
public class AudioSampleViewModel
{
public string Name { get; set; }
public string FileName { get; set; }
public string LanguageCode { get; set; }
}
public class SupportedLanguage
{
public string Name { get; set; }
public string Code { get; set; }
public string TranslationCode { get; set; }
}
public class RecognitionData
{
public string Text { get; set; }
public SupportedLanguage Language { get; set; }
public bool HighlightStyle { get; set; }
public RecognitionData(string text = "", SupportedLanguage language = null, bool highlightStyle = false)
{
Text = text;
Language = language;
HighlightStyle = highlightStyle;
}
}
public class NotificationViewModel
{
public NotificationType NotificationType { get; set; } = NotificationType.Warning;
public bool IsFileProcessing { get; set; }
public NotificationParams NotificationParams { get; set; }
}
public class PageStateToVisibilityConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
SpeechExplorerState? state = value as SpeechExplorerState?;
SpeechExplorerState? stateParameter = (SpeechExplorerState)Enum.Parse(typeof(SpeechExplorerState), parameter.ToString());
if (state.HasValue && stateParameter.HasValue && state == stateParameter)
{
return Visibility.Visible;
}
return Visibility.Collapsed;
}
public object ConvertBack(object value, Type targetType, object parameter, string language)
{
return value;
}
}
}