Advertisement
eduardogr

Untitled

Dec 25th, 2019
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.84 KB | None | 0 0
  1.     private const string ON = "enabled";
  2.         private const string OFF = "disabled";
  3.         private SpeechRecognizer speechRecognizer = new SpeechRecognizer();
  4.         private CoreDispatcher dispatcher;
  5.         private StringBuilder dictateBuilder = new StringBuilder();
  6.         bool isListening = false;
  7.  
  8.         public NotesPage() {
  9.             InitializeComponent();
  10.             dispatcher = CoreWindow.GetForCurrentThread().Dispatcher;
  11.             var dictationConstraint = new SpeechRecognitionTopicConstraint(SpeechRecognitionScenario.Dictation, "dictation");
  12.             speechRecognizer.Constraints.Add(dictationConstraint);
  13.             speechRecognizer.ContinuousRecognitionSession.Completed += ContinuousRecognitionSession_Completed;
  14.             speechRecognizer.ContinuousRecognitionSession.ResultGenerated += ContinuousRecognitionSession_ResultGenerated;
  15.             speechRecognizer.HypothesisGenerated += SpeechRecognizer_HypothesisGenerated;
  16.         }
  17.  
  18.         private void Cotent_TextChanged(object sender, RoutedEventArgs e) {
  19.  
  20.             richEbitBox.Document.GetText(TextGetOptions.None, out string value);
  21.             charactersCount.Text = $"Characters: {value.Length - 1}";
  22.         }
  23.  
  24.         private async void Actions_Click(object sender, RoutedEventArgs e) {
  25.  
  26.             richEbitBox.Document.Selection.SetRange(0, richEbitBox.Document.Selection.EndPosition);
  27.  
  28.             var id = sender as Button;
  29.  
  30.             richEbitBox.Focus(FocusState.Pointer);
  31.  
  32.             switch (id.Tag) {
  33.  
  34.                 case "0":
  35.                     if (isListening == false) {
  36.                         // The recognizer can only start listening in a continuous fashion if the recognizer is currently idle.
  37.                         // This prevents an exception from occurring.
  38.                         if (speechRecognizer.State == SpeechRecognizerState.Idle) {
  39.                             isListening = true;
  40.                             await speechRecognizer.ContinuousRecognitionSession.StartAsync();
  41.                             isListening = false;
  42.                         }
  43.                     } else {
  44.                         isListening = false;
  45.                         if (speechRecognizer.State != SpeechRecognizerState.Idle) {
  46.                             // Cancelling recognition prevents any currently recognized speech from
  47.                             // generating a ResultGenerated event. StopAsync() will allow the final session to
  48.                             // complete.
  49.                             await speechRecognizer.ContinuousRecognitionSession.StopAsync();
  50.                         }
  51.                     }
  52.                     break;
  53.                 case "1":
  54.                     if (richEbitBox.Document.Selection.CharacterFormat.Bold == FormatEffect.On) {
  55.                         richEbitBox.Document.Selection.CharacterFormat.Bold = FormatEffect.Off;
  56.                         FormatBoltText.Background = (SolidColorBrush)Resources[OFF];
  57.                     } else {
  58.                         richEbitBox.Document.Selection.CharacterFormat.Bold = FormatEffect.On;
  59.                         FormatBoltText.Background = (SolidColorBrush)Resources[ON];
  60.                     }
  61.                     break;
  62.                 case "2":
  63.                     if (richEbitBox.Document.Selection.CharacterFormat.Italic == FormatEffect.On) {
  64.                         richEbitBox.Document.Selection.CharacterFormat.Italic = FormatEffect.Off;
  65.                         formatItalicText.Background = (SolidColorBrush)Resources[OFF];
  66.                     } else {
  67.                         richEbitBox.Document.Selection.CharacterFormat.Italic = FormatEffect.On;
  68.                         formatItalicText.Background = (SolidColorBrush)Resources[ON];
  69.                     }
  70.                     break;
  71.                 case "3":
  72.                     if (richEbitBox.Document.Selection.CharacterFormat.Underline == UnderlineType.Single) {
  73.                         richEbitBox.Document.Selection.CharacterFormat.Underline = UnderlineType.None;
  74.                         formatUnderlineText.Background = (SolidColorBrush)Resources[OFF];
  75.                     } else {
  76.                         richEbitBox.Document.Selection.CharacterFormat.Underline = UnderlineType.Single;
  77.                         formatUnderlineText.Background = (SolidColorBrush)Resources[ON];
  78.                     }
  79.                     break;
  80.                 case "5":
  81.                     richEbitBox.Document.GetText(TextGetOptions.AdjustCrlf, out string value);
  82.                     speak(value);
  83.                     break;
  84.                 default:
  85.                     break;
  86.             }
  87.         }
  88.  
  89.         private async void SpeechRecognizer_HypothesisGenerated(
  90.             SpeechRecognizer sender,
  91.             SpeechRecognitionHypothesisGeneratedEventArgs args) {
  92.  
  93.             string hypothesis = args.Hypothesis.Text;
  94.             string textboxContent = dictateBuilder.ToString() + " " + hypothesis + " ...";
  95.  
  96.             await dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => {
  97.                 richEbitBox.Document.SetText(TextSetOptions.None, textboxContent);
  98.             });
  99.         }
  100.  
  101.         private async void ContinuousRecognitionSession_ResultGenerated(
  102.             SpeechContinuousRecognitionSession sender,
  103.             SpeechContinuousRecognitionResultGeneratedEventArgs args) {
  104.  
  105.             if (args.Result.Confidence == SpeechRecognitionConfidence.Medium ||
  106.                   args.Result.Confidence == SpeechRecognitionConfidence.High) {
  107.  
  108.                 dictateBuilder.Append(args.Result.Text + " ");
  109.  
  110.                 await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => {
  111.                     richEbitBox.Document.SetText(TextSetOptions.None, dictateBuilder.ToString());
  112.                 });
  113.             }
  114.         }
  115.  
  116.         private void ContinuousRecognitionSession_Completed(
  117.             SpeechContinuousRecognitionSession sender,
  118.             SpeechContinuousRecognitionCompletedEventArgs args) {
  119.         }
  120.         private async void speak(string value) {
  121.  
  122.             MediaElement mediaElement = new MediaElement();
  123.  
  124.             var synth = new SpeechSynthesizer();
  125.  
  126.             VoiceInformation voiceInfo = (from voice in SpeechSynthesizer.AllVoices
  127.                                           where voice.Gender == VoiceGender.Female
  128.                                           select voice).FirstOrDefault();
  129.  
  130.             synth.Voice = voiceInfo;
  131.  
  132.             // Generate the audio stream from plain text.
  133.             SpeechSynthesisStream stream = await synth.SynthesizeTextToStreamAsync(value);
  134.  
  135.             // Send the stream to the media object.
  136.             mediaElement.SetSource(stream, stream.ContentType);
  137.             mediaElement.Play();
  138.         }
  139.  
  140.         private void ComboChanged(object sender, SelectionChangedEventArgs e) {
  141.  
  142.             richEbitBox.Focus(FocusState.Pointer);
  143.  
  144.             var id = sender as ComboBox;
  145.             switch (id.Tag) {
  146.  
  147.                 case "1":
  148.                     string fontName = id.SelectedItem.ToString();
  149.                     richEbitBox.Document.Selection.CharacterFormat.Name = fontName;
  150.                     break;
  151.                 case "2":
  152.                     var size = id.SelectedItem.ToString();
  153.                     //set size to the Selection
  154.                     richEbitBox.Document.Selection.CharacterFormat.Size = Convert.ToInt32(size);
  155.                     break;
  156.                 default:
  157.                     break;
  158.             }
  159.         }
  160.  
  161.         private void fontBox_Loaded(object sender, RoutedEventArgs e) {
  162.             fontBox.Text = richEbitBox.FontFamily.Source.ToString();
  163.             fontSizeBox.Text = richEbitBox.FontSize.ToString();
  164.         }
  165.  
  166.         private void fontBox_TextSubmitted(ComboBox sender, ComboBoxTextSubmittedEventArgs args) {
  167.             richEbitBox.Focus(FocusState.Pointer);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement