.Net Maui
I'm working off of the example here:
https://learn.microsoft.com/en-us/dotnet/communitytoolkit/maui/essentials/speech-to-text?tabs=ios
The performance of this ISpeechToText from the CommunityToolkit is horrendous. It's a minute before it times out and continues with both the listenAsync and the event driven method. It seems it's hitting a timeout vs not detecting speech ending properly.
I've adjusted the listen method to this for testing:
async Task StartListener(CancellationToken cancellationToken)
{
var isGranted = await speechToText.RequestPermissions(cancellationToken);
if (!isGranted)
{
await Toast.Make("Permission not granted").Show(CancellationToken.None);
return;
}
do
{
Stopwatch sw = Stopwatch.StartNew();
var recognitionResult = await speechToText.ListenAsync(
CultureInfo.GetCultureInfo(Language),
new Progress<string>(partialText =>
{
RecognitionText += partialText + " ";
}), cancellationToken);
sw.Stop();
if (recognitionResult.IsSuccessful)
{
RecognitionText = recognitionResult.Text;
Debug.WriteLine("Success " + RecognitionText + " Time " + sw.Elapsed);
RecognitionText = string.Empty;
}
else
{
Debug.WriteLine("failed - Time " + sw.Elapsed);
}
} while (!cancellationToken.IsCancellationRequested);
}
When I run this on my iPad, it loads and waits. I speak, "This is a test" and then wait for 1 minute when it responds below in the debug window with the success message and time it took. It doesn't seem to matter if I say this phrase quickly after start or waiting for 30 seconds, it still seems to take a minute.
[0:] Success This is a test Time 00:01:00.9459623
I looked into the method call and there doesn't appear to be anything that can be set to affect the timer in iSpeechToText.
Any ideas one why this is happening and how to speed things up? Having a user wait a minute for a response will not work.
I had the same problem. Try the below. It cancels the listen task after a specified period of time and then handles whatever audio has been captured during that time. Hope it helps.
public async Task BeginListen()
{
var isGranted = await speechToText.RequestPermissions(CancellationToken.None);
if (!isGranted)
{
await Toast.Make("Permission not granted").Show(CancellationToken.None);
return;
}
IsListening = true;
CancellationTokenSource s_cts = new CancellationTokenSource();
s_cts.CancelAfter(5000);
var cultureInfo = CultureInfo.CurrentCulture;
var recognitionResult = await SpeechToText.ListenAsync(
cultureInfo,
new Progress<string>(partialText =>
{
RecognitionText += partialText + " ";
}), s_cts.Token);
if (recognitionResult.IsSuccessful || recognitionResult.Exception is TaskCanceledException)
{
if (!string.IsNullOrEmpty(RecognitionText))
await Toast.Make("You said " + RecognitionText).Show(CancellationToken.None);
RecognitionText = string.Empty;
}
else
{
await Toast.Make(recognitionResult.Exception?.Message ?? "Unable to recognize speech").Show(CancellationToken.None);
}
IsListening = false;
}