Advertisement
eduardogr

Untitled

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