-
Notifications
You must be signed in to change notification settings - Fork 0
/
SpeechRecognition.cs
139 lines (123 loc) · 5.87 KB
/
SpeechRecognition.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
using System;
using System.Threading.Tasks;
using Microsoft.CognitiveServices.Speech;
using Microsoft.CognitiveServices.Speech.Audio;
using Microsoft.CognitiveServices.Speech.Translation;
using SpeechToText.ViewModels;
using static SpeechToText.Logging;
namespace SpeechToText
{
public class SpeechRecognition
{
private AudioConfig _audioConfig;
private TranslationRecognizer _translator;
private bool _isReady = false;
/// <summary>
/// Create the configuration and
/// </summary>
public void Initialise()
{
if (AudiosourceViewModel.Instance.SelectedDevice == null)
{
ShowMessage("Selecteer een audiobron");
}
SpeechTranslationConfig translationConfig = SpeechTranslationConfig.FromSubscription(
Settings.Instance.AzureKey, Settings.Instance.AzureRegion);
translationConfig.SpeechRecognitionLanguage = "nl-NL";
translationConfig.AddTargetLanguage("uk");
// https://stackoverflow.com/questions/3992798/how-to-programmatically-get-the-current-audio-level
// https://learn.microsoft.com/en-us/dotnet/standard/native-interop/pinvoke
_audioConfig = AudioConfig.FromDefaultMicrophoneInput();
//_audioConfig = AudioConfig.FromMicrophoneInput(AudiosourceViewModel.Instance.SelectedDevice.Id);
_translator = new TranslationRecognizer(translationConfig, _audioConfig);
_translator.Recognized += OnSpeechTranslated;
_translator.SessionStarted += (object sender, SessionEventArgs e)
=> ShowMessage("Spraakherkenning gestart");
_translator.SessionStopped += (object sender, SessionEventArgs e)
=> ShowMessage("Spraakherkenning gestopt");
_translator.Canceled += OnTranslationError;
_isReady = true;
}
public void Reset()
{
_translator?.Dispose();
_audioConfig?.Dispose();
Initialise();
}
public void Start()
{
if (!_isReady || _translator is null)
Reset();
_translator.StartContinuousRecognitionAsync();
}
public void Stop()
{
_translator?.StopContinuousRecognitionAsync();
}
public void OnTranslationError(object sender, TranslationRecognitionCanceledEventArgs e)
{
PlaystateViewModel.ChangeFromBackgroundthread(isPlaying: false);
_isReady = false; // Translator has to be restarted, possibly with new settings
switch (e.ErrorCode)
{
case CancellationErrorCode.NoError:
break;
case CancellationErrorCode.AuthenticationFailure:
case CancellationErrorCode.Forbidden:
Log($"{e.ErrorCode}, {e.ErrorDetails}", LogLevel.Exception);
ShowError("Authentificatie mislukt", "Instellingen openen", Settings.ShowSettings,
"Controleer de key-instelling en probeer opnieuw");
break;
case CancellationErrorCode.ConnectionFailure:
Log($"{e.ErrorCode}, {e.ErrorDetails}", LogLevel.Exception);
ShowError("Geen verbinding", "Instellingen openen", Settings.ShowSettings,
"Controleer de regio-instelling en probeer opnieuw");
break;
default:
Log($"{e.ErrorCode}, {e.ErrorDetails}", LogLevel.Exception);
ShowError($"Er is een fout opgetreden: {e.ErrorCode}",
"Check instellingen misschien?", Settings.ShowSettings,
description: $"Details: {e.ErrorDetails}");
break;
}
}
private void OnSpeechTranslated(object sender, TranslationRecognitionEventArgs e)
{
TranslationRecognitionResult result = e.Result;
switch (result.Reason)
{
case ResultReason.TranslatedSpeech:
ShowMessage($"Recognized: {result.Text}",
$"Translation: {result.Translations[_translator.TargetLanguages[0]]}");
Log($"Recognized: {result.Text}\n Translation: " +
$"{result.Translations[_translator.TargetLanguages[0]]}");
Task.Run(async () => await App.TelegramConnection.Send(result.Translations[_translator.TargetLanguages[0]]));
// TODO: do something with the translation
break;
case ResultReason.NoMatch:
ShowMessage($"Geen spraak herkend");
Log($"No speech recognized");
break;
case ResultReason.Canceled:
CancellationDetails details = CancellationDetails.FromResult(result);
ShowMessage($"Geen vertalingsresultaat: {details.Reason}",
$"Error: {details.ErrorCode}, details: {details.ErrorDetails}");
Log($"No result: {details.Reason}. Error: {details.ErrorCode}, " +
$"details: {details.ErrorDetails}", LogLevel.Warning);
break;
default:
Log($"Default case, result reason is {result.Reason}");
break;
}
}
private static void ShowMessage(string message, string subtext = "")
{
LogCollectionViewModel.AddLogFromBackgroundthread(message, subtext);
}
private static void ShowError(string title, string actionmessage, Action action, string description = "")
{
Log("SpeechRecognition.ShowError: " + title);
ErrormessageViewModel.ShowFromBackgroundthread(title, actionmessage, action, description);
}
}
}