Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. C# Text to Speech

C# Text to Speech

Scheduled Pinned Locked Moved C#
csharpperformancehelpcareer
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Pavlex4
    wrote on last edited by
    #1

    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");
        }
    
    M 1 Reply Last reply
    0
    • P Pavlex4

      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");
          }
      
      M Offline
      M Offline
      Michael_Davies
      wrote on last edited by
      #2

      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.

      P B 2 Replies Last reply
      0
      • M Michael_Davies

        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.

        P Offline
        P Offline
        Pavlex4
        wrote on last edited by
        #3

        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!!!!

        P P 2 Replies Last reply
        0
        • P Pavlex4

          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!!!!

          P Offline
          P Offline
          Pavlex4
          wrote on last edited by
          #4

          ????

          1 Reply Last reply
          0
          • P Pavlex4

            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!!!!

            P Offline
            P Offline
            Pete OHanlon
            wrote on last edited by
            #5

            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 the new Synthesizer() call.

            This space for rent

            1 Reply Last reply
            0
            • M Michael_Davies

              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.

              B Offline
              B Offline
              Bernhard Hiller
              wrote on last edited by
              #6

              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.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups