Exemple #1
0
        public async void TextToSpeach(string text)
        {
            var voice             = SpeechSynthesizer.AllVoices.First(a => a.Language.Contains("en"));
            var speechSynthezizer = new SpeechSynthesizer();

            if (voice == null)
            {
                Device.BeginInvokeOnMainThread(async() =>
                {
                    var messageDialog = new Windows.UI.Popups.MessageDialog("Please download en-US Language-Pack", "Language not found");
                    await messageDialog.ShowAsync();
                    Recorded?.Invoke("");
                });
            }
            else
            {
                speechSynthezizer.Voice = voice;
                var stream = await speechSynthezizer.SynthesizeTextToStreamAsync(text);

                Device.BeginInvokeOnMainThread(() =>
                {
                    MediaElement e = new MediaElement();
                    e.SetSource(stream, stream.ContentType);
                    e.Play();
                });
            }
        }
Exemple #2
0
        public void RecordSpeachToText()
        {
            Task.Factory.StartNew(async() =>
            {
                try
                {
                    var language = new Windows.Globalization.Language("en-US");

                    var speechRecognizer = new SpeechRecognizer(language);
                    await speechRecognizer.CompileConstraintsAsync();
                    var result = await speechRecognizer.RecognizeWithUIAsync();

                    Recorded?.Invoke(result.Text);
                }
                catch (System.Runtime.InteropServices.COMException)
                {
                    Device.BeginInvokeOnMainThread(async() =>
                    {
                        var messageDialog = new Windows.UI.Popups.MessageDialog("Please download en-US Language-Pack", "Language not found");
                        await messageDialog.ShowAsync();
                        Recorded?.Invoke("");
                    });
                }
                catch
                {
                    Device.BeginInvokeOnMainThread(async() =>
                    {
                        var messageDialog = new Windows.UI.Popups.MessageDialog("No permission to record", "Permission denied");
                        await messageDialog.ShowAsync();
                        Recorded?.Invoke("");
                    });
                }
            });
        }
 /// <summary>
 /// 将会用作主要的函数来记录操作。
 /// </summary>
 /// <param name="operations"></param>
 /// <param name="operationTime"></param>
 public void Record(OperationStack operations, DateTime operationTime)
 {
     if (operations != null)
     {
         Recording?.Invoke(operations, operationTime);
         UndoStack.Push(new Tuple <OperationStack, DateTime>(operations, operationTime));
         Recorded?.Invoke(operations, operationTime);
     }
 }
Exemple #4
0
 private void EndRecognition(object sender, ElapsedEventArgs e)
 {
     node.RemoveTapOnBus(0);
     audioEngine.Stop();
     liveSpeechRequest.EndAudio();
     RecognitionTask.Cancel();
     recording = false;
     Recorded?.Invoke(lastSpokenString);
 }
 /// <summary>
 /// 添加一个操作
 /// </summary>
 /// <param name="operation">要添加的操作</param>
 public void Record(Operation operation)
 {
     if (operation != null && IsRecording)
     {
         var stack = new OperationStack(operation.PropertyName.ToString());
         stack.Push(operation);
         var recordTime = DateTime.Now;
         Recording?.Invoke(stack, recordTime);
         UndoStack.Push(new Tuple <OperationStack, DateTime>(stack, recordTime));
         RedoStack.Clear();
         Recorded?.Invoke(stack, recordTime);
     }
 }
Exemple #6
0
        private void StartSpeechRecognizer()
        {
            if (!recording)
            {
                speechRecognizer = new SFSpeechRecognizer();
                node             = audioEngine.InputNode;
                var recordingFormat = node.GetBusOutputFormat(0);
                liveSpeechRequest = new SFSpeechAudioBufferRecognitionRequest();

                node.InstallTapOnBus(0, 1024, recordingFormat,
                                     (AVAudioPcmBuffer buffer, AVAudioTime when) =>
                {
                    liveSpeechRequest.Append(buffer);
                });
                recording = true;

                audioEngine.Prepare();
                audioEngine.StartAndReturnError(out NSError error);
                if (error != null)
                {
                    return;
                }

                Timer timer = new Timer(2000);
                timer.Start();
                timer.Elapsed  += EndRecognition;
                RecognitionTask = speechRecognizer.GetRecognitionTask(liveSpeechRequest,
                                                                      (SFSpeechRecognitionResult result, NSError err) =>
                {
                    if (err != null)
                    {
                        Recorded?.Invoke("");
                        return;
                    }
                    else
                    {
                        lastSpokenString = result.BestTranscription.FormattedString;
                        timer.Stop();
                        timer.Interval = 2000;
                        timer.Start();
                    }
                });
            }
            else
            {
                Recorded?.Invoke("");
            }
        }
Exemple #7
0
        public void OnActivityResult(Result result, Intent data)
        {
            if (result == Result.Ok)
            {
                var    matches = data.GetStringArrayListExtra(RecognizerIntent.ExtraResults);
                string text;
                if (matches.Count != 0)
                {
                    text = matches[0];
                }
                else
                {
                    text = "No speech was recognised";
                }

                Recorded?.Invoke(text);
                var activity = MainActivity.Instance;
                activity.SpeechActivityResult -= OnActivityResult;
            }
        }