-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathForm1.cs
More file actions
132 lines (126 loc) · 6.26 KB
/
Form1.cs
File metadata and controls
132 lines (126 loc) · 6.26 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
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
using Microsoft.CognitiveServices.Speech;
using Microsoft.CognitiveServices.Speech.Audio;
using NeuroTTS.Properties;
using System;
using System.Windows.Forms;
namespace NeuroTTS
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
comboBox1.SelectedIndex = 0;
if (Settings.Default.REGION == "" || Settings.Default.API_KEY == "")
{
Form2 form2 = new Form2();
form2.ShowDialog();
}
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboBox1.SelectedIndex > 0)
{
comboBox1.SelectedIndex = 0;
}
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
label2.Text = "Current words: " + textBox1.Text.Length.ToString();
label3.Visible = textBox1.Text.Length > 2000 ? true : false;
}
private void button1_Click(object sender, EventArgs e)
{
if (Settings.Default.REGION == "" || Settings.Default.API_KEY == "")
{
MessageBox.Show("Please setup your API key first!", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
Form2 form2 = new Form2();
form2.ShowDialog();
return;
}
string ssml = "<speak version='1.0' xml:lang='en-US'><voice name='en-US-AshleyNeural'><express-as style='chat'><prosody pitch='+25%'>Hello! I'm Neuro. Someone tell vedal there is a problem with my AI.</prosody></express-as></voice></speak>";
SpeechConfig speechConfig = SpeechConfig.FromSubscription(Settings.Default.API_KEY, Settings.Default.REGION);
button1.Enabled = false;
using (SpeechSynthesizer speechSynthesizer = new SpeechSynthesizer(speechConfig))
{
SpeechSynthesisResult result = speechSynthesizer.SpeakSsmlAsync(ssml).Result;
if (result.Reason == ResultReason.SynthesizingAudioCompleted)
{
button1.Enabled = true;
MessageBox.Show("Test successful!", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
button1.Enabled = true;
MessageBox.Show("Test failed: " + result.Reason.ToString(), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
if (MessageBox.Show("It looks like the API configuration didn't take effect. Have you rotated the API key?", this.Text, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
Form2 form2 = new Form2();
form2.ShowDialog();
}
}
}
}
private void button2_Click(object sender, EventArgs e)
{
if (Settings.Default.REGION == "" || Settings.Default.API_KEY == "")
{
MessageBox.Show("Please setup your API key first!", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
Form2 form2 = new Form2();
form2.ShowDialog();
return;
}
if (string.IsNullOrWhiteSpace(textBox1.Text))
{
MessageBox.Show("Please enter some text to synthesize!", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (textBox1.Text.Length > 2000)
{
if (MessageBox.Show("The word count exceeds 2000. This will cause a lot of API quota and times. Do you want to continue?", this.Text, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
{
return;
}
}
using (SaveFileDialog fileDialog = new SaveFileDialog())
{
fileDialog.Filter = "WAV files (*.wav)|*.wav";
fileDialog.DefaultExt = "wav";
fileDialog.AddExtension = true;
fileDialog.Title = "Save synthesized audio";
if (fileDialog.ShowDialog() == DialogResult.OK)
{
string ssml = "<speak version='1.0' xml:lang='en-US'><voice name='en-US-AshleyNeural'><express-as style='chat'><prosody pitch='+25%'>" + textBox1.Text + "</prosody></express-as></voice></speak>";
SpeechConfig speechConfig = SpeechConfig.FromSubscription(Settings.Default.API_KEY, Settings.Default.REGION);
button1.Enabled = false;
using (SpeechSynthesizer speechSynthesizer = new SpeechSynthesizer(speechConfig, null as AudioConfig))
{
SpeechSynthesisResult result = speechSynthesizer.SpeakSsmlAsync(ssml).Result;
if (result.Reason == ResultReason.SynthesizingAudioCompleted)
{
using (AudioDataStream audioDataStream = AudioDataStream.FromResult(result))
{
audioDataStream.SaveToWaveFileAsync(fileDialog.FileName).Wait();
button1.Enabled = true;
MessageBox.Show("Generation complete!", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
else
{
button1.Enabled = true;
MessageBox.Show("Generation failed: " + result.Reason.ToString(), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
MessageBox.Show("Generation failed, please try again. If this continues to happen, you can click the \"Test\" button to test your API key configuration." + result.Reason.ToString(), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
else
{
MessageBox.Show("Generation cancelled.", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}
}