TTS(Text-To-Speech)是指文本转语音的英文简称,即通过TTS引擎把文本转化为语音输出。本文将讲述如何在QT中使用TTS引擎,先看成品。
Windows Speech SDK可以在微软的官网上免费下载,下载地址为:http://www.microsoft.com/download/en/details.aspx?id=10121
进入微软下载页面后,如下图所示,点击【Download】。
勾选“SpeechSDK51.exe”和“SpeechSDK51LangPack.exe”,然后点击【Next】下载文件。
SpeechSDK51.exe是简体中文语音引擎,SpeechSDK51LangPach.exe是中文男生语音库。
先安装SpeechSDK51.exe,再安装SpeechSDK51LangPach.exe。
都安装完成后,在【控制面板】搜索“语音”,如下图所示。
点击“更改文本到语音转换设置”,弹出如下界面。
点击“预听语音(P)”,如果可以听见朗读声音,恭喜你!安装成功了。
最后,添加系统变量,如下图所示。
用VS创建一个QT应用程序工程,配置如下3张图。
//QTTS.h
#pragma once
#include
#include
class QTTS : public QObject
{
Q_OBJECT
public:
QTTS(QObject *parent = Q_NULLPTR);
~QTTS();
bool speak(const QString &txt);
void pause();
void resume();
void stop();
quint16 getVolume();
void setVolume(quint16 volume);
private:
bool initSpeech();
private:
QAxObject *m_pVoice;
bool m_bInit;
bool _bReading;
};
//QTTS.cpp
#include "QTTS.h"
#include
QTTS::QTTS(QObject *parent)
: QObject(parent)
{
m_bInit = 0;
m_pVoice = Q_NULLPTR;
}
QTTS::~QTTS()
{
}
bool QTTS::initSpeech()
{
if (m_bInit)
return true;
if (m_pVoice == Q_NULLPTR)
{
m_pVoice = new QAxObject();
}
m_bInit = this->m_pVoice->setControl("96749377-3391-11D2-9EE3-00C04F797396");
return m_bInit;
}
bool QTTS::speak(const QString &txt)
{
if (!m_bInit)
{
initSpeech();
}
return this->m_pVoice->dynamicCall("Speak(QString, SpeechVoiceSpeakFlags)", txt, SPF_ASYNC).toInt();
}
void QTTS::pause()
{
if (!m_bInit)
return;
this->m_pVoice->dynamicCall("Pause()");
}
void QTTS::resume()
{
if (!m_bInit)
return;
this->m_pVoice->dynamicCall("Resume()");
}
void QTTS::stop()
{
if (!m_bInit)
return;
this->m_pVoice->dynamicCall("Speak(QString, SpeechVoiceSpeakFlags)", "", 2).toInt();
}
quint16 QTTS::getVolume()
{
return this->m_pVoice->property("Volume").toInt();
}
void QTTS::setVolume(quint16 volume)
{
if (!m_bInit)
return;
this->m_pVoice->dynamicCall("SetVolume(int)", volume);
}
QString str = QString::fromLocal8Bit("如何在QT中使用TTS引擎?你的程序猿大叔马上讲解!");
QTTS tts;
Tts.speak(str);
点赞 & 关注你的程序猿大叔,带你提升战斗力!更想了解哪个方面的知识点,私信我!
页面更新:2024-02-11
本站资料均由网友自行发布提供,仅用于学习交流。如有版权问题,请与我联系,QQ:4156828
© CopyRight 2020-2024 All Rights Reserved. Powered By 71396.com 闽ICP备11008920号-4
闽公网安备35020302034903号