forked from goldendict/goldendict
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspeechclient_mac.mm
191 lines (155 loc) · 4.71 KB
/
speechclient_mac.mm
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#include "speechclient.hh"
#include <QtCore>
#include <AppKit/NSSpeechSynthesizer.h>
#include <Foundation/NSArray.h>
#include <Foundation/NSError.h>
#include <Foundation/NSString.h>
#include <Foundation/NSAutoreleasePool.h>
static QString NSStringToQString(const NSString * nsstr )
{
return QString::fromUtf8( [ nsstr UTF8String ] );
}
static NSString * QStringToNSString( QString const & qstr, bool needAlloc = false )
{
if( needAlloc )
return [ [ NSString alloc ] initWithUTF8String : qstr.toUtf8().data() ];
return [ NSString stringWithUTF8String : qstr.toUtf8().data() ];
}
struct SpeechClient::InternalData
{
InternalData( Config::VoiceEngine const & e ):
waitingFinish( false )
, engine( e )
, oldVolume( -1 )
, oldRate( -1 )
, stringToPlay( nil )
{
NSAutoreleasePool * pool = [ [ NSAutoreleasePool alloc ] init ];
sp = [ [ NSSpeechSynthesizer alloc ] initWithVoice : QStringToNSString( e.id ) ];
[ pool drain ];
}
~InternalData()
{
[ sp release ];
if( stringToPlay != nil )
[ stringToPlay release ];
}
NSSpeechSynthesizer * sp;
bool waitingFinish;
SpeechClient::Engine engine;
float oldVolume;
float oldRate;
QString oldMode;
NSString * stringToPlay;
};
SpeechClient::SpeechClient( Config::VoiceEngine const & e, QObject * parent ):
QObject( parent ),
internalData( new InternalData( e ) )
{
}
SpeechClient::~SpeechClient()
{
delete internalData;
}
SpeechClient::Engines SpeechClient::availableEngines()
{
Engines engines;
NSAutoreleasePool * pool = [ [ NSAutoreleasePool alloc ] init ];
NSArray * voices = [ NSSpeechSynthesizer availableVoices ];
int voicesNum = [ voices count ];
for( int i = 0; i < voicesNum; i++ )
{
QString id = NSStringToQString( [ voices objectAtIndex : i ] );
QString name;
int n = id.lastIndexOf( '.' );
if( n >= 0 )
name = id.right( id.size() - n - 1 );
else
name = id;
engines.push_back( SpeechClient::Engine( Config::VoiceEngine(
id, name, 50, 50 ) ) );
}
[ pool drain ];
return engines;
}
const SpeechClient::Engine & SpeechClient::engine() const
{
return internalData->engine;
}
bool SpeechClient::tell( QString const & text, int volume, int rate )
{
if( !internalData->sp || [ NSSpeechSynthesizer isAnyApplicationSpeaking ] )
return false;
if ( internalData->waitingFinish )
return false;
if( volume < 0 )
volume = engine().volume;
if( rate < 0 )
rate = engine().rate;
NSAutoreleasePool * pool = [ [ NSAutoreleasePool alloc ] init ];
internalData->oldVolume = [ internalData->sp volume ];
[ internalData->sp setVolume : ( volume / 100.0 ) ];
internalData->oldRate = [ internalData->sp rate ];
[ internalData->sp setRate : ( rate * 2.0 + 100 ) ];
NSError * err = nil;
NSString * oldMode = [ internalData->sp objectForProperty : NSSpeechInputModeProperty error : &err ];
if( err == nil || [ err code ] == 0 )
{
internalData->oldMode = NSStringToQString( oldMode );
[ internalData->sp setObject : NSSpeechModeText forProperty : NSSpeechInputModeProperty error : &err ];
}
else
internalData->oldMode.clear();
internalData->stringToPlay = QStringToNSString( text, true );
bool ok = [ internalData->sp startSpeakingString : internalData->stringToPlay ];
emit started( ok );
if ( ok )
{
internalData->waitingFinish = true;
startTimer( 50 );
}
else
{
if( internalData->stringToPlay != nil )
[ internalData->stringToPlay release ];
internalData->stringToPlay = nil;
emit finished();
}
[ pool drain ];
return ok;
}
bool SpeechClient::say( QString const & text, int volume, int rate )
{
(void) text;
(void) volume;
(void) rate;
return false;
}
void SpeechClient::timerEvent( QTimerEvent * evt )
{
QObject::timerEvent( evt );
if ( !internalData->waitingFinish )
return;
if ( ![ internalData->sp isSpeaking ] )
{
killTimer( evt->timerId() ) ;
internalData->waitingFinish = false;
NSAutoreleasePool * pool = [ [ NSAutoreleasePool alloc ] init ];
if( internalData->oldVolume >=0 )
[ internalData->sp setVolume : internalData->oldVolume ];
if( internalData->oldRate >=0 )
[ internalData->sp setRate : internalData->oldRate ];
internalData->oldVolume = -1;
internalData->oldRate = -1;
NSError * err;
if( !internalData->oldMode.isEmpty() )
[ internalData->sp setObject : QStringToNSString( internalData->oldMode )
forProperty : NSSpeechInputModeProperty error : &err ];
internalData->oldMode.clear();
if( internalData->stringToPlay != nil )
[ internalData->stringToPlay release ];
internalData->stringToPlay = nil;
[ pool drain ];
emit finished();
}
}