c# - 如何在 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 来实现它。请帮我。
解决方案
您需要实施UtteranceProgressListener
,以便您知道语音何时完成。在那之前你不需要听。
推荐阅读
- java - 正则表达式双运算符是单计数
- java - Android:使用未经检查或不安全的操作
- flutter - 如何动态地对 PageView.Builder 项目的移除进行动画处理
- google-apps-script - Google 表格:根据单元格值隐藏行 是/否
- api - 如何使用 Foswiki API 从外部系统访问表单元数据(单元格内容)
- asp.net - Elmah 没有看到数据库
- mysql - 用于 nil:NilClass 的 Rails 未定义方法“电子邮件”
- azure - 想要创建 Azure 逻辑应用表达式
- javascript - package.json - 如何确定安装了模块的哪些部分
- c - 替换整个单词而不是C中字符串中的子字符串