At the forefront of Artificial Intelligence
  Home Articles Reviews Interviews JDK Glossary Features Discussion Search

SAPI 5.0 Tutorial II: Text-to-Speech

This is an extremely short article about how to add a voice to your SAPI-enabled application. This article assumes you read the previous tutorial about how to set up your application with the XML rules and necessary COM objects.

To set up a voice, you need another COM pointer into your class (CSapiTutorial1Dlg in our case):

      CComPtr<ISpVoice>    g_cpVoice;
We then have to initialize the object. The easiest way to do this is append our initialization code in the InitializeSapi function we've been using. To create a voice, you retrieve the voice associated with the recognition context. Here is the code to be added:
	hRes = g_cpRecoCtxt->GetVoice(&g_cpVoice);

	if (FAILED(hRes)) {
		AfxMessageBox("Error creating voice");
		return false;
	}
Now that we've initialized it we are ready to have our application speak! To make the program speak simply call the Speak method. For example, SapiTutorial01 will say "Text to Speech enabled" upon startup. This is accomplished using this call:
    g_cpVoice->Speak( T2W("Text to Speech enabled"), SPF_ASYNC, NULL);
The T2W is a conversion macro, therefore the USES_CONVERSION macro has to be called before using a call to Speak (if you need to convert the string). The Speak function can be broken up as follows (adapted from the SAPI5 documentation):

HRESULT Speak(
   const WCHAR   *pwcs, 
   DWORD          dwFlags, 
   ULONG         *pulStreamNumber
);
Parameters
pwcs
Pointer to the null-terminated text string to be spoken. The string can contain XML data to be parsed if necessary. If dwFlags is set to SPF_IS_FILENAME the string should be a complete filename to be spoken.
dwFlags
These flags are used to control how and what is spoken. See the SPEAKFLAGs documentation for more details. In our example, the SPF_ASYNC means that the call will be asynchronous.
pulStreamNumber
A pointer to a ULONG that denotes the current input stream number associated with this Speak request. This is fairly advanced feature that we can ignore for the meanwhile.

This is basically all the information required to use text-to-speech within your program. Remember to release the voice object (in the ReleaseSAPI function) before exiting.

Submitted: 24/03/2001

Article content copyright © James Matthews, 2001.
 Article Toolbar
Print
BibTeX entry

Search

Latest News
- Generation5 10-year Anniversary (03/09/2008)
- New Generation5 Design! (09/04/2007)
- Happy New Year 2007 (02/01/2007)
- Where has Generation5 Gone?! (04/11/2005)
- NeuroEvolving Robotic Operatives (NERO) (25/06/2005)

What's New?
- Back-propagation using the Generation5 JDK (07/04/2008)
- Hough Transforms (02/01/2008)
- Kohonen-based Image Analysis using the Generation5 JDK (11/12/2007)
- Modelling Bacterium using the JDK (19/03/2007)
- Modelling Bacterium using the JDK (19/03/2007)


All content copyright © 1998-2007, Generation5 unless otherwise noted.
- Privacy Policy - Legal - Terms of Use -