Click here to Skip to main content
15,885,767 members
Articles / Desktop Programming / MFC
Article

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

Rate me:
Please Sign up or sign in to vote.
4.52/5 (17 votes)
17 May 20053 min read 115.3K   3.2K   67   24
The CSpeech class demonstrates a simple way to make an application talk.

Introduction

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
{
public:
    App();

// Overrides
    virtual BOOL InitInstance();

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

// Implementation

    DECLARE_MESSAGE_MAP()
};
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.

History

  • 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).

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


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralUse to answer a phone Pin
Member 15948129-Jan-08 18:45
Member 15948129-Jan-08 18:45 
GeneralRe: Use to answer a phone Pin
NetDave1-Apr-09 5:02
NetDave1-Apr-09 5:02 
GeneralCompiling in VS 2005 Pin
Samuel Velazquez19-Jul-07 16:14
Samuel Velazquez19-Jul-07 16:14 
GeneralNice! Pin
Michel Helms2-Oct-06 22:02
Michel Helms2-Oct-06 22:02 
QuestionSonething similar in C#? Pin
anderslundsgard13-Jun-05 18:24
anderslundsgard13-Jun-05 18:24 
GeneralThat's great. Pin
imacess24-May-05 18:12
imacess24-May-05 18:12 
GeneralMFC Pin
NGS 54967219-May-05 5:07
NGS 54967219-May-05 5:07 
QuestionWhat about non english speech ? Pin
Davide Zaccanti18-May-05 7:17
Davide Zaccanti18-May-05 7:17 
AnswerRe: What about non english speech ? Pin
DaveeCom18-May-05 19:12
DaveeCom18-May-05 19:12 
GeneralCSpeech updated. Non-MFC Builds Pin
DaveeCom17-May-05 5:35
DaveeCom17-May-05 5:35 
GeneralMFC requirement Pin
David Crow17-May-05 4:04
David Crow17-May-05 4:04 
GeneralRe: MFC requirement Pin
DaveeCom17-May-05 4:25
DaveeCom17-May-05 4:25 
GeneralA few suggestions... Pin
James R. Twine10-May-05 1:13
James R. Twine10-May-05 1:13 
GeneralRe: A few suggestions... Pin
DaveeCom10-May-05 1:37
DaveeCom10-May-05 1:37 
GeneralRe: A few suggestions... Pin
DaveeCom10-May-05 2:03
DaveeCom10-May-05 2:03 
GeneralRe: A few suggestions... Pin
DaveeCom10-May-05 2:31
DaveeCom10-May-05 2:31 
GeneralRe: A few suggestions... Pin
James R. Twine10-May-05 5:49
James R. Twine10-May-05 5:49 
Generalcannot find sapi.h Pin
Simone Busoli9-May-05 23:25
Simone Busoli9-May-05 23:25 
GeneralRe: cannot find sapi.h Pin
DaveeCom10-May-05 0:47
DaveeCom10-May-05 0:47 
GeneralRe: cannot find sapi.h Pin
James R. Twine10-May-05 0:53
James R. Twine10-May-05 0:53 
GeneralRe: cannot find sapi.h Pin
Abbas_Riazi10-May-05 0:52
professionalAbbas_Riazi10-May-05 0:52 
GeneralRe: cannot find sapi.h Pin
Simone Busoli10-May-05 0:55
Simone Busoli10-May-05 0:55 
GeneralRe: cannot find sapi.h Pin
Abbas_Riazi10-May-05 0:59
professionalAbbas_Riazi10-May-05 0:59 
GeneralRe: cannot find sapi.h Pin
Simone Busoli10-May-05 1:05
Simone Busoli10-May-05 1: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.