Question

I would like to change the gender and age of the voice of System.Speech in c#. For example, a girl of 10 years but can not find any simple example to help me adjust the parameters.

Was it helpful?

Solution

First, check which voices you have installed by enumerating the GetInstalledVoices method of the SpeechSynthesizer class, and then use SelectVoiceByHints to select one of them:

using (SpeechSynthesizer synthesizer = new SpeechSynthesizer())
{
    // show installed voices
    foreach (var v in synthesizer.GetInstalledVoices().Select(v => v.VoiceInfo))
    {
        Console.WriteLine("Name:{0}, Gender:{1}, Age:{2}",
          v.Description, v.Gender, v.Age);
    }

    // select male senior (if it exists)
    synthesizer.SelectVoiceByHints(VoiceGender.Male, VoiceAge.Senior);

    // select audio device
    synthesizer.SetOutputToDefaultAudioDevice();

    // build and speak a prompt
    PromptBuilder builder = new PromptBuilder();
    builder.AppendText("Found this on Stack Overflow.");
    synthesizer.Speak(builder);
}

OTHER TIPS

first you need to intialise the reference speech using the add reference.

then create an event handler for the speak started then you can edit the paramemters inside that handler.

in the handler is where you can change the voice and age using the

synthesizer.SelectVoiceByHints(VoiceGender.Male , VoiceAge.Adult);
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Speech.Synthesis; // first import this package

    namespace textToSpeech
    {
        public partial class home : Form
        {
            public string s = "pran"; // storing string (pran) to s

            private void home_Load(object sender, EventArgs e)
                {
                    speech(s); // calling the function with a string argument
                }

            private void speech(string args) // defining the function which will accept a string parameter
                {
                    SpeechSynthesizer synthesizer = new SpeechSynthesizer();
                    synthesizer.SelectVoiceByHints(VoiceGender.Male , VoiceAge.Adult); // to change VoiceGender and VoiceAge check out those links below
                    synthesizer.Volume = 100;  // (0 - 100)
                    synthesizer.Rate = 0;     // (-10 - 10)
                    // Synchronous
                    synthesizer.Speak("Now I'm speaking, no other function'll work");
                    // Asynchronous
                    synthesizer.SpeakAsync("Welcome" + args); // here args = pran
                }       
         }
    }
  • It'll be better choice to use "SpeakAsync" because when "Speak" function is executing/running none of other function will work until it finishes it's work (personally recommended)

Change VoiceGender
Change VoiceAge

These age and gender is actually of no use. If you have many voices installed in your windows, then you may call specific voices by these parameters. Otherwise, its simply fake!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top