Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Simple Text Editor With Speech Abilities

0.00/5 (No votes)
27 Jun 2007 1  
An article on using the Microsoft Speech SDK to make a simple text-to-speech application

Screenshot - capture.jpg

Introduction

This program serves mainly as a text editor, but with extended capabilities such as inserting an image and speaking the text you write. The most important part is how to make speech out of the written text. I did this using the Microsoft Text-to-Speech SDK.

Using the code

You must first install the Microsoft Text-to-Speech SDK. Then you can use all of the Text-to-Speech features in this program. Include the Speech Library into the program as follows:

using SpeechLib;

Then define the following variables:

string voice="name=Microsoft Sam";  // default

int volume=50;
int rate=3;

This piece of code is responsible for speaking the text you write. To speak the given text string synchronously, we must use:

SpeechVoiceSpeakFlags SpFlags = SpeechVoiceSpeakFlags.SVSFlagsAsync;

Other values are:

  • SVSFPurgeBeforeSpeak: Purges all pending speak requests prior to this speak call.
  • SVSFIsFilename: The string passed to the Speak method is a file name rather than text. As a result, the string itself is not spoken, but rather the file the path that it points to.
  • SVSFIsXML: The input text will be parsed for XML markup.
  • SVSFIsNotXML: The input text will not be parsed for XML markup.
  • SVSFPersistXML: Global state changes in the XML markup will persist across speak calls.
  • SVSFNLPSpeakPunc: Punctuation characters should be expanded into words, e.g. "This is it." would become "This is it period"
  • SVSFNLPMask: Flags handled by SAPI -- as opposed to the text-to-speech engine -- are set in this mask.
  • SVSFVoiceMask: This mask has every flag bit set.
  • SVSFUnusedFlags: This mask has every unused bit set.
TextArea ta;
ta=(TextArea)this.ActiveMdiChild;
SpeechVoiceSpeakFlags SpFlags = SpeechVoiceSpeakFlags.SVSFlagsAsync;
SpVoice Vr = new SpVoice();
Vr.Rate=rate;
Vr.Volume=volume;
SpeechLib.SpObjectToken tok;
tok=Vr.Voice;            
Vr.Voice=Vr.GetVoices(voice,"").Item(0);
try
{    
    Vr.Speak(ta.richTextBox1.SelectedText, SpFlags);
}
catch
{
    MessageBox.Show(
        "Nothing To Read\nPlease Open A Document And Write Some Text First",
        "Error");
}

History

  • 27 June, 2007 -- Original version posted

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here