As part of a little side project, I wanted to have Windows speak some text is response to a call to a service. Initially I started to look at Cortana, but this seemed overkill for my initial needs (i.e. you need to write a bot, deploy it to Azure etc.), whereas System.Speech.Synthesis offers a simple API to use text to speech.
Getting started
It can be as simple as this, add the System.Speech assembly to your references then we can use the following
using (var speech = new SpeechSynthesizer())
{
speech.Volume = 100;
speech.Rate = -2;
speech.Speak("Hello World");
}
Volume takes a value between [0, 100] and Rate which is the speaking rate can range between [-10, 10].
Taking things a little further
We can also look at controlling other aspects of speech such as emphasis, when the word should be spelled out and ways to create “styles” for different sections of speech. To use these we can use the PromptBuilder. Let’s start by create a “style” for a section of speech
var promptBuilder = new PromptBuilder();
var promptStyle = new PromptStyle
{
Volume = PromptVolume.Soft,
Rate = PromptRate.Slow
};
promptBuilder.StartStyle(promptStyle);
promptBuilder.AppendText("Hello World");
promptBuilder.EndStyle();
using (var speech = new SpeechSynthesizer())
{
speech.Speak(promptBuilder);
}
We can build up our speech from different styles and include emphasis using
promptBuilder.AppendText("Hello ", PromptEmphasis.Strong);
we can also spell out words using
promptBuilder.AppendTextWithHint("Hello", SayAs.SpellOut);
Speech Recognition
The System.Speech assembly also includes the ability to recongize speech. This will require the Speech Recongition software within Windows to run (see Control Panel | Ease of Access | Speech Recognition.
To enable your application to use speech recognition you need to execute the following
SpeechRecognizer recognizer = new SpeechRecognizer();
Just executing this will allow the speech recognition code (on the focused application) to do things like execute button code as if the button was pressed. You can also hook into events to use recogonised speech within your application, for example using
SpeechRecognizer recognizer = new SpeechRecognizer();
recognizer.SpeechRecognized += (sender, args) =>
{
input.Text = args.Result.Text;
};