KTextAddons

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

KDE's Doxygen guidelines are available online.