-
Notifications
You must be signed in to change notification settings - Fork 342
/
SpeechToTextImplementation.shared.cs
102 lines (85 loc) · 3.83 KB
/
SpeechToTextImplementation.shared.cs
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
using System.Globalization;
using Microsoft.Maui.ApplicationModel;
namespace CommunityToolkit.Maui.Media;
/// <inheritdoc cref="ISpeechToText"/>
public sealed partial class SpeechToTextImplementation : ISpeechToText
{
readonly WeakEventManager recognitionResultUpdatedWeakEventManager = new();
readonly WeakEventManager recognitionResultCompletedWeakEventManager = new();
readonly WeakEventManager speechToTextStateChangedWeakEventManager = new();
/// <inheritdoc />
public event EventHandler<SpeechToTextRecognitionResultUpdatedEventArgs> RecognitionResultUpdated
{
add => recognitionResultUpdatedWeakEventManager.AddEventHandler(value);
remove => recognitionResultUpdatedWeakEventManager.RemoveEventHandler(value);
}
/// <inheritdoc />
public event EventHandler<SpeechToTextRecognitionResultCompletedEventArgs> RecognitionResultCompleted
{
add => recognitionResultCompletedWeakEventManager.AddEventHandler(value);
remove => recognitionResultCompletedWeakEventManager.RemoveEventHandler(value);
}
/// <inheritdoc />
public event EventHandler<SpeechToTextStateChangedEventArgs> StateChanged
{
add => speechToTextStateChangedWeakEventManager.AddEventHandler(value);
remove => speechToTextStateChangedWeakEventManager.RemoveEventHandler(value);
}
/// <inheritdoc/>
public async Task<SpeechToTextResult> ListenAsync(CultureInfo culture, IProgress<string>? recognitionResult, CancellationToken cancellationToken)
{
try
{
cancellationToken.ThrowIfCancellationRequested();
var isPermissionGranted = await IsSpeechPermissionAuthorized(cancellationToken).ConfigureAwait(false);
if (!isPermissionGranted)
{
return new SpeechToTextResult(null, new Exception("Speech Recognizer Permission not granted"));
}
var finalResult = await InternalListenAsync(culture, recognitionResult, cancellationToken).ConfigureAwait(false);
return new SpeechToTextResult(finalResult, null);
}
catch (Exception e)
{
return new SpeechToTextResult(null, e);
}
}
/// <inheritdoc/>
public async Task StartListenAsync(CultureInfo culture, CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
var isPermissionGranted = await IsSpeechPermissionAuthorized(cancellationToken).ConfigureAwait(false);
if (!isPermissionGranted)
{
throw new PermissionException($"{nameof(Permissions)}.{nameof(Permissions.Microphone)} Not Granted");
}
await InternalStartListeningAsync(culture, cancellationToken).ConfigureAwait(false);
}
/// <inheritdoc/>
public Task StopListenAsync(CancellationToken cancellationToken) => InternalStopListeningAsync(cancellationToken);
void OnRecognitionResultUpdated(string recognitionResult)
{
recognitionResultUpdatedWeakEventManager.HandleEvent(this, new SpeechToTextRecognitionResultUpdatedEventArgs(recognitionResult), nameof(RecognitionResultUpdated));
}
void OnRecognitionResultCompleted(string recognitionResult)
{
recognitionResultCompletedWeakEventManager.HandleEvent(this, new SpeechToTextRecognitionResultCompletedEventArgs(recognitionResult), nameof(RecognitionResultCompleted));
}
void OnSpeechToTextStateChanged(SpeechToTextState speechToTextState)
{
speechToTextStateChangedWeakEventManager.HandleEvent(this, new SpeechToTextStateChangedEventArgs(speechToTextState), nameof(StateChanged));
}
#if !MACCATALYST && !IOS
/// <inheritdoc/>
public async Task<bool> RequestPermissions(CancellationToken cancellationToken)
{
var status = await Permissions.RequestAsync<Permissions.Microphone>().WaitAsync(cancellationToken).ConfigureAwait(false);
return status is PermissionStatus.Granted;
}
static async Task<bool> IsSpeechPermissionAuthorized(CancellationToken cancellationToken)
{
var status = await Permissions.CheckStatusAsync<Permissions.Microphone>().WaitAsync(cancellationToken).ConfigureAwait(false);
return status is PermissionStatus.Granted;
}
#endif
}