-
Notifications
You must be signed in to change notification settings - Fork 493
Expand file tree
/
Copy pathOfflineSpeechToTextImplementation.ios.cs
More file actions
53 lines (42 loc) · 1.6 KB
/
OfflineSpeechToTextImplementation.ios.cs
File metadata and controls
53 lines (42 loc) · 1.6 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
44
45
46
47
48
49
50
51
52
53
using System.Diagnostics.CodeAnalysis;
using System.Runtime.Versioning;
using Speech;
namespace CommunityToolkit.Maui.Media;
/// <inheritdoc />
public sealed partial class OfflineSpeechToTextImplementation
{
[MemberNotNull(nameof(liveSpeechRequest))]
[SupportedOSPlatform("ios13.0")]
[SupportedOSPlatform("maccatalyst")]
async Task InternalStartListening(SpeechToTextOptions options, CancellationToken token = default)
{
if (!UIDevice.CurrentDevice.CheckSystemVersion(13, 0))
{
throw new NotSupportedException("Offline listening is supported on iOS 13 and later");
}
speechRecognizer = new SFSpeechRecognizer(NSLocale.FromLocaleIdentifier(options.Culture.Name));
speechRecognizer.SupportsOnDeviceRecognition = true;
if (!speechRecognizer.Available)
{
throw new InvalidOperationException("Speech recognizer is not available");
}
liveSpeechRequest = new SFSpeechAudioBufferRecognitionRequest()
{
ShouldReportPartialResults = options.ShouldReportPartialResults,
RequiresOnDeviceRecognition = true
};
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);
}
token.ThrowIfCancellationRequested();
silenceTimer = await CreateSilenceTimer(options, token);
recognitionTask = CreateSpeechRecognizerTask(speechRecognizer, liveSpeechRequest);
}
}