C# Text to Speech
-
I have created app that converts text to speech but whatever gender I select it speaks same voice!!!
SpeechSynthesizer ss;
private void Form1_Load(object sender, EventArgs e)
{
ss = new SpeechSynthesizer();
}private void button1\_Click(object sender, EventArgs e) { //Read (button\_click) ss.Rate = SpeedTrackBar.Value; //sets speed ss.Volume = VolumeTrackBar.Value; //sets volume try { switch (comboBox1.SelectedIndex) { case 0: ss.SelectVoiceByHints(VoiceGender.NotSet); break; case 1: ss.SelectVoiceByHints(VoiceGender.Male); break; case 2: ss.SelectVoiceByHints(VoiceGender.Female); break; case 3: ss.SelectVoiceByHints(VoiceGender.Neutral); break; default: break; } ss.SpeakAsync(textBox1.Text); } catch(Exception ex) { MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error); } } private void button2\_Click(object sender, EventArgs e) { //Pause (button\_click) ss.Pause(); } private void button3\_Click(object sender, EventArgs e) { //Continue (button\_click) ss.Resume(); } private void button4\_Click(object sender, EventArgs e) { //Record (button\_click) SpeechSynthesizer ss = new SpeechSynthesizer(); ss.Rate = SpeedTrackBar.Value; ss.Volume = VolumeTrackBar.Value; SaveFileDialog sfd = new SaveFileDialog(); sfd.Filter = "Wave Files| \*.wav"; sfd.ShowDialog(); ss.SetOutputToWaveFile(sfd.FileName); ss.Speak(textBox1.Text); ss.SetOutputToDefaultAudioDevice(); MessageBox.Show("Recording Completed..", "T2S"); }
-
I have created app that converts text to speech but whatever gender I select it speaks same voice!!!
SpeechSynthesizer ss;
private void Form1_Load(object sender, EventArgs e)
{
ss = new SpeechSynthesizer();
}private void button1\_Click(object sender, EventArgs e) { //Read (button\_click) ss.Rate = SpeedTrackBar.Value; //sets speed ss.Volume = VolumeTrackBar.Value; //sets volume try { switch (comboBox1.SelectedIndex) { case 0: ss.SelectVoiceByHints(VoiceGender.NotSet); break; case 1: ss.SelectVoiceByHints(VoiceGender.Male); break; case 2: ss.SelectVoiceByHints(VoiceGender.Female); break; case 3: ss.SelectVoiceByHints(VoiceGender.Neutral); break; default: break; } ss.SpeakAsync(textBox1.Text); } catch(Exception ex) { MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error); } } private void button2\_Click(object sender, EventArgs e) { //Pause (button\_click) ss.Pause(); } private void button3\_Click(object sender, EventArgs e) { //Continue (button\_click) ss.Resume(); } private void button4\_Click(object sender, EventArgs e) { //Record (button\_click) SpeechSynthesizer ss = new SpeechSynthesizer(); ss.Rate = SpeedTrackBar.Value; ss.Volume = VolumeTrackBar.Value; SaveFileDialog sfd = new SaveFileDialog(); sfd.Filter = "Wave Files| \*.wav"; sfd.ShowDialog(); ss.SetOutputToWaveFile(sfd.FileName); ss.Speak(textBox1.Text); ss.SetOutputToDefaultAudioDevice(); MessageBox.Show("Recording Completed..", "T2S"); }
You have a global variable speachsynthesizer (ss) that you configure on button1 then on the actual record button4 you create a new LOCAL variable speachsynthesizer of the same name (ss) that you do not configure but use that to speak the text.
-
You have a global variable speachsynthesizer (ss) that you configure on button1 then on the actual record button4 you create a new LOCAL variable speachsynthesizer of the same name (ss) that you do not configure but use that to speak the text.
-
The Record button is used to save text inside textBox! I have replaced varibles from button1 and button4 with SpeechSynthesizer ss = new SpeechSynthesizer(); as global varible but still not working!!!!
-
The Record button is used to save text inside textBox! I have replaced varibles from button1 and button4 with SpeechSynthesizer ss = new SpeechSynthesizer(); as global varible but still not working!!!!
If you now have two lines like this in your code, that's not correct. You only need to declare it once, in the Form_Load. So, you have a member variable called ss (that's a poor choice for a name - why not just call it speechSynthesizer), and in your Form_Load you have
ss = new SpeechSynthesizer
. That's it - that's the only place you should see thenew Synthesizer()
call.This space for rent
-
You have a global variable speachsynthesizer (ss) that you configure on button1 then on the actual record button4 you create a new LOCAL variable speachsynthesizer of the same name (ss) that you do not configure but use that to speak the text.
Exactly. That's a bug in the language specification of C# which can cause bugs in the software which are very hard to find. Fortunately, ReSharper can issue a warning. And a well designed style guide should suggest different styles for fields vs. local variables.