使用微软的tts
Posted qianbo_insist
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用微软的tts相关的知识,希望对你有一定的参考价值。
tts
很简单,直接复制代码使用,:)
微软的tts可以直接将输入的文字读出语音,在windows10上是可以直接用的
code is here
#pragma warning(disable: 4996)
#include <sapi.h>
#include <sphelper.h>
#include <iostream>
using namespace std;
#pragma comment(lib,"sapi.lib") //导入语音头文件库
void MSSSpeak(LPCTSTR speakContent,int volume,int rate)
{
ISpVoice *pVoice = NULL;
HRESULT hr = CoCreateInstance(CLSID_SpVoice, NULL, CLSCTX_ALL, IID_ISpVoice, (void**)&pVoice);
if (SUCCEEDED(hr))
{
ISpObjectToken * pSpObjectToken = NULL;
//这一句是可以使用中文
if (SUCCEEDED(SpFindBestToken(SPCAT_VOICES, L"language=804", NULL, &pSpObjectToken)))
{
pVoice->SetVoice(pSpObjectToken);
pSpObjectToken->Release();
}
//设置音量
pVoice->SetVolume((USHORT)volume);
pVoice->SetRate(rate);
hr = pVoice->Speak(speakContent, 0, NULL);
pVoice->Release();
pVoice = NULL;
}
}
//测试代码
int _tmain(int argc, _TCHAR* argv[])
{
//if (FAILED(::CoInitialize(NULL)))
::CoInitialize(NULL); // COM初始化
wstring read = L"我是中国人,你好";
MSSSpeak(L"hello,中国", 100, -2);
CLSID CLSID_SpVoice;
CLSIDFromProgID(_T("SAPI.SpVoice"), &CLSID_SpVoice);
ISpVoice *pSpVoice = NULL;
IEnumSpObjectTokens *pSpEnumTokens = NULL;
if (FAILED(CoCreateInstance(CLSID_SpVoice, NULL, CLSCTX_INPROC_SERVER, IID_ISpVoice, (void**)&pSpVoice)))
{
return -1;
}
//列举语音token
if (SUCCEEDED(SpEnumTokens(SPCAT_VOICES, NULL, NULL, &pSpEnumTokens)))
{
ISpObjectToken *pSpToken = NULL;
while (SUCCEEDED(pSpEnumTokens->Next(1, &pSpToken, NULL)) && pSpToken != NULL)
{
pSpVoice->SetVoice(pSpToken); // 设置当前语音token为pSpToken
// 朗读中文和英文的混合字符串
pSpVoice->Speak(read.c_str(), SPF_DEFAULT, NULL);
pSpToken->Release(); // 释放token
}
pSpEnumTokens->Release(); // 释放pSpEnumTokens接口
}
::CoUninitialize();
return 0;
}
#endif
以上是关于使用微软的tts的主要内容,如果未能解决你的问题,请参考以下文章
MS-TTS:免费微软TTS语音合成工具(一键合成导出MP3音频)