下载3个语音API安装包 http://www.microsoft.com/en-us/download/details.aspx?id=10121
需要安装微软语音API安装包:SpeechSDK51LangPack、SpeechSDK51以及 msttss22L、
为了在VC中使用这SDK,必需在工程中添加SDK的include和lib目录,为免每个工程都添加目录,最好的办法是在VC的
Option->Directoris立加上SDK的include和lib目录。也可以直接将sapi.h sapi.lib 分别放到VC6的 头文件合库文件中
有关TTS用法:https://msdn.microsoft.com/en-us/library/ms720165(v=vs.85).aspx#InitializeCOM
一个最简单的例子
#include <sapi.h> #pragma comment(lib,"ole32.lib") //CoInitialize CoCreateInstance需要调用ole32.dll #pragma comment (lib,"sapi.lib") //sapi.lib在SDK的lib目录,必需正确配置 int main (int argc, char* argv[]) { ISpVoice * pVoice = NULL; //COM初始化: if (FAILED(::CoInitialize(NULL))) return FALSE; //获取ISpVoice接口: HRESULT hr = CoCreateInstance(CLSID_SpVoice, NULL, CLSCTX_ALL, IID_ISpVoice, (void **)&pVoice); if( SUCCEEDED( hr ) ) {hr = pVoice->Speak(L"欢迎使用本系统,管理员请登录或者注册", 0, NULL); pVoice->Release(); pVoice = NULL;} ::CoUninitialize(); //释放 return TRUE; }
详细解释:
void voice_succeed() { ISpVoice * pVoice; //pVoice指针 ::CoInitialize(NULL); //初始化COM库 //获取核心的应用程序接口ISpVoice接口 CoCreateInstance(CLSID_SpVoice, NULL, CLSCTX_ALL, IID_ISpVoice, (void **)&pVoice);//创建识别引擎 pVoice->Speak(L"登录成功!欢迎使用!", 0,NULL);
//发音 speak 参数详解:1.用来输入文本字符串 2.标志speak的方式 3.获取当前文本输入等候播放队列位置
pVoice->Release();//释放资源
pVoice->Release();//释放资源 ::CoUninitialize(); //释放资源 }
用CoInitialize(), CoCreateInstance()获取IspVoice接口就够了, 程序结束前一定要用CoUninitialize()释放资源。
获取IspVoice接口,然后用ISpVoice::Speak()把文本输出为语音,程序的核心就是IspVoice接口。
把文本语音输出为WAV文件
#include <sapi.h> #include <sphelper.h> #pragma comment(lib,"ole32.lib") #pragma comment(lib,"sapi.lib") int main(int argc, char* argv[]) { ISpVoice * pVoice = NULL; if (FAILED(::CoInitialize(NULL))) return FALSE; HRESULT hr = CoCreateInstance(CLSID_SpVoice, NULL, CLSCTX_ALL, IID_ISpVoice, (void **)&pVoice); if( SUCCEEDED( hr ) ) { CComPtr<ISpStream> cpWavStream; CComPtr<ISpStreamFormat> cpOldStream; CSpStreamFormat OriginalFmt; pVoice->GetOutputStream( &cpOldStream ); OriginalFmt.AssignFormat(cpOldStream); hr = SPBindToFile( L"D:\\output.wav",SPFM_CREATE_ALWAYS, &cpWavStream,&OriginalFmt.FormatId(), OriginalFmt.WaveFormatExPtr() ); if( SUCCEEDED( hr ) ) { pVoice->SetOutput(cpWavStream,TRUE); WCHAR WTX[] = L"<VOICE REQUIRED=''NAME=Microsoft Mary''/>text to wave"; pVoice->Speak(WTX, SPF_IS_XML, NULL); pVoice->Release(); pVoice = NULL; } } ::CoUninitialize(); return TRUE; }