Advertisement
eduardogr

Untitled

Dec 17th, 2019
292
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.12 KB | None | 0 0
  1.      private const string ON = "enabled";
  2.             private const string OFF = "disabled";
  3.             private SpeechRecognizer speechRecognizer;
  4.             private CoreDispatcher dispatcher;
  5.             private StringBuilder dictateBuilder;
  6.    
  7.             public NotesPage() {
  8.                 InitializeComponent();
  9.                 dispatcher = CoreWindow.GetForCurrentThread().Dispatcher;
  10.                 speechRecognizer = new SpeechRecognizer();
  11.                 dictateBuilder = new StringBuilder();
  12.             }
  13.    
  14.      case "0":
  15.                         var dictationConstraint = new SpeechRecognitionTopicConstraint(SpeechRecognitionScenario.Dictation, "dictation");
  16.                         speechRecognizer.Constraints.Add(dictationConstraint);
  17.                         SpeechRecognitionCompilationResult result = await speechRecognizer.CompileConstraintsAsync();
  18.                         speechRecognizer.ContinuousRecognitionSession.Completed += ContinuousRecognitionSession_CompletedAsync;
  19.                         speechRecognizer.ContinuousRecognitionSession.ResultGenerated += ContinuousRecognitionSession_ResultGenerated;
  20.                         speechRecognizer.HypothesisGenerated += SpeechRecognizer_HypothesisGenerated;
  21.                         await speechRecognizer.ContinuousRecognitionSession.StartAsync();
  22.                         textToSpeech.Background = (SolidColorBrush)Resources[ON];
  23.     break;
  24.    
  25.        private async void SpeechRecognizer_HypothesisGenerated(
  26.                 SpeechRecognizer sender,
  27.                 SpeechRecognitionHypothesisGeneratedEventArgs args) {
  28.    
  29.                 string hypothesis = args.Hypothesis.Text;
  30.                 string textboxContent = dictateBuilder.ToString() + " " + hypothesis + " ...";
  31.    
  32.                 await dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
  33.                 {
  34.                     richEbitBox.Document.SetText(TextSetOptions.None, textboxContent);
  35.                 });
  36.             }
  37.    
  38.             private async void ContinuousRecognitionSession_ResultGenerated(
  39.                 SpeechContinuousRecognitionSession sender,
  40.                 SpeechContinuousRecognitionResultGeneratedEventArgs args) {
  41.    
  42.                 if (args.Result.Confidence == SpeechRecognitionConfidence.Medium ||
  43.                       args.Result.Confidence == SpeechRecognitionConfidence.High) {
  44.    
  45.                      dictateBuilder.Append(args.Result.Text + " ");
  46.    
  47.                     await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => {
  48.                         richEbitBox.Document.SetText(TextSetOptions.None, dictateBuilder.ToString());
  49.                     });
  50.                 }
  51.             }
  52.    
  53.             private async void ContinuousRecognitionSession_CompletedAsync(
  54.                 SpeechContinuousRecognitionSession sender,
  55.                 SpeechContinuousRecognitionCompletedEventArgs args) {
  56.    
  57.                 await speechRecognizer.ContinuousRecognitionSession.StopAsync();
  58.                 speechRecognizer.Dispose();
  59.                 textToSpeech.Background = (SolidColorBrush)Resources[OFF];
  60.             }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement