首页 > 解决方案 > 如何在 TextToSpeech 和 SpeechRecognizer 之间实现 AcousticEchoCanceler

问题描述

我的成就是 TextToSpeech 会说一些段落,同时 SpeechRecognizer 正在监听用户输入。如果 SpeechRecognition 检测到任何文本,则 TextToSpeech 必须停止。

public class SpeechService : ISpeechRecognizer
 {
     protected Subject<bool> ListenSubject { get; } = new Subject<bool>();
     readonly object syncLock = new object();
     [Obsolete]
     public IObservable<string> Listen(Action action = null) => Observable.Create<string>(ob =>
          {
              speechRecognizer = SpeechRecognizer.CreateSpeechRecognizer(Application.Context);
              var listener = new SpeechRecognitionListener(); 
              listener.ReadyForSpeech = () => this.ListenSubject.OnNext(true);
              listener.PartialResults = sentence =>
              {
                      if (action != null)
                      {
                          action.Invoke();
                      }
                      lock (this.syncLock)
                      {
                          sentence = sentence.Trim();
                          if (currentIndex > sentence.Length)
                              currentIndex = 0;
                          var newPart = sentence.Substring(currentIndex);
                          currentIndex = sentence.Length;
                          final = sentence;
                      }
          }
          listener.EndOfSpeech = () =>
          {
             ob.OnNext(final);
             ob.OnCompleted();
             this.ListenSubject.OnNext(false);
          }
          speechRecognizer.SetRecognitionListener(listener);
          speechRecognizer.StartListening(this.CreateSpeechIntent(true));
          return () =>
          {
              audioManager.SetStreamMute(Stream.Notification, false);
              stop = true;
              speechRecognizer?.StopListening();
              speechRecognizer?.Destroy();
              this.ListenSubject.OnNext(false);
          };
     });
     protected virtual Intent CreateSpeechIntent(bool partialResults)
     {
         var intent = new Intent(RecognizerIntent.ActionRecognizeSpeech);
         intent.PutExtra(RecognizerIntent.ExtraLanguagePreference, Java.Util.Locale.Default);
         intent.PutExtra(RecognizerIntent.ExtraLanguage, Java.Util.Locale.Default);
         intent.PutExtra(RecognizerIntent.ExtraLanguageModel, RecognizerIntent.LanguageModelFreeForm);
         intent.PutExtra(RecognizerIntent.ExtraCallingPackage, Application.Context.PackageName);
         intent.PutExtra(RecognizerIntent.ExtraPartialResults, partialResults);
         return intent;
     }
 }

这是我的语音识别电话

 public class MyViewModel : ReactiveObject
     {
         CancellationTokenSource textToSpeechCancellationToken;
         public MyViewModel ()
         {
             speak("Xamarin is a Microsoft-owned San Francisco-based software company founded in May 2011 by the engineers that created Mono, Xamarin.Android and Xamarin.iOS, which are cross-platform implementations of the Common Language Infrastructure and Common Language Specifications.");
             Action actionAfterSpeechDetect = delegate
             {
                 if (textToSpeechCancellationToken != null && !textToSpeechCancellationToken.IsCancellationRequested)
                 {
                     textToSpeechCancellationToken.Cancel();
                 }
             };
             using (var cancelSrc = new CancellationTokenSource())
             {
                 output = await DependencyService.Get<ISpeechRecognizer>().Listen(actionAfterSpeechDetect).ToTask(cancelSrc.Token);
             }    
         }
         public void speak(string text)
         {
             Task.Run(async () =>
             {
                 textToSpeechCancellationToken = new CancellationTokenSource();
                 await TextToSpeech.SpeakAsync(text, textToSpeechCancellationToken.Token);
             });
         }
     }

对于上述实现,如果我什么都没说,那么 TextToSpeech 将停止。因为语音识别从说话者输出中检测文本。所以,我会知道如何在 Xamarin Android 中忽略移动扬声器输出 (TextToSpeech) 以进行语音识别。

我找到了 AcousticEchoCanceler,但我不知道如何使用 TextToSpeech 和 SpeechRecognizer 来实现它。请帮我。

标签: c#xamarin.formsxamarin.android

解决方案


您需要实施UtteranceProgressListener,以便您知道语音何时完成。在那之前你不需要听。


推荐阅读