Adding an option for converting text to speech in an application can come in handy in a variety of different situations. So today in this short but useful tutorial, I’ll teach you how to build a text-to-speech sample using Microsoft Speech API (SAPI) with Windows Presentation Foundation (WPF). This application will allow you to open a text file in a RichTextBox control and then read out the text present in the text file.
Lets first start with a brief intro on Microsoft Speech API (SAPI) version 5.3. It is basically a managed API that allows developers to write speech enabled applications in .NET Framework 3.0 and above and is already integrated with Windows Vista. The API resides in System.Speech.Dll assembly.
To begin, create a new WPF Application project in Visual Studio 2008. You must add System.Speech.Dll assembly reference to your project. To do this, go to Solution Explorer, right click on your project name, select Add Reference and then look into .NET tab where you will find System.Speech, select it and press OK.
Now, you can import all namespaces of System.Speech in your application. For now, just import System.Speech.Synthesis, System.Windows.Form and System.IO.
Speech Synthesis is a process of converting text to speech. For this, we will use SpeechSynthesizer() class, its methods and properties in our application to accomplish our goal.
The speech synthesizer class has got four properties: Rate, State, Volume and Voice that are used to get and set rate, state, volume and voice of the speech. The value of rate is between -10 to 10 where as Volume varies from 0 to 100.
The Speak() and SpeakAsync() methods are used to speak synchronously and asynchronously respectively.
SpeechSyntehsizer foo = new SpeechSynthesizer();
So, on the basis of above class methods and properties I will start building this app. First of all, we will write the XAML code for the design elements and the controls.