Advertisement
eduardogr

Untitled

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