Merge PR #3071: TextToSpeech_unix: lazy initialize speech-dispatcher.

This commit is contained in:
Mikkel Krautz 2017-05-08 21:02:45 +02:00 committed by GitHub
commit d3470c30bb

View File

@ -21,6 +21,11 @@ class TextToSpeechPrivate {
#ifdef USE_SPEECHD
protected:
SPDConnection *spd;
/// Used to store the requested volume of the TextToSpeech object
/// before speech-dispatcher has been initialized.
int volume;
bool initialized;
void ensureInitialized();
#endif
public:
TextToSpeechPrivate();
@ -31,6 +36,23 @@ class TextToSpeechPrivate {
#ifdef USE_SPEECHD
TextToSpeechPrivate::TextToSpeechPrivate() {
initialized = false;
volume = -1;
spd = NULL;
}
TextToSpeechPrivate::~TextToSpeechPrivate() {
if (spd) {
spd_close(spd);
spd = NULL;
}
}
void TextToSpeechPrivate::ensureInitialized() {
if (initialized) {
return;
}
spd = spd_open("Mumble", NULL, NULL, SPD_MODE_THREADED);
if (! spd) {
qWarning("TextToSpeech: Failed to contact speech dispatcher.");
@ -56,21 +78,27 @@ TextToSpeechPrivate::TextToSpeechPrivate() {
if (spd_set_spelling(spd, SPD_SPELL_ON) != 0)
qWarning("TextToSpeech: Failed to set spelling mode.");
}
}
TextToSpeechPrivate::~TextToSpeechPrivate() {
if (spd) {
spd_close(spd);
spd = NULL;
initialized = true;
if (volume != -1) {
setVolume(volume);
}
}
void TextToSpeechPrivate::say(const QString &txt) {
ensureInitialized();
if (spd)
spd_say(spd, SPD_MESSAGE, txt.toUtf8());
}
void TextToSpeechPrivate::setVolume(int vol) {
if (!initialized) {
volume = vol;
return;
}
if (spd)
spd_set_volume(spd, vol * 2 - 100);
}