-
Notifications
You must be signed in to change notification settings - Fork 0
/
Form1.cs
209 lines (164 loc) · 7.23 KB
/
Form1.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
using Speech_To_Text.Class;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Windows.Forms;
namespace Speech_To_Text
{
public partial class Form1 : Form
{
private string speech_apiKey;
private string translator_apiKey;
private string region;
private Speech speech;
public Form1()
{
InitializeComponent();
// Get the API key and region from the environment variables
speech_apiKey = Environment.GetEnvironmentVariable("Speech_API_KEY");
translator_apiKey = Environment.GetEnvironmentVariable("Translator_API_KEY");
region = Environment.GetEnvironmentVariable("Region");
}
private async void buttStart_Click(object sender, EventArgs e)
{
//if the button is clicked in first time, start the background worker
//if the button is clicked again, stop the background worker
if (buttStart.Text == "Start")
{
backgroundWorker1.RunWorkerAsync();
buttStart.Text = "Stop";
}
else
{
buttStart.Text = "Start";
backgroundWorker1.CancelAsync();
}
}
private async void backgroundWorker1_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
{
var selectedFromValue = "";
var selectedToValue = "";
if (cbFrom.InvokeRequired)
{
cbFrom.Invoke(new MethodInvoker(delegate ()
{
selectedFromValue = (string)cbFrom.SelectedValue;
}));
}
else
{
selectedFromValue = (string)cbFrom.SelectedValue;
}
if (cbTo.InvokeRequired)
{
cbTo.Invoke(new MethodInvoker(delegate ()
{
selectedToValue = (string)cbTo.SelectedValue;
}));
}
else
{
selectedToValue = (string)cbTo.SelectedValue;
}
// Create a new instance of the Speech class and the Translation class
var speechLanguage = Languages.speechLanguages(selectedFromValue.ToString());
var speech = new Speech(speech_apiKey, region, speechLanguage);
var translation = new Translation(translator_apiKey, region);
// Start the speech recognition
await speech.StartMyContinuousRecognitionAsync();
// Loop until the speech recognition returns a value
while (true)
{
if (speech.Text != null)
{
if (rbTranslation.Checked)
{
await translation.TranslateTextAsync(speech.Text, selectedFromValue.ToString(), selectedToValue.ToString());
}
if (buttStart.InvokeRequired)
{
buttStart.BeginInvoke(new Action(() => { buttStart.Text = "Start"; }));
}
// Check if rbSpeech is Checked
if (rbSpeech.Checked)
{
var SpText = Convert.ToString(speech.Text, CultureInfo.InvariantCulture);
//if the CheckBox is checked, show the text in the message box
if (cbShowText.Checked)
{
MessageBox.Show(SpText, "Speech");
}
//if the CheckBox is checked, copy the text to the clipboard
if (cbCopyText.Checked)
{
TheText theText = new TheText();
theText.CopyText(SpText);
}
}
// Check if rbTranslation is Checked
if (rbTranslation.Checked)
{
//Convert change the text to string that can be shown in the message box
var speechText = Convert.ToString(speech.Text, CultureInfo.InvariantCulture);
var trText = Convert.ToString(translation.TrText.text, CultureInfo.InvariantCulture);
var trlanguage = Convert.ToString(translation.TrText.to, CultureInfo.InvariantCulture);
//if the CheckBox is checked, show the text in the message box
if (cbShowText.Checked)
{
MessageBox.Show($"{speechText}\n{trText}", $"From: {selectedFromValue} To: {selectedToValue}");
}
//if the CheckBox is checked, copy the text to the clipboard
if (cbCopyText.Checked)
{
TheText theText = new TheText();
theText.CopyText(trText);
}
}
// Clear the text
await speech.StopMyContinuousRecognitionAsync();
break;
}
//if background worker was canceled, stop the speech recognition
if (backgroundWorker1.CancellationPending)
{
e.Cancel = true;
await speech.StopMyContinuousRecognitionAsync();
Console.WriteLine("Speech recognition stopped");
backgroundWorker1.Dispose();
break;
}
}
}
private void Form1_Load(object sender, EventArgs e)
{
// Add the languages to the combo boxes
var cb1Items = new List<Languages>(Languages.Items);
var cb2Items = new List<Languages>(Languages.Items);
cbFrom.DataSource = cb1Items;
cbFrom.DisplayMember = "Text";
cbFrom.ValueMember = "Value";
cbTo.DataSource = cb2Items;
cbTo.DisplayMember = "Text";
cbTo.ValueMember = "Value";
//Loading settings
rbSpeech.Checked = Properties.Settings.Default.rbSpeech;
rbTranslation.Checked = Properties.Settings.Default.rbTranslation;
cbCopyText.Checked = Properties.Settings.Default.cbCopyText;
cbShowText.Checked = Properties.Settings.Default.cbShowText;
cbFrom.SelectedIndex = Properties.Settings.Default.cbFrom;
cbTo.SelectedIndex = Properties.Settings.Default.cbTo;
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
//Saving settings
Properties.Settings.Default.rbSpeech = rbSpeech.Checked;
Properties.Settings.Default.rbTranslation = rbTranslation.Checked;
Properties.Settings.Default.cbCopyText = cbCopyText.Checked;
Properties.Settings.Default.cbShowText = cbShowText.Checked;
Properties.Settings.Default.cbFrom = cbFrom.SelectedIndex;
Properties.Settings.Default.cbTo = cbTo.SelectedIndex;
// Save the settings
Properties.Settings.Default.Save();
}
}
}