Click here to Skip to main content
Click here to Skip to main content

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.

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

Share

About the Author

DaveeCom

United States United States
No Biography provided

Comments and Discussions

 
GeneralUse to answer a phone PinmemberMember 15948129-Jan-08 19:45 
GeneralRe: Use to answer a phone PinmemberNetDave1-Apr-09 6:02 
GeneralCompiling in VS 2005 Pinmembersvelazquez19-Jul-07 17:14 
GeneralNice! PinmemberMichel Helms2-Oct-06 23:02 
QuestionSonething similar in C#? Pinmemberd00_ape13-Jun-05 19:24 
GeneralThat's great. Pinmemberimacess24-May-05 19:12 
GeneralMFC PinmemberNGS 54967219-May-05 6:07 
QuestionWhat about non english speech ? PinmemberDavide Zaccanti18-May-05 8:17 
AnswerRe: What about non english speech ? PinmemberDaveeCom18-May-05 20:12 
GeneralCSpeech updated. Non-MFC Builds PinmemberDaveeCom17-May-05 6:35 
GeneralMFC requirement PinmemberDavidCrow17-May-05 5:04 
GeneralRe: MFC requirement PinmemberDaveeCom17-May-05 5:25 
GeneralA few suggestions... PinmemberJames R. Twine10-May-05 2:13 
GeneralRe: A few suggestions... PinmemberDaveeCom10-May-05 2:37 
GeneralRe: A few suggestions... PinmemberDaveeCom10-May-05 3:03 
GeneralRe: A few suggestions... PinmemberDaveeCom10-May-05 3:31 
GeneralRe: A few suggestions... PinmemberJames R. Twine10-May-05 6:49 
Generalcannot find sapi.h Pinmembersimone_b10-May-05 0:25 
GeneralRe: cannot find sapi.h PinmemberDaveeCom10-May-05 1:47 
GeneralRe: cannot find sapi.h PinmemberJames R. Twine10-May-05 1:53 
GeneralRe: cannot find sapi.h PinmemberA. Riazi10-May-05 1:52 
GeneralRe: cannot find sapi.h Pinmembersimone_b10-May-05 1:55 
GeneralRe: cannot find sapi.h PinmemberA. Riazi10-May-05 1:59 
GeneralRe: cannot find sapi.h Pinmembersimone_b10-May-05 2:05 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.141216.1 | Last Updated 18 May 2005
Article Copyright 2005 by DaveeCom
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid