KTextAddons

texttospeech.cpp
1/*
2 SPDX-FileCopyrightText: 2014-2025 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "texttospeech.h"
8#include "texttospeechutil.h"
9
10#include <KConfig>
11#include <QLocale>
12#include <QTextToSpeech>
13#include <QVector>
14
15class TextEditTextToSpeech::TextToSpeechPrivate
16{
17public:
18 QString mDefaultEngine;
19 QTextToSpeech *mTextToSpeech = nullptr;
20};
21
22using namespace TextEditTextToSpeech;
23TextToSpeech::TextToSpeech(QObject *parent)
24 : QObject(parent)
25 , d(new TextEditTextToSpeech::TextToSpeechPrivate)
26{
27 reloadSettings();
28}
29
30TextToSpeech::~TextToSpeech() = default;
31
32void TextToSpeech::reloadSettings()
33{
34 const TextEditTextToSpeech::TextToSpeechUtil::TextToSpeechSettings settings = TextEditTextToSpeech::TextToSpeechUtil::loadSettings();
35
36 const QString engineName = settings.engineName;
37 if (d->mDefaultEngine != engineName) {
38 if (d->mTextToSpeech) {
39 if (d->mTextToSpeech && (d->mTextToSpeech->engine() != engineName)) {
40 disconnect(d->mTextToSpeech, &QTextToSpeech::stateChanged, this, &TextToSpeech::slotStateChanged);
41 delete d->mTextToSpeech;
42 d->mTextToSpeech = nullptr;
43 }
44 }
45 }
46 if (!d->mTextToSpeech) {
47 d->mTextToSpeech = new QTextToSpeech(engineName, this);
48 connect(d->mTextToSpeech, &QTextToSpeech::stateChanged, this, &TextToSpeech::slotStateChanged);
49 }
50 d->mDefaultEngine = engineName;
51 const int rate = settings.rate;
52 const double rateDouble = rate / 100.0;
53 d->mTextToSpeech->setRate(rateDouble);
54 const int pitch = settings.pitch;
55 const double pitchDouble = pitch / 100.0;
56 d->mTextToSpeech->setPitch(pitchDouble);
57 const int volumeValue = settings.volumeValue;
58 const double volumeDouble = volumeValue / 100.0;
59 d->mTextToSpeech->setVolume(volumeDouble);
60 d->mTextToSpeech->setLocale(QLocale(settings.localeName));
61 // It doesn't have api for it d->mTextToSpeech->setVoice(grp.readEntry("voice"));
62}
63
64TextToSpeech *TextToSpeech::self()
65{
66 static TextToSpeech s_self;
67 return &s_self;
68}
69
70void TextToSpeech::slotStateChanged()
71{
72 TextToSpeech::State state;
73 switch (d->mTextToSpeech->state()) {
74 case QTextToSpeech::Ready:
75 state = TextToSpeech::Ready;
76 break;
77 case QTextToSpeech::Speaking:
78 state = TextToSpeech::Speaking;
79 break;
80 case QTextToSpeech::Paused:
81 state = TextToSpeech::Paused;
82 break;
83 case QTextToSpeech::Error:
84 state = TextToSpeech::BackendError;
85 break;
86 case QTextToSpeech::Synthesizing:
87 state = TextToSpeech::Synthesizing;
88 break;
89 }
90 Q_EMIT stateChanged(state);
91}
92
93bool TextToSpeech::isReady() const
94{
95 return d->mTextToSpeech->state() != QTextToSpeech::Error;
96}
97
98void TextToSpeech::say(const QString &text)
99{
100 d->mTextToSpeech->say(text);
101}
102
103void TextToSpeech::stop()
104{
105 d->mTextToSpeech->stop();
106}
107
108void TextToSpeech::pause()
109{
110 d->mTextToSpeech->pause();
111}
112
113void TextToSpeech::resume()
114{
115 d->mTextToSpeech->resume();
116}
117
118void TextToSpeech::setRate(double rate)
119{
120 d->mTextToSpeech->setRate(rate);
121}
122
123void TextToSpeech::setPitch(double pitch)
124{
125 d->mTextToSpeech->setPitch(pitch);
126}
127
128void TextToSpeech::setVolume(double volume)
129{
130 d->mTextToSpeech->setVolume(volume);
131}
132
133double TextToSpeech::volume() const
134{
135 return d->mTextToSpeech->volume();
136}
137
138QVector<QLocale> TextToSpeech::availableLocales() const
139{
140 return d->mTextToSpeech->availableLocales();
141}
142
143QStringList TextToSpeech::availableVoices() const
144{
145 QStringList lst;
146 const QVector<QVoice> voices = d->mTextToSpeech->availableVoices();
147 lst.reserve(voices.count());
148 for (const QVoice &voice : voices) {
149 lst << voice.name();
150 }
151 return lst;
152}
153
154QStringList TextToSpeech::availableEngines() const
155{
156 return QTextToSpeech::availableEngines();
157}
158
159void TextToSpeech::setLocale(const QLocale &locale) const
160{
161 d->mTextToSpeech->setLocale(locale);
162}
163
164QLocale TextToSpeech::locale() const
165{
166 return d->mTextToSpeech->locale();
167}
168
169#include "moc_texttospeech.cpp"
The TextToSpeech class.
qsizetype count() const const
void reserve(qsizetype size)
Q_EMITQ_EMIT
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
bool disconnect(const QMetaObject::Connection &connection)
This file is part of the KDE documentation.
Documentation copyright © 1996-2025 The KDE developers.
Generated on Fri Jan 3 2025 11:46:56 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.