织梦CMS - 轻松建站从此开始!

罗索

当前位置: 主页>杂项技术>VC(MFC)>

The CSpeech Class - give your application a voice with very

jackyhwei 发布于 2011-06-19 13:31 点击:次 
The CSpeech class demonstrates a simple way to make an application talk.
TAG:

本文完整源码下载:http://bbs.rosoo.net/forum.php?mod=viewthread&tid=5895

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:

Collapse
class App : public CWinApp
{
public:
    App();

// Overrides 
    virtual BOOL InitInstance();

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

// Implementation 

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

文章:来源
(DaveeCom)
本站文章除注明转载外,均为本站原创或编译欢迎任何形式的转载,但请务必注明出处,尊重他人劳动,同学习共成长。转载请注明:文章转载自:罗索实验室 [http://www.rosoo.net/a/201106/14578.html]
本文出处:CodeProject.com 作者:DaveeCom
顶一下
(0)
0%
踩一下
(0)
0%
------分隔线----------------------------
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 验证码:点击我更换图片
栏目列表
将本文分享到微信
织梦二维码生成器
推荐内容