forked from goldendict/goldendict
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspeechclient_win.cc
144 lines (116 loc) · 3.1 KB
/
speechclient_win.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include "speechclient.hh"
#include <windows.h>
#include "speechhlp.hh"
#include <QtCore>
struct SpeechClient::InternalData
{
InternalData( Config::VoiceEngine const & e ):
waitingFinish( false )
, engine( e )
, oldVolume( -1 )
, oldRate( -1 )
{
sp = speechCreate( e.id.toStdWString().c_str() );
}
~InternalData()
{
speechDestroy( sp );
}
SpeechHelper sp;
bool waitingFinish;
Engine engine;
int oldVolume;
int oldRate;
};
SpeechClient::SpeechClient( Config::VoiceEngine const & e, QObject * parent ):
QObject( parent ),
internalData( new InternalData( e ) )
{
}
SpeechClient::~SpeechClient()
{
delete internalData;
}
static bool enumEngines( void * /* token */,
const wchar_t * id,
const wchar_t * name,
void * userData )
{
SpeechClient::Engines * pEngines = ( SpeechClient::Engines * )userData;
SpeechClient::Engine engine( Config::VoiceEngine(
QString::fromWCharArray( id ),
QString::fromWCharArray( name ),
50, 50 ) );
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;
}
bool SpeechClient::tell( QString const & text, int volume, int rate )
{
if ( !speechAvailable( internalData->sp ) )
return false;
if ( internalData->waitingFinish )
return false;
if( volume < 0 )
volume = engine().volume;
if( rate < 0 )
rate = engine().rate;
internalData->oldVolume = setSpeechVolume( internalData->sp, volume );
internalData->oldRate = setSpeechRate( internalData->sp, rate );
bool ok = speechTell( internalData->sp, text.toStdWString().c_str() );
emit started( ok );
if ( ok )
{
internalData->waitingFinish = true;
startTimer( 50 );
}
else
{
emit finished();
}
return ok;
}
bool SpeechClient::say( QString const & text, int volume, int rate )
{
if ( !speechAvailable( internalData->sp ) )
return false;
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;
}
void SpeechClient::timerEvent( QTimerEvent * evt )
{
QObject::timerEvent( evt );
if ( !internalData->waitingFinish )
return;
if ( speechTellFinished( internalData->sp ) )
{
killTimer( evt->timerId() ) ;
internalData->waitingFinish = false;
if( internalData->oldVolume >=0 )
setSpeechVolume( internalData->sp, internalData->oldVolume );
if( internalData->oldRate >=0 )
setSpeechRate( internalData->sp, internalData->oldRate );
internalData->oldVolume = -1;
internalData->oldRate = -1;
emit finished();
}
}