Advertisement
eduardogr

Untitled

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