天天看點

Qt下使用微軟的TTS

微軟的sdk下載下傳位址為:www.microsoft.com/en-us/download/details.aspx?id=10121

Qt下使用微軟的TTS

下載下傳并安裝上圖勾選的兩個,建議按預設的路徑安裝

安裝完成後,接下來就是寫程式了

首先在建立的.pro檔案中添加

LIBS +=.\sapi.lib        lib在下載下傳的檔案中可以看到,也可以在你安裝的SpeechSDK中找到     

LIBS +=-lole32

#include <windows.h>

#include <mmsystem.h>      
#include <conio.h>      
#include <iostream>      
#include "sapi.h"      
#include "sphelper.h"      
using namespace std;      
#pragma comment(lib,"winmm.lib")      
ISpVoice *pVoice=NULL;      
MainWindow::MainWindow(QWidget *parent) :      
QMainWindow(parent),      
ui(new Ui::MainWindow)      
{      
ui->setupUi(this);      
//初始化COM接口      
if (FAILED(::CoInitialize(NULL)))      
return;      
//擷取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(L"你好", 0, NULL);      
pVoice->Release();      
pVoice = NULL;      
}      
//釋放com資源      
::CoUninitialize();      
}      

基本的代碼就是這樣,一定要加上#include <window.h>  否則會出現bug

接下來是借用别人寫的類,直接調用即可

分别是speech.h 和speech.cpp檔案

#ifndef SPEECH_H

#define SPEECH_H      
#include <QThread>      
#include <windows.h>      
#include <windows.h>      
#include <mmsystem.h>      
#include <conio.h>      
#include <iostream>      
#include "sapi.h"      
#include "sphelper.h"      
using namespace std;      
class Speech : public QThread       //用線程是為了防止一直按“speak”而導緻挂掉      
{      
Q_OBJECT      
private:                //類為公共靜态類,類構造由類内部自己調用,是以可直接用私有構造      
explicit Speech(QObject *parent = 0);      
~Speech();      
public:      
static Speech * getInstance();      
void speak(const QString & speakText);      
void setSpeechEnabled(bool speechEnable);      
private:      
static Speech * pSpeech;      
ISpVoice * pSpVoice;      
ISpObjectToken * pSpObjectToken;      
QList<QString> speechList;      
bool isSpeechEnabled;      
protected:      
void run();      
signals:      
void sendMsg(QString);      
public slots:      
};      
#endif // SPEECH_H      

#include "speech.h"

Speech * Speech::pSpeech = NULL;      
Speech::Speech(QObject *parent) :      
QThread(parent)      
{      
CoInitialize(NULL);      
pSpVoice = NULL;      
isSpeechEnabled = true;      
if(FAILED(CoCreateInstance(CLSID_SpVoice, NULL, CLSCTX_INPROC_SERVER, IID_ISpVoice, (void **)&pSpVoice)))      
{      
return;      
}      
else      
{      
emit sendMsg("ERROR!! Can not create instance!!");      
}      
pSpObjectToken = NULL;      
pSpVoice->SetVolume((USHORT)100); //設定音量,範圍是 0 -100      
pSpVoice->SetRate(0); //設定速度,範圍是 -10 - 10      
/* if(SUCCEEDED(      
SpFindBestToken(SPCAT_VOICES, L"language=405", NULL, &pSpObjectToken)))      
{      
pSpVoice->SetVoice(pSpObjectToken);      
pSpVoice->SetRate(8);      
}      
else      
{      
emit sendMsg("ERROR!! Can not find token!!");      
}*/      
}      
Speech * Speech::getInstance()      
{      
if(!pSpeech)      
{      
pSpeech = new Speech();      
}      
return pSpeech;      
}      
void Speech::speak(const QString &speakText)      
{      
if(!isSpeechEnabled)      
{      
return;      
}      
speechList.append(speakText);      
if(!isRunning())      
{      
start();      
}      
}      
void Speech::setSpeechEnabled(bool speechEnable)      
{      
isSpeechEnabled = speechEnable;      
}      
void Speech::run()      
{      
CoInitialize(NULL);      
while(!speechList.isEmpty())      
{      
QString speakText = speechList.first();      
speechList.pop_front();      
wchar_t * pBuf = new wchar_t[speakText.length()+1];      
memset(pBuf, 0, sizeof(wchar_t)*(speakText.length()+1));      
speakText.toWCharArray(pBuf);      
pSpVoice->Speak(pBuf, SPF_DEFAULT, NULL);      
delete pBuf;      
}      
CoUninitialize();      
}      
Speech::~Speech()      
{      
pSpObjectToken->Release();      
CoUninitialize();      
}      

調用該類後,在MainWindows中直接用即可

MainWindow.h

#include "speech.h"

classSpeech;

Speech*speech;

MainWindow.cpp

   speech=Speech::getInstance();

voidMainWindow::on_pushButton_clicked()

{      
speech->speak(ui->plainTextEdit->toPlainText());      
}      

結果如圖所示

Qt下使用微軟的TTS

在文本框中輸入文本,點選speak按鈕便可有聲音發出

但該程式還是存在了問題,在初始化時,會出現如下的提醒,但不會影響使用 ,要是有大神知道解決辦法,歡迎留言評論。

Qt下使用微軟的TTS

代碼我已上傳至  http://download.csdn.net/download/zhulichen/9991752

繼續閱讀