I recently studied the relevant materials that call the Microsoft TTS engine for reading and found that it is actually very simple to share with many bloggers.
First, let's take a look at how Microsoft's msdn official documentation calls TTS.
Microsoft TTS simple sample#include <stdafx.h>#include <sapi.h>int main(int argc, char* argv[]){ ISpVoice * pVoice = NULL; if (FAILED(::CoInitialize(NULL))) return FALSE; HRESULT hr = CoCreateInstance(CLSID_SpVoice, NULL, CLSCTX_ALL, IID_ISpVoice, (void **)&pVoice;); if( SUCCEEDED( hr ) ) { hr = pVoice->Speak(L"Hello world", 0, NULL); pVoice->Release(); pVoice = NULL; } ::CoUninitialize(); return TRUE;}
It can be found that it is actually very simple, that is, after initializing COM, you can create an ispvoice instance and call the Speak method. If you want to learn more about this, you can refer to msdn, http://msdn.microsoft.com/en-us/library/ee125082 (V = vs.85 ). aspx
It is very simple to use Com on QT, and activityqt deals with COM on QT. Because it calls COM, we can use qaxobject to meet the requirements.
First, design an Interface
ITTS#include <QObject>class ITTS : public QObject{Q_OBJECTpublic:ITTS(void);virtual ~ITTS(void);virtual bool initSpeech() = 0;virtual bool speak(QString & txt) = 0;virtual void pause() = 0;virtual void resume() = 0;virtual void stop() = 0;//rate range : -10 - 10virtual int rate() = 0; virtual void setRate(int rate) = 0;//volume range : 0 - 100virtual int volume() = 0;virtual void setVolume(int value) = 0;virtual bool isSpeaking() = 0;signals:void speakComplete();};
Although we use it on Windows 7 later, since we may develop TTS on XP and Linux in the future, we define an interface, in our call class, we only need to define a pointer to an interface, and then initialize TTS instances in different environments during initialization according to the actual environment. This will be demonstrated later.
Based on the TTS on win7, we will create the win7tts class, which inherits from the ITTS defined above.
Win7TSclass Win7TTS :public ITTS{Q_OBJECT public:Win7TTS(void);virtual ~Win7TTS(void);virtual bool initSpeech();virtual bool speak(QString & txt);virtual void pause();virtual void resume();virtual void stop();//rate range : -10 - 10virtual int rate(); virtual void setRate(int rate);//volume range : 0 - 100virtual int volume();virtual void setVolume(int value) ;virtual bool isSpeaking();private slots:void dealevent(QString name, int arc , void* argv);private:QAxObject _voice;bool _binit;bool _bReading;};
_ Voice is a member used to create an ispvoice COM object.
Win7TTSWin7TTS::Win7TTS(void) :_binit(0),_bReading(0){}Win7TTS::~Win7TTS(void){}bool Win7TTS::initSpeech(){if(_binit)return true;_binit = this->_voice.setControl("96749377-3391-11D2-9EE3-00C04F797396");if(_binit){connect(&this->_voice,SIGNAL(signal(QString, int, void*)), this, SLOT(dealevent(QString, int, void*)));}return _binit;}bool Win7TTS::speak(QString & txt){if(!_binit)return false;int result = this->_voice.dynamicCall("Speak(QString, SpeechVoiceSpeakFlags)", txt ,1).toInt();_bReading = true;return result;}void Win7TTS::pause(){if(!_binit)return;_bReading = false;this->_voice.dynamicCall("Pause()");}void Win7TTS::resume(){if(!_binit)return;_bReading = true;this->_voice.dynamicCall("Resume()");}void Win7TTS::stop(){if(!_binit)return;_bReading = false;int result = this->_voice.dynamicCall("Speak(QString, SpeechVoiceSpeakFlags)", "" ,2).toInt();}bool Win7TTS::isSpeaking(){return _bReading;}//rate range : -10 - 10int Win7TTS::rate(){if(!_binit)return -99999;return this->_voice.property("Rate").toInt();} void Win7TTS::setRate(int rate){if(!_binit)return;this->_voice.dynamicCall("SetRate(int)", rate);}//volume range : 0 - 100int Win7TTS::volume(){if(!_binit)return -99999;return this->_voice.property("Volume").toInt();}void Win7TTS::setVolume(int value) {if(!_binit)return;this->_voice.dynamicCall("SetVolume(int)", value);}void Win7TTS::dealevent(QString name, int arc , void* argv){if(name == "EndStream(int,QVariant)"){_bReading = false;emit speakComplete();}}
The TTS framework has been set up. Now let's try the charm of TTS. Upload a TTS sample later and download it for fun!
Win7 TTS example