-
Notifications
You must be signed in to change notification settings - Fork 493
Expand file tree
/
Copy pathSpeechToTextImplementation.ios.cs
More file actions
43 lines (33 loc) · 1.26 KB
/
SpeechToTextImplementation.ios.cs
File metadata and controls
43 lines (33 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
using System.Diagnostics.CodeAnalysis;
using Speech;
namespace CommunityToolkit.Maui.Media;
/// <inheritdoc />
public sealed partial class SpeechToTextImplementation
{
[MemberNotNull(nameof(liveSpeechRequest))]
async Task InternalStartListeningAsync(SpeechToTextOptions options, CancellationToken cancellationToken)
{
speechRecognizer = new SFSpeechRecognizer(NSLocale.FromLocaleIdentifier(options.Culture.Name));
if (!speechRecognizer.Available)
{
throw new ArgumentException("Speech recognizer is not available");
}
liveSpeechRequest = new SFSpeechAudioBufferRecognitionRequest
{
ShouldReportPartialResults = options.ShouldReportPartialResults
};
InitializeAvAudioSession(out _);
var node = audioEngine.InputNode;
var recordingFormat = node.GetBusOutputFormat(audioEngineBusTap);
node.InstallTapOnBus(audioEngineBusTap, 1024, recordingFormat, (buffer, _) => liveSpeechRequest.Append(buffer));
audioEngine.Prepare();
audioEngine.StartAndReturnError(out var error);
if (error is not null)
{
throw new NSErrorException(error);
}
cancellationToken.ThrowIfCancellationRequested();
silenceTimer = await CreateSilenceTimer(options, cancellationToken);
recognitionTask = CreateSpeechRecognizerTask(speechRecognizer, liveSpeechRequest);
}
}