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

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

0.00/5 (No votes)
17 May 2005 1  
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