File: texttospeech.cpp

package info (click to toggle)
ktextaddons 1.9.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 17,856 kB
  • sloc: cpp: 62,045; ansic: 6,520; xml: 2,630; sh: 11; makefile: 7
file content (212 lines) | stat: -rw-r--r-- 5,556 bytes parent folder | download
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*
   SPDX-FileCopyrightText: 2014-2026 Laurent Montel <montel@kde.org>

   SPDX-License-Identifier: LGPL-2.0-or-later
*/

#include "texttospeech.h"
#include "textedittexttospeech_debug.h"

#include "texttospeechutil.h"

#include <QLocale>
#include <QTextToSpeech>
#include <QVector>

using namespace Qt::Literals::StringLiterals;
class TextEditTextToSpeech::TextToSpeechPrivate
{
public:
    QString mDefaultEngine;
    QTextToSpeech *mTextToSpeech = nullptr;
    qsizetype mCurrentTextToSpeechId = -1;
    bool initialized = false;
};

using namespace TextEditTextToSpeech;
TextToSpeech::TextToSpeech(QObject *parent)
    : QObject(parent)
    , d(new TextEditTextToSpeech::TextToSpeechPrivate)
{
}

TextToSpeech::~TextToSpeech() = default;

void TextToSpeech::reloadSettings()
{
    d->initialized = true;
    const TextEditTextToSpeech::TextToSpeechUtil::TextToSpeechSettings settings = TextEditTextToSpeech::TextToSpeechUtil::loadSettings();
    qCDebug(TEXTEDITTEXTTOSPEECH_LOG) << settings;
    const QString engineName = settings.engineName;
    if (d->mDefaultEngine != engineName) {
        if (d->mTextToSpeech) {
            if (d->mTextToSpeech->engine() != engineName) {
                disconnect(d->mTextToSpeech, &QTextToSpeech::stateChanged, this, &TextToSpeech::slotStateChanged);
                disconnect(d->mTextToSpeech, &QTextToSpeech::aboutToSynthesize, this, &TextToSpeech::slotAboutToSynthesize);
                delete d->mTextToSpeech;
                d->mTextToSpeech = nullptr;
            }
        }
    }
    if (!d->mTextToSpeech) {
        d->mTextToSpeech = new QTextToSpeech(engineName, this);
        connect(d->mTextToSpeech, &QTextToSpeech::stateChanged, this, &TextToSpeech::slotStateChanged);
        connect(d->mTextToSpeech, &QTextToSpeech::aboutToSynthesize, this, &TextToSpeech::slotAboutToSynthesize);
    }
    d->mDefaultEngine = engineName;
    const int rate = settings.rate;
    const double rateDouble = rate / 100.0;
    d->mTextToSpeech->setRate(rateDouble);
    const int pitch = settings.pitch;
    const double pitchDouble = pitch / 100.0;
    d->mTextToSpeech->setPitch(pitchDouble);
    const int volumeValue = settings.volumeValue;
    const double volumeDouble = volumeValue / 100.0;
    d->mTextToSpeech->setVolume(volumeDouble);
    d->mTextToSpeech->setLocale(QLocale(settings.localeName));
    d->mTextToSpeech->setVoice(settings.voice);
}

TextToSpeech *TextToSpeech::self()
{
    static TextToSpeech s_self;
    return &s_self;
}

void TextToSpeech::initialize()
{
    if (!d->initialized) {
        reloadSettings();
    }
}

void TextToSpeech::slotAboutToSynthesize([[maybe_unused]] qsizetype id)
{
    const auto currentSpeechId = d->mCurrentTextToSpeechId;
    Q_EMIT aboutToSynthesize(currentSpeechId, ++d->mCurrentTextToSpeechId);
}

void TextToSpeech::slotStateChanged()
{
    TextToSpeech::State state = TextToSpeech::State::Unknown;
    switch (d->mTextToSpeech->state()) {
    case QTextToSpeech::Ready:
        state = TextToSpeech::Ready;
        break;
    case QTextToSpeech::Speaking:
        state = TextToSpeech::Speaking;
        break;
    case QTextToSpeech::Paused:
        state = TextToSpeech::Paused;
        break;
    case QTextToSpeech::Error:
        state = TextToSpeech::BackendError;
        break;
    case QTextToSpeech::Synthesizing:
        state = TextToSpeech::Synthesizing;
        break;
    }
    Q_EMIT stateChanged(state);
    if (state == TextToSpeech::Ready) {
        // Workaround for bug in qtextspeech
        d->mTextToSpeech->stop();
        Q_EMIT aboutToSynthesize(d->mCurrentTextToSpeechId, -1);
        // Reinitialize
        d->mCurrentTextToSpeechId = -1;
    }
}

bool TextToSpeech::isReady() const
{
    if (!d->mTextToSpeech) {
        return false;
    }
    return d->mTextToSpeech->state() != QTextToSpeech::Error;
}

void TextToSpeech::say(const QString &text)
{
    initialize();
    d->mTextToSpeech->say(text);
}

qsizetype TextToSpeech::enqueue(const QString &text)
{
    initialize();
    return d->mTextToSpeech->enqueue(text);
}

void TextToSpeech::stop()
{
    initialize();
    d->mTextToSpeech->stop();
}

void TextToSpeech::pause()
{
    initialize();
    d->mTextToSpeech->pause();
}

void TextToSpeech::resume()
{
    initialize();
    d->mTextToSpeech->resume();
}

void TextToSpeech::setRate(double rate)
{
    initialize();
    d->mTextToSpeech->setRate(rate);
}

void TextToSpeech::setPitch(double pitch)
{
    initialize();
    d->mTextToSpeech->setPitch(pitch);
}

void TextToSpeech::setVolume(double volume)
{
    initialize();
    d->mTextToSpeech->setVolume(volume);
}

double TextToSpeech::volume() const
{
    return d->mTextToSpeech ? d->mTextToSpeech->volume() : -1.0;
}

QVector<QLocale> TextToSpeech::availableLocales() const
{
    return d->mTextToSpeech ? d->mTextToSpeech->availableLocales() : QVector<QLocale>();
}

QStringList TextToSpeech::availableVoices() const
{
    QStringList lst;
    const QVector<QVoice> voices = d->mTextToSpeech ? d->mTextToSpeech->availableVoices() : QVector<QVoice>();
    lst.reserve(voices.count());
    for (const QVoice &voice : voices) {
        lst << voice.name();
    }
    return lst;
}

QStringList TextToSpeech::availableEngines() const
{
    return QTextToSpeech::availableEngines();
}

void TextToSpeech::setLocale(const QLocale &locale)
{
    initialize();
    d->mTextToSpeech->setLocale(locale);
}

QLocale TextToSpeech::locale() const
{
    return d->mTextToSpeech ? d->mTextToSpeech->locale() : QLocale();
}

#include "moc_texttospeech.cpp"