-
Notifications
You must be signed in to change notification settings - Fork 2
/
SynthesizerUIHelper.cs
176 lines (156 loc) · 5.98 KB
/
SynthesizerUIHelper.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
using CefSharp.WinForms;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Speech.Synthesis;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Newtonsoft.Json;
using System.Dynamic;
namespace Synthesizer.net
{
class SynthesizerUIHelper
{
private SpeechSynthesizer sintetizador = new SpeechSynthesizer();
private static ChromiumWebBrowser Chrome = null;
private static Synthesizer ChromeForm = null;
/// <summary>
/// Admin constructor. Access chrome and form from another thread
/// </summary>
/// <param name="browser">A ChromiumWebBrowser object</param>
/// <param name="formulario">A (your formular) object</param>
public SynthesizerUIHelper(ChromiumWebBrowser browser, Synthesizer formulario)
{
if (browser is ChromiumWebBrowser)
{
Chrome = browser;
}
else
{
throw new Exception("The first parameter of the class must be a ChromiumWebBrowser object. "+ browser.GetType() + " given.");
}
if (formulario is Synthesizer)
{
ChromeForm = formulario;
}
else
{
throw new Exception("The second parameter of the class must be a (Your formular) object. " + formulario.GetType() + " given.");
}
}
public void speak(string content = "")
{
try
{
sintetizador.SelectVoice(Properties.Settings.Default.voicename);
sintetizador.SpeakProgress += new EventHandler<SpeakProgressEventArgs>(synthesizer_SpeakProgress);
sintetizador.SpeakCompleted += new EventHandler<SpeakCompletedEventArgs>(synthesizer_SpeakCompleted);
sintetizador.SetOutputToDefaultAudioDevice();
sintetizador.SpeakAsync(content);
}
catch (InvalidOperationException e) { Console.WriteLine(e.Message); }
}
private void synthesizer_SpeakCompleted(object sender, SpeakCompletedEventArgs e)
{
this.InjectScript("$('#action-buttons-progress').text('');");
//this.InjectScript("$('#action-buttons-progress').html('<b>" + e.Text + "</b>')");
//Console.WriteLine("SpeakProgress: AudioPosition=" + e.AudioPosition + ",\tCharacterPosition=" + e.CharacterPosition + ",\tCharacterCount=" + e.CharacterCount + ",\tText=" + e.Text);
}
private void synthesizer_SpeakProgress(object sender, SpeakProgressEventArgs e)
{
this.InjectScript("$('#action-buttons-progress').html('<b>" + e.Text + "</b>')");
Console.WriteLine("SpeakProgress: AudioPosition=" + e.AudioPosition + ",\tCharacterPosition=" + e.CharacterPosition + ",\tCharacterCount=" + e.CharacterCount + ",\tText=" + e.Text);
}
private void InjectScript(string script)
{
Chrome.ExecuteScriptAsync(script);
}
public void stop()
{
try
{
sintetizador.SpeakAsyncCancelAll();
}
catch (ObjectDisposedException) { }
}
/// <summary>
/// Set the sinthesizer volume with an integer (0 - 100)
/// </summary>
/// <param name="level"></param>
public void setVolume(int level)
{
if (level > 100)
{
sintetizador.Volume = 100;
}
else if (level < 0)
{
sintetizador.Volume = 0;
}
else
{
sintetizador.Volume = level;
}
}
public void resume()
{
sintetizador.Resume();
}
public void pause()
{
sintetizador.Pause();
}
/// <summary>
/// Send speechSynthesizer output to a .wav file
/// </summary>
/// <param name="content"></param>
public void toWAVFile(string content = "")
{
sintetizador.SelectVoice(Properties.Settings.Default.voicename);
string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
sintetizador.SetOutputToWaveFile(path + "/test.wav");
sintetizador.Speak(content);
MessageBox.Show("File exported succesfully !",".wav File succesfully exported",MessageBoxButtons.OK,MessageBoxIcon.Information);
sintetizador.SetOutputToDefaultAudioDevice();
}
public void listAvailableVoicesConfigurator()
{
var list = new List<dynamic>();
using (sintetizador)
{
foreach (InstalledVoice voice in sintetizador.GetInstalledVoices())
{
dynamic VOZ = new ExpandoObject();
var info = voice.VoiceInfo;
VOZ.name = info.Name;
VOZ.culture = info.Culture;
VOZ.age = info.Age;
list.Add(VOZ);
}
}
string JSON = JsonConvert.SerializeObject(list);
this.InjectScript("AddVoiceToLayout('" +System.Net.WebUtility.UrlEncode(JSON) + "');");
}
public void saveConfiguration(string name, string culture)
{
Properties.Settings.Default.voicename = name;
Properties.Settings.Default.culture = culture;
Properties.Settings.Default.firstUse = false;
Properties.Settings.Default.Save();
Application.Restart();
}
public void resetConfiguration()
{
Properties.Settings.Default.voicename = "";
Properties.Settings.Default.culture = "";
Properties.Settings.Default.firstUse = true;
Properties.Settings.Default.Save();
Application.Restart();
}
public void showDevTools()
{
Chrome.ShowDevTools();
}
}
}