c语言文字转语音代码,C语言实现将文字转为语音

#include

using namespace std;

#include //导入语音头文件

#include

#pragma comment(lib,”sapi.lib”) //导入语音头文件库

void  MSSSpeak(LPCTSTR speakContent)// speakContent为LPCTSTR型的字符串,调用此函数即可将文字转为语音

{

ISpVoice *pVoice = NULL;

//初始化COM接口

if (FAILED(::CoInitialize(NULL)))

MessageBox(NULL, (LPCWSTR)L”COM接口初始化失败!”, (LPCWSTR)L”提示”, MB_ICONWARNING | MB_CANCELTRYCONTINUE | MB_DEFBUTTON2);

//获取SpVoice接口

HRESULT hr = CoCreateInstance(CLSID_SpVoice, NULL, CLSCTX_ALL, IID_ISpVoice, (void**)&pVoice);

if (SUCCEEDED(hr))

{

pVoice->SetVolume((USHORT)100); //设置音量,范围是 0 -100

pVoice->SetRate(0); //设置速度,范围是 -10 – 10

hr = pVoice->Speak(speakContent, 0, NULL);

pVoice->Release();

pVoice = NULL;

}

//释放com资源

::CoUninitialize();

}

//string转换车wstring

std::wstring  StringToWString(const std::string& s)

{

std::wstring wszStr;

int nLength = MultiByteToWideChar(CP_ACP, 0, s.c_str(), -1, NULL, NULL);

wszStr.resize(nLength);

LPWSTR lpwszStr = new wchar_t[nLength];

MultiByteToWideChar(CP_ACP, 0, s.c_str(), -1, lpwszStr, nLength);

wszStr = lpwszStr;

delete[] lpwszStr;

return wszStr;

}

void read(string temp) {

wstring a = StringToWString(temp);

LPCWSTR str = a.c_str();

/*不知道为什么Cstr不行*/

MSSSpeak(str);

cout

}

int  main()

{

char  buf[128];

int i;

while (1)

{

cout

cin >> i;

switch (i)

{

case 1: cout

cin >> buf;

read(buf);

break;

case 2:break;

}

if (i == 2)

break;

}

return 0;

}

文章知识点与官方知识档案匹配,可进一步学习相关知识C技能树首页概览115007 人正在系统学习中 相关资源:经典刻录软件尼禄Nero6.6.1.4中文安装版及序列 -其它工具类资源…

声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2021年4月21日
下一篇 2021年4月21日

相关推荐