-
Notifications
You must be signed in to change notification settings - Fork 493
Expand file tree
/
Copy pathSpeechToTextImplementation.macos.cs
More file actions
60 lines (47 loc) · 1.8 KB
/
SpeechToTextImplementation.macos.cs
File metadata and controls
60 lines (47 loc) · 1.8 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
54
55
56
57
58
59
60
using System.Diagnostics.CodeAnalysis;
using AVFoundation;
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 audioSession);
var mode = audioSession.AvailableModes.Contains("AVAudioSessionModeMeasurement")
? AVAudioSessionMode.Measurement
: AVAudioSessionMode.Default;
audioSession.SetMode(mode, out var audioSessionError);
if (audioSessionError is not null)
{
throw new NSErrorException(audioSessionError);
}
audioSession.SetActive(true, AVAudioSessionSetActiveOptions.NotifyOthersOnDeactivation, out audioSessionError);
if (audioSessionError is not null)
{
throw new NSErrorException(audioSessionError);
}
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);
}
}