Click here to Skip to main content
13,249,813 members (68,037 online)
Click here to Skip to main content
Add your own
alternative version


67 bookmarked
Posted 9 May 2005

The CSpeech Class - give your application a voice with very little effort

, 17 May 2005
Rate this:
Please Sign up or sign in to vote.
The CSpeech class demonstrates a simple way to make an application talk.


This sample VC project demonstrates how to use the CSpeech class to perform simple text to speech conversion. CSpeech is a drop-in class that provides the ability for an application to talk using the Microsoft's (SAPI) Speech API 5.1. There is only one public method to call in this class and that is the CSpeech::Speak function. CSpeech has its own worker thread that is used to communicate with the Speech API in the background. By using a worker thread instead of the caller's thread to perform the speech, the CSpeech::Speak function returns to the caller immediately after being called. The speech is performed by calling the ISpVoice::Speak SAPI method in synchronous mode. A worker thread with SAPI's synchronous mode is a necessity to circumvent a problem in SAPI where a queued speech was being presented out of order if SAPI's async mode was used.

Using the code

In order to use the CSpeech class in your own applications, you need to:

  • Add the Speech.cpp and Speech.h files to your VC project.
  • Add the statement #include "speech.h" to your source and/or header file(s).
  • Declare the CSpeech object.
  • Finally, call the CSpeech::Speak function as many times as you'd like to begin speaking.
  • Make sure that you have set up a build environment that includes the Microsoft Speech SDK 5.1 and ensure that the Include and Lib paths are added to your build's options.

Speech will occur now until either:

  1. The queued messages are all spoken.
  2. The CSpeech destructor is called which will discard any unspoken message that exists after the active speech completes.

An example of how to declare the CSpeech in the App class:

class App : public CWinApp

// Overrides
    virtual BOOL InitInstance();

    CSpeech m_Speech;   // <=======

// Implementation

BOOL App::InitInstance()
    // ...

    m_Speech.Speak(_T("Text to speech is very easy" 
        " if you use the C-Speech class.")); // <=======

    // ...

    return FALSE;

Points of Interest

  • Your project can be built using Unicode or single byte strings.
  • There's minimal error checking. If there is a problem initializing, such as a missing SAPI speech server, the Speak function will quietly fail by returning FALSE.
  • The notify messages from SAPI are being ignored by this class. Since queuing is being handled ourselves, the notify messages don't seem to be needed, as far as I know.
  • MFC is no longer needed by the class. You must, however, make sure you are using a multi-threaded runtime library in order to avoid _beginthreadex being unresolved at link time. (I've been using MFC long enough to where I hardly know how to build a non-MFC project without some hard knocks. But I found out that in order to get the _beginthreadex symbol to be part of the build in a non-MFC project, I had to go to the project properties page and set the "Use MFC in a Shared DLL" property.) If anyone has some advice on building multithreaded non-MFC projects, please contact me so I can update this article with the correct information.


  • 05-04-2005 - Created CSpeech class and demo project.
  • 05-07-2005 - Fixed string length bug. Removed Unicode requirement.
  • 05-16-2005 - Removed dependency on MFC. Added CCS class to provide a wrapper for the Critical Section calls (as requested).


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


About the Author

United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralUse to answer a phone Pin
Member 15948129-Jan-08 19:45
memberMember 15948129-Jan-08 19:45 
GeneralRe: Use to answer a phone Pin
NetDave1-Apr-09 6:02
memberNetDave1-Apr-09 6:02 
GeneralCompiling in VS 2005 Pin
svelazquez19-Jul-07 17:14
membersvelazquez19-Jul-07 17:14 
GeneralNice! Pin
Michel Helms2-Oct-06 23:02
memberMichel Helms2-Oct-06 23:02 
QuestionSonething similar in C#? Pin
d00_ape13-Jun-05 19:24
memberd00_ape13-Jun-05 19:24 
GeneralThat's great. Pin
imacess24-May-05 19:12
memberimacess24-May-05 19:12 
GeneralMFC Pin
NGS 54967219-May-05 6:07
memberNGS 54967219-May-05 6:07 
QuestionWhat about non english speech ? Pin
Davide Zaccanti18-May-05 8:17
memberDavide Zaccanti18-May-05 8:17 
AnswerRe: What about non english speech ? Pin
DaveeCom18-May-05 20:12
memberDaveeCom18-May-05 20:12 
GeneralCSpeech updated. Non-MFC Builds Pin
DaveeCom17-May-05 6:35
memberDaveeCom17-May-05 6:35 
GeneralMFC requirement Pin
DavidCrow17-May-05 5:04
memberDavidCrow17-May-05 5:04 
GeneralRe: MFC requirement Pin
DaveeCom17-May-05 5:25
memberDaveeCom17-May-05 5:25 
GeneralA few suggestions... Pin
James R. Twine10-May-05 2:13
memberJames R. Twine10-May-05 2:13 
GeneralRe: A few suggestions... Pin
DaveeCom10-May-05 2:37
memberDaveeCom10-May-05 2:37 
GeneralRe: A few suggestions... Pin
DaveeCom10-May-05 3:03
memberDaveeCom10-May-05 3:03 
GeneralRe: A few suggestions... Pin
DaveeCom10-May-05 3:31
memberDaveeCom10-May-05 3:31 
GeneralRe: A few suggestions... Pin
James R. Twine10-May-05 6:49
memberJames R. Twine10-May-05 6:49 
Generalcannot find sapi.h Pin
simone_b10-May-05 0:25
membersimone_b10-May-05 0:25 
GeneralRe: cannot find sapi.h Pin
DaveeCom10-May-05 1:47
memberDaveeCom10-May-05 1:47 
GeneralRe: cannot find sapi.h Pin
James R. Twine10-May-05 1:53
memberJames R. Twine10-May-05 1:53 
GeneralRe: cannot find sapi.h Pin
A. Riazi10-May-05 1:52
memberA. Riazi10-May-05 1:52 
GeneralRe: cannot find sapi.h Pin
simone_b10-May-05 1:55
membersimone_b10-May-05 1:55 
GeneralRe: cannot find sapi.h Pin
A. Riazi10-May-05 1:59
memberA. Riazi10-May-05 1:59 
GeneralRe: cannot find sapi.h Pin
simone_b10-May-05 2:05
membersimone_b10-May-05 2:05 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.171114.1 | Last Updated 18 May 2005
Article Copyright 2005 by DaveeCom
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid