至此已做好了编写语音程序的准备工作,可以开始编写语音程序了。下面首先介绍文本-语音转换的编程技术。
11.2.1 构造CText2Speech类
为了便于使用Speech SDK提供的文本-语音转换COM接口,笔者编写了一个类CText2Speech,其中封装了文本-语音转换COM接口的基本方法。借助该类来编写文本-语音转换程序非常方便。
先来讨论该CText2Speech类的设计,其定义文件列举如下:
///
// active speech engine
//
#include <atlbase.h>
extern CComModule _Module;
#include <atlcom.h>
#include "sapi.h"
#include <sphelper.h>
///
// speech message
//
#define WM_TTSEVENT WM_USER+101
///
// text-to-speech class
//
class CText2Speech
{
public:
CText2Speech();
virtual ~CText2Speech();
// initialize
BOOL Initialize(HWND hWnd = NULL);
void Destroy();
// speak
HRESULT Speak(const WCHAR *pwcs, DWORD dwFlags = SPF_DEFAULT);
HRESULT Pause();
HRESULT Resume();
// rate
HRESULT SetRate(long lRateAdjust);
HRESULT GetRate(long* plRateAdjust);
// volume
HRESULT SetVolume(USHORT usVolume);
HRESULT GetVolume(USHORT* pusVolume);
// voice
ULONG GetVoiceCount();
HRESULT GetVoice(WCHAR **ppszDescription, ULONG lIndex = -1);
HRESULT SetVoice(WCHAR **ppszDescription);
// error string
CString GetErrorString()
{
return m_sError;
}
// interface
CComPtr<ISpVoice> m_IpVoice;
private:
CString m_sError;
};
文件的开始几行语句:
#include <atlbase.h>
extern CComModule _Module;
#include <atlcom.h>
#include "sapi.h"
#include <sphelper.h>