Using MS speech sdk5.1 in QT for text reading

Source: Internet
Author: User

1. First, let's talk about your environment configuration. It doesn't make sure This Code applies to all configurations.

Operating System: Windows XP SP3

Compiling environment: QT sdk1.2.1 and msvc2008

Since Microsoft Speech SDK is used, you must first install it:

Http://www.microsoft.com/en-us/download/details.aspx? Id = 10121

Download the two images in the red box. After the download is complete, install speechsdk51.exeand then install speechsdk51langpack.exe,

Follow the default installation path and use the path in the code.

Because the sphelper. h header file is used in the code, errors may occur during compilation. For the solution, see the following link:

Http://blog.csdn.net/wangyangtao/article/details/5933734

2. We encapsulate the call to the MS speech sdk5.1 API in the speech class, and then call the interface provided by the class to read the text.

The following describes how to implement the speech class. Let's talk about the code.

First, add the following code to the Pro file:

    INCLUDEPATH += -L"C:/Program Files/Microsoft Speech SDK 5.1/Include"    LIBS += -L"C:/Program Files/Microsoft Speech SDK 5.1/Lib/i386"

The following is the header file speechclass. h.

#ifndef SPEECHCLASS_H#define SPEECHCLASS_H#undef UNICODE#include <sapi.h>#include <sphelper.h>#include <comdef.h>#define UNICODE#include <windows.h>#include <windowsx.h>#include <commctrl.h>#include <QString>class speech{public:    speech();    ~speech();    void speak(QString text);    void setVolume(int volume);    void setRate(int rate);    void pause();    void resume();    void finish();    int  getVolume();    int  getRate();private:    void sounding(QString s, int voice);    void speak(QString s, int mode);    HRESULT                   hr;    CComPtr<ISpObjectToken>   pToken;    CComPtr<ISpVoice>         pVoice;    WCHAR *pChnToken;    WCHAR *pEngToken;    bool   over;};#endif // SPEECHCLASS_H

The following is the source file speechclass. cpp.

# Include "speechclass. H "# define Eng 0x00 # define CHN 0x01 // constructor speech: Speech () {hR = s_ OK; wchar * W1 = l "HKEY_LOCAL_MACHINE \ Software \ Microsoft \ speech \ voices \ tokens \ mssimplifiedchinesevoice "; wchar * W2 = l "HKEY_LOCAL_MACHINE \ Software \ Microsoft \ speech \ voices \ tokens \ msmike"; pchntoken = W1; pengtoken = W2; if (succeeded (HR) HR = pvoice. cocreateinstance (clsid_spvoice); over = true;} Speec H ::~ Speech () {pchntoken = NULL; pengtoken = NULL ;} /*************************************** ***************************** // convert Text to Speech, supports Chinese and English hybrid reading /*********************************** * *********************************/void speech:: Speak (qstring text) {qchar curr; qstring STR; int flagcur; int flagpre (CHN); int CNT (0); int first (0); int Len = text. length (); For (INT I = 0; I <Len; ++ I) {curr = text. at (I ); // Obtain the IF (curr> = 32 & curr <= 47) character at position I | (curr> = 58 & curr <= 64 )) flagcur = flagpre; // if it is a space or punctuation, the flag remains unchanged else {If (curr> = 'A' & curr <= 'Z ') | (curr> = 'A' & curr <= 'Z') flagcur = ENG; else flagcur = CHN;} If (flagcur = flagpre) CNT ++; else {If (CNT! = 0) // character type change, read the previous text {STR = text. mid (first, CNT); sounding (STR, flagpre);} flagpre = flagcur; first = I; CNT = 1 ;}// read the last section STR = text. mid (first, CNT); sounding (STR, flagcur );} /*************************************** ***************************** // read the text S, if voice = ENG, read English, if voice = CHN, read Chinese /************************************* * *******************************/void speech:: Sounding (qstring S, int voice) {// convert s to wchar type string wchar * w; W = new wchar [S. length () + 1]; S. towchararray (w); W [S. length ()] = 0; // select the pronunciation type based on The Voice Value, Chinese or English if (succeeded (HR) {If (voice = ENG) hR = spgettokenfromid (pengtoken, & ptoken); else if (voice = CHN) HR = spgettokenfromid (pchntoken, & ptoken);} If (succeeded (HR )) hR = pvoice-> setvoice (ptoken); // pronunciation if (succeeded (HR) HR = pvoice-> speak (W, spf_default | svsflagsasync, null ); // release the token ptoken. release (); Delete W ;} /*************************************** ***************************** // obtain the current voice volume/ **************************************** * ****************************/INT speech:: getvolume () {ushort V; If (succeeded (HR) HR = pvoice-> getvolume (& V); Return (INT) V ;} /*************************************** ***************************** // obtain the current voice speed/ **************************************** * ****************************/INT speech:: getrate () {long R; If (succeeded (HR) HR = pvoice-> getrate (& R); Return (INT) r ;} /*************************************** ***************************** // set the voice volume, the size is volume /************************************ * ********************************/void speech:: setvolume (INT volume) {If (succeeded (HR) HR = pvoice-> setvolume (ushort) volume );} /*************************************** ***************************** // set the voice speed, the size is rate /************************************ * ********************************/void speech:: setrate (INT rate) {If (succeeded (HR) HR = pvoice-> setrate (long) rate );} /*************************************** ***************************** // pause the current playback /** **************************************** * **************************/void speech:: Pause () {over = false; If (succeeded (HR) HR = pvoice-> pause ();} /*************************************** ***************************** // continue the current playback /** **************************************** * **************************/void speech:: Resume () {over = true; If (succeeded (HR) HR = pvoice-> resume ();} /*************************************** ****************************** // end the current playback /** **************************************** * **************************/void speech:: Finish () {over = true; If (succeeded (HR) HR = pvoice-> speak (null, spf_purgebeforespeak, 0 );}

This speech class is used as a small example to illustrate how to use the speech class in QT.

: Http://download.csdn.net/detail/qin_lin_sb/4462802

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.