~neon/kmouth/master

1 by Gunnar Schmidt
Imported KMouth into kdeaccessibility
1
/***************************************************************************
486.1.3 by Jeremy Whiting
Updated copyright headers and add missing ones.
2
 *   Copyright (C) 2002 by Gunnar Schmi Dt <kmouth@schmi-dt.de             *
660 by Jeremy Whiting
Add engine and voice selection to KMouth default system.
3
 *             (C) 2015, 2022 by Jeremy Whiting <jpwhiting@kde.org>        *
1 by Gunnar Schmidt
Imported KMouth into kdeaccessibility
4
 *                                                                         *
5
 *   This program is free software; you can redistribute it and/or modify  *
6
 *   it under the terms of the GNU General Public License as published by  *
7
 *   the Free Software Foundation; either version 2 of the License, or     *
8
 *   (at your option) any later version.                                   *
9
 *                                                                         *
486.1.3 by Jeremy Whiting
Updated copyright headers and add missing ones.
10
 *   This program is distributed in the hope that it will be useful,       *
11
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13
 *   GNU General Public License for more details.                          *
14
 *                                                                         *
15
 *   You should have received a copy of the GNU General Public License     *
16
 *   along with this program; if not, write to the                         *
17
 *   Free Software Foundation, Inc.,                                       *
18
 *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.          *
1 by Gunnar Schmidt
Imported KMouth into kdeaccessibility
19
 ***************************************************************************/
20
21
#include "texttospeechsystem.h"
489 by Jeremy Whiting
Cleanup headers and bump version to 1.2.0
22
23
#include <QTextCodec>
487 by Jeremy Whiting
Initial port to kf5/qt5/QtSpeech.
24
#include <QTextToSpeech>
14 by Gunnar Schmidt
Added kttsd (Proklam) support and some small changes
25
495 by Jeremy Whiting
Port from KConfig to KSharedConfig.
26
#include <KSharedConfig>
489 by Jeremy Whiting
Cleanup headers and bump version to 1.2.0
27
#include <KConfigGroup>
28
1 by Gunnar Schmidt
Imported KMouth into kdeaccessibility
29
#include "speech.h"
30
647 by Laurent Montel
Minor optimization. Remove unused code
31
TextToSpeechSystem::TextToSpeechSystem(QObject *parent)
661 by Jeremy Whiting
Fix items gitlab noticed as code degradation.
32
    : QObject(parent),
33
      codec(Speech::Local),
34
      stdIn(true),
35
      useQtSpeech(true),
36
      ttsEngine(QLatin1String("speechd")),
37
      m_speech(new QTextToSpeech(ttsEngine))
484.1.19 by Jeremy Whiting
Restyle with astyle as per kdelibs coding style.
38
{
39
    buildCodecList();
40
}
41
42
TextToSpeechSystem::~TextToSpeechSystem()
43
{
44
    delete codecList;
487 by Jeremy Whiting
Initial port to kf5/qt5/QtSpeech.
45
    delete m_speech;
484.1.19 by Jeremy Whiting
Restyle with astyle as per kdelibs coding style.
46
}
47
48
void TextToSpeechSystem::speak(const QString &text, const QString &language)
49
{
50
    if (text.length() > 0) {
487 by Jeremy Whiting
Initial port to kf5/qt5/QtSpeech.
51
        if (useQtSpeech) {
52
            m_speech->say(text);
53
            return;
484.1.19 by Jeremy Whiting
Restyle with astyle as per kdelibs coding style.
54
        }
55
56
        if (codec < Speech::UseCodec)
572 by Laurent Montel
Fix some clazy warning
57
            (new Speech())->speak(ttsCommand, stdIn, text, language, codec, nullptr);
484.1.19 by Jeremy Whiting
Restyle with astyle as per kdelibs coding style.
58
        else
59
            (new Speech())->speak(ttsCommand, stdIn, text, language, Speech::UseCodec,
60
                                  codecList->at(codec - Speech::UseCodec));
61
    }
62
}
63
495 by Jeremy Whiting
Port from KConfig to KSharedConfig.
64
void TextToSpeechSystem::readOptions(const QString &langGroup)
484.1.19 by Jeremy Whiting
Restyle with astyle as per kdelibs coding style.
65
{
495 by Jeremy Whiting
Port from KConfig to KSharedConfig.
66
    KConfigGroup cg(KSharedConfig::openConfig(), langGroup);
484.1.19 by Jeremy Whiting
Restyle with astyle as per kdelibs coding style.
67
    ttsCommand = cg.readPathEntry("Command", QString());
68
    stdIn = cg.readEntry("StdIn", true);
487 by Jeremy Whiting
Initial port to kf5/qt5/QtSpeech.
69
    useQtSpeech = cg.readEntry("useQtSpeech", true);
660 by Jeremy Whiting
Add engine and voice selection to KMouth default system.
70
    ttsEngine = cg.readEntry("ttsEngine", "speechd");
71
    // No default, depends on current locale, etc. so just naturally
72
    // select first voice if none set by user.
73
    ttsVoice = cg.readEntry("ttsVoice", "");
484.1.19 by Jeremy Whiting
Restyle with astyle as per kdelibs coding style.
74
75
    QString codecString = cg.readEntry("Codec", "Local");
76
    if (codecString == QLatin1String("Local"))
77
        codec = Speech::Local;
78
    else if (codecString == QLatin1String("Latin1"))
79
        codec = Speech::Latin1;
80
    else if (codecString == QLatin1String("Unicode"))
81
        codec = Speech::Unicode;
82
    else {
83
        codec = Speech::Local;
84
        for (int i = 0; i < codecList->count(); i++)
85
            if (codecString == QLatin1String(codecList->at(i)->name()))
86
                codec = Speech::UseCodec + i;
87
    }
88
}
89
495 by Jeremy Whiting
Port from KConfig to KSharedConfig.
90
void TextToSpeechSystem::saveOptions(const QString &langGroup)
484.1.19 by Jeremy Whiting
Restyle with astyle as per kdelibs coding style.
91
{
495 by Jeremy Whiting
Port from KConfig to KSharedConfig.
92
    KConfigGroup cg(KSharedConfig::openConfig(), langGroup);
484.1.19 by Jeremy Whiting
Restyle with astyle as per kdelibs coding style.
93
    cg.writePathEntry("Command", ttsCommand);
94
    cg.writeEntry("StdIn", stdIn);
487 by Jeremy Whiting
Initial port to kf5/qt5/QtSpeech.
95
    cg.writeEntry("useQtSpeech", useQtSpeech);
660 by Jeremy Whiting
Add engine and voice selection to KMouth default system.
96
    cg.writeEntry("ttsEngine", ttsEngine);
97
    cg.writeEntry("ttsVoice", ttsVoice);
484.1.19 by Jeremy Whiting
Restyle with astyle as per kdelibs coding style.
98
    if (codec == Speech::Local)
99
        cg.writeEntry("Codec", "Local");
100
    else if (codec == Speech::Latin1)
101
        cg.writeEntry("Codec", "Latin1");
102
    else if (codec == Speech::Unicode)
103
        cg.writeEntry("Codec", "Unicode");
104
    else {
105
        QString codeName = QLatin1String(codecList->at(codec - Speech::UseCodec)->name());
106
        cg.writeEntry("Codec", codeName);
107
    }
660 by Jeremy Whiting
Add engine and voice selection to KMouth default system.
108
109
    delete m_speech;
110
    m_speech = new QTextToSpeech(ttsEngine);
111
    const QVector<QVoice> voices = m_speech->availableVoices();
112
    for (const QVoice &voice: voices) {
113
        if (voice.name() == ttsVoice) {
114
            m_speech->setVoice(voice);
115
        }
116
    }
484.1.19 by Jeremy Whiting
Restyle with astyle as per kdelibs coding style.
117
}
118
119
void TextToSpeechSystem::buildCodecList()
120
{
121
    codecList = new QList<QTextCodec*>;
122
    QList<QByteArray> availableCodecs = QTextCodec::availableCodecs();
123
    for (int i = 0; i < availableCodecs.count(); ++i) {
124
        QTextCodec *codec = QTextCodec::codecForName(availableCodecs[i]);
125
        codecList->append(codec);
126
    }
1 by Gunnar Schmidt
Imported KMouth into kdeaccessibility
127
}
128