#include "CWinTTS.h"
CWinTTS::CWinTTS(void)
{
m_pIEnumSpObjectTokens = NULL;
m_pISpObjectToken = NULL;
m_pISpVoice = NULL;
m_bComInit = FALSE;
initEngine();
}
CWinTTS::~CWinTTS(void)
{
UnInit();
m_pIEnumSpObjectTokens->Release();
m_pISpObjectToken->Release();
DeleteSpVoice();
}
void CWinTTS::initEngine()
{
Init();
QString arrayVoicePackageName[50] = {0};
int nVoicePackageNameCount = 50;
int nCount = EnumAudioToken(arrayVoicePackageName,nVoicePackageNameCount);
CreateSpVoice();
ISpObjectToken* ppToken = NULL;
GetVoice(0,&ppToken);
SetVoice(ppToken);
SetRate(0);
SetVolume(100);
}
int CWinTTS::Init()
{
//初始化COM组件
if(FAILED(::CoInitializeEx(NULL,0)))
{
return -1;
}
m_bComInit = TRUE;
return 0;
}
int CWinTTS::UnInit()
{
if(m_bComInit)
{
::CoUninitialize();
}
return 0;
}
int CWinTTS::EnumAudioToken(QString arrayVoicePackageName[],int nVoicePackageNameCount)
{
//枚举所有语音Token
if(SUCCEEDED(SpEnumTokens(SPCAT_VOICES, NULL, NULL, &m_pIEnumSpObjectTokens)))
{
//得到所有语音Token的个数
ULONG ulTokensNumber = 0;
m_pIEnumSpObjectTokens->GetCount(&ulTokensNumber);
//检测该机器是否安装有语音包
if(ulTokensNumber == 0)
{
return -1;
}
if(ulTokensNumber > nVoicePackageNameCount)
{
return 0;
}
//将语音包的名字加入数组中
QString strVoicePackageName = "";
QString strTokenPrefixText = "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Speech\\Voices\\Tokens\\" ;
for(ULONG i=0; i<ulTokensNumber; i++)
{
m_pIEnumSpObjectTokens->Item(i, &m_pISpObjectToken);
WCHAR* pChar;
m_pISpObjectToken->GetId(&pChar);
strVoicePackageName = QString::fromStdWString(pChar) ;
strVoicePackageName = strVoicePackageName.mid(strTokenPrefixText.size());
arrayVoicePackageName[i] = strVoicePackageName;
}
return ulTokensNumber;
}
return -1;
}
//创建SpVoice
int CWinTTS::CreateSpVoice()
{
//获取ISpVoice接口
if(FAILED(CoCreateInstance(CLSID_SpVoice, NULL, CLSCTX_INPROC_SERVER, IID_ISpVoice, (void**)&m_pISpVoice)))
{
return -1;
}
return 0;
}
//释放SpVoice
int CWinTTS::DeleteSpVoice()
{
if(m_pISpVoice != NULL)
{
m_pISpVoice->Release();
}
m_pISpVoice = NULL;
return 0;
}
//重置SpVoice
int CWinTTS::ResetSpVoice()
{
DeleteSpVoice();
return CreateSpVoice();
}
//设置朗读速度(取值范围:-10到10)
int CWinTTS::SetRate( long RateAdjust)
{
if(m_pISpVoice == NULL)
{
return -1;
}
//设置播放速度
m_pISpVoice->SetRate(RateAdjust);
return 0;
}
//获取朗读速度
int CWinTTS::GetRate(long *pRateAdjust)
{
if(m_pISpVoice == NULL)
{
return -1;
}
m_pISpVoice->GetRate(pRateAdjust);
return 0;
}
//设置使用的语音库
int CWinTTS::SetVoice(ISpObjectToken *pToken)
{
if(m_pISpVoice == NULL)
{
return -1;
}
m_pISpVoice->SetVoice(pToken);
return 0;
}
//获取语音库
int CWinTTS::GetVoice(unsigned int nIndex,ISpObjectToken** ppToken)
{
if(m_pIEnumSpObjectTokens == NULL)
{
return -1;
}
//设置语言
m_pIEnumSpObjectTokens->Item(nIndex, ppToken);
m_pISpObjectToken = *ppToken;
return 0;
}
//设置音量(取值范围:0到100)
int CWinTTS::SetVolume(USHORT usVolume)
{
if(m_pISpVoice == NULL)
{
return -1;
}
//设置音量大小
m_pISpVoice->SetVolume(usVolume);
return 0;
}
//获取音量
int CWinTTS::GetVolume(USHORT *pusVolume)
{
if(m_pISpVoice == NULL)
{
return -1;
}
//设置音量大小
m_pISpVoice->GetVolume(pusVolume);
return 0;
}
bool CWinTTS::isVoiceReady()
{
if(m_pISpVoice == NULL)
{
return false;
}
SPVOICESTATUS voiceStatus;
// 当前播放
if(S_OK == m_pISpVoice->GetStatus(&voiceStatus, NULL))
{
if (voiceStatus.dwRunningState == SPRS_DONE )
{
return true;
}
}
return false;
}
// 停止朗读
int CWinTTS::Stop()
{
return ResetSpVoice();
}
//开始朗读
int CWinTTS::Speak(QString strContent, DWORD dwFlags)
{
if(m_pISpVoice == NULL)
{
return -1;
}
LPCTSTR speakContent = reinterpret_cast<const wchar_t *>(strContent.utf16());
//开始进行朗读
HRESULT hSucess = m_pISpVoice->Speak(speakContent, dwFlags, NULL);
return 0;
}
//朗读生成文件
int CWinTTS::SpeakToWaveFile(QString strContent,char *pFilePathName,DWORD dwFlags)
{
if(m_pISpVoice == NULL || pFilePathName == NULL)
{
return -1;
}
//生成WAV文件
CComPtr<ISpStream> cpISpStream;
CComPtr<ISpStreamFormat> cpISpStreamFormat;
CSpStreamFormat spStreamFormat;
m_pISpVoice->GetOutputStream(&cpISpStreamFormat);
spStreamFormat.AssignFormat(cpISpStreamFormat);
HRESULT hResult = SPBindToFile(pFilePathName, SPFM_CREATE_ALWAYS,
&cpISpStream, &spStreamFormat.FormatId(), spStreamFormat.WaveFormatExPtr());
if(SUCCEEDED(hResult))
{
LPCTSTR speakContent = reinterpret_cast<const wchar_t *>(strContent.utf16());
m_pISpVoice->SetOutput(cpISpStream, TRUE);
m_pISpVoice->Speak(speakContent, dwFlags, NULL);
return 0;
}
else
{
return 1;
}
}
//暂停朗读
int CWinTTS::Pause()
{
if(m_pISpVoice != NULL)
{
m_pISpVoice->Pause();
}
return 0;
}
//继续朗读
int CWinTTS::Resume()
{
if(m_pISpVoice != NULL)
{
m_pISpVoice->Resume();
}
return 0;
}
漫步者968
- 粉丝: 3
- 资源: 22
最新资源
- 串联式、并联式、混联式混合动力系统simulink控制策略模型(串联式、并联式、混联式每个都是独立的需要单独说拿哪个,默认是混联式RB) 有基于逻辑门限值、状态机的规则控制策略(RB)、基于等效燃油
- 法码滋.exe法码滋2.exe法码滋3.exe
- python-geohash-0.8.5-cp38-cp38-win-amd64
- Matlab根据flac、pfc或其他软件导出的坐标及应力、位移数据再现云图 案例包括导出在flac6.0中导出位移的fish代码(也可以自己先准备软件导出的坐标数据及对应点的位移或应力数据,可根据需
- 拳皇97.exe拳皇972.exe拳皇973.exe
- 捕鱼达人1.exe捕鱼达人2.exe捕鱼达人3.exe
- 医疗骨折摄像检测29-YOLO(v5至v9)、COCO、CreateML、Darknet、Paligemma数据集合集.rar
- ks滑块加密算法与源代码
- 医护人员检测23-YOLOv8数据集合集.rar
- 1.电力系统短路故障引起电压暂降 2.不对称短路故障分析 包括:共两份自编word+相应matlab模型 1.短路故障的发生频次以及不同类型短路故障严重程度,本文选取三类典型的不对称短路展开研究
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈