2013-04-24 14:52:04 +00:00
|
|
|
#include "speechclient.hh"
|
|
|
|
|
|
|
|
#include <windows.h>
|
|
|
|
#include "speechhlp.hh"
|
|
|
|
|
2013-04-24 16:01:44 +00:00
|
|
|
#include <QtCore>
|
|
|
|
|
2013-04-24 14:52:04 +00:00
|
|
|
struct SpeechClient::InternalData
|
|
|
|
{
|
2013-04-26 13:41:39 +00:00
|
|
|
InternalData( Config::VoiceEngine const & e ):
|
2013-04-24 14:52:04 +00:00
|
|
|
waitingFinish( false )
|
2013-04-26 13:41:39 +00:00
|
|
|
, engine( e )
|
|
|
|
, oldVolume( -1 )
|
|
|
|
, oldRate( -1 )
|
2013-04-24 14:52:04 +00:00
|
|
|
{
|
2013-04-26 13:41:39 +00:00
|
|
|
sp = speechCreate( e.id.toStdWString().c_str() );
|
2013-04-24 14:52:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
~InternalData()
|
|
|
|
{
|
|
|
|
speechDestroy( sp );
|
|
|
|
}
|
|
|
|
|
|
|
|
SpeechHelper sp;
|
|
|
|
bool waitingFinish;
|
2013-04-26 13:41:39 +00:00
|
|
|
Engine engine;
|
|
|
|
int oldVolume;
|
|
|
|
int oldRate;
|
2013-04-24 14:52:04 +00:00
|
|
|
};
|
|
|
|
|
2013-04-26 13:41:39 +00:00
|
|
|
SpeechClient::SpeechClient( Config::VoiceEngine const & e, QObject * parent ):
|
2013-04-24 14:52:04 +00:00
|
|
|
QObject( parent ),
|
2013-04-26 13:41:39 +00:00
|
|
|
internalData( new InternalData( e ) )
|
2013-04-24 14:52:04 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
SpeechClient::~SpeechClient()
|
|
|
|
{
|
|
|
|
delete internalData;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool enumEngines( void * /* token */,
|
|
|
|
const wchar_t * id,
|
|
|
|
const wchar_t * name,
|
|
|
|
void * userData )
|
|
|
|
{
|
2013-04-24 16:01:44 +00:00
|
|
|
SpeechClient::Engines * pEngines = ( SpeechClient::Engines * )userData;
|
2013-04-26 13:41:39 +00:00
|
|
|
SpeechClient::Engine engine( Config::VoiceEngine(
|
2013-04-24 14:52:04 +00:00
|
|
|
QString::fromWCharArray( id ),
|
2013-04-26 13:41:39 +00:00
|
|
|
QString::fromWCharArray( name ),
|
|
|
|
50, 50 ) );
|
2013-04-24 14:52:04 +00:00
|
|
|
pEngines->push_back( engine );
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
SpeechClient::Engines SpeechClient::availableEngines()
|
|
|
|
{
|
|
|
|
Engines engines;
|
|
|
|
speechEnumerateAvailableEngines( enumEngines, &engines );
|
|
|
|
return engines;
|
|
|
|
}
|
|
|
|
|
|
|
|
const SpeechClient::Engine & SpeechClient::engine() const
|
|
|
|
{
|
|
|
|
return internalData->engine;
|
|
|
|
}
|
|
|
|
|
2013-04-26 13:41:39 +00:00
|
|
|
bool SpeechClient::tell( QString const & text, int volume, int rate )
|
2013-04-24 14:52:04 +00:00
|
|
|
{
|
|
|
|
if ( !speechAvailable( internalData->sp ) )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if ( internalData->waitingFinish )
|
|
|
|
return false;
|
|
|
|
|
2013-04-26 13:41:39 +00:00
|
|
|
if( volume < 0 )
|
|
|
|
volume = engine().volume;
|
|
|
|
if( rate < 0 )
|
|
|
|
rate = engine().rate;
|
|
|
|
|
|
|
|
internalData->oldVolume = setSpeechVolume( internalData->sp, volume );
|
|
|
|
internalData->oldRate = setSpeechRate( internalData->sp, rate );
|
|
|
|
|
2013-04-24 16:01:44 +00:00
|
|
|
bool ok = speechTell( internalData->sp, text.toStdWString().c_str() );
|
2013-04-26 13:41:39 +00:00
|
|
|
|
2013-04-24 16:01:44 +00:00
|
|
|
emit started( ok );
|
|
|
|
|
|
|
|
if ( ok )
|
|
|
|
{
|
|
|
|
internalData->waitingFinish = true;
|
|
|
|
startTimer( 50 );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
emit finished();
|
|
|
|
}
|
|
|
|
return ok;
|
2013-04-24 14:52:04 +00:00
|
|
|
}
|
|
|
|
|
2013-04-26 13:41:39 +00:00
|
|
|
bool SpeechClient::say( QString const & text, int volume, int rate )
|
2013-04-24 14:52:04 +00:00
|
|
|
{
|
|
|
|
if ( !speechAvailable( internalData->sp ) )
|
|
|
|
return false;
|
|
|
|
|
2013-04-26 13:41:39 +00:00
|
|
|
if( volume < 0 )
|
|
|
|
volume = engine().volume;
|
|
|
|
if( rate < 0 )
|
|
|
|
rate = engine().rate;
|
|
|
|
|
|
|
|
int oldVolume = setSpeechVolume( internalData->sp, volume );
|
|
|
|
int oldRate = setSpeechRate( internalData->sp, rate );
|
|
|
|
|
|
|
|
bool ok = speechSay( internalData->sp, text.toStdWString().c_str() );
|
|
|
|
|
|
|
|
if( oldVolume >=0 )
|
|
|
|
setSpeechVolume( internalData->sp, oldVolume );
|
|
|
|
if( oldRate >=0 )
|
|
|
|
setSpeechRate( internalData->sp, oldRate );
|
|
|
|
|
|
|
|
return ok;
|
2013-04-24 14:52:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SpeechClient::timerEvent( QTimerEvent * evt )
|
|
|
|
{
|
|
|
|
QObject::timerEvent( evt );
|
|
|
|
|
|
|
|
if ( !internalData->waitingFinish )
|
|
|
|
return;
|
|
|
|
|
|
|
|
if ( speechTellFinished( internalData->sp ) )
|
|
|
|
{
|
2013-04-24 16:01:44 +00:00
|
|
|
killTimer( evt->timerId() ) ;
|
|
|
|
internalData->waitingFinish = false;
|
2013-04-26 13:41:39 +00:00
|
|
|
|
|
|
|
if( internalData->oldVolume >=0 )
|
|
|
|
setSpeechVolume( internalData->sp, internalData->oldVolume );
|
|
|
|
if( internalData->oldRate >=0 )
|
|
|
|
setSpeechRate( internalData->sp, internalData->oldRate );
|
|
|
|
internalData->oldVolume = -1;
|
|
|
|
internalData->oldRate = -1;
|
|
|
|
|
2013-04-24 16:01:44 +00:00
|
|
|
emit finished();
|
2013-04-24 14:52:04 +00:00
|
|
|
}
|
|
|
|
}
|