00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00037
00038 #include "plugin.h"
00039 #include <iostream>
00040
00041
00043 void __stdcall NotifyCallbackFunction(WPARAM wParam, LPARAM lParam)
00044 {
00045 Speech* pThis = (Speech*)lParam;
00046 pThis->callbackEvent();
00047 }
00048
00049
00051 Speech::Speech()
00052 {
00053 HRESULT hr = S_OK;
00054 s_text = NULL;
00055 pVoice = NULL;
00056 m_bPause = FALSE;
00057 m_bStop = TRUE;
00058 m_DefaultRate = NULL;
00059 m_DefaultVolume = NULL;
00060
00061
00062 hr = CoCreateInstance(CLSID_SpVoice, NULL, CLSCTX_ALL, IID_ISpVoice, (void **)&this->pVoice);
00063
00064
00065 if( SUCCEEDED( hr ))
00066 hr = pVoice->SetInterest( SPFEI_ALL_TTS_EVENTS, SPFEI_ALL_TTS_EVENTS );
00067 else
00068 MMechostr(MSKDEBUG,"Error CoCreateInstance\n");
00069
00070 if( SUCCEEDED( hr ))
00071 hr = pVoice->SetNotifyCallbackFunction(NotifyCallbackFunction, 0, LPARAM(this));
00072 else
00073 MMechostr(MSKDEBUG,"Error SetInterest\n");
00074
00075
00076 if( SUCCEEDED( hr ) )
00077 hr = pVoice->GetRate( &m_DefaultRate );
00078 else
00079 MMechostr(MSKDEBUG,"Error SetNotifyCallbackFunction\n");
00080 if( SUCCEEDED( hr ) )
00081 hr = pVoice->GetVolume( &m_DefaultVolume );
00082 else
00083 MMechostr(MSKDEBUG,"Error GetRate\n");
00084 }
00085
00086
00088 void Speech::callbackEvent()
00089 {
00090 CSpEvent event;
00091
00092 SPVOICESTATUS Stat;
00093 HRESULT hr = S_OK;
00094
00095
00096 WPARAM nStart;
00097 LPARAM nEnd;
00098
00099 std::string word,text,phrase;
00100 cbData * cbdataWord;
00101 cbData * cbdataPhrase;
00102 cbData *cbdataPhoneme;
00103 char * test ;
00104
00105 while( event.GetFrom(pVoice) == S_OK )
00106 {
00107 switch( event.eEventId )
00108 {
00109 case SPEI_START_INPUT_STREAM:
00110 MMechostr(MSKDEBUG,"StartStream event\n") ;
00111 break;
00112
00113 case SPEI_END_INPUT_STREAM:
00114 m_bStop = TRUE;
00115 m_bPause = FALSE;
00116 MMechostr(MSKDEBUG,"EndStream event\n");
00117 PostMessage(HScol, SPEECH_END_CB, (int)this,(LPARAM)NULL);
00118 MMechostr(MSKDEBUG,"EndStream event OK \n");
00119 break;
00120
00121
00122
00123
00124
00125 case SPEI_WORD_BOUNDARY:
00126 hr = pVoice->GetStatus( &Stat, NULL );
00127 text = s_text;
00128 word = text.substr(Stat.ulInputWordPos,Stat.ulInputWordLen);
00129
00130 test = new char[word.size() + 1];
00131 strcpy(test,word.c_str());
00132 cbdataWord = new cbData(test);
00133 PostMessage(HScol, SPEECH_WORD_CB, (int)this,(LPARAM)cbdataWord);
00134
00135 if( FAILED( hr ) )
00136 MMechostr(MSKDEBUG,"Voice GetStatus error\n");
00137 break;
00138
00139
00140 case SPEI_SENTENCE_BOUNDARY:
00141 hr = pVoice->GetStatus( &Stat, NULL );
00142 text = s_text;
00143 phrase = text.substr(Stat.ulInputSentPos,Stat.ulInputSentLen);
00144
00145 test = new char[phrase.size() + 1];
00146 strcpy(test,phrase.c_str());
00147 cbdataPhrase = new cbData(test);
00148 PostMessage(HScol, SPEECH_TEXT_CB, (int)this,(LPARAM)cbdataPhrase);
00149 break;
00150
00151 case SPEI_VISEME:
00152 PostMessage(HScol, SPEECH_PHONEME_CB, (int)this,(LPARAM)(event.Viseme()));
00153 break;
00154
00155 case SPEI_PHONEME:
00156 break;
00157
00158 case SPEI_TTS_PRIVATE:
00159
00160 break;
00161
00162
00163 default:
00164 break;
00165 }
00166 }
00167 }
00168
00169
00171 Speech::~Speech()
00172 {
00173 pVoice->Release();
00174 pVoice = NULL;
00175 }
00176
00177
00179 void Speech::SetSpeechText(char* text)
00180 {
00181 this->s_text = text;
00182 }
00183
00184
00186 void Speech::play()
00187 {
00188 HRESULT hr = S_OK;
00189
00190 if( SUCCEEDED( hr ) )
00191 {
00192 m_bStop = FALSE;
00193 if( !m_bPause )
00194 {
00195 wchar_t * w_text = convertCharToLPCWSTR(s_text);
00196 hr = pVoice->Speak(w_text, SPF_ASYNC|SPF_IS_XML|SPF_PURGEBEFORESPEAK, 0);
00197 if( FAILED( hr ))
00198 MMechostr(MSKDEBUG,"Error : Speak\n");
00199
00200 delete []w_text;
00201 }
00202
00203 pVoice->Resume();
00204 m_bPause = FALSE;
00205 }
00206 }
00207
00208
00210 void Speech::pauseResume()
00211 {
00212 if( !m_bStop )
00213 {
00214 if( !m_bPause )
00215 {
00216
00217 pVoice->Pause();
00218 m_bPause = TRUE;
00219 }
00220 }
00221 }
00222
00223
00225 void Speech::stop()
00226 {
00227
00228 if( FAILED( pVoice->Speak( NULL, SPF_PURGEBEFORESPEAK, 0 ) ) )
00229 MMechostr(MSKDEBUG,">>>>>>>> Stop error\n");
00230
00231 m_bPause = FALSE;
00232 m_bStop = TRUE;
00233 }
00234
00235
00237 int Speech::getVolumeSpeech()
00238 {
00239
00240 HRESULT hr = S_OK;
00241 int volumeSpeech = 0;
00242 hr = pVoice->GetVolume((USHORT*)&volumeSpeech);
00243 if(FAILED(hr ))
00244 return -1;
00245 else
00246 return volumeSpeech;
00247 }
00248
00249
00251 void Speech::setVolumeSpeech(int volumeSpeech)
00252 {
00253 this->pVoice->SetVolume((USHORT)volumeSpeech);
00254 }
00255
00256
00258 int Speech::getRateSpeech()
00259 {
00260
00261 HRESULT hr = S_OK;
00262 int rateSpeech = 0;
00263 hr = pVoice->GetRate((long*)&rateSpeech);
00264 if(FAILED(hr ))
00265 return -1;
00266 else
00267 return rateSpeech;
00268 }
00269
00270
00272 void Speech::setRateSpeech(int rateSpeech)
00273 {
00274 this->pVoice->SetRate(rateSpeech);
00275 }
00276
00277
00279 std::list <char *> Speech::getVoiceAll()
00280 {
00281 ISpObjectToken* pToken;
00282 IEnumSpObjectTokens* cpIEnum;
00283
00284
00285 HRESULT hr = S_OK;
00286 std::list <char *> lParamSp;
00287 ULONG sizel;
00288
00289 hr = SpEnumTokens(SPCAT_VOICES, NULL, NULL, &cpIEnum);
00290
00291 if (FAILED(hr))
00292 {
00293 MMechostr(MSKDEBUG,"getVoiceAll -> Erreur : EnumTokens\n");
00294 return lParamSp;
00295 }
00296 else
00297 {
00298 cpIEnum->GetCount(&sizel);
00299 if (sizel <=0)
00300 {
00301 MMechostr(MSKDEBUG,"getVoiceAll -> Erreur : EnumTokens\n");
00302 return lParamSp;
00303 }
00304 else
00305 while (cpIEnum->Next(1, &pToken, NULL) == S_OK)
00306 {
00307 CSpDynamicString dstrText;
00308 hr = SpGetDescription(pToken, &dstrText);
00309 if (SUCCEEDED(hr))
00310 lParamSp.push_back(dstrText.CopyToChar());
00311 else
00312 MMechostr(MSKDEBUG,"getVoiceAll -> SpGetDescription : ERROR\n");
00313 pToken->Release();
00314
00315
00316
00317 dstrText.Clear() ;
00318 delete dstrText ;
00319 }
00320 }
00321 cpIEnum->Release();
00322
00323 return lParamSp;
00324 }
00325
00326
00328 void Speech::SetSpeechVoice( char * voice)
00329 {
00330 HRESULT hr = S_OK;
00331 IEnumSpObjectTokens* cpIEnum;
00332 ISpObjectToken* pToken;
00333 bool testVoice = true;
00334
00335 hr = SpEnumTokens(SPCAT_VOICES, NULL, NULL, &cpIEnum);
00336 if (SUCCEEDED(hr))
00337 {
00338 wchar_t * dstrTextTest ;
00339 char *lpszVoiceId;
00340
00341 while ((cpIEnum->Next(1, &pToken, NULL) == S_OK) &&(testVoice == true))
00342 {
00343 hr = SpGetDescription(pToken, &dstrTextTest);
00344 char * textV = convertWcharToChar(dstrTextTest);
00345
00346 MMechostr(MSKDEBUG,"hamza 111111 :::: %s\n",textV);
00347
00348 if (SUCCEEDED(hr))
00349 {
00350 if (strcmp(textV,voice)==0)
00351 {
00352 hr = pVoice->SetVoice(pToken);
00353 if (SUCCEEDED(hr))
00354 MMechostr(MSKDEBUG,"SetSpeechVoice -> SetVoice : OK \n");
00355 else
00356 MMechostr(MSKDEBUG,"SetSpeechVoice -> SetVoice : ERROR\n");
00357 testVoice = false;
00358 }
00359 else
00360 MMechostr(MSKDEBUG,"SetSpeechVoice -> lpszVoiceId <> voice\n");
00361 }
00362 else
00363 MMechostr(MSKDEBUG,"SetSpeechVoice -> SpGetDescription : ERROR\n");
00364
00365 pToken->Release();
00366 }
00367 }
00368 else
00369 MMechostr(MSKDEBUG,"SetSpeechVoice -> Erreur EnumTokens : \n");
00370
00371 cpIEnum->Release();
00372 }
00373
00374
00376 char * Speech::getSpeechVoice()
00377 {
00378 ISpObjectToken* pToken;
00379 char * token;
00380
00381 HRESULT hr = S_OK;
00382 CSpDynamicString dstrText;
00383
00384 pVoice->GetVoice(&pToken);
00385 hr = SpGetDescription(pToken, &dstrText);
00386 token = dstrText.CopyToChar();
00387
00388 dstrText.Clear() ;
00389 delete dstrText ;
00390 pToken->Release();
00391
00392 return token;
00393 }
00394
00395